|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +// For example: |
| 8 | +// we must a execute some command |
| 9 | +// so before that we must to create new terminal session |
| 10 | +// and provide our user name and command |
| 11 | +func main() { |
| 12 | + // Create new instance of Proxy terminal |
| 13 | + t, err := NewTerminal("gopher") |
| 14 | + if err != nil { |
| 15 | + // panic: User cant be empty |
| 16 | + // Or |
| 17 | + // panic: You (badUser) are not allowed to use terminal and execute commands |
| 18 | + panic(err.Error()) |
| 19 | + } |
| 20 | + |
| 21 | + // Execute user command |
| 22 | + excResp, excErr := t.Execute("say_hi") // Proxy prints to STDOUT -> PROXY: Intercepted execution of user (gopher), asked command (say_hi) |
| 23 | + if excErr != nil { |
| 24 | + fmt.Printf("ERROR: %s\n", excErr.Error()) // Prints: ERROR: I know only how to execute commands: say_hi, man |
| 25 | + } |
| 26 | + |
| 27 | + // Show execution response |
| 28 | + fmt.Println(excResp) // Prints: gopher@go_term$: Hi gopher |
| 29 | +} |
| 30 | + |
| 31 | +/* |
| 32 | + From that it's can be different terminals realizations with different methods, propertys, yda yda... |
| 33 | +*/ |
| 34 | + |
| 35 | +// ITerminal is interface, it's a public method whose implemented in Terminal(Proxy) and Gopher Terminal |
| 36 | +type ITerminal interface { |
| 37 | + Execute(cmd string) (resp string, err error) |
| 38 | +} |
| 39 | + |
| 40 | +// GopherTerminal for example: |
| 41 | +// Its a "huge" structure with different public methods |
| 42 | +type GopherTerminal struct { |
| 43 | + // user is a current authorized user |
| 44 | + User string |
| 45 | +} |
| 46 | + |
| 47 | +// Execute just runs known commands for current authorized user |
| 48 | +func (gt *GopherTerminal) Execute(cmd string) (resp string, err error) { |
| 49 | + // Set "terminal" prefix for output |
| 50 | + prefix := fmt.Sprintf("%s@go_term$:", gt.User) |
| 51 | + |
| 52 | + // Execute some asked commands if we know them |
| 53 | + switch cmd { |
| 54 | + case "say_hi": |
| 55 | + resp = fmt.Sprintf("%s Hi %s", prefix, gt.User) |
| 56 | + case "man": |
| 57 | + resp = fmt.Sprintf("%s Visit 'https://golang.org/doc/' for Golang documentation", prefix) |
| 58 | + default: |
| 59 | + err = fmt.Errorf("%s Unknown command", prefix) |
| 60 | + } |
| 61 | + |
| 62 | + return |
| 63 | +} |
| 64 | + |
| 65 | +/* |
| 66 | + And now we will create owr proxy to deliver user and commands to specific objects |
| 67 | +*/ |
| 68 | + |
| 69 | +// Terminal is a implementation of Proxy, it's validates and sends data to GopherTerminal |
| 70 | +// As example before send commands, user must be authorized |
| 71 | +type Terminal struct { |
| 72 | + currentUser string |
| 73 | + gopherTerminal *GopherTerminal |
| 74 | +} |
| 75 | + |
| 76 | +// NewTerminal creates new instance of terminal |
| 77 | +func NewTerminal(user string) (t *Terminal, err error) { |
| 78 | + // Check user if given correctly |
| 79 | + if user == "" { |
| 80 | + err = fmt.Errorf("User cant be empty") |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + // Before we execute user commands, we validate current user, if he have rights to do it |
| 85 | + if authErr := authorizeUser(user); authErr != nil { |
| 86 | + err = fmt.Errorf("You (%s) are not allowed to use terminal and execute commands", user) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + // Create new instance of terminal and set valid user |
| 91 | + t = &Terminal{currentUser: user} |
| 92 | + |
| 93 | + return |
| 94 | +} |
| 95 | + |
| 96 | +// Execute intercepts execution of command, implements authorizing user, validates it and |
| 97 | +// poxing command to real terminal (gopherTerminal) method |
| 98 | +func (t *Terminal) Execute(command string) (resp string, err error) { |
| 99 | + // If user allowed to execute send commands then, for example we can decide which terminal can be used, remote or local etc.. |
| 100 | + // but for example we just creating new instance of terminal, |
| 101 | + // set current user and send user command to execution in terminal |
| 102 | + t.gopherTerminal = &GopherTerminal{User: t.currentUser} |
| 103 | + |
| 104 | + // For example our proxy can log or output intercepted execution... etc |
| 105 | + fmt.Printf("PROXY: Intercepted execution of user (%s), asked command (%s)\n", t.currentUser, command) |
| 106 | + |
| 107 | + // Transfer data to original object and execute command |
| 108 | + if resp, err = t.gopherTerminal.Execute(command); err != nil { |
| 109 | + err = fmt.Errorf("I know only how to execute commands: say_hi, man") |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + return |
| 114 | +} |
| 115 | + |
| 116 | +// authorize validates user right to execute commands |
| 117 | +func authorizeUser(user string) (err error) { |
| 118 | + // As we use terminal like proxy, then |
| 119 | + // we will intercept user name to validate if it's allowed to execute commands |
| 120 | + if user != "gopher" { |
| 121 | + // Do some logs, notifications etc... |
| 122 | + err = fmt.Errorf("User %s in black list", user) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + return |
| 127 | +} |
0 commit comments