ummm, connect plz

master
Jordan Orelli 10 years ago
parent cb475af77a
commit f7b169ea74

@ -1,9 +1,11 @@
package main package main
import ( import (
"code.google.com/p/go.crypto/ssh/terminal"
"crypto/rsa" "crypto/rsa"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net" "net"
"os" "os"
) )
@ -17,7 +19,15 @@ func (a *Auth) Kind() string {
return "auth" return "auth"
} }
type ReadWriter struct {
io.Reader
io.Writer
}
func connect() { func connect() {
if !terminal.IsTerminal(0) {
exit(1, "yeah you have to run this from a tty")
}
f, err := os.Open(options.key) f, err := os.Open(options.key)
if err != nil { if err != nil {
exit(1, "unable to open private key file at %s: %v", options.key, err) exit(1, "unable to open private key file at %s: %v", options.key, err)
@ -39,6 +49,21 @@ func connect() {
Key: key.PublicKey, Key: key.PublicKey,
} }
encodeRequest(conn, &auth) 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)
}
} }

@ -20,13 +20,16 @@ func encodeRequest(conn net.Conn, r request) {
if err != nil { if err != nil {
exit(1, "unable to encode client request body: %v", err) exit(1, "unable to encode client request body: %v", err)
} }
e := Envelope{ msg := json.RawMessage(b)
e := &Envelope{
Kind: r.Kind(), 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) exit(1, "unable to encode client request: %v", err)
} }
conn.Write(raw)
} }
func decodeRequest(conn net.Conn) (request, error) { func decodeRequest(conn net.Conn) (request, error) {

@ -1,36 +1,39 @@
package main package main
import ( import (
// "crypto/rsa"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net" "net"
) )
func handleConnection(conn net.Conn) { func authConnection(conn net.Conn) error {
defer conn.Close() var raw json.RawMessage
d := json.NewDecoder(conn) d := json.NewDecoder(conn)
if err := d.Decode(&raw); err != nil {
// var nick string return fmt.Errorf("unable to decode client request: %v", err)
// var key rsa.PublicKey }
var env Envelope var env Envelope
for { if err := json.Unmarshal(raw, &env); err != nil {
if err := d.Decode(&env); err != nil { return fmt.Errorf("man, fuck all this. %v", err)
error_log.Printf("unable to decode client request: %v", err) }
return switch env.Kind {
} case "auth":
switch env.Kind { var auth Auth
case "auth": if err := json.Unmarshal(env.Body, &auth); err != nil {
var auth Auth return fmt.Errorf("unable to decode auth body: %v", err)
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
} }
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() { func serve() {

Loading…
Cancel
Save