]> git.r.bdr.sh - rbdr/mobius/blob - hotline/server.go
e4232b6feb30ac2e36c92b40565216f84e8e218f
[rbdr/mobius] / hotline / server.go
1 package hotline
2
3 import (
4 "bufio"
5 "context"
6 "encoding/binary"
7 "errors"
8 "fmt"
9 "github.com/go-playground/validator/v10"
10 "go.uber.org/zap"
11 "gopkg.in/yaml.v3"
12 "io"
13 "io/fs"
14 "math/big"
15 "math/rand"
16 "net"
17 "os"
18 "path/filepath"
19 "strings"
20 "sync"
21 "time"
22 )
23
24 type contextKey string
25
26 var contextKeyReq = contextKey("req")
27
28 type requestCtx struct {
29 remoteAddr string
30 }
31
32 type Server struct {
33 NetInterface string
34 Port int
35 Accounts map[string]*Account
36 Agreement []byte
37 Clients map[uint16]*ClientConn
38 fileTransfers map[[4]byte]*FileTransfer
39
40 Config *Config
41 ConfigDir string
42 Logger *zap.SugaredLogger
43
44 PrivateChatsMu sync.Mutex
45 PrivateChats map[uint32]*PrivateChat
46
47 NextGuestID *uint16
48 TrackerPassID [4]byte
49
50 StatsMu sync.Mutex
51 Stats *Stats
52
53 FS FileStore // Storage backend to use for File storage
54
55 outbox chan Transaction
56 mux sync.Mutex
57
58 threadedNewsMux sync.Mutex
59 ThreadedNews *ThreadedNews
60
61 flatNewsMux sync.Mutex
62 FlatNews []byte
63
64 banListMU sync.Mutex
65 banList map[string]*time.Time
66 }
67
68 func (s *Server) CurrentStats() Stats {
69 s.StatsMu.Lock()
70 defer s.StatsMu.Unlock()
71
72 stats := s.Stats
73 stats.CurrentlyConnected = len(s.Clients)
74
75 return *stats
76 }
77
78 type PrivateChat struct {
79 Subject string
80 ClientConn map[uint16]*ClientConn
81 }
82
83 func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error {
84 s.Logger.Infow("Hotline server started",
85 "version", VERSION,
86 "API port", fmt.Sprintf("%s:%v", s.NetInterface, s.Port),
87 "Transfer port", fmt.Sprintf("%s:%v", s.NetInterface, s.Port+1),
88 )
89
90 var wg sync.WaitGroup
91
92 wg.Add(1)
93 go func() {
94 ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", s.NetInterface, s.Port))
95 if err != nil {
96 s.Logger.Fatal(err)
97 }
98
99 s.Logger.Fatal(s.Serve(ctx, ln))
100 }()
101
102 wg.Add(1)
103 go func() {
104 ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", s.NetInterface, s.Port+1))
105 if err != nil {
106 s.Logger.Fatal(err)
107 }
108
109 s.Logger.Fatal(s.ServeFileTransfers(ctx, ln))
110 }()
111
112 wg.Wait()
113
114 return nil
115 }
116
117 func (s *Server) ServeFileTransfers(ctx context.Context, ln net.Listener) error {
118 for {
119 conn, err := ln.Accept()
120 if err != nil {
121 return err
122 }
123
124 go func() {
125 defer func() { _ = conn.Close() }()
126
127 err = s.handleFileTransfer(
128 context.WithValue(ctx, contextKeyReq, requestCtx{
129 remoteAddr: conn.RemoteAddr().String(),
130 }),
131 conn,
132 )
133
134 if err != nil {
135 s.Logger.Errorw("file transfer error", "reason", err)
136 }
137 }()
138 }
139 }
140
141 func (s *Server) sendTransaction(t Transaction) error {
142 clientID, err := byteToInt(*t.clientID)
143 if err != nil {
144 return err
145 }
146
147 s.mux.Lock()
148 client := s.Clients[uint16(clientID)]
149 s.mux.Unlock()
150 if client == nil {
151 return fmt.Errorf("invalid client id %v", *t.clientID)
152 }
153
154 b, err := t.MarshalBinary()
155 if err != nil {
156 return err
157 }
158
159 _, err = client.Connection.Write(b)
160 if err != nil {
161 return err
162 }
163
164 return nil
165 }
166
167 func (s *Server) processOutbox() {
168 for {
169 t := <-s.outbox
170 go func() {
171 if err := s.sendTransaction(t); err != nil {
172 s.Logger.Errorw("error sending transaction", "err", err)
173 }
174 }()
175 }
176 }
177
178 func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
179 go s.processOutbox()
180
181 for {
182 conn, err := ln.Accept()
183 if err != nil {
184 s.Logger.Errorw("error accepting connection", "err", err)
185 }
186 connCtx := context.WithValue(ctx, contextKeyReq, requestCtx{
187 remoteAddr: conn.RemoteAddr().String(),
188 })
189
190 go func() {
191 s.Logger.Infow("Connection established", "RemoteAddr", conn.RemoteAddr())
192
193 defer conn.Close()
194 if err := s.handleNewConnection(connCtx, conn, conn.RemoteAddr().String()); err != nil {
195 if err == io.EOF {
196 s.Logger.Infow("Client disconnected", "RemoteAddr", conn.RemoteAddr())
197 } else {
198 s.Logger.Errorw("error serving request", "RemoteAddr", conn.RemoteAddr(), "err", err)
199 }
200 }
201 }()
202 }
203 }
204
205 const (
206 agreementFile = "Agreement.txt"
207 )
208
209 // NewServer constructs a new Server from a config dir
210 func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredLogger, fs FileStore) (*Server, error) {
211 server := Server{
212 NetInterface: netInterface,
213 Port: netPort,
214 Accounts: make(map[string]*Account),
215 Config: new(Config),
216 Clients: make(map[uint16]*ClientConn),
217 fileTransfers: make(map[[4]byte]*FileTransfer),
218 PrivateChats: make(map[uint32]*PrivateChat),
219 ConfigDir: configDir,
220 Logger: logger,
221 NextGuestID: new(uint16),
222 outbox: make(chan Transaction),
223 Stats: &Stats{Since: time.Now()},
224 ThreadedNews: &ThreadedNews{},
225 FS: fs,
226 banList: make(map[string]*time.Time),
227 }
228
229 var err error
230
231 // generate a new random passID for tracker registration
232 if _, err := rand.Read(server.TrackerPassID[:]); err != nil {
233 return nil, err
234 }
235
236 server.Agreement, err = os.ReadFile(filepath.Join(configDir, agreementFile))
237 if err != nil {
238 return nil, err
239 }
240
241 if server.FlatNews, err = os.ReadFile(filepath.Join(configDir, "MessageBoard.txt")); err != nil {
242 return nil, err
243 }
244
245 // try to load the ban list, but ignore errors as this file may not be present or may be empty
246 _ = server.loadBanList(filepath.Join(configDir, "Banlist.yaml"))
247
248 if err := server.loadThreadedNews(filepath.Join(configDir, "ThreadedNews.yaml")); err != nil {
249 return nil, err
250 }
251
252 if err := server.loadConfig(filepath.Join(configDir, "config.yaml")); err != nil {
253 return nil, err
254 }
255
256 if err := server.loadAccounts(filepath.Join(configDir, "Users/")); err != nil {
257 return nil, err
258 }
259
260 // If the FileRoot is an absolute path, use it, otherwise treat as a relative path to the config dir.
261 if !filepath.IsAbs(server.Config.FileRoot) {
262 server.Config.FileRoot = filepath.Join(configDir, server.Config.FileRoot)
263 }
264
265 *server.NextGuestID = 1
266
267 if server.Config.EnableTrackerRegistration {
268 server.Logger.Infow(
269 "Tracker registration enabled",
270 "frequency", fmt.Sprintf("%vs", trackerUpdateFrequency),
271 "trackers", server.Config.Trackers,
272 )
273
274 go func() {
275 for {
276 tr := &TrackerRegistration{
277 UserCount: server.userCount(),
278 PassID: server.TrackerPassID[:],
279 Name: server.Config.Name,
280 Description: server.Config.Description,
281 }
282 binary.BigEndian.PutUint16(tr.Port[:], uint16(server.Port))
283 for _, t := range server.Config.Trackers {
284 if err := register(t, tr); err != nil {
285 server.Logger.Errorw("unable to register with tracker %v", "error", err)
286 }
287 server.Logger.Debugw("Sent Tracker registration", "addr", t)
288 }
289
290 time.Sleep(trackerUpdateFrequency * time.Second)
291 }
292 }()
293 }
294
295 // Start Client Keepalive go routine
296 go server.keepaliveHandler()
297
298 return &server, nil
299 }
300
301 func (s *Server) userCount() int {
302 s.mux.Lock()
303 defer s.mux.Unlock()
304
305 return len(s.Clients)
306 }
307
308 func (s *Server) keepaliveHandler() {
309 for {
310 time.Sleep(idleCheckInterval * time.Second)
311 s.mux.Lock()
312
313 for _, c := range s.Clients {
314 c.IdleTime += idleCheckInterval
315 if c.IdleTime > userIdleSeconds && !c.Idle {
316 c.Idle = true
317
318 flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(c.Flags)))
319 flagBitmap.SetBit(flagBitmap, UserFlagAway, 1)
320 binary.BigEndian.PutUint16(c.Flags, uint16(flagBitmap.Int64()))
321
322 c.sendAll(
323 TranNotifyChangeUser,
324 NewField(FieldUserID, *c.ID),
325 NewField(FieldUserFlags, c.Flags),
326 NewField(FieldUserName, c.UserName),
327 NewField(FieldUserIconID, c.Icon),
328 )
329 }
330 }
331 s.mux.Unlock()
332 }
333 }
334
335 func (s *Server) writeBanList() error {
336 s.banListMU.Lock()
337 defer s.banListMU.Unlock()
338
339 out, err := yaml.Marshal(s.banList)
340 if err != nil {
341 return err
342 }
343 err = os.WriteFile(
344 filepath.Join(s.ConfigDir, "Banlist.yaml"),
345 out,
346 0666,
347 )
348 return err
349 }
350
351 func (s *Server) writeThreadedNews() error {
352 s.threadedNewsMux.Lock()
353 defer s.threadedNewsMux.Unlock()
354
355 out, err := yaml.Marshal(s.ThreadedNews)
356 if err != nil {
357 return err
358 }
359 err = s.FS.WriteFile(
360 filepath.Join(s.ConfigDir, "ThreadedNews.yaml"),
361 out,
362 0666,
363 )
364 return err
365 }
366
367 func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *ClientConn {
368 s.mux.Lock()
369 defer s.mux.Unlock()
370
371 clientConn := &ClientConn{
372 ID: &[]byte{0, 0},
373 Icon: []byte{0, 0},
374 Flags: []byte{0, 0},
375 UserName: []byte{},
376 Connection: conn,
377 Server: s,
378 Version: []byte{},
379 AutoReply: []byte{},
380 transfers: map[int]map[[4]byte]*FileTransfer{},
381 RemoteAddr: remoteAddr,
382 }
383 clientConn.transfers = map[int]map[[4]byte]*FileTransfer{
384 FileDownload: {},
385 FileUpload: {},
386 FolderDownload: {},
387 FolderUpload: {},
388 bannerDownload: {},
389 }
390
391 *s.NextGuestID++
392 ID := *s.NextGuestID
393
394 binary.BigEndian.PutUint16(*clientConn.ID, ID)
395 s.Clients[ID] = clientConn
396
397 return clientConn
398 }
399
400 // NewUser creates a new user account entry in the server map and config file
401 func (s *Server) NewUser(login, name, password string, access accessBitmap) error {
402 s.mux.Lock()
403 defer s.mux.Unlock()
404
405 account := Account{
406 Login: login,
407 Name: name,
408 Password: hashAndSalt([]byte(password)),
409 Access: access,
410 }
411 out, err := yaml.Marshal(&account)
412 if err != nil {
413 return err
414 }
415 s.Accounts[login] = &account
416
417 return s.FS.WriteFile(filepath.Join(s.ConfigDir, "Users", login+".yaml"), out, 0666)
418 }
419
420 func (s *Server) UpdateUser(login, newLogin, name, password string, access accessBitmap) error {
421 s.mux.Lock()
422 defer s.mux.Unlock()
423
424 // update renames the user login
425 if login != newLogin {
426 err := os.Rename(filepath.Join(s.ConfigDir, "Users", login+".yaml"), filepath.Join(s.ConfigDir, "Users", newLogin+".yaml"))
427 if err != nil {
428 return err
429 }
430 s.Accounts[newLogin] = s.Accounts[login]
431 delete(s.Accounts, login)
432 }
433
434 account := s.Accounts[newLogin]
435 account.Access = access
436 account.Name = name
437 account.Password = password
438
439 out, err := yaml.Marshal(&account)
440 if err != nil {
441 return err
442 }
443
444 if err := os.WriteFile(filepath.Join(s.ConfigDir, "Users", newLogin+".yaml"), out, 0666); err != nil {
445 return err
446 }
447
448 return nil
449 }
450
451 // DeleteUser deletes the user account
452 func (s *Server) DeleteUser(login string) error {
453 s.mux.Lock()
454 defer s.mux.Unlock()
455
456 delete(s.Accounts, login)
457
458 return s.FS.Remove(filepath.Join(s.ConfigDir, "Users", login+".yaml"))
459 }
460
461 func (s *Server) connectedUsers() []Field {
462 s.mux.Lock()
463 defer s.mux.Unlock()
464
465 var connectedUsers []Field
466 for _, c := range sortedClients(s.Clients) {
467 user := User{
468 ID: *c.ID,
469 Icon: c.Icon,
470 Flags: c.Flags,
471 Name: string(c.UserName),
472 }
473 connectedUsers = append(connectedUsers, NewField(FieldUsernameWithInfo, user.Payload()))
474 }
475 return connectedUsers
476 }
477
478 func (s *Server) loadBanList(path string) error {
479 fh, err := os.Open(path)
480 if err != nil {
481 return err
482 }
483 decoder := yaml.NewDecoder(fh)
484
485 return decoder.Decode(s.banList)
486 }
487
488 // loadThreadedNews loads the threaded news data from disk
489 func (s *Server) loadThreadedNews(threadedNewsPath string) error {
490 fh, err := os.Open(threadedNewsPath)
491 if err != nil {
492 return err
493 }
494 decoder := yaml.NewDecoder(fh)
495
496 return decoder.Decode(s.ThreadedNews)
497 }
498
499 // loadAccounts loads account data from disk
500 func (s *Server) loadAccounts(userDir string) error {
501 matches, err := filepath.Glob(filepath.Join(userDir, "*.yaml"))
502 if err != nil {
503 return err
504 }
505
506 if len(matches) == 0 {
507 return errors.New("no user accounts found in " + userDir)
508 }
509
510 for _, file := range matches {
511 fh, err := s.FS.Open(file)
512 if err != nil {
513 return err
514 }
515
516 account := Account{}
517 decoder := yaml.NewDecoder(fh)
518 if err := decoder.Decode(&account); err != nil {
519 return err
520 }
521
522 s.Accounts[account.Login] = &account
523 }
524 return nil
525 }
526
527 func (s *Server) loadConfig(path string) error {
528 fh, err := s.FS.Open(path)
529 if err != nil {
530 return err
531 }
532
533 decoder := yaml.NewDecoder(fh)
534 err = decoder.Decode(s.Config)
535 if err != nil {
536 return err
537 }
538
539 validate := validator.New()
540 err = validate.Struct(s.Config)
541 if err != nil {
542 return err
543 }
544 return nil
545 }
546
547 // handleNewConnection takes a new net.Conn and performs the initial login sequence
548 func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error {
549 defer dontPanic(s.Logger)
550
551 if err := Handshake(rwc); err != nil {
552 return err
553 }
554
555 // Create a new scanner for parsing incoming bytes into transaction tokens
556 scanner := bufio.NewScanner(rwc)
557 scanner.Split(transactionScanner)
558
559 scanner.Scan()
560
561 // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the
562 // scanner re-uses the buffer for subsequent scans.
563 buf := make([]byte, len(scanner.Bytes()))
564 copy(buf, scanner.Bytes())
565
566 var clientLogin Transaction
567 if _, err := clientLogin.Write(buf); err != nil {
568 return err
569 }
570
571 // check if remoteAddr is present in the ban list
572 if banUntil, ok := s.banList[strings.Split(remoteAddr, ":")[0]]; ok {
573 // permaban
574 if banUntil == nil {
575 t := NewTransaction(
576 TranServerMsg,
577 &[]byte{0, 0},
578 NewField(FieldData, []byte("You are permanently banned on this server")),
579 NewField(FieldChatOptions, []byte{0, 0}),
580 )
581
582 b, err := t.MarshalBinary()
583 if err != nil {
584 return err
585 }
586
587 _, err = rwc.Write(b)
588 if err != nil {
589 return err
590 }
591
592 time.Sleep(1 * time.Second)
593 return nil
594 }
595
596 // temporary ban
597 if time.Now().Before(*banUntil) {
598 t := NewTransaction(
599 TranServerMsg,
600 &[]byte{0, 0},
601 NewField(FieldData, []byte("You are temporarily banned on this server")),
602 NewField(FieldChatOptions, []byte{0, 0}),
603 )
604 b, err := t.MarshalBinary()
605 if err != nil {
606 return err
607 }
608
609 _, err = rwc.Write(b)
610 if err != nil {
611 return err
612 }
613
614 time.Sleep(1 * time.Second)
615 return nil
616 }
617 }
618
619 c := s.NewClientConn(rwc, remoteAddr)
620 defer c.Disconnect()
621
622 encodedLogin := clientLogin.GetField(FieldUserLogin).Data
623 encodedPassword := clientLogin.GetField(FieldUserPassword).Data
624 c.Version = clientLogin.GetField(FieldVersion).Data
625
626 var login string
627 for _, char := range encodedLogin {
628 login += string(rune(255 - uint(char)))
629 }
630 if login == "" {
631 login = GuestAccount
632 }
633
634 c.logger = s.Logger.With("remoteAddr", remoteAddr, "login", login)
635
636 // If authentication fails, send error reply and close connection
637 if !c.Authenticate(login, encodedPassword) {
638 t := c.NewErrReply(&clientLogin, "Incorrect login.")
639 b, err := t.MarshalBinary()
640 if err != nil {
641 return err
642 }
643 if _, err := rwc.Write(b); err != nil {
644 return err
645 }
646
647 c.logger.Infow("Login failed", "clientVersion", fmt.Sprintf("%x", c.Version))
648
649 return nil
650 }
651
652 if clientLogin.GetField(FieldUserIconID).Data != nil {
653 c.Icon = clientLogin.GetField(FieldUserIconID).Data
654 }
655
656 c.Account = c.Server.Accounts[login]
657
658 if clientLogin.GetField(FieldUserName).Data != nil {
659 if c.Authorize(accessAnyName) {
660 c.UserName = clientLogin.GetField(FieldUserName).Data
661 } else {
662 c.UserName = []byte(c.Account.Name)
663 }
664 }
665
666 if c.Authorize(accessDisconUser) {
667 c.Flags = []byte{0, 2}
668 }
669
670 s.outbox <- c.NewReply(&clientLogin,
671 NewField(FieldVersion, []byte{0x00, 0xbe}),
672 NewField(FieldCommunityBannerID, []byte{0, 0}),
673 NewField(FieldServerName, []byte(s.Config.Name)),
674 )
675
676 // Send user access privs so client UI knows how to behave
677 c.Server.outbox <- *NewTransaction(TranUserAccess, c.ID, NewField(FieldUserAccess, c.Account.Access[:]))
678
679 // Accounts with accessNoAgreement do not receive the server agreement on login. The behavior is different between
680 // client versions. For 1.2.3 client, we do not send TranShowAgreement. For other client versions, we send
681 // TranShowAgreement but with the NoServerAgreement field set to 1.
682 if c.Authorize(accessNoAgreement) {
683 // If client version is nil, then the client uses the 1.2.3 login behavior
684 if c.Version != nil {
685 c.Server.outbox <- *NewTransaction(TranShowAgreement, c.ID, NewField(FieldNoServerAgreement, []byte{1}))
686 }
687 } else {
688 c.Server.outbox <- *NewTransaction(TranShowAgreement, c.ID, NewField(FieldData, s.Agreement))
689 }
690
691 // If the client has provided a username as part of the login, we can infer that it is using the 1.2.3 login
692 // flow and not the 1.5+ flow.
693 if len(c.UserName) != 0 {
694 // Add the client username to the logger. For 1.5+ clients, we don't have this information yet as it comes as
695 // part of TranAgreed
696 c.logger = c.logger.With("name", string(c.UserName))
697
698 c.logger.Infow("Login successful", "clientVersion", "Not sent (probably 1.2.3)")
699
700 // Notify other clients on the server that the new user has logged in. For 1.5+ clients we don't have this
701 // information yet, so we do it in TranAgreed instead
702 for _, t := range c.notifyOthers(
703 *NewTransaction(
704 TranNotifyChangeUser, nil,
705 NewField(FieldUserName, c.UserName),
706 NewField(FieldUserID, *c.ID),
707 NewField(FieldUserIconID, c.Icon),
708 NewField(FieldUserFlags, c.Flags),
709 ),
710 ) {
711 c.Server.outbox <- t
712 }
713 }
714
715 c.Server.Stats.ConnectionCounter += 1
716 if len(s.Clients) > c.Server.Stats.ConnectionPeak {
717 c.Server.Stats.ConnectionPeak = len(s.Clients)
718 }
719
720 // Scan for new transactions and handle them as they come in.
721 for scanner.Scan() {
722 // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the
723 // scanner re-uses the buffer for subsequent scans.
724 buf := make([]byte, len(scanner.Bytes()))
725 copy(buf, scanner.Bytes())
726
727 var t Transaction
728 if _, err := t.Write(buf); err != nil {
729 return err
730 }
731
732 if err := c.handleTransaction(t); err != nil {
733 c.logger.Errorw("Error handling transaction", "err", err)
734 }
735 }
736 return nil
737 }
738
739 func (s *Server) NewPrivateChat(cc *ClientConn) []byte {
740 s.PrivateChatsMu.Lock()
741 defer s.PrivateChatsMu.Unlock()
742
743 randID := make([]byte, 4)
744 rand.Read(randID)
745 data := binary.BigEndian.Uint32(randID)
746
747 s.PrivateChats[data] = &PrivateChat{
748 ClientConn: make(map[uint16]*ClientConn),
749 }
750 s.PrivateChats[data].ClientConn[cc.uint16ID()] = cc
751
752 return randID
753 }
754
755 const dlFldrActionSendFile = 1
756 const dlFldrActionResumeFile = 2
757 const dlFldrActionNextFile = 3
758
759 // handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection
760 func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) error {
761 defer dontPanic(s.Logger)
762
763 txBuf := make([]byte, 16)
764 if _, err := io.ReadFull(rwc, txBuf); err != nil {
765 return err
766 }
767
768 var t transfer
769 if _, err := t.Write(txBuf); err != nil {
770 return err
771 }
772
773 defer func() {
774 s.mux.Lock()
775 delete(s.fileTransfers, t.ReferenceNumber)
776 s.mux.Unlock()
777
778 // Wait a few seconds before closing the connection: this is a workaround for problems
779 // observed with Windows clients where the client must initiate close of the TCP connection before
780 // the server does. This is gross and seems unnecessary. TODO: Revisit?
781 time.Sleep(3 * time.Second)
782 }()
783
784 s.mux.Lock()
785 fileTransfer, ok := s.fileTransfers[t.ReferenceNumber]
786 s.mux.Unlock()
787 if !ok {
788 return errors.New("invalid transaction ID")
789 }
790
791 defer func() {
792 fileTransfer.ClientConn.transfersMU.Lock()
793 delete(fileTransfer.ClientConn.transfers[fileTransfer.Type], t.ReferenceNumber)
794 fileTransfer.ClientConn.transfersMU.Unlock()
795 }()
796
797 rLogger := s.Logger.With(
798 "remoteAddr", ctx.Value(contextKeyReq).(requestCtx).remoteAddr,
799 "login", fileTransfer.ClientConn.Account.Login,
800 "name", string(fileTransfer.ClientConn.UserName),
801 )
802
803 fullPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
804 if err != nil {
805 return err
806 }
807
808 switch fileTransfer.Type {
809 case bannerDownload:
810 if err := s.bannerDownload(rwc); err != nil {
811 return err
812 }
813 case FileDownload:
814 s.Stats.DownloadCounter += 1
815 s.Stats.DownloadsInProgress += 1
816 defer func() {
817 s.Stats.DownloadsInProgress -= 1
818 }()
819
820 var dataOffset int64
821 if fileTransfer.fileResumeData != nil {
822 dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.fileResumeData.ForkInfoList[0].DataSize[:]))
823 }
824
825 fw, err := newFileWrapper(s.FS, fullPath, 0)
826 if err != nil {
827 return err
828 }
829
830 rLogger.Infow("File download started", "filePath", fullPath)
831
832 // if file transfer options are included, that means this is a "quick preview" request from a 1.5+ client
833 if fileTransfer.options == nil {
834 // Start by sending flat file object to client
835 if _, err := rwc.Write(fw.ffo.BinaryMarshal()); err != nil {
836 return err
837 }
838 }
839
840 file, err := fw.dataForkReader()
841 if err != nil {
842 return err
843 }
844
845 br := bufio.NewReader(file)
846 if _, err := br.Discard(int(dataOffset)); err != nil {
847 return err
848 }
849
850 if _, err = io.Copy(rwc, io.TeeReader(br, fileTransfer.bytesSentCounter)); err != nil {
851 return err
852 }
853
854 // if the client requested to resume transfer, do not send the resource fork header, or it will be appended into the fileWrapper data
855 if fileTransfer.fileResumeData == nil {
856 err = binary.Write(rwc, binary.BigEndian, fw.rsrcForkHeader())
857 if err != nil {
858 return err
859 }
860 }
861
862 rFile, err := fw.rsrcForkFile()
863 if err != nil {
864 return nil
865 }
866
867 if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil {
868 return err
869 }
870
871 case FileUpload:
872 s.Stats.UploadCounter += 1
873 s.Stats.UploadsInProgress += 1
874 defer func() { s.Stats.UploadsInProgress -= 1 }()
875
876 var file *os.File
877
878 // A file upload has three possible cases:
879 // 1) Upload a new file
880 // 2) Resume a partially transferred file
881 // 3) Replace a fully uploaded file
882 // We have to infer which case applies by inspecting what is already on the filesystem
883
884 // 1) Check for existing file:
885 _, err = os.Stat(fullPath)
886 if err == nil {
887 return errors.New("existing file found at " + fullPath)
888 }
889 if errors.Is(err, fs.ErrNotExist) {
890 // If not found, open or create a new .incomplete file
891 file, err = os.OpenFile(fullPath+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
892 if err != nil {
893 return err
894 }
895 }
896
897 f, err := newFileWrapper(s.FS, fullPath, 0)
898 if err != nil {
899 return err
900 }
901
902 rLogger.Infow("File upload started", "dstFile", fullPath)
903
904 rForkWriter := io.Discard
905 iForkWriter := io.Discard
906 if s.Config.PreserveResourceForks {
907 rForkWriter, err = f.rsrcForkWriter()
908 if err != nil {
909 return err
910 }
911
912 iForkWriter, err = f.infoForkWriter()
913 if err != nil {
914 return err
915 }
916 }
917
918 if err := receiveFile(rwc, file, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil {
919 s.Logger.Error(err)
920 }
921
922 if err := file.Close(); err != nil {
923 return err
924 }
925
926 if err := s.FS.Rename(fullPath+".incomplete", fullPath); err != nil {
927 return err
928 }
929
930 rLogger.Infow("File upload complete", "dstFile", fullPath)
931
932 case FolderDownload:
933 s.Stats.DownloadCounter += 1
934 s.Stats.DownloadsInProgress += 1
935 defer func() { s.Stats.DownloadsInProgress -= 1 }()
936
937 // Folder Download flow:
938 // 1. Get filePath from the transfer
939 // 2. Iterate over files
940 // 3. For each fileWrapper:
941 // Send fileWrapper header to client
942 // The client can reply in 3 ways:
943 //
944 // 1. If type is an odd number (unknown type?), or fileWrapper download for the current fileWrapper is completed:
945 // client sends []byte{0x00, 0x03} to tell the server to continue to the next fileWrapper
946 //
947 // 2. If download of a fileWrapper is to be resumed:
948 // client sends:
949 // []byte{0x00, 0x02} // download folder action
950 // [2]byte // Resume data size
951 // []byte fileWrapper resume data (see myField_FileResumeData)
952 //
953 // 3. Otherwise, download of the fileWrapper is requested and client sends []byte{0x00, 0x01}
954 //
955 // When download is requested (case 2 or 3), server replies with:
956 // [4]byte - fileWrapper size
957 // []byte - Flattened File Object
958 //
959 // After every fileWrapper download, client could request next fileWrapper with:
960 // []byte{0x00, 0x03}
961 //
962 // This notifies the server to send the next item header
963
964 basePathLen := len(fullPath)
965
966 rLogger.Infow("Start folder download", "path", fullPath)
967
968 nextAction := make([]byte, 2)
969 if _, err := io.ReadFull(rwc, nextAction); err != nil {
970 return err
971 }
972
973 i := 0
974 err = filepath.Walk(fullPath+"/", func(path string, info os.FileInfo, err error) error {
975 s.Stats.DownloadCounter += 1
976 i += 1
977
978 if err != nil {
979 return err
980 }
981
982 // skip dot files
983 if strings.HasPrefix(info.Name(), ".") {
984 return nil
985 }
986
987 hlFile, err := newFileWrapper(s.FS, path, 0)
988 if err != nil {
989 return err
990 }
991
992 subPath := path[basePathLen+1:]
993 rLogger.Debugw("Sending fileheader", "i", i, "path", path, "fullFilePath", fullPath, "subPath", subPath, "IsDir", info.IsDir())
994
995 if i == 1 {
996 return nil
997 }
998
999 fileHeader := NewFileHeader(subPath, info.IsDir())
1000
1001 // Send the fileWrapper header to client
1002 if _, err := rwc.Write(fileHeader.Payload()); err != nil {
1003 s.Logger.Errorf("error sending file header: %v", err)
1004 return err
1005 }
1006
1007 // Read the client's Next Action request
1008 if _, err := io.ReadFull(rwc, nextAction); err != nil {
1009 return err
1010 }
1011
1012 rLogger.Debugw("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
1013
1014 var dataOffset int64
1015
1016 switch nextAction[1] {
1017 case dlFldrActionResumeFile:
1018 // get size of resumeData
1019 resumeDataByteLen := make([]byte, 2)
1020 if _, err := io.ReadFull(rwc, resumeDataByteLen); err != nil {
1021 return err
1022 }
1023
1024 resumeDataLen := binary.BigEndian.Uint16(resumeDataByteLen)
1025 resumeDataBytes := make([]byte, resumeDataLen)
1026 if _, err := io.ReadFull(rwc, resumeDataBytes); err != nil {
1027 return err
1028 }
1029
1030 var frd FileResumeData
1031 if err := frd.UnmarshalBinary(resumeDataBytes); err != nil {
1032 return err
1033 }
1034 dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:]))
1035 case dlFldrActionNextFile:
1036 // client asked to skip this file
1037 return nil
1038 }
1039
1040 if info.IsDir() {
1041 return nil
1042 }
1043
1044 rLogger.Infow("File download started",
1045 "fileName", info.Name(),
1046 "TransferSize", fmt.Sprintf("%x", hlFile.ffo.TransferSize(dataOffset)),
1047 )
1048
1049 // Send file size to client
1050 if _, err := rwc.Write(hlFile.ffo.TransferSize(dataOffset)); err != nil {
1051 s.Logger.Error(err)
1052 return err
1053 }
1054
1055 // Send ffo bytes to client
1056 if _, err := rwc.Write(hlFile.ffo.BinaryMarshal()); err != nil {
1057 s.Logger.Error(err)
1058 return err
1059 }
1060
1061 file, err := s.FS.Open(path)
1062 if err != nil {
1063 return err
1064 }
1065
1066 // wr := bufio.NewWriterSize(rwc, 1460)
1067 if _, err = io.Copy(rwc, io.TeeReader(file, fileTransfer.bytesSentCounter)); err != nil {
1068 return err
1069 }
1070
1071 if nextAction[1] != 2 && hlFile.ffo.FlatFileHeader.ForkCount[1] == 3 {
1072 err = binary.Write(rwc, binary.BigEndian, hlFile.rsrcForkHeader())
1073 if err != nil {
1074 return err
1075 }
1076
1077 rFile, err := hlFile.rsrcForkFile()
1078 if err != nil {
1079 return err
1080 }
1081
1082 if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil {
1083 return err
1084 }
1085 }
1086
1087 // Read the client's Next Action request. This is always 3, I think?
1088 if _, err := io.ReadFull(rwc, nextAction); err != nil {
1089 return err
1090 }
1091
1092 return nil
1093 })
1094
1095 if err != nil {
1096 return err
1097 }
1098
1099 case FolderUpload:
1100 s.Stats.UploadCounter += 1
1101 s.Stats.UploadsInProgress += 1
1102 defer func() { s.Stats.UploadsInProgress -= 1 }()
1103 rLogger.Infow(
1104 "Folder upload started",
1105 "dstPath", fullPath,
1106 "TransferSize", binary.BigEndian.Uint32(fileTransfer.TransferSize),
1107 "FolderItemCount", fileTransfer.FolderItemCount,
1108 )
1109
1110 // Check if the target folder exists. If not, create it.
1111 if _, err := s.FS.Stat(fullPath); os.IsNotExist(err) {
1112 if err := s.FS.Mkdir(fullPath, 0777); err != nil {
1113 return err
1114 }
1115 }
1116
1117 // Begin the folder upload flow by sending the "next file action" to client
1118 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
1119 return err
1120 }
1121
1122 fileSize := make([]byte, 4)
1123
1124 for i := 0; i < fileTransfer.ItemCount(); i++ {
1125 s.Stats.UploadCounter += 1
1126
1127 var fu folderUpload
1128 if _, err := io.ReadFull(rwc, fu.DataSize[:]); err != nil {
1129 return err
1130 }
1131 if _, err := io.ReadFull(rwc, fu.IsFolder[:]); err != nil {
1132 return err
1133 }
1134 if _, err := io.ReadFull(rwc, fu.PathItemCount[:]); err != nil {
1135 return err
1136 }
1137
1138 fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes
1139
1140 if _, err := io.ReadFull(rwc, fu.FileNamePath); err != nil {
1141 return err
1142 }
1143
1144 rLogger.Infow(
1145 "Folder upload continued",
1146 "FormattedPath", fu.FormattedPath(),
1147 "IsFolder", fmt.Sprintf("%x", fu.IsFolder),
1148 "PathItemCount", binary.BigEndian.Uint16(fu.PathItemCount[:]),
1149 )
1150
1151 if fu.IsFolder == [2]byte{0, 1} {
1152 if _, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath())); os.IsNotExist(err) {
1153 if err := os.Mkdir(filepath.Join(fullPath, fu.FormattedPath()), 0777); err != nil {
1154 return err
1155 }
1156 }
1157
1158 // Tell client to send next file
1159 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
1160 return err
1161 }
1162 } else {
1163 nextAction := dlFldrActionSendFile
1164
1165 // Check if we have the full file already. If so, send dlFldrAction_NextFile to client to skip.
1166 _, err = os.Stat(filepath.Join(fullPath, fu.FormattedPath()))
1167 if err != nil && !errors.Is(err, fs.ErrNotExist) {
1168 return err
1169 }
1170 if err == nil {
1171 nextAction = dlFldrActionNextFile
1172 }
1173
1174 // Check if we have a partial file already. If so, send dlFldrAction_ResumeFile to client to resume upload.
1175 incompleteFile, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath()+incompleteFileSuffix))
1176 if err != nil && !errors.Is(err, fs.ErrNotExist) {
1177 return err
1178 }
1179 if err == nil {
1180 nextAction = dlFldrActionResumeFile
1181 }
1182
1183 if _, err := rwc.Write([]byte{0, uint8(nextAction)}); err != nil {
1184 return err
1185 }
1186
1187 switch nextAction {
1188 case dlFldrActionNextFile:
1189 continue
1190 case dlFldrActionResumeFile:
1191 offset := make([]byte, 4)
1192 binary.BigEndian.PutUint32(offset, uint32(incompleteFile.Size()))
1193
1194 file, err := os.OpenFile(fullPath+"/"+fu.FormattedPath()+incompleteFileSuffix, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
1195 if err != nil {
1196 return err
1197 }
1198
1199 fileResumeData := NewFileResumeData([]ForkInfoList{*NewForkInfoList(offset)})
1200
1201 b, _ := fileResumeData.BinaryMarshal()
1202
1203 bs := make([]byte, 2)
1204 binary.BigEndian.PutUint16(bs, uint16(len(b)))
1205
1206 if _, err := rwc.Write(append(bs, b...)); err != nil {
1207 return err
1208 }
1209
1210 if _, err := io.ReadFull(rwc, fileSize); err != nil {
1211 return err
1212 }
1213
1214 if err := receiveFile(rwc, file, io.Discard, io.Discard, fileTransfer.bytesSentCounter); err != nil {
1215 s.Logger.Error(err)
1216 }
1217
1218 err = os.Rename(fullPath+"/"+fu.FormattedPath()+".incomplete", fullPath+"/"+fu.FormattedPath())
1219 if err != nil {
1220 return err
1221 }
1222
1223 case dlFldrActionSendFile:
1224 if _, err := io.ReadFull(rwc, fileSize); err != nil {
1225 return err
1226 }
1227
1228 filePath := filepath.Join(fullPath, fu.FormattedPath())
1229
1230 hlFile, err := newFileWrapper(s.FS, filePath, 0)
1231 if err != nil {
1232 return err
1233 }
1234
1235 rLogger.Infow("Starting file transfer", "path", filePath, "fileNum", i+1, "fileSize", binary.BigEndian.Uint32(fileSize))
1236
1237 incWriter, err := hlFile.incFileWriter()
1238 if err != nil {
1239 return err
1240 }
1241
1242 rForkWriter := io.Discard
1243 iForkWriter := io.Discard
1244 if s.Config.PreserveResourceForks {
1245 iForkWriter, err = hlFile.infoForkWriter()
1246 if err != nil {
1247 return err
1248 }
1249
1250 rForkWriter, err = hlFile.rsrcForkWriter()
1251 if err != nil {
1252 return err
1253 }
1254 }
1255 if err := receiveFile(rwc, incWriter, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil {
1256 return err
1257 }
1258
1259 if err := os.Rename(filePath+".incomplete", filePath); err != nil {
1260 return err
1261 }
1262 }
1263
1264 // Tell client to send next fileWrapper
1265 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
1266 return err
1267 }
1268 }
1269 }
1270 rLogger.Infof("Folder upload complete")
1271 }
1272
1273 return nil
1274 }