ummm, connect plz

master
Jordan Orelli 10 years ago
parent cb475af77a
commit f7b169ea74

@ -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)
}
}

@ -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) {

@ -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
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 {
error_log.Printf("unable to decode auth body: %v", err)
break
return fmt.Errorf("unable to decode auth body: %v", err)
}
// 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() {

Loading…
Cancel
Save