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