-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
149 lines (129 loc) · 2.63 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"isucdc.com/keyescrow/escrow"
"isucdc.com/keyescrow/server"
zmq "github.com/pebbe/zmq3"
)
type Client struct {
Requester *zmq.Socket
}
func NewClient(addr *net.TCPAddr) (*Client, error) {
req, err := zmq.NewSocket(zmq.REQ)
if err != nil {
return nil, err
}
url := fmt.Sprintf("tcp://%v", addr)
log.Printf("Connecting to %v", url)
err = req.Connect(url)
if err != nil {
return nil, err
}
client := &Client{
Requester: req,
}
return client, nil
}
func (c *Client) Dispatch(username string) error {
req := &server.Dispatch{
User: username,
}
err := req.Send(c.Requester)
if err != nil {
return err
}
recv, err := c.Requester.RecvBytes(0)
if err != nil {
return err
}
msg := server.RecvMsg(recv)
switch msg.(type) {
case server.Dispatch:
// Server re-sends dispatch as an ACK
return nil
case server.ErrorMessage:
m := msg.(server.ErrorMessage)
return errors.New(m.Message)
default:
return errors.New("Got unexpected response from server")
}
}
func (c *Client) GetKey(username string) (*escrow.Key, error) {
req := &server.KeyRequest{
User: username,
}
err := req.Send(c.Requester)
if err != nil {
return nil, err
}
recv, err := c.Requester.RecvBytes(0)
if err != nil {
return nil, err
}
msg := server.RecvMsg(recv)
switch msg.(type) {
case server.ErrorMessage:
m := msg.(server.ErrorMessage)
return nil, errors.New(fmt.Sprintf("The server returned an error: %v", m.Message))
case server.KeyResponse:
m := msg.(server.KeyResponse)
return &escrow.Key{
User: username,
PublicKey: m.PubKey,
PrivateKey: m.PrivKey,
}, nil
default:
return nil, errors.New("Got unexpected response from server")
}
}
func (c *Client) SetKey(user string, pubkey string, privkey string) error {
kr := &server.KeyResponse{
User: user,
}
pub, err := os.Open(pubkey)
if err != nil {
return err
}
data, err := ioutil.ReadAll(pub)
if err != nil {
return err
}
kr.PubKey = string(data)
priv, err := os.Open(privkey)
if err != nil {
return err
}
data, err = ioutil.ReadAll(priv)
if err != nil {
return err
}
kr.PrivKey = string(data)
err = kr.Send(c.Requester)
if err != nil {
return err
}
recv, err := c.Requester.RecvBytes(0)
if err != nil {
return err
}
msg := server.RecvMsg(recv)
switch msg.(type) {
case server.ErrorMessage:
m := msg.(server.ErrorMessage)
return errors.New(m.Message)
case server.KeyRequest:
log.Println("Successfully set key")
return nil
default:
return errors.New("Got unexpectd response from server")
}
return nil
}
func (c *Client) Close() {
c.Requester.Close()
}