1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
|
package hotline
import (
"bufio"
"bytes"
"context"
"crypto/rand"
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
"io"
"log/slog"
"net"
"strings"
"sync"
"time"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/time/rate"
)
type contextKey string
var contextKeyReq = contextKey("req")
type requestCtx struct {
remoteAddr string
}
type Server struct {
NetInterface string
Port int
rateLimiters map[string]*rateLimiterEntry
rateLimitersMu sync.Mutex
handlers map[TranType]HandlerFunc
Config Config
Logger *slog.Logger
TrackerPassID [4]byte
Stats Counter
FS FileStore // Storage backend to use for File storage
Agreement io.ReadSeeker
banner []byte // server banner image; guarded by bannerMu as it is replaced on config reload
bannerMu sync.RWMutex
FileTransferMgr FileTransferMgr
ChatMgr ChatManager
ClientMgr ClientManager
AccountManager AccountManager
ThreadedNewsMgr ThreadedNewsMgr
BanList BanMgr
MessageBoard io.ReadWriteSeeker
// Presence optionally records user session lifecycle events for an external online-user
// list. When nil, online presence is derived from ClientMgr.
Presence PresenceTracker
// TrackerRegistrar handles tracker registration (injectable for testing)
TrackerRegistrar TrackerRegistrar
TextDecoder *encoding.Decoder
TextEncoder *encoding.Encoder
TLSConfig *tls.Config
TLSPort int
shutdownInit sync.Once // lazily creates shutdownCh so Shutdown works on test-constructed Servers
shutdownOnce sync.Once // guards close(shutdownCh)
shutdownCh chan struct{} // closed by Shutdown to stop ListenAndServe
// activeConns tracks every accepted connection (sessions and file transfers) so shutdown can
// force-close them, which unblocks their read loops. connWG counts the goroutines serving
// them (including the client writer goroutines) so ListenAndServe can wait for all of them to
// finish before returning. activeConns is lazily initialized under activeConnsMu so
// test-constructed Servers keep working.
activeConns map[io.Closer]struct{}
activeConnsMu sync.Mutex
connsClosed bool // set once shutdown force-closes connections; later conns are closed on arrival
connWG sync.WaitGroup
}
func (s *Server) initShutdownCh() {
s.shutdownInit.Do(func() { s.shutdownCh = make(chan struct{}) })
}
// trackConn registers an accepted connection for forced close on shutdown. It reports whether
// the connection was registered: once shutdown has begun the connection is closed instead, and
// the caller must not serve it.
func (s *Server) trackConn(conn io.Closer) bool {
s.activeConnsMu.Lock()
defer s.activeConnsMu.Unlock()
if s.connsClosed {
_ = conn.Close()
return false
}
if s.activeConns == nil {
s.activeConns = make(map[io.Closer]struct{})
}
s.activeConns[conn] = struct{}{}
return true
}
func (s *Server) untrackConn(conn io.Closer) {
s.activeConnsMu.Lock()
defer s.activeConnsMu.Unlock()
delete(s.activeConns, conn)
}
// closeActiveConns force-closes all tracked connections, unblocking the read loops of the
// goroutines serving them. Connections accepted after this are closed on arrival by trackConn.
func (s *Server) closeActiveConns() {
s.activeConnsMu.Lock()
defer s.activeConnsMu.Unlock()
s.connsClosed = true
for conn := range s.activeConns {
_ = conn.Close()
}
}
// Banner returns the server banner image. Callers must not modify the returned slice.
func (s *Server) Banner() []byte {
s.bannerMu.RLock()
defer s.bannerMu.RUnlock()
return s.banner
}
// SetBanner replaces the server banner image.
func (s *Server) SetBanner(banner []byte) {
s.bannerMu.Lock()
defer s.bannerMu.Unlock()
s.banner = banner
}
type Option = func(s *Server)
func WithConfig(config Config) func(s *Server) {
return func(s *Server) {
s.Config = config
}
}
func WithLogger(logger *slog.Logger) func(s *Server) {
return func(s *Server) {
s.Logger = logger
}
}
// WithPort optionally overrides the default TCP port.
func WithPort(port int) func(s *Server) {
return func(s *Server) {
s.Port = port
}
}
// WithInterface optionally sets a specific interface to listen on.
func WithInterface(netInterface string) func(s *Server) {
return func(s *Server) {
s.NetInterface = netInterface
}
}
// WithTrackerRegistrar optionally sets a custom tracker registrar (useful for testing).
func WithTrackerRegistrar(registrar TrackerRegistrar) func(s *Server) {
return func(s *Server) {
s.TrackerRegistrar = registrar
}
}
// WithPresenceTracker optionally sets a PresenceTracker to record user session lifecycle events.
func WithPresenceTracker(p PresenceTracker) func(s *Server) {
return func(s *Server) {
s.Presence = p
}
}
// WithFileStore sets the storage backend for the file library. Defaults to OSFileStore.
func WithFileStore(fs FileStore) func(s *Server) {
return func(s *Server) {
s.FS = fs
}
}
// WithTLS optionally enables TLS support on the specified port.
func WithTLS(tlsConfig *tls.Config, port int) func(s *Server) {
return func(s *Server) {
s.TLSConfig = tlsConfig
s.TLSPort = port
}
}
type ServerConfig struct {
}
func NewServer(options ...Option) (*Server, error) {
server := Server{
handlers: make(map[TranType]HandlerFunc),
rateLimiters: make(map[string]*rateLimiterEntry),
FS: &OSFileStore{},
ChatMgr: NewMemChatManager(),
ClientMgr: NewMemClientMgr(),
FileTransferMgr: NewMemFileTransferMgr(),
Stats: NewStats(),
TrackerRegistrar: NewRealTrackerRegistrar(),
}
for _, opt := range options {
opt(&server)
}
// Initialize text encoding based on config.
switch server.Config.Encoding {
case "utf8":
server.TextDecoder = encoding.Nop.NewDecoder()
server.TextEncoder = encoding.Nop.NewEncoder()
default:
server.TextDecoder = charmap.Macintosh.NewDecoder()
server.TextEncoder = charmap.Macintosh.NewEncoder()
}
// generate a new random passID for tracker registration
_, err := rand.Read(server.TrackerPassID[:])
if err != nil {
return nil, err
}
return &server, nil
}
func (s *Server) CurrentStats() StatValues {
return s.Stats.Values()
}
// ListenAndServe starts the Hotline and file transfer listeners and blocks until the context is
// canceled, Shutdown is called, or a serve loop fails. Canceling the context closes all
// listeners, which unblocks their accept loops. Accepted connections are then force-closed and
// ListenAndServe waits for every session and file transfer goroutine to finish before returning.
func (s *Server) ListenAndServe(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Cancel the context when Shutdown is called.
s.initShutdownCh()
go func() {
select {
case <-s.shutdownCh:
cancel()
case <-ctx.Done():
}
}()
go s.registerWithTrackers(ctx)
go s.keepaliveHandler(ctx)
errCh := make(chan error, 4)
listen := func(port int, serve func(context.Context, net.Listener) error) error {
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", s.NetInterface, port))
if err != nil {
return err
}
// Close the listener when the context is canceled to unblock Accept.
go func() {
<-ctx.Done()
_ = ln.Close()
}()
go func() { errCh <- serve(ctx, ln) }()
return nil
}
if err := listen(s.Port, s.Serve); err != nil {
return err
}
if err := listen(s.Port+1, s.ServeFileTransfers); err != nil {
return err
}
if s.TLSConfig != nil {
if err := listen(s.TLSPort, s.ServeWithTLS); err != nil {
return err
}
if err := listen(s.TLSPort+1, s.ServeFileTransfersWithTLS); err != nil {
return err
}
}
// Block until the first serve loop returns, then cancel to close the remaining listeners and
// stop their serve loops.
err := <-errCh
if ctx.Err() != nil {
s.Logger.Info("Server shutting down")
}
cancel()
// Force-close accepted connections to unblock their read loops, then wait for every session,
// file transfer, and client writer goroutine to finish before returning.
s.closeActiveConns()
s.connWG.Wait()
return err
}
func (s *Server) ServeFileTransfers(ctx context.Context, ln net.Listener) error {
for {
conn, err := ln.Accept()
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
return err
}
s.connWG.Add(1)
go func() {
defer s.connWG.Done()
if !s.trackConn(conn) {
return
}
defer s.untrackConn(conn)
defer func() { _ = conn.Close() }()
err := s.handleFileTransfer(
context.WithValue(ctx, contextKeyReq, requestCtx{remoteAddr: conn.RemoteAddr().String()}),
conn,
)
if err != nil {
s.Logger.Error("file transfer error", "err", err)
}
}()
}
}
func (s *Server) ServeWithTLS(ctx context.Context, ln net.Listener) error {
return s.Serve(ctx, tls.NewListener(ln, s.TLSConfig))
}
func (s *Server) ServeFileTransfersWithTLS(ctx context.Context, ln net.Listener) error {
return s.ServeFileTransfers(ctx, tls.NewListener(ln, s.TLSConfig))
}
// Send routes t to the send queue of the client identified by t.ClientID. Transactions for
// clients that are no longer connected are dropped.
func (s *Server) Send(t Transaction) {
if c := s.ClientMgr.Get(t.ClientID); c != nil {
c.Send(t)
}
}
// perIPRateLimit controls how frequently an IP address can connect before being throttled.
// 0.5 = 1 connection every 2 seconds
const perIPRateLimit = rate.Limit(0.5)
// rateLimiterTTL is how long the rate limiter for an idle IP address is retained before eviction.
const rateLimiterTTL = 7 * 24 * time.Hour
type rateLimiterEntry struct {
limiter *rate.Limiter
lastSeen time.Time
}
// sweepRateLimiters evicts rate limiters for IP addresses that have not connected recently, so
// the rate limiter map does not grow unbounded over the lifetime of the server.
func (s *Server) sweepRateLimiters() {
cutoff := time.Now().Add(-rateLimiterTTL)
s.rateLimitersMu.Lock()
defer s.rateLimitersMu.Unlock()
for ip, entry := range s.rateLimiters {
if entry.lastSeen.Before(cutoff) {
delete(s.rateLimiters, ip)
}
}
}
func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
for {
conn, err := ln.Accept()
if err != nil {
// Context cancellation closes the listener, unblocking Accept.
if ctx.Err() != nil {
return ctx.Err()
}
if errors.Is(err, net.ErrClosed) {
return err
}
s.Logger.Error("Error accepting connection", "err", err)
continue
}
s.connWG.Add(1)
go func() {
defer s.connWG.Done()
if !s.trackConn(conn) {
return
}
defer s.untrackConn(conn)
ipAddr, _, _ := net.SplitHostPort(conn.RemoteAddr().String())
connCtx := context.WithValue(ctx, contextKeyReq, requestCtx{
remoteAddr: conn.RemoteAddr().String(),
})
s.Logger.Info("Connection established", "ip", ipAddr)
defer func() { _ = conn.Close() }()
// Check if we have an existing rate limit for the IP and create one if we do not.
s.rateLimitersMu.Lock()
entry, ok := s.rateLimiters[ipAddr]
if !ok {
entry = &rateLimiterEntry{limiter: rate.NewLimiter(perIPRateLimit, 1)}
s.rateLimiters[ipAddr] = entry
}
entry.lastSeen = time.Now()
rl := entry.limiter
s.rateLimitersMu.Unlock()
// Check if the rate limit is exceeded and close the connection if so.
if !rl.Allow() {
s.Logger.Info("Rate limit exceeded", "remoteAddr", conn.RemoteAddr())
_ = conn.Close()
return
}
if err := s.handleNewConnection(connCtx, conn, conn.RemoteAddr().String()); err != nil {
if err == io.EOF {
s.Logger.Info("Client disconnected", "remoteAddr", conn.RemoteAddr())
} else {
s.Logger.Error("Error serving request", "remoteAddr", conn.RemoteAddr(), "err", err)
}
}
}()
}
}
// time in seconds between tracker re-registration
const trackerUpdateFrequency = 300
// TrackerRegistrar interface for tracker registration operations
type TrackerRegistrar interface {
Register(tracker string, registration *TrackerRegistration) error
}
// RealTrackerRegistrar implements TrackerRegistrar using the real network operations
type RealTrackerRegistrar struct {
dialer Dialer
}
func NewRealTrackerRegistrar() *RealTrackerRegistrar {
return &RealTrackerRegistrar{
dialer: &RealDialer{},
}
}
func (r *RealTrackerRegistrar) Register(tracker string, registration *TrackerRegistration) error {
return register(r.dialer, tracker, registration)
}
// parseTrackerPassword extracts the password from a tracker address in format "host:port:password"
// Returns empty string if no password is present or if the format is invalid
// For addresses with more than 3 parts (like passwords containing colons), everything after the second colon is treated as the password
func parseTrackerPassword(trackerAddr string) string {
splitAddr := strings.Split(trackerAddr, ":")
if len(splitAddr) >= 3 {
// Join everything from the third part onwards (index 2+) to handle passwords with colons
return strings.Join(splitAddr[2:], ":")
}
return ""
}
// registerWithAllTrackers performs tracker registration for all configured trackers
func (s *Server) registerWithAllTrackers() {
if !s.Config.EnableTrackerRegistration {
return
}
for _, t := range s.Config.Trackers {
tr := &TrackerRegistration{
UserCount: len(s.ClientMgr.List()),
PassID: s.TrackerPassID,
Name: s.Config.Name,
Description: s.Config.Description,
}
binary.BigEndian.PutUint16(tr.Port[:], uint16(s.Port))
binary.BigEndian.PutUint16(tr.TLSPort[:], uint16(s.TLSPort))
tr.Password = parseTrackerPassword(t)
if err := s.TrackerRegistrar.Register(t, tr); err != nil {
s.Logger.Error("Unable to register with tracker", "tracker", t, "err", err)
}
}
}
// registerWithTrackers runs every trackerUpdateFrequency seconds to update the server's tracker entry on all configured
// trackers.
func (s *Server) registerWithTrackers(ctx context.Context) {
if s.Config.EnableTrackerRegistration {
s.Logger.Info("Tracker registration enabled", "trackers", s.Config.Trackers)
}
// Do the first registration immediately
s.registerWithAllTrackers()
ticker := time.NewTicker(trackerUpdateFrequency * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.registerWithAllTrackers()
}
}
}
const (
userIdleSeconds = 300 // time in seconds before an inactive user is marked idle
idleCheckInterval = 10 // time in seconds to check for idle users
)
// keepaliveHandler runs every idleCheckInterval seconds and increments a user's idle time by idleCheckInterval seconds.
// If the updated idle time exceeds userIdleSeconds and the user was not previously idle, we notify all connected clients
// that the user has gone idle. For most clients, this turns the user grey in the user list.
// It also sweeps stale per-IP rate limiters on each tick.
func (s *Server) keepaliveHandler(ctx context.Context) {
ticker := time.NewTicker(idleCheckInterval * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
for _, c := range s.ClientMgr.List() {
if c.incrementIdleTime(idleCheckInterval) {
c.SendAll(
TranNotifyChangeUser,
NewField(FieldUserID, c.ID[:]),
NewField(FieldUserFlags, c.FlagBytes()),
NewField(FieldUserName, c.GetUserName()),
NewField(FieldUserIconID, c.GetIcon()),
)
}
}
s.sweepRateLimiters()
}
}
}
func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *ClientConn {
clientConn := &ClientConn{
Icon: []byte{0, 0}, // TODO: make array type
Connection: conn,
Server: s,
RemoteAddr: remoteAddr,
ClientFileTransferMgr: NewClientFileTransferMgr(),
}
return clientConn
}
func sendBanMessage(rwc io.Writer, message string) {
t := NewTransaction(
TranServerMsg,
[2]byte{0, 0},
NewField(FieldData, []byte(message)),
NewField(FieldChatOptions, []byte{0, 0}),
)
_, _ = io.Copy(rwc, &t)
time.Sleep(1 * time.Second)
}
// handleNewConnection takes a new net.Conn and performs the initial login sequence
func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error {
defer dontPanic(s.Logger)
if err := performHandshake(rwc); err != nil {
return fmt.Errorf("perform handshake: %w", err)
}
// Create a new scanner for parsing incoming bytes into transaction tokens
scanner := bufio.NewScanner(rwc)
scanner.Split(transactionScanner)
scanner.Scan()
// Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the
// scanner re-uses the buffer for subsequent scans.
buf := make([]byte, len(scanner.Bytes()))
copy(buf, scanner.Bytes())
var clientLogin Transaction
if _, err := clientLogin.Write(buf); err != nil {
return fmt.Errorf("error writing login transaction: %w", err)
}
login := clientLogin.GetField(FieldUserLogin).DecodeObfuscatedString()
if login == "" {
login = GuestAccount
}
// Check if remoteAddr is present in the ban list, we do this after we have the login name
ipAddr, _, _ := net.SplitHostPort(remoteAddr)
// Check if user is banned
if s.BanList != nil && s.BanList.IsUsernameBanned(login) {
_ = s.BanList.Add(ipAddr, nil)
sendBanMessage(rwc, "You are banned on this server")
s.Logger.Debug("Disconnecting banned user", "login", login, "ip", ipAddr)
return nil
}
// Check if IP is banned
if s.BanList != nil {
if isBanned, banUntil := s.BanList.IsBanned(ipAddr); isBanned {
// permaban
if banUntil == nil {
sendBanMessage(rwc, "You are permanently banned on this server")
s.Logger.Debug("Disconnecting permanently banned IP", "remoteAddr", ipAddr)
return nil
}
// temporary ban
if time.Now().Before(*banUntil) {
sendBanMessage(rwc, "You are temporarily banned on this server")
s.Logger.Debug("Disconnecting temporarily banned IP", "remoteAddr", ipAddr)
return nil
}
}
}
c := s.NewClientConn(rwc, remoteAddr)
// Start the client's writer goroutine: the single writer to the connection, which preserves
// transaction ordering and prevents interleaved writes. It exits when the send queue is
// closed by Disconnect, so it is joined by the same WaitGroup as the session goroutines.
s.connWG.Add(1)
go func() {
defer s.connWG.Done()
c.writeLoop()
}()
if s.Presence != nil {
s.Presence.UserConnected(login, ipAddr)
}
// Remove the client from the list of connected clients when they disconnect
defer func() {
if s.Presence != nil {
s.Presence.UserDisconnected(login, string(c.GetUserName()), ipAddr)
}
c.Disconnect()
}()
encodedPassword := clientLogin.GetField(FieldUserPassword).Data
c.Version = clientLogin.GetField(FieldVersion).Data
c.Logger = s.Logger.With("ip", ipAddr, "login", login)
// If authentication fails, send error reply and close connection
if !c.Authenticate(login, encodedPassword) {
t := c.NewErrReply(&clientLogin, "Incorrect login.")[0]
_, err := io.Copy(rwc, &t)
if err != nil {
return err
}
c.Logger.Info("Incorrect login")
return nil
}
if clientLogin.GetField(FieldUserIconID).Data != nil {
c.SetIcon(clientLogin.GetField(FieldUserIconID).Data)
}
account := c.Server.AccountManager.Get(login)
if account == nil {
return nil
}
c.SetAccount(account)
if clientLogin.GetField(FieldUserName).Data != nil {
if c.Authorize(AccessAnyName) {
c.SetUserName(clientLogin.GetField(FieldUserName).Data)
} else {
c.SetUserName([]byte(account.Name))
}
}
if c.Authorize(AccessDisconUser) {
c.SetFlag(UserFlagAdmin, 1)
}
// Publish the client to the manager only now that its session state (Account, Version,
// UserName, Flags) is fully initialized. Other goroutines iterate ClientMgr.List() and
// dereference Account, so a client must never be visible before login completes.
s.ClientMgr.Add(c)
c.Send(c.NewReply(&clientLogin,
NewField(FieldVersion, []byte{0x00, 0xbe}),
NewField(FieldCommunityBannerID, []byte{0, 0}),
NewField(FieldServerName, []byte(s.Config.Name)),
))
// Send user access privs so client UI knows how to behave
c.Send(NewTransaction(TranUserAccess, c.ID, NewField(FieldUserAccess, c.AccessBytes())))
// Accounts with AccessNoAgreement do not receive the server agreement on login. The behavior is different between
// client versions. For 1.2.3 client, we do not send TranShowAgreement. For other client versions, we send
// TranShowAgreement but with the NoServerAgreement field set to 1.
if c.Authorize(AccessNoAgreement) {
// If client version is nil, then the client uses the 1.2.3 login behavior
if c.Version != nil {
c.Send(NewTransaction(TranShowAgreement, c.ID, NewField(FieldNoServerAgreement, []byte{1})))
}
} else {
_, _ = c.Server.Agreement.Seek(0, 0)
data, _ := io.ReadAll(c.Server.Agreement)
c.Send(NewTransaction(TranShowAgreement, c.ID, NewField(FieldData, data)))
}
// If the client has provided a username as part of the login, we can infer that it is using the 1.2.3 login
// flow and not the 1.5+ flow.
if userName := c.GetUserName(); len(userName) != 0 {
// Add the client username to the logger. For 1.5+ clients, we don't have this information yet as it comes as
// part of TranAgreed
c.Logger = c.Logger.With("name", string(userName))
c.Logger.Info("Login successful")
if s.Presence != nil {
s.Presence.UserRenamed(login, "", string(userName), ipAddr)
}
// Notify other clients on the server that the new user has logged in. For 1.5+ clients we don't have this
// information yet, so we do it in TranAgreed instead
for _, t := range c.NotifyOthers(
NewTransaction(
TranNotifyChangeUser, [2]byte{0, 0},
NewField(FieldUserName, userName),
NewField(FieldUserID, c.ID[:]),
NewField(FieldUserIconID, c.GetIcon()),
NewField(FieldUserFlags, c.FlagBytes()),
),
) {
c.Server.Send(t)
}
}
c.Server.Stats.Increment(StatConnectionCounter, StatCurrentlyConnected)
defer c.Server.Stats.Decrement(StatCurrentlyConnected)
c.Server.Stats.Max(StatConnectionPeak, len(s.ClientMgr.List()))
// Scan for new transactions and handle them as they come in.
for scanner.Scan() {
// Copy the scanner bytes to a new slice to it to avoid a data race when the scanner re-uses the buffer.
tmpBuf := make([]byte, len(scanner.Bytes()))
copy(tmpBuf, scanner.Bytes())
var t Transaction
if _, err := t.Write(tmpBuf); err != nil {
return err
}
c.handleTransaction(t)
}
return nil
}
// handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection
func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) error {
defer dontPanic(s.Logger)
// The first 16 bytes contain the file transfer.
var t transfer
if _, err := io.CopyN(&t, rwc, 16); err != nil {
return fmt.Errorf("error reading file transfer: %w", err)
}
fileTransfer := s.FileTransferMgr.Get(t.ReferenceNumber)
if fileTransfer == nil {
return fmt.Errorf("invalid transaction ID: %v", t.ReferenceNumber)
}
defer func() {
s.FileTransferMgr.Delete(t.ReferenceNumber)
// Wait a few seconds before closing the connection: this is a workaround for problems
// observed with Windows clients where the client must initiate close of the TCP connection before
// the server does. This is gross and seems unnecessary. TODO: Revisit?
// Skipped during shutdown, when it would only delay process exit.
select {
case <-ctx.Done():
case <-time.After(3 * time.Second):
}
}()
var remoteAddr string
if rc, ok := ctx.Value(contextKeyReq).(requestCtx); ok {
remoteAddr = rc.remoteAddr
}
rLogger := s.Logger.With(
"remoteAddr", remoteAddr,
"login", fileTransfer.ClientConn.GetAccount().Login,
"Name", string(fileTransfer.ClientConn.GetUserName()),
)
fullPath, err := ReadPath(fileTransfer.FileRoot, fileTransfer.FilePath, fileTransfer.FileName, s.TextDecoder)
if err != nil {
return err
}
switch fileTransfer.Type {
case BannerDownload:
if _, err := io.Copy(rwc, bytes.NewReader(s.Banner())); err != nil {
return fmt.Errorf("banner download: %w", err)
}
case FileDownload:
s.Stats.Increment(StatDownloadCounter, StatDownloadsInProgress)
defer func() {
s.Stats.Decrement(StatDownloadsInProgress)
}()
err = DownloadHandler(rwc, fullPath, fileTransfer, s.FS, rLogger, true)
if err != nil {
return fmt.Errorf("file download: %w", err)
}
case FileUpload:
s.Stats.Increment(StatUploadCounter, StatUploadsInProgress)
defer func() {
s.Stats.Decrement(StatUploadsInProgress)
}()
err = UploadHandler(rwc, fullPath, fileTransfer, s.FS, rLogger, s.Config.PreserveResourceForks)
if err != nil {
return fmt.Errorf("file upload: %w", err)
}
case FolderDownload:
s.Stats.Increment(StatDownloadCounter, StatDownloadsInProgress)
defer func() {
s.Stats.Decrement(StatDownloadsInProgress)
}()
err = DownloadFolderHandler(rwc, fullPath, fileTransfer, s.FS, rLogger, s.Config.PreserveResourceForks)
if err != nil {
return fmt.Errorf("folder download: %w", err)
}
case FolderUpload:
s.Stats.Increment(StatUploadCounter, StatUploadsInProgress)
defer func() {
s.Stats.Decrement(StatUploadsInProgress)
}()
var transferSizeValue uint32
switch len(fileTransfer.TransferSize) {
case 2: // 16-bit
transferSizeValue = uint32(binary.BigEndian.Uint16(fileTransfer.TransferSize))
case 4: // 32-bit
transferSizeValue = binary.BigEndian.Uint32(fileTransfer.TransferSize)
default:
rLogger.Warn("Unexpected TransferSize length", "bytes", len(fileTransfer.TransferSize))
}
rLogger.Info(
"Folder upload started",
"dstPath", fullPath,
"TransferSize", transferSizeValue,
"FolderItemCount", fileTransfer.FolderItemCount,
)
err = UploadFolderHandler(rwc, fullPath, fileTransfer, s.FS, rLogger, s.Config.PreserveResourceForks)
if err != nil {
return fmt.Errorf("folder upload: %w", err)
}
}
return nil
}
func (s *Server) SendAll(t TranType, fields ...Field) {
for _, c := range s.ClientMgr.List() {
c.Send(NewTransaction(t, c.ID, fields...))
}
}
// Shutdown sends msg to all connected clients and stops ListenAndServe, which force-closes any
// remaining connections and waits for their goroutines to finish.
func (s *Server) Shutdown(msg []byte) {
s.Logger.Info("Shutdown signal received")
s.SendAll(TranDisconnectMsg, NewField(FieldData, msg))
// Give the client writer goroutines a moment to flush the disconnect message.
time.Sleep(3 * time.Second)
s.initShutdownCh()
s.shutdownOnce.Do(func() { close(s.shutdownCh) })
}
|