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