-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
322 lines (266 loc) · 7.02 KB
/
server.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"net"
"os"
"os/signal"
"sync/atomic"
"time"
"unicode/utf8"
log "github.com/sirupsen/logrus"
"github.com/h2non/filetype"
"github.com/tardisx/netgiv/secure"
)
type Server struct {
port int
authToken string
}
// An NGF is a Netgiv File
type NGF struct {
Id uint32
StorePath string
Filename string // could be empty string if we were not supplied with one
Kind string //
Size uint64 // file size
Timestamp time.Time
}
var ngfs []NGF
var globalId uint32
func (s *Server) Run() {
log.Infof("starting server on :%d", s.port)
address := fmt.Sprintf(":%d", s.port)
networkAddress, _ := net.ResolveTCPAddr("tcp", address)
listener, err := net.ListenTCP("tcp", networkAddress)
if err != nil {
log.Fatalf("error creating listener: %v", err)
}
ngfs = make([]NGF, 0)
go func() {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
<-sigchan
for _, ngf := range ngfs {
log.Printf("removing file: %s", ngf.StorePath)
err := os.Remove(ngf.StorePath)
if err != nil {
log.Printf("could not remove %s: %v", ngf.StorePath, err)
}
}
os.Exit(0)
}()
// start main program tasks
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Print(err)
}
go s.handleConnection(conn)
}
}
func (s *Server) handleConnection(conn *net.TCPConn) {
defer conn.Close()
conn.SetDeadline(time.Now().Add(time.Second * 5))
sharedKey := secure.Handshake(conn)
secureConnection := secure.SecureConnection{Conn: conn, SharedKey: sharedKey, Buffer: &bytes.Buffer{}}
gob.Register(secure.PacketStartRequest{})
gob.Register(secure.PacketSendDataStart{})
dec := gob.NewDecoder(&secureConnection)
enc := gob.NewEncoder(&secureConnection)
// Get the start packet
start := secure.PacketStartRequest{}
err := dec.Decode(&start)
if err == io.EOF {
log.Errorf("connection has been closed prematurely")
return
}
if err != nil {
log.Errorf("error while expecting PacketStart: %v", err)
return
}
// tell the client if the connection is ok.
startResponse := secure.PacketStartResponse{}
if start.ProtocolVersion != ProtocolVersion {
log.Errorf("bad protocol version")
startResponse.Response = secure.PacketStartResponseEnumWrongProtocol
enc.Encode(startResponse)
return
}
if start.AuthToken != s.authToken {
log.Errorf("bad authtoken")
startResponse.Response = secure.PacketStartResponseEnumBadAuthToken
enc.Encode(startResponse)
return
}
// otherwise we are good to continue, tell the client that
startResponse.Response = secure.PacketStartResponseEnumOK
enc.Encode(startResponse)
conn.SetDeadline(time.Now().Add(time.Second * 5))
if start.OperationType == secure.OperationTypeSend {
log.Debugf("file incoming")
sendStart := secure.PacketSendDataStart{}
err = dec.Decode(&sendStart)
if err != nil {
log.Errorf("error - expecting PacketSendDataStart: %v", err)
return
}
file, err := os.CreateTemp("", "netgiv_")
if err != nil {
log.Fatalf("can't open tempfile: %v", err)
}
defer file.Close()
ngf := NGF{
StorePath: file.Name(),
Filename: sendStart.Filename,
Kind: "",
Size: 0,
Id: atomic.AddUint32(&globalId, 1),
Timestamp: time.Now(),
}
if err != nil {
log.Errorf("got error with temp file: %v", err)
return
}
sendData := secure.PacketSendDataNext{}
determinedKind := false
for {
conn.SetDeadline(time.Now().Add(time.Second * 5))
err = dec.Decode(&sendData)
if err == io.EOF {
break
}
if err != nil {
log.Errorf("error while expecting PacketSendDataNext: %s", err)
return
}
// filetype.Match needs a few hundred bytes - I guess there is a chance
// we don't have enough in the very first packet? This might need rework.
if !determinedKind {
kind, _ := filetype.Match(sendData.Data)
if kind.MIME.Value == "" {
// this is pretty fragile. If our chunk boundary happens in the
// middle of an actual UTF-8 character, we will fail this test.
// However it's good for small chunks of text which fit in a
// single chunk, which I suspect to be a common use case.
if utf8.ValidString(string(sendData.Data)) {
ngf.Kind = "UTF-8 text"
}
} else {
ngf.Kind = kind.MIME.Value
}
determinedKind = true
}
file.Write(sendData.Data)
}
info, err := file.Stat()
if err != nil {
log.Errorf("couldn't stat file %s", err)
return
}
ngf.Size = uint64(info.Size())
file.Close()
ngfs = append(ngfs, ngf)
log.Printf("done receiving file: %v", ngf)
return
} else if start.OperationType == secure.OperationTypeReceive {
log.Printf("client requesting file receive")
// wait for them to send the request
req := secure.PacketReceiveDataStartRequest{}
err := dec.Decode(&req)
if err != nil {
log.Printf("error expecting PacketReceiveDataStartRequest: %v", err)
return
}
log.Debugf("The asked for %v", req)
// do we have this ngf by id?
var requestedNGF NGF
if len(ngfs) > 0 {
if req.Id == 0 {
// they want the most recent one
requestedNGF = ngfs[len(ngfs)-1]
} else {
for _, ngf := range ngfs {
if ngf.Id == req.Id {
requestedNGF = ngf
}
}
}
}
log.Debugf("going to deliver %v", requestedNGF)
if requestedNGF.Id == 0 {
// not found
log.Errorf("user requested %d, not found", req.Id)
res := secure.PacketReceiveDataStartResponse{
Status: secure.ReceiveDataStartResponseNotFound,
}
err = enc.Encode(res)
if err != nil {
log.Printf("could not send NotFound: %v", err)
}
return
}
res := secure.PacketReceiveDataStartResponse{
Status: secure.ReceiveDataStartResponseOK,
Filename: requestedNGF.Filename,
Kind: requestedNGF.Kind,
TotalSize: uint32(requestedNGF.Size),
}
err = enc.Encode(res)
if err != nil {
log.Errorf("error sending PacketReceiveDataStartResponse: %v", err)
return
}
// now just start sending the file in batches
buf := make([]byte, 2048)
filename := requestedNGF.StorePath
log.Debugf("opening %s", filename)
f, err := os.Open(filename)
if err != nil {
log.Errorf("could not find file %s: %v", filename, err)
return
}
for {
n, err := f.Read(buf)
eof := false
if err != nil && err != io.EOF {
log.Errorf("error reading data: %v", err)
break
}
if err == io.EOF {
eof = true
}
chunk := secure.PacketReceiveDataNext{
Size: uint16(n),
Data: buf[:n],
Last: eof,
}
err = enc.Encode(chunk)
if err != nil {
log.Errorf("error sending chunk: %v", err)
}
if eof {
break
}
}
log.Printf("sending done")
return
} else if start.OperationType == secure.OperationTypeList {
log.Debugf("client requesting file list")
for _, ngf := range ngfs {
p := secure.PacketListData{}
p.FileSize = uint32(ngf.Size)
p.Kind = ngf.Kind
p.Id = ngf.Id
p.Filename = ngf.Filename
p.Timestamp = ngf.Timestamp
enc.Encode(p)
}
log.Debugf("done sending list, closing connection")
return
} else {
log.Errorf("bad operation")
return
}
}