func routeNewIdentity(res http.ResponseWriter, req *http.Request) { log_request(req) dec := json.NewDecoder(req.Body) type identRequest struct { PrivateId string Passphrase string } var ir identRequest if err := dec.Decode(&ir); err != nil { respond(res, 400, err) return } if ir.PrivateId == "" || ir.Passphrase == "" { respond(res, 400, "There’s a blank field or two here.") return } peer := plong.FindPrivatePeer(ir.PrivateId) if peer.PublicId == "" { respond(res, 404, "No such peer.") return } peer.NewIdentity(ir.Passphrase) respond(res, 200, nil) }
func wsNewIdentity(c *Connection, args []string) { arg := []byte(strings.Join(args, " ")) type identRequest struct { PrivateId string Passphrase string } var ir identRequest if err := json.Unmarshal(arg, &ir); err != nil { wsJson(c, err, true) return } if ir.PrivateId == "" || ir.Passphrase == "" { wsJson(c, "There’s a blank field or two here.", true) return } peer := plong.FindPrivatePeer(ir.PrivateId) if peer.PublicId == "" { wsJson(c, "No such peer.", true) return } peer.NewIdentity(ir.Passphrase) }
func routeDelClient(res http.ResponseWriter, req *http.Request) { log_request(req) dec := json.NewDecoder(req.Body) type anid struct { PrivateId string } var j anid if err := dec.Decode(&j); err != nil { respond(res, 400, err) fmt.Println(err) return } peer := plong.FindPrivatePeer(j.PrivateId) peer.Destroy() respond(res, 200, nil) }
func wsHandler(ws *websocket.Conn) { url := strings.Split(fmt.Sprintf("%s", ws.LocalAddr()), "/") id := url[len(url)-1] peer := plong.FindPrivatePeer(id) if peer.PrivateId == "" { fmt.Printf("[WebSocket] Error: No such peer “%s”.\n", id) return } c := &Connection{send: make(chan string, BufferSize), ws: ws, peer: peer} wsHub.Add(c) fmt.Printf("[WebSocket] New connection: “%s”.\n", id) defer func() { wsHub.Remove(c) }() go c.Writer() c.Reader() }