]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
6988a057 JH |
4 | "context" |
5 | "encoding/binary" | |
6 | "errors" | |
7 | "fmt" | |
8 | "go.uber.org/zap" | |
9 | "io" | |
16a4ad70 | 10 | "io/fs" |
6988a057 | 11 | "io/ioutil" |
6988a057 JH |
12 | "math/big" |
13 | "math/rand" | |
14 | "net" | |
15 | "os" | |
16 | "path" | |
17 | "path/filepath" | |
18 | "runtime/debug" | |
19 | "sort" | |
20 | "strings" | |
21 | "sync" | |
22 | "time" | |
23 | ||
0197c3f5 | 24 | "gopkg.in/yaml.v3" |
6988a057 JH |
25 | ) |
26 | ||
27 | const ( | |
28 | userIdleSeconds = 300 // time in seconds before an inactive user is marked idle | |
29 | idleCheckInterval = 10 // time in seconds to check for idle users | |
30 | trackerUpdateFrequency = 300 // time in seconds between tracker re-registration | |
31 | ) | |
32 | ||
33 | type Server struct { | |
6988a057 JH |
34 | Port int |
35 | Accounts map[string]*Account | |
36 | Agreement []byte | |
37 | Clients map[uint16]*ClientConn | |
38 | FlatNews []byte | |
39 | ThreadedNews *ThreadedNews | |
40 | FileTransfers map[uint32]*FileTransfer | |
41 | Config *Config | |
42 | ConfigDir string | |
43 | Logger *zap.SugaredLogger | |
44 | PrivateChats map[uint32]*PrivateChat | |
45 | NextGuestID *uint16 | |
46 | TrackerPassID []byte | |
47 | Stats *Stats | |
48 | ||
49 | APIListener net.Listener | |
50 | FileListener net.Listener | |
51 | ||
aebc4d36 JH |
52 | // newsReader io.Reader |
53 | // newsWriter io.WriteCloser | |
6988a057 JH |
54 | |
55 | outbox chan Transaction | |
56 | ||
57 | mux sync.Mutex | |
58 | flatNewsMux sync.Mutex | |
59 | } | |
60 | ||
61 | type PrivateChat struct { | |
62 | Subject string | |
63 | ClientConn map[uint16]*ClientConn | |
64 | } | |
65 | ||
66 | func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error { | |
67 | s.Logger.Infow("Hotline server started", "version", VERSION) | |
68 | var wg sync.WaitGroup | |
69 | ||
70 | wg.Add(1) | |
71 | go func() { s.Logger.Fatal(s.Serve(ctx, cancelRoot, s.APIListener)) }() | |
72 | ||
73 | wg.Add(1) | |
74 | go func() { s.Logger.Fatal(s.ServeFileTransfers(s.FileListener)) }() | |
75 | ||
76 | wg.Wait() | |
77 | ||
78 | return nil | |
79 | } | |
80 | ||
81 | func (s *Server) APIPort() int { | |
82 | return s.APIListener.Addr().(*net.TCPAddr).Port | |
83 | } | |
84 | ||
85 | func (s *Server) ServeFileTransfers(ln net.Listener) error { | |
86 | s.Logger.Infow("Hotline file transfer server started", "Addr", fmt.Sprintf(":%v", s.Port+1)) | |
87 | ||
88 | for { | |
89 | conn, err := ln.Accept() | |
90 | if err != nil { | |
91 | return err | |
92 | } | |
93 | ||
94 | go func() { | |
85767504 | 95 | if err := s.handleFileTransfer(conn); err != nil { |
6988a057 JH |
96 | s.Logger.Errorw("file transfer error", "reason", err) |
97 | } | |
98 | }() | |
99 | } | |
100 | } | |
101 | ||
102 | func (s *Server) sendTransaction(t Transaction) error { | |
103 | requestNum := binary.BigEndian.Uint16(t.Type) | |
104 | clientID, err := byteToInt(*t.clientID) | |
105 | if err != nil { | |
106 | return err | |
107 | } | |
108 | ||
109 | s.mux.Lock() | |
110 | client := s.Clients[uint16(clientID)] | |
111 | s.mux.Unlock() | |
112 | if client == nil { | |
bd1ce113 | 113 | return fmt.Errorf("invalid client id %v", *t.clientID) |
6988a057 | 114 | } |
72dd37f1 | 115 | userName := string(client.UserName) |
6988a057 JH |
116 | login := client.Account.Login |
117 | ||
118 | handler := TransactionHandlers[requestNum] | |
119 | ||
72dd37f1 JH |
120 | b, err := t.MarshalBinary() |
121 | if err != nil { | |
122 | return err | |
123 | } | |
6988a057 | 124 | var n int |
72dd37f1 | 125 | if n, err = client.Connection.Write(b); err != nil { |
6988a057 JH |
126 | return err |
127 | } | |
128 | s.Logger.Debugw("Sent Transaction", | |
129 | "name", userName, | |
130 | "login", login, | |
131 | "IsReply", t.IsReply, | |
132 | "type", handler.Name, | |
133 | "sentBytes", n, | |
134 | "remoteAddr", client.Connection.RemoteAddr(), | |
135 | ) | |
136 | return nil | |
137 | } | |
138 | ||
139 | func (s *Server) Serve(ctx context.Context, cancelRoot context.CancelFunc, ln net.Listener) error { | |
140 | s.Logger.Infow("Hotline server started", "Addr", fmt.Sprintf(":%v", s.Port)) | |
141 | ||
142 | for { | |
143 | conn, err := ln.Accept() | |
144 | if err != nil { | |
145 | s.Logger.Errorw("error accepting connection", "err", err) | |
146 | } | |
147 | ||
148 | go func() { | |
149 | for { | |
150 | t := <-s.outbox | |
151 | go func() { | |
152 | if err := s.sendTransaction(t); err != nil { | |
153 | s.Logger.Errorw("error sending transaction", "err", err) | |
154 | } | |
155 | }() | |
156 | } | |
157 | }() | |
158 | go func() { | |
159 | if err := s.handleNewConnection(conn); err != nil { | |
160 | if err == io.EOF { | |
161 | s.Logger.Infow("Client disconnected", "RemoteAddr", conn.RemoteAddr()) | |
162 | } else { | |
163 | s.Logger.Errorw("error serving request", "RemoteAddr", conn.RemoteAddr(), "err", err) | |
164 | } | |
165 | } | |
166 | }() | |
167 | } | |
168 | } | |
169 | ||
170 | const ( | |
c7e932c0 | 171 | agreementFile = "Agreement.txt" |
6988a057 JH |
172 | ) |
173 | ||
174 | // NewServer constructs a new Server from a config dir | |
175 | func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredLogger) (*Server, error) { | |
176 | server := Server{ | |
177 | Port: netPort, | |
178 | Accounts: make(map[string]*Account), | |
179 | Config: new(Config), | |
180 | Clients: make(map[uint16]*ClientConn), | |
181 | FileTransfers: make(map[uint32]*FileTransfer), | |
182 | PrivateChats: make(map[uint32]*PrivateChat), | |
183 | ConfigDir: configDir, | |
184 | Logger: logger, | |
185 | NextGuestID: new(uint16), | |
186 | outbox: make(chan Transaction), | |
187 | Stats: &Stats{StartTime: time.Now()}, | |
188 | ThreadedNews: &ThreadedNews{}, | |
189 | TrackerPassID: make([]byte, 4), | |
190 | } | |
191 | ||
192 | ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", netInterface, netPort)) | |
193 | if err != nil { | |
194 | return nil, err | |
195 | } | |
196 | server.APIListener = ln | |
197 | ||
198 | if netPort != 0 { | |
199 | netPort += 1 | |
200 | } | |
201 | ||
202 | ln2, err := net.Listen("tcp", fmt.Sprintf("%s:%v", netInterface, netPort)) | |
203 | server.FileListener = ln2 | |
204 | if err != nil { | |
205 | return nil, err | |
206 | } | |
207 | ||
208 | // generate a new random passID for tracker registration | |
209 | if _, err := rand.Read(server.TrackerPassID); err != nil { | |
210 | return nil, err | |
211 | } | |
212 | ||
213 | server.Logger.Debugw("Loading Agreement", "path", configDir+agreementFile) | |
214 | if server.Agreement, err = os.ReadFile(configDir + agreementFile); err != nil { | |
215 | return nil, err | |
216 | } | |
217 | ||
218 | if server.FlatNews, err = os.ReadFile(configDir + "MessageBoard.txt"); err != nil { | |
219 | return nil, err | |
220 | } | |
221 | ||
222 | if err := server.loadThreadedNews(configDir + "ThreadedNews.yaml"); err != nil { | |
223 | return nil, err | |
224 | } | |
225 | ||
226 | if err := server.loadConfig(configDir + "config.yaml"); err != nil { | |
227 | return nil, err | |
228 | } | |
229 | ||
230 | if err := server.loadAccounts(configDir + "Users/"); err != nil { | |
231 | return nil, err | |
232 | } | |
233 | ||
234 | server.Config.FileRoot = configDir + "Files/" | |
235 | ||
236 | *server.NextGuestID = 1 | |
237 | ||
238 | if server.Config.EnableTrackerRegistration { | |
239 | go func() { | |
240 | for { | |
241 | tr := TrackerRegistration{ | |
242 | Port: []byte{0x15, 0x7c}, | |
243 | UserCount: server.userCount(), | |
244 | PassID: server.TrackerPassID, | |
245 | Name: server.Config.Name, | |
246 | Description: server.Config.Description, | |
247 | } | |
248 | for _, t := range server.Config.Trackers { | |
249 | server.Logger.Infof("Registering with tracker %v", t) | |
250 | ||
251 | if err := register(t, tr); err != nil { | |
252 | server.Logger.Errorw("unable to register with tracker %v", "error", err) | |
253 | } | |
254 | } | |
255 | ||
256 | time.Sleep(trackerUpdateFrequency * time.Second) | |
257 | } | |
258 | }() | |
259 | } | |
260 | ||
261 | // Start Client Keepalive go routine | |
262 | go server.keepaliveHandler() | |
263 | ||
264 | return &server, nil | |
265 | } | |
266 | ||
267 | func (s *Server) userCount() int { | |
268 | s.mux.Lock() | |
269 | defer s.mux.Unlock() | |
270 | ||
271 | return len(s.Clients) | |
272 | } | |
273 | ||
274 | func (s *Server) keepaliveHandler() { | |
275 | for { | |
276 | time.Sleep(idleCheckInterval * time.Second) | |
277 | s.mux.Lock() | |
278 | ||
279 | for _, c := range s.Clients { | |
61c272e1 JH |
280 | c.IdleTime += idleCheckInterval |
281 | if c.IdleTime > userIdleSeconds && !c.Idle { | |
6988a057 JH |
282 | c.Idle = true |
283 | ||
284 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags))) | |
285 | flagBitmap.SetBit(flagBitmap, userFlagAway, 1) | |
286 | binary.BigEndian.PutUint16(*c.Flags, uint16(flagBitmap.Int64())) | |
287 | ||
288 | c.sendAll( | |
289 | tranNotifyChangeUser, | |
290 | NewField(fieldUserID, *c.ID), | |
291 | NewField(fieldUserFlags, *c.Flags), | |
72dd37f1 | 292 | NewField(fieldUserName, c.UserName), |
6988a057 JH |
293 | NewField(fieldUserIconID, *c.Icon), |
294 | ) | |
295 | } | |
296 | } | |
297 | s.mux.Unlock() | |
298 | } | |
299 | } | |
300 | ||
301 | func (s *Server) writeThreadedNews() error { | |
302 | s.mux.Lock() | |
303 | defer s.mux.Unlock() | |
304 | ||
305 | out, err := yaml.Marshal(s.ThreadedNews) | |
306 | if err != nil { | |
307 | return err | |
308 | } | |
309 | err = ioutil.WriteFile( | |
310 | s.ConfigDir+"ThreadedNews.yaml", | |
311 | out, | |
312 | 0666, | |
313 | ) | |
314 | return err | |
315 | } | |
316 | ||
317 | func (s *Server) NewClientConn(conn net.Conn) *ClientConn { | |
318 | s.mux.Lock() | |
319 | defer s.mux.Unlock() | |
320 | ||
321 | clientConn := &ClientConn{ | |
322 | ID: &[]byte{0, 0}, | |
323 | Icon: &[]byte{0, 0}, | |
324 | Flags: &[]byte{0, 0}, | |
72dd37f1 | 325 | UserName: []byte{}, |
6988a057 JH |
326 | Connection: conn, |
327 | Server: s, | |
328 | Version: &[]byte{}, | |
aebc4d36 | 329 | AutoReply: []byte{}, |
6988a057 | 330 | Transfers: make(map[int][]*FileTransfer), |
bd1ce113 | 331 | Agreed: false, |
6988a057 JH |
332 | } |
333 | *s.NextGuestID++ | |
334 | ID := *s.NextGuestID | |
335 | ||
6988a057 JH |
336 | binary.BigEndian.PutUint16(*clientConn.ID, ID) |
337 | s.Clients[ID] = clientConn | |
338 | ||
339 | return clientConn | |
340 | } | |
341 | ||
342 | // NewUser creates a new user account entry in the server map and config file | |
343 | func (s *Server) NewUser(login, name, password string, access []byte) error { | |
344 | s.mux.Lock() | |
345 | defer s.mux.Unlock() | |
346 | ||
347 | account := Account{ | |
348 | Login: login, | |
349 | Name: name, | |
350 | Password: hashAndSalt([]byte(password)), | |
351 | Access: &access, | |
352 | } | |
353 | out, err := yaml.Marshal(&account) | |
354 | if err != nil { | |
355 | return err | |
356 | } | |
357 | s.Accounts[login] = &account | |
358 | ||
481631f6 | 359 | return FS.WriteFile(s.ConfigDir+"Users/"+login+".yaml", out, 0666) |
6988a057 JH |
360 | } |
361 | ||
d2810ae9 JH |
362 | func (s *Server) UpdateUser(login, newLogin, name, password string, access []byte) error { |
363 | s.mux.Lock() | |
364 | defer s.mux.Unlock() | |
365 | ||
366 | fmt.Printf("login: %v, newLogin: %v: ", login, newLogin) | |
367 | ||
368 | // update renames the user login | |
369 | if login != newLogin { | |
370 | err := os.Rename(s.ConfigDir+"Users/"+login+".yaml", s.ConfigDir+"Users/"+newLogin+".yaml") | |
371 | if err != nil { | |
372 | return err | |
373 | } | |
374 | s.Accounts[newLogin] = s.Accounts[login] | |
375 | delete(s.Accounts, login) | |
376 | } | |
377 | ||
378 | account := s.Accounts[newLogin] | |
379 | account.Access = &access | |
380 | account.Name = name | |
381 | account.Password = password | |
382 | ||
383 | out, err := yaml.Marshal(&account) | |
384 | if err != nil { | |
385 | return err | |
386 | } | |
387 | if err := os.WriteFile(s.ConfigDir+"Users/"+newLogin+".yaml", out, 0666); err != nil { | |
388 | return err | |
389 | } | |
390 | ||
391 | return nil | |
392 | } | |
393 | ||
6988a057 JH |
394 | // DeleteUser deletes the user account |
395 | func (s *Server) DeleteUser(login string) error { | |
396 | s.mux.Lock() | |
397 | defer s.mux.Unlock() | |
398 | ||
399 | delete(s.Accounts, login) | |
400 | ||
003a743e | 401 | return FS.Remove(s.ConfigDir + "Users/" + login + ".yaml") |
6988a057 JH |
402 | } |
403 | ||
404 | func (s *Server) connectedUsers() []Field { | |
405 | s.mux.Lock() | |
406 | defer s.mux.Unlock() | |
407 | ||
408 | var connectedUsers []Field | |
c7e932c0 | 409 | for _, c := range sortedClients(s.Clients) { |
aebc4d36 | 410 | if !c.Agreed { |
bd1ce113 JH |
411 | continue |
412 | } | |
6988a057 JH |
413 | user := User{ |
414 | ID: *c.ID, | |
415 | Icon: *c.Icon, | |
416 | Flags: *c.Flags, | |
72dd37f1 | 417 | Name: string(c.UserName), |
6988a057 JH |
418 | } |
419 | connectedUsers = append(connectedUsers, NewField(fieldUsernameWithInfo, user.Payload())) | |
420 | } | |
421 | return connectedUsers | |
422 | } | |
423 | ||
424 | // loadThreadedNews loads the threaded news data from disk | |
425 | func (s *Server) loadThreadedNews(threadedNewsPath string) error { | |
426 | fh, err := os.Open(threadedNewsPath) | |
427 | if err != nil { | |
428 | return err | |
429 | } | |
430 | decoder := yaml.NewDecoder(fh) | |
6988a057 JH |
431 | |
432 | return decoder.Decode(s.ThreadedNews) | |
433 | } | |
434 | ||
435 | // loadAccounts loads account data from disk | |
436 | func (s *Server) loadAccounts(userDir string) error { | |
437 | matches, err := filepath.Glob(path.Join(userDir, "*.yaml")) | |
438 | if err != nil { | |
439 | return err | |
440 | } | |
441 | ||
442 | if len(matches) == 0 { | |
443 | return errors.New("no user accounts found in " + userDir) | |
444 | } | |
445 | ||
446 | for _, file := range matches { | |
d492c46d | 447 | fh, err := FS.Open(file) |
6988a057 JH |
448 | if err != nil { |
449 | return err | |
450 | } | |
451 | ||
452 | account := Account{} | |
453 | decoder := yaml.NewDecoder(fh) | |
6988a057 JH |
454 | if err := decoder.Decode(&account); err != nil { |
455 | return err | |
456 | } | |
457 | ||
458 | s.Accounts[account.Login] = &account | |
459 | } | |
460 | return nil | |
461 | } | |
462 | ||
463 | func (s *Server) loadConfig(path string) error { | |
d492c46d | 464 | fh, err := FS.Open(path) |
6988a057 JH |
465 | if err != nil { |
466 | return err | |
467 | } | |
468 | ||
469 | decoder := yaml.NewDecoder(fh) | |
6988a057 JH |
470 | err = decoder.Decode(s.Config) |
471 | if err != nil { | |
472 | return err | |
473 | } | |
474 | return nil | |
475 | } | |
476 | ||
477 | const ( | |
478 | minTransactionLen = 22 // minimum length of any transaction | |
479 | ) | |
480 | ||
481 | // handleNewConnection takes a new net.Conn and performs the initial login sequence | |
482 | func (s *Server) handleNewConnection(conn net.Conn) error { | |
483 | handshakeBuf := make([]byte, 12) // handshakes are always 12 bytes in length | |
484 | if _, err := conn.Read(handshakeBuf); err != nil { | |
485 | return err | |
486 | } | |
487 | if err := Handshake(conn, handshakeBuf[:12]); err != nil { | |
488 | return err | |
489 | } | |
490 | ||
491 | buf := make([]byte, 1024) | |
492 | readLen, err := conn.Read(buf) | |
493 | if readLen < minTransactionLen { | |
494 | return err | |
495 | } | |
496 | if err != nil { | |
497 | return err | |
498 | } | |
499 | ||
500 | clientLogin, _, err := ReadTransaction(buf[:readLen]) | |
501 | if err != nil { | |
502 | return err | |
503 | } | |
504 | ||
505 | c := s.NewClientConn(conn) | |
506 | defer c.Disconnect() | |
507 | defer func() { | |
508 | if r := recover(); r != nil { | |
509 | fmt.Println("stacktrace from panic: \n" + string(debug.Stack())) | |
510 | c.Server.Logger.Errorw("PANIC", "err", r, "trace", string(debug.Stack())) | |
511 | c.Disconnect() | |
512 | } | |
513 | }() | |
514 | ||
515 | encodedLogin := clientLogin.GetField(fieldUserLogin).Data | |
516 | encodedPassword := clientLogin.GetField(fieldUserPassword).Data | |
517 | *c.Version = clientLogin.GetField(fieldVersion).Data | |
518 | ||
519 | var login string | |
520 | for _, char := range encodedLogin { | |
521 | login += string(rune(255 - uint(char))) | |
522 | } | |
523 | if login == "" { | |
524 | login = GuestAccount | |
525 | } | |
526 | ||
527 | // If authentication fails, send error reply and close connection | |
528 | if !c.Authenticate(login, encodedPassword) { | |
72dd37f1 JH |
529 | t := c.NewErrReply(clientLogin, "Incorrect login.") |
530 | b, err := t.MarshalBinary() | |
531 | if err != nil { | |
532 | return err | |
533 | } | |
534 | if _, err := conn.Write(b); err != nil { | |
6988a057 JH |
535 | return err |
536 | } | |
537 | return fmt.Errorf("incorrect login") | |
538 | } | |
539 | ||
6988a057 | 540 | if clientLogin.GetField(fieldUserName).Data != nil { |
72dd37f1 | 541 | c.UserName = clientLogin.GetField(fieldUserName).Data |
6988a057 JH |
542 | } |
543 | ||
544 | if clientLogin.GetField(fieldUserIconID).Data != nil { | |
545 | *c.Icon = clientLogin.GetField(fieldUserIconID).Data | |
546 | } | |
547 | ||
548 | c.Account = c.Server.Accounts[login] | |
549 | ||
550 | if c.Authorize(accessDisconUser) { | |
551 | *c.Flags = []byte{0, 2} | |
552 | } | |
553 | ||
554 | s.Logger.Infow("Client connection received", "login", login, "version", *c.Version, "RemoteAddr", conn.RemoteAddr().String()) | |
555 | ||
556 | s.outbox <- c.NewReply(clientLogin, | |
557 | NewField(fieldVersion, []byte{0x00, 0xbe}), | |
558 | NewField(fieldCommunityBannerID, []byte{0x00, 0x01}), | |
559 | NewField(fieldServerName, []byte(s.Config.Name)), | |
560 | ) | |
561 | ||
562 | // Send user access privs so client UI knows how to behave | |
563 | c.Server.outbox <- *NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, *c.Account.Access)) | |
564 | ||
565 | // Show agreement to client | |
566 | c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement)) | |
567 | ||
bd1ce113 JH |
568 | // assume simplified hotline v1.2.3 login flow that does not require agreement |
569 | if *c.Version == nil { | |
570 | c.Agreed = true | |
003a743e JH |
571 | |
572 | c.notifyOthers( | |
573 | *NewTransaction( | |
574 | tranNotifyChangeUser, nil, | |
575 | NewField(fieldUserName, c.UserName), | |
576 | NewField(fieldUserID, *c.ID), | |
577 | NewField(fieldUserIconID, *c.Icon), | |
578 | NewField(fieldUserFlags, *c.Flags), | |
579 | ), | |
580 | ) | |
6988a057 | 581 | } |
bd1ce113 | 582 | |
6988a057 JH |
583 | c.Server.Stats.LoginCount += 1 |
584 | ||
585 | const readBuffSize = 1024000 // 1KB - TODO: what should this be? | |
6988a057 JH |
586 | tranBuff := make([]byte, 0) |
587 | tReadlen := 0 | |
588 | // Infinite loop where take action on incoming client requests until the connection is closed | |
589 | for { | |
590 | buf = make([]byte, readBuffSize) | |
591 | tranBuff = tranBuff[tReadlen:] | |
592 | ||
593 | readLen, err := c.Connection.Read(buf) | |
594 | if err != nil { | |
595 | return err | |
596 | } | |
597 | tranBuff = append(tranBuff, buf[:readLen]...) | |
598 | ||
599 | // We may have read multiple requests worth of bytes from Connection.Read. readTransactions splits them | |
600 | // into a slice of transactions | |
601 | var transactions []Transaction | |
602 | if transactions, tReadlen, err = readTransactions(tranBuff); err != nil { | |
603 | c.Server.Logger.Errorw("Error handling transaction", "err", err) | |
604 | } | |
605 | ||
606 | // iterate over all of the transactions that were parsed from the byte slice and handle them | |
607 | for _, t := range transactions { | |
608 | if err := c.handleTransaction(&t); err != nil { | |
609 | c.Server.Logger.Errorw("Error handling transaction", "err", err) | |
610 | } | |
611 | } | |
612 | } | |
613 | } | |
614 | ||
6988a057 JH |
615 | // NewTransactionRef generates a random ID for the file transfer. The Hotline client includes this ID |
616 | // in the file transfer request payload, and the file transfer server will use it to map the request | |
617 | // to a transfer | |
618 | func (s *Server) NewTransactionRef() []byte { | |
619 | transactionRef := make([]byte, 4) | |
620 | rand.Read(transactionRef) | |
621 | ||
622 | return transactionRef | |
623 | } | |
624 | ||
625 | func (s *Server) NewPrivateChat(cc *ClientConn) []byte { | |
626 | s.mux.Lock() | |
627 | defer s.mux.Unlock() | |
628 | ||
629 | randID := make([]byte, 4) | |
630 | rand.Read(randID) | |
631 | data := binary.BigEndian.Uint32(randID[:]) | |
632 | ||
633 | s.PrivateChats[data] = &PrivateChat{ | |
634 | Subject: "", | |
635 | ClientConn: make(map[uint16]*ClientConn), | |
636 | } | |
637 | s.PrivateChats[data].ClientConn[cc.uint16ID()] = cc | |
638 | ||
639 | return randID | |
640 | } | |
641 | ||
642 | const dlFldrActionSendFile = 1 | |
643 | const dlFldrActionResumeFile = 2 | |
644 | const dlFldrActionNextFile = 3 | |
645 | ||
85767504 JH |
646 | // handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection |
647 | func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error { | |
648 | defer func() { | |
649 | if err := conn.Close(); err != nil { | |
650 | s.Logger.Errorw("error closing connection", "error", err) | |
651 | } | |
652 | }() | |
6988a057 | 653 | |
2e7c03cf JH |
654 | txBuf := make([]byte, 16) |
655 | _, err := conn.Read(txBuf) | |
656 | if err != nil { | |
6988a057 JH |
657 | return err |
658 | } | |
659 | ||
df2735b2 | 660 | var t transfer |
2e7c03cf | 661 | _, err = t.Write(txBuf) |
6988a057 JH |
662 | if err != nil { |
663 | return err | |
664 | } | |
665 | ||
666 | transferRefNum := binary.BigEndian.Uint32(t.ReferenceNumber[:]) | |
667 | fileTransfer := s.FileTransfers[transferRefNum] | |
668 | ||
16a4ad70 JH |
669 | // delete single use transferRefNum |
670 | delete(s.FileTransfers, transferRefNum) | |
671 | ||
6988a057 JH |
672 | switch fileTransfer.Type { |
673 | case FileDownload: | |
92a7e455 JH |
674 | fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName) |
675 | if err != nil { | |
676 | return err | |
677 | } | |
6988a057 | 678 | |
16a4ad70 JH |
679 | var dataOffset int64 |
680 | if fileTransfer.fileResumeData != nil { | |
681 | dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.fileResumeData.ForkInfoList[0].DataSize[:])) | |
682 | } | |
683 | ||
684 | ffo, err := NewFlattenedFileObject(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName, dataOffset) | |
6988a057 JH |
685 | if err != nil { |
686 | return err | |
687 | } | |
688 | ||
85767504 | 689 | s.Logger.Infow("File download started", "filePath", fullFilePath, "transactionRef", fileTransfer.ReferenceNumber) |
6988a057 | 690 | |
d1cd6664 JH |
691 | if fileTransfer.options == nil { |
692 | // Start by sending flat file object to client | |
693 | if _, err := conn.Write(ffo.BinaryMarshal()); err != nil { | |
694 | return err | |
695 | } | |
6988a057 JH |
696 | } |
697 | ||
92a7e455 | 698 | file, err := FS.Open(fullFilePath) |
6988a057 JH |
699 | if err != nil { |
700 | return err | |
701 | } | |
702 | ||
703 | sendBuffer := make([]byte, 1048576) | |
16a4ad70 | 704 | var totalSent int64 |
6988a057 JH |
705 | for { |
706 | var bytesRead int | |
16a4ad70 JH |
707 | if bytesRead, err = file.ReadAt(sendBuffer, dataOffset+totalSent); err == io.EOF { |
708 | if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil { | |
709 | return err | |
710 | } | |
6988a057 JH |
711 | break |
712 | } | |
16a4ad70 JH |
713 | if err != nil { |
714 | return err | |
715 | } | |
716 | totalSent += int64(bytesRead) | |
6988a057 JH |
717 | |
718 | fileTransfer.BytesSent += bytesRead | |
719 | ||
6988a057 JH |
720 | if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil { |
721 | return err | |
722 | } | |
723 | } | |
724 | case FileUpload: | |
6988a057 | 725 | destinationFile := s.Config.FileRoot + ReadFilePath(fileTransfer.FilePath) + "/" + string(fileTransfer.FileName) |
16a4ad70 JH |
726 | tmpFile := destinationFile + ".incomplete" |
727 | ||
728 | file, err := effectiveFile(destinationFile) | |
729 | if errors.Is(err, fs.ErrNotExist) { | |
730 | file, err = FS.Create(tmpFile) | |
731 | if err != nil { | |
732 | return err | |
733 | } | |
6988a057 | 734 | } |
16a4ad70 JH |
735 | |
736 | defer func() { _ = file.Close() }() | |
6988a057 | 737 | |
85767504 | 738 | s.Logger.Infow("File upload started", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile) |
6988a057 | 739 | |
16a4ad70 JH |
740 | // TODO: replace io.Discard with a real file when ready to implement storing of resource fork data |
741 | if err := receiveFile(conn, file, io.Discard); err != nil { | |
742 | return err | |
743 | } | |
744 | ||
745 | if err := os.Rename(destinationFile+".incomplete", destinationFile); err != nil { | |
746 | return err | |
6988a057 | 747 | } |
85767504 JH |
748 | |
749 | s.Logger.Infow("File upload complete", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile) | |
6988a057 JH |
750 | case FolderDownload: |
751 | // Folder Download flow: | |
df2735b2 | 752 | // 1. Get filePath from the transfer |
6988a057 JH |
753 | // 2. Iterate over files |
754 | // 3. For each file: | |
755 | // Send file header to client | |
756 | // The client can reply in 3 ways: | |
757 | // | |
758 | // 1. If type is an odd number (unknown type?), or file download for the current file is completed: | |
759 | // client sends []byte{0x00, 0x03} to tell the server to continue to the next file | |
760 | // | |
761 | // 2. If download of a file is to be resumed: | |
762 | // client sends: | |
763 | // []byte{0x00, 0x02} // download folder action | |
764 | // [2]byte // Resume data size | |
765 | // []byte file resume data (see myField_FileResumeData) | |
766 | // | |
16a4ad70 | 767 | // 3. Otherwise, download of the file is requested and client sends []byte{0x00, 0x01} |
6988a057 JH |
768 | // |
769 | // When download is requested (case 2 or 3), server replies with: | |
770 | // [4]byte - file size | |
771 | // []byte - Flattened File Object | |
772 | // | |
773 | // After every file download, client could request next file with: | |
774 | // []byte{0x00, 0x03} | |
775 | // | |
776 | // This notifies the server to send the next item header | |
777 | ||
92a7e455 JH |
778 | fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName) |
779 | if err != nil { | |
780 | return err | |
781 | } | |
6988a057 JH |
782 | |
783 | basePathLen := len(fullFilePath) | |
784 | ||
85767504 | 785 | s.Logger.Infow("Start folder download", "path", fullFilePath, "ReferenceNumber", fileTransfer.ReferenceNumber) |
6988a057 | 786 | |
85767504 JH |
787 | nextAction := make([]byte, 2) |
788 | if _, err := conn.Read(nextAction); err != nil { | |
789 | return err | |
790 | } | |
6988a057 JH |
791 | |
792 | i := 0 | |
85767504 JH |
793 | err = filepath.Walk(fullFilePath+"/", func(path string, info os.FileInfo, err error) error { |
794 | if err != nil { | |
795 | return err | |
796 | } | |
6988a057 | 797 | i += 1 |
85767504 | 798 | subPath := path[basePathLen+1:] |
6988a057 JH |
799 | s.Logger.Infow("Sending fileheader", "i", i, "path", path, "fullFilePath", fullFilePath, "subPath", subPath, "IsDir", info.IsDir()) |
800 | ||
6988a057 JH |
801 | if i == 1 { |
802 | return nil | |
803 | } | |
804 | ||
85767504 JH |
805 | fileHeader := NewFileHeader(subPath, info.IsDir()) |
806 | ||
6988a057 JH |
807 | // Send the file header to client |
808 | if _, err := conn.Write(fileHeader.Payload()); err != nil { | |
809 | s.Logger.Errorf("error sending file header: %v", err) | |
810 | return err | |
811 | } | |
812 | ||
813 | // Read the client's Next Action request | |
85767504 | 814 | if _, err := conn.Read(nextAction); err != nil { |
6988a057 JH |
815 | return err |
816 | } | |
817 | ||
85767504 | 818 | s.Logger.Infow("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2])) |
6988a057 | 819 | |
16a4ad70 JH |
820 | var dataOffset int64 |
821 | ||
822 | switch nextAction[1] { | |
823 | case dlFldrActionResumeFile: | |
824 | // client asked to resume this file | |
825 | var frd FileResumeData | |
826 | // get size of resumeData | |
827 | if _, err := conn.Read(nextAction); err != nil { | |
828 | return err | |
829 | } | |
830 | ||
831 | resumeDataLen := binary.BigEndian.Uint16(nextAction) | |
832 | resumeDataBytes := make([]byte, resumeDataLen) | |
833 | if _, err := conn.Read(resumeDataBytes); err != nil { | |
834 | return err | |
835 | } | |
836 | ||
837 | if err := frd.UnmarshalBinary(resumeDataBytes); err != nil { | |
838 | return err | |
839 | } | |
840 | dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:])) | |
841 | case dlFldrActionNextFile: | |
842 | // client asked to skip this file | |
843 | return nil | |
844 | } | |
845 | ||
6988a057 JH |
846 | if info.IsDir() { |
847 | return nil | |
848 | } | |
849 | ||
850 | splitPath := strings.Split(path, "/") | |
6988a057 | 851 | |
16a4ad70 | 852 | ffo, err := NewFlattenedFileObject(strings.Join(splitPath[:len(splitPath)-1], "/"), nil, []byte(info.Name()), dataOffset) |
6988a057 JH |
853 | if err != nil { |
854 | return err | |
855 | } | |
856 | s.Logger.Infow("File download started", | |
857 | "fileName", info.Name(), | |
858 | "transactionRef", fileTransfer.ReferenceNumber, | |
6988a057 JH |
859 | "TransferSize", fmt.Sprintf("%x", ffo.TransferSize()), |
860 | ) | |
861 | ||
862 | // Send file size to client | |
863 | if _, err := conn.Write(ffo.TransferSize()); err != nil { | |
864 | s.Logger.Error(err) | |
865 | return err | |
866 | } | |
867 | ||
85767504 | 868 | // Send ffo bytes to client |
c5d9af5a | 869 | if _, err := conn.Write(ffo.BinaryMarshal()); err != nil { |
6988a057 JH |
870 | s.Logger.Error(err) |
871 | return err | |
872 | } | |
873 | ||
d492c46d | 874 | file, err := FS.Open(path) |
6988a057 JH |
875 | if err != nil { |
876 | return err | |
877 | } | |
878 | ||
16a4ad70 JH |
879 | // // Copy N bytes from file to connection |
880 | // _, err = io.CopyN(conn, file, int64(binary.BigEndian.Uint32(ffo.FlatFileDataForkHeader.DataSize[:]))) | |
881 | // if err != nil { | |
882 | // return err | |
883 | // } | |
884 | // file.Close() | |
885 | sendBuffer := make([]byte, 1048576) | |
886 | var totalSent int64 | |
887 | for { | |
888 | var bytesRead int | |
889 | if bytesRead, err = file.ReadAt(sendBuffer, dataOffset+totalSent); err == io.EOF { | |
890 | if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil { | |
891 | return err | |
892 | } | |
893 | break | |
894 | } | |
895 | if err != nil { | |
896 | panic(err) | |
897 | } | |
898 | totalSent += int64(bytesRead) | |
899 | ||
900 | fileTransfer.BytesSent += bytesRead | |
901 | ||
902 | if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil { | |
903 | return err | |
904 | } | |
85767504 | 905 | } |
6988a057 | 906 | |
85767504 | 907 | // TODO: optionally send resource fork header and resource fork data |
6988a057 | 908 | |
16a4ad70 | 909 | // Read the client's Next Action request. This is always 3, I think? |
85767504 JH |
910 | if _, err := conn.Read(nextAction); err != nil { |
911 | return err | |
6988a057 | 912 | } |
85767504 | 913 | |
16a4ad70 | 914 | return nil |
6988a057 JH |
915 | }) |
916 | ||
917 | case FolderUpload: | |
92a7e455 JH |
918 | dstPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName) |
919 | if err != nil { | |
920 | return err | |
921 | } | |
6988a057 JH |
922 | s.Logger.Infow( |
923 | "Folder upload started", | |
924 | "transactionRef", fileTransfer.ReferenceNumber, | |
6988a057 | 925 | "dstPath", dstPath, |
92a7e455 | 926 | "TransferSize", fmt.Sprintf("%x", fileTransfer.TransferSize), |
6988a057 JH |
927 | "FolderItemCount", fileTransfer.FolderItemCount, |
928 | ) | |
929 | ||
930 | // Check if the target folder exists. If not, create it. | |
92a7e455 | 931 | if _, err := FS.Stat(dstPath); os.IsNotExist(err) { |
92a7e455 | 932 | if err := FS.Mkdir(dstPath, 0777); err != nil { |
16a4ad70 | 933 | return err |
6988a057 JH |
934 | } |
935 | } | |
936 | ||
6988a057 JH |
937 | // Begin the folder upload flow by sending the "next file action" to client |
938 | if _, err := conn.Write([]byte{0, dlFldrActionNextFile}); err != nil { | |
939 | return err | |
940 | } | |
941 | ||
942 | fileSize := make([]byte, 4) | |
85767504 | 943 | readBuffer := make([]byte, 1024) |
16a4ad70 JH |
944 | |
945 | for i := 0; i < fileTransfer.ItemCount(); i++ { | |
946 | ||
85767504 JH |
947 | _, err := conn.Read(readBuffer) |
948 | if err != nil { | |
6988a057 JH |
949 | return err |
950 | } | |
951 | fu := readFolderUpload(readBuffer) | |
952 | ||
953 | s.Logger.Infow( | |
954 | "Folder upload continued", | |
955 | "transactionRef", fmt.Sprintf("%x", fileTransfer.ReferenceNumber), | |
6988a057 JH |
956 | "FormattedPath", fu.FormattedPath(), |
957 | "IsFolder", fmt.Sprintf("%x", fu.IsFolder), | |
c5d9af5a | 958 | "PathItemCount", binary.BigEndian.Uint16(fu.PathItemCount[:]), |
6988a057 JH |
959 | ) |
960 | ||
c5d9af5a | 961 | if fu.IsFolder == [2]byte{0, 1} { |
6988a057 | 962 | if _, err := os.Stat(dstPath + "/" + fu.FormattedPath()); os.IsNotExist(err) { |
6988a057 | 963 | if err := os.Mkdir(dstPath+"/"+fu.FormattedPath(), 0777); err != nil { |
16a4ad70 | 964 | return err |
6988a057 JH |
965 | } |
966 | } | |
967 | ||
968 | // Tell client to send next file | |
969 | if _, err := conn.Write([]byte{0, dlFldrActionNextFile}); err != nil { | |
6988a057 JH |
970 | return err |
971 | } | |
972 | } else { | |
16a4ad70 JH |
973 | nextAction := dlFldrActionSendFile |
974 | ||
975 | // Check if we have the full file already. If so, send dlFldrAction_NextFile to client to skip. | |
976 | _, err := os.Stat(dstPath + "/" + fu.FormattedPath()) | |
977 | if err != nil && !errors.Is(err, fs.ErrNotExist) { | |
6988a057 JH |
978 | return err |
979 | } | |
16a4ad70 JH |
980 | if err == nil { |
981 | nextAction = dlFldrActionNextFile | |
982 | } | |
6988a057 | 983 | |
16a4ad70 JH |
984 | // Check if we have a partial file already. If so, send dlFldrAction_ResumeFile to client to resume upload. |
985 | inccompleteFile, err := os.Stat(dstPath + "/" + fu.FormattedPath() + incompleteFileSuffix) | |
986 | if err != nil && !errors.Is(err, fs.ErrNotExist) { | |
85767504 | 987 | return err |
6988a057 | 988 | } |
16a4ad70 JH |
989 | if err == nil { |
990 | nextAction = dlFldrActionResumeFile | |
991 | } | |
6988a057 | 992 | |
16a4ad70 | 993 | fmt.Printf("Next Action: %v\n", nextAction) |
6988a057 | 994 | |
16a4ad70 | 995 | if _, err := conn.Write([]byte{0, uint8(nextAction)}); err != nil { |
85767504 JH |
996 | return err |
997 | } | |
998 | ||
16a4ad70 JH |
999 | switch nextAction { |
1000 | case dlFldrActionNextFile: | |
1001 | continue | |
1002 | case dlFldrActionResumeFile: | |
1003 | offset := make([]byte, 4) | |
1004 | binary.BigEndian.PutUint32(offset, uint32(inccompleteFile.Size())) | |
1005 | ||
1006 | file, err := os.OpenFile(dstPath+"/"+fu.FormattedPath()+incompleteFileSuffix, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | |
1007 | if err != nil { | |
1008 | return err | |
1009 | } | |
1010 | ||
1011 | fileResumeData := NewFileResumeData([]ForkInfoList{ | |
1012 | *NewForkInfoList(offset), | |
1013 | }) | |
1014 | ||
1015 | b, _ := fileResumeData.BinaryMarshal() | |
1016 | ||
1017 | bs := make([]byte, 2) | |
1018 | binary.BigEndian.PutUint16(bs, uint16(len(b))) | |
1019 | ||
1020 | if _, err := conn.Write(append(bs, b...)); err != nil { | |
1021 | return err | |
1022 | } | |
1023 | ||
1024 | if _, err := conn.Read(fileSize); err != nil { | |
1025 | return err | |
1026 | } | |
1027 | ||
1028 | if err := receiveFile(conn, file, ioutil.Discard); err != nil { | |
1029 | s.Logger.Error(err) | |
1030 | } | |
1031 | ||
1032 | err = os.Rename(dstPath+"/"+fu.FormattedPath()+".incomplete", dstPath+"/"+fu.FormattedPath()) | |
1033 | if err != nil { | |
1034 | return err | |
1035 | } | |
1036 | ||
1037 | case dlFldrActionSendFile: | |
1038 | if _, err := conn.Read(fileSize); err != nil { | |
1039 | return err | |
1040 | } | |
1041 | ||
1042 | filePath := dstPath + "/" + fu.FormattedPath() | |
1043 | s.Logger.Infow("Starting file transfer", "path", filePath, "fileNum", i+1, "totalFiles", "zz", "fileSize", binary.BigEndian.Uint32(fileSize)) | |
1044 | ||
1045 | newFile, err := FS.Create(filePath + ".incomplete") | |
1046 | if err != nil { | |
1047 | return err | |
1048 | } | |
1049 | ||
1050 | if err := receiveFile(conn, newFile, ioutil.Discard); err != nil { | |
1051 | s.Logger.Error(err) | |
1052 | } | |
1053 | _ = newFile.Close() | |
1054 | if err := os.Rename(filePath+".incomplete", filePath); err != nil { | |
1055 | return err | |
1056 | } | |
6988a057 JH |
1057 | } |
1058 | ||
1059 | // Tell client to send next file | |
1060 | if _, err := conn.Write([]byte{0, dlFldrActionNextFile}); err != nil { | |
6988a057 JH |
1061 | return err |
1062 | } | |
6988a057 JH |
1063 | } |
1064 | } | |
1065 | s.Logger.Infof("Folder upload complete") | |
1066 | } | |
1067 | ||
1068 | return nil | |
1069 | } | |
1070 | ||
6988a057 JH |
1071 | // sortedClients is a utility function that takes a map of *ClientConn and returns a sorted slice of the values. |
1072 | // The purpose of this is to ensure that the ordering of client connections is deterministic so that test assertions work. | |
1073 | func sortedClients(unsortedClients map[uint16]*ClientConn) (clients []*ClientConn) { | |
1074 | for _, c := range unsortedClients { | |
1075 | clients = append(clients, c) | |
1076 | } | |
1077 | sort.Sort(byClientID(clients)) | |
1078 | return clients | |
1079 | } |