-
Notifications
You must be signed in to change notification settings - Fork 24
/
rendezvous.go
232 lines (205 loc) · 5.17 KB
/
rendezvous.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2016 Company 0, LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/base64"
"fmt"
"path"
"strconv"
"time"
"github.com/companyzero/zkc/inidb"
"github.com/companyzero/zkc/rpc"
"github.com/companyzero/zkc/tools"
"github.com/davecgh/go-xdr/xdr2"
)
//func (z *ZKS) pruneRendezvous(rz *inidb.INIDB) error {
// r := rz.Records("")
// for k, v := range r {
// rzXDR, err := base64.StdEncoding.DecodeString(v)
// if err != nil {
// return fmt.Errorf("corrupt rendezvous record: %v", k)
// }
// rzRecord := rpc.Rendezvous{} // deliberate instantiate
// br := bytes.NewReader(rzXDR)
// _, err = xdr.Unmarshal(br, &rzRecord)
// if err != nil {
// return fmt.Errorf("could not unmarshal rendezvous "+
// "record: %v", k)
// }
//
// t := time.Now().Add(rzRecord.Expiration * time.Hour)
// // t, err := strconv.ParseInt(v, 10, 64)
// // if err != nil {
// // // token corrupt, remove from db and complain
// // z.Dbg(idApp, "corrupt token %v", k)
// // _ = rz.Del("", k)
// // continue
// // }
// // ts := time.Unix(t, 0)
// // if ts.Before(time.Now()) {
// // // token expired, remove from db
// // _ = rz.Del("", k)
// // continue
// // }
// }
//
//}
func (z *ZKS) handleRendezvousPull(writer chan *RPCWrapper,
msg rpc.Message, r rpc.RendezvousPull) error {
z.T(idRPC, "handleRendezvousPull tag %v", msg.Tag)
// always reply from here on out (provided non fatal error)
reply := RPCWrapper{
Message: rpc.Message{
Command: rpc.TaggedCmdRendezvousPullReply,
Tag: msg.Tag,
},
}
// default error
payload := rpc.RendezvousPullReply{
Error: "internal error, contact server administrator",
}
// open db
rz, err := inidb.New(path.Join(z.settings.Root, rendezvousFilename),
true, 10)
if err != nil && err != inidb.ErrCreated {
return fmt.Errorf("could not open rendezvous db: %v", err)
}
err = rz.Lock()
if err != nil {
return fmt.Errorf("could not lock rendezvous db: %v", err)
}
// not much error recovery to do on unlock
defer rz.Unlock()
// vars to deal with go bitching about goto
var (
rzXDR []byte
rzRecord rpc.Rendezvous
br *bytes.Reader
)
// get token
v, err := rz.Get("", r.Token)
if err != nil {
payload.Error = fmt.Sprintf("invalid PIN")
goto bad
}
// decode value
rzXDR, err = base64.StdEncoding.DecodeString(v)
if err != nil {
payload.Error = fmt.Sprintf("internal error base64decode")
goto bad
}
br = bytes.NewReader(rzXDR)
_, err = xdr.Unmarshal(br, &rzRecord)
if err != nil {
payload.Error = fmt.Sprintf("internal error unmarshal")
goto bad
}
// XXX check for expiration here
// setup reply
payload.Error = ""
payload.Token = r.Token
payload.Blob = rzRecord.Blob
bad:
reply.Payload = payload
writer <- &reply
return nil
}
// handleRendezvous handles all aspects of a Rendezvous message. This
// includes the client reply. Note that returning an error from this
// function will result in a closed connection.
func (z *ZKS) handleRendezvous(writer chan *RPCWrapper,
msg rpc.Message, r rpc.Rendezvous) error {
z.T(idRPC, "handleRendezvous tag %v", msg.Tag)
// always reply from here on out (provided non fatal error)
reply := RPCWrapper{
Message: rpc.Message{
Command: rpc.TaggedCmdRendezvousReply,
Tag: msg.Tag,
},
}
// default error
payload := rpc.RendezvousReply{
Error: "internal error, contact server administrator",
}
// do these declarations before goto to shut go compiler up
retry := 25
// open db
rz, err := inidb.New(path.Join(z.settings.Root, rendezvousFilename),
true, 10)
if err != nil && err != inidb.ErrCreated {
return fmt.Errorf("could not open rendezvous db: %v",
err)
}
err = rz.Lock()
if err != nil {
return fmt.Errorf("could not lock rendezvous db: %v", err)
}
//defer z.pruneRendezvous(rz) // kill all expired records
defer func() {
// save db back
err := rz.Save()
if err != nil {
z.Error(idApp, "could not save rendezvous db: %v", err)
}
err = rz.Unlock()
if err != nil {
z.Error(idApp, "could not unlock rendezvous db: %v",
err)
}
}()
// sanitize inputs
if len(r.Blob) > 4096 {
payload.Error = "invalid blob size"
goto bad
}
if exp, err := strconv.ParseUint(r.Expiration, 10, 64); err != nil ||
exp > 168 {
payload.Error = "invalid expiration"
goto bad
}
// store blob
for retry > 0 {
token, err := tools.RandomUint64()
if err != nil {
// out of entropy
time.Sleep(500 * time.Millisecond)
retry--
continue
}
token %= 1000000
tokenS := strconv.FormatUint(token, 10)
// get token
_, err = rz.Get("", tokenS)
if err == nil {
// duplicate
retry--
continue
}
// value = base64(xdr(TaggedCmdRendezvous))
var b bytes.Buffer
_, err = xdr.Marshal(&b, r)
if err != nil {
z.Error(idRPC, "handleRendezvous: could not marshal")
goto bad
}
err = rz.Set("", tokenS,
base64.StdEncoding.EncodeToString(b.Bytes()))
if err != nil {
// db error
retry--
z.Error(idRPC, "could not insert in rendezvous db: %v",
err)
continue
}
// success
payload.Error = ""
payload.Token = tokenS
break
}
bad:
reply.Payload = payload
writer <- &reply
return nil
}