]> git.r.bdr.sh - rbdr/mobius/blame - hotline/server.go
Replace path.Join with filepath.Join
[rbdr/mobius] / hotline / server.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
7cd900d6 4 "bufio"
a82b93cf 5 "bytes"
6988a057
JH
6 "context"
7 "encoding/binary"
8 "errors"
9 "fmt"
041c2df6 10 "github.com/go-playground/validator/v10"
6988a057 11 "go.uber.org/zap"
7cd900d6 12 "gopkg.in/yaml.v3"
6988a057 13 "io"
16a4ad70 14 "io/fs"
6988a057 15 "io/ioutil"
6988a057
JH
16 "math/big"
17 "math/rand"
18 "net"
19 "os"
6988a057
JH
20 "path/filepath"
21 "runtime/debug"
6988a057
JH
22 "strings"
23 "sync"
24 "time"
6988a057
JH
25)
26
7cd900d6
JH
27type contextKey string
28
29var contextKeyReq = contextKey("req")
30
31type requestCtx struct {
32 remoteAddr string
33 login string
34 name string
35}
36
6988a057
JH
37const (
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
a82b93cf
JH
43var nostalgiaVersion = []byte{0, 0, 2, 0x2c} // version ID used by the Nostalgia client
44
6988a057 45type Server struct {
6988a057
JH
46 Port int
47 Accounts map[string]*Account
48 Agreement []byte
49 Clients map[uint16]*ClientConn
6988a057
JH
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
40414f92 57 TrackerPassID [4]byte
6988a057
JH
58 Stats *Stats
59
7cd900d6 60 FS FileStore // Storage backend to use for File storage
6988a057
JH
61
62 outbox chan Transaction
7cd900d6 63 mux sync.Mutex
6988a057 64
6988a057 65 flatNewsMux sync.Mutex
7cd900d6 66 FlatNews []byte
6988a057
JH
67}
68
69type PrivateChat struct {
70 Subject string
71 ClientConn map[uint16]*ClientConn
72}
73
74func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error {
aeec1015
JH
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
6988a057
JH
81 var wg sync.WaitGroup
82
83 wg.Add(1)
8168522a
JH
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
7cd900d6 90 s.Logger.Fatal(s.Serve(ctx, ln))
8168522a 91 }()
6988a057
JH
92
93 wg.Add(1)
8168522a
JH
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
7cd900d6 101 s.Logger.Fatal(s.ServeFileTransfers(ctx, ln))
8168522a 102 }()
6988a057
JH
103
104 wg.Wait()
105
106 return nil
107}
108
7cd900d6 109func (s *Server) ServeFileTransfers(ctx context.Context, ln net.Listener) error {
6988a057
JH
110 for {
111 conn, err := ln.Accept()
112 if err != nil {
113 return err
114 }
115
116 go func() {
7cd900d6
JH
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 {
6988a057
JH
127 s.Logger.Errorw("file transfer error", "reason", err)
128 }
129 }()
130 }
131}
132
133func (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 {
bd1ce113 144 return fmt.Errorf("invalid client id %v", *t.clientID)
6988a057 145 }
72dd37f1 146 userName := string(client.UserName)
6988a057
JH
147 login := client.Account.Login
148
149 handler := TransactionHandlers[requestNum]
150
72dd37f1
JH
151 b, err := t.MarshalBinary()
152 if err != nil {
153 return err
154 }
6988a057 155 var n int
72dd37f1 156 if n, err = client.Connection.Write(b); err != nil {
6988a057
JH
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,
d4c152a4 165 "remoteAddr", client.RemoteAddr,
6988a057
JH
166 )
167 return nil
168}
169
7cd900d6 170func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
6988a057
JH
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() {
7cd900d6
JH
188 if err := s.handleNewConnection(ctx, conn, conn.RemoteAddr().String()); err != nil {
189 s.Logger.Infow("New client connection established", "RemoteAddr", conn.RemoteAddr())
6988a057
JH
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
200const (
c7e932c0 201 agreementFile = "Agreement.txt"
6988a057
JH
202)
203
204// NewServer constructs a new Server from a config dir
7cd900d6 205func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS FileStore) (*Server, error) {
6988a057
JH
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{},
b196a50a 219 FS: FS,
6988a057
JH
220 }
221
8168522a 222 var err error
6988a057
JH
223
224 // generate a new random passID for tracker registration
40414f92 225 if _, err := rand.Read(server.TrackerPassID[:]); err != nil {
6988a057
JH
226 return nil, err
227 }
228
f22acf38 229 server.Agreement, err = os.ReadFile(filepath.Join(configDir, agreementFile))
b196a50a 230 if err != nil {
6988a057
JH
231 return nil, err
232 }
233
f22acf38 234 if server.FlatNews, err = os.ReadFile(filepath.Join(configDir, "MessageBoard.txt")); err != nil {
6988a057
JH
235 return nil, err
236 }
237
f22acf38 238 if err := server.loadThreadedNews(filepath.Join(configDir, "ThreadedNews.yaml")); err != nil {
6988a057
JH
239 return nil, err
240 }
241
f22acf38 242 if err := server.loadConfig(filepath.Join(configDir, "config.yaml")); err != nil {
6988a057
JH
243 return nil, err
244 }
245
f22acf38 246 if err := server.loadAccounts(filepath.Join(configDir, "Users/")); err != nil {
6988a057
JH
247 return nil, err
248 }
249
f22acf38 250 server.Config.FileRoot = filepath.Join(configDir, "Files")
6988a057
JH
251
252 *server.NextGuestID = 1
253
254 if server.Config.EnableTrackerRegistration {
e42888eb
JH
255 server.Logger.Infow(
256 "Tracker registration enabled",
257 "frequency", fmt.Sprintf("%vs", trackerUpdateFrequency),
258 "trackers", server.Config.Trackers,
259 )
260
6988a057
JH
261 go func() {
262 for {
e42888eb 263 tr := &TrackerRegistration{
6988a057 264 UserCount: server.userCount(),
40414f92 265 PassID: server.TrackerPassID[:],
6988a057
JH
266 Name: server.Config.Name,
267 Description: server.Config.Description,
268 }
40414f92 269 binary.BigEndian.PutUint16(tr.Port[:], uint16(server.Port))
6988a057 270 for _, t := range server.Config.Trackers {
6988a057
JH
271 if err := register(t, tr); err != nil {
272 server.Logger.Errorw("unable to register with tracker %v", "error", err)
273 }
e42888eb 274 server.Logger.Infow("Sent Tracker registration", "data", tr)
6988a057
JH
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
288func (s *Server) userCount() int {
289 s.mux.Lock()
290 defer s.mux.Unlock()
291
292 return len(s.Clients)
293}
294
295func (s *Server) keepaliveHandler() {
296 for {
297 time.Sleep(idleCheckInterval * time.Second)
298 s.mux.Lock()
299
300 for _, c := range s.Clients {
61c272e1
JH
301 c.IdleTime += idleCheckInterval
302 if c.IdleTime > userIdleSeconds && !c.Idle {
6988a057
JH
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),
72dd37f1 313 NewField(fieldUserName, c.UserName),
6988a057
JH
314 NewField(fieldUserIconID, *c.Icon),
315 )
316 }
317 }
318 s.mux.Unlock()
319 }
320}
321
322func (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(
f22acf38 331 filepath.Join(s.ConfigDir, "ThreadedNews.yaml"),
6988a057
JH
332 out,
333 0666,
334 )
335 return err
336}
337
d4c152a4 338func (s *Server) NewClientConn(conn net.Conn, remoteAddr string) *ClientConn {
6988a057
JH
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},
72dd37f1 346 UserName: []byte{},
6988a057
JH
347 Connection: conn,
348 Server: s,
349 Version: &[]byte{},
aebc4d36 350 AutoReply: []byte{},
6988a057 351 Transfers: make(map[int][]*FileTransfer),
bd1ce113 352 Agreed: false,
d4c152a4 353 RemoteAddr: remoteAddr,
6988a057
JH
354 }
355 *s.NextGuestID++
356 ID := *s.NextGuestID
357
6988a057
JH
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
365func (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
f22acf38 381 return s.FS.WriteFile(filepath.Join(s.ConfigDir, "Users", login+".yaml"), out, 0666)
6988a057
JH
382}
383
d2810ae9
JH
384func (s *Server) UpdateUser(login, newLogin, name, password string, access []byte) error {
385 s.mux.Lock()
386 defer s.mux.Unlock()
387
d2810ae9
JH
388 // update renames the user login
389 if login != newLogin {
f22acf38 390 err := os.Rename(filepath.Join(s.ConfigDir, "Users", login+".yaml"), filepath.Join(s.ConfigDir, "Users", newLogin+".yaml"))
d2810ae9
JH
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 }
f22acf38
JH
407
408 if err := os.WriteFile(filepath.Join(s.ConfigDir, "Users", newLogin+".yaml"), out, 0666); err != nil {
d2810ae9
JH
409 return err
410 }
411
412 return nil
413}
414
6988a057
JH
415// DeleteUser deletes the user account
416func (s *Server) DeleteUser(login string) error {
417 s.mux.Lock()
418 defer s.mux.Unlock()
419
420 delete(s.Accounts, login)
421
f22acf38 422 return s.FS.Remove(filepath.Join(s.ConfigDir, "Users", login+".yaml"))
6988a057
JH
423}
424
425func (s *Server) connectedUsers() []Field {
426 s.mux.Lock()
427 defer s.mux.Unlock()
428
429 var connectedUsers []Field
c7e932c0 430 for _, c := range sortedClients(s.Clients) {
aebc4d36 431 if !c.Agreed {
bd1ce113
JH
432 continue
433 }
6988a057
JH
434 user := User{
435 ID: *c.ID,
436 Icon: *c.Icon,
437 Flags: *c.Flags,
72dd37f1 438 Name: string(c.UserName),
6988a057
JH
439 }
440 connectedUsers = append(connectedUsers, NewField(fieldUsernameWithInfo, user.Payload()))
441 }
442 return connectedUsers
443}
444
445// loadThreadedNews loads the threaded news data from disk
446func (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)
6988a057
JH
452
453 return decoder.Decode(s.ThreadedNews)
454}
455
456// loadAccounts loads account data from disk
457func (s *Server) loadAccounts(userDir string) error {
f22acf38 458 matches, err := filepath.Glob(filepath.Join(userDir, "*.yaml"))
6988a057
JH
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 {
b196a50a 468 fh, err := s.FS.Open(file)
6988a057
JH
469 if err != nil {
470 return err
471 }
472
473 account := Account{}
474 decoder := yaml.NewDecoder(fh)
6988a057
JH
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
484func (s *Server) loadConfig(path string) error {
b196a50a 485 fh, err := s.FS.Open(path)
6988a057
JH
486 if err != nil {
487 return err
488 }
489
490 decoder := yaml.NewDecoder(fh)
6988a057
JH
491 err = decoder.Decode(s.Config)
492 if err != nil {
493 return err
494 }
041c2df6
JH
495
496 validate := validator.New()
497 err = validate.Struct(s.Config)
498 if err != nil {
499 return err
500 }
6988a057
JH
501 return nil
502}
503
504const (
505 minTransactionLen = 22 // minimum length of any transaction
506)
507
37a954c8
JH
508// dontPanic recovers and logs panics instead of crashing
509// TODO: remove this after known issues are fixed
510func 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
6988a057 517// handleNewConnection takes a new net.Conn and performs the initial login sequence
7cd900d6 518func (s *Server) handleNewConnection(ctx context.Context, conn net.Conn, remoteAddr string) error {
c4208f86
JH
519 defer dontPanic(s.Logger)
520
d4c152a4 521 if err := Handshake(conn); err != nil {
6988a057
JH
522 return err
523 }
524
525 buf := make([]byte, 1024)
c4208f86 526 // TODO: fix potential short read with io.ReadFull
6988a057
JH
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
d4c152a4 540 c := s.NewClientConn(conn, remoteAddr)
6988a057 541 defer c.Disconnect()
6988a057
JH
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) {
72dd37f1
JH
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 {
6988a057
JH
563 return err
564 }
565 return fmt.Errorf("incorrect login")
566 }
567
6988a057 568 if clientLogin.GetField(fieldUserName).Data != nil {
72dd37f1 569 c.UserName = clientLogin.GetField(fieldUserName).Data
6988a057
JH
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
d4c152a4 582 s.Logger.Infow("Client connection received", "login", login, "version", *c.Version, "RemoteAddr", remoteAddr)
6988a057
JH
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
a82b93cf
JH
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) {
bd1ce113 598 c.Agreed = true
003a743e
JH
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 )
6988a057 609 }
bd1ce113 610
6988a057
JH
611 c.Server.Stats.LoginCount += 1
612
613 const readBuffSize = 1024000 // 1KB - TODO: what should this be?
6988a057
JH
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
7cd900d6 634 // iterate over all the transactions that were parsed from the byte slice and handle them
6988a057
JH
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
6988a057 643// NewTransactionRef generates a random ID for the file transfer. The Hotline client includes this ID
7cd900d6 644// in the transfer request payload, and the file transfer server will use it to map the request
6988a057
JH
645// to a transfer
646func (s *Server) NewTransactionRef() []byte {
647 transactionRef := make([]byte, 4)
648 rand.Read(transactionRef)
649
650 return transactionRef
651}
652
653func (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
670const dlFldrActionSendFile = 1
671const dlFldrActionResumeFile = 2
672const dlFldrActionNextFile = 3
673
85767504 674// handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection
7cd900d6 675func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) error {
37a954c8 676 defer dontPanic(s.Logger)
0a92e50b 677
2e7c03cf 678 txBuf := make([]byte, 16)
7cd900d6 679 if _, err := io.ReadFull(rwc, txBuf); err != nil {
6988a057
JH
680 return err
681 }
682
df2735b2 683 var t transfer
0a92e50b 684 if _, err := t.Write(txBuf); err != nil {
6988a057
JH
685 return err
686 }
687
688 transferRefNum := binary.BigEndian.Uint32(t.ReferenceNumber[:])
0a92e50b
JH
689 defer func() {
690 s.mux.Lock()
691 delete(s.FileTransfers, transferRefNum)
692 s.mux.Unlock()
693 }()
6988a057 694
0a92e50b
JH
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 }
16a4ad70 701
7cd900d6
JH
702 rLogger := s.Logger.With(
703 "remoteAddr", ctx.Value(contextKeyReq).(requestCtx).remoteAddr,
704 "xferID", transferRefNum,
705 )
706
6988a057
JH
707 switch fileTransfer.Type {
708 case FileDownload:
23411fc2
JH
709 s.Stats.DownloadCounter += 1
710
92a7e455
JH
711 fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
712 if err != nil {
713 return err
714 }
6988a057 715
16a4ad70
JH
716 var dataOffset int64
717 if fileTransfer.fileResumeData != nil {
718 dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.fileResumeData.ForkInfoList[0].DataSize[:]))
719 }
720
7cd900d6 721 fw, err := newFileWrapper(s.FS, fullFilePath, 0)
6988a057
JH
722 if err != nil {
723 return err
724 }
725
7cd900d6 726 rLogger.Infow("File download started", "filePath", fullFilePath, "transactionRef", fileTransfer.ReferenceNumber)
6988a057 727
7cd900d6
JH
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
d1cd6664
JH
731 if fileTransfer.options == nil {
732 // Start by sending flat file object to client
7cd900d6 733 if _, err := wr.Write(fw.ffo.BinaryMarshal()); err != nil {
d1cd6664
JH
734 return err
735 }
6988a057
JH
736 }
737
7cd900d6 738 file, err := fw.dataForkReader()
6988a057
JH
739 if err != nil {
740 return err
741 }
742
7cd900d6
JH
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())
16a4ad70
JH
754 if err != nil {
755 return err
756 }
7cd900d6 757 if err := wr.Flush(); err != nil {
6988a057
JH
758 return err
759 }
760 }
7cd900d6
JH
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
6988a057 773 case FileUpload:
23411fc2
JH
774 s.Stats.UploadCounter += 1
775
7cd900d6
JH
776 destinationFile, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
777 if err != nil {
778 return err
779 }
16a4ad70 780
5c14e4c9
JH
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
7cd900d6 787 // We have to infer which case applies by inspecting what is already on the filesystem
5c14e4c9
JH
788
789 // 1) Check for existing file:
7cd900d6 790 _, err = os.Stat(destinationFile)
5c14e4c9
JH
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 }
16a4ad70 798 if errors.Is(err, fs.ErrNotExist) {
7cd900d6 799 // If not found, open or create a new .incomplete file
5c14e4c9 800 file, err = os.OpenFile(destinationFile+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
16a4ad70
JH
801 if err != nil {
802 return err
803 }
6988a057 804 }
16a4ad70 805
7cd900d6
JH
806 f, err := newFileWrapper(s.FS, destinationFile, 0)
807 if err != nil {
808 return err
809 }
810
85767504 811 s.Logger.Infow("File upload started", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
6988a057 812
7cd900d6
JH
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 {
16a4ad70
JH
828 return err
829 }
830
e00ff8fe
JH
831 if err := file.Close(); err != nil {
832 return err
833 }
834
7cd900d6 835 if err := s.FS.Rename(destinationFile+".incomplete", destinationFile); err != nil {
16a4ad70 836 return err
6988a057 837 }
85767504
JH
838
839 s.Logger.Infow("File upload complete", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
6988a057
JH
840 case FolderDownload:
841 // Folder Download flow:
df2735b2 842 // 1. Get filePath from the transfer
6988a057 843 // 2. Iterate over files
7cd900d6
JH
844 // 3. For each fileWrapper:
845 // Send fileWrapper header to client
6988a057
JH
846 // The client can reply in 3 ways:
847 //
7cd900d6
JH
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
6988a057 850 //
7cd900d6 851 // 2. If download of a fileWrapper is to be resumed:
6988a057
JH
852 // client sends:
853 // []byte{0x00, 0x02} // download folder action
854 // [2]byte // Resume data size
7cd900d6 855 // []byte fileWrapper resume data (see myField_FileResumeData)
6988a057 856 //
7cd900d6 857 // 3. Otherwise, download of the fileWrapper is requested and client sends []byte{0x00, 0x01}
6988a057
JH
858 //
859 // When download is requested (case 2 or 3), server replies with:
7cd900d6 860 // [4]byte - fileWrapper size
6988a057
JH
861 // []byte - Flattened File Object
862 //
7cd900d6 863 // After every fileWrapper download, client could request next fileWrapper with:
6988a057
JH
864 // []byte{0x00, 0x03}
865 //
866 // This notifies the server to send the next item header
867
92a7e455
JH
868 fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
869 if err != nil {
870 return err
871 }
6988a057
JH
872
873 basePathLen := len(fullFilePath)
874
85767504 875 s.Logger.Infow("Start folder download", "path", fullFilePath, "ReferenceNumber", fileTransfer.ReferenceNumber)
6988a057 876
85767504 877 nextAction := make([]byte, 2)
7cd900d6 878 if _, err := io.ReadFull(rwc, nextAction); err != nil {
85767504
JH
879 return err
880 }
6988a057
JH
881
882 i := 0
85767504 883 err = filepath.Walk(fullFilePath+"/", func(path string, info os.FileInfo, err error) error {
23411fc2 884 s.Stats.DownloadCounter += 1
7cd900d6 885 i += 1
23411fc2 886
85767504
JH
887 if err != nil {
888 return err
889 }
7cd900d6
JH
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
85767504 901 subPath := path[basePathLen+1:]
6988a057
JH
902 s.Logger.Infow("Sending fileheader", "i", i, "path", path, "fullFilePath", fullFilePath, "subPath", subPath, "IsDir", info.IsDir())
903
6988a057
JH
904 if i == 1 {
905 return nil
906 }
907
85767504
JH
908 fileHeader := NewFileHeader(subPath, info.IsDir())
909
7cd900d6
JH
910 // Send the fileWrapper header to client
911 if _, err := rwc.Write(fileHeader.Payload()); err != nil {
6988a057
JH
912 s.Logger.Errorf("error sending file header: %v", err)
913 return err
914 }
915
916 // Read the client's Next Action request
7cd900d6 917 if _, err := io.ReadFull(rwc, nextAction); err != nil {
6988a057
JH
918 return err
919 }
920
85767504 921 s.Logger.Infow("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
6988a057 922
16a4ad70
JH
923 var dataOffset int64
924
925 switch nextAction[1] {
926 case dlFldrActionResumeFile:
16a4ad70 927 // get size of resumeData
7cd900d6
JH
928 resumeDataByteLen := make([]byte, 2)
929 if _, err := io.ReadFull(rwc, resumeDataByteLen); err != nil {
16a4ad70
JH
930 return err
931 }
932
7cd900d6 933 resumeDataLen := binary.BigEndian.Uint16(resumeDataByteLen)
16a4ad70 934 resumeDataBytes := make([]byte, resumeDataLen)
7cd900d6 935 if _, err := io.ReadFull(rwc, resumeDataBytes); err != nil {
16a4ad70
JH
936 return err
937 }
938
7cd900d6 939 var frd FileResumeData
16a4ad70
JH
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
6988a057
JH
949 if info.IsDir() {
950 return nil
951 }
952
6988a057
JH
953 s.Logger.Infow("File download started",
954 "fileName", info.Name(),
955 "transactionRef", fileTransfer.ReferenceNumber,
7cd900d6 956 "TransferSize", fmt.Sprintf("%x", hlFile.ffo.TransferSize(dataOffset)),
6988a057
JH
957 )
958
959 // Send file size to client
7cd900d6 960 if _, err := rwc.Write(hlFile.ffo.TransferSize(dataOffset)); err != nil {
6988a057
JH
961 s.Logger.Error(err)
962 return err
963 }
964
85767504 965 // Send ffo bytes to client
7cd900d6 966 if _, err := rwc.Write(hlFile.ffo.BinaryMarshal()); err != nil {
6988a057
JH
967 s.Logger.Error(err)
968 return err
969 }
970
b196a50a 971 file, err := s.FS.Open(path)
6988a057
JH
972 if err != nil {
973 return err
974 }
975
7cd900d6
JH
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())
16a4ad70 984 if err != nil {
7cd900d6 985 return err
16a4ad70 986 }
16a4ad70 987
7cd900d6
JH
988 rFile, err := hlFile.rsrcForkFile()
989 if err != nil {
990 return err
991 }
16a4ad70 992
7cd900d6
JH
993 err = sendFile(rwc, rFile, int(dataOffset))
994 if err != nil {
16a4ad70
JH
995 return err
996 }
85767504 997 }
6988a057 998
16a4ad70 999 // Read the client's Next Action request. This is always 3, I think?
7cd900d6 1000 if _, err := io.ReadFull(rwc, nextAction); err != nil {
85767504 1001 return err
6988a057 1002 }
85767504 1003
16a4ad70 1004 return nil
6988a057
JH
1005 })
1006
1007 case FolderUpload:
92a7e455
JH
1008 dstPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
1009 if err != nil {
1010 return err
1011 }
7cd900d6 1012
6988a057
JH
1013 s.Logger.Infow(
1014 "Folder upload started",
1015 "transactionRef", fileTransfer.ReferenceNumber,
6988a057 1016 "dstPath", dstPath,
92a7e455 1017 "TransferSize", fmt.Sprintf("%x", fileTransfer.TransferSize),
6988a057
JH
1018 "FolderItemCount", fileTransfer.FolderItemCount,
1019 )
1020
1021 // Check if the target folder exists. If not, create it.
b196a50a
JH
1022 if _, err := s.FS.Stat(dstPath); os.IsNotExist(err) {
1023 if err := s.FS.Mkdir(dstPath, 0777); err != nil {
16a4ad70 1024 return err
6988a057
JH
1025 }
1026 }
1027
6988a057 1028 // Begin the folder upload flow by sending the "next file action" to client
7cd900d6 1029 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
6988a057
JH
1030 return err
1031 }
1032
1033 fileSize := make([]byte, 4)
16a4ad70
JH
1034
1035 for i := 0; i < fileTransfer.ItemCount(); i++ {
ba29c43b
JH
1036 s.Stats.UploadCounter += 1
1037
1038 var fu folderUpload
7cd900d6 1039 if _, err := io.ReadFull(rwc, fu.DataSize[:]); err != nil {
ba29c43b
JH
1040 return err
1041 }
7cd900d6 1042 if _, err := io.ReadFull(rwc, fu.IsFolder[:]); err != nil {
ba29c43b
JH
1043 return err
1044 }
7cd900d6 1045 if _, err := io.ReadFull(rwc, fu.PathItemCount[:]); err != nil {
ba29c43b
JH
1046 return err
1047 }
ba29c43b 1048
7cd900d6
JH
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 {
6988a057
JH
1052 return err
1053 }
6988a057
JH
1054
1055 s.Logger.Infow(
1056 "Folder upload continued",
1057 "transactionRef", fmt.Sprintf("%x", fileTransfer.ReferenceNumber),
6988a057
JH
1058 "FormattedPath", fu.FormattedPath(),
1059 "IsFolder", fmt.Sprintf("%x", fu.IsFolder),
c5d9af5a 1060 "PathItemCount", binary.BigEndian.Uint16(fu.PathItemCount[:]),
6988a057
JH
1061 )
1062
c5d9af5a 1063 if fu.IsFolder == [2]byte{0, 1} {
f22acf38
JH
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 {
16a4ad70 1066 return err
6988a057
JH
1067 }
1068 }
1069
1070 // Tell client to send next file
7cd900d6 1071 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
6988a057
JH
1072 return err
1073 }
1074 } else {
16a4ad70
JH
1075 nextAction := dlFldrActionSendFile
1076
1077 // Check if we have the full file already. If so, send dlFldrAction_NextFile to client to skip.
f22acf38 1078 _, err = os.Stat(filepath.Join(dstPath, fu.FormattedPath()))
16a4ad70 1079 if err != nil && !errors.Is(err, fs.ErrNotExist) {
6988a057
JH
1080 return err
1081 }
16a4ad70
JH
1082 if err == nil {
1083 nextAction = dlFldrActionNextFile
1084 }
6988a057 1085
16a4ad70 1086 // Check if we have a partial file already. If so, send dlFldrAction_ResumeFile to client to resume upload.
f22acf38 1087 incompleteFile, err := os.Stat(filepath.Join(dstPath, fu.FormattedPath()+incompleteFileSuffix))
16a4ad70 1088 if err != nil && !errors.Is(err, fs.ErrNotExist) {
85767504 1089 return err
6988a057 1090 }
16a4ad70
JH
1091 if err == nil {
1092 nextAction = dlFldrActionResumeFile
1093 }
6988a057 1094
7cd900d6 1095 if _, err := rwc.Write([]byte{0, uint8(nextAction)}); err != nil {
85767504
JH
1096 return err
1097 }
1098
16a4ad70
JH
1099 switch nextAction {
1100 case dlFldrActionNextFile:
1101 continue
1102 case dlFldrActionResumeFile:
1103 offset := make([]byte, 4)
7cd900d6 1104 binary.BigEndian.PutUint32(offset, uint32(incompleteFile.Size()))
16a4ad70
JH
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
7cd900d6 1111 fileResumeData := NewFileResumeData([]ForkInfoList{*NewForkInfoList(offset)})
16a4ad70
JH
1112
1113 b, _ := fileResumeData.BinaryMarshal()
1114
1115 bs := make([]byte, 2)
1116 binary.BigEndian.PutUint16(bs, uint16(len(b)))
1117
7cd900d6 1118 if _, err := rwc.Write(append(bs, b...)); err != nil {
16a4ad70
JH
1119 return err
1120 }
1121
7cd900d6 1122 if _, err := io.ReadFull(rwc, fileSize); err != nil {
16a4ad70
JH
1123 return err
1124 }
1125
7cd900d6 1126 if err := receiveFile(rwc, file, ioutil.Discard, ioutil.Discard); err != nil {
16a4ad70
JH
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:
7cd900d6 1136 if _, err := io.ReadFull(rwc, fileSize); err != nil {
16a4ad70
JH
1137 return err
1138 }
1139
f22acf38 1140 filePath := filepath.Join(dstPath, fu.FormattedPath())
16a4ad70 1141
7cd900d6 1142 hlFile, err := newFileWrapper(s.FS, filePath, 0)
16a4ad70
JH
1143 if err != nil {
1144 return err
1145 }
1146
7cd900d6
JH
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 }
16a4ad70 1166 }
7cd900d6
JH
1167 if err := receiveFile(rwc, incWriter, rForkWriter, iForkWriter); err != nil {
1168 return err
1169 }
1170 // _ = newFile.Close()
16a4ad70
JH
1171 if err := os.Rename(filePath+".incomplete", filePath); err != nil {
1172 return err
1173 }
6988a057
JH
1174 }
1175
7cd900d6
JH
1176 // Tell client to send next fileWrapper
1177 if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
6988a057
JH
1178 return err
1179 }
6988a057
JH
1180 }
1181 }
1182 s.Logger.Infof("Folder upload complete")
1183 }
1184
1185 return nil
1186}