clientpackets.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package remoteauth
  2. import (
  3. "crypto/x509"
  4. "encoding/base64"
  5. "fmt"
  6. )
  7. type clientPacket interface {
  8. send(client *Client) error
  9. }
  10. ///////////////////////////////////////////////////////////////////////////////
  11. // Heartbeat
  12. ///////////////////////////////////////////////////////////////////////////////
  13. type clientHeartbeat struct {
  14. OP string `json:"op"`
  15. }
  16. func (h *clientHeartbeat) send(client *Client) error {
  17. // make sure our op string is set
  18. h.OP = "heartbeat"
  19. client.heartbeats += 1
  20. if client.heartbeats > 2 {
  21. return fmt.Errorf("server failed to acknowledge our heartbeats")
  22. }
  23. return client.write(h)
  24. }
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // Init
  27. ///////////////////////////////////////////////////////////////////////////////
  28. type clientInit struct {
  29. OP string `json:"op"`
  30. EncodedPublicKey string `json:"encoded_public_key"`
  31. }
  32. func (i *clientInit) send(client *Client) error {
  33. i.OP = "init"
  34. pubkey := client.privateKey.Public()
  35. raw, err := x509.MarshalPKIXPublicKey(pubkey)
  36. if err != nil {
  37. return err
  38. }
  39. i.EncodedPublicKey = base64.RawStdEncoding.EncodeToString(raw)
  40. return client.write(i)
  41. }
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // NonceProof
  44. ///////////////////////////////////////////////////////////////////////////////
  45. type clientNonceProof struct {
  46. OP string `json:"op"`
  47. Proof string `json:"proof"`
  48. }
  49. func (n *clientNonceProof) send(client *Client) error {
  50. n.OP = "nonce_proof"
  51. // All of the other work was taken care of by the server packet as it knows
  52. // the payload.
  53. return client.write(n)
  54. }