-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
179 lines (156 loc) · 3.34 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"isucdc.com/keyescrow-client/escrow"
"isucdc.com/keyescrow-client/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)
err = req.Connect(url)
if err != nil {
return nil, err
}
client := &Client{
Requester: req,
}
return client, nil
}
func (c *Client) Dispatch(username string, token 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, token string) (*escrow.Key, error) {
req := &server.KeyRequest{
User: username,
Token: token,
}
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) GetToken(username string, password string) (*escrow.Token, error) {
req := &server.AuthRequest {
User: username,
Password: password,
}
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.AuthResponse:
m := msg.(server.AuthResponse)
return &escrow.Token{
User: username,
Token: m.Token,
}, nil
default:
return nil, errors.New("Got unexpected response from server")
}
}
func (c *Client) SetKey(user string, token string, pubkey string, privkey string) error {
kr := &server.KeyResponse{
User: user,
Token: token,
}
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:
return nil
default:
return errors.New("Got unexpectd response from server")
}
return nil
}
func (c *Client) Close() {
c.Requester.Close()
}