func HandleTerminalReading(channel ssh.Channel, term *terminal.Terminal) { defer channel.Close() for { line, err := term.ReadLine() if err != nil { break } cmd_log := Command{Cmd: string(line)} if strings.Contains(string(line), "exit") { logfile.Println("[exit requested]") channel.Close() } if line == "passwd" { line, _ := term.ReadPassword("Enter new UNIX password: "******"[password changed]: " + line) line, _ = term.ReadPassword("Retype new UNIX password: "******"[password changed confirmation]: " + line) term.Write([]byte("passwd: password updated successfully\r\n")) cmd_log.Cmd += " " + line } else { term.Write(RunCommand(line)) } cmd_log.Save() logfile.Println(line) } }
func updateTerminalSize(term *terminal.Terminal) { width, height, err := terminal.GetSize(0) if err != nil { return } term.SetSize(width, height) }
func HandleTcpReading(channel ssh.Channel, term *terminal.Terminal, http map[string]string) { defer channel.Close() for { line, err := term.ReadLine() if err != nil { break } logfile.Println(line) if line == "" { channel.Close() return } if strings.Contains(line, ":") { kv := strings.SplitAfterN(line, ":", 2) http[kv[0]] = strings.TrimSpace(kv[1]) } else { kv := strings.Fields(line) if kv[0] == "POST" || kv[0] == "GET" { http["Method"] = kv[0] http["URI"] = kv[1] } else { http[kv[0]] = kv[1] } } } }
// newStartedMongoState takes a term argument to have a context for printing func (msh *mongoStateHolder) newStartedMongoState(replicaID, backupID, mongodPath, mongoPath, mountPath string, term *terminal.Terminal, driver *strata.Driver) (*mongoState, error) { mongoState := mongoState{} var err error mongoState.dbpath, err = ioutil.TempDir("", "mongoq_") if err != nil { return &mongoState, err } if err := driver.RestoreReadOnly(replicaID, backupID, mountPath, mongoState.dbpath); err != nil { return &mongoState, err } // Try to start mongod // Look for output text to determine success // If output text indicates that port is already in use, try another port for mongoState.mongod == nil { mongoState.mongod = exec.Command(mongodPath, "--port="+strconv.Itoa(msh.nextPort), "--dbpath="+mongoState.dbpath, "--storageEngine=rocksdb", "--rocksdbConfigString=max_open_files=10") mongodOut, err := mongoState.mongod.StdoutPipe() if err != nil { return &mongoState, err } defer mongodOut.Close() if err := mongoState.mongod.Start(); err != nil { return &mongoState, err } // Wait until mongod is ready to accept a connection for { buf := make([]byte, 10000) n, _ := mongodOut.Read(buf) term.Write(buf[:n]) // If there is a problem starting mongod, the user should see it and kill process rec := string(buf[:n]) if strings.Contains(rec, "waiting for connections on port") { mongodOut.Close() break } else if strings.Contains(rec, "Address already in use for socket") { mongodOut.Close() if err := mongoState.mongod.Process.Kill(); err != nil { return &mongoState, err } mongoState.mongod = nil term.Write([]byte("MONGOQ Trying to start mongod again on another port\n")) msh.nextPort++ break } } } mongoState.mongo = exec.Command(mongoPath, "--port="+strconv.Itoa(msh.nextPort)) msh.nextPort++ mongoState.mongoPty, err = pty.Start(mongoState.mongo) return &mongoState, err }
func terminalMessage(term *terminal.Terminal, color []byte, msg string, critical bool) { line := make([]byte, len(msg)+16)[:0] line = append(line, ' ') line = append(line, color...) line = append(line, '*') line = append(line, term.Escape.Reset...) line = append(line, []byte(fmt.Sprintf(" (%s) ", time.Now().Format(time.Kitchen)))...) if critical { line = append(line, term.Escape.Red...) } line = appendTerminalEscaped(line, []byte(msg)) if critical { line = append(line, term.Escape.Reset...) } line = append(line, '\n') term.Write(line) }
func terminalMessage(term *terminal.Terminal, color []byte, msg string) { line := make([]byte, len(msg)+16)[:0] line = append(line, ' ') line = append(line, color...) line = append(line, '*') line = append(line, term.Escape.Reset...) line = append(line, []byte(fmt.Sprintf(" (%s) ", time.Now().Format(time.Kitchen)))...) for _, c := range msg { if (c < 32 || c > 126) && c != '\t' { line = append(line, '?') } else { line = append(line, byte(c)) } } line = append(line, '\n') term.Write(line) }
func HandleSshRequests(channel ssh.Channel, in <-chan *ssh.Request, term *terminal.Terminal) { for req := range in { ok := false logfile.Println("[request " + req.Type + "]: " + string(req.Payload)) switch req.Type { case "shell": // hacky way to get around presenting the correct prompt channel.Write([]byte("root@web1:/root# ")) term.SetPrompt("root@web1:/root# ") case "exec": term.SetPrompt("") fmt.Println(req) channel.Write(RunCommand(string(req.Payload[4:]))) // close after executing their one off command channel.Close() } /* this condition set and reply is needed to allow a PTY */ ok = true req.Reply(ok, nil) } }
func enroll(config *Config, term *terminal.Terminal) bool { var err error warn(term, "Enrolling new config file") var domain string for { term.SetPrompt("Account (i.e. [email protected], enter to quit): ") if config.Account, err = term.ReadLine(); err != nil || len(config.Account) == 0 { return false } parts := strings.SplitN(config.Account, "@", 2) if len(parts) != 2 { alert(term, "invalid username (want user@domain): "+config.Account) continue } domain = parts[1] break } term.SetPrompt("Enable debug logging to /tmp/xmpp-client-debug.log? ") if debugLog, err := term.ReadLine(); err != nil || debugLog != "yes" { info(term, "Not enabling debug logging...") } else { info(term, "Debug logging enabled...") config.RawLogFile = "/tmp/xmpp-client-debug.log" } term.SetPrompt("Use Tor?: ") if useTorQuery, err := term.ReadLine(); err != nil || useTorQuery != "yes" { info(term, "Not using Tor...") config.UseTor = false } else { info(term, "Using Tor...") config.UseTor = true } term.SetPrompt("File to import libotr private key from (enter to generate): ") var priv otr.PrivateKey for { importFile, err := term.ReadLine() if err != nil { return false } if len(importFile) > 0 { privKeyBytes, err := ioutil.ReadFile(importFile) if err != nil { alert(term, "Failed to open private key file: "+err.Error()) continue } if !priv.Import(privKeyBytes) { alert(term, "Failed to parse libotr private key file (the parser is pretty simple I'm afraid)") continue } break } else { info(term, "Generating private key...") priv.Generate(rand.Reader) break } } config.PrivateKey = priv.Serialize(nil) config.OTRAutoAppendTag = true config.OTRAutoStartSession = true config.OTRAutoTearDown = false // If we find ourselves here - we want to autoconfigure everything quickly if domain == "jabber.ccc.de" && config.UseTor == true { const torProxyURL = "socks5://127.0.0.1:9050" info(term, "It appears that you are using a well known server and we will use its Tor hidden service to connect.") config.Server = "okj7xc6j2szr2y75.onion" config.Port = 5222 config.Proxies = []string{torProxyURL} term.SetPrompt("> ") return true } if domain == "riseup.net" && config.UseTor == true { const torProxyURL = "socks5://127.0.0.1:9050" info(term, "It appears that you are using a well known server and we will use its Tor hidden service to connect.") config.Server = "ztmc4p37hvues222.onion" config.Port = 5222 config.Proxies = []string{torProxyURL} term.SetPrompt("> ") return true } var proxyStr string term.SetPrompt("Proxy (i.e socks5://127.0.0.1:9050, enter for none): ") for { if proxyStr, err = term.ReadLine(); err != nil { return false } if len(proxyStr) == 0 { break } u, err := url.Parse(proxyStr) if err != nil { alert(term, "Failed to parse "+proxyStr+" as a URL: "+err.Error()) continue } if _, err = proxy.FromURL(u, proxy.Direct); err != nil { alert(term, "Failed to parse "+proxyStr+" as a proxy: "+err.Error()) continue } break } if len(proxyStr) > 0 { config.Proxies = []string{proxyStr} info(term, "Since you selected a proxy, we need to know the server and port to connect to as a SRV lookup would leak information every time.") term.SetPrompt("Server (i.e. xmpp.example.com, enter to lookup using unproxied DNS): ") if config.Server, err = term.ReadLine(); err != nil { return false } if len(config.Server) == 0 { var port uint16 info(term, "Performing SRV lookup") if config.Server, port, err = xmpp.Resolve(domain); err != nil { alert(term, "SRV lookup failed: "+err.Error()) return false } config.Port = int(port) info(term, "Resolved "+config.Server+":"+strconv.Itoa(config.Port)) } else { for { term.SetPrompt("Port (enter for 5222): ") portStr, err := term.ReadLine() if err != nil { return false } if len(portStr) == 0 { portStr = "5222" } if config.Port, err = strconv.Atoi(portStr); err != nil || config.Port <= 0 || config.Port > 65535 { info(term, "Port numbers must be 0 < port <= 65535") continue } break } } } term.SetPrompt("> ") return true }
func readLine(shell *terminal.Terminal, t *testing.T) { if _, err := shell.ReadLine(); err != nil && err != io.EOF { t.Errorf("unable to read line: %v", err) } }