diff --git a/client.go b/client.go index 76583ad..4e22767 100644 --- a/client.go +++ b/client.go @@ -1,9 +1,11 @@ package main import ( + "code.google.com/p/go.crypto/ssh/terminal" "crypto/rsa" "encoding/json" "fmt" + "io" "net" "os" ) @@ -17,7 +19,15 @@ func (a *Auth) Kind() string { return "auth" } +type ReadWriter struct { + io.Reader + io.Writer +} + func connect() { + if !terminal.IsTerminal(0) { + exit(1, "yeah you have to run this from a tty") + } f, err := os.Open(options.key) if err != nil { exit(1, "unable to open private key file at %s: %v", options.key, err) @@ -39,6 +49,21 @@ func connect() { Key: key.PublicKey, } encodeRequest(conn, &auth) + old, err := terminal.MakeRaw(0) + if err != nil { + panic(err) + } + defer terminal.Restore(0, old) + r := &ReadWriter{Reader: os.Stdin, Writer: os.Stdout} + term := terminal.NewTerminal(r, "> ") - fmt.Println(conn) + line, err := term.ReadLine() + switch err { + case io.EOF: + return + case nil: + fmt.Println(line) + default: + exit(1, "error on line read: %v", err) + } } diff --git a/request.go b/request.go index f737c4d..f98b3b1 100644 --- a/request.go +++ b/request.go @@ -20,13 +20,16 @@ func encodeRequest(conn net.Conn, r request) { if err != nil { exit(1, "unable to encode client request body: %v", err) } - e := Envelope{ + msg := json.RawMessage(b) + e := &Envelope{ Kind: r.Kind(), - Body: b, + Body: msg, } - if err := json.NewEncoder(conn).Encode(e); err != nil { + raw, err := json.Marshal(e) + if err != nil { exit(1, "unable to encode client request: %v", err) } + conn.Write(raw) } func decodeRequest(conn net.Conn) (request, error) { diff --git a/server.go b/server.go index 88f6f06..7a3fd8a 100644 --- a/server.go +++ b/server.go @@ -1,36 +1,39 @@ package main import ( - // "crypto/rsa" "encoding/json" "fmt" "net" ) -func handleConnection(conn net.Conn) { - defer conn.Close() +func authConnection(conn net.Conn) error { + var raw json.RawMessage d := json.NewDecoder(conn) - - // var nick string - // var key rsa.PublicKey - + if err := d.Decode(&raw); err != nil { + return fmt.Errorf("unable to decode client request: %v", err) + } var env Envelope - for { - if err := d.Decode(&env); err != nil { - error_log.Printf("unable to decode client request: %v", err) - return - } - switch env.Kind { - case "auth": - var auth Auth - if err := json.Unmarshal(env.Body, &auth); err != nil { - error_log.Printf("unable to decode auth body: %v", err) - break - } - // nick = auth.Nick - // key = auth.Key + if err := json.Unmarshal(raw, &env); err != nil { + return fmt.Errorf("man, fuck all this. %v", err) + } + switch env.Kind { + case "auth": + var auth Auth + if err := json.Unmarshal(env.Body, &auth); err != nil { + return fmt.Errorf("unable to decode auth body: %v", err) } + info_log.Printf("authenticated user %s", auth.Nick) } + return nil +} + +func handleConnection(conn net.Conn) { + defer func() { + conn.Close() + info_log.Printf("connection ended: %v", conn.RemoteAddr()) + }() + info_log.Printf("connection start: %v", conn.RemoteAddr()) + authConnection(conn) } func serve() {