-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathinstances.go
More file actions
921 lines (814 loc) · 32.4 KB
/
instances.go
File metadata and controls
921 lines (814 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
// Copyright (c) 2025-2026, s0up and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package handlers
import (
"context"
"encoding/json"
"errors"
"net/http"
"slices"
"strconv"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
"github.com/autobrr/qui/internal/domain"
"github.com/autobrr/qui/internal/models"
internalqbittorrent "github.com/autobrr/qui/internal/qbittorrent"
"github.com/autobrr/qui/internal/services/reannounce"
)
type InstancesHandler struct {
instanceStore *models.InstanceStore
reannounceStore *models.InstanceReannounceStore
reannounceCache *reannounce.SettingsCache
clientPool *internalqbittorrent.ClientPool
syncManager *internalqbittorrent.SyncManager
reannounceSvc *reannounce.Service
}
func NewInstancesHandler(instanceStore *models.InstanceStore, reannounceStore *models.InstanceReannounceStore, reannounceCache *reannounce.SettingsCache, clientPool *internalqbittorrent.ClientPool, syncManager *internalqbittorrent.SyncManager, svc *reannounce.Service) *InstancesHandler {
return &InstancesHandler{
instanceStore: instanceStore,
reannounceStore: reannounceStore,
reannounceCache: reannounceCache,
clientPool: clientPool,
syncManager: syncManager,
reannounceSvc: svc,
}
}
// GetInstanceCapabilities returns lightweight capability metadata for an instance.
func (h *InstancesHandler) GetInstanceCapabilities(w http.ResponseWriter, r *http.Request) {
instanceID, err := strconv.Atoi(chi.URLParam(r, "instanceID"))
if err != nil {
RespondError(w, http.StatusBadRequest, "Invalid instance ID")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
client, err := h.clientPool.GetClientOffline(ctx, instanceID)
if err != nil {
client, err = h.clientPool.GetClientWithTimeout(ctx, instanceID, 15*time.Second)
if err != nil {
if respondIfInstanceDisabled(w, err, instanceID, "instances:getCapabilities") {
return
}
log.Error().Err(err).Int("instanceID", instanceID).Msg("Failed to get client for capabilities")
RespondError(w, http.StatusServiceUnavailable, "Failed to load instance capabilities")
return
}
}
if client.GetWebAPIVersion() == "" {
if err := client.RefreshCapabilities(ctx); err != nil {
log.Error().
Err(err).
Int("instanceID", instanceID).
Msg("Unable to refresh qBittorrent capabilities during request")
}
}
capabilities := NewInstanceCapabilitiesResponse(client)
RespondJSON(w, http.StatusOK, capabilities)
}
// GetReannounceActivity returns recent reannounce events for an instance.
func (h *InstancesHandler) GetReannounceActivity(w http.ResponseWriter, r *http.Request) {
instanceID, err := strconv.Atoi(chi.URLParam(r, "instanceID"))
if err != nil {
RespondError(w, http.StatusBadRequest, "Invalid instance ID")
return
}
if h.reannounceSvc == nil {
RespondJSON(w, http.StatusOK, []reannounce.ActivityEvent{})
return
}
limitParam := strings.TrimSpace(r.URL.Query().Get("limit"))
var limit int
if limitParam != "" {
if parsed, err := strconv.Atoi(limitParam); err == nil && parsed > 0 {
limit = parsed
}
}
events := h.reannounceSvc.GetActivity(instanceID, limit)
if events == nil {
events = []reannounce.ActivityEvent{}
}
normalized := make([]reannounce.ActivityEvent, len(events))
for i, event := range events {
event.Hash = strings.ToLower(event.Hash)
normalized[i] = event
}
RespondJSON(w, http.StatusOK, normalized)
}
// GetReannounceCandidates returns torrents that currently fall within the
// reannounce monitoring scope and either have tracker problems or are still
// waiting for their initial tracker contact.
func (h *InstancesHandler) GetReannounceCandidates(w http.ResponseWriter, r *http.Request) {
instanceID, err := strconv.Atoi(chi.URLParam(r, "instanceID"))
if err != nil {
RespondError(w, http.StatusBadRequest, "Invalid instance ID")
return
}
if h.reannounceSvc == nil {
RespondJSON(w, http.StatusOK, []reannounce.MonitoredTorrent{})
return
}
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
candidates := h.reannounceSvc.GetMonitoredTorrents(ctx, instanceID)
if candidates == nil {
candidates = []reannounce.MonitoredTorrent{}
}
normalized := make([]reannounce.MonitoredTorrent, len(candidates))
for i, candidate := range candidates {
candidate.Hash = strings.ToLower(candidate.Hash)
normalized[i] = candidate
}
RespondJSON(w, http.StatusOK, normalized)
}
// GetTransferInfo returns lightweight transfer stats for an instance.
func (h *InstancesHandler) GetTransferInfo(w http.ResponseWriter, r *http.Request) {
instanceID, err := strconv.Atoi(chi.URLParam(r, "instanceID"))
if err != nil {
RespondError(w, http.StatusBadRequest, "Invalid instance ID")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
client, err := h.clientPool.GetClient(ctx, instanceID)
if err != nil {
if respondIfInstanceDisabled(w, err, instanceID, "instances:getTransferInfo") {
return
}
log.Error().Err(err).Int("instanceID", instanceID).Msg("Failed to get client for transfer info")
RespondError(w, http.StatusServiceUnavailable, "Failed to load transfer info")
return
}
info, err := client.GetTransferInfoCtx(ctx)
if err != nil {
log.Error().Err(err).Int("instanceID", instanceID).Msg("Failed to get transfer info")
RespondError(w, http.StatusInternalServerError, "Failed to get transfer info")
return
}
RespondJSON(w, http.StatusOK, info)
}
func (h *InstancesHandler) buildInstanceResponsesParallel(ctx context.Context, instances []*models.Instance) []InstanceResponse {
if len(instances) == 0 {
return []InstanceResponse{}
}
type result struct {
index int
response InstanceResponse
}
resultCh := make(chan result, len(instances))
for i, instance := range instances {
go func(index int, inst *models.Instance) {
response := h.buildInstanceResponse(ctx, inst)
resultCh <- result{index: index, response: response}
}(i, instance)
}
responses := make([]InstanceResponse, len(instances))
for i := range instances {
select {
case res := <-resultCh:
responses[res.index] = res.response
case <-ctx.Done():
// Handle context cancellation gracefully
responses[i] = InstanceResponse{
ID: instances[i].ID,
Name: instances[i].Name,
Host: instances[i].Host,
Username: instances[i].Username,
BasicUsername: instances[i].BasicUsername,
TLSSkipVerify: instances[i].TLSSkipVerify,
HasLocalFilesystemAccess: instances[i].HasLocalFilesystemAccess,
UseHardlinks: instances[i].UseHardlinks,
HardlinkBaseDir: instances[i].HardlinkBaseDir,
HardlinkDirPreset: instances[i].HardlinkDirPreset,
LinkDirName: instances[i].LinkDirName,
UseReflinks: instances[i].UseReflinks,
FallbackToRegularMode: instances[i].FallbackToRegularMode,
Connected: false,
HasDecryptionError: false,
SortOrder: instances[i].SortOrder,
IsActive: instances[i].IsActive,
ReannounceSettings: payloadFromModel(models.DefaultInstanceReannounceSettings(instances[i].ID)),
ConnectionStatus: func(active bool) string {
if !active {
return "disabled"
}
return ""
}(instances[i].IsActive),
}
}
}
return responses
}
// buildInstanceResponse creates a consistent response for an instance
func (h *InstancesHandler) buildInstanceResponse(ctx context.Context, instance *models.Instance) InstanceResponse {
// Use cached connection status only, do not test connection synchronously
client, _ := h.clientPool.GetClientOffline(ctx, instance.ID)
healthy := client != nil && client.IsHealthy() && instance.IsActive
var connectionStatus string
if !instance.IsActive {
connectionStatus = "disabled"
} else if client != nil {
if status := strings.TrimSpace(client.GetCachedConnectionStatus()); status != "" {
connectionStatus = strings.ToLower(status)
}
}
decryptionErrorInstances := h.clientPool.GetInstancesWithDecryptionErrors()
hasDecryptionError := slices.Contains(decryptionErrorInstances, instance.ID)
response := InstanceResponse{
ID: instance.ID,
Name: instance.Name,
Host: instance.Host,
Username: instance.Username,
BasicUsername: instance.BasicUsername,
TLSSkipVerify: instance.TLSSkipVerify,
HasLocalFilesystemAccess: instance.HasLocalFilesystemAccess,
UseHardlinks: instance.UseHardlinks,
HardlinkBaseDir: instance.HardlinkBaseDir,
HardlinkDirPreset: instance.HardlinkDirPreset,
LinkDirName: instance.LinkDirName,
UseReflinks: instance.UseReflinks,
FallbackToRegularMode: instance.FallbackToRegularMode,
Connected: healthy,
HasDecryptionError: hasDecryptionError,
ConnectionStatus: connectionStatus,
SortOrder: instance.SortOrder,
IsActive: instance.IsActive,
}
response.ReannounceSettings = h.getReannounceSettingsPayload(ctx, instance.ID)
// Fetch recent errors for disconnected instances
if instance.IsActive && !healthy {
errorStore := h.clientPool.GetErrorStore()
recentErrors, err := errorStore.GetRecentErrors(ctx, instance.ID, 5)
if err != nil {
log.Error().Err(err).Int("instanceID", instance.ID).Msg("Failed to get recent errors")
} else {
response.RecentErrors = recentErrors
}
}
return response
}
// buildQuickInstanceResponse creates a response without testing connection
func (h *InstancesHandler) buildQuickInstanceResponse(instance *models.Instance) InstanceResponse {
connectionStatus := ""
if !instance.IsActive {
connectionStatus = "disabled"
}
return InstanceResponse{
ID: instance.ID,
Name: instance.Name,
Host: instance.Host,
Username: instance.Username,
BasicUsername: instance.BasicUsername,
TLSSkipVerify: instance.TLSSkipVerify,
HasLocalFilesystemAccess: instance.HasLocalFilesystemAccess,
UseHardlinks: instance.UseHardlinks,
HardlinkBaseDir: instance.HardlinkBaseDir,
HardlinkDirPreset: instance.HardlinkDirPreset,
LinkDirName: instance.LinkDirName,
UseReflinks: instance.UseReflinks,
FallbackToRegularMode: instance.FallbackToRegularMode,
Connected: false, // Will be updated asynchronously
HasDecryptionError: false,
SortOrder: instance.SortOrder,
IsActive: instance.IsActive,
ConnectionStatus: connectionStatus,
}
}
// testConnectionAsync tests connection in background and updates cache
func (h *InstancesHandler) testConnectionAsync(instanceID int) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
log.Debug().Int("instanceID", instanceID).Msg("Testing connection asynchronously")
client, err := h.clientPool.GetClient(ctx, instanceID)
if err != nil {
log.Debug().Err(err).Int("instanceID", instanceID).Msg("Async connection test failed")
return
}
if err := client.HealthCheck(ctx); err != nil {
log.Debug().Err(err).Int("instanceID", instanceID).Msg("Async health check failed")
return
}
log.Debug().Int("instanceID", instanceID).Msg("Async connection test succeeded")
}
func (h *InstancesHandler) loadReannounceSettings(ctx context.Context, instanceID int) *models.InstanceReannounceSettings {
if h.reannounceStore == nil {
return models.DefaultInstanceReannounceSettings(instanceID)
}
settings, err := h.reannounceStore.Get(ctx, instanceID)
if err != nil {
log.Error().Err(err).Int("instanceID", instanceID).Msg("Failed to load reannounce settings")
return models.DefaultInstanceReannounceSettings(instanceID)
}
return settings
}
func (h *InstancesHandler) getReannounceSettingsPayload(ctx context.Context, instanceID int) InstanceReannounceSettingsPayload {
if h.reannounceCache != nil {
if cached := h.reannounceCache.Get(instanceID); cached != nil {
return payloadFromModel(cached)
}
}
return payloadFromModel(h.loadReannounceSettings(ctx, instanceID))
}
func (h *InstancesHandler) persistReannounceSettings(ctx context.Context, instanceID int, payload *InstanceReannounceSettingsPayload) (*models.InstanceReannounceSettings, error) {
desired := payload.toModel(instanceID, nil)
if h.reannounceStore == nil {
if h.reannounceCache != nil {
h.reannounceCache.Replace(desired)
}
return desired, nil
}
saved, err := h.reannounceStore.Upsert(ctx, desired)
if err != nil {
log.Error().Err(err).Int("instanceID", instanceID).Msg("Failed to persist reannounce settings")
return nil, err
}
if h.reannounceCache != nil {
h.reannounceCache.Replace(saved)
}
return saved, nil
}
// CreateInstanceRequest represents a request to create a new instance
type CreateInstanceRequest struct {
Name string `json:"name"`
Host string `json:"host"`
Username string `json:"username"`
Password string `json:"password"`
BasicUsername *string `json:"basicUsername,omitempty"`
BasicPassword *string `json:"basicPassword,omitempty"`
TLSSkipVerify bool `json:"tlsSkipVerify,omitempty"`
HasLocalFilesystemAccess *bool `json:"hasLocalFilesystemAccess,omitempty"`
LinkDirName *string `json:"linkDirName,omitempty"`
ReannounceSettings *InstanceReannounceSettingsPayload `json:"reannounceSettings,omitempty"`
}
// UpdateInstanceRequest represents a request to update an instance
type UpdateInstanceRequest struct {
Name string `json:"name"`
Host string `json:"host"`
Username string `json:"username"`
Password string `json:"password,omitempty"` // Optional for updates
BasicUsername *string `json:"basicUsername,omitempty"`
BasicPassword *string `json:"basicPassword,omitempty"`
TLSSkipVerify *bool `json:"tlsSkipVerify,omitempty"`
HasLocalFilesystemAccess *bool `json:"hasLocalFilesystemAccess,omitempty"`
UseHardlinks *bool `json:"useHardlinks,omitempty"`
HardlinkBaseDir *string `json:"hardlinkBaseDir,omitempty"`
HardlinkDirPreset *string `json:"hardlinkDirPreset,omitempty"`
LinkDirName *string `json:"linkDirName,omitempty"`
UseReflinks *bool `json:"useReflinks,omitempty"`
FallbackToRegularMode *bool `json:"fallbackToRegularMode,omitempty"`
ReannounceSettings *InstanceReannounceSettingsPayload `json:"reannounceSettings,omitempty"`
}
type UpdateInstanceStatusRequest struct {
IsActive bool `json:"isActive"`
}
// InstanceResponse represents an instance in API responses
type InstanceResponse struct {
ID int `json:"id"`
Name string `json:"name"`
Host string `json:"host"`
Username string `json:"username"`
BasicUsername *string `json:"basicUsername,omitempty"`
TLSSkipVerify bool `json:"tlsSkipVerify"`
HasLocalFilesystemAccess bool `json:"hasLocalFilesystemAccess"`
UseHardlinks bool `json:"useHardlinks"`
HardlinkBaseDir string `json:"hardlinkBaseDir"`
HardlinkDirPreset string `json:"hardlinkDirPreset"`
LinkDirName string `json:"linkDirName"`
UseReflinks bool `json:"useReflinks"`
FallbackToRegularMode bool `json:"fallbackToRegularMode"`
Connected bool `json:"connected"`
HasDecryptionError bool `json:"hasDecryptionError"`
RecentErrors []models.InstanceError `json:"recentErrors,omitempty"`
ConnectionStatus string `json:"connectionStatus,omitempty"`
SortOrder int `json:"sortOrder"`
IsActive bool `json:"isActive"`
ReannounceSettings InstanceReannounceSettingsPayload `json:"reannounceSettings"`
}
// InstanceReannounceSettingsPayload carries tracker monitoring config.
type InstanceReannounceSettingsPayload struct {
Enabled bool `json:"enabled"`
InitialWaitSeconds int `json:"initialWaitSeconds"`
ReannounceIntervalSeconds int `json:"reannounceIntervalSeconds"`
MaxAgeSeconds int `json:"maxAgeSeconds"`
MaxRetries int `json:"maxRetries"`
Aggressive bool `json:"aggressive"`
MonitorAll bool `json:"monitorAll"`
ExcludeCategories bool `json:"excludeCategories"`
Categories []string `json:"categories"`
ExcludeTags bool `json:"excludeTags"`
Tags []string `json:"tags"`
ExcludeTrackers bool `json:"excludeTrackers"`
Trackers []string `json:"trackers"`
}
// TestConnectionResponse represents connection test results
type TestConnectionResponse struct {
Connected bool `json:"connected"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}
func (p *InstanceReannounceSettingsPayload) toModel(instanceID int, base *models.InstanceReannounceSettings) *models.InstanceReannounceSettings {
var target *models.InstanceReannounceSettings
if base != nil {
clone := *base
target = &clone
} else {
target = models.DefaultInstanceReannounceSettings(instanceID)
}
if p == nil {
target.InstanceID = instanceID
return target
}
target.InstanceID = instanceID
target.Enabled = p.Enabled
target.InitialWaitSeconds = p.InitialWaitSeconds
target.ReannounceIntervalSeconds = p.ReannounceIntervalSeconds
target.MaxAgeSeconds = p.MaxAgeSeconds
target.MaxRetries = p.MaxRetries
target.Aggressive = p.Aggressive
target.MonitorAll = p.MonitorAll
target.ExcludeCategories = p.ExcludeCategories
target.Categories = append([]string{}, p.Categories...)
target.ExcludeTags = p.ExcludeTags
target.Tags = append([]string{}, p.Tags...)
target.ExcludeTrackers = p.ExcludeTrackers
target.Trackers = append([]string{}, p.Trackers...)
return target
}
func payloadFromModel(settings *models.InstanceReannounceSettings) InstanceReannounceSettingsPayload {
if settings == nil {
settings = models.DefaultInstanceReannounceSettings(0)
}
return InstanceReannounceSettingsPayload{
Enabled: settings.Enabled,
InitialWaitSeconds: settings.InitialWaitSeconds,
ReannounceIntervalSeconds: settings.ReannounceIntervalSeconds,
MaxAgeSeconds: settings.MaxAgeSeconds,
MaxRetries: settings.MaxRetries,
Aggressive: settings.Aggressive,
MonitorAll: settings.MonitorAll,
ExcludeCategories: settings.ExcludeCategories,
Categories: append([]string{}, settings.Categories...),
ExcludeTags: settings.ExcludeTags,
Tags: append([]string{}, settings.Tags...),
ExcludeTrackers: settings.ExcludeTrackers,
Trackers: append([]string{}, settings.Trackers...),
}
}
// DeleteInstanceResponse represents delete operation result
type DeleteInstanceResponse struct {
Message string `json:"message"`
}
// ListInstances returns all instances
func (h *InstancesHandler) ListInstances(w http.ResponseWriter, r *http.Request) {
instances, err := h.instanceStore.List(r.Context())
if err != nil {
log.Error().Err(err).Msg("Failed to list instances")
RespondError(w, http.StatusInternalServerError, "Failed to list instances")
return
}
response := h.buildInstanceResponsesParallel(r.Context(), instances)
RespondJSON(w, http.StatusOK, response)
}
// UpdateInstanceOrder updates the display order for all instances
func (h *InstancesHandler) UpdateInstanceOrder(w http.ResponseWriter, r *http.Request) {
var req struct {
InstanceIDs []int `json:"instanceIds"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
RespondError(w, http.StatusBadRequest, "Invalid request body")
return
}
if len(req.InstanceIDs) == 0 {
RespondError(w, http.StatusBadRequest, "instanceIds must not be empty")
return
}
instances, err := h.instanceStore.List(r.Context())
if err != nil {
log.Error().Err(err).Msg("Failed to list instances for reorder")
RespondError(w, http.StatusInternalServerError, "Failed to list instances")
return
}
if len(req.InstanceIDs) != len(instances) {
RespondError(w, http.StatusBadRequest, "instanceIds must include all instances")
return
}
validIDs := make(map[int]struct{}, len(instances))
for _, inst := range instances {
validIDs[inst.ID] = struct{}{}
}
seen := make(map[int]struct{}, len(req.InstanceIDs))
for _, id := range req.InstanceIDs {
if _, ok := validIDs[id]; !ok {
RespondError(w, http.StatusBadRequest, "instanceIds contains an unknown instance")
return
}
if _, ok := seen[id]; ok {
RespondError(w, http.StatusBadRequest, "instanceIds must not contain duplicates")
return
}
seen[id] = struct{}{}
}
if err := h.instanceStore.UpdateOrder(r.Context(), req.InstanceIDs); err != nil {
log.Error().Err(err).Msg("Failed to update instance order")
RespondError(w, http.StatusInternalServerError, "Failed to update instance order")
return
}
updatedInstances, err := h.instanceStore.List(r.Context())
if err != nil {
log.Error().Err(err).Msg("Failed to list instances after reorder")
RespondError(w, http.StatusInternalServerError, "Failed to list instances")
return
}
response := h.buildInstanceResponsesParallel(r.Context(), updatedInstances)
RespondJSON(w, http.StatusOK, response)
}
// CreateInstance creates a new instance
func (h *InstancesHandler) CreateInstance(w http.ResponseWriter, r *http.Request) {
var req CreateInstanceRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
RespondError(w, http.StatusBadRequest, "Invalid request body")
return
}
// Validate input
if req.Name == "" || req.Host == "" {
RespondError(w, http.StatusBadRequest, "Name and host are required")
return
}
req.LinkDirName = normalizeOptionalString(req.LinkDirName)
settingsPayload := req.ReannounceSettings.toModel(0, nil)
instance, settings, err := h.instanceStore.CreateWithReannounce(
r.Context(),
h.reannounceStore,
req.Name,
req.Host,
req.Username,
req.Password,
req.BasicUsername,
req.BasicPassword,
req.TLSSkipVerify,
req.HasLocalFilesystemAccess,
req.LinkDirName,
settingsPayload,
)
if err != nil {
if errors.Is(err, models.ErrInvalidLinkDirName) {
RespondError(w, http.StatusBadRequest, err.Error())
return
}
log.Error().Err(err).Msg("Failed to create instance")
RespondError(w, http.StatusInternalServerError, "Failed to create instance")
return
}
if h.reannounceCache != nil && settings != nil {
h.reannounceCache.Replace(settings)
}
// Return quickly without testing connection
response := h.buildQuickInstanceResponse(instance)
response.ReannounceSettings = payloadFromModel(settings)
// Test connection asynchronously
go h.testConnectionAsync(instance.ID)
RespondJSON(w, http.StatusCreated, response)
}
// UpdateInstance updates an existing instance
func (h *InstancesHandler) UpdateInstance(w http.ResponseWriter, r *http.Request) {
// Get instance ID from URL
instanceID, err := strconv.Atoi(chi.URLParam(r, "instanceID"))
if err != nil {
RespondError(w, http.StatusBadRequest, "Invalid instance ID")
return
}
var req UpdateInstanceRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
RespondError(w, http.StatusBadRequest, "Invalid request body")
return
}
// Validate input
if req.Name == "" || req.Host == "" {
RespondError(w, http.StatusBadRequest, "Name and host are required")
return
}
// Fetch existing instance to handle redacted values
existingInstance, err := h.instanceStore.Get(r.Context(), instanceID)
if err != nil {
if errors.Is(err, models.ErrInstanceNotFound) {
RespondError(w, http.StatusNotFound, "Instance not found")
return
}
log.Error().Err(err).Msg("Failed to fetch existing instance")
RespondError(w, http.StatusInternalServerError, "Failed to fetch instance")
return
}
// Handle redacted password - if redacted, use existing password
if req.Password != "" && domain.IsRedactedString(req.Password) {
req.Password = existingInstance.PasswordEncrypted
}
// Handle redacted basic password - if redacted, use existing basic password
if req.BasicPassword != nil && *req.BasicPassword != "" && domain.IsRedactedString(*req.BasicPassword) {
req.BasicPassword = existingInstance.BasicPasswordEncrypted
}
req.LinkDirName = normalizeOptionalString(req.LinkDirName)
// Validate hardlink/reflink settings
effectiveLocalAccess := existingInstance.HasLocalFilesystemAccess
if req.HasLocalFilesystemAccess != nil {
effectiveLocalAccess = *req.HasLocalFilesystemAccess
}
effectiveUseHardlinks := existingInstance.UseHardlinks
if req.UseHardlinks != nil {
effectiveUseHardlinks = *req.UseHardlinks
}
effectiveUseReflinks := existingInstance.UseReflinks
if req.UseReflinks != nil {
effectiveUseReflinks = *req.UseReflinks
}
effectiveHardlinkBaseDir := existingInstance.HardlinkBaseDir
if req.HardlinkBaseDir != nil {
effectiveHardlinkBaseDir = *req.HardlinkBaseDir
}
// Mutual exclusivity: cannot enable both hardlink and reflink modes
if effectiveUseHardlinks && effectiveUseReflinks {
RespondError(w, http.StatusBadRequest, "Cannot enable both hardlink and reflink modes - they are mutually exclusive")
return
}
if effectiveUseHardlinks {
if !effectiveLocalAccess {
RespondError(w, http.StatusBadRequest, "Cannot enable hardlink mode without local filesystem access")
return
}
if effectiveHardlinkBaseDir == "" {
RespondError(w, http.StatusBadRequest, "Cannot enable hardlink mode without a base directory")
return
}
}
if effectiveUseReflinks {
if !effectiveLocalAccess {
RespondError(w, http.StatusBadRequest, "Cannot enable reflink mode without local filesystem access")
return
}
if effectiveHardlinkBaseDir == "" {
RespondError(w, http.StatusBadRequest, "Cannot enable reflink mode without a base directory")
return
}
}
// Update instance
updateParams := &models.InstanceUpdateParams{
TLSSkipVerify: req.TLSSkipVerify,
HasLocalFilesystemAccess: req.HasLocalFilesystemAccess,
UseHardlinks: req.UseHardlinks,
HardlinkBaseDir: req.HardlinkBaseDir,
HardlinkDirPreset: req.HardlinkDirPreset,
LinkDirName: req.LinkDirName,
UseReflinks: req.UseReflinks,
FallbackToRegularMode: req.FallbackToRegularMode,
}
var desiredSettings *models.InstanceReannounceSettings
if req.ReannounceSettings != nil {
desiredSettings = req.ReannounceSettings.toModel(instanceID, nil)
}
instance, settings, err := h.instanceStore.UpdateWithReannounce(
r.Context(),
instanceID,
req.Name,
req.Host,
req.Username,
req.Password,
req.BasicUsername,
req.BasicPassword,
updateParams,
h.reannounceStore,
desiredSettings,
)
if err != nil {
if errors.Is(err, models.ErrInstanceNotFound) {
RespondError(w, http.StatusNotFound, "Instance not found")
return
}
if errors.Is(err, models.ErrInvalidLinkDirName) {
RespondError(w, http.StatusBadRequest, err.Error())
return
}
log.Error().Err(err).Msg("Failed to update instance")
RespondError(w, http.StatusInternalServerError, "Failed to update instance")
return
}
// Remove old client from pool to force reconnection
h.clientPool.RemoveClient(instanceID)
if req.ReannounceSettings == nil {
settings = h.loadReannounceSettings(r.Context(), instanceID)
} else if h.reannounceCache != nil && settings != nil {
h.reannounceCache.Replace(settings)
}
// Return quickly without testing connection
response := h.buildQuickInstanceResponse(instance)
response.ReannounceSettings = payloadFromModel(settings)
// Test connection asynchronously
go h.testConnectionAsync(instance.ID)
RespondJSON(w, http.StatusOK, response)
}
func normalizeOptionalString(value *string) *string {
if value == nil {
return nil
}
trimmed := strings.TrimSpace(*value)
return &trimmed
}
// DeleteInstance deletes an instance
func (h *InstancesHandler) DeleteInstance(w http.ResponseWriter, r *http.Request) {
// Get instance ID from URL
instanceID, err := strconv.Atoi(chi.URLParam(r, "instanceID"))
if err != nil {
RespondError(w, http.StatusBadRequest, "Invalid instance ID")
return
}
// Delete instance
if err := h.instanceStore.Delete(r.Context(), instanceID); err != nil {
if errors.Is(err, models.ErrInstanceNotFound) {
RespondError(w, http.StatusNotFound, "Instance not found")
return
}
log.Error().Err(err).Msg("Failed to delete instance")
RespondError(w, http.StatusInternalServerError, "Failed to delete instance")
return
}
// Remove client from pool
h.clientPool.RemoveClient(instanceID)
response := DeleteInstanceResponse{
Message: "Instance deleted successfully",
}
RespondJSON(w, http.StatusOK, response)
}
// UpdateInstanceStatus toggles whether an instance should be actively polled
func (h *InstancesHandler) UpdateInstanceStatus(w http.ResponseWriter, r *http.Request) {
instanceID, err := strconv.Atoi(chi.URLParam(r, "instanceID"))
if err != nil {
RespondError(w, http.StatusBadRequest, "Invalid instance ID")
return
}
var req UpdateInstanceStatusRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
RespondError(w, http.StatusBadRequest, "Invalid request body")
return
}
instance, err := h.instanceStore.SetActiveState(r.Context(), instanceID, req.IsActive)
if err != nil {
if errors.Is(err, models.ErrInstanceNotFound) {
RespondError(w, http.StatusNotFound, "Instance not found")
return
}
log.Error().Err(err).Int("instanceID", instanceID).Msg("Failed to update instance status")
RespondError(w, http.StatusInternalServerError, "Failed to update instance status")
return
}
if !req.IsActive {
h.clientPool.RemoveClient(instanceID)
} else {
// Clear backoff state and errors when re-enabling instance
h.clientPool.ResetFailureTracking(instanceID)
go h.testConnectionAsync(instanceID)
}
response := h.buildQuickInstanceResponse(instance)
response.ReannounceSettings = h.getReannounceSettingsPayload(r.Context(), instanceID)
RespondJSON(w, http.StatusOK, response)
}
// TestConnection tests the connection to an instance
func (h *InstancesHandler) TestConnection(w http.ResponseWriter, r *http.Request) {
// Get instance ID from URL
instanceID, err := strconv.Atoi(chi.URLParam(r, "instanceID"))
if err != nil {
RespondError(w, http.StatusBadRequest, "Invalid instance ID")
return
}
// Try to get client (this will create connection if needed)
client, err := h.clientPool.GetClient(r.Context(), instanceID)
if err != nil {
if errors.Is(err, internalqbittorrent.ErrInstanceDisabled) {
response := TestConnectionResponse{
Connected: false,
Message: "Instance is disabled",
Error: err.Error(),
}
RespondJSON(w, http.StatusOK, response)
return
}
response := TestConnectionResponse{
Connected: false,
Error: err.Error(),
}
RespondJSON(w, http.StatusOK, response)
return
}
// Perform health check
if err := client.HealthCheck(r.Context()); err != nil {
response := TestConnectionResponse{
Connected: false,
Error: err.Error(),
}
RespondJSON(w, http.StatusOK, response)
return
}
response := TestConnectionResponse{
Connected: true,
Message: "Connection successful",
}
RespondJSON(w, http.StatusOK, response)
}