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