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