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