something about fixing notes

master
Jordan Orelli 10 years ago
parent 56bdc5e6b0
commit 728b9fa9fc

@ -20,7 +20,7 @@ import (
type Auth struct { type Auth struct {
Nick string Nick string
Key rsa.PublicKey Key *rsa.PublicKey
} }
func (a *Auth) Kind() string { func (a *Auth) Kind() string {
@ -85,6 +85,7 @@ func (c *Client) handleMessages() {
// handle a message received from the server // handle a message received from the server
func (c *Client) handleMessage(m Envelope) error { func (c *Client) handleMessage(m Envelope) error {
c.info("received response for message %d", m.Id)
res, ok := c.outstanding[m.Id] res, ok := c.outstanding[m.Id]
if !ok { if !ok {
c.info("%v", m) c.info("%v", m)
@ -160,9 +161,27 @@ func (c *Client) handleListNotes(raw json.RawMessage) error {
} }
func (c *Client) handshake() error { func (c *Client) handshake() error {
r := &Auth{Nick: c.nick, Key: c.key.PublicKey} r := &Auth{Nick: c.nick, Key: &c.key.PublicKey}
c.info("authenticating as %s", c.nick) c.info("authenticating as %s", c.nick)
_, err := c.sendRequest(r) promise, err := c.sendRequest(r)
if err != nil {
return err
}
res := <-promise
switch res.Kind {
case "error":
var e ErrorDoc
if err := json.Unmarshal(res.Body, &e); err != nil {
return fmt.Errorf("cannot read server error: %v", err)
}
c.err("server error: %v", e.Error())
close(c.done)
case "bool":
c.info(string(res.Body))
default:
c.err("i dunno what to do with this")
close(c.done)
}
return err return err
} }

@ -91,7 +91,7 @@ func getUserDB(nick string, create bool) (*userdb, error) {
} }
opts := &opt.Options{ opts := &opt.Options{
ErrorIfMissing: create, ErrorIfMissing: !create,
} }
path := fmt.Sprintf("./%s.db", nick) path := fmt.Sprintf("./%s.db", nick)
conn, err := leveldb.OpenFile(path, opts) conn, err := leveldb.OpenFile(path, opts)

@ -0,0 +1,13 @@
package main
import ()
type ErrorDoc string
func (e ErrorDoc) Kind() string {
return "error"
}
func (e ErrorDoc) Error() string {
return string(e)
}

@ -8,9 +8,9 @@ import (
) )
type Envelope struct { type Envelope struct {
Id int Id int `json:"id"`
Kind string Kind string `json:"kind"`
Body json.RawMessage Body json.RawMessage `json:"body"`
} }
type Bool bool type Bool bool

@ -31,7 +31,7 @@ func stream(r io.Reader, c chan Envelope, e chan error, done chan interface{}) {
type serverConnection struct { type serverConnection struct {
conn net.Conn conn net.Conn
nick string nick string
key rsa.PublicKey key *rsa.PublicKey
db *userdb db *userdb
} }
@ -68,9 +68,14 @@ func (s *serverConnection) handleAuthRequest(requestId int, body json.RawMessage
if err := json.Unmarshal(body, &auth); err != nil { if err := json.Unmarshal(body, &auth); err != nil {
return fmt.Errorf("bad auth request: %v", err) return fmt.Errorf("bad auth request: %v", err)
} }
if auth.Nick == "" {
return fmt.Errorf("empty username")
}
s.nick = auth.Nick s.nick = auth.Nick
if auth.Key == nil {
return fmt.Errorf("empty key")
}
s.key = auth.Key s.key = auth.Key
// s.sendMeta("hello, %s", auth.Nick)
if err := s.openDB(); err != nil { if err := s.openDB(); err != nil {
return fmt.Errorf("failed to open user database: %v", err) return fmt.Errorf("failed to open user database: %v", err)
} }
@ -79,36 +84,29 @@ func (s *serverConnection) handleAuthRequest(requestId int, body json.RawMessage
case leveldb.ErrNotFound: case leveldb.ErrNotFound:
keybytes, err := json.Marshal(auth.Key) keybytes, err := json.Marshal(auth.Key)
if err != nil { if err != nil {
error_log.Printf("motherfucking bullshit fuck shit fuck: %v", err) return fmt.Errorf("cannot marshal auth key: %v", err)
break
} }
if err := s.db.Put([]byte("public_key"), keybytes, nil); err != nil { if err := s.db.Put([]byte("public_key"), keybytes, nil); err != nil {
error_log.Printf("man fuck all this stupid key bullshit i hate it: %v", err) return fmt.Errorf("cannot write public key to database: %v", err)
break
} }
info_log.Printf("saved key for user %s", auth.Nick) info_log.Printf("saved key for user %s", auth.Nick)
case nil: case nil:
var key rsa.PublicKey var key rsa.PublicKey
if err := json.Unmarshal(b, &key); err != nil { if err := json.Unmarshal(b, &key); err != nil {
error_log.Printf("ok no i can't even do this key unmarshal shit: %v", err) return fmt.Errorf("cannot unmarshal auth key from request: %v", err)
break
} }
if auth.Key.E != key.E { if auth.Key.E != key.E {
error_log.Printf("that's totally the wrong key! hang up. just hang up.") return fmt.Errorf("client presented wrong auth key")
// todo: make there be a way to hang up lol
break
} }
if auth.Key.N.Cmp(key.N) != 0 { if auth.Key.N.Cmp(key.N) != 0 {
error_log.Printf("that's totally the wrong key! hang up. just hang up.") return fmt.Errorf("client presented wrong auth key")
// todo: make there be a way to hang up lol
break
} }
info_log.Printf("ok the key checks out.")
default: default:
error_log.Printf("unable to get public key for user %s: %v", auth.Nick, err) return fmt.Errorf("unable to read public key: %v", err)
} }
info_log.Printf("%s", b) info_log.Printf("%s", b)
info_log.Printf("authenticated user %s", auth.Nick) info_log.Printf("authenticated user %s", auth.Nick)
s.sendResponse(requestId, Bool(true))
return nil return nil
} }
@ -145,14 +143,11 @@ func (s *serverConnection) handleGetNoteRequest(requestId int, body json.RawMess
if err != nil { if err != nil {
return fmt.Errorf("couldn't retrieve note: %v", err) return fmt.Errorf("couldn't retrieve note: %v", err)
} }
raw, err := json.Marshal(&Envelope{Kind: "note", Body: b}) var note EncryptedNote
if err != nil { if err := json.Unmarshal(b, &note); err != nil {
return fmt.Errorf("couldn't send note back to client: %v", err) return fmt.Errorf("couldn't unmarshal note: %v", err)
} }
if _, err := s.conn.Write(raw); err != nil { return s.sendResponse(requestId, note)
return fmt.Errorf("couldn't send note back to client: %v", err)
}
return nil
} }
func (s *serverConnection) handleListNotesRequest(requestId int, body json.RawMessage) error { func (s *serverConnection) handleListNotesRequest(requestId int, body json.RawMessage) error {
@ -307,6 +302,7 @@ func (s *serverConnection) run() {
case request := <-requests: case request := <-requests:
if err := s.handleRequest(request); err != nil { if err := s.handleRequest(request); err != nil {
error_log.Printf("client error: %v", err) error_log.Printf("client error: %v", err)
s.sendResponse(request.Id, ErrorDoc(err.Error()))
} }
case err := <-errors: case err := <-errors:
error_log.Printf("connection error: %v", err) error_log.Printf("connection error: %v", err)

Loading…
Cancel
Save