diff options
-rw-r--r-- | main.go | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/main.go b/main.go index 57a77a6..2ebc6fa 100644 --- a/main.go +++ b/main.go @@ -16,24 +16,31 @@ var ( ) func main() { - log.Println("Starting SSH listener (:22)") + log.Println("Starting SSH listener") + + //// start the ssh server + //go func() { + // listenErr := ssh.ListenAndServe(":2222", nil, ssh.PasswordAuth(handlePass)) + // if listenErr != nil { + // log.Fatalln(listenErr.Error()) + // } + //}() go func() { // star the metrics listener - log.Println("Starting HTTP metrics listener (:8033)") + log.Println("Starting HTTP metrics listener") http.HandleFunc("/metrics", metricsHandler) - listenErr := http.ListenAndServe(":8033", nil) + listenErr := http.ListenAndServe(":8080", nil) if listenErr != nil { log.Fatalln(listenErr.Error()) } }() ssh.Handle(handleConnection) - log.Fatal(ssh.ListenAndServe(":22", nil, ssh.PasswordAuth(handlePass))) + log.Fatal(ssh.ListenAndServe(":2222", nil, ssh.PasswordAuth(handlePass))) } func handleConnection(s ssh.Session) { - log.Println("handling a connection!") cmd := exec.Command("bash") p, _ := pty.Start(cmd) @@ -46,28 +53,33 @@ func handleConnection(s ssh.Session) { commandBuffer := make([]byte, 0) // the current char - //var char string + var char string // read until ENTER is pressed - //for char != "\x0d"{ - for { + for char != "\x0d" { // read the char inserted by the user into the buffer _, readErr = s.Read(buf) + if len(bytes.Trim(buf, "\x00")) == 0 { + buf[0] = []byte("\x108\x105")[0] + } + // trim the char and append it to the commandBuffer - log.Printf("--> %v", bytes.Trim(buf, "\x00")) - char1 := bytes.Trim(buf, "\x00")[0] - log.Printf("%x -> %s", char1, char1) + currentChar := bytes.Trim(buf, "\x00")[0] + log.Println(currentChar) - if char1 == []byte("\x03")[0] { + // if <C-c> ist pressed, close the connection + if currentChar == []byte("\x03")[0] { s.Close() return } - commandBuffer = append(commandBuffer, char1) + + // append the current char to the command buffer + commandBuffer = append(commandBuffer, currentChar) // write the char to stdout - //char := string(bytes.Trim(buf, "\x00")) + char = string(bytes.Trim(buf, "\x00")) input := string(bytes.Trim(buf, "\x00")) io.WriteString(s, input) } @@ -80,7 +92,7 @@ func handleConnection(s ssh.Session) { // write the string to the commandHandler io.WriteString(p, filteredInput) - //s.Close() + s.Close() return } }() @@ -108,7 +120,5 @@ func handlePass(ctx ssh.Context, pass string) bool { // Handle HTTP /metrics requests func metricsHandler(w http.ResponseWriter, req *http.Request) { - log.Println("The metricsHandler was acessed") fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords) } - |