Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f9861e3b54 |
@@ -5,7 +5,7 @@ tmp_dir = "tmp"
|
||||
[build]
|
||||
args_bin = ["--config", "data/"]
|
||||
bin = "./tmp/main"
|
||||
cmd = "bash -c 'go build -ldflags \"-X github.com/sirrobot01/decypharr/pkg/version.Version=0.0.0 -X github.com/sirrobot01/decypharr/pkg/version.Channel=dev\" -o ./tmp/main .'"
|
||||
cmd = "bash -c 'npm run build && go build -ldflags \"-X github.com/sirrobot01/decypharr/pkg/version.Version=0.0.0 -X github.com/sirrobot01/decypharr/pkg/version.Channel=dev\" -o ./tmp/main .'"
|
||||
delay = 1000
|
||||
exclude_dir = ["tmp", "vendor", "testdata", "data", "logs", "docs", "dist", "node_modules", ".ven"]
|
||||
exclude_file = []
|
||||
@@ -16,7 +16,7 @@ tmp_dir = "tmp"
|
||||
include_dir = []
|
||||
include_ext = ["go", "tpl", "tmpl", "html", ".json", ".js", ".css"]
|
||||
include_file = []
|
||||
kill_delay = "1s"
|
||||
kill_delay = "0s"
|
||||
log = "build-errors.log"
|
||||
poll = false
|
||||
poll_interval = 0
|
||||
@@ -24,8 +24,8 @@ tmp_dir = "tmp"
|
||||
pre_cmd = []
|
||||
rerun = false
|
||||
rerun_delay = 500
|
||||
send_interrupt = true
|
||||
stop_on_error = true
|
||||
send_interrupt = false
|
||||
stop_on_error = false
|
||||
|
||||
[color]
|
||||
app = ""
|
||||
|
||||
@@ -26,8 +26,4 @@ node_modules/
|
||||
decypharr
|
||||
healthcheck
|
||||
*.exe
|
||||
.venv/
|
||||
data/**
|
||||
|
||||
.stignore
|
||||
.stfolder/**
|
||||
.venv/
|
||||
2
.github/workflows/deploy-docs.yml
vendored
@@ -24,5 +24,5 @@ jobs:
|
||||
path: .cache
|
||||
restore-keys: |
|
||||
mkdocs-material-
|
||||
- run: cd docs && pip install -r requirements.txt
|
||||
- run: pip install mkdocs-material
|
||||
- run: cd docs && mkdocs gh-deploy --force
|
||||
4
.gitignore
vendored
@@ -14,7 +14,5 @@ logs/**
|
||||
auth.json
|
||||
.ven/
|
||||
.env
|
||||
node_modules/
|
||||
.venv/
|
||||
.stignore
|
||||
.stfolder/**
|
||||
node_modules/
|
||||
51
Dockerfile
@@ -29,49 +29,40 @@ RUN --mount=type=cache,target=/go/pkg/mod \
|
||||
go build -trimpath -ldflags="-w -s" \
|
||||
-o /healthcheck cmd/healthcheck/main.go
|
||||
|
||||
# Stage 2: Final image
|
||||
FROM alpine:latest
|
||||
# Stage 2: Create directory structure
|
||||
FROM alpine:3.19 as dirsetup
|
||||
RUN mkdir -p /app/logs && \
|
||||
mkdir -p /app/cache && \
|
||||
chmod 777 /app/logs && \
|
||||
touch /app/logs/decypharr.log && \
|
||||
chmod 666 /app/logs/decypharr.log
|
||||
|
||||
ARG VERSION=0.0.0
|
||||
ARG CHANNEL=dev
|
||||
# Stage 3: Final image
|
||||
FROM gcr.io/distroless/static-debian12:nonroot
|
||||
|
||||
LABEL version = "${VERSION}-${CHANNEL}"
|
||||
|
||||
LABEL org.opencontainers.image.source = "https://github.com/sirrobot01/decypharr"
|
||||
LABEL org.opencontainers.image.title = "decypharr"
|
||||
LABEL org.opencontainers.image.authors = "sirrobot01"
|
||||
LABEL org.opencontainers.image.documentation = "https://github.com/sirrobot01/decypharr/blob/main/README.md"
|
||||
|
||||
# Install dependencies including rclone
|
||||
RUN apk add --no-cache fuse3 ca-certificates su-exec shadow curl unzip && \
|
||||
echo "user_allow_other" >> /etc/fuse.conf && \
|
||||
case "$(uname -m)" in \
|
||||
x86_64) ARCH=amd64 ;; \
|
||||
aarch64) ARCH=arm64 ;; \
|
||||
armv7l) ARCH=arm ;; \
|
||||
*) echo "Unsupported architecture: $(uname -m)" && exit 1 ;; \
|
||||
esac && \
|
||||
curl -O "https://downloads.rclone.org/rclone-current-linux-${ARCH}.zip" && \
|
||||
unzip "rclone-current-linux-${ARCH}.zip" && \
|
||||
cp rclone-*/rclone /usr/local/bin/ && \
|
||||
chmod +x /usr/local/bin/rclone && \
|
||||
rm -rf rclone-* && \
|
||||
apk del curl unzip
|
||||
# Copy binaries
|
||||
COPY --from=builder --chown=nonroot:nonroot /decypharr /usr/bin/decypharr
|
||||
COPY --from=builder --chown=nonroot:nonroot /healthcheck /usr/bin/healthcheck
|
||||
|
||||
# Copy binaries and entrypoint
|
||||
COPY --from=builder /decypharr /usr/bin/decypharr
|
||||
COPY --from=builder /healthcheck /usr/bin/healthcheck
|
||||
COPY scripts/entrypoint.sh /entrypoint.sh
|
||||
RUN chmod +x /entrypoint.sh
|
||||
# Copy pre-made directory structure
|
||||
COPY --from=dirsetup --chown=nonroot:nonroot /app /app
|
||||
|
||||
# Set environment variables
|
||||
ENV PUID=1000
|
||||
ENV PGID=1000
|
||||
|
||||
# Metadata
|
||||
ENV LOG_PATH=/app/logs
|
||||
|
||||
EXPOSE 8282
|
||||
VOLUME ["/app"]
|
||||
USER nonroot:nonroot
|
||||
|
||||
HEALTHCHECK --interval=10s --retries=10 CMD ["/usr/bin/healthcheck", "--config", "/app", "--basic"]
|
||||
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
# Base healthcheck
|
||||
HEALTHCHECK --interval=3s --retries=10 CMD ["/usr/bin/healthcheck", "--config", "/app", "--basic"]
|
||||
|
||||
CMD ["/usr/bin/decypharr", "--config", "/app"]
|
||||
35
README.md
@@ -12,9 +12,9 @@ Decypharr combines the power of QBittorrent with popular Debrid services to enha
|
||||
|
||||
- Mock Qbittorent API that supports the Arrs (Sonarr, Radarr, Lidarr etc)
|
||||
- Full-fledged UI for managing torrents
|
||||
- Proxy support for filtering out un-cached Debrid torrents
|
||||
- Multiple Debrid providers support
|
||||
- WebDAV server support for each debrid provider
|
||||
- Optional mounting of WebDAV to your system(using [Rclone](https://rclone.org/))
|
||||
- Repair Worker for missing files
|
||||
|
||||
## Supported Debrid Providers
|
||||
@@ -29,22 +29,17 @@ Decypharr combines the power of QBittorrent with popular Debrid services to enha
|
||||
### Docker (Recommended)
|
||||
|
||||
```yaml
|
||||
version: '3.7'
|
||||
services:
|
||||
decypharr:
|
||||
image: cy01/blackhole:latest
|
||||
image: cy01/blackhole:latest # or cy01/blackhole:beta
|
||||
container_name: decypharr
|
||||
ports:
|
||||
- "8282:8282"
|
||||
- "8282:8282" # qBittorrent
|
||||
volumes:
|
||||
- /mnt/:/mnt:rshared
|
||||
- /mnt/:/mnt
|
||||
- ./configs/:/app # config.json must be in this directory
|
||||
restart: unless-stopped
|
||||
devices:
|
||||
- /dev/fuse:/dev/fuse:rwm
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor:unconfined
|
||||
```
|
||||
|
||||
## Documentation
|
||||
@@ -62,7 +57,25 @@ The documentation includes:
|
||||
|
||||
## Basic Configuration
|
||||
|
||||
You can configure Decypharr through the Web UI or by editing the `config.json` file directly.
|
||||
```json
|
||||
{
|
||||
"debrids": [
|
||||
{
|
||||
"name": "realdebrid",
|
||||
"api_key": "your_api_key_here",
|
||||
"folder": "/mnt/remote/realdebrid/__all__/",
|
||||
"use_webdav": true
|
||||
}
|
||||
],
|
||||
"qbittorrent": {
|
||||
"download_folder": "/mnt/symlinks/",
|
||||
"categories": ["sonarr", "radarr"]
|
||||
},
|
||||
"use_auth": false,
|
||||
"log_level": "info",
|
||||
"port": "8282"
|
||||
}
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
|
||||
@@ -6,8 +6,10 @@ import (
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/internal/logger"
|
||||
"github.com/sirrobot01/decypharr/pkg/qbit"
|
||||
"github.com/sirrobot01/decypharr/pkg/sabnzbd"
|
||||
"github.com/sirrobot01/decypharr/pkg/server"
|
||||
"github.com/sirrobot01/decypharr/pkg/store"
|
||||
"github.com/sirrobot01/decypharr/pkg/usenet"
|
||||
"github.com/sirrobot01/decypharr/pkg/version"
|
||||
"github.com/sirrobot01/decypharr/pkg/web"
|
||||
"github.com/sirrobot01/decypharr/pkg/webdav"
|
||||
@@ -40,7 +42,6 @@ func Start(ctx context.Context) error {
|
||||
svcCtx, cancelSvc := context.WithCancel(ctx)
|
||||
defer cancelSvc()
|
||||
|
||||
// Create the logger path if it doesn't exist
|
||||
for {
|
||||
cfg := config.Get()
|
||||
_log := logger.Default()
|
||||
@@ -59,28 +60,30 @@ func Start(ctx context.Context) error {
|
||||
`, version.GetInfo(), cfg.LogLevel)
|
||||
|
||||
// Initialize services
|
||||
qb := qbit.New()
|
||||
wd := webdav.New()
|
||||
_usenet := usenet.New()
|
||||
debridCaches := store.Get().Debrid().Caches()
|
||||
wd := webdav.New(debridCaches, _usenet)
|
||||
var sb *sabnzbd.SABnzbd
|
||||
|
||||
ui := web.New().Routes()
|
||||
ui := web.New(_usenet).Routes()
|
||||
webdavRoutes := wd.Routes()
|
||||
qbitRoutes := qb.Routes()
|
||||
|
||||
qb := qbit.New()
|
||||
|
||||
// Register routes
|
||||
handlers := map[string]http.Handler{
|
||||
"/": ui,
|
||||
"/api/v2": qbitRoutes,
|
||||
"/webdav": webdavRoutes,
|
||||
}
|
||||
srv := server.New(handlers)
|
||||
|
||||
reset := func() {
|
||||
// Reset the store and services
|
||||
qb.Reset()
|
||||
store.Reset()
|
||||
// refresh GC
|
||||
runtime.GC()
|
||||
if qb != nil {
|
||||
handlers["/api/v2"] = qb.Routes()
|
||||
}
|
||||
if _usenet != nil {
|
||||
sb = sabnzbd.New(_usenet)
|
||||
sabRoutes := sb.Routes()
|
||||
handlers["/sabnzbd"] = sabRoutes
|
||||
}
|
||||
srv := server.New(_usenet, handlers)
|
||||
|
||||
done := make(chan struct{})
|
||||
go func(ctx context.Context) {
|
||||
@@ -96,18 +99,27 @@ func Start(ctx context.Context) error {
|
||||
// graceful shutdown
|
||||
cancelSvc() // propagate to services
|
||||
<-done // wait for them to finish
|
||||
_log.Info().Msg("Decypharr has been stopped gracefully.")
|
||||
reset() // reset store and services
|
||||
return nil
|
||||
|
||||
case <-restartCh:
|
||||
cancelSvc() // tell existing services to shut down
|
||||
_log.Info().Msg("Restarting Decypharr...")
|
||||
<-done // wait for them to finish
|
||||
_log.Info().Msg("Decypharr has been restarted.")
|
||||
reset() // reset store and services
|
||||
if qb != nil {
|
||||
qb.Reset()
|
||||
}
|
||||
store.Reset()
|
||||
if _usenet != nil {
|
||||
_usenet.Close()
|
||||
}
|
||||
|
||||
// rebuild svcCtx off the original parent
|
||||
svcCtx, cancelSvc = context.WithCancel(ctx)
|
||||
runtime.GC()
|
||||
|
||||
config.Reload()
|
||||
store.Reset()
|
||||
// loop will restart services automatically
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -149,13 +161,12 @@ func startServices(ctx context.Context, cancelSvc context.CancelFunc, wd *webdav
|
||||
return srv.Start(ctx)
|
||||
})
|
||||
|
||||
// Start rclone RC server if enabled
|
||||
safeGo(func() error {
|
||||
rcManager := store.Get().RcloneManager()
|
||||
if rcManager == nil {
|
||||
arr := store.Get().Arr()
|
||||
if arr == nil {
|
||||
return nil
|
||||
}
|
||||
return rcManager.Start(ctx)
|
||||
return arr.StartSchedule(ctx)
|
||||
})
|
||||
|
||||
if cfg := config.Get(); cfg.Repair.Enabled {
|
||||
@@ -171,8 +182,7 @@ func startServices(ctx context.Context, cancelSvc context.CancelFunc, wd *webdav
|
||||
}
|
||||
|
||||
safeGo(func() error {
|
||||
store.Get().StartWorkers(ctx)
|
||||
return nil
|
||||
return store.Get().StartQueueSchedule(ctx)
|
||||
})
|
||||
|
||||
go func() {
|
||||
|
||||
@@ -1,418 +0,0 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: Decypharr API
|
||||
description: QbitTorrent with Debrid Support API
|
||||
version: 1.0.0
|
||||
contact:
|
||||
name: Decypharr
|
||||
url: https://github.com/sirrobot01/decypharr
|
||||
|
||||
servers:
|
||||
- url: /api
|
||||
description: API endpoints
|
||||
|
||||
security:
|
||||
- cookieAuth: []
|
||||
- bearerAuth: []
|
||||
|
||||
paths:
|
||||
/arrs:
|
||||
get:
|
||||
summary: Get all configured Arrs
|
||||
description: Retrieve a list of all configured Arr applications (Sonarr, Radarr, etc.)
|
||||
tags:
|
||||
- Arrs
|
||||
responses:
|
||||
'200':
|
||||
description: Successfully retrieved Arrs
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Arr'
|
||||
|
||||
/add:
|
||||
post:
|
||||
summary: Add content for processing
|
||||
description: Add torrent files or magnet links for processing through debrid services
|
||||
tags:
|
||||
- Content
|
||||
requestBody:
|
||||
content:
|
||||
multipart/form-data:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
arr:
|
||||
type: string
|
||||
description: Name of the Arr application
|
||||
action:
|
||||
type: string
|
||||
description: Action to perform
|
||||
debrid:
|
||||
type: string
|
||||
description: Debrid service to use
|
||||
callbackUrl:
|
||||
type: string
|
||||
description: Optional callback URL
|
||||
downloadFolder:
|
||||
type: string
|
||||
description: Download folder path
|
||||
downloadUncached:
|
||||
type: boolean
|
||||
description: Whether to download uncached content
|
||||
urls:
|
||||
type: string
|
||||
description: Newline-separated URLs or magnet links
|
||||
files:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
format: binary
|
||||
description: Torrent files to upload
|
||||
responses:
|
||||
'200':
|
||||
description: Content added successfully
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
results:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/ImportRequest'
|
||||
errors:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
'400':
|
||||
description: Bad request
|
||||
|
||||
/repair:
|
||||
post:
|
||||
summary: Repair media
|
||||
description: Start a repair process for specified media items
|
||||
tags:
|
||||
- Repair
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RepairRequest'
|
||||
responses:
|
||||
'200':
|
||||
description: Repair started or completed
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
'400':
|
||||
description: Bad request
|
||||
'404':
|
||||
description: Arr not found
|
||||
'500':
|
||||
description: Internal server error
|
||||
|
||||
/repair/jobs:
|
||||
get:
|
||||
summary: Get repair jobs
|
||||
description: Retrieve all repair jobs
|
||||
tags:
|
||||
- Repair
|
||||
responses:
|
||||
'200':
|
||||
description: Successfully retrieved repair jobs
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/RepairJob'
|
||||
delete:
|
||||
summary: Delete repair jobs
|
||||
description: Delete multiple repair jobs by IDs
|
||||
tags:
|
||||
- Repair
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
ids:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
required:
|
||||
- ids
|
||||
responses:
|
||||
'200':
|
||||
description: Jobs deleted successfully
|
||||
'400':
|
||||
description: Bad request
|
||||
|
||||
/repair/jobs/{id}/process:
|
||||
post:
|
||||
summary: Process repair job
|
||||
description: Process a specific repair job by ID
|
||||
tags:
|
||||
- Repair
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
description: Job ID
|
||||
responses:
|
||||
'200':
|
||||
description: Job processed successfully
|
||||
'400':
|
||||
description: Bad request
|
||||
|
||||
/repair/jobs/{id}/stop:
|
||||
post:
|
||||
summary: Stop repair job
|
||||
description: Stop a running repair job by ID
|
||||
tags:
|
||||
- Repair
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
description: Job ID
|
||||
responses:
|
||||
'200':
|
||||
description: Job stopped successfully
|
||||
'400':
|
||||
description: Bad request
|
||||
'500':
|
||||
description: Internal server error
|
||||
|
||||
/torrents:
|
||||
get:
|
||||
summary: Get all torrents
|
||||
description: Retrieve all torrents sorted by added date
|
||||
tags:
|
||||
- Torrents
|
||||
responses:
|
||||
'200':
|
||||
description: Successfully retrieved torrents
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Torrent'
|
||||
delete:
|
||||
summary: Delete multiple torrents
|
||||
description: Delete multiple torrents by hash list
|
||||
tags:
|
||||
- Torrents
|
||||
parameters:
|
||||
- name: hashes
|
||||
in: query
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
description: Comma-separated list of torrent hashes
|
||||
- name: removeFromDebrid
|
||||
in: query
|
||||
schema:
|
||||
type: boolean
|
||||
default: false
|
||||
description: Whether to remove from debrid service
|
||||
responses:
|
||||
'200':
|
||||
description: Torrents deleted successfully
|
||||
'400':
|
||||
description: Bad request
|
||||
|
||||
/torrents/{category}/{hash}:
|
||||
delete:
|
||||
summary: Delete single torrent
|
||||
description: Delete a specific torrent by category and hash
|
||||
tags:
|
||||
- Torrents
|
||||
parameters:
|
||||
- name: category
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
description: Torrent category
|
||||
- name: hash
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
description: Torrent hash
|
||||
- name: removeFromDebrid
|
||||
in: query
|
||||
schema:
|
||||
type: boolean
|
||||
default: false
|
||||
description: Whether to remove from debrid service
|
||||
responses:
|
||||
'200':
|
||||
description: Torrent deleted successfully
|
||||
'400':
|
||||
description: Bad request
|
||||
|
||||
components:
|
||||
securitySchemes:
|
||||
cookieAuth:
|
||||
type: apiKey
|
||||
in: cookie
|
||||
name: auth-session
|
||||
bearerAuth:
|
||||
type: http
|
||||
scheme: bearer
|
||||
bearerFormat: token
|
||||
description: API token for authentication
|
||||
|
||||
schemas:
|
||||
Arr:
|
||||
type: object
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
description: Name of the Arr application
|
||||
host:
|
||||
type: string
|
||||
description: Host URL of the Arr application
|
||||
token:
|
||||
type: string
|
||||
description: API token for the Arr application
|
||||
cleanup:
|
||||
type: boolean
|
||||
description: Whether to cleanup after processing
|
||||
skipRepair:
|
||||
type: boolean
|
||||
description: Whether to skip repair operations
|
||||
downloadUncached:
|
||||
type: boolean
|
||||
description: Whether to download uncached content
|
||||
selectedDebrid:
|
||||
type: string
|
||||
description: Selected debrid service
|
||||
source:
|
||||
type: string
|
||||
description: Source of the Arr configuration
|
||||
|
||||
ImportRequest:
|
||||
type: object
|
||||
properties:
|
||||
debridName:
|
||||
type: string
|
||||
description: Name of the debrid service
|
||||
downloadFolder:
|
||||
type: string
|
||||
description: Download folder path
|
||||
magnet:
|
||||
type: string
|
||||
description: Magnet link
|
||||
arr:
|
||||
$ref: '#/components/schemas/Arr'
|
||||
action:
|
||||
type: string
|
||||
description: Action to perform
|
||||
downloadUncached:
|
||||
type: boolean
|
||||
description: Whether to download uncached content
|
||||
callbackUrl:
|
||||
type: string
|
||||
description: Callback URL
|
||||
importType:
|
||||
type: string
|
||||
description: Type of import (API, etc.)
|
||||
|
||||
RepairRequest:
|
||||
type: object
|
||||
properties:
|
||||
arrName:
|
||||
type: string
|
||||
description: Name of the Arr application
|
||||
mediaIds:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
description: List of media IDs to repair
|
||||
autoProcess:
|
||||
type: boolean
|
||||
description: Whether to auto-process the repair
|
||||
async:
|
||||
type: boolean
|
||||
description: Whether to run repair asynchronously
|
||||
required:
|
||||
- arrName
|
||||
|
||||
RepairJob:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
description: Job ID
|
||||
status:
|
||||
type: string
|
||||
description: Job status
|
||||
arrName:
|
||||
type: string
|
||||
description: Associated Arr application
|
||||
mediaIds:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
description: Media IDs being repaired
|
||||
createdAt:
|
||||
type: string
|
||||
format: date-time
|
||||
description: Job creation timestamp
|
||||
|
||||
Torrent:
|
||||
type: object
|
||||
properties:
|
||||
hash:
|
||||
type: string
|
||||
description: Torrent hash
|
||||
name:
|
||||
type: string
|
||||
description: Torrent name
|
||||
category:
|
||||
type: string
|
||||
description: Torrent category
|
||||
addedOn:
|
||||
type: string
|
||||
format: date-time
|
||||
description: Date when torrent was added
|
||||
size:
|
||||
type: integer
|
||||
description: Torrent size in bytes
|
||||
progress:
|
||||
type: number
|
||||
format: float
|
||||
description: Download progress (0-1)
|
||||
status:
|
||||
type: string
|
||||
description: Torrent status
|
||||
|
||||
|
||||
tags:
|
||||
- name: Arrs
|
||||
description: Arr application management
|
||||
- name: Content
|
||||
description: Content addition and processing
|
||||
- name: Repair
|
||||
description: Media repair operations
|
||||
- name: Torrents
|
||||
description: Torrent management
|
||||
- name: Configuration
|
||||
description: Application configuration
|
||||
- name: Authentication
|
||||
description: API token management
|
||||
@@ -1,90 +0,0 @@
|
||||
# API Documentation
|
||||
|
||||
Decypharr provides a RESTful API for managing torrents, debrid services, and Arr integrations. The API requires authentication and all endpoints are prefixed with `/api`.
|
||||
|
||||
## Authentication
|
||||
|
||||
The API supports two authentication methods:
|
||||
|
||||
### 1. Session-based Authentication (Cookies)
|
||||
Log in through the web interface (`/login`) to establish an authenticated session. The session cookie (`auth-session`) will be automatically included in subsequent API requests from the same browser session.
|
||||
|
||||
### 2. API Token Authentication (Bearer Token)
|
||||
Use API tokens for programmatic access. Include the token in the `Authorization` header for each request:
|
||||
|
||||
- `Authorization: Bearer <your-token>`
|
||||
|
||||
## Interactive API Documentation
|
||||
|
||||
<swagger-ui src="api-spec.yaml"/>
|
||||
|
||||
## API Endpoints Overview
|
||||
|
||||
### Arrs Management
|
||||
- `GET /api/arrs` - Get all configured Arr applications (Sonarr, Radarr, etc.)
|
||||
|
||||
### Content Management
|
||||
- `POST /api/add` - Add torrent files or magnet links for processing through debrid services
|
||||
|
||||
### Repair Operations
|
||||
- `POST /api/repair` - Start repair process for media items
|
||||
- `GET /api/repair/jobs` - Get all repair jobs
|
||||
- `POST /api/repair/jobs/{id}/process` - Process a specific repair job
|
||||
- `POST /api/repair/jobs/{id}/stop` - Stop a running repair job
|
||||
- `DELETE /api/repair/jobs` - Delete multiple repair jobs
|
||||
|
||||
### Torrent Management
|
||||
- `GET /api/torrents` - Get all torrents
|
||||
- `DELETE /api/torrents/{category}/{hash}` - Delete a specific torrent
|
||||
- `DELETE /api/torrents/` - Delete multiple torrents
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Adding Content via API
|
||||
|
||||
#### Using API Token:
|
||||
```bash
|
||||
curl -H "Authorization: Bearer $API_TOKEN" -X POST http://localhost:8080/api/add \
|
||||
-F "arr=sonarr" \
|
||||
-F "debrid=realdebrid" \
|
||||
-F "urls=magnet:?xt=urn:btih:..." \
|
||||
-F "downloadUncached=true"
|
||||
-F "file=@/path/to/torrent/file.torrent"
|
||||
-F "callbackUrl=http://your.callback.url/endpoint"
|
||||
```
|
||||
|
||||
#### Using Session Cookies:
|
||||
```bash
|
||||
# Login first (this sets the session cookie)
|
||||
curl -c cookies.txt -X POST http://localhost:8080/login \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"username": "your_username", "password": "your_password"}'
|
||||
|
||||
# Then use the session cookie for API calls
|
||||
curl -b cookies.txt -X POST http://localhost:8080/api/add \
|
||||
-F "arr=sonarr" \
|
||||
-F "debrid=realdebrid" \
|
||||
-F "urls=magnet:?xt=urn:btih:..." \
|
||||
-F "downloadUncached=true"
|
||||
```
|
||||
|
||||
### Getting Torrents
|
||||
|
||||
```bash
|
||||
# With API token
|
||||
curl -H "Authorization: Bearer $API_TOKEN" -X GET http://localhost:8080/api/torrents
|
||||
```
|
||||
|
||||
### Starting a Repair Job
|
||||
|
||||
```bash
|
||||
# With API token
|
||||
curl -H "Authorization: Bearer $API_TOKEN" -X POST http://localhost:8080/api/repair \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"arrName": "sonarr",
|
||||
"mediaIds": ["123", "456"],
|
||||
"autoProcess": true,
|
||||
"async": true
|
||||
}'
|
||||
```
|
||||
186
docs/docs/changelog.md
Normal file
@@ -0,0 +1,186 @@
|
||||
# Changelog
|
||||
|
||||
## 1.0.0
|
||||
|
||||
- Add WebDAV support for debrid providers
|
||||
- Some refactoring and code cleanup
|
||||
- Fixes
|
||||
- Fix Alldebrid not downloading torrents
|
||||
- Fix Alldebrid not downloading uncached torrents
|
||||
- Fix uncached torrents not being downloaded for RealDebrid
|
||||
- Add support for multiple download API keys for debrid providers
|
||||
- Add support for editable config.json via the UI
|
||||
- Fix downloading timeout
|
||||
- Fix UMASK for Windows
|
||||
- Retries 50x(except 503) errors for RD
|
||||
|
||||
|
||||
## 0.5.0
|
||||
|
||||
- A more refined repair worker (with more control)
|
||||
- UI Improvements
|
||||
- Pagination for torrents
|
||||
- Dark mode
|
||||
- Ordered torrents table
|
||||
- Fix Arr API flaky behavior
|
||||
- Discord Notifications
|
||||
- Minor bug fixes
|
||||
- Add Tautulli support
|
||||
- playback_failed event triggers a repair
|
||||
- Miscellaneous improvements
|
||||
- Add an option to skip the repair worker for a specific arr
|
||||
- Arr specific uncached downloading option
|
||||
- Option to download uncached torrents from UI
|
||||
- Remove QbitTorrent Log level (Use the global log level)
|
||||
|
||||
## 0.4.2
|
||||
|
||||
- Hotfixes
|
||||
- Fix saving torrents error
|
||||
- Fix bugs with the UI
|
||||
- Speed improvements
|
||||
|
||||
## 0.4.1
|
||||
|
||||
- Adds optional UI authentication
|
||||
- Downloaded Torrents persist on restart
|
||||
- Fixes
|
||||
- Fix Alldebrid struggling to find the correct file
|
||||
- Minor bug fixes or speed-gains
|
||||
- A new cleanup worker to clean up ARR queues
|
||||
|
||||
## 0.4.0
|
||||
|
||||
- Add support for multiple debrid providers
|
||||
- A full-fledged UI for adding torrents, repairing files, viewing config and managing torrents
|
||||
- Fix issues with Alldebrid
|
||||
- Fix file transversal bug
|
||||
- Fix files with no parent directory
|
||||
- Logging
|
||||
- Add a more robust logging system
|
||||
- Add logging to a file
|
||||
- Add logging to the UI
|
||||
- Qbittorrent
|
||||
- Add support for tags (creating, deleting, listing)
|
||||
- Add support for categories (creating, deleting, listing)
|
||||
- Fix issues with arr sending torrents using a different content type
|
||||
|
||||
## 0.3.3
|
||||
|
||||
- Add AllDebrid Support
|
||||
- Fix Torbox not downloading uncached torrents
|
||||
- Fix Rar files being downloaded
|
||||
|
||||
## 0.3.2
|
||||
|
||||
- Fix DebridLink not downloading
|
||||
- Fix Torbox with uncached torrents
|
||||
- Add new /internal/cached endpoint to check if an hash is cached
|
||||
- Implement per-debrid local cache
|
||||
- Fix file check for torbox
|
||||
- Other minor bug fixes
|
||||
|
||||
## 0.3.1
|
||||
|
||||
- Add DebridLink Support
|
||||
- Refactor error handling
|
||||
|
||||
## 0.3.0
|
||||
|
||||
- Add UI for adding torrents
|
||||
- Refraction of the code
|
||||
- Fix Torbox bug
|
||||
- Update CI/CD
|
||||
- Update Readme
|
||||
|
||||
## 0.2.7
|
||||
|
||||
- Add support for multiple debrid providers
|
||||
- Add Torbox support
|
||||
- Add support for configurable debrid cache checks
|
||||
- Add support for configurable debrid download uncached torrents
|
||||
|
||||
## 0.2.6
|
||||
|
||||
- Delete torrent for empty matched files
|
||||
- Update Readme
|
||||
|
||||
## 0.2.5
|
||||
|
||||
- Fix ContentPath not being set prior
|
||||
- Rewrote Readme
|
||||
- Cleaned up the code
|
||||
|
||||
## 0.2.4
|
||||
|
||||
- Add file download support (Sequential Download)
|
||||
- Fix http handler error
|
||||
- Fix *arrs map failing concurrently
|
||||
- Fix cache not being updated
|
||||
|
||||
## 0.2.3
|
||||
|
||||
- Delete uncached items from RD
|
||||
- Fail if the torrent is not cached (optional)
|
||||
- Fix cache not being updated
|
||||
|
||||
## 0.2.2
|
||||
|
||||
- Fix name mismatch in the cache
|
||||
- Fix directory mapping with mounts
|
||||
- Add Support for refreshing the *arrs
|
||||
|
||||
## 0.2.1
|
||||
|
||||
- Fix Uncached torrents not being downloaded/downloaded
|
||||
- Minor bug fixed
|
||||
- Fix Race condition in the cache and file system
|
||||
|
||||
## 0.2.0
|
||||
|
||||
- Implement 0.2.0-beta changes
|
||||
- Removed Blackhole
|
||||
- Added QbitTorrent API
|
||||
- Cleaned up the code
|
||||
|
||||
## 0.2.0-beta
|
||||
|
||||
- Switch to QbitTorrent API instead of Blackhole
|
||||
- Rewrote the whole codebase
|
||||
|
||||
## 0.1.4
|
||||
|
||||
- Rewrote Report log
|
||||
- Fix YTS, 1337x not grabbing infohash
|
||||
- Fix Torrent symlink bug
|
||||
|
||||
## 0.1.3
|
||||
|
||||
- Searching for infohashes in the xml description/summary/comments
|
||||
- Added local cache support
|
||||
- Added max cache size
|
||||
- Rewrite blackhole.go
|
||||
- Bug fixes
|
||||
- Fixed indexer getting disabled
|
||||
- Fixed blackhole not working
|
||||
|
||||
## 0.1.2
|
||||
|
||||
- Bug fixes
|
||||
- Code cleanup
|
||||
- Get available hashes at once
|
||||
|
||||
## 0.1.1
|
||||
|
||||
- Added support for "No Blackhole" for Arrs
|
||||
- Added support for "Cached Only" for Proxy
|
||||
- Bug Fixes
|
||||
|
||||
## 0.1.0
|
||||
|
||||
- Initial Release
|
||||
- Added Real Debrid Support
|
||||
- Added Arrs Support
|
||||
- Added Proxy Support
|
||||
- Added Basic Authentication for Proxy
|
||||
- Added Rate Limiting for Debrid Providers
|
||||
77
docs/docs/configuration/arrs.md
Normal file
@@ -0,0 +1,77 @@
|
||||
# Arr Applications Configuration
|
||||
|
||||
Decypharr can integrate directly with Sonarr, Radarr, and other Arr applications. This section explains how to configure the Arr integration in your `config.json` file.
|
||||
|
||||
## Basic Configuration
|
||||
|
||||
The Arr applications are configured under the `arrs` key:
|
||||
|
||||
```json
|
||||
"arrs": [
|
||||
{
|
||||
"name": "sonarr",
|
||||
"host": "http://sonarr:8989",
|
||||
"token": "your-sonarr-api-key",
|
||||
"cleanup": true
|
||||
},
|
||||
{
|
||||
"name": "radarr",
|
||||
"host": "http://radarr:7878",
|
||||
"token": "your-radarr-api-key",
|
||||
"cleanup": true
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
### !!! note
|
||||
This configuration is optional if you've already set up the qBittorrent client in your Arr applications with the correct host and token information. It's particularly useful for the Repair Worker functionality.
|
||||
|
||||
|
||||
### Configuration Options
|
||||
Each Arr application supports the following options:
|
||||
|
||||
- `name`: The name of the Arr application, which should match the category in qBittorrent
|
||||
- `host`: The host URL of the Arr application, including protocol and port
|
||||
- `token`: The API token/key of the Arr application
|
||||
- `cleanup`: Whether to clean up the Arr queue (removes completed downloads). This is only useful for Sonarr.
|
||||
- `skip_repair`: Automated repair will be skipped for this *arr.
|
||||
- `download_uncached`: Whether to download uncached torrents (defaults to debrid/manual setting)
|
||||
|
||||
### Finding Your API Key
|
||||
#### Sonarr/Radarr/Lidarr
|
||||
|
||||
1. Go to Sonarr > Settings > General
|
||||
2. Look for "API Key" in the "Security" section
|
||||
3. Copy the API key
|
||||
|
||||
### Multiple Arr Applications
|
||||
You can configure multiple Arr applications by adding more entries to the arrs array:
|
||||
|
||||
```json
|
||||
"arrs": [
|
||||
{
|
||||
"name": "sonarr",
|
||||
"host": "http://sonarr:8989",
|
||||
"token": "your-sonarr-api-key",
|
||||
"cleanup": true
|
||||
},
|
||||
{
|
||||
"name": "sonarr-anime",
|
||||
"host": "http://sonarr-anime:8989",
|
||||
"token": "your-sonarr-anime-api-key",
|
||||
"cleanup": true
|
||||
},
|
||||
{
|
||||
"name": "radarr",
|
||||
"host": "http://radarr:7878",
|
||||
"token": "your-radarr-api-key",
|
||||
"cleanup": false
|
||||
},
|
||||
{
|
||||
"name": "lidarr",
|
||||
"host": "http://lidarr:8686",
|
||||
"token": "your-lidarr-api-key",
|
||||
"cleanup": false
|
||||
}
|
||||
]
|
||||
```
|
||||
131
docs/docs/configuration/debrid.md
Normal file
@@ -0,0 +1,131 @@
|
||||
|
||||
# Debrid Providers Configuration
|
||||
|
||||
Decypharr supports multiple Debrid providers. This section explains how to configure each provider in your `config.json` file.
|
||||
|
||||
## Basic Configuration
|
||||
|
||||
Each Debrid provider is configured in the `debrids` array:
|
||||
|
||||
```json
|
||||
"debrids": [
|
||||
{
|
||||
"name": "realdebrid",
|
||||
"api_key": "your-api-key",
|
||||
"folder": "/mnt/remote/realdebrid/__all__/",
|
||||
},
|
||||
{
|
||||
"name": "alldebrid",
|
||||
"api_key": "your-api-key",
|
||||
"folder": "/mnt/remote/alldebrid/downloads/"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
### Provider Options
|
||||
|
||||
Each Debrid provider accepts the following configuration options:
|
||||
|
||||
|
||||
#### Basic(Required) Options
|
||||
|
||||
- `name`: The name of the Debrid provider (realdebrid, alldebrid, debridlink, torbox)
|
||||
- `host`: The API endpoint of the Debrid provider
|
||||
- `api_key`: Your API key for the Debrid service (can be comma-separated for multiple keys)
|
||||
- `folder`: The folder where your Debrid content is mounted (via webdav, rclone, zurg, etc.)
|
||||
|
||||
#### Advanced Options
|
||||
|
||||
- `rate_limit`: Rate limit for API requests (null by default)
|
||||
- `download_uncached`: Whether to download uncached torrents (disabled by default)
|
||||
- `check_cached`: Whether to check if torrents are cached (disabled by default)
|
||||
- `use_webdav`: Whether to create a WebDAV server for this Debrid provider (disabled by default)
|
||||
- `proxy`: Proxy URL for the Debrid provider (optional)
|
||||
|
||||
#### WebDAV and Rclone Options
|
||||
- `torrents_refresh_interval`: Interval for refreshing torrent data (e.g., `15s`, `1m`, `1h`).
|
||||
- `download_links_refresh_interval`: Interval for refreshing download links (e.g., `40m`, `1h`).
|
||||
- `workers`: Number of concurrent workers for processing requests.
|
||||
- `serve_from_rclone`: Whether to serve files directly from Rclone (disabled by default)
|
||||
- `add_samples`: Whether to add sample files when adding torrents to debrid (disabled by default)
|
||||
- `folder_naming`: Naming convention for folders:
|
||||
- `original_no_ext`: Original file name without extension
|
||||
- `original`: Original file name with extension
|
||||
- `filename`: Torrent filename
|
||||
- `filename_no_ext`: Torrent filename without extension
|
||||
- `id`: Torrent ID
|
||||
- `hash`: Torrent hash
|
||||
- `auto_expire_links_after`: Time after which download links will expire (e.g., `3d`, `1w`).
|
||||
- `rc_url`, `rc_user`, `rc_pass`, `rc_refresh_dirs`: Rclone RC configuration for VFS refreshes
|
||||
- `directories`: A map of virtual folders to serve via the webDAV server. The key is the virtual folder name, and the values are map of filters and their value
|
||||
|
||||
#### Example of `directories` configuration
|
||||
```json
|
||||
"directories": {
|
||||
"Newly Added": {
|
||||
"filters": {
|
||||
"exclude": "9-1-1",
|
||||
"last_added": "20h"
|
||||
}
|
||||
},
|
||||
"Spiderman Collection": {
|
||||
"filters": {
|
||||
"regex": "(?i)spider[-\\s]?man(\\s+collection|\\s+\\d|\\s+trilogy|\\s+complete|\\s+ultimate|\\s+box\\s+set|:?\\s+homecoming|:?\\s+far\\s+from\\s+home|:?\\s+no\\s+way\\s+home)"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Example Configuration
|
||||
|
||||
#### Real Debrid
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "realdebrid",
|
||||
"api_key": "your-api-key",
|
||||
"folder": "/mnt/remote/realdebrid/__all__/",
|
||||
"rate_limit": null,
|
||||
"download_uncached": false,
|
||||
"use_webdav": true
|
||||
}
|
||||
```
|
||||
|
||||
#### All Debrid
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "alldebrid",
|
||||
"api_key": "your-api-key",
|
||||
"folder": "/mnt/remote/alldebrid/torrents/",
|
||||
"rate_limit": null,
|
||||
"download_uncached": false,
|
||||
"use_webdav": true
|
||||
}
|
||||
```
|
||||
|
||||
#### Debrid Link
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "debridlink",
|
||||
"api_key": "your-api-key",
|
||||
"folder": "/mnt/remote/debridlink/torrents/",
|
||||
"rate_limit": null,
|
||||
"download_uncached": false,
|
||||
"use_webdav": true
|
||||
}
|
||||
```
|
||||
|
||||
#### Torbox
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "torbox",
|
||||
"api_key": "your-api-key",
|
||||
"folder": "/mnt/remote/torbox/torrents/",
|
||||
"rate_limit": null,
|
||||
"download_uncached": false,
|
||||
"use_webdav": true
|
||||
}
|
||||
```
|
||||
81
docs/docs/configuration/general.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# General Configuration
|
||||
|
||||
This section covers the basic configuration options for Decypharr that apply to the entire application.
|
||||
|
||||
## Basic Settings
|
||||
|
||||
Here are the fundamental configuration options:
|
||||
|
||||
```json
|
||||
{
|
||||
"use_auth": false,
|
||||
"port": 8282,
|
||||
"log_level": "info",
|
||||
"discord_webhook_url": "",
|
||||
"min_file_size": 0,
|
||||
"max_file_size": 0,
|
||||
"allowed_file_types": ["mp4", "mkv", "avi", ...],
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Configuration Options
|
||||
|
||||
#### Log Level
|
||||
The `log_level` setting determines how verbose the application logs will be:
|
||||
|
||||
- `debug`: Detailed information, useful for troubleshooting
|
||||
- `info`: General operational information (default)
|
||||
- `warn`: Warning messages
|
||||
- `error`: Error messages only
|
||||
- `trace`: Very detailed information, including all requests and responses
|
||||
|
||||
#### Port
|
||||
|
||||
The `port` setting specifies the port on which Decypharr will run. The default is `8282`. You can change this to any available port on your server.
|
||||
|
||||
Ensure this port:
|
||||
|
||||
- Is not used by other applications
|
||||
- Is accessible to your Arr applications
|
||||
- Is properly exposed if using Docker (see the Docker Compose example in the Installation guide)
|
||||
|
||||
|
||||
#### Authentication
|
||||
The `use_auth` option enables basic authentication for the UI:
|
||||
|
||||
```json
|
||||
"use_auth": true
|
||||
```
|
||||
|
||||
When enabled, you'll need to provide a username and password to access the Decypharr interface.
|
||||
|
||||
|
||||
#### File Size Limits
|
||||
|
||||
You can set minimum and maximum file size limits for torrents:
|
||||
```json
|
||||
"min_file_size": 0,
|
||||
"max_file_size": 0
|
||||
```
|
||||
|
||||
#### Allowed File Types
|
||||
You can restrict the types of files that Decypharr will process by specifying allowed file extensions. This is useful for filtering out unwanted file types.
|
||||
|
||||
```json
|
||||
"allowed_file_types": [
|
||||
"mp4", "mkv", "avi", "mov",
|
||||
"m4v", "mpg", "mpeg", "wmv",
|
||||
"m4a", "mp3", "flac", "wav"
|
||||
]
|
||||
```
|
||||
|
||||
If not specified, all movie, TV show, and music file types are allowed by default.
|
||||
|
||||
|
||||
#### Discord Notifications
|
||||
To receive notifications on Discord, add your webhook URL:
|
||||
```json
|
||||
"discord_webhook_url": "https://discord.com/api/webhooks/..."
|
||||
```
|
||||
This will send notifications for various events, such as successful downloads or errors.
|
||||
43
docs/docs/configuration/index.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Configuration Overview
|
||||
|
||||
Decypharr uses a JSON configuration file to manage its settings. This file should be named `config.json` and placed in your configured directory.
|
||||
|
||||
## Basic Configuration
|
||||
|
||||
Here's a minimal configuration to get started:
|
||||
|
||||
```json
|
||||
{
|
||||
"debrids": [
|
||||
{
|
||||
"name": "realdebrid",
|
||||
"api_key": "realdebrid_key",
|
||||
"folder": "/mnt/remote/realdebrid/__all__/",
|
||||
"use_webdav": true
|
||||
}
|
||||
],
|
||||
"qbittorrent": {
|
||||
"port": "8282",
|
||||
"download_folder": "/mnt/symlinks/",
|
||||
"categories": ["sonarr", "radarr"]
|
||||
},
|
||||
"repair": {
|
||||
"enabled": false,
|
||||
"interval": "12h"
|
||||
},
|
||||
"use_auth": false,
|
||||
"log_level": "info"
|
||||
}
|
||||
```
|
||||
|
||||
### Configuration Sections
|
||||
|
||||
Decypharr's configuration is divided into several sections:
|
||||
|
||||
- [General Configuration](general.md) - Basic settings like logging and authentication
|
||||
- [Debrid Providers](debrid.md) - Configure one or more Debrid services
|
||||
- [qBittorrent Settings](qbittorrent.md) - Settings for the qBittorrent API
|
||||
- [Arr Integration](arrs.md) - Configuration for Sonarr, Radarr, etc.
|
||||
|
||||
Full Configuration Example
|
||||
For a complete configuration file with all available options, see our [full configuration example](../extras/config.full.json).
|
||||
61
docs/docs/configuration/qbittorrent.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# qBittorrent Configuration
|
||||
|
||||
Decypharr emulates a qBittorrent instance to integrate with Arr applications. This section explains how to configure the qBittorrent settings in your `config.json` file.
|
||||
|
||||
## Basic Configuration
|
||||
|
||||
The qBittorrent functionality is configured under the `qbittorrent` key:
|
||||
|
||||
```json
|
||||
"qbittorrent": {
|
||||
"download_folder": "/mnt/symlinks/",
|
||||
"categories": ["sonarr", "radarr", "lidarr"],
|
||||
"refresh_interval": 5
|
||||
}
|
||||
```
|
||||
|
||||
### Configuration Options
|
||||
#### Required Settings
|
||||
|
||||
- `download_folder`: The folder where symlinks or downloaded files will be placed
|
||||
- `categories`: An array of categories to organize downloads (usually matches your Arr applications)
|
||||
|
||||
#### Advanced Settings
|
||||
|
||||
- `refresh_interval`: How often (in seconds) to refresh the Arrs Monitored Downloads (default: 5)
|
||||
- `max_downloads`: The maximum number of concurrent downloads. This is only for downloading real files(Not symlinks). If you set this to 0, it will download all files at once. This is not recommended for most users.(default: 5)
|
||||
- `skip_pre_cache`: This option disables the process of pre-caching files. This caches a small portion of the file to speed up your *arrs import process.
|
||||
|
||||
#### Categories
|
||||
Categories help organize your downloads and match them to specific Arr applications. Typically, you'll want to configure categories that match your Sonarr, Radarr, or other Arr applications:
|
||||
|
||||
```json
|
||||
"categories": ["sonarr", "radarr", "lidarr", "readarr"]
|
||||
```
|
||||
|
||||
When setting up your Arr applications to connect to Decypharr, you'll specify these same category names.
|
||||
|
||||
#### Download Folder
|
||||
|
||||
The `download_folder` setting specifies where Decypharr will place downloaded files or create symlinks:
|
||||
|
||||
```json
|
||||
"download_folder": "/mnt/symlinks/"
|
||||
```
|
||||
|
||||
This folder should be:
|
||||
|
||||
- Accessible to Decypharr
|
||||
- Accessible to your Arr applications
|
||||
- Have sufficient space if downloading files locally
|
||||
|
||||
|
||||
#### Refresh Interval
|
||||
The refresh_interval setting controls how often Decypharr checks for updates from your Arr applications:
|
||||
|
||||
```json
|
||||
"refresh_interval": 5
|
||||
```
|
||||
|
||||
|
||||
This value is in seconds. Lower values provide more responsive updates but may increase CPU usage.
|
||||
88
docs/docs/extras/config.full.json
Normal file
@@ -0,0 +1,88 @@
|
||||
{
|
||||
"debrids": [
|
||||
{
|
||||
"name": "realdebrid",
|
||||
"api_key": "realdebrid_key",
|
||||
"folder": "/mnt/remote/realdebrid/__all__/",
|
||||
"download_api_keys": [],
|
||||
"proxy": "",
|
||||
"rate_limit": "250/minute",
|
||||
"download_uncached": false,
|
||||
"use_webdav": true,
|
||||
"torrents_refresh_interval": "15s",
|
||||
"folder_naming": "original_no_ext",
|
||||
"auto_expire_links_after": "3d",
|
||||
"rc_url": "http://your-ip-address:9990",
|
||||
"rc_user": "your_rclone_rc_user",
|
||||
"rc_pass": "your_rclone_rc_pass"
|
||||
},
|
||||
{
|
||||
"name": "torbox",
|
||||
"api_key": "torbox_api_key",
|
||||
"folder": "/mnt/remote/torbox/torrents/",
|
||||
"rate_limit": "250/minute",
|
||||
"download_uncached": false,
|
||||
},
|
||||
{
|
||||
"name": "debridlink",
|
||||
"api_key": "debridlink_key",
|
||||
"folder": "/mnt/remote/debridlink/torrents/",
|
||||
"rate_limit": "250/minute",
|
||||
"download_uncached": false,
|
||||
},
|
||||
{
|
||||
"name": "alldebrid",
|
||||
"api_key": "alldebrid_key",
|
||||
"folder": "/mnt/remote/alldebrid/magnet/",
|
||||
"rate_limit": "600/minute",
|
||||
"download_uncached": false,
|
||||
}
|
||||
],
|
||||
"max_cache_size": 1000,
|
||||
"qbittorrent": {
|
||||
"port": "8282",
|
||||
"download_folder": "/mnt/symlinks/",
|
||||
"categories": ["sonarr", "radarr"],
|
||||
"refresh_interval": 5,
|
||||
"skip_pre_cache": false
|
||||
},
|
||||
"arrs": [
|
||||
{
|
||||
"name": "sonarr",
|
||||
"host": "http://sonarr:8989",
|
||||
"token": "arr_key",
|
||||
"cleanup": true,
|
||||
"skip_repair": true,
|
||||
"download_uncached": false
|
||||
},
|
||||
{
|
||||
"name": "radarr",
|
||||
"host": "http://radarr:7878",
|
||||
"token": "arr_key",
|
||||
"cleanup": false,
|
||||
"download_uncached": false
|
||||
},
|
||||
{
|
||||
"name": "lidarr",
|
||||
"host": "http://lidarr:8686",
|
||||
"token": "arr_key",
|
||||
"cleanup": false,
|
||||
"skip_repair": true,
|
||||
"download_uncached": false
|
||||
}
|
||||
],
|
||||
"repair": {
|
||||
"enabled": false,
|
||||
"interval": "12h",
|
||||
"run_on_start": false,
|
||||
"zurg_url": "",
|
||||
"use_webdav": false,
|
||||
"auto_process": false
|
||||
},
|
||||
"log_level": "info",
|
||||
"min_file_size": "",
|
||||
"max_file_size": "",
|
||||
"allowed_file_types": [],
|
||||
"use_auth": false,
|
||||
"discord_webhook_url": "https://discord.com/api/webhooks/..."
|
||||
}
|
||||
5
docs/docs/extras/rclone.conf
Normal file
@@ -0,0 +1,5 @@
|
||||
[decypharr]
|
||||
type = webdav
|
||||
url = http://decypharr:8282/webdav/realdebrid
|
||||
vendor = other
|
||||
pacer_min_sleep = 0
|
||||
@@ -25,10 +25,8 @@ The Decypharr user interface provides:
|
||||
|
||||
Decypharr includes several advanced features that extend its capabilities:
|
||||
|
||||
- [Repair Support](repair-worker.md): Identifies and fixes issues with your media files
|
||||
- WebDav Server: Provides direct access to your Debrid files
|
||||
- Mounting Support: Allows you to mount Debrid services using [rclone](https://rclone.org), making it easy to access your files directly from your system
|
||||
- Multiple Debrid Providers: Supports Real Debrid, Torbox, Debrid Link, and All Debrid, allowing you to choose the best service for your needs
|
||||
- [Repair Worker](repair-worker.md): Identifies and fixes issues with your media files
|
||||
- [WebDAV Server](webdav.md): Provides direct access to your Debrid files
|
||||
|
||||
## Supported Debrid Providers
|
||||
|
||||
@@ -38,7 +36,5 @@ Decypharr supports multiple Debrid providers:
|
||||
- Torbox
|
||||
- Debrid Link
|
||||
- All Debrid
|
||||
- Premiumize(Coming Soon)
|
||||
- Usenet(Coming Soon)
|
||||
|
||||
Each provider can be configured separately, allowing you to use one or multiple services simultaneously.
|
||||
@@ -15,4 +15,27 @@ The Repair Worker performs the following tasks:
|
||||
|
||||
## Configuration
|
||||
|
||||
You can enable and configure the Repair Worker in the Decypharr settings. It can be set to run at regular intervals, such as every 12 hours or daily.
|
||||
To enable and configure the Repair Worker, add the following to your `config.json`:
|
||||
|
||||
```json
|
||||
"repair": {
|
||||
"enabled": true,
|
||||
"interval": "12h",
|
||||
"use_webdav": false,
|
||||
"zurg_url": "http://localhost:9999",
|
||||
"auto_process": true
|
||||
}
|
||||
```
|
||||
|
||||
### Configuration Options
|
||||
|
||||
- `enabled`: Set to `true` to enable the Repair Worker.
|
||||
- `interval`: The time interval for the Repair Worker to run (e.g., `12h`, `1d`).
|
||||
- `use_webdav`: If set to `true`, the Repair Worker will use WebDAV for file operations.
|
||||
- `zurg_url`: The URL for the Zurg service (if using).
|
||||
- `auto_process`: If set to `true`, the Repair Worker will automatically process files that it finds issues with.
|
||||
|
||||
|
||||
### Performance Tips
|
||||
- For users of the WebDAV server, enable `use_webdav` for exponentially faster repair processes
|
||||
- If using Zurg, set the `zurg_url` parameter to greatly improve repair speed
|
||||
74
docs/docs/features/webdav.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# WebDAV Server
|
||||
|
||||

|
||||
|
||||
Decypharr includes a built-in WebDAV server that provides direct access to your Debrid files, making them easily accessible to media players and other applications.
|
||||
|
||||
|
||||
## Overview
|
||||
|
||||
While most Debrid providers have their own WebDAV servers, Decypharr's implementation offers faster access and additional features.
|
||||
|
||||
## Accessing the WebDAV Server
|
||||
|
||||
- URL: `http://localhost:8282/webdav` or `http://<your-server-ip>:8282/webdav`
|
||||
|
||||
## Configuration
|
||||
|
||||
You can configure WebDAV settings either globally or per-Debrid provider in your `config.json`:
|
||||
|
||||
```json
|
||||
"webdav": {
|
||||
"torrents_refresh_interval": "15s",
|
||||
"download_links_refresh_interval": "40m",
|
||||
"folder_naming": "original_no_ext",
|
||||
"auto_expire_links_after": "3d",
|
||||
"rc_url": "http://localhost:5572",
|
||||
"rc_user": "username",
|
||||
"rc_pass": "password",
|
||||
"serve_from_rclone": false,
|
||||
"directories": {
|
||||
"Newly Added": {
|
||||
"filters": {
|
||||
"exclude": "9-1-1",
|
||||
"last_added": "20h"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Configuration Options
|
||||
|
||||
- `torrents_refresh_interval`: Interval for refreshing torrent data (e.g., `15s`, `1m`, `1h`).
|
||||
- `download_links_refresh_interval`: Interval for refreshing download links (e.g., `40m`, `1h`).
|
||||
- `workers`: Number of concurrent workers for processing requests.
|
||||
- folder_naming: Naming convention for folders:
|
||||
- `original_no_ext`: Original file name without extension
|
||||
- `original`: Original file name with extension
|
||||
- `filename`: Torrent filename
|
||||
- `filename_no_ext`: Torrent filename without extension
|
||||
- `id`: Torrent ID
|
||||
- `auto_expire_links_after`: Time after which download links will expire (e.g., `3d`, `1w`).
|
||||
- `rc_url`, `rc_user`, `rc_pass`: Rclone RC configuration for VFS refreshes
|
||||
- `directories`: A map of virtual folders to serve via the WebDAV server. The key is the virtual folder name, and the values are a map of filters and their values.
|
||||
- `serve_from_rclone`: Whether to serve files directly from Rclone (disabled by default).
|
||||
|
||||
### Using with Media Players
|
||||
The WebDAV server works well with media players like:
|
||||
|
||||
- Infuse
|
||||
- VidHub
|
||||
- Plex, Emby, Jellyfin (with rclone, Check [this guide](../guides/rclone.md))
|
||||
- Kodi
|
||||
|
||||
### Mounting with Rclone
|
||||
You can mount the WebDAV server locally using Rclone. Example configuration:
|
||||
|
||||
```conf
|
||||
[decypharr]
|
||||
type = webdav
|
||||
url = http://localhost:8282/webdav/realdebrid
|
||||
vendor = other
|
||||
```
|
||||
For a complete Rclone configuration example, see our [sample rclone.conf](../extras/rclone.conf).
|
||||
@@ -2,25 +2,21 @@
|
||||
|
||||
While Decypharr provides a Qbittorent API for integration with media management applications, it also allows you to manually download torrents directly through its interface. This guide will walk you through the process of downloading torrents using Decypharr.
|
||||
|
||||
- You can either use the Decypharr UI to add torrents manually or use its [API](../api.md) to automate the process.
|
||||
- You can either use the Decypharr UI to add torrents manually or use its API to automate the process.
|
||||
|
||||
## Manual Downloading
|
||||
|
||||

|
||||
To manually download a torrent using Decypharr, follow these steps:
|
||||
1. Navigate to the "Download" section in the Decypharr UI.
|
||||
1. **Access the Download Page**: Navigate to the "Download" section in the Decypharr UI.
|
||||
2. You can either upload torrent file(s) or paste magnet links directly into the input fields
|
||||
3. Select the action(defaults to Symlink)
|
||||
|
||||
4. Add any additional options, such as:
|
||||
5. Add any additional options, such as:
|
||||
- *Download Folder*: Specify the folder where the downloaded files will be saved.
|
||||
- *Arr Category*: Choose the category for the download, which helps in organizing files in your media management applications.
|
||||
- **Post Download Action**: Select what to do after the download completes:
|
||||
- **Create Symlink**: Create a symlink to the downloaded files in the mount folder(default)
|
||||
- **Download**: Download the file directly.
|
||||
- **No Action**: Do nothing after the download completes.
|
||||
- **Debrid Provider**: Choose which Debrid service to use for the download(if you have multiple)
|
||||
- **Download Uncached**: If enabled, Decypharr will attempt to download uncached files from the Debrid service.
|
||||
- **File Size Limits**: Set minimum and maximum file size limits if needed.
|
||||
- **Allowed File Types**: Specify which file types are allowed for download.
|
||||
|
||||
Note:
|
||||
- If you use an arr category, your download will go into **{download_folder}/{arr}**
|
||||
@@ -1,4 +1,5 @@
|
||||
# Guides for setting up Decypharr
|
||||
|
||||
- [Manual Downloading with Decypharr](downloading.md)
|
||||
- [Internal Mounting](internal-mounting.md)
|
||||
|
||||
- [Setting up with Rclone](rclone.md)
|
||||
- [Manual Downloading with Decypharr](downloading.md)
|
||||
@@ -1,85 +0,0 @@
|
||||
# Internal Mounting
|
||||
|
||||
This guide explains how to use Decypharr's internal mounting feature to eliminate the need for external rclone setup.
|
||||
|
||||
## Overview
|
||||
|
||||

|
||||
|
||||
Instead of requiring users to install and configure rclone separately, Decypharr can now mount your WebDAV endpoints internally using rclone as a library dependency. This provides a seamless experience where files appear as regular filesystem paths without any external dependencies.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- **Docker users**: FUSE support may need to be enabled in the container depending on your Docker setup
|
||||
- **macOS users**: May need [macFUSE](https://osxfuse.github.io/) installed for mounting functionality
|
||||
- **Linux users**: FUSE should be available by default on most distributions
|
||||
- **Windows users**: Mounting functionality may be limited
|
||||
|
||||
### Configuration Options
|
||||
|
||||
You can set the options in the Web UI or directly in the configuration file:
|
||||
|
||||
#### Note:
|
||||
Check the Rclone documentation for more details on the available options: [Rclone Mount Options](https://rclone.org/commands/rclone_mount/).
|
||||
|
||||
## How It Works
|
||||
|
||||
1. **WebDAV Server**: Decypharr starts its internal WebDAV server for enabled providers
|
||||
2. **Internal Mount**: Rclone is used internally to mount the WebDAV endpoint to a local filesystem path
|
||||
3. **File Access**: Your applications can access files using regular filesystem paths like `/mnt/decypharr/realdebrid/__all__/MyMovie/`
|
||||
|
||||
## Benefits
|
||||
|
||||
- **Automatic Setup**: Mounting is handled automatically by Decypharr using internal rclone rcd
|
||||
- **Filesystem Access**: Files appear as regular directories and files
|
||||
- **Seamless Integration**: Works with existing media servers without changes
|
||||
|
||||
## Docker Compose
|
||||
|
||||
```yaml
|
||||
version: '3.8'
|
||||
services:
|
||||
decypharr:
|
||||
image: sirrobot01/decypharr:latest
|
||||
container_name: decypharr
|
||||
ports:
|
||||
- "8282:8282"
|
||||
volumes:
|
||||
- ./config:/config
|
||||
- /mnt:/mnt:rshared # Important: use 'rshared' for mount propagation
|
||||
devices:
|
||||
- /dev/fuse:/dev/fuse:rwm
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor:unconfined
|
||||
environment:
|
||||
- UMASK=002
|
||||
- PUID=1000 # Change to your user ID
|
||||
- PGID=1000 # Change to your group ID
|
||||
```
|
||||
|
||||
**Important Docker Notes:**
|
||||
- Mount volumes with `:rshared` to allow mount propagation
|
||||
- Include `/dev/fuse` device for FUSE mounting
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Mount Failures
|
||||
|
||||
If mounting fails, check:
|
||||
|
||||
1. **FUSE Installation**:
|
||||
- **macOS**: Install macFUSE from https://osxfuse.github.io/
|
||||
- **Linux**: Install fuse package (`apt install fuse` or `yum install fuse`)
|
||||
- **Docker**: Fuse is already included in the container, but ensure the host supports it
|
||||
2. **Permissions**: Ensure the application has sufficient privileges
|
||||
|
||||
### No Mount Methods Available
|
||||
|
||||
If you see "no mount method available" errors:
|
||||
|
||||
1. **Check Platform Support**: Some platforms have limited FUSE support
|
||||
2. **Install Dependencies**: Ensure FUSE libraries are installed
|
||||
3. **Use WebDAV Directly**: Access files via `http://localhost:8282/webdav/provider/`
|
||||
4. **External Mounting**: Use OS-native WebDAV mounting as fallback
|
||||
152
docs/docs/guides/rclone.md
Normal file
@@ -0,0 +1,152 @@
|
||||
# Setting up Decypharr with Rclone
|
||||
|
||||
This guide will help you set up Decypharr with Rclone, allowing you to use your Debrid providers as a remote storage solution.
|
||||
|
||||
#### Rclone
|
||||
Make sure you have Rclone installed and configured on your system. You can follow the [Rclone installation guide](https://rclone.org/install/) for instructions.
|
||||
|
||||
It's recommended to use a docker version of Rclone, as it provides a consistent environment across different platforms.
|
||||
|
||||
|
||||
### Steps
|
||||
|
||||
We'll be using docker compose to set up Rclone and Decypharr together.
|
||||
|
||||
#### Note
|
||||
This guide assumes you have a basic understanding of Docker and Docker Compose. If you're new to Docker, consider checking out the [Docker documentation](https://docs.docker.com/get-started/) for more information.
|
||||
|
||||
Also, ensure you have Docker and Docker Compose installed on your system. You can find installation instructions in the [Docker documentation](https://docs.docker.com/get-docker/) and [Docker Compose documentation](https://docs.docker.com/compose/install/).
|
||||
|
||||
|
||||
Create a directory for your Decypharr and Rclone setup:
|
||||
```bash
|
||||
mkdir -p /opt/decypharr
|
||||
mkdir -p /opt/rclone
|
||||
mkdir -p /mnt/remote/realdebrid
|
||||
|
||||
# Set permissions
|
||||
chown -R $USER:$USER /opt/decypharr
|
||||
chown -R $USER:$USER /opt/rclone
|
||||
chown -R $USER:$USER /mnt/remote/realdebrid
|
||||
```
|
||||
|
||||
Create a `rclone.conf` file in `/opt/rclone/` with your Rclone configuration.
|
||||
|
||||
```conf
|
||||
[decypharr]
|
||||
type = webdav
|
||||
url = http://your-ip-or-domain:8282/webdav/realdebrid
|
||||
vendor = other
|
||||
pacer_min_sleep = 0
|
||||
```
|
||||
|
||||
Create a `config.json` file in `/opt/decypharr/` with your Decypharr configuration.
|
||||
|
||||
```json
|
||||
{
|
||||
"debrids": [
|
||||
{
|
||||
"name": "realdebrid",
|
||||
"api_key": "realdebrid_key",
|
||||
"folder": "/mnt/remote/realdebrid/__all__/",
|
||||
"rate_limit": "250/minute",
|
||||
"use_webdav": true,
|
||||
"rc_url": "rclone:5572"
|
||||
}
|
||||
],
|
||||
"qbittorrent": {
|
||||
"download_folder": "data/media/symlinks/",
|
||||
"refresh_interval": 10
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Docker Compose Setup
|
||||
|
||||
- Check your current user and group IDs by running `id -u` and `id -g` in your terminal. You can use these values to set the `PUID` and `PGID` environment variables in the Docker Compose file.
|
||||
- You should also set `user` to your user ID and group ID in the Docker Compose file to ensure proper file permissions.
|
||||
|
||||
Create a `docker-compose.yml` file with the following content:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
decypharr:
|
||||
image: cy01/blackhole:latest
|
||||
container_name: decypharr
|
||||
user: "${PUID:-1000}:${PGID:-1000}"
|
||||
volumes:
|
||||
- /mnt/:/mnt:rslave
|
||||
- /opt/decypharr/:/app
|
||||
environment:
|
||||
- UMASK=002
|
||||
- PUID=1000 # Replace with your user ID
|
||||
- PGID=1000 # Replace with your group ID
|
||||
ports:
|
||||
- "8282:8282/tcp"
|
||||
restart: unless-stopped
|
||||
|
||||
rclone:
|
||||
image: rclone/rclone:latest
|
||||
container_name: rclone
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
TZ: UTC
|
||||
ports:
|
||||
- 5572:5572
|
||||
volumes:
|
||||
- /mnt/remote/realdebrid:/data:rshared
|
||||
- /opt/rclone/rclone.conf:/config/rclone/rclone.conf
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor:unconfined
|
||||
devices:
|
||||
- /dev/fuse:/dev/fuse:rwm
|
||||
depends_on:
|
||||
decypharr:
|
||||
condition: service_healthy
|
||||
restart: true
|
||||
command: "mount decypharr: /data --allow-non-empty --allow-other --dir-cache-time 10s --rc --rc-addr :5572 --rc-no-auth"
|
||||
```
|
||||
|
||||
#### Docker Notes
|
||||
|
||||
- Ensure that the `/mnt/` directory is mounted correctly to access your media files.
|
||||
- You can check your current user and group IDs and UMASK by running `id -a` and `umask` commands in your terminal.
|
||||
- You can adjust the `PUID` and `PGID` environment variables to match your user and group IDs for proper file permissions.
|
||||
- Also adding `--uid=$YOUR_PUID --gid=$YOUR_PGID` to the `rclone mount` command can help with permissions.
|
||||
- The `UMASK` environment variable can be set to control file permissions created by Decypharr.
|
||||
|
||||
Start the containers:
|
||||
```bash
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
Access the Decypharr web interface at `http://your-ip-address:8282` and configure your settings as needed.
|
||||
|
||||
- Access your webdav server at `http://your-ip-address:8282/webdav` to see your files.
|
||||
- You should be able to see your files in the `/mnt/remote/realdebrid/__all__/` directory.
|
||||
- You can now use your Debrid provider as a remote storage solution with Rclone and Decypharr.
|
||||
- You can also use the Rclone mount command to mount your Debrid provider locally. For example:
|
||||
|
||||
|
||||
### Notes
|
||||
|
||||
- Make sure to replace `your-ip-address` with the actual IP address of your server.
|
||||
- You can use multiple Debrid providers by adding them to the `debrids` array in the `config.json` file.
|
||||
|
||||
For each provider, you'll need a different rclone. OR you can change your `rclone.conf`
|
||||
|
||||
|
||||
```apache
|
||||
[decypharr]
|
||||
type = webdav
|
||||
url = http://your-ip-or-domain:8282/webdav/
|
||||
vendor = other
|
||||
pacer_min_sleep = 0
|
||||
```
|
||||
|
||||
You'll still be able to access the directories via `/mnt/remote/realdebrid, /mnt/remote/alldebrid` etc
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 293 KiB After Width: | Height: | Size: 218 KiB |
|
Before Width: | Height: | Size: 431 KiB |
|
Before Width: | Height: | Size: 417 KiB After Width: | Height: | Size: 188 KiB |
|
Before Width: | Height: | Size: 286 KiB After Width: | Height: | Size: 226 KiB |
|
Before Width: | Height: | Size: 264 KiB |
|
Before Width: | Height: | Size: 169 KiB |
|
Before Width: | Height: | Size: 364 KiB |
|
Before Width: | Height: | Size: 216 KiB |
|
Before Width: | Height: | Size: 264 KiB After Width: | Height: | Size: 264 KiB |
@@ -1,19 +1,20 @@
|
||||
# Decypharr
|
||||
{: .light-mode-image}
|
||||
{: .dark-mode-image}
|
||||
|
||||

|
||||
|
||||
**Decypharr** is an implementation of QbitTorrent with **Multiple Debrid service support**, written in Go.
|
||||
|
||||
## What is Decypharr?
|
||||
|
||||
**TLDR**; Decypharr is a self-hosted, open-source download client that integrates with multiple Debrid services. It provides a user-friendly interface for managing files and supports popular media management applications like Sonarr and Radarr.
|
||||
TLDR; Decypharr is a self-hosted, open-source torrent client that integrates with multiple Debrid services. It provides a user-friendly interface for managing torrents and supports popular media management applications like Sonarr and Radarr.
|
||||
|
||||
|
||||
## Key Features
|
||||
|
||||
- Mock Qbittorent API that supports Sonarr, Radarr, Lidarr, and other Arr applications
|
||||
- Full-fledged UI for managing torrents
|
||||
- Multiple Debrid providers support
|
||||
- WebDAV server support for each Debrid provider with an optional mounting feature(using [rclone](https://rclone.org))
|
||||
- WebDAV server support for each Debrid provider
|
||||
- Repair Worker for missing files, symlinks etc
|
||||
|
||||
## Supported Debrid Providers
|
||||
|
||||
@@ -18,6 +18,7 @@ You can use either Docker Hub or GitHub Container Registry to pull the image:
|
||||
- `latest`: The latest stable release
|
||||
- `beta`: The latest beta release
|
||||
- `vX.Y.Z`: A specific version (e.g., `v0.1.0`)
|
||||
- `nightly`: The latest nightly build (usually unstable)
|
||||
- `experimental`: The latest experimental build (highly unstable)
|
||||
|
||||
### Docker CLI Setup
|
||||
@@ -30,13 +31,12 @@ Run the Docker container:
|
||||
```bash
|
||||
docker run -d \
|
||||
--name decypharr \
|
||||
--restart unless-stopped \
|
||||
-p 8282:8282 \
|
||||
-v /mnt/:/mnt:rshared \
|
||||
-v /mnt/:/mnt \
|
||||
-v ./config/:/app \
|
||||
--device /dev/fuse:/dev/fuse:rwm \
|
||||
--cap-add SYS_ADMIN \
|
||||
--security-opt apparmor:unconfined \
|
||||
-e PUID=1000 \
|
||||
-e PGID=1000 \
|
||||
-e UMASK=002 \
|
||||
cy01/blackhole:latest
|
||||
```
|
||||
|
||||
@@ -52,15 +52,10 @@ services:
|
||||
ports:
|
||||
- "8282:8282"
|
||||
volumes:
|
||||
- /mnt/:/mnt:rshared
|
||||
- ./config/:/app
|
||||
- /mnt/:/mnt:rslave # Mount your media directory
|
||||
- ./config/:/app # config.json must be in this directory
|
||||
- QBIT_PORT=8282 # qBittorrent Port (optional)
|
||||
restart: unless-stopped
|
||||
devices:
|
||||
- /dev/fuse:/dev/fuse:rwm
|
||||
cap_add:
|
||||
- SYS_ADMIN
|
||||
security_opt:
|
||||
- apparmor:unconfined
|
||||
```
|
||||
|
||||
Run the Docker Compose setup:
|
||||
@@ -72,7 +67,7 @@ docker-compose up -d
|
||||
## Binary Installation
|
||||
If you prefer not to use Docker, you can download and run the binary directly.
|
||||
|
||||
Download your OS-specific release from the [release page](https://github.com/sirrobot01/decypharr/releases).
|
||||
Download your OS-specific release from the [releases page](https://github.com/sirrobot01/decypharr/releases).
|
||||
Create a configuration file (see Configuration)
|
||||
Run the binary:
|
||||
|
||||
@@ -81,15 +76,44 @@ chmod +x decypharr
|
||||
./decypharr --config /path/to/config/folder
|
||||
```
|
||||
|
||||
The config directory should contain your config.json file.
|
||||
|
||||
## config.json
|
||||
|
||||
The `config.json` file is where you configure Decypharr. You can find a sample configuration file in the `configs` directory of the repository.
|
||||
|
||||
You can also configure Decypharr through the web interface, but it's recommended to start with the config file for initial setup.
|
||||
|
||||
```json
|
||||
{
|
||||
"debrids": [
|
||||
{
|
||||
"name": "realdebrid",
|
||||
"api_key": "your_api_key_here",
|
||||
"folder": "/mnt/remote/realdebrid/__all__/",
|
||||
"use_webdav": true
|
||||
}
|
||||
],
|
||||
"qbittorrent": {
|
||||
"download_folder": "/mnt/symlinks/",
|
||||
"categories": ["sonarr", "radarr"]
|
||||
},
|
||||
"use_auth": false,
|
||||
"log_level": "info",
|
||||
"port": "8282"
|
||||
}
|
||||
```
|
||||
|
||||
### Notes for Docker Users
|
||||
|
||||
- Ensure that the `/mnt/` directory is mounted correctly to access your media files.
|
||||
- The `./config/` directory should contain your `config.json` file.
|
||||
- You can adjust the `PUID` and `PGID` environment variables to match your user and group IDs for proper file permissions.
|
||||
- The `UMASK` environment variable can be set to control file permissions created by Decypharr.
|
||||
|
||||
##### Health Checks
|
||||
- Health checks are disabled by default. You can enable them by adding a `healthcheck` section in your `docker-compose.yml` file.
|
||||
- Health checks the availability of several parts of the application;
|
||||
- Health checks checks for availability of several parts of the application;
|
||||
- The main web interface
|
||||
- The qBittorrent API
|
||||
- The WebDAV server (if enabled). You should disable health checks for the initial indexes as they can take a long time to complete.
|
||||
@@ -101,7 +125,7 @@ services:
|
||||
...
|
||||
healthcheck:
|
||||
test: ["CMD", "/usr/bin/healthcheck", "--config", "/app/"]
|
||||
interval: 10s
|
||||
interval: 5s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
```
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
/* Light mode image - visible by default */
|
||||
.light-mode-image {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* Dark mode image - hidden by default */
|
||||
.dark-mode-image {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* When dark theme (slate) is active */
|
||||
[data-md-color-scheme="slate"] .light-mode-image {
|
||||
display: none;
|
||||
}
|
||||
|
||||
[data-md-color-scheme="slate"] .dark-mode-image {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* Optional: smooth transition */
|
||||
.light-mode-image,
|
||||
.dark-mode-image {
|
||||
transition: opacity 0.2s ease-in-out;
|
||||
}
|
||||
@@ -2,35 +2,15 @@
|
||||
|
||||
This guide will help you get started with Decypharr after installation.
|
||||
|
||||
After installing Decypharr, you can access the web interface at `http://localhost:8282` or your configured host/port.
|
||||
## Basic Setup
|
||||
|
||||
### Initial Configuration
|
||||
If it's the first time you're accessing the UI, you will be prompted to set up your credentials. You can skip this step if you don't want to enable authentication. If you choose to set up credentials, enter a username and password confirm password, then click **Save**. You will be redirected to the settings page.
|
||||
1. Create your `config.json` file (see [Configuration](configuration/index.md) for details)
|
||||
2. Start the Decypharr service using Docker or binary
|
||||
3. Access the UI at `http://localhost:8282` (or your configured host/port)
|
||||
4. Connect your Arr applications (Sonarr, Radarr, etc.)
|
||||
|
||||
### Debrid Configuration
|
||||

|
||||
- Click on **Debrid** in the tab
|
||||
- Add your desired Debrid services (Real Debrid, Torbox, Debrid Link, All Debrid) by entering the required API keys or tokens.
|
||||
- Set the **Mount/Rclone Folder**. This is where decypharr will look for added torrents to symlink them to your media library.
|
||||
- If you're using internal webdav, do not forget the `/__all__` suffix
|
||||
- Enable WebDAV
|
||||
- You can leave the remaining settings as default for now.
|
||||
## Connecting to Sonarr/Radarr
|
||||
|
||||
### Qbittorent Configuration
|
||||

|
||||
|
||||
- Click on **Qbittorrent** in the tab
|
||||
- Set the **Download Folder** to where you want Decypharr to save downloaded files. These files will be symlinked to the mount folder you configured earlier.
|
||||
You can leave the remaining settings as default for now.
|
||||
|
||||
### Arrs Configuration
|
||||
|
||||
You can skip Arr configuration for now. Decypharr will auto-add them when you connect to Sonarr or Radarr later.
|
||||
|
||||
|
||||
#### Connecting to Sonarr/Radarr
|
||||
|
||||

|
||||
To connect Decypharr to your Sonarr or Radarr instance:
|
||||
|
||||
1. In Sonarr/Radarr, go to **Settings → Download Client → Add Client → qBittorrent**
|
||||
@@ -38,38 +18,22 @@ To connect Decypharr to your Sonarr or Radarr instance:
|
||||
- **Host**: `localhost` (or the IP of your Decypharr server)
|
||||
- **Port**: `8282` (or your configured qBittorrent port)
|
||||
- **Username**: `http://sonarr:8989` (your Arr host with http/https)
|
||||
- **Password**: `sonarr_token` (your Arr API token, you can get this from Sonarr/Radarr settings)
|
||||
- **Password**: `sonarr_token` (your Arr API token)
|
||||
- **Category**: e.g., `sonarr`, `radarr` (match what you configured in Decypharr)
|
||||
- **Use SSL**: `No`
|
||||
- **Sequential Download**: `No` or `Yes` (if you want to download torrents locally instead of symlink)
|
||||
3. Click **Test** to verify the connection
|
||||
4. Click **Save** to add the download client
|
||||
|
||||

|
||||
|
||||
### Rclone Configuration
|
||||
## Using the UI
|
||||
|
||||

|
||||
The Decypharr UI provides a familiar qBittorrent-like interface with additional features for Debrid services:
|
||||
|
||||
If you want Decypharr to automatically mount WebDAV folders using Rclone, you need to set up Rclone first:
|
||||
- Add new torrents
|
||||
- Monitor download status
|
||||
- Access WebDAV functionality
|
||||
- Edit your configuration
|
||||
|
||||
If you're using Docker, the rclone binary is already included in the container. If you're running Decypharr directly, make sure Rclone is installed on your system.
|
||||
|
||||
Enable **Mount**
|
||||
- **Global Mount Path**: Set the path where you want to mount the WebDAV folders (e.g., `/mnt/remote`). Decypharr will create subfolders for each Debrid service. For example, if you set `/mnt/remote`, it will create `/mnt/remote/realdebrid`, `/mnt/remote/torbox`, etc. This should be the grandparent of your mount folder set in the Debrid configuration.
|
||||
- **User ID**: Set the user ID for Rclone mounts (default is gotten from the environment variable `PUID`).
|
||||
- **Group ID**: Set the group ID for Rclone mounts (default is gotten from the environment variable `PGID`).
|
||||
- **Buffer Size**: Set the buffer size for Rclone mounts.
|
||||
|
||||
You should set other options based on your use case. If you don't know what you're doing, leave it as defaults. Checkout the [Rclone documentation](https://rclone.org/commands/rclone_mount/) for more details.
|
||||
|
||||
### Repair Configuration
|
||||
|
||||

|
||||
|
||||
Repair is an optional feature that allows you to fix missing files, symlinks, and other issues in your media library.
|
||||
- Click on **Repair** in the tab
|
||||
- Enable **Scheduled Repair** if you want Decypharr to automatically check for missing files at your specified interval.
|
||||
- Set the **Repair Interval** to how often you want Decypharr to check for missing files (e.g 1h, 6h, 12h, 24h, you can also use cron syntax like `0 0 * * *` for daily checks).
|
||||
- Enable **WebDav**(You shoukd enable this, if you enabled WebDav in Debrid configuration)
|
||||
- **Auto Process**: Enable this if you want Decypharr to automatically process repair jobs when they are done. This could delete the original files, symlinks, be wary!!!
|
||||
- **Worker Threads**: Set the number of worker threads for processing repair jobs. More threads can speed up the process but may consume more resources.
|
||||
Access the UI at `http://localhost:8282` or your configured host/port.
|
||||
@@ -6,9 +6,6 @@ repo_name: sirrobot01/decypharr
|
||||
edit_uri: blob/main/docs
|
||||
|
||||
|
||||
extra_css:
|
||||
- styles/styles.css
|
||||
|
||||
theme:
|
||||
name: material
|
||||
logo: images/logo.png
|
||||
@@ -62,17 +59,22 @@ nav:
|
||||
- Home: index.md
|
||||
- Installation: installation.md
|
||||
- Usage: usage.md
|
||||
- API Documentation: api.md
|
||||
- Configuration:
|
||||
- Overview: configuration/index.md
|
||||
- General: configuration/general.md
|
||||
- Debrid Providers: configuration/debrid.md
|
||||
- qBittorrent: configuration/qbittorrent.md
|
||||
- Arr Integration: configuration/arrs.md
|
||||
- Features:
|
||||
- Overview: features/index.md
|
||||
- Repair Worker: features/repair-worker.md
|
||||
- WebDAV: features/webdav.md
|
||||
- Guides:
|
||||
- Overview: guides/index.md
|
||||
- Manual Downloading: guides/downloading.md
|
||||
- Internal Mounting: guides/internal-mounting.md
|
||||
- Setting Up with Rclone: guides/rclone.md
|
||||
- Changelog: changelog.md
|
||||
|
||||
|
||||
plugins:
|
||||
- search
|
||||
- tags
|
||||
- swagger-ui-tag
|
||||
- tags
|
||||
@@ -1,3 +0,0 @@
|
||||
mkdocs==1.6.1
|
||||
mkdocs-material==9.6.16
|
||||
mkdocs-swagger-ui-tag==0.6.10
|
||||
23
go.mod
@@ -5,35 +5,44 @@ go 1.24.0
|
||||
toolchain go1.24.3
|
||||
|
||||
require (
|
||||
github.com/Tensai75/nzbparser v0.1.0
|
||||
github.com/anacrolix/torrent v1.55.0
|
||||
github.com/cavaliergopher/grab/v3 v3.0.1
|
||||
github.com/go-chi/chi/v5 v5.2.2
|
||||
github.com/chrisfarms/yenc v0.0.0-20140520125709-00bca2f8b3cb
|
||||
github.com/go-chi/chi/v5 v5.1.0
|
||||
github.com/go-co-op/gocron/v2 v2.16.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/sessions v1.4.0
|
||||
github.com/nwaples/rardecode/v2 v2.0.0-beta.4
|
||||
github.com/puzpuzpuz/xsync/v4 v4.1.0
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/rs/zerolog v1.33.0
|
||||
github.com/sourcegraph/conc v0.3.0
|
||||
github.com/stanNthe5/stringbuf v0.0.3
|
||||
go.uber.org/ratelimit v0.3.1
|
||||
golang.org/x/crypto v0.39.0
|
||||
golang.org/x/net v0.41.0
|
||||
golang.org/x/crypto v0.33.0
|
||||
golang.org/x/net v0.35.0
|
||||
golang.org/x/sync v0.15.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Tensai75/subjectparser v0.1.0 // indirect
|
||||
github.com/anacrolix/missinggo v1.3.0 // indirect
|
||||
github.com/anacrolix/missinggo/v2 v2.7.3 // indirect
|
||||
github.com/benbjohnson/clock v1.3.0 // indirect
|
||||
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/google/go-cmp v0.7.0 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/gorilla/securecookie v1.1.2 // indirect
|
||||
github.com/huandu/xstrings v1.3.2 // indirect
|
||||
github.com/jonboulle/clockwork v0.5.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.13.1 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/text v0.26.0 // indirect
|
||||
)
|
||||
|
||||
43
go.sum
@@ -8,6 +8,10 @@ github.com/RoaringBitmap/roaring v0.4.17/go.mod h1:D3qVegWTmfCaX4Bl5CrBE9hfrSrrX
|
||||
github.com/RoaringBitmap/roaring v0.4.23/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06Mq5mKs52e1TwOo=
|
||||
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
|
||||
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
|
||||
github.com/Tensai75/nzbparser v0.1.0 h1:6RppAuWFahqu/kKjWO5Br0xuEYcxGz+XBTxYc+qvPo4=
|
||||
github.com/Tensai75/nzbparser v0.1.0/go.mod h1:IUIIaeGaYp2dLAAF29BWYeKTfI4COvXaeQAzQiTOfMY=
|
||||
github.com/Tensai75/subjectparser v0.1.0 h1:6fEWnRov8lDHxJS2EWqY6VonwYfrIRN+k8h8H7fFwHA=
|
||||
github.com/Tensai75/subjectparser v0.1.0/go.mod h1:PNBFBnkOGbVDfX+56ZmC4GKSpqoRMCF1Y44xYd7NLGI=
|
||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
@@ -49,6 +53,8 @@ github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8/go.mod h1:spo1JLcs67
|
||||
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
|
||||
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/chrisfarms/yenc v0.0.0-20140520125709-00bca2f8b3cb h1:BK9pqCayfiXrcRypTPxDsunA6hPJtOyOTJYY2DJ429g=
|
||||
github.com/chrisfarms/yenc v0.0.0-20140520125709-00bca2f8b3cb/go.mod h1:V4bkS2felTTOSIsYx9JivzrbdBOuksi02ZkzfbHUVAk=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -70,8 +76,8 @@ github.com/glycerine/go-unsnap-stream v0.0.0-20190901134440-81cf024a9e0a/go.mod
|
||||
github.com/glycerine/goconvey v0.0.0-20180728074245-46e3a41ad493/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
|
||||
github.com/glycerine/goconvey v0.0.0-20190315024820-982ee783a72e/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
|
||||
github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
|
||||
github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
|
||||
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
||||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
||||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/go-co-op/gocron/v2 v2.16.1 h1:ux/5zxVRveCaCuTtNI3DiOk581KC1KpJbpJFYUEVYwo=
|
||||
github.com/go-co-op/gocron/v2 v2.16.1/go.mod h1:opexeOFy5BplhsKdA7bzY9zeYih8I8/WNJ4arTIFPVc=
|
||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
@@ -102,8 +108,8 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
@@ -143,9 +149,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
@@ -158,6 +163,8 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
|
||||
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
|
||||
github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/nwaples/rardecode/v2 v2.0.0-beta.4 h1:sdiJxQdPjECn2lh9nLFFhgLCf+0ulDU5rODbtERTlUY=
|
||||
github.com/nwaples/rardecode/v2 v2.0.0-beta.4/go.mod h1:yntwv/HfMc/Hbvtq9I19D1n58te3h6KsqCf3GxyfBGY=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
@@ -186,11 +193,13 @@ github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R
|
||||
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
||||
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
|
||||
github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
|
||||
github.com/puzpuzpuz/xsync/v4 v4.1.0 h1:x9eHRl4QhZFIPJ17yl4KKW9xLyVWbb3/Yq4SXpjF71U=
|
||||
github.com/puzpuzpuz/xsync/v4 v4.1.0/go.mod h1:VJDmTCJMBt8igNxnkQd86r+8KUeN1quSfNKu5bLYFQo=
|
||||
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
|
||||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
@@ -201,6 +210,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
|
||||
github.com/smartystreets/assertions v0.0.0-20190215210624-980c5ac6f3ac/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
|
||||
github.com/smartystreets/goconvey v0.0.0-20190306220146-200a235640ff/go.mod h1:KSQcGKpxUMHk3nbYzs/tIBAM2iDooCn0BmttHOJEbLs=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/stanNthe5/stringbuf v0.0.3 h1:3ChRipDckEY6FykaQ1Dowy3B+ZQa72EDBCasvT5+D1w=
|
||||
github.com/stanNthe5/stringbuf v0.0.3/go.mod h1:hii5Vr+mucoWkNJlIYQVp8YvuPtq45fFnJEAhcPf2cQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
@@ -223,12 +234,14 @@ go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
go.uber.org/ratelimit v0.3.1 h1:K4qVE+byfv/B3tC+4nYWP7v/6SimcO7HzHekoMNBma0=
|
||||
go.uber.org/ratelimit v0.3.1/go.mod h1:6euWsTB6U/Nb3X++xEUXA8ciPJvr19Q/0h1+oDcJhRk=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
|
||||
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
@@ -244,8 +257,8 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
|
||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
||||
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -269,10 +282,12 @@ golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
|
||||
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
|
||||
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
|
||||
@@ -2,8 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -32,7 +30,6 @@ type Debrid struct {
|
||||
APIKey string `json:"api_key,omitempty"`
|
||||
DownloadAPIKeys []string `json:"download_api_keys,omitempty"`
|
||||
Folder string `json:"folder,omitempty"`
|
||||
RcloneMountPath string `json:"rclone_mount_path,omitempty"` // Custom rclone mount path for this debrid service
|
||||
DownloadUncached bool `json:"download_uncached,omitempty"`
|
||||
CheckCached bool `json:"check_cached,omitempty"`
|
||||
RateLimit string `json:"rate_limit,omitempty"` // 200/minute or 10/second
|
||||
@@ -42,7 +39,6 @@ type Debrid struct {
|
||||
UnpackRar bool `json:"unpack_rar,omitempty"`
|
||||
AddSamples bool `json:"add_samples,omitempty"`
|
||||
MinimumFreeSlot int `json:"minimum_free_slot,omitempty"` // Minimum active pots to use this debrid
|
||||
Limit int `json:"limit,omitempty"` // Maximum number of total torrents
|
||||
|
||||
UseWebDav bool `json:"use_webdav,omitempty"`
|
||||
WebDav
|
||||
@@ -51,7 +47,6 @@ type Debrid struct {
|
||||
type QBitTorrent struct {
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Port string `json:"port,omitempty"` // deprecated
|
||||
DownloadFolder string `json:"download_folder,omitempty"`
|
||||
Categories []string `json:"categories,omitempty"`
|
||||
RefreshInterval int `json:"refresh_interval,omitempty"`
|
||||
@@ -84,42 +79,33 @@ type Repair struct {
|
||||
type Auth struct {
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
APIToken string `json:"api_token,omitempty"`
|
||||
}
|
||||
|
||||
type Rclone struct {
|
||||
// Global mount folder where all providers will be mounted as subfolders
|
||||
Enabled bool `json:"enabled,omitempty"`
|
||||
MountPath string `json:"mount_path,omitempty"`
|
||||
type SABnzbd struct {
|
||||
DownloadFolder string `json:"download_folder,omitempty"`
|
||||
RefreshInterval int `json:"refresh_interval,omitempty"`
|
||||
Categories []string `json:"categories,omitempty"`
|
||||
}
|
||||
|
||||
// Cache settings
|
||||
CacheDir string `json:"cache_dir,omitempty"`
|
||||
type Usenet struct {
|
||||
Providers []UsenetProvider `json:"providers,omitempty"` // List of usenet providers
|
||||
MountFolder string `json:"mount_folder,omitempty"` // Folder where usenet downloads are mounted
|
||||
SkipPreCache bool `json:"skip_pre_cache,omitempty"`
|
||||
Chunks int `json:"chunks,omitempty"` // Number of chunks to pre-cache
|
||||
RcUrl string `json:"rc_url,omitempty"` // Rclone RC URL for the webdav
|
||||
RcUser string `json:"rc_user,omitempty"` // Rclone RC username
|
||||
RcPass string `json:"rc_pass,omitempty"` // Rclone RC password
|
||||
}
|
||||
|
||||
// VFS settings
|
||||
VfsCacheMode string `json:"vfs_cache_mode,omitempty"` // off, minimal, writes, full
|
||||
VfsCacheMaxAge string `json:"vfs_cache_max_age,omitempty"` // Maximum age of objects in the cache (default 1h)
|
||||
VfsCacheMaxSize string `json:"vfs_cache_max_size,omitempty"` // Maximum size of the cache (default off)
|
||||
VfsCachePollInterval string `json:"vfs_cache_poll_interval,omitempty"` // How often to poll for changes (default 1m)
|
||||
VfsReadChunkSize string `json:"vfs_read_chunk_size,omitempty"` // Read chunk size (default 128M)
|
||||
VfsReadChunkSizeLimit string `json:"vfs_read_chunk_size_limit,omitempty"` // Max chunk size (default off)
|
||||
VfsReadAhead string `json:"vfs_read_ahead,omitempty"` // read ahead size
|
||||
VfsPollInterval string `json:"vfs_poll_interval,omitempty"` // How often to rclone cleans the cache (default 1m)
|
||||
BufferSize string `json:"buffer_size,omitempty"` // Buffer size for reading files (default 16M)
|
||||
|
||||
// File system settings
|
||||
UID uint32 `json:"uid,omitempty"` // User ID for mounted files
|
||||
GID uint32 `json:"gid,omitempty"` // Group ID for mounted files
|
||||
Umask string `json:"umask,omitempty"`
|
||||
|
||||
// Timeout settings
|
||||
AttrTimeout string `json:"attr_timeout,omitempty"` // Attribute cache timeout (default 1s)
|
||||
DirCacheTime string `json:"dir_cache_time,omitempty"` // Directory cache time (default 5m)
|
||||
|
||||
// Performance settings
|
||||
NoModTime bool `json:"no_modtime,omitempty"` // Don't read/write modification time
|
||||
NoChecksum bool `json:"no_checksum,omitempty"` // Don't checksum files on upload
|
||||
|
||||
LogLevel string `json:"log_level,omitempty"`
|
||||
type UsenetProvider struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Host string `json:"host,omitempty"` // Host of the usenet server
|
||||
Port int `json:"port,omitempty"` // Port of the usenet server
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Connections int `json:"connections,omitempty"` // Number of connections to use
|
||||
SSL bool `json:"ssl,omitempty"` // Use SSL for the connection
|
||||
UseTLS bool `json:"use_tls,omitempty"` // Use TLS for the connection
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -128,21 +114,22 @@ type Config struct {
|
||||
URLBase string `json:"url_base,omitempty"`
|
||||
Port string `json:"port,omitempty"`
|
||||
|
||||
LogLevel string `json:"log_level,omitempty"`
|
||||
Debrids []Debrid `json:"debrids,omitempty"`
|
||||
QBitTorrent QBitTorrent `json:"qbittorrent,omitempty"`
|
||||
Arrs []Arr `json:"arrs,omitempty"`
|
||||
Repair Repair `json:"repair,omitempty"`
|
||||
WebDav WebDav `json:"webdav,omitempty"`
|
||||
Rclone Rclone `json:"rclone,omitempty"`
|
||||
AllowedExt []string `json:"allowed_file_types,omitempty"`
|
||||
MinFileSize string `json:"min_file_size,omitempty"` // Minimum file size to download, 10MB, 1GB, etc
|
||||
MaxFileSize string `json:"max_file_size,omitempty"` // Maximum file size to download (0 means no limit)
|
||||
Path string `json:"-"` // Path to save the config file
|
||||
UseAuth bool `json:"use_auth,omitempty"`
|
||||
Auth *Auth `json:"-"`
|
||||
DiscordWebhook string `json:"discord_webhook_url,omitempty"`
|
||||
RemoveStalledAfter string `json:"remove_stalled_after,omitzero"`
|
||||
LogLevel string `json:"log_level,omitempty"`
|
||||
Debrids []Debrid `json:"debrids,omitempty"`
|
||||
QBitTorrent *QBitTorrent `json:"qbittorrent,omitempty"`
|
||||
SABnzbd *SABnzbd `json:"sabnzbd,omitempty"`
|
||||
Usenet *Usenet `json:"usenet,omitempty"` // Usenet configuration
|
||||
Arrs []Arr `json:"arrs,omitempty"`
|
||||
Repair Repair `json:"repair,omitempty"`
|
||||
WebDav WebDav `json:"webdav,omitempty"`
|
||||
AllowedExt []string `json:"allowed_file_types,omitempty"`
|
||||
MinFileSize string `json:"min_file_size,omitempty"` // Minimum file size to download, 10MB, 1GB, etc
|
||||
MaxFileSize string `json:"max_file_size,omitempty"` // Maximum file size to download (0 means no limit)
|
||||
Path string `json:"-"` // Path to save the config file
|
||||
UseAuth bool `json:"use_auth,omitempty"`
|
||||
Auth *Auth `json:"-"`
|
||||
DiscordWebhook string `json:"discord_webhook_url,omitempty"`
|
||||
RemoveStalledAfter string `json:"remove_stalled_after,omitzero"`
|
||||
}
|
||||
|
||||
func (c *Config) JsonFile() string {
|
||||
@@ -156,6 +143,10 @@ func (c *Config) TorrentsFile() string {
|
||||
return filepath.Join(c.Path, "torrents.json")
|
||||
}
|
||||
|
||||
func (c *Config) NZBsPath() string {
|
||||
return filepath.Join(c.Path, "cache/nzbs")
|
||||
}
|
||||
|
||||
func (c *Config) loadConfig() error {
|
||||
// Load the config file
|
||||
if configPath == "" {
|
||||
@@ -183,9 +174,6 @@ func (c *Config) loadConfig() error {
|
||||
}
|
||||
|
||||
func validateDebrids(debrids []Debrid) error {
|
||||
if len(debrids) == 0 {
|
||||
return errors.New("no debrids configured")
|
||||
}
|
||||
|
||||
for _, debrid := range debrids {
|
||||
// Basic field validation
|
||||
@@ -200,17 +188,51 @@ func validateDebrids(debrids []Debrid) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateQbitTorrent(config *QBitTorrent) error {
|
||||
if config.DownloadFolder == "" {
|
||||
return errors.New("qbittorent download folder is required")
|
||||
func validateUsenet(usenet *Usenet) error {
|
||||
if usenet == nil {
|
||||
return nil // No usenet configuration provided
|
||||
}
|
||||
if _, err := os.Stat(config.DownloadFolder); os.IsNotExist(err) {
|
||||
return fmt.Errorf("qbittorent download folder(%s) does not exist", config.DownloadFolder)
|
||||
for _, usenet := range usenet.Providers {
|
||||
// Basic field validation
|
||||
if usenet.Host == "" {
|
||||
return errors.New("usenet host is required")
|
||||
}
|
||||
if usenet.Username == "" {
|
||||
return errors.New("usenet username is required")
|
||||
}
|
||||
if usenet.Password == "" {
|
||||
return errors.New("usenet password is required")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateSabznbd(config *SABnzbd) error {
|
||||
if config == nil {
|
||||
return nil // No SABnzbd configuration provided
|
||||
}
|
||||
if config.DownloadFolder != "" {
|
||||
if _, err := os.Stat(config.DownloadFolder); os.IsNotExist(err) {
|
||||
return fmt.Errorf("sabnzbd download folder(%s) does not exist", config.DownloadFolder)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRepair(config *Repair) error {
|
||||
func validateQbitTorrent(config *QBitTorrent) error {
|
||||
if config == nil {
|
||||
return nil // No qBittorrent configuration provided
|
||||
}
|
||||
if config.DownloadFolder != "" {
|
||||
if _, err := os.Stat(config.DownloadFolder); os.IsNotExist(err) {
|
||||
return fmt.Errorf("qbittorent download folder(%s) does not exist", config.DownloadFolder)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRepair(config Repair) error {
|
||||
if !config.Enabled {
|
||||
return nil
|
||||
}
|
||||
@@ -222,31 +244,37 @@ func validateRepair(config *Repair) error {
|
||||
|
||||
func ValidateConfig(config *Config) error {
|
||||
// Run validations concurrently
|
||||
// Check if there's at least one debrid or usenet configured
|
||||
hasUsenet := false
|
||||
if config.Usenet != nil && len(config.Usenet.Providers) > 0 {
|
||||
hasUsenet = true
|
||||
}
|
||||
if len(config.Debrids) == 0 && !hasUsenet {
|
||||
return errors.New("at least one debrid or usenet provider must be configured")
|
||||
}
|
||||
|
||||
if err := validateDebrids(config.Debrids); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := validateQbitTorrent(&config.QBitTorrent); err != nil {
|
||||
if err := validateUsenet(config.Usenet); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := validateRepair(&config.Repair); err != nil {
|
||||
if err := validateSabznbd(config.SABnzbd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := validateQbitTorrent(config.QBitTorrent); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := validateRepair(config.Repair); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// generateAPIToken creates a new random API token
|
||||
func generateAPIToken() (string, error) {
|
||||
bytes := make([]byte, 32) // 256-bit token
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func SetConfigPath(path string) {
|
||||
configPath = path
|
||||
}
|
||||
@@ -349,19 +377,20 @@ func (c *Config) updateDebrid(d Debrid) Debrid {
|
||||
}
|
||||
d.DownloadAPIKeys = downloadKeys
|
||||
|
||||
if d.Workers == 0 {
|
||||
d.Workers = perDebrid
|
||||
}
|
||||
|
||||
if !d.UseWebDav {
|
||||
return d
|
||||
}
|
||||
|
||||
if d.TorrentsRefreshInterval == "" {
|
||||
d.TorrentsRefreshInterval = cmp.Or(c.WebDav.TorrentsRefreshInterval, "45s") // 45 seconds
|
||||
d.TorrentsRefreshInterval = cmp.Or(c.WebDav.TorrentsRefreshInterval, "15s") // 15 seconds
|
||||
}
|
||||
if d.WebDav.DownloadLinksRefreshInterval == "" {
|
||||
d.DownloadLinksRefreshInterval = cmp.Or(c.WebDav.DownloadLinksRefreshInterval, "40m") // 40 minutes
|
||||
}
|
||||
if d.Workers == 0 {
|
||||
d.Workers = perDebrid
|
||||
}
|
||||
if d.FolderNaming == "" {
|
||||
d.FolderNaming = cmp.Or(c.WebDav.FolderNaming, "original_no_ext")
|
||||
}
|
||||
@@ -388,17 +417,47 @@ func (c *Config) updateDebrid(d Debrid) Debrid {
|
||||
return d
|
||||
}
|
||||
|
||||
func (c *Config) updateUsenet(u UsenetProvider) UsenetProvider {
|
||||
if u.Name == "" {
|
||||
parts := strings.Split(u.Host, ".")
|
||||
if len(parts) >= 2 {
|
||||
u.Name = parts[len(parts)-2] // Gets "example" from "news.example.com"
|
||||
} else {
|
||||
u.Name = u.Host // Fallback to host if it doesn't look like a domain
|
||||
}
|
||||
}
|
||||
if u.Port == 0 {
|
||||
u.Port = 119 // Default port for usenet
|
||||
}
|
||||
if u.Connections == 0 {
|
||||
u.Connections = 30 // Default connections
|
||||
}
|
||||
if u.SSL && !u.UseTLS {
|
||||
u.UseTLS = true // Use TLS if SSL is enabled
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func (c *Config) setDefaults() {
|
||||
for i, debrid := range c.Debrids {
|
||||
c.Debrids[i] = c.updateDebrid(debrid)
|
||||
}
|
||||
|
||||
if c.SABnzbd != nil {
|
||||
c.SABnzbd.RefreshInterval = cmp.Or(c.SABnzbd.RefreshInterval, 10) // Default to 10 seconds
|
||||
}
|
||||
|
||||
if c.Usenet != nil {
|
||||
c.Usenet.Chunks = cmp.Or(c.Usenet.Chunks, 5)
|
||||
for i, provider := range c.Usenet.Providers {
|
||||
c.Usenet.Providers[i] = c.updateUsenet(provider)
|
||||
}
|
||||
}
|
||||
|
||||
if len(c.AllowedExt) == 0 {
|
||||
c.AllowedExt = getDefaultExtensions()
|
||||
}
|
||||
|
||||
c.Port = cmp.Or(c.Port, c.QBitTorrent.Port)
|
||||
|
||||
if c.URLBase == "" {
|
||||
c.URLBase = "/"
|
||||
}
|
||||
@@ -415,42 +474,8 @@ func (c *Config) setDefaults() {
|
||||
c.Repair.Strategy = RepairStrategyPerTorrent
|
||||
}
|
||||
|
||||
// Rclone defaults
|
||||
if c.Rclone.Enabled {
|
||||
c.Rclone.VfsCacheMode = cmp.Or(c.Rclone.VfsCacheMode, "off")
|
||||
if c.Rclone.UID == 0 {
|
||||
c.Rclone.UID = uint32(os.Getuid())
|
||||
}
|
||||
if c.Rclone.GID == 0 {
|
||||
if runtime.GOOS == "windows" {
|
||||
// On Windows, we use the current user's SID as GID
|
||||
c.Rclone.GID = uint32(os.Getuid()) // Windows does not have GID, using UID instead
|
||||
} else {
|
||||
c.Rclone.GID = uint32(os.Getgid())
|
||||
}
|
||||
}
|
||||
if c.Rclone.VfsCacheMode != "off" {
|
||||
c.Rclone.VfsCachePollInterval = cmp.Or(c.Rclone.VfsCachePollInterval, "1m") // Clean cache every minute
|
||||
}
|
||||
c.Rclone.DirCacheTime = cmp.Or(c.Rclone.DirCacheTime, "5m")
|
||||
c.Rclone.LogLevel = cmp.Or(c.Rclone.LogLevel, "INFO")
|
||||
}
|
||||
// Load the auth file
|
||||
c.Auth = c.GetAuth()
|
||||
|
||||
// Generate API token if auth is enabled and no token exists
|
||||
if c.UseAuth {
|
||||
if c.Auth == nil {
|
||||
c.Auth = &Auth{}
|
||||
}
|
||||
if c.Auth.APIToken == "" {
|
||||
if token, err := generateAPIToken(); err == nil {
|
||||
c.Auth.APIToken = token
|
||||
// Save the updated auth config
|
||||
_ = c.SaveAuth(c.Auth)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) Save() error {
|
||||
@@ -479,11 +504,6 @@ func (c *Config) createConfig(path string) error {
|
||||
c.Port = "8282"
|
||||
c.LogLevel = "info"
|
||||
c.UseAuth = true
|
||||
c.QBitTorrent = QBitTorrent{
|
||||
DownloadFolder: filepath.Join(path, "downloads"),
|
||||
Categories: []string{"sonarr", "radarr"},
|
||||
RefreshInterval: 15,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -492,7 +512,3 @@ func Reload() {
|
||||
instance = nil
|
||||
once = sync.Once{}
|
||||
}
|
||||
|
||||
func DefaultFreeSlot() int {
|
||||
return 10
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (c *Config) IsAllowedFile(filename string) bool {
|
||||
}
|
||||
|
||||
func getDefaultExtensions() []string {
|
||||
videoExts := strings.Split("webm,m4v,3gp,nsv,ty,strm,rm,rmvb,m3u,ifo,mov,qt,divx,xvid,bivx,nrg,pva,wmv,asf,asx,ogm,ogv,m2v,avi,bin,dat,dvr-ms,mpg,mpeg,mp4,avc,vp3,svq3,nuv,viv,dv,fli,flv,wpl,vob,mkv,mk3d,ts,wtv,m2ts", ",")
|
||||
videoExts := strings.Split("webm,m4v,3gp,nsv,ty,strm,rm,rmvb,m3u,ifo,mov,qt,divx,xvid,bivx,nrg,pva,wmv,asf,asx,ogm,ogv,m2v,avi,bin,dat,dvr-ms,mpg,mpeg,mp4,avc,vp3,svq3,nuv,viv,dv,fli,flv,wpl,img,iso,vob,mkv,mk3d,ts,wtv,m2ts", ",")
|
||||
musicExts := strings.Split("MP3,WAV,FLAC,OGG,WMA,AIFF,ALAC,M4A,APE,AC3,DTS,M4P,MID,MIDI,MKA,MP2,MPA,RA,VOC,WV,AMR", ",")
|
||||
|
||||
// Combine both slices
|
||||
|
||||
@@ -26,7 +26,7 @@ func GetLogPath() string {
|
||||
}
|
||||
}
|
||||
|
||||
return logsDir
|
||||
return filepath.Join(logsDir, "decypharr.log")
|
||||
}
|
||||
|
||||
func New(prefix string) zerolog.Logger {
|
||||
@@ -34,7 +34,7 @@ func New(prefix string) zerolog.Logger {
|
||||
level := config.Get().LogLevel
|
||||
|
||||
rotatingLogFile := &lumberjack.Logger{
|
||||
Filename: filepath.Join(GetLogPath(), "decypharr.log"),
|
||||
Filename: GetLogPath(),
|
||||
MaxSize: 10,
|
||||
MaxAge: 15,
|
||||
Compress: true,
|
||||
|
||||
178
internal/nntp/client.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package nntp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/puzpuzpuz/xsync/v4"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/internal/logger"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Client represents a failover NNTP client that manages multiple providers
|
||||
type Client struct {
|
||||
providers []config.UsenetProvider
|
||||
pools *xsync.Map[string, *Pool]
|
||||
logger zerolog.Logger
|
||||
closed atomic.Bool
|
||||
minimumMaxConns int // Minimum number of max connections across all pools
|
||||
}
|
||||
|
||||
func NewClient(providers []config.UsenetProvider) (*Client, error) {
|
||||
|
||||
client := &Client{
|
||||
providers: providers,
|
||||
logger: logger.New("nntp"),
|
||||
pools: xsync.NewMap[string, *Pool](),
|
||||
}
|
||||
if len(providers) == 0 {
|
||||
return nil, fmt.Errorf("no NNTP providers configured")
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (c *Client) InitPools() error {
|
||||
|
||||
var initErrors []error
|
||||
successfulPools := 0
|
||||
|
||||
for _, provider := range c.providers {
|
||||
serverPool, err := NewPool(provider, c.logger)
|
||||
if err != nil {
|
||||
c.logger.Error().
|
||||
Err(err).
|
||||
Str("server", provider.Host).
|
||||
Int("port", provider.Port).
|
||||
Msg("Failed to initialize server pool")
|
||||
initErrors = append(initErrors, err)
|
||||
continue
|
||||
}
|
||||
if c.minimumMaxConns == 0 {
|
||||
// Set minimumMaxConns to the max connections of the first successful pool
|
||||
c.minimumMaxConns = serverPool.ConnectionCount()
|
||||
} else {
|
||||
c.minimumMaxConns = min(c.minimumMaxConns, serverPool.ConnectionCount())
|
||||
}
|
||||
|
||||
c.pools.Store(provider.Name, serverPool)
|
||||
successfulPools++
|
||||
}
|
||||
|
||||
if successfulPools == 0 {
|
||||
return fmt.Errorf("failed to initialize any server pools: %v", initErrors)
|
||||
}
|
||||
|
||||
c.logger.Info().
|
||||
Int("providers", len(c.providers)).
|
||||
Msg("NNTP client created")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
if c.closed.Load() {
|
||||
c.logger.Warn().Msg("NNTP client already closed")
|
||||
return
|
||||
}
|
||||
|
||||
c.pools.Range(func(key string, value *Pool) bool {
|
||||
if value != nil {
|
||||
err := value.Close()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
c.closed.Store(true)
|
||||
c.logger.Info().Msg("NNTP client closed")
|
||||
}
|
||||
|
||||
func (c *Client) GetConnection(ctx context.Context) (*Connection, func(), error) {
|
||||
if c.closed.Load() {
|
||||
return nil, nil, fmt.Errorf("nntp client is closed")
|
||||
}
|
||||
|
||||
// Prevent workers from waiting too long for connections
|
||||
connCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
providerCount := len(c.providers)
|
||||
|
||||
for _, provider := range c.providers {
|
||||
pool, ok := c.pools.Load(provider.Name)
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("no pool found for provider %s", provider.Name)
|
||||
}
|
||||
|
||||
if !pool.IsFree() && providerCount > 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
conn, err := pool.Get(connCtx) // Use timeout context
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNoAvailableConnection) || errors.Is(err, context.DeadlineExceeded) {
|
||||
continue
|
||||
}
|
||||
return nil, nil, fmt.Errorf("error getting connection from provider %s: %w", provider.Name, err)
|
||||
}
|
||||
|
||||
if conn == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
return conn, func() { pool.Put(conn) }, nil
|
||||
}
|
||||
|
||||
return nil, nil, ErrNoAvailableConnection
|
||||
}
|
||||
|
||||
func (c *Client) DownloadHeader(ctx context.Context, messageID string) (*YencMetadata, error) {
|
||||
conn, cleanup, err := c.GetConnection(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
data, err := conn.GetBody(messageID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// yEnc decode
|
||||
part, err := DecodeYencHeaders(bytes.NewReader(data))
|
||||
if err != nil || part == nil {
|
||||
return nil, fmt.Errorf("failed to decode segment")
|
||||
}
|
||||
|
||||
// Return both the filename and decoded data
|
||||
return part, nil
|
||||
}
|
||||
|
||||
func (c *Client) MinimumMaxConns() int {
|
||||
return c.minimumMaxConns
|
||||
}
|
||||
|
||||
func (c *Client) TotalActiveConnections() int {
|
||||
total := 0
|
||||
c.pools.Range(func(key string, value *Pool) bool {
|
||||
if value != nil {
|
||||
total += value.ActiveConnections()
|
||||
}
|
||||
return true
|
||||
})
|
||||
return total
|
||||
}
|
||||
|
||||
func (c *Client) Pools() *xsync.Map[string, *Pool] {
|
||||
return c.pools
|
||||
}
|
||||
|
||||
func (c *Client) GetProviders() []config.UsenetProvider {
|
||||
return c.providers
|
||||
}
|
||||
394
internal/nntp/conns.go
Normal file
@@ -0,0 +1,394 @@
|
||||
package nntp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"github.com/chrisfarms/yenc"
|
||||
"github.com/rs/zerolog"
|
||||
"io"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Connection represents an NNTP connection
|
||||
type Connection struct {
|
||||
username, password, address string
|
||||
port int
|
||||
conn net.Conn
|
||||
text *textproto.Conn
|
||||
reader *bufio.Reader
|
||||
writer *bufio.Writer
|
||||
logger zerolog.Logger
|
||||
}
|
||||
|
||||
func (c *Connection) authenticate() error {
|
||||
// Send AUTHINFO USER command
|
||||
if err := c.sendCommand(fmt.Sprintf("AUTHINFO USER %s", c.username)); err != nil {
|
||||
return NewConnectionError(fmt.Errorf("failed to send username: %w", err))
|
||||
}
|
||||
|
||||
resp, err := c.readResponse()
|
||||
if err != nil {
|
||||
return NewConnectionError(fmt.Errorf("failed to read user response: %w", err))
|
||||
}
|
||||
|
||||
if resp.Code != 381 {
|
||||
return classifyNNTPError(resp.Code, fmt.Sprintf("unexpected response to AUTHINFO USER: %s", resp.Message))
|
||||
}
|
||||
|
||||
// Send AUTHINFO PASS command
|
||||
if err := c.sendCommand(fmt.Sprintf("AUTHINFO PASS %s", c.password)); err != nil {
|
||||
return NewConnectionError(fmt.Errorf("failed to send password: %w", err))
|
||||
}
|
||||
|
||||
resp, err = c.readResponse()
|
||||
if err != nil {
|
||||
return NewConnectionError(fmt.Errorf("failed to read password response: %w", err))
|
||||
}
|
||||
|
||||
if resp.Code != 281 {
|
||||
return classifyNNTPError(resp.Code, fmt.Sprintf("authentication failed: %s", resp.Message))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// startTLS initiates TLS encryption with proper error handling
|
||||
func (c *Connection) startTLS() error {
|
||||
if err := c.sendCommand("STARTTLS"); err != nil {
|
||||
return NewConnectionError(fmt.Errorf("failed to send STARTTLS: %w", err))
|
||||
}
|
||||
|
||||
resp, err := c.readResponse()
|
||||
if err != nil {
|
||||
return NewConnectionError(fmt.Errorf("failed to read STARTTLS response: %w", err))
|
||||
}
|
||||
|
||||
if resp.Code != 382 {
|
||||
return classifyNNTPError(resp.Code, fmt.Sprintf("STARTTLS not supported: %s", resp.Message))
|
||||
}
|
||||
|
||||
// Upgrade connection to TLS
|
||||
tlsConn := tls.Client(c.conn, &tls.Config{
|
||||
ServerName: c.address,
|
||||
InsecureSkipVerify: false,
|
||||
})
|
||||
|
||||
c.conn = tlsConn
|
||||
c.reader = bufio.NewReader(tlsConn)
|
||||
c.writer = bufio.NewWriter(tlsConn)
|
||||
c.text = textproto.NewConn(tlsConn)
|
||||
|
||||
c.logger.Debug().Msg("TLS encryption enabled")
|
||||
return nil
|
||||
}
|
||||
|
||||
// ping sends a simple command to test the connection
|
||||
func (c *Connection) ping() error {
|
||||
if err := c.sendCommand("DATE"); err != nil {
|
||||
return NewConnectionError(err)
|
||||
}
|
||||
_, err := c.readResponse()
|
||||
if err != nil {
|
||||
return NewConnectionError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendCommand sends a command to the NNTP server
|
||||
func (c *Connection) sendCommand(command string) error {
|
||||
_, err := fmt.Fprintf(c.writer, "%s\r\n", command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.writer.Flush()
|
||||
}
|
||||
|
||||
// readResponse reads a response from the NNTP server
|
||||
func (c *Connection) readResponse() (*Response, error) {
|
||||
line, err := c.text.ReadLine()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
parts := strings.SplitN(line, " ", 2)
|
||||
code, err := strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid response code: %s", parts[0])
|
||||
}
|
||||
|
||||
message := ""
|
||||
if len(parts) > 1 {
|
||||
message = parts[1]
|
||||
}
|
||||
|
||||
return &Response{
|
||||
Code: code,
|
||||
Message: message,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// readMultilineResponse reads a multiline response
|
||||
func (c *Connection) readMultilineResponse() (*Response, error) {
|
||||
resp, err := c.readResponse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if this is a multiline response
|
||||
if resp.Code < 200 || resp.Code >= 300 {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
lines, err := c.text.ReadDotLines()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp.Lines = lines
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// GetArticle retrieves an article by message ID with proper error classification
|
||||
func (c *Connection) GetArticle(messageID string) (*Article, error) {
|
||||
messageID = FormatMessageID(messageID)
|
||||
if err := c.sendCommand(fmt.Sprintf("ARTICLE %s", messageID)); err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to send ARTICLE command: %w", err))
|
||||
}
|
||||
|
||||
resp, err := c.readMultilineResponse()
|
||||
if err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to read article response: %w", err))
|
||||
}
|
||||
|
||||
if resp.Code != 220 {
|
||||
return nil, classifyNNTPError(resp.Code, resp.Message)
|
||||
}
|
||||
|
||||
return c.parseArticle(messageID, resp.Lines)
|
||||
}
|
||||
|
||||
// GetBody retrieves article body by message ID with proper error classification
|
||||
func (c *Connection) GetBody(messageID string) ([]byte, error) {
|
||||
messageID = FormatMessageID(messageID)
|
||||
if err := c.sendCommand(fmt.Sprintf("BODY %s", messageID)); err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to send BODY command: %w", err))
|
||||
}
|
||||
|
||||
// Read the initial response
|
||||
resp, err := c.readResponse()
|
||||
if err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to read body response: %w", err))
|
||||
}
|
||||
|
||||
if resp.Code != 222 {
|
||||
return nil, classifyNNTPError(resp.Code, resp.Message)
|
||||
}
|
||||
|
||||
// Read the raw body data directly using textproto to preserve exact formatting for yEnc
|
||||
lines, err := c.text.ReadDotLines()
|
||||
if err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to read body data: %w", err))
|
||||
}
|
||||
|
||||
// Join with \r\n to preserve original line endings and add final \r\n
|
||||
body := strings.Join(lines, "\r\n")
|
||||
if len(lines) > 0 {
|
||||
body += "\r\n"
|
||||
}
|
||||
|
||||
return []byte(body), nil
|
||||
}
|
||||
|
||||
// GetHead retrieves article headers by message ID
|
||||
func (c *Connection) GetHead(messageID string) ([]byte, error) {
|
||||
messageID = FormatMessageID(messageID)
|
||||
if err := c.sendCommand(fmt.Sprintf("HEAD %s", messageID)); err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to send HEAD command: %w", err))
|
||||
}
|
||||
|
||||
// Read the initial response
|
||||
resp, err := c.readResponse()
|
||||
if err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to read head response: %w", err))
|
||||
}
|
||||
|
||||
if resp.Code != 221 {
|
||||
return nil, classifyNNTPError(resp.Code, resp.Message)
|
||||
}
|
||||
|
||||
// Read the header data using textproto
|
||||
lines, err := c.text.ReadDotLines()
|
||||
if err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to read header data: %w", err))
|
||||
}
|
||||
|
||||
// Join with \r\n to preserve original line endings and add final \r\n
|
||||
headers := strings.Join(lines, "\r\n")
|
||||
if len(lines) > 0 {
|
||||
headers += "\r\n"
|
||||
}
|
||||
|
||||
return []byte(headers), nil
|
||||
}
|
||||
|
||||
// GetSegment retrieves a specific segment with proper error handling
|
||||
func (c *Connection) GetSegment(messageID string, segmentNumber int) (*Segment, error) {
|
||||
messageID = FormatMessageID(messageID)
|
||||
body, err := c.GetBody(messageID)
|
||||
if err != nil {
|
||||
return nil, err // GetBody already returns classified errors
|
||||
}
|
||||
|
||||
return &Segment{
|
||||
MessageID: messageID,
|
||||
Number: segmentNumber,
|
||||
Bytes: int64(len(body)),
|
||||
Data: body,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Stat retrieves article statistics by message ID with proper error classification
|
||||
func (c *Connection) Stat(messageID string) (articleNumber int, echoedID string, err error) {
|
||||
messageID = FormatMessageID(messageID)
|
||||
|
||||
if err = c.sendCommand(fmt.Sprintf("STAT %s", messageID)); err != nil {
|
||||
return 0, "", NewConnectionError(fmt.Errorf("failed to send STAT: %w", err))
|
||||
}
|
||||
|
||||
resp, err := c.readResponse()
|
||||
if err != nil {
|
||||
return 0, "", NewConnectionError(fmt.Errorf("failed to read STAT response: %w", err))
|
||||
}
|
||||
|
||||
if resp.Code != 223 {
|
||||
return 0, "", classifyNNTPError(resp.Code, resp.Message)
|
||||
}
|
||||
|
||||
fields := strings.Fields(resp.Message)
|
||||
if len(fields) < 2 {
|
||||
return 0, "", NewProtocolError(resp.Code, fmt.Sprintf("unexpected STAT response format: %q", resp.Message))
|
||||
}
|
||||
|
||||
if articleNumber, err = strconv.Atoi(fields[0]); err != nil {
|
||||
return 0, "", NewProtocolError(resp.Code, fmt.Sprintf("invalid article number %q: %v", fields[0], err))
|
||||
}
|
||||
echoedID = fields[1]
|
||||
|
||||
return articleNumber, echoedID, nil
|
||||
}
|
||||
|
||||
// SelectGroup selects a newsgroup and returns group information
|
||||
func (c *Connection) SelectGroup(groupName string) (*GroupInfo, error) {
|
||||
if err := c.sendCommand(fmt.Sprintf("GROUP %s", groupName)); err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to send GROUP command: %w", err))
|
||||
}
|
||||
|
||||
resp, err := c.readResponse()
|
||||
if err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to read GROUP response: %w", err))
|
||||
}
|
||||
|
||||
if resp.Code != 211 {
|
||||
return nil, classifyNNTPError(resp.Code, resp.Message)
|
||||
}
|
||||
|
||||
// Parse GROUP response: "211 number low high group-name"
|
||||
fields := strings.Fields(resp.Message)
|
||||
if len(fields) < 4 {
|
||||
return nil, NewProtocolError(resp.Code, fmt.Sprintf("unexpected GROUP response format: %q", resp.Message))
|
||||
}
|
||||
|
||||
groupInfo := &GroupInfo{
|
||||
Name: groupName,
|
||||
}
|
||||
|
||||
if count, err := strconv.Atoi(fields[0]); err == nil {
|
||||
groupInfo.Count = count
|
||||
}
|
||||
if low, err := strconv.Atoi(fields[1]); err == nil {
|
||||
groupInfo.Low = low
|
||||
}
|
||||
if high, err := strconv.Atoi(fields[2]); err == nil {
|
||||
groupInfo.High = high
|
||||
}
|
||||
|
||||
return groupInfo, nil
|
||||
}
|
||||
|
||||
// parseArticle parses article data from response lines
|
||||
func (c *Connection) parseArticle(messageID string, lines []string) (*Article, error) {
|
||||
article := &Article{
|
||||
MessageID: messageID,
|
||||
Groups: []string{},
|
||||
}
|
||||
|
||||
headerEnd := -1
|
||||
for i, line := range lines {
|
||||
if line == "" {
|
||||
headerEnd = i
|
||||
break
|
||||
}
|
||||
|
||||
// Parse headers
|
||||
if strings.HasPrefix(line, "Subject: ") {
|
||||
article.Subject = strings.TrimPrefix(line, "Subject: ")
|
||||
} else if strings.HasPrefix(line, "From: ") {
|
||||
article.From = strings.TrimPrefix(line, "From: ")
|
||||
} else if strings.HasPrefix(line, "Date: ") {
|
||||
article.Date = strings.TrimPrefix(line, "Date: ")
|
||||
} else if strings.HasPrefix(line, "Newsgroups: ") {
|
||||
groups := strings.TrimPrefix(line, "Newsgroups: ")
|
||||
article.Groups = strings.Split(groups, ",")
|
||||
for i := range article.Groups {
|
||||
article.Groups[i] = strings.TrimSpace(article.Groups[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Join body lines
|
||||
if headerEnd != -1 && headerEnd+1 < len(lines) {
|
||||
body := strings.Join(lines[headerEnd+1:], "\n")
|
||||
article.Body = []byte(body)
|
||||
article.Size = int64(len(article.Body))
|
||||
}
|
||||
|
||||
return article, nil
|
||||
}
|
||||
|
||||
// close closes the NNTP connection
|
||||
func (c *Connection) close() error {
|
||||
if c.conn != nil {
|
||||
return c.conn.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DecodeYenc(reader io.Reader) (*yenc.Part, error) {
|
||||
part, err := yenc.Decode(reader)
|
||||
if err != nil {
|
||||
return nil, NewYencDecodeError(fmt.Errorf("failed to create yenc decoder: %w", err))
|
||||
}
|
||||
return part, nil
|
||||
}
|
||||
|
||||
func IsValidMessageID(messageID string) bool {
|
||||
if len(messageID) < 3 {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(messageID, "@")
|
||||
}
|
||||
|
||||
// FormatMessageID ensures message ID has proper format
|
||||
func FormatMessageID(messageID string) string {
|
||||
messageID = strings.TrimSpace(messageID)
|
||||
if !strings.HasPrefix(messageID, "<") {
|
||||
messageID = "<" + messageID
|
||||
}
|
||||
if !strings.HasSuffix(messageID, ">") {
|
||||
messageID = messageID + ">"
|
||||
}
|
||||
return messageID
|
||||
}
|
||||
116
internal/nntp/decoder.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package nntp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// YencMetadata contains just the header information
|
||||
type YencMetadata struct {
|
||||
Name string // filename
|
||||
Size int64 // total file size
|
||||
Part int // part number
|
||||
Total int // total parts
|
||||
Begin int64 // part start byte
|
||||
End int64 // part end byte
|
||||
LineSize int // line length
|
||||
}
|
||||
|
||||
// DecodeYencHeaders extracts only yenc header metadata without decoding body
|
||||
func DecodeYencHeaders(reader io.Reader) (*YencMetadata, error) {
|
||||
buf := bufio.NewReader(reader)
|
||||
metadata := &YencMetadata{}
|
||||
|
||||
// Find and parse =ybegin header
|
||||
if err := parseYBeginHeader(buf, metadata); err != nil {
|
||||
return nil, NewYencDecodeError(fmt.Errorf("failed to parse ybegin header: %w", err))
|
||||
}
|
||||
|
||||
// Parse =ypart header if this is a multipart file
|
||||
if metadata.Part > 0 {
|
||||
if err := parseYPartHeader(buf, metadata); err != nil {
|
||||
return nil, NewYencDecodeError(fmt.Errorf("failed to parse ypart header: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func parseYBeginHeader(buf *bufio.Reader, metadata *YencMetadata) error {
|
||||
var s string
|
||||
var err error
|
||||
|
||||
// Find the =ybegin line
|
||||
for {
|
||||
s, err = buf.ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(s) >= 7 && s[:7] == "=ybegin" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the header line
|
||||
parts := strings.SplitN(s[7:], "name=", 2)
|
||||
if len(parts) > 1 {
|
||||
metadata.Name = strings.TrimSpace(parts[1])
|
||||
}
|
||||
|
||||
// Parse other parameters
|
||||
for _, header := range strings.Split(parts[0], " ") {
|
||||
kv := strings.SplitN(strings.TrimSpace(header), "=", 2)
|
||||
if len(kv) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch kv[0] {
|
||||
case "size":
|
||||
metadata.Size, _ = strconv.ParseInt(kv[1], 10, 64)
|
||||
case "line":
|
||||
metadata.LineSize, _ = strconv.Atoi(kv[1])
|
||||
case "part":
|
||||
metadata.Part, _ = strconv.Atoi(kv[1])
|
||||
case "total":
|
||||
metadata.Total, _ = strconv.Atoi(kv[1])
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseYPartHeader(buf *bufio.Reader, metadata *YencMetadata) error {
|
||||
var s string
|
||||
var err error
|
||||
|
||||
// Find the =ypart line
|
||||
for {
|
||||
s, err = buf.ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(s) >= 6 && s[:6] == "=ypart" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Parse part parameters
|
||||
for _, header := range strings.Split(s[6:], " ") {
|
||||
kv := strings.SplitN(strings.TrimSpace(header), "=", 2)
|
||||
if len(kv) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch kv[0] {
|
||||
case "begin":
|
||||
metadata.Begin, _ = strconv.ParseInt(kv[1], 10, 64)
|
||||
case "end":
|
||||
metadata.End, _ = strconv.ParseInt(kv[1], 10, 64)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
195
internal/nntp/errors.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package nntp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Error types for NNTP operations
|
||||
type ErrorType int
|
||||
|
||||
const (
|
||||
ErrorTypeUnknown ErrorType = iota
|
||||
ErrorTypeConnection
|
||||
ErrorTypeAuthentication
|
||||
ErrorTypeTimeout
|
||||
ErrorTypeArticleNotFound
|
||||
ErrorTypeGroupNotFound
|
||||
ErrorTypePermissionDenied
|
||||
ErrorTypeServerBusy
|
||||
ErrorTypeInvalidCommand
|
||||
ErrorTypeProtocol
|
||||
ErrorTypeYencDecode
|
||||
ErrorTypeNoAvailableConnection
|
||||
)
|
||||
|
||||
// Error represents an NNTP-specific error
|
||||
type Error struct {
|
||||
Type ErrorType
|
||||
Code int // NNTP response code
|
||||
Message string // Error message
|
||||
Err error // Underlying error
|
||||
}
|
||||
|
||||
// Predefined errors for common cases
|
||||
var (
|
||||
ErrArticleNotFound = &Error{Type: ErrorTypeArticleNotFound, Code: 430, Message: "article not found"}
|
||||
ErrGroupNotFound = &Error{Type: ErrorTypeGroupNotFound, Code: 411, Message: "group not found"}
|
||||
ErrPermissionDenied = &Error{Type: ErrorTypePermissionDenied, Code: 502, Message: "permission denied"}
|
||||
ErrAuthenticationFail = &Error{Type: ErrorTypeAuthentication, Code: 482, Message: "authentication failed"}
|
||||
ErrServerBusy = &Error{Type: ErrorTypeServerBusy, Code: 400, Message: "server busy"}
|
||||
ErrPoolNotFound = &Error{Type: ErrorTypeUnknown, Code: 0, Message: "NNTP pool not found", Err: nil}
|
||||
ErrNoAvailableConnection = &Error{Type: ErrorTypeNoAvailableConnection, Code: 0, Message: "no available connection in pool", Err: nil}
|
||||
)
|
||||
|
||||
func (e *Error) Error() string {
|
||||
if e.Err != nil {
|
||||
return fmt.Sprintf("NNTP %s (code %d): %s - %v", e.Type.String(), e.Code, e.Message, e.Err)
|
||||
}
|
||||
return fmt.Sprintf("NNTP %s (code %d): %s", e.Type.String(), e.Code, e.Message)
|
||||
}
|
||||
|
||||
func (e *Error) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
func (e *Error) Is(target error) bool {
|
||||
if t, ok := target.(*Error); ok {
|
||||
return e.Type == t.Type
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsRetryable returns true if the error might be resolved by retrying
|
||||
func (e *Error) IsRetryable() bool {
|
||||
switch e.Type {
|
||||
case ErrorTypeConnection, ErrorTypeTimeout, ErrorTypeServerBusy:
|
||||
return true
|
||||
case ErrorTypeArticleNotFound, ErrorTypeGroupNotFound, ErrorTypePermissionDenied, ErrorTypeAuthentication:
|
||||
return false
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// ShouldStopParsing returns true if this error should stop the entire parsing process
|
||||
func (e *Error) ShouldStopParsing() bool {
|
||||
switch e.Type {
|
||||
case ErrorTypeAuthentication, ErrorTypePermissionDenied:
|
||||
return true // Critical auth issues
|
||||
case ErrorTypeConnection:
|
||||
return false // Can continue with other connections
|
||||
case ErrorTypeArticleNotFound:
|
||||
return false // Can continue searching for other articles
|
||||
case ErrorTypeServerBusy:
|
||||
return false // Temporary issue
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (et ErrorType) String() string {
|
||||
switch et {
|
||||
case ErrorTypeConnection:
|
||||
return "CONNECTION"
|
||||
case ErrorTypeAuthentication:
|
||||
return "AUTHENTICATION"
|
||||
case ErrorTypeTimeout:
|
||||
return "TIMEOUT"
|
||||
case ErrorTypeArticleNotFound:
|
||||
return "ARTICLE_NOT_FOUND"
|
||||
case ErrorTypeGroupNotFound:
|
||||
return "GROUP_NOT_FOUND"
|
||||
case ErrorTypePermissionDenied:
|
||||
return "PERMISSION_DENIED"
|
||||
case ErrorTypeServerBusy:
|
||||
return "SERVER_BUSY"
|
||||
case ErrorTypeInvalidCommand:
|
||||
return "INVALID_COMMAND"
|
||||
case ErrorTypeProtocol:
|
||||
return "PROTOCOL"
|
||||
case ErrorTypeYencDecode:
|
||||
return "YENC_DECODE"
|
||||
default:
|
||||
return "UNKNOWN"
|
||||
}
|
||||
}
|
||||
|
||||
// Helper functions to create specific errors
|
||||
func NewConnectionError(err error) *Error {
|
||||
return &Error{
|
||||
Type: ErrorTypeConnection,
|
||||
Message: "connection failed",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTimeoutError(err error) *Error {
|
||||
return &Error{
|
||||
Type: ErrorTypeTimeout,
|
||||
Message: "operation timed out",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func NewProtocolError(code int, message string) *Error {
|
||||
return &Error{
|
||||
Type: ErrorTypeProtocol,
|
||||
Code: code,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewYencDecodeError(err error) *Error {
|
||||
return &Error{
|
||||
Type: ErrorTypeYencDecode,
|
||||
Message: "yEnc decode failed",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// classifyNNTPError classifies an NNTP response code into an error type
|
||||
func classifyNNTPError(code int, message string) *Error {
|
||||
switch {
|
||||
case code == 430 || code == 423:
|
||||
return &Error{Type: ErrorTypeArticleNotFound, Code: code, Message: message}
|
||||
case code == 411:
|
||||
return &Error{Type: ErrorTypeGroupNotFound, Code: code, Message: message}
|
||||
case code == 502 || code == 503:
|
||||
return &Error{Type: ErrorTypePermissionDenied, Code: code, Message: message}
|
||||
case code == 481 || code == 482:
|
||||
return &Error{Type: ErrorTypeAuthentication, Code: code, Message: message}
|
||||
case code == 400:
|
||||
return &Error{Type: ErrorTypeServerBusy, Code: code, Message: message}
|
||||
case code == 500 || code == 501:
|
||||
return &Error{Type: ErrorTypeInvalidCommand, Code: code, Message: message}
|
||||
case code >= 400:
|
||||
return &Error{Type: ErrorTypeProtocol, Code: code, Message: message}
|
||||
default:
|
||||
return &Error{Type: ErrorTypeUnknown, Code: code, Message: message}
|
||||
}
|
||||
}
|
||||
|
||||
func IsArticleNotFoundError(err error) bool {
|
||||
var nntpErr *Error
|
||||
if errors.As(err, &nntpErr) {
|
||||
return nntpErr.Type == ErrorTypeArticleNotFound
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsAuthenticationError(err error) bool {
|
||||
var nntpErr *Error
|
||||
if errors.As(err, &nntpErr) {
|
||||
return nntpErr.Type == ErrorTypeAuthentication
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsRetryableError(err error) bool {
|
||||
var nntpErr *Error
|
||||
if errors.As(err, &nntpErr) {
|
||||
return nntpErr.IsRetryable()
|
||||
}
|
||||
return false
|
||||
}
|
||||
299
internal/nntp/pool.go
Normal file
@@ -0,0 +1,299 @@
|
||||
package nntp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Pool manages a pool of NNTP connections
|
||||
type Pool struct {
|
||||
address, username, password string
|
||||
maxConns, port int
|
||||
ssl bool
|
||||
useTLS bool
|
||||
connections chan *Connection
|
||||
logger zerolog.Logger
|
||||
closed atomic.Bool
|
||||
totalConnections atomic.Int32
|
||||
activeConnections atomic.Int32
|
||||
}
|
||||
|
||||
// Segment represents a usenet segment
|
||||
type Segment struct {
|
||||
MessageID string
|
||||
Number int
|
||||
Bytes int64
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// Article represents a complete usenet article
|
||||
type Article struct {
|
||||
MessageID string
|
||||
Subject string
|
||||
From string
|
||||
Date string
|
||||
Groups []string
|
||||
Body []byte
|
||||
Size int64
|
||||
}
|
||||
|
||||
// Response represents an NNTP server response
|
||||
type Response struct {
|
||||
Code int
|
||||
Message string
|
||||
Lines []string
|
||||
}
|
||||
|
||||
// GroupInfo represents information about a newsgroup
|
||||
type GroupInfo struct {
|
||||
Name string
|
||||
Count int // Number of articles in the group
|
||||
Low int // Lowest article number
|
||||
High int // Highest article number
|
||||
}
|
||||
|
||||
// NewPool creates a new NNTP connection pool
|
||||
func NewPool(provider config.UsenetProvider, logger zerolog.Logger) (*Pool, error) {
|
||||
maxConns := provider.Connections
|
||||
if maxConns <= 0 {
|
||||
maxConns = 1
|
||||
}
|
||||
|
||||
pool := &Pool{
|
||||
address: provider.Host,
|
||||
username: provider.Username,
|
||||
password: provider.Password,
|
||||
port: provider.Port,
|
||||
maxConns: maxConns,
|
||||
ssl: provider.SSL,
|
||||
useTLS: provider.UseTLS,
|
||||
connections: make(chan *Connection, maxConns),
|
||||
logger: logger,
|
||||
}
|
||||
|
||||
return pool.initializeConnections()
|
||||
}
|
||||
|
||||
func (p *Pool) initializeConnections() (*Pool, error) {
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
var successfulConnections []*Connection
|
||||
var errs []error
|
||||
|
||||
// Create connections concurrently
|
||||
for i := 0; i < p.maxConns; i++ {
|
||||
wg.Add(1)
|
||||
go func(connIndex int) {
|
||||
defer wg.Done()
|
||||
|
||||
conn, err := p.createConnection()
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
successfulConnections = append(successfulConnections, conn)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
// Wait for all connection attempts to complete
|
||||
wg.Wait()
|
||||
|
||||
// Add successful connections to the pool
|
||||
for _, conn := range successfulConnections {
|
||||
p.connections <- conn
|
||||
}
|
||||
p.totalConnections.Store(int32(len(successfulConnections)))
|
||||
|
||||
if len(successfulConnections) == 0 {
|
||||
return nil, fmt.Errorf("failed to create any connections: %v", errs)
|
||||
}
|
||||
|
||||
// Log results
|
||||
p.logger.Info().
|
||||
Str("server", p.address).
|
||||
Int("port", p.port).
|
||||
Int("requested_connections", p.maxConns).
|
||||
Int("successful_connections", len(successfulConnections)).
|
||||
Int("failed_connections", len(errs)).
|
||||
Msg("NNTP connection pool created")
|
||||
|
||||
// If some connections failed, log a warning but continue
|
||||
if len(errs) > 0 {
|
||||
p.logger.Warn().
|
||||
Int("failed_count", len(errs)).
|
||||
Msg("Some connections failed during pool initialization")
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Get retrieves a connection from the pool
|
||||
func (p *Pool) Get(ctx context.Context) (*Connection, error) {
|
||||
if p.closed.Load() {
|
||||
return nil, NewConnectionError(fmt.Errorf("connection pool is closed"))
|
||||
}
|
||||
|
||||
select {
|
||||
case conn := <-p.connections:
|
||||
if conn == nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("received nil connection from pool"))
|
||||
}
|
||||
p.activeConnections.Add(1)
|
||||
|
||||
if err := conn.ping(); err != nil {
|
||||
p.activeConnections.Add(-1)
|
||||
err := conn.close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Create a new connection
|
||||
newConn, err := p.createConnection()
|
||||
if err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to create replacement connection: %w", err))
|
||||
}
|
||||
p.activeConnections.Add(1)
|
||||
return newConn, nil
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
case <-ctx.Done():
|
||||
return nil, NewTimeoutError(ctx.Err())
|
||||
}
|
||||
}
|
||||
|
||||
// Put returns a connection to the pool
|
||||
func (p *Pool) Put(conn *Connection) {
|
||||
if conn == nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer p.activeConnections.Add(-1)
|
||||
|
||||
if p.closed.Load() {
|
||||
conn.close()
|
||||
return
|
||||
}
|
||||
|
||||
// Try non-blocking first
|
||||
select {
|
||||
case p.connections <- conn:
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
// If pool is full, this usually means we have too many connections
|
||||
// Force return by making space (close oldest connection)
|
||||
select {
|
||||
case oldConn := <-p.connections:
|
||||
oldConn.close() // Close the old connection
|
||||
p.connections <- conn // Put the new one back
|
||||
case <-time.After(1 * time.Second):
|
||||
// Still can't return - close this connection
|
||||
conn.close()
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes all connections in the pool
|
||||
func (p *Pool) Close() error {
|
||||
|
||||
if p.closed.Load() {
|
||||
return nil
|
||||
}
|
||||
p.closed.Store(true)
|
||||
|
||||
close(p.connections)
|
||||
for conn := range p.connections {
|
||||
err := conn.close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
p.logger.Info().Msg("NNTP connection pool closed")
|
||||
return nil
|
||||
}
|
||||
|
||||
// createConnection creates a new NNTP connection with proper error handling
|
||||
func (p *Pool) createConnection() (*Connection, error) {
|
||||
addr := fmt.Sprintf("%s:%d", p.address, p.port)
|
||||
|
||||
var conn net.Conn
|
||||
var err error
|
||||
|
||||
if p.ssl {
|
||||
conn, err = tls.DialWithDialer(&net.Dialer{}, "tcp", addr, &tls.Config{
|
||||
InsecureSkipVerify: false,
|
||||
})
|
||||
} else {
|
||||
conn, err = net.Dial("tcp", addr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to connect to %s: %w", addr, err))
|
||||
}
|
||||
|
||||
reader := bufio.NewReaderSize(conn, 256*1024) // 256KB buffer for better performance
|
||||
writer := bufio.NewWriterSize(conn, 256*1024) // 256KB buffer for better performance
|
||||
text := textproto.NewConn(conn)
|
||||
|
||||
nntpConn := &Connection{
|
||||
username: p.username,
|
||||
password: p.password,
|
||||
address: p.address,
|
||||
port: p.port,
|
||||
conn: conn,
|
||||
text: text,
|
||||
reader: reader,
|
||||
writer: writer,
|
||||
logger: p.logger,
|
||||
}
|
||||
|
||||
// Read welcome message
|
||||
_, err = nntpConn.readResponse()
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, NewConnectionError(fmt.Errorf("failed to read welcome message: %w", err))
|
||||
}
|
||||
|
||||
// Authenticate if credentials are provided
|
||||
if p.username != "" && p.password != "" {
|
||||
if err := nntpConn.authenticate(); err != nil {
|
||||
conn.Close()
|
||||
return nil, err // authenticate() already returns NNTPError
|
||||
}
|
||||
}
|
||||
|
||||
// Enable TLS if requested (STARTTLS)
|
||||
if p.useTLS && !p.ssl {
|
||||
if err := nntpConn.startTLS(); err != nil {
|
||||
conn.Close()
|
||||
return nil, err // startTLS() already returns NNTPError
|
||||
}
|
||||
}
|
||||
return nntpConn, nil
|
||||
}
|
||||
|
||||
func (p *Pool) ConnectionCount() int {
|
||||
return int(p.totalConnections.Load())
|
||||
}
|
||||
|
||||
func (p *Pool) ActiveConnections() int {
|
||||
return int(p.activeConnections.Load())
|
||||
}
|
||||
|
||||
func (p *Pool) IsFree() bool {
|
||||
return p.ActiveConnections() < p.maxConns
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/logger"
|
||||
@@ -180,8 +179,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
|
||||
resp, err = c.doRequest(req)
|
||||
if err != nil {
|
||||
// Check if this is a network error that might be worth retrying
|
||||
if isRetryableError(err) && attempt < c.maxRetries {
|
||||
if attempt < c.maxRetries {
|
||||
// Apply backoff with jitter
|
||||
jitter := time.Duration(rand.Int63n(int64(backoff / 4)))
|
||||
sleepTime := backoff + jitter
|
||||
@@ -390,30 +388,3 @@ func Default() *Client {
|
||||
})
|
||||
return instance
|
||||
}
|
||||
|
||||
func isRetryableError(err error) bool {
|
||||
errString := err.Error()
|
||||
|
||||
// Connection reset and other network errors
|
||||
if strings.Contains(errString, "connection reset by peer") ||
|
||||
strings.Contains(errString, "read: connection reset") ||
|
||||
strings.Contains(errString, "connection refused") ||
|
||||
strings.Contains(errString, "network is unreachable") ||
|
||||
strings.Contains(errString, "connection timed out") ||
|
||||
strings.Contains(errString, "no such host") ||
|
||||
strings.Contains(errString, "i/o timeout") ||
|
||||
strings.Contains(errString, "unexpected EOF") ||
|
||||
strings.Contains(errString, "TLS handshake timeout") {
|
||||
return true
|
||||
}
|
||||
|
||||
// Check for net.Error type which can provide more information
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) {
|
||||
// Retry on timeout errors and temporary errors
|
||||
return netErr.Timeout()
|
||||
}
|
||||
|
||||
// Not a retryable error
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RemoveItem[S ~[]E, E comparable](s S, values ...E) S {
|
||||
result := make(S, 0, len(s))
|
||||
outer:
|
||||
@@ -22,3 +33,131 @@ func Contains(slice []string, value string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GenerateHash(data string) string {
|
||||
// Simple hash generation using a basic algorithm (for demonstration purposes)
|
||||
_hash := 0
|
||||
for _, char := range data {
|
||||
_hash = (_hash*31 + int(char)) % 1000003 // Simple hash function
|
||||
}
|
||||
return string(rune(_hash))
|
||||
}
|
||||
|
||||
func DownloadFile(url string) (string, []byte, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("failed to download file: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", nil, fmt.Errorf("failed to download file: status code %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
filename := getFilenameFromResponse(resp, url)
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
return filename, data, nil
|
||||
}
|
||||
|
||||
func getFilenameFromResponse(resp *http.Response, originalURL string) string {
|
||||
// 1. Try Content-Disposition header
|
||||
if cd := resp.Header.Get("Content-Disposition"); cd != "" {
|
||||
if _, params, err := mime.ParseMediaType(cd); err == nil {
|
||||
if filename := params["filename"]; filename != "" {
|
||||
return filename
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Try to decode URL-encoded filename from Content-Disposition
|
||||
if cd := resp.Header.Get("Content-Disposition"); cd != "" {
|
||||
if strings.Contains(cd, "filename*=") {
|
||||
// Handle RFC 5987 encoded filenames
|
||||
parts := strings.Split(cd, "filename*=")
|
||||
if len(parts) > 1 {
|
||||
encoded := strings.Trim(parts[1], `"`)
|
||||
if strings.HasPrefix(encoded, "UTF-8''") {
|
||||
if decoded, err := url.QueryUnescape(encoded[7:]); err == nil {
|
||||
return decoded
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Fall back to URL path
|
||||
if parsedURL, err := url.Parse(originalURL); err == nil {
|
||||
if filename := filepath.Base(parsedURL.Path); filename != "." && filename != "/" {
|
||||
// URL decode the filename
|
||||
if decoded, err := url.QueryUnescape(filename); err == nil {
|
||||
return decoded
|
||||
}
|
||||
return filename
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Default filename
|
||||
return "downloaded_file"
|
||||
}
|
||||
|
||||
func ValidateServiceURL(urlStr string) error {
|
||||
if urlStr == "" {
|
||||
return fmt.Errorf("URL cannot be empty")
|
||||
}
|
||||
|
||||
// Try parsing as full URL first
|
||||
u, err := url.Parse(urlStr)
|
||||
if err == nil && u.Scheme != "" && u.Host != "" {
|
||||
// It's a full URL, validate scheme
|
||||
if u.Scheme != "http" && u.Scheme != "https" {
|
||||
return fmt.Errorf("URL scheme must be http or https")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if it's a host:port format (no scheme)
|
||||
if strings.Contains(urlStr, ":") && !strings.Contains(urlStr, "://") {
|
||||
// Try parsing with http:// prefix
|
||||
testURL := "http://" + urlStr
|
||||
u, err := url.Parse(testURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid host:port format: %w", err)
|
||||
}
|
||||
|
||||
if u.Host == "" {
|
||||
return fmt.Errorf("host is required in host:port format")
|
||||
}
|
||||
|
||||
// Validate port number
|
||||
if u.Port() == "" {
|
||||
return fmt.Errorf("port is required in host:port format")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("invalid URL format: %s", urlStr)
|
||||
}
|
||||
|
||||
func ExtractFilenameFromURL(rawURL string) string {
|
||||
// Parse the URL
|
||||
parsedURL, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Get the base filename from path
|
||||
filename := path.Base(parsedURL.Path)
|
||||
|
||||
// Handle edge cases
|
||||
if filename == "/" || filename == "." || filename == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
return filename
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
videoMatch = "(?i)(\\.)(webm|m4v|3gp|nsv|ty|strm|rm|rmvb|m3u|ifo|mov|qt|divx|xvid|bivx|nrg|pva|wmv|asf|asx|ogm|ogv|m2v|avi|bin|dat|dvr-ms|mpg|mpeg|mp4|avc|vp3|svq3|nuv|viv|dv|fli|flv|wpl|vob|mkv|mk3d|ts|wtv|m2ts)$"
|
||||
videoMatch = "(?i)(\\.)(webm|m4v|3gp|nsv|ty|strm|rm|rmvb|m3u|ifo|mov|qt|divx|xvid|bivx|nrg|pva|wmv|asf|asx|ogm|ogv|m2v|avi|bin|dat|dvr-ms|mpg|mpeg|mp4|avc|vp3|svq3|nuv|viv|dv|fli|flv|wpl|img|iso|vob|mkv|mk3d|ts|wtv|m2ts)$"
|
||||
musicMatch = "(?i)(\\.)(mp2|mp3|m4a|m4b|m4p|ogg|oga|opus|wma|wav|wv|flac|ape|aif|aiff|aifc)$"
|
||||
sampleMatch = `(?i)(^|[\s/\\])(sample|trailer|thumb|special|extras?)s?[-/]|(\((sample|trailer|thumb|special|extras?)s?\))|(-\s*(sample|trailer|thumb|special|extras?)s?)`
|
||||
)
|
||||
@@ -57,3 +57,15 @@ func IsSampleFile(path string) bool {
|
||||
}
|
||||
return RegexMatch(sampleRegex, path)
|
||||
}
|
||||
|
||||
func IsParFile(path string) bool {
|
||||
ext := filepath.Ext(path)
|
||||
return strings.EqualFold(ext, ".par") || strings.EqualFold(ext, ".par2")
|
||||
}
|
||||
|
||||
func IsRarFile(path string) bool {
|
||||
ext := filepath.Ext(path)
|
||||
return strings.EqualFold(ext, ".rar") || strings.EqualFold(ext, ".r00") ||
|
||||
strings.EqualFold(ext, ".r01") || strings.EqualFold(ext, ".r02") ||
|
||||
strings.EqualFold(ext, ".r03") || strings.EqualFold(ext, ".r04")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/robfig/cron/v3"
|
||||
@@ -9,6 +10,25 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func ScheduleJob(ctx context.Context, interval string, loc *time.Location, jobFunc func()) (gocron.Scheduler, error) {
|
||||
if loc == nil {
|
||||
loc = time.Local
|
||||
}
|
||||
s, err := gocron.NewScheduler(gocron.WithLocation(loc))
|
||||
if err != nil {
|
||||
return s, fmt.Errorf("failed to create scheduler: %w", err)
|
||||
}
|
||||
jd, err := ConvertToJobDef(interval)
|
||||
if err != nil {
|
||||
return s, fmt.Errorf("failed to convert interval to job definition: %w", err)
|
||||
}
|
||||
// Schedule the job
|
||||
if _, err = s.NewJob(jd, gocron.NewTask(jobFunc), gocron.WithContext(ctx)); err != nil {
|
||||
return s, fmt.Errorf("failed to create job: %w", err)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// ConvertToJobDef converts a string interval to a gocron.JobDefinition.
|
||||
func ConvertToJobDef(interval string) (gocron.JobDefinition, error) {
|
||||
// Parse the interval string
|
||||
|
||||
@@ -190,7 +190,7 @@ func (s *Storage) GetAll() []*Arr {
|
||||
return arrs
|
||||
}
|
||||
|
||||
func (s *Storage) StartWorker(ctx context.Context) error {
|
||||
func (s *Storage) StartSchedule(ctx context.Context) error {
|
||||
|
||||
ticker := time.NewTicker(10 * time.Second)
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ func (a *Arr) CleanupQueue() error {
|
||||
messages := q.StatusMessages
|
||||
if len(messages) > 0 {
|
||||
for _, m := range messages {
|
||||
if strings.Contains(strings.Join(m.Messages, " "), "No files found are eligible") {
|
||||
if strings.Contains(strings.Join(m.Messages, " "), "No files found are eligible for import in") {
|
||||
isMessedUp = true
|
||||
break
|
||||
}
|
||||
|
||||
@@ -9,70 +9,50 @@ import (
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
"github.com/sirrobot01/decypharr/pkg/arr"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/providers/alldebrid"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/providers/debridlink"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/providers/debrid_link"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/providers/realdebrid"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/providers/torbox"
|
||||
debridStore "github.com/sirrobot01/decypharr/pkg/debrid/store"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/store"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/types"
|
||||
"github.com/sirrobot01/decypharr/pkg/rclone"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Debrid struct {
|
||||
cache *debridStore.Cache // Could be nil if not using WebDAV
|
||||
client types.Client // HTTP client for making requests to the debrid service
|
||||
cache *store.Cache // Could be nil if not using WebDAV
|
||||
client types.Client // HTTP client for making requests to the debrid service
|
||||
}
|
||||
|
||||
func (de *Debrid) Client() types.Client {
|
||||
return de.client
|
||||
}
|
||||
|
||||
func (de *Debrid) Cache() *debridStore.Cache {
|
||||
func (de *Debrid) Cache() *store.Cache {
|
||||
return de.cache
|
||||
}
|
||||
|
||||
func (de *Debrid) Reset() {
|
||||
if de.cache != nil {
|
||||
de.cache.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
debrids map[string]*Debrid
|
||||
mu sync.RWMutex
|
||||
lastUsed string
|
||||
}
|
||||
|
||||
func NewStorage(rcManager *rclone.Manager) *Storage {
|
||||
func NewStorage() *Storage {
|
||||
cfg := config.Get()
|
||||
|
||||
_logger := logger.Default()
|
||||
|
||||
debrids := make(map[string]*Debrid)
|
||||
|
||||
bindAddress := cfg.BindAddress
|
||||
if bindAddress == "" {
|
||||
bindAddress = "localhost"
|
||||
}
|
||||
webdavUrl := fmt.Sprintf("http://%s:%s%s/webdav", bindAddress, cfg.Port, cfg.URLBase)
|
||||
|
||||
for _, dc := range cfg.Debrids {
|
||||
client, err := createDebridClient(dc)
|
||||
if err != nil {
|
||||
_logger.Error().Err(err).Str("Debrid", dc.Name).Msg("failed to connect to debrid client")
|
||||
continue
|
||||
}
|
||||
var (
|
||||
cache *debridStore.Cache
|
||||
mounter *rclone.Mount
|
||||
)
|
||||
var cache *store.Cache
|
||||
_log := client.Logger()
|
||||
if dc.UseWebDav {
|
||||
if cfg.Rclone.Enabled && rcManager != nil {
|
||||
mounter = rclone.NewMount(dc.Name, dc.RcloneMountPath, webdavUrl, rcManager)
|
||||
}
|
||||
cache = debridStore.NewDebridCache(dc, client, mounter)
|
||||
cache = store.NewDebridCache(dc, client)
|
||||
_log.Info().Msg("Debrid Service started with WebDAV")
|
||||
} else {
|
||||
_log.Info().Msg("Debrid Service started")
|
||||
@@ -99,47 +79,6 @@ func (d *Storage) Debrid(name string) *Debrid {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Storage) StartWorker(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
// Start all debrid syncAccounts
|
||||
// Runs every 1m
|
||||
if err := d.syncAccounts(); err != nil {
|
||||
return err
|
||||
}
|
||||
ticker := time.NewTicker(5 * time.Minute)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
_ = d.syncAccounts()
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Storage) syncAccounts() error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
for name, debrid := range d.debrids {
|
||||
if debrid == nil || debrid.client == nil {
|
||||
continue
|
||||
}
|
||||
_log := debrid.client.Logger()
|
||||
if err := debrid.client.SyncAccounts(); err != nil {
|
||||
_log.Error().Err(err).Msgf("Failed to sync account for %s", name)
|
||||
continue
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Storage) Debrids() map[string]*Debrid {
|
||||
d.mu.RLock()
|
||||
defer d.mu.RUnlock()
|
||||
@@ -163,17 +102,8 @@ func (d *Storage) Client(name string) types.Client {
|
||||
|
||||
func (d *Storage) Reset() {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
// Reset all debrid clients and caches
|
||||
for _, debrid := range d.debrids {
|
||||
if debrid != nil {
|
||||
debrid.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
// Reinitialize the debrids map
|
||||
d.debrids = make(map[string]*Debrid)
|
||||
d.mu.Unlock()
|
||||
d.lastUsed = ""
|
||||
}
|
||||
|
||||
@@ -189,10 +119,10 @@ func (d *Storage) Clients() map[string]types.Client {
|
||||
return clientsCopy
|
||||
}
|
||||
|
||||
func (d *Storage) Caches() map[string]*debridStore.Cache {
|
||||
func (d *Storage) Caches() map[string]*store.Cache {
|
||||
d.mu.RLock()
|
||||
defer d.mu.RUnlock()
|
||||
cachesCopy := make(map[string]*debridStore.Cache)
|
||||
cachesCopy := make(map[string]*store.Cache)
|
||||
for name, debrid := range d.debrids {
|
||||
if debrid != nil && debrid.cache != nil {
|
||||
cachesCopy[name] = debrid.cache
|
||||
@@ -220,7 +150,7 @@ func createDebridClient(dc config.Debrid) (types.Client, error) {
|
||||
case "torbox":
|
||||
return torbox.New(dc)
|
||||
case "debridlink":
|
||||
return debridlink.New(dc)
|
||||
return debrid_link.New(dc)
|
||||
case "alldebrid":
|
||||
return alldebrid.New(dc)
|
||||
default:
|
||||
@@ -263,7 +193,7 @@ func Process(ctx context.Context, store *Storage, selectedDebrid string, magnet
|
||||
debridTorrent.DownloadUncached = false
|
||||
}
|
||||
|
||||
for _, db := range clients {
|
||||
for index, db := range clients {
|
||||
_logger := db.Logger()
|
||||
_logger.Info().
|
||||
Str("Debrid", db.Name()).
|
||||
@@ -284,7 +214,7 @@ func Process(ctx context.Context, store *Storage, selectedDebrid string, magnet
|
||||
}
|
||||
dbt.Arr = a
|
||||
_logger.Info().Str("id", dbt.Id).Msgf("Torrent: %s submitted to %s", dbt.Name, db.Name())
|
||||
store.lastUsed = db.Name()
|
||||
store.lastUsed = index
|
||||
|
||||
torrent, err := db.CheckStatus(dbt)
|
||||
if err != nil && torrent != nil && torrent.Id != "" {
|
||||
|
||||
@@ -25,7 +25,6 @@ type AllDebrid struct {
|
||||
autoExpiresLinksAfter time.Duration
|
||||
DownloadUncached bool
|
||||
client *request.Client
|
||||
Profile *types.Profile `json:"profile"`
|
||||
|
||||
MountPath string
|
||||
logger zerolog.Logger
|
||||
@@ -34,6 +33,10 @@ type AllDebrid struct {
|
||||
minimumFreeSlot int
|
||||
}
|
||||
|
||||
func (ad *AllDebrid) GetProfile() (*types.Profile, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func New(dc config.Debrid) (*AllDebrid, error) {
|
||||
rl := request.ParseRateLimit(dc.RateLimit)
|
||||
|
||||
@@ -446,58 +449,6 @@ func (ad *AllDebrid) GetAvailableSlots() (int, error) {
|
||||
return 0, fmt.Errorf("GetAvailableSlots not implemented for AllDebrid")
|
||||
}
|
||||
|
||||
func (ad *AllDebrid) GetProfile() (*types.Profile, error) {
|
||||
if ad.Profile != nil {
|
||||
return ad.Profile, nil
|
||||
}
|
||||
url := fmt.Sprintf("%s/user", ad.Host)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := ad.client.MakeRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var res UserProfileResponse
|
||||
err = json.Unmarshal(resp, &res)
|
||||
if err != nil {
|
||||
ad.logger.Error().Err(err).Msgf("Error unmarshalling user profile")
|
||||
return nil, err
|
||||
}
|
||||
if res.Status != "success" {
|
||||
message := "unknown error"
|
||||
if res.Error != nil {
|
||||
message = res.Error.Message
|
||||
}
|
||||
return nil, fmt.Errorf("error getting user profile: %s", message)
|
||||
}
|
||||
userData := res.Data.User
|
||||
expiration := time.Unix(userData.PremiumUntil, 0)
|
||||
profile := &types.Profile{
|
||||
Id: 1,
|
||||
Name: ad.name,
|
||||
Username: userData.Username,
|
||||
Email: userData.Email,
|
||||
Points: userData.FidelityPoints,
|
||||
Premium: userData.PremiumUntil,
|
||||
Expiration: expiration,
|
||||
}
|
||||
if userData.IsPremium {
|
||||
profile.Type = "premium"
|
||||
} else if userData.IsTrial {
|
||||
profile.Type = "trial"
|
||||
} else {
|
||||
profile.Type = "free"
|
||||
}
|
||||
ad.Profile = profile
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
func (ad *AllDebrid) Accounts() *types.Accounts {
|
||||
return ad.accounts
|
||||
}
|
||||
|
||||
func (ad *AllDebrid) SyncAccounts() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -112,22 +112,3 @@ func (m *Magnets) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
return fmt.Errorf("magnets: unsupported JSON format")
|
||||
}
|
||||
|
||||
type UserProfileResponse struct {
|
||||
Status string `json:"status"`
|
||||
Error *errorResponse `json:"error"`
|
||||
Data struct {
|
||||
User struct {
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
IsPremium bool `json:"isPremium"`
|
||||
IsSubscribed bool `json:"isSubscribed"`
|
||||
IsTrial bool `json:"isTrial"`
|
||||
PremiumUntil int64 `json:"premiumUntil"`
|
||||
Lang string `json:"lang"`
|
||||
FidelityPoints int `json:"fidelityPoints"`
|
||||
LimitedHostersQuotas map[string]int `json:"limitedHostersQuotas"`
|
||||
Notifications []string `json:"notifications"`
|
||||
} `json:"user"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package debridlink
|
||||
package debrid_link
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -30,8 +30,6 @@ type DebridLink struct {
|
||||
logger zerolog.Logger
|
||||
checkCached bool
|
||||
addSamples bool
|
||||
|
||||
Profile *types.Profile `json:"profile,omitempty"`
|
||||
}
|
||||
|
||||
func New(dc config.Debrid) (*DebridLink, error) {
|
||||
@@ -68,6 +66,10 @@ func New(dc config.Debrid) (*DebridLink, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (dl *DebridLink) GetProfile() (*types.Profile, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (dl *DebridLink) Name() string {
|
||||
return dl.name
|
||||
}
|
||||
@@ -474,55 +476,6 @@ func (dl *DebridLink) GetAvailableSlots() (int, error) {
|
||||
return 0, fmt.Errorf("GetAvailableSlots not implemented for DebridLink")
|
||||
}
|
||||
|
||||
func (dl *DebridLink) GetProfile() (*types.Profile, error) {
|
||||
if dl.Profile != nil {
|
||||
return dl.Profile, nil
|
||||
}
|
||||
url := fmt.Sprintf("%s/account/infos", dl.Host)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := dl.client.MakeRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var res UserInfo
|
||||
err = json.Unmarshal(resp, &res)
|
||||
if err != nil {
|
||||
dl.logger.Error().Err(err).Msgf("Error unmarshalling user info")
|
||||
return nil, err
|
||||
}
|
||||
if !res.Success || res.Value == nil {
|
||||
return nil, fmt.Errorf("error getting user info")
|
||||
}
|
||||
data := *res.Value
|
||||
expiration := time.Unix(data.PremiumLeft, 0)
|
||||
profile := &types.Profile{
|
||||
Id: 1,
|
||||
Username: data.Username,
|
||||
Name: dl.name,
|
||||
Email: data.Email,
|
||||
Points: data.Points,
|
||||
Premium: data.PremiumLeft,
|
||||
Expiration: expiration,
|
||||
}
|
||||
if expiration.IsZero() {
|
||||
profile.Expiration = time.Now().AddDate(1, 0, 0) // Default to 1 year if no expiration
|
||||
}
|
||||
if data.PremiumLeft > 0 {
|
||||
profile.Type = "premium"
|
||||
} else {
|
||||
profile.Type = "free"
|
||||
}
|
||||
dl.Profile = profile
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
func (dl *DebridLink) Accounts() *types.Accounts {
|
||||
return dl.accounts
|
||||
}
|
||||
|
||||
func (dl *DebridLink) SyncAccounts() error {
|
||||
return nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package debridlink
|
||||
package debrid_link
|
||||
|
||||
type APIResponse[T any] struct {
|
||||
Success bool `json:"success"`
|
||||
@@ -43,12 +43,3 @@ type _torrentInfo struct {
|
||||
type torrentInfo APIResponse[[]_torrentInfo]
|
||||
|
||||
type SubmitTorrentInfo APIResponse[_torrentInfo]
|
||||
|
||||
type UserInfo APIResponse[struct {
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
AccountType int `json:"accountType"`
|
||||
PremiumLeft int64 `json:"premiumLeft"`
|
||||
Points int `json:"pts"`
|
||||
Trafficshare int `json:"trafficshare"`
|
||||
}]
|
||||
@@ -46,7 +46,7 @@ type RealDebrid struct {
|
||||
addSamples bool
|
||||
Profile *types.Profile
|
||||
minimumFreeSlot int // Minimum number of active pots to maintain (used for cached stuffs, etc.)
|
||||
limit int
|
||||
|
||||
}
|
||||
|
||||
func New(dc config.Debrid) (*RealDebrid, error) {
|
||||
@@ -101,7 +101,6 @@ func New(dc config.Debrid) (*RealDebrid, error) {
|
||||
checkCached: dc.CheckCached,
|
||||
addSamples: dc.AddSamples,
|
||||
minimumFreeSlot: dc.MinimumFreeSlot,
|
||||
limit: dc.Limit,
|
||||
}
|
||||
|
||||
if _, err := r.GetProfile(); err != nil {
|
||||
@@ -161,23 +160,6 @@ func (r *RealDebrid) getSelectedFiles(t *types.Torrent, data torrentInfo) (map[s
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (r *RealDebrid) handleRarFallback(t *types.Torrent, data torrentInfo) (map[string]types.File, error) {
|
||||
files := make(map[string]types.File)
|
||||
file := types.File{
|
||||
TorrentId: t.Id,
|
||||
Id: "0",
|
||||
Name: t.Name + ".rar",
|
||||
Size: data.Bytes,
|
||||
IsRar: true,
|
||||
ByteRange: nil,
|
||||
Path: t.Name + ".rar",
|
||||
Link: data.Links[0],
|
||||
Generated: time.Now(),
|
||||
}
|
||||
files[file.Name] = file
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// handleRarArchive processes RAR archives with multiple files
|
||||
func (r *RealDebrid) handleRarArchive(t *types.Torrent, data torrentInfo, selectedFiles []types.File) (map[string]types.File, error) {
|
||||
// This will block if 2 RAR operations are already in progress
|
||||
@@ -189,8 +171,21 @@ func (r *RealDebrid) handleRarArchive(t *types.Torrent, data torrentInfo, select
|
||||
files := make(map[string]types.File)
|
||||
|
||||
if !r.UnpackRar {
|
||||
r.logger.Debug().Msgf("RAR file detected, but unpacking is disabled: %s. Falling back to single file representation.", t.Name)
|
||||
return r.handleRarFallback(t, data)
|
||||
r.logger.Debug().Msgf("RAR file detected, but unpacking is disabled: %s", t.Name)
|
||||
// Create a single file representing the RAR archive
|
||||
file := types.File{
|
||||
TorrentId: t.Id,
|
||||
Id: "0",
|
||||
Name: t.Name + ".rar",
|
||||
Size: 0,
|
||||
IsRar: true,
|
||||
ByteRange: nil,
|
||||
Path: t.Name + ".rar",
|
||||
Link: data.Links[0],
|
||||
Generated: time.Now(),
|
||||
}
|
||||
files[file.Name] = file
|
||||
return files, nil
|
||||
}
|
||||
|
||||
r.logger.Info().Msgf("RAR file detected, unpacking: %s", t.Name)
|
||||
@@ -198,23 +193,20 @@ func (r *RealDebrid) handleRarArchive(t *types.Torrent, data torrentInfo, select
|
||||
downloadLinkObj, err := r.GetDownloadLink(t, linkFile)
|
||||
|
||||
if err != nil {
|
||||
r.logger.Debug().Err(err).Msgf("Error getting download link for RAR file: %s. Falling back to single file representation.", t.Name)
|
||||
return r.handleRarFallback(t, data)
|
||||
return nil, fmt.Errorf("failed to get download link for RAR file: %w", err)
|
||||
}
|
||||
|
||||
dlLink := downloadLinkObj.DownloadLink
|
||||
reader, err := rar.NewReader(dlLink)
|
||||
|
||||
if err != nil {
|
||||
r.logger.Debug().Err(err).Msgf("Error creating RAR reader for %s. Falling back to single file representation.", t.Name)
|
||||
return r.handleRarFallback(t, data)
|
||||
return nil, fmt.Errorf("failed to create RAR reader: %w", err)
|
||||
}
|
||||
|
||||
rarFiles, err := reader.GetFiles()
|
||||
|
||||
if err != nil {
|
||||
r.logger.Debug().Err(err).Msgf("Error reading RAR files for %s. Falling back to single file representation.", t.Name)
|
||||
return r.handleRarFallback(t, data)
|
||||
return nil, fmt.Errorf("failed to read RAR files: %w", err)
|
||||
}
|
||||
|
||||
// Create lookup map for faster matching
|
||||
@@ -239,11 +231,7 @@ func (r *RealDebrid) handleRarArchive(t *types.Torrent, data torrentInfo, select
|
||||
r.logger.Warn().Msgf("RAR file %s not found in torrent files", rarFile.Name())
|
||||
}
|
||||
}
|
||||
if len(files) == 0 {
|
||||
r.logger.Warn().Msgf("No valid files found in RAR archive for torrent: %s", t.Name)
|
||||
return r.handleRarFallback(t, data)
|
||||
}
|
||||
r.logger.Info().Msgf("Unpacked RAR archive for torrent: %s with %d files", t.Name, len(files))
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
@@ -522,7 +510,7 @@ func (r *RealDebrid) CheckStatus(t *types.Torrent) (*types.Torrent, error) {
|
||||
if status == "waiting_files_selection" {
|
||||
t.Files = r.getTorrentFiles(t, data)
|
||||
if len(t.Files) == 0 {
|
||||
return t, fmt.Errorf("no valid files found")
|
||||
return t, fmt.Errorf("no video files found")
|
||||
}
|
||||
filesId := make([]string, 0)
|
||||
for _, f := range t.Files {
|
||||
@@ -711,7 +699,7 @@ func (r *RealDebrid) _getDownloadLink(file *types.File) (*types.DownloadLink, er
|
||||
|
||||
func (r *RealDebrid) GetDownloadLink(t *types.Torrent, file *types.File) (*types.DownloadLink, error) {
|
||||
|
||||
accounts := r.accounts.Active()
|
||||
accounts := r.accounts.All()
|
||||
|
||||
for _, account := range accounts {
|
||||
r.downloadClient.SetHeader("Authorization", fmt.Sprintf("Bearer %s", account.Token))
|
||||
@@ -806,10 +794,6 @@ func (r *RealDebrid) getTorrents(offset int, limit int) (int, []*types.Torrent,
|
||||
|
||||
func (r *RealDebrid) GetTorrents() ([]*types.Torrent, error) {
|
||||
limit := 5000
|
||||
if r.limit != 0 {
|
||||
limit = r.limit
|
||||
}
|
||||
hardLimit := r.limit
|
||||
|
||||
// Get first batch and total count
|
||||
allTorrents := make([]*types.Torrent, 0)
|
||||
@@ -828,10 +812,6 @@ func (r *RealDebrid) GetTorrents() ([]*types.Torrent, error) {
|
||||
}
|
||||
allTorrents = append(allTorrents, torrents...)
|
||||
offset += totalTorrents
|
||||
if hardLimit != 0 && len(allTorrents) >= hardLimit {
|
||||
// If hard limit is set, stop fetching more torrents
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if fetchError != nil {
|
||||
@@ -846,7 +826,7 @@ func (r *RealDebrid) GetDownloadLinks() (map[string]*types.DownloadLink, error)
|
||||
offset := 0
|
||||
limit := 1000
|
||||
|
||||
accounts := r.accounts.Active()
|
||||
accounts := r.accounts.All()
|
||||
|
||||
if len(accounts) < 1 {
|
||||
// No active download keys. It's likely that the key has reached bandwidth limit
|
||||
@@ -944,7 +924,6 @@ func (r *RealDebrid) GetProfile() (*types.Profile, error) {
|
||||
return nil, err
|
||||
}
|
||||
profile := &types.Profile{
|
||||
Name: r.name,
|
||||
Id: data.Id,
|
||||
Username: data.Username,
|
||||
Email: data.Email,
|
||||
@@ -974,71 +953,3 @@ func (r *RealDebrid) GetAvailableSlots() (int, error) {
|
||||
func (r *RealDebrid) Accounts() *types.Accounts {
|
||||
return r.accounts
|
||||
}
|
||||
|
||||
func (r *RealDebrid) SyncAccounts() error {
|
||||
// Sync accounts with the current configuration
|
||||
if len(r.accounts.Active()) == 0 {
|
||||
return nil
|
||||
}
|
||||
for idx, account := range r.accounts.Active() {
|
||||
if err := r.syncAccount(idx, account); err != nil {
|
||||
r.logger.Error().Err(err).Msgf("Error syncing account %s", account.Username)
|
||||
continue // Skip this account and continue with the next
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RealDebrid) syncAccount(index int, account *types.Account) error {
|
||||
if account.Token == "" {
|
||||
return fmt.Errorf("account %s has no token", account.Username)
|
||||
}
|
||||
client := http.DefaultClient
|
||||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/user", r.Host), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating request for account %s: %w", account.Username, err)
|
||||
}
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", account.Token))
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error checking account %s: %w", account.Username, err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
resp.Body.Close()
|
||||
return fmt.Errorf("account %s is not valid, status code: %d", account.Username, resp.StatusCode)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var profile profileResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&profile); err != nil {
|
||||
return fmt.Errorf("error decoding profile for account %s: %w", account.Username, err)
|
||||
}
|
||||
account.Username = profile.Username
|
||||
|
||||
// Get traffic usage
|
||||
trafficReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/traffic/details", r.Host), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating request for traffic details for account %s: %w", account.Username, err)
|
||||
}
|
||||
trafficReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", account.Token))
|
||||
trafficResp, err := client.Do(trafficReq)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error checking traffic for account %s: %w", account.Username, err)
|
||||
}
|
||||
if trafficResp.StatusCode != http.StatusOK {
|
||||
trafficResp.Body.Close()
|
||||
return fmt.Errorf("error checking traffic for account %s, status code: %d", account.Username, trafficResp.StatusCode)
|
||||
}
|
||||
|
||||
defer trafficResp.Body.Close()
|
||||
var trafficData TrafficResponse
|
||||
if err := json.NewDecoder(trafficResp.Body).Decode(&trafficData); err != nil {
|
||||
return fmt.Errorf("error decoding traffic details for account %s: %w", account.Username, err)
|
||||
}
|
||||
today := time.Now().Format(time.DateOnly)
|
||||
if todayData, exists := trafficData[today]; exists {
|
||||
account.TrafficUsed = todayData.Bytes
|
||||
}
|
||||
|
||||
r.accounts.Update(index, account)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -144,11 +144,11 @@ type profileResponse struct {
|
||||
Id int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Points int `json:"points"`
|
||||
Points int64 `json:"points"`
|
||||
Locale string `json:"locale"`
|
||||
Avatar string `json:"avatar"`
|
||||
Type string `json:"type"`
|
||||
Premium int64 `json:"premium"`
|
||||
Premium int `json:"premium"`
|
||||
Expiration time.Time `json:"expiration"`
|
||||
}
|
||||
|
||||
@@ -156,10 +156,3 @@ type AvailableSlotsResponse struct {
|
||||
ActiveSlots int `json:"nb"`
|
||||
TotalSlots int `json:"limit"`
|
||||
}
|
||||
|
||||
type hostData struct {
|
||||
Host map[string]int64 `json:"host"`
|
||||
Bytes int64 `json:"bytes"`
|
||||
}
|
||||
|
||||
type TrafficResponse map[string]hostData
|
||||
|
||||
@@ -40,6 +40,10 @@ type Torbox struct {
|
||||
addSamples bool
|
||||
}
|
||||
|
||||
func (tb *Torbox) GetProfile() (*types.Profile, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func New(dc config.Debrid) (*Torbox, error) {
|
||||
rl := request.ParseRateLimit(dc.RateLimit)
|
||||
|
||||
@@ -628,14 +632,6 @@ func (tb *Torbox) GetAvailableSlots() (int, error) {
|
||||
return 0, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (tb *Torbox) GetProfile() (*types.Profile, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (tb *Torbox) Accounts() *types.Accounts {
|
||||
return tb.accounts
|
||||
}
|
||||
|
||||
func (tb *Torbox) SyncAccounts() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/sirrobot01/decypharr/pkg/rclone"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@@ -107,10 +106,9 @@ type Cache struct {
|
||||
|
||||
config config.Debrid
|
||||
customFolders []string
|
||||
mounter *rclone.Mount
|
||||
}
|
||||
|
||||
func NewDebridCache(dc config.Debrid, client types.Client, mounter *rclone.Mount) *Cache {
|
||||
func NewDebridCache(dc config.Debrid, client types.Client) *Cache {
|
||||
cfg := config.Get()
|
||||
cet, err := time.LoadLocation("CET")
|
||||
if err != nil {
|
||||
@@ -122,13 +120,9 @@ func NewDebridCache(dc config.Debrid, client types.Client, mounter *rclone.Mount
|
||||
cetSc, err := gocron.NewScheduler(gocron.WithLocation(cet))
|
||||
if err != nil {
|
||||
// If we can't create a CET scheduler, fallback to local time
|
||||
cetSc, _ = gocron.NewScheduler(gocron.WithLocation(time.Local), gocron.WithGlobalJobOptions(
|
||||
gocron.WithTags("decypharr-"+dc.Name)))
|
||||
cetSc, _ = gocron.NewScheduler(gocron.WithLocation(time.Local))
|
||||
}
|
||||
scheduler, err := gocron.NewScheduler(
|
||||
gocron.WithLocation(time.Local),
|
||||
gocron.WithGlobalJobOptions(
|
||||
gocron.WithTags("decypharr-"+dc.Name)))
|
||||
scheduler, err := gocron.NewScheduler(gocron.WithLocation(time.Local))
|
||||
if err != nil {
|
||||
// If we can't create a local scheduler, fallback to CET
|
||||
scheduler = cetSc
|
||||
@@ -169,7 +163,6 @@ func NewDebridCache(dc config.Debrid, client types.Client, mounter *rclone.Mount
|
||||
|
||||
config: dc,
|
||||
customFolders: customFolders,
|
||||
mounter: mounter,
|
||||
|
||||
ready: make(chan struct{}),
|
||||
}
|
||||
@@ -193,15 +186,6 @@ func (c *Cache) StreamWithRclone() bool {
|
||||
// and before you discard the instance on a restart.
|
||||
func (c *Cache) Reset() {
|
||||
|
||||
// Unmount first
|
||||
if c.mounter != nil && c.mounter.IsMounted() {
|
||||
if err := c.mounter.Unmount(); err != nil {
|
||||
c.logger.Error().Err(err).Msgf("Failed to unmount %s", c.config.Name)
|
||||
} else {
|
||||
c.logger.Info().Msgf("Unmounted %s", c.config.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if err := c.scheduler.StopJobs(); err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to stop scheduler jobs")
|
||||
}
|
||||
@@ -214,9 +198,7 @@ func (c *Cache) Reset() {
|
||||
c.listingDebouncer.Stop()
|
||||
|
||||
// Close the repair channel
|
||||
if c.repairChan != nil {
|
||||
close(c.repairChan)
|
||||
}
|
||||
close(c.repairChan)
|
||||
|
||||
// 1. Reset torrent storage
|
||||
c.torrents.reset()
|
||||
@@ -237,9 +219,6 @@ func (c *Cache) Reset() {
|
||||
|
||||
// 6. Reset repair channel so the next Start() can spin it up
|
||||
c.repairChan = make(chan RepairRequest, 100)
|
||||
|
||||
// Reset the ready channel
|
||||
c.ready = make(chan struct{})
|
||||
}
|
||||
|
||||
func (c *Cache) Start(ctx context.Context) error {
|
||||
@@ -258,6 +237,11 @@ func (c *Cache) Start(ctx context.Context) error {
|
||||
|
||||
// initial download links
|
||||
go c.refreshDownloadLinks(ctx)
|
||||
|
||||
if err := c.StartSchedule(ctx); err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to start cache worker")
|
||||
}
|
||||
|
||||
c.repairChan = make(chan RepairRequest, 100) // Initialize the repair channel, max 100 requests buffered
|
||||
go c.repairWorker(ctx)
|
||||
|
||||
@@ -266,13 +250,10 @@ func (c *Cache) Start(ctx context.Context) error {
|
||||
addr := cfg.BindAddress + ":" + cfg.Port + cfg.URLBase + "webdav/" + name + "/"
|
||||
c.logger.Info().Msgf("%s WebDav server running at %s", name, addr)
|
||||
|
||||
if c.mounter != nil {
|
||||
if err := c.mounter.Mount(ctx); err != nil {
|
||||
c.logger.Error().Err(err).Msgf("Failed to mount %s", c.config.Name)
|
||||
}
|
||||
} else {
|
||||
c.logger.Warn().Msgf("Mounting is disabled for %s", c.config.Name)
|
||||
}
|
||||
<-ctx.Done()
|
||||
c.logger.Info().Msgf("Stopping %s WebDav server", name)
|
||||
c.Reset()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -707,7 +688,7 @@ func (c *Cache) ProcessTorrent(t *types.Torrent) error {
|
||||
Str("torrent_id", t.Id).
|
||||
Str("torrent_name", t.Name).
|
||||
Int("total_files", len(t.Files)).
|
||||
Msg("Torrent still not complete after refresh, marking as bad")
|
||||
Msg("Torrent still not complete after refresh")
|
||||
} else {
|
||||
|
||||
addedOn, err := time.Parse(time.RFC3339, t.Added)
|
||||
@@ -900,7 +881,3 @@ func (c *Cache) RemoveFile(torrentId string, filename string) error {
|
||||
func (c *Cache) Logger() zerolog.Logger {
|
||||
return c.logger
|
||||
}
|
||||
|
||||
func (c *Cache) GetConfig() config.Debrid {
|
||||
return c.config
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package store
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/types"
|
||||
)
|
||||
@@ -102,8 +103,10 @@ func (c *Cache) fetchDownloadLink(torrentName, filename, fileLink string) (*type
|
||||
}
|
||||
|
||||
c.logger.Trace().Msgf("Getting download link for %s(%s)", filename, file.Link)
|
||||
|
||||
downloadLink, err := c.client.GetDownloadLink(ct.Torrent, &file)
|
||||
if err != nil {
|
||||
|
||||
if errors.Is(err, utils.HosterUnavailableError) {
|
||||
c.logger.Trace().
|
||||
Str("filename", filename).
|
||||
@@ -127,7 +130,6 @@ func (c *Cache) fetchDownloadLink(torrentName, filename, fileLink string) (*type
|
||||
if downloadLink == nil {
|
||||
return nil, fmt.Errorf("download link is empty after retry")
|
||||
}
|
||||
return nil, nil
|
||||
} else if errors.Is(err, utils.TrafficExceededError) {
|
||||
// This is likely a fair usage limit error
|
||||
return nil, err
|
||||
|
||||
@@ -127,21 +127,10 @@ func (c *Cache) refreshTorrents(ctx context.Context) {
|
||||
|
||||
func (c *Cache) refreshRclone() error {
|
||||
cfg := c.config
|
||||
dirs := strings.FieldsFunc(cfg.RcRefreshDirs, func(r rune) bool {
|
||||
return r == ',' || r == '&'
|
||||
})
|
||||
if len(dirs) == 0 {
|
||||
dirs = []string{"__all__"}
|
||||
}
|
||||
if c.mounter != nil {
|
||||
return c.mounter.RefreshDir(dirs)
|
||||
} else {
|
||||
return c.refreshRcloneWithRC(dirs)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) refreshRcloneWithRC(dirs []string) error {
|
||||
cfg := c.config
|
||||
if cfg.RcUrl == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if cfg.RcUrl == "" {
|
||||
return nil
|
||||
@@ -149,7 +138,7 @@ func (c *Cache) refreshRcloneWithRC(dirs []string) error {
|
||||
|
||||
client := http.DefaultClient
|
||||
// Create form data
|
||||
data := c.buildRcloneRequestData(dirs)
|
||||
data := c.buildRcloneRequestData()
|
||||
|
||||
if err := c.sendRcloneRequest(client, "vfs/forget", data); err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to send rclone vfs/forget request")
|
||||
@@ -162,7 +151,16 @@ func (c *Cache) refreshRcloneWithRC(dirs []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) buildRcloneRequestData(dirs []string) string {
|
||||
func (c *Cache) buildRcloneRequestData() string {
|
||||
cfg := c.config
|
||||
dirs := strings.FieldsFunc(cfg.RcRefreshDirs, func(r rune) bool {
|
||||
return r == ',' || r == '&'
|
||||
})
|
||||
|
||||
if len(dirs) == 0 {
|
||||
return "dir=__all__"
|
||||
}
|
||||
|
||||
var data strings.Builder
|
||||
for index, dir := range dirs {
|
||||
if dir != "" {
|
||||
|
||||
@@ -6,12 +6,9 @@ import (
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
)
|
||||
|
||||
func (c *Cache) StartWorker(ctx context.Context) error {
|
||||
func (c *Cache) StartSchedule(ctx context.Context) error {
|
||||
// For now, we just want to refresh the listing and download links
|
||||
|
||||
// Stop any existing jobs before starting new ones
|
||||
c.scheduler.RemoveByTags("decypharr-%s", c.GetConfig().Name)
|
||||
|
||||
// Schedule download link refresh job
|
||||
if jd, err := utils.ConvertToJobDef(c.downloadLinksRefreshInterval); err != nil {
|
||||
c.logger.Error().Err(err).Msg("Failed to convert download link refresh interval to job definition")
|
||||
|
||||
@@ -33,17 +33,15 @@ func NewAccounts(debridConf config.Debrid) *Accounts {
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
Debrid string // e.g., "realdebrid", "torbox", etc.
|
||||
Order int
|
||||
Disabled bool
|
||||
Token string `json:"token"`
|
||||
links map[string]*DownloadLink
|
||||
mu sync.RWMutex
|
||||
TrafficUsed int64 `json:"traffic_used"` // Traffic used in bytes
|
||||
Username string `json:"username"` // Username for the account
|
||||
Debrid string // e.g., "realdebrid", "torbox", etc.
|
||||
Order int
|
||||
Disabled bool
|
||||
Token string
|
||||
links map[string]*DownloadLink
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func (a *Accounts) Active() []*Account {
|
||||
func (a *Accounts) All() []*Account {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
activeAccounts := make([]*Account, 0)
|
||||
@@ -55,12 +53,6 @@ func (a *Accounts) Active() []*Account {
|
||||
return activeAccounts
|
||||
}
|
||||
|
||||
func (a *Accounts) All() []*Account {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
return a.accounts
|
||||
}
|
||||
|
||||
func (a *Accounts) Current() *Account {
|
||||
a.mu.RLock()
|
||||
if a.current != nil {
|
||||
@@ -185,23 +177,6 @@ func (a *Accounts) SetDownloadLinks(links map[string]*DownloadLink) {
|
||||
a.Current().setLinks(links)
|
||||
}
|
||||
|
||||
func (a *Accounts) Update(index int, account *Account) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
|
||||
if index < 0 || index >= len(a.accounts) {
|
||||
return // Index out of bounds
|
||||
}
|
||||
|
||||
// Update the account at the specified index
|
||||
a.accounts[index] = account
|
||||
|
||||
// If the updated account is the current one, update the current reference
|
||||
if a.current == nil || a.current.Order == index {
|
||||
a.current = account
|
||||
}
|
||||
}
|
||||
|
||||
func newAccount(debridName, token string, index int) *Account {
|
||||
return &Account{
|
||||
Debrid: debridName,
|
||||
@@ -238,6 +213,7 @@ func (a *Account) LinksCount() int {
|
||||
defer a.mu.RUnlock()
|
||||
return len(a.links)
|
||||
}
|
||||
|
||||
func (a *Account) disable() {
|
||||
a.Disabled = true
|
||||
}
|
||||
|
||||
@@ -25,5 +25,4 @@ type Client interface {
|
||||
DeleteDownloadLink(linkId string) error
|
||||
GetProfile() (*Profile, error)
|
||||
GetAvailableSlots() (int, error)
|
||||
SyncAccounts() error // Updates each accounts details(like traffic, username, etc.)
|
||||
}
|
||||
|
||||
@@ -114,27 +114,19 @@ type IngestData struct {
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
type LibraryStats struct {
|
||||
Total int `json:"total"`
|
||||
Bad int `json:"bad"`
|
||||
ActiveLinks int `json:"active_links"`
|
||||
}
|
||||
|
||||
type Stats struct {
|
||||
Profile *Profile `json:"profile"`
|
||||
Library LibraryStats `json:"library"`
|
||||
Accounts []map[string]any `json:"accounts"`
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
Name string `json:"name"`
|
||||
Id int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Points int `json:"points"`
|
||||
Points int64 `json:"points"`
|
||||
Type string `json:"type"`
|
||||
Premium int64 `json:"premium"`
|
||||
Premium int `json:"premium"`
|
||||
Expiration time.Time `json:"expiration"`
|
||||
|
||||
LibrarySize int `json:"library_size"`
|
||||
BadTorrents int `json:"bad_torrents"`
|
||||
ActiveLinks int `json:"active_links"`
|
||||
}
|
||||
|
||||
type DownloadLink struct {
|
||||
|
||||
@@ -3,14 +3,11 @@ package qbit
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
"github.com/sirrobot01/decypharr/pkg/arr"
|
||||
"github.com/sirrobot01/decypharr/pkg/store"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -22,45 +19,6 @@ const (
|
||||
arrKey contextKey = "arr"
|
||||
)
|
||||
|
||||
func validateServiceURL(urlStr string) error {
|
||||
if urlStr == "" {
|
||||
return fmt.Errorf("URL cannot be empty")
|
||||
}
|
||||
|
||||
// Try parsing as full URL first
|
||||
u, err := url.Parse(urlStr)
|
||||
if err == nil && u.Scheme != "" && u.Host != "" {
|
||||
// It's a full URL, validate scheme
|
||||
if u.Scheme != "http" && u.Scheme != "https" {
|
||||
return fmt.Errorf("URL scheme must be http or https")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if it's a host:port format (no scheme)
|
||||
if strings.Contains(urlStr, ":") && !strings.Contains(urlStr, "://") {
|
||||
// Try parsing with http:// prefix
|
||||
testURL := "http://" + urlStr
|
||||
u, err := url.Parse(testURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid host:port format: %w", err)
|
||||
}
|
||||
|
||||
if u.Host == "" {
|
||||
return fmt.Errorf("host is required in host:port format")
|
||||
}
|
||||
|
||||
// Validate port number
|
||||
if u.Port() == "" {
|
||||
return fmt.Errorf("port is required in host:port format")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("invalid URL format: %s", urlStr)
|
||||
}
|
||||
|
||||
func getCategory(ctx context.Context) string {
|
||||
if category, ok := ctx.Value(categoryKey).(string); ok {
|
||||
return category
|
||||
@@ -127,7 +85,6 @@ func (q *QBit) categoryContext(next http.Handler) http.Handler {
|
||||
// Only a valid host and token will be added to the context/config. The rest are manual
|
||||
func (q *QBit) authContext(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
cfg := config.Get()
|
||||
host, token, err := decodeAuthHeader(r.Header.Get("Authorization"))
|
||||
category := getCategory(r.Context())
|
||||
arrs := store.Get().Arr()
|
||||
@@ -148,22 +105,12 @@ func (q *QBit) authContext(next http.Handler) http.Handler {
|
||||
a.Token = token
|
||||
}
|
||||
}
|
||||
if cfg.NeedsAuth() {
|
||||
if a.Host == "" || a.Token == "" {
|
||||
http.Error(w, "Unauthorized: Host and token are required for authentication", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
// try to use either Arr validate, or user auth validation
|
||||
if err := a.Validate(); err != nil {
|
||||
// If this failed, try to use user auth validation
|
||||
if !verifyAuth(host, token) {
|
||||
http.Error(w, "Unauthorized: Invalid host or token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
a.Source = "auto"
|
||||
if err := utils.ValidateServiceURL(a.Host); err != nil {
|
||||
// Return silently, no need to raise a problem. Just do not add the Arr to the context/config.json
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
arrs.AddOrUpdate(a)
|
||||
ctx := context.WithValue(r.Context(), arrKey, a)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
@@ -189,19 +136,3 @@ func hashesContext(next http.Handler) http.Handler {
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func verifyAuth(username, password string) bool {
|
||||
// If you're storing hashed password, use bcrypt to compare
|
||||
if username == "" {
|
||||
return false
|
||||
}
|
||||
auth := config.Get().GetAuth()
|
||||
if auth == nil {
|
||||
return false
|
||||
}
|
||||
if username != auth.Username {
|
||||
return false
|
||||
}
|
||||
err := bcrypt.CompareHashAndPassword([]byte(auth.Password), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ func (q *QBit) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if err := _arr.Validate(); err != nil {
|
||||
q.logger.Error().Err(err).Msgf("Error validating arr")
|
||||
http.Error(w, "Invalid arr configuration", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte("Ok."))
|
||||
}
|
||||
|
||||
@@ -18,13 +18,16 @@ type QBit struct {
|
||||
}
|
||||
|
||||
func New() *QBit {
|
||||
_cfg := config.Get()
|
||||
cfg := _cfg.QBitTorrent
|
||||
cfg := config.Get()
|
||||
qbitCfg := cfg.QBitTorrent
|
||||
if qbitCfg == nil {
|
||||
return nil
|
||||
}
|
||||
return &QBit{
|
||||
Username: cfg.Username,
|
||||
Password: cfg.Password,
|
||||
DownloadFolder: cfg.DownloadFolder,
|
||||
Categories: cfg.Categories,
|
||||
Username: qbitCfg.Username,
|
||||
Password: qbitCfg.Password,
|
||||
DownloadFolder: qbitCfg.DownloadFolder,
|
||||
Categories: qbitCfg.Categories,
|
||||
storage: store.Get().Torrents(),
|
||||
logger: logger.New("qbit"),
|
||||
}
|
||||
|
||||
@@ -684,18 +684,3 @@ func (r *Reader) ExtractFile(file *File) ([]byte, error) {
|
||||
|
||||
return r.readBytes(file.DataOffset, int(file.CompressedSize))
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -1,403 +0,0 @@
|
||||
package rclone
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
)
|
||||
|
||||
// Mount creates a mount using the rclone RC API with retry logic
|
||||
func (m *Manager) Mount(provider, webdavURL string) error {
|
||||
return m.mountWithRetry(provider, webdavURL, 3)
|
||||
}
|
||||
|
||||
// mountWithRetry attempts to mount with retry logic
|
||||
func (m *Manager) mountWithRetry(provider, webdavURL string, maxRetries int) error {
|
||||
if !m.IsReady() {
|
||||
if err := m.WaitForReady(30 * time.Second); err != nil {
|
||||
return fmt.Errorf("rclone RC server not ready: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
for attempt := 0; attempt <= maxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
// Wait before retry
|
||||
wait := time.Duration(attempt*2) * time.Second
|
||||
m.logger.Debug().
|
||||
Int("attempt", attempt).
|
||||
Str("provider", provider).
|
||||
Msg("Retrying mount operation")
|
||||
time.Sleep(wait)
|
||||
}
|
||||
|
||||
if err := m.performMount(provider, webdavURL); err != nil {
|
||||
m.logger.Error().
|
||||
Err(err).
|
||||
Str("provider", provider).
|
||||
Int("attempt", attempt+1).
|
||||
Msg("Mount attempt failed")
|
||||
continue
|
||||
}
|
||||
|
||||
return nil // Success
|
||||
}
|
||||
return fmt.Errorf("mount failed for %s", provider)
|
||||
}
|
||||
|
||||
// performMount performs a single mount attempt
|
||||
func (m *Manager) performMount(provider, webdavURL string) error {
|
||||
cfg := config.Get()
|
||||
mountPath := filepath.Join(cfg.Rclone.MountPath, provider)
|
||||
|
||||
// Create mount directory
|
||||
if err := os.MkdirAll(mountPath, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create mount directory %s: %w", mountPath, err)
|
||||
}
|
||||
|
||||
// Check if already mounted
|
||||
m.mountsMutex.RLock()
|
||||
existingMount, exists := m.mounts[provider]
|
||||
m.mountsMutex.RUnlock()
|
||||
|
||||
if exists && existingMount.Mounted {
|
||||
m.logger.Info().Str("provider", provider).Str("path", mountPath).Msg("Already mounted")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Clean up any stale mount first
|
||||
if exists && !existingMount.Mounted {
|
||||
err := m.forceUnmountPath(mountPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Create rclone config for this provider
|
||||
if err := m.createConfig(provider, webdavURL); err != nil {
|
||||
return fmt.Errorf("failed to create rclone config: %w", err)
|
||||
}
|
||||
|
||||
// Prepare mount arguments
|
||||
mountArgs := map[string]interface{}{
|
||||
"fs": fmt.Sprintf("%s:", provider),
|
||||
"mountPoint": mountPath,
|
||||
}
|
||||
mountOpt := map[string]interface{}{
|
||||
"AllowNonEmpty": true,
|
||||
"AllowOther": true,
|
||||
"DebugFUSE": false,
|
||||
"DeviceName": fmt.Sprintf("decypharr-%s", provider),
|
||||
"VolumeName": fmt.Sprintf("decypharr-%s", provider),
|
||||
}
|
||||
|
||||
configOpts := make(map[string]interface{})
|
||||
|
||||
if cfg.Rclone.BufferSize != "" {
|
||||
configOpts["BufferSize"] = cfg.Rclone.BufferSize
|
||||
}
|
||||
|
||||
if len(configOpts) > 0 {
|
||||
// Only add _config if there are options to set
|
||||
mountArgs["_config"] = configOpts
|
||||
}
|
||||
vfsOpt := map[string]interface{}{
|
||||
"CacheMode": cfg.Rclone.VfsCacheMode,
|
||||
}
|
||||
vfsOpt["PollInterval"] = 0 // Poll interval not supported for webdav, set to 0
|
||||
|
||||
// Add VFS options if caching is enabled
|
||||
if cfg.Rclone.VfsCacheMode != "off" {
|
||||
|
||||
if cfg.Rclone.VfsCacheMaxAge != "" {
|
||||
vfsOpt["CacheMaxAge"] = cfg.Rclone.VfsCacheMaxAge
|
||||
}
|
||||
if cfg.Rclone.VfsCacheMaxSize != "" {
|
||||
vfsOpt["CacheMaxSize"] = cfg.Rclone.VfsCacheMaxSize
|
||||
}
|
||||
if cfg.Rclone.VfsCachePollInterval != "" {
|
||||
vfsOpt["CachePollInterval"] = cfg.Rclone.VfsCachePollInterval
|
||||
}
|
||||
if cfg.Rclone.VfsReadChunkSize != "" {
|
||||
vfsOpt["ChunkSize"] = cfg.Rclone.VfsReadChunkSize
|
||||
}
|
||||
if cfg.Rclone.VfsReadAhead != "" {
|
||||
vfsOpt["ReadAhead"] = cfg.Rclone.VfsReadAhead
|
||||
}
|
||||
if cfg.Rclone.NoChecksum {
|
||||
vfsOpt["NoChecksum"] = cfg.Rclone.NoChecksum
|
||||
}
|
||||
if cfg.Rclone.NoModTime {
|
||||
vfsOpt["NoModTime"] = cfg.Rclone.NoModTime
|
||||
}
|
||||
}
|
||||
|
||||
// Add mount options based on configuration
|
||||
if cfg.Rclone.UID != 0 {
|
||||
mountOpt["UID"] = cfg.Rclone.UID
|
||||
}
|
||||
if cfg.Rclone.GID != 0 {
|
||||
mountOpt["GID"] = cfg.Rclone.GID
|
||||
}
|
||||
if cfg.Rclone.AttrTimeout != "" {
|
||||
if attrTimeout, err := time.ParseDuration(cfg.Rclone.AttrTimeout); err == nil {
|
||||
mountOpt["AttrTimeout"] = attrTimeout.String()
|
||||
}
|
||||
}
|
||||
|
||||
mountArgs["vfsOpt"] = vfsOpt
|
||||
mountArgs["mountOpt"] = mountOpt
|
||||
// Make the mount request
|
||||
req := RCRequest{
|
||||
Command: "mount/mount",
|
||||
Args: mountArgs,
|
||||
}
|
||||
|
||||
_, err := m.makeRequest(req, true)
|
||||
if err != nil {
|
||||
// Clean up mount point on failure
|
||||
m.forceUnmountPath(mountPath)
|
||||
return fmt.Errorf("failed to create mount for %s: %w", provider, err)
|
||||
}
|
||||
|
||||
// Store mount info
|
||||
mountInfo := &MountInfo{
|
||||
Provider: provider,
|
||||
LocalPath: mountPath,
|
||||
WebDAVURL: webdavURL,
|
||||
Mounted: true,
|
||||
MountedAt: time.Now().Format(time.RFC3339),
|
||||
ConfigName: provider,
|
||||
}
|
||||
|
||||
m.mountsMutex.Lock()
|
||||
m.mounts[provider] = mountInfo
|
||||
m.mountsMutex.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unmount unmounts a specific provider
|
||||
func (m *Manager) Unmount(provider string) error {
|
||||
return m.unmount(provider)
|
||||
}
|
||||
|
||||
// unmount is the internal unmount function
|
||||
func (m *Manager) unmount(provider string) error {
|
||||
m.mountsMutex.RLock()
|
||||
mountInfo, exists := m.mounts[provider]
|
||||
m.mountsMutex.RUnlock()
|
||||
|
||||
if !exists || !mountInfo.Mounted {
|
||||
m.logger.Info().Str("provider", provider).Msg("Mount not found or already unmounted")
|
||||
return nil
|
||||
}
|
||||
|
||||
m.logger.Info().Str("provider", provider).Str("path", mountInfo.LocalPath).Msg("Unmounting")
|
||||
|
||||
// Try RC unmount first
|
||||
req := RCRequest{
|
||||
Command: "mount/unmount",
|
||||
Args: map[string]interface{}{
|
||||
"mountPoint": mountInfo.LocalPath,
|
||||
},
|
||||
}
|
||||
|
||||
var rcErr error
|
||||
if m.IsReady() {
|
||||
_, rcErr = m.makeRequest(req, true)
|
||||
}
|
||||
|
||||
// If RC unmount fails or server is not ready, try force unmount
|
||||
if rcErr != nil {
|
||||
m.logger.Warn().Err(rcErr).Str("provider", provider).Msg("RC unmount failed, trying force unmount")
|
||||
if err := m.forceUnmountPath(mountInfo.LocalPath); err != nil {
|
||||
m.logger.Error().Err(err).Str("provider", provider).Msg("Force unmount failed")
|
||||
// Don't return error here, update the state anyway
|
||||
}
|
||||
}
|
||||
|
||||
// Update mount info
|
||||
m.mountsMutex.Lock()
|
||||
if info, exists := m.mounts[provider]; exists {
|
||||
info.Mounted = false
|
||||
info.Error = ""
|
||||
if rcErr != nil {
|
||||
info.Error = rcErr.Error()
|
||||
}
|
||||
}
|
||||
m.mountsMutex.Unlock()
|
||||
|
||||
m.logger.Info().Str("provider", provider).Msg("Unmount completed")
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmountAll unmounts all mounts
|
||||
func (m *Manager) UnmountAll() error {
|
||||
m.mountsMutex.RLock()
|
||||
providers := make([]string, 0, len(m.mounts))
|
||||
for provider, mount := range m.mounts {
|
||||
if mount.Mounted {
|
||||
providers = append(providers, provider)
|
||||
}
|
||||
}
|
||||
m.mountsMutex.RUnlock()
|
||||
|
||||
var lastError error
|
||||
for _, provider := range providers {
|
||||
if err := m.unmount(provider); err != nil {
|
||||
lastError = err
|
||||
m.logger.Error().Err(err).Str("provider", provider).Msg("Failed to unmount")
|
||||
}
|
||||
}
|
||||
|
||||
return lastError
|
||||
}
|
||||
|
||||
// GetMountInfo returns information about a specific mount
|
||||
func (m *Manager) GetMountInfo(provider string) (*MountInfo, bool) {
|
||||
m.mountsMutex.RLock()
|
||||
defer m.mountsMutex.RUnlock()
|
||||
|
||||
info, exists := m.mounts[provider]
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Create a copy to avoid race conditions
|
||||
mountInfo := *info
|
||||
return &mountInfo, true
|
||||
}
|
||||
|
||||
// GetAllMounts returns information about all mounts
|
||||
func (m *Manager) GetAllMounts() map[string]*MountInfo {
|
||||
m.mountsMutex.RLock()
|
||||
defer m.mountsMutex.RUnlock()
|
||||
|
||||
result := make(map[string]*MountInfo, len(m.mounts))
|
||||
for provider, info := range m.mounts {
|
||||
// Create a copy to avoid race conditions
|
||||
mountInfo := *info
|
||||
result[provider] = &mountInfo
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// IsMounted checks if a provider is mounted
|
||||
func (m *Manager) IsMounted(provider string) bool {
|
||||
info, exists := m.GetMountInfo(provider)
|
||||
return exists && info.Mounted
|
||||
}
|
||||
|
||||
// RefreshDir refreshes directories in the VFS cache
|
||||
func (m *Manager) RefreshDir(provider string, dirs []string) error {
|
||||
if !m.IsReady() {
|
||||
return fmt.Errorf("rclone RC server not ready")
|
||||
}
|
||||
|
||||
mountInfo, exists := m.GetMountInfo(provider)
|
||||
if !exists || !mountInfo.Mounted {
|
||||
return fmt.Errorf("provider %s not mounted", provider)
|
||||
}
|
||||
|
||||
// If no specific directories provided, refresh root
|
||||
if len(dirs) == 0 {
|
||||
dirs = []string{"/"}
|
||||
}
|
||||
args := map[string]interface{}{
|
||||
"fs": fmt.Sprintf("%s:", provider),
|
||||
}
|
||||
for i, dir := range dirs {
|
||||
if dir != "" {
|
||||
if i == 0 {
|
||||
args["dir"] = dir
|
||||
} else {
|
||||
args[fmt.Sprintf("dir%d", i+1)] = dir
|
||||
}
|
||||
}
|
||||
}
|
||||
req := RCRequest{
|
||||
Command: "vfs/forget",
|
||||
Args: args,
|
||||
}
|
||||
|
||||
_, err := m.makeRequest(req, true)
|
||||
if err != nil {
|
||||
m.logger.Error().Err(err).
|
||||
Str("provider", provider).
|
||||
Msg("Failed to refresh directory")
|
||||
return fmt.Errorf("failed to refresh directory %s for provider %s: %w", dirs, provider, err)
|
||||
}
|
||||
|
||||
req = RCRequest{
|
||||
Command: "vfs/refresh",
|
||||
Args: args,
|
||||
}
|
||||
|
||||
_, err = m.makeRequest(req, true)
|
||||
if err != nil {
|
||||
m.logger.Error().Err(err).
|
||||
Str("provider", provider).
|
||||
Msg("Failed to refresh directory")
|
||||
return fmt.Errorf("failed to refresh directory %s for provider %s: %w", dirs, provider, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// createConfig creates an rclone config entry for the provider
|
||||
func (m *Manager) createConfig(configName, webdavURL string) error {
|
||||
req := RCRequest{
|
||||
Command: "config/create",
|
||||
Args: map[string]interface{}{
|
||||
"name": configName,
|
||||
"type": "webdav",
|
||||
"parameters": map[string]interface{}{
|
||||
"url": webdavURL,
|
||||
"vendor": "other",
|
||||
"pacer_min_sleep": "0",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err := m.makeRequest(req, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create config %s: %w", configName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// forceUnmountPath attempts to force unmount a path using system commands
|
||||
func (m *Manager) forceUnmountPath(mountPath string) error {
|
||||
methods := [][]string{
|
||||
{"umount", mountPath},
|
||||
{"umount", "-l", mountPath}, // lazy unmount
|
||||
{"fusermount", "-uz", mountPath},
|
||||
{"fusermount3", "-uz", mountPath},
|
||||
}
|
||||
|
||||
for _, method := range methods {
|
||||
if err := m.tryUnmountCommand(method...); err == nil {
|
||||
m.logger.Info().
|
||||
Strs("command", method).
|
||||
Str("path", mountPath).
|
||||
Msg("Successfully unmounted using system command")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("all force unmount attempts failed for %s", mountPath)
|
||||
}
|
||||
|
||||
// tryUnmountCommand tries to run an unmount command
|
||||
func (m *Manager) tryUnmountCommand(args ...string) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("no command provided")
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(m.ctx, args[0], args[1:]...)
|
||||
return cmd.Run()
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
package rclone
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HealthCheck performs comprehensive health checks on the rclone system
|
||||
func (m *Manager) HealthCheck() error {
|
||||
if !m.serverStarted {
|
||||
return fmt.Errorf("rclone RC server is not started")
|
||||
}
|
||||
|
||||
if !m.IsReady() {
|
||||
return fmt.Errorf("rclone RC server is not ready")
|
||||
}
|
||||
|
||||
// Check if we can communicate with the server
|
||||
if !m.pingServer() {
|
||||
return fmt.Errorf("rclone RC server is not responding")
|
||||
}
|
||||
|
||||
// Check mounts health
|
||||
m.mountsMutex.RLock()
|
||||
unhealthyMounts := make([]string, 0)
|
||||
for provider, mount := range m.mounts {
|
||||
if mount.Mounted && !m.checkMountHealth(provider) {
|
||||
unhealthyMounts = append(unhealthyMounts, provider)
|
||||
}
|
||||
}
|
||||
m.mountsMutex.RUnlock()
|
||||
|
||||
if len(unhealthyMounts) > 0 {
|
||||
return fmt.Errorf("unhealthy mounts detected: %v", unhealthyMounts)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkMountHealth checks if a specific mount is healthy
|
||||
func (m *Manager) checkMountHealth(provider string) bool {
|
||||
// Try to list the root directory of the mount
|
||||
req := RCRequest{
|
||||
Command: "operations/list",
|
||||
Args: map[string]interface{}{
|
||||
"fs": fmt.Sprintf("%s:", provider),
|
||||
"remote": "",
|
||||
},
|
||||
}
|
||||
|
||||
_, err := m.makeRequest(req, true)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// RecoverMount attempts to recover a failed mount
|
||||
func (m *Manager) RecoverMount(provider string) error {
|
||||
m.mountsMutex.RLock()
|
||||
mountInfo, exists := m.mounts[provider]
|
||||
m.mountsMutex.RUnlock()
|
||||
|
||||
if !exists {
|
||||
return fmt.Errorf("mount for provider %s does not exist", provider)
|
||||
}
|
||||
|
||||
m.logger.Warn().Str("provider", provider).Msg("Attempting to recover mount")
|
||||
|
||||
// First try to unmount cleanly
|
||||
if err := m.unmount(provider); err != nil {
|
||||
m.logger.Error().Err(err).Str("provider", provider).Msg("Failed to unmount during recovery")
|
||||
}
|
||||
|
||||
// Wait a moment
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
// Try to remount
|
||||
if err := m.Mount(provider, mountInfo.WebDAVURL); err != nil {
|
||||
return fmt.Errorf("failed to recover mount for %s: %w", provider, err)
|
||||
}
|
||||
|
||||
m.logger.Info().Str("provider", provider).Msg("Successfully recovered mount")
|
||||
return nil
|
||||
}
|
||||
|
||||
// MonitorMounts continuously monitors mount health and attempts recovery
|
||||
func (m *Manager) MonitorMounts(ctx context.Context) {
|
||||
if !m.serverStarted {
|
||||
return
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(30 * time.Second) // Check every 30 seconds
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
m.logger.Debug().Msg("Mount monitoring stopped")
|
||||
return
|
||||
case <-ticker.C:
|
||||
m.performMountHealthCheck()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// performMountHealthCheck checks and attempts to recover unhealthy mounts
|
||||
func (m *Manager) performMountHealthCheck() {
|
||||
if !m.IsReady() {
|
||||
return
|
||||
}
|
||||
|
||||
m.mountsMutex.RLock()
|
||||
providers := make([]string, 0, len(m.mounts))
|
||||
for provider, mount := range m.mounts {
|
||||
if mount.Mounted {
|
||||
providers = append(providers, provider)
|
||||
}
|
||||
}
|
||||
m.mountsMutex.RUnlock()
|
||||
|
||||
for _, provider := range providers {
|
||||
if !m.checkMountHealth(provider) {
|
||||
m.logger.Warn().Str("provider", provider).Msg("Mount health check failed, attempting recovery")
|
||||
|
||||
// Mark mount as unhealthy
|
||||
m.mountsMutex.Lock()
|
||||
if mount, exists := m.mounts[provider]; exists {
|
||||
mount.Error = "Health check failed"
|
||||
mount.Mounted = false
|
||||
}
|
||||
m.mountsMutex.Unlock()
|
||||
|
||||
// Attempt recovery
|
||||
go func(provider string) {
|
||||
if err := m.RecoverMount(provider); err != nil {
|
||||
m.logger.Error().Err(err).Str("provider", provider).Msg("Failed to recover mount")
|
||||
}
|
||||
}(provider)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
//go:build !windows
|
||||
|
||||
package rclone
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// WasHardTerminated reports true iff the process was ended by SIGKILL or SIGTERM.
|
||||
func WasHardTerminated(err error) bool {
|
||||
var ee *exec.ExitError
|
||||
if !errors.As(err, &ee) {
|
||||
return false
|
||||
}
|
||||
ws, ok := ee.Sys().(syscall.WaitStatus)
|
||||
if !ok || !ws.Signaled() {
|
||||
return false
|
||||
}
|
||||
sig := ws.Signal()
|
||||
return sig == syscall.SIGKILL || sig == syscall.SIGTERM
|
||||
}
|
||||
|
||||
// ExitCode returns the numeric exit code when available.
|
||||
func ExitCode(err error) (int, bool) {
|
||||
var ee *exec.ExitError
|
||||
if !errors.As(err, &ee) {
|
||||
return 0, false
|
||||
}
|
||||
ws, ok := ee.Sys().(syscall.WaitStatus)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
if ws.Exited() {
|
||||
return ws.ExitStatus(), true
|
||||
}
|
||||
// Conventional shell “killed by signal” code is 128 + signal.
|
||||
if ws.Signaled() {
|
||||
return 128 + int(ws.Signal()), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package rclone
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func WasHardTerminated(err error) bool {
|
||||
var ee *exec.ExitError
|
||||
if !errors.As(err, &ee) {
|
||||
return false
|
||||
}
|
||||
ws, ok := ee.Sys().(syscall.WaitStatus)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
// No Signaled() on Windows; consider "hard terminated" if not success.
|
||||
return ws.ExitStatus() != 0 // Use the ExitStatus() method
|
||||
}
|
||||
|
||||
// ExitCode returns the process exit code when available.
|
||||
func ExitCode(err error) (int, bool) {
|
||||
var ee *exec.ExitError
|
||||
if !errors.As(err, &ee) {
|
||||
return 0, false
|
||||
}
|
||||
ws, ok := ee.Sys().(syscall.WaitStatus)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
return ws.ExitStatus(), true // Use the ExitStatus() method
|
||||
}
|
||||
@@ -1,402 +0,0 @@
|
||||
package rclone
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/internal/logger"
|
||||
)
|
||||
|
||||
// Manager handles the rclone RC server and provides mount operations
|
||||
type Manager struct {
|
||||
cmd *exec.Cmd
|
||||
rcPort string
|
||||
rcUser string
|
||||
rcPass string
|
||||
rcloneDir string
|
||||
mounts map[string]*MountInfo
|
||||
mountsMutex sync.RWMutex
|
||||
logger zerolog.Logger
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
httpClient *http.Client
|
||||
serverReady chan struct{}
|
||||
serverStarted bool
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
type MountInfo struct {
|
||||
Provider string `json:"provider"`
|
||||
LocalPath string `json:"local_path"`
|
||||
WebDAVURL string `json:"webdav_url"`
|
||||
Mounted bool `json:"mounted"`
|
||||
MountedAt string `json:"mounted_at,omitempty"`
|
||||
ConfigName string `json:"config_name"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type RCRequest struct {
|
||||
Command string `json:"command"`
|
||||
Args map[string]interface{} `json:"args,omitempty"`
|
||||
}
|
||||
|
||||
type RCResponse struct {
|
||||
Result interface{} `json:"result,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// NewManager creates a new rclone RC manager
|
||||
func NewManager() *Manager {
|
||||
cfg := config.Get()
|
||||
|
||||
rcPort := "5572"
|
||||
rcloneDir := filepath.Join(cfg.Path, "rclone")
|
||||
|
||||
// Ensure config directory exists
|
||||
if err := os.MkdirAll(rcloneDir, 0755); err != nil {
|
||||
_logger := logger.New("rclone")
|
||||
_logger.Error().Err(err).Msg("Failed to create rclone config directory")
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
return &Manager{
|
||||
rcPort: rcPort,
|
||||
rcloneDir: rcloneDir,
|
||||
mounts: make(map[string]*MountInfo),
|
||||
logger: logger.New("rclone"),
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
||||
serverReady: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Start starts the rclone RC server
|
||||
func (m *Manager) Start(ctx context.Context) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.serverStarted {
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := config.Get()
|
||||
if !cfg.Rclone.Enabled {
|
||||
m.logger.Info().Msg("Rclone is disabled, skipping RC server startup")
|
||||
return nil
|
||||
}
|
||||
|
||||
logFile := filepath.Join(logger.GetLogPath(), "rclone.log")
|
||||
|
||||
// Delete old log file if it exists
|
||||
if _, err := os.Stat(logFile); err == nil {
|
||||
if err := os.Remove(logFile); err != nil {
|
||||
return fmt.Errorf("failed to remove old rclone log file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"rcd",
|
||||
"--rc-addr", ":" + m.rcPort,
|
||||
"--rc-no-auth", // We'll handle auth at the application level
|
||||
"--config", filepath.Join(m.rcloneDir, "rclone.conf"),
|
||||
"--log-file", logFile,
|
||||
}
|
||||
|
||||
logLevel := cfg.Rclone.LogLevel
|
||||
if logLevel != "" {
|
||||
if !slices.Contains([]string{"DEBUG", "INFO", "NOTICE", "ERROR"}, logLevel) {
|
||||
logLevel = "INFO"
|
||||
}
|
||||
args = append(args, "--log-level", logLevel)
|
||||
}
|
||||
|
||||
if cfg.Rclone.CacheDir != "" {
|
||||
if err := os.MkdirAll(cfg.Rclone.CacheDir, 0755); err == nil {
|
||||
args = append(args, "--cache-dir", cfg.Rclone.CacheDir)
|
||||
}
|
||||
}
|
||||
m.cmd = exec.CommandContext(ctx, "rclone", args...)
|
||||
|
||||
// Capture output for debugging
|
||||
var stdout, stderr bytes.Buffer
|
||||
m.cmd.Stdout = &stdout
|
||||
m.cmd.Stderr = &stderr
|
||||
|
||||
if err := m.cmd.Start(); err != nil {
|
||||
m.logger.Error().Str("stderr", stderr.String()).Str("stdout", stdout.String()).
|
||||
Err(err).Msg("Failed to start rclone RC server")
|
||||
return fmt.Errorf("failed to start rclone RC server: %w", err)
|
||||
}
|
||||
m.serverStarted = true
|
||||
|
||||
// Wait for server to be ready in a goroutine
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
m.logger.Error().Interface("panic", r).Msg("Panic in rclone RC server monitor")
|
||||
}
|
||||
}()
|
||||
|
||||
m.waitForServer()
|
||||
close(m.serverReady)
|
||||
|
||||
// Start mount monitoring once server is ready
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
m.logger.Error().Interface("panic", r).Msg("Panic in mount monitor")
|
||||
}
|
||||
}()
|
||||
m.MonitorMounts(ctx)
|
||||
}()
|
||||
|
||||
// Wait for command to finish and log output
|
||||
err := m.cmd.Wait()
|
||||
switch {
|
||||
case err == nil:
|
||||
m.logger.Info().Msg("Rclone RC server exited normally")
|
||||
|
||||
case errors.Is(err, context.Canceled):
|
||||
m.logger.Info().Msg("Rclone RC server terminated: context canceled")
|
||||
|
||||
case WasHardTerminated(err): // SIGKILL on *nix; non-zero exit on Windows
|
||||
m.logger.Info().Msg("Rclone RC server hard-terminated")
|
||||
|
||||
default:
|
||||
if code, ok := ExitCode(err); ok {
|
||||
m.logger.Debug().Int("exit_code", code).Err(err).
|
||||
Str("stderr", stderr.String()).
|
||||
Str("stdout", stdout.String()).
|
||||
Msg("Rclone RC server error")
|
||||
} else {
|
||||
m.logger.Debug().Err(err).Str("stderr", stderr.String()).
|
||||
Str("stdout", stdout.String()).Msg("Rclone RC server error (no exit code)")
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop stops the rclone RC server and unmounts all mounts
|
||||
func (m *Manager) Stop() error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if !m.serverStarted {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.logger.Info().Msg("Stopping rclone RC server")
|
||||
|
||||
// Unmount all mounts first
|
||||
m.mountsMutex.RLock()
|
||||
mountList := make([]*MountInfo, 0, len(m.mounts))
|
||||
for _, mount := range m.mounts {
|
||||
if mount.Mounted {
|
||||
mountList = append(mountList, mount)
|
||||
}
|
||||
}
|
||||
m.mountsMutex.RUnlock()
|
||||
|
||||
// Unmount in parallel
|
||||
var wg sync.WaitGroup
|
||||
for _, mount := range mountList {
|
||||
wg.Add(1)
|
||||
go func(mount *MountInfo) {
|
||||
defer wg.Done()
|
||||
if err := m.unmount(mount.Provider); err != nil {
|
||||
m.logger.Error().Err(err).Str("provider", mount.Provider).Msg("Failed to unmount during shutdown")
|
||||
}
|
||||
}(mount)
|
||||
}
|
||||
|
||||
// Wait for unmounts with timeout
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
m.logger.Info().Msg("All mounts unmounted successfully")
|
||||
case <-time.After(30 * time.Second):
|
||||
m.logger.Warn().Msg("Timeout waiting for mounts to unmount, proceeding with shutdown")
|
||||
}
|
||||
|
||||
// Cancel context and stop process
|
||||
m.cancel()
|
||||
|
||||
if m.cmd != nil && m.cmd.Process != nil {
|
||||
// Try graceful shutdown first
|
||||
if err := m.cmd.Process.Signal(os.Interrupt); err != nil {
|
||||
m.logger.Warn().Err(err).Msg("Failed to send interrupt signal, using kill")
|
||||
if killErr := m.cmd.Process.Kill(); killErr != nil {
|
||||
m.logger.Error().Err(killErr).Msg("Failed to kill rclone process")
|
||||
return killErr
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for process to exit with timeout
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- m.cmd.Wait()
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil && !errors.Is(err, context.Canceled) && !WasHardTerminated(err) {
|
||||
m.logger.Warn().Err(err).Msg("Rclone process exited with error")
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
m.logger.Warn().Msg("Timeout waiting for rclone to exit, force killing")
|
||||
if err := m.cmd.Process.Kill(); err != nil {
|
||||
m.logger.Error().Err(err).Msg("Failed to force kill rclone process")
|
||||
return err
|
||||
}
|
||||
// Wait a bit more for the kill to take effect
|
||||
select {
|
||||
case <-done:
|
||||
m.logger.Info().Msg("Rclone process killed successfully")
|
||||
case <-time.After(5 * time.Second):
|
||||
m.logger.Error().Msg("Process may still be running after kill")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up any remaining mount directories
|
||||
cfg := config.Get()
|
||||
if cfg.Rclone.MountPath != "" {
|
||||
m.cleanupMountDirectories(cfg.Rclone.MountPath)
|
||||
}
|
||||
|
||||
m.serverStarted = false
|
||||
m.logger.Info().Msg("Rclone RC server stopped")
|
||||
return nil
|
||||
}
|
||||
|
||||
// cleanupMountDirectories removes empty mount directories
|
||||
func (m *Manager) cleanupMountDirectories(_ string) {
|
||||
m.mountsMutex.RLock()
|
||||
defer m.mountsMutex.RUnlock()
|
||||
|
||||
for _, mount := range m.mounts {
|
||||
if mount.LocalPath != "" {
|
||||
// Try to remove the directory if it's empty
|
||||
if err := os.Remove(mount.LocalPath); err == nil {
|
||||
m.logger.Debug().Str("path", mount.LocalPath).Msg("Removed empty mount directory")
|
||||
}
|
||||
// Don't log errors here as the directory might not be empty, which is fine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// waitForServer waits for the RC server to become available
|
||||
func (m *Manager) waitForServer() {
|
||||
maxAttempts := 30
|
||||
for i := 0; i < maxAttempts; i++ {
|
||||
if m.ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if m.pingServer() {
|
||||
m.logger.Info().Msg("Rclone RC server is ready")
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
m.logger.Error().Msg("Rclone RC server not responding - mount operations will be disabled")
|
||||
}
|
||||
|
||||
// pingServer checks if the RC server is responding
|
||||
func (m *Manager) pingServer() bool {
|
||||
req := RCRequest{Command: "core/version"}
|
||||
_, err := m.makeRequest(req, true)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (m *Manager) makeRequest(req RCRequest, close bool) (*http.Response, error) {
|
||||
reqBody, err := json.Marshal(req.Args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("http://localhost:%s/%s", m.rcPort, req.Command)
|
||||
httpReq, err := http.NewRequestWithContext(m.ctx, "POST", url, bytes.NewBuffer(reqBody))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := m.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to make request: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// Read the response body to get more details
|
||||
defer resp.Body.Close()
|
||||
var errorResp RCResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&errorResp); err != nil {
|
||||
return nil, fmt.Errorf("request failed with status %s, but could not decode error response: %w", resp.Status, err)
|
||||
}
|
||||
if errorResp.Error != "" {
|
||||
return nil, fmt.Errorf("%s", errorResp.Error)
|
||||
} else {
|
||||
return nil, fmt.Errorf("request failed with status %s and no error message", resp.Status)
|
||||
}
|
||||
}
|
||||
|
||||
if close {
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
m.logger.Debug().Err(err).Msg("Failed to close response body")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// IsReady returns true if the RC server is ready
|
||||
func (m *Manager) IsReady() bool {
|
||||
select {
|
||||
case <-m.serverReady:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// WaitForReady waits for the RC server to be ready
|
||||
func (m *Manager) WaitForReady(timeout time.Duration) error {
|
||||
select {
|
||||
case <-m.serverReady:
|
||||
return nil
|
||||
case <-time.After(timeout):
|
||||
return fmt.Errorf("timeout waiting for rclone RC server to be ready")
|
||||
case <-m.ctx.Done():
|
||||
return m.ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) GetLogger() zerolog.Logger {
|
||||
return m.logger
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
package rclone
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Mount represents a mount using the rclone RC client
|
||||
type Mount struct {
|
||||
Provider string
|
||||
LocalPath string
|
||||
WebDAVURL string
|
||||
logger zerolog.Logger
|
||||
rcManager *Manager
|
||||
}
|
||||
|
||||
// NewMount creates a new RC-based mount
|
||||
func NewMount(provider, customRcloneMount, webdavURL string, rcManager *Manager) *Mount {
|
||||
cfg := config.Get()
|
||||
var mountPath string
|
||||
if customRcloneMount != "" {
|
||||
mountPath = customRcloneMount
|
||||
} else {
|
||||
mountPath = filepath.Join(cfg.Rclone.MountPath, provider)
|
||||
}
|
||||
|
||||
_url, err := url.JoinPath(webdavURL, provider)
|
||||
if err != nil {
|
||||
_url = fmt.Sprintf("%s/%s", webdavURL, provider)
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(_url, "/") {
|
||||
_url += "/"
|
||||
}
|
||||
|
||||
return &Mount{
|
||||
Provider: provider,
|
||||
LocalPath: mountPath,
|
||||
WebDAVURL: _url,
|
||||
rcManager: rcManager,
|
||||
logger: rcManager.GetLogger(),
|
||||
}
|
||||
}
|
||||
|
||||
// Mount creates the mount using rclone RC
|
||||
func (m *Mount) Mount(ctx context.Context) error {
|
||||
if m.rcManager == nil {
|
||||
return fmt.Errorf("rclone manager is not available")
|
||||
}
|
||||
|
||||
// Check if already mounted
|
||||
if m.rcManager.IsMounted(m.Provider) {
|
||||
m.logger.Info().Msgf("Mount %s is already mounted at %s", m.Provider, m.LocalPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
m.logger.Info().
|
||||
Str("provider", m.Provider).
|
||||
Str("webdav_url", m.WebDAVURL).
|
||||
Str("mount_path", m.LocalPath).
|
||||
Msg("Creating mount via RC")
|
||||
|
||||
if err := m.rcManager.Mount(m.Provider, m.WebDAVURL); err != nil {
|
||||
m.logger.Error().Str("provider", m.Provider).Msg("Mount operation failed")
|
||||
return fmt.Errorf("mount failed for %s", m.Provider)
|
||||
}
|
||||
|
||||
m.logger.Info().Msgf("Successfully mounted %s WebDAV at %s via RC", m.Provider, m.LocalPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unmount removes the mount using rclone RC
|
||||
func (m *Mount) Unmount() error {
|
||||
if m.rcManager == nil {
|
||||
m.logger.Warn().Msg("Rclone manager is not available, skipping unmount")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !m.rcManager.IsMounted(m.Provider) {
|
||||
m.logger.Info().Msgf("Mount %s is not mounted, skipping unmount", m.Provider)
|
||||
return nil
|
||||
}
|
||||
|
||||
m.logger.Info().Str("provider", m.Provider).Msg("Unmounting via RC")
|
||||
|
||||
if err := m.rcManager.Unmount(m.Provider); err != nil {
|
||||
return fmt.Errorf("failed to unmount %s via RC: %w", m.Provider, err)
|
||||
}
|
||||
|
||||
m.logger.Info().Msgf("Successfully unmounted %s", m.Provider)
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsMounted checks if the mount is active via RC
|
||||
func (m *Mount) IsMounted() bool {
|
||||
if m.rcManager == nil {
|
||||
return false
|
||||
}
|
||||
return m.rcManager.IsMounted(m.Provider)
|
||||
}
|
||||
|
||||
// RefreshDir refreshes directories in the mount
|
||||
func (m *Mount) RefreshDir(dirs []string) error {
|
||||
if m.rcManager == nil {
|
||||
return fmt.Errorf("rclone manager is not available")
|
||||
}
|
||||
|
||||
if !m.IsMounted() {
|
||||
return fmt.Errorf("provider %s not properly mounted. Skipping refreshes", m.Provider)
|
||||
}
|
||||
|
||||
if err := m.rcManager.RefreshDir(m.Provider, dirs); err != nil {
|
||||
return fmt.Errorf("failed to refresh directories for %s: %w", m.Provider, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMountInfo returns mount information
|
||||
func (m *Mount) GetMountInfo() (*MountInfo, bool) {
|
||||
if m.rcManager == nil {
|
||||
return nil, false
|
||||
}
|
||||
return m.rcManager.GetMountInfo(m.Provider)
|
||||
}
|
||||
@@ -1,184 +0,0 @@
|
||||
package rclone
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type TransferringStat struct {
|
||||
Bytes int64 `json:"bytes"`
|
||||
ETA int64 `json:"eta"`
|
||||
Name string `json:"name"`
|
||||
Speed float64 `json:"speed"`
|
||||
Size int64 `json:"size"`
|
||||
Progress float64 `json:"progress"`
|
||||
}
|
||||
|
||||
type VersionResponse struct {
|
||||
Arch string `json:"arch"`
|
||||
Version string `json:"version"`
|
||||
OS string `json:"os"`
|
||||
}
|
||||
|
||||
type CoreStatsResponse struct {
|
||||
Bytes int64 `json:"bytes"`
|
||||
Checks int `json:"checks"`
|
||||
DeletedDirs int `json:"deletedDirs"`
|
||||
Deletes int `json:"deletes"`
|
||||
ElapsedTime float64 `json:"elapsedTime"`
|
||||
Errors int `json:"errors"`
|
||||
Eta int `json:"eta"`
|
||||
Speed float64 `json:"speed"`
|
||||
TotalBytes int64 `json:"totalBytes"`
|
||||
TotalChecks int `json:"totalChecks"`
|
||||
TotalTransfers int `json:"totalTransfers"`
|
||||
TransferTime float64 `json:"transferTime"`
|
||||
Transfers int `json:"transfers"`
|
||||
Transferring []TransferringStat `json:"transferring,omitempty"`
|
||||
}
|
||||
|
||||
type MemoryStats struct {
|
||||
Sys int `json:"Sys"`
|
||||
TotalAlloc int64 `json:"TotalAlloc"`
|
||||
}
|
||||
|
||||
type BandwidthStats struct {
|
||||
BytesPerSecond int64 `json:"bytesPerSecond"`
|
||||
Rate string `json:"rate"`
|
||||
}
|
||||
|
||||
// Stats represents rclone statistics
|
||||
type Stats struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Ready bool `json:"server_ready"`
|
||||
Core CoreStatsResponse `json:"core"`
|
||||
Memory MemoryStats `json:"memory"`
|
||||
Mount map[string]*MountInfo `json:"mount"`
|
||||
Bandwidth BandwidthStats `json:"bandwidth"`
|
||||
Version VersionResponse `json:"version"`
|
||||
}
|
||||
|
||||
// GetStats retrieves statistics from the rclone RC server
|
||||
func (m *Manager) GetStats() (*Stats, error) {
|
||||
stats := &Stats{}
|
||||
stats.Ready = m.IsReady()
|
||||
stats.Enabled = true
|
||||
|
||||
coreStats, err := m.GetCoreStats()
|
||||
if err == nil {
|
||||
stats.Core = *coreStats
|
||||
}
|
||||
|
||||
// Get memory usage
|
||||
memStats, err := m.GetMemoryUsage()
|
||||
if err == nil {
|
||||
stats.Memory = *memStats
|
||||
}
|
||||
// Get bandwidth stats
|
||||
bwStats, err := m.GetBandwidthStats()
|
||||
if err == nil && bwStats != nil {
|
||||
stats.Bandwidth = *bwStats
|
||||
} else {
|
||||
fmt.Println("Failed to get rclone stats", err)
|
||||
}
|
||||
|
||||
// Get version info
|
||||
versionResp, err := m.GetVersion()
|
||||
if err == nil {
|
||||
stats.Version = *versionResp
|
||||
}
|
||||
|
||||
// Get mount info
|
||||
stats.Mount = m.GetAllMounts()
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (m *Manager) GetCoreStats() (*CoreStatsResponse, error) {
|
||||
if !m.IsReady() {
|
||||
return nil, fmt.Errorf("rclone RC server not ready")
|
||||
}
|
||||
|
||||
req := RCRequest{
|
||||
Command: "core/stats",
|
||||
}
|
||||
|
||||
resp, err := m.makeRequest(req, false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get core stats: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var coreStats CoreStatsResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&coreStats); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode core stats response: %w", err)
|
||||
}
|
||||
return &coreStats, nil
|
||||
}
|
||||
|
||||
// GetMemoryUsage returns memory usage statistics
|
||||
func (m *Manager) GetMemoryUsage() (*MemoryStats, error) {
|
||||
if !m.IsReady() {
|
||||
return nil, fmt.Errorf("rclone RC server not ready")
|
||||
}
|
||||
|
||||
req := RCRequest{
|
||||
Command: "core/memstats",
|
||||
}
|
||||
|
||||
resp, err := m.makeRequest(req, false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get memory stats: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var memStats MemoryStats
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&memStats); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode memory stats response: %w", err)
|
||||
}
|
||||
return &memStats, nil
|
||||
}
|
||||
|
||||
// GetBandwidthStats returns bandwidth usage for all transfers
|
||||
func (m *Manager) GetBandwidthStats() (*BandwidthStats, error) {
|
||||
if !m.IsReady() {
|
||||
return nil, fmt.Errorf("rclone RC server not ready")
|
||||
}
|
||||
|
||||
req := RCRequest{
|
||||
Command: "core/bwlimit",
|
||||
}
|
||||
|
||||
resp, err := m.makeRequest(req, false)
|
||||
if err != nil {
|
||||
// Bandwidth stats might not be available, return empty
|
||||
return nil, nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var bwStats BandwidthStats
|
||||
if err := json.NewDecoder(resp.Body).Decode(&bwStats); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode bandwidth stats response: %w", err)
|
||||
}
|
||||
return &bwStats, nil
|
||||
}
|
||||
|
||||
// GetVersion returns rclone version information
|
||||
func (m *Manager) GetVersion() (*VersionResponse, error) {
|
||||
if !m.IsReady() {
|
||||
return nil, fmt.Errorf("rclone RC server not ready")
|
||||
}
|
||||
|
||||
req := RCRequest{
|
||||
Command: "core/version",
|
||||
}
|
||||
|
||||
resp, err := m.makeRequest(req, false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get version: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var versionResp VersionResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&versionResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode version response: %w", err)
|
||||
}
|
||||
return &versionResp, nil
|
||||
}
|
||||
@@ -214,7 +214,6 @@ func (r *Repair) newJob(arrsNames []string, mediaIDs []string) *Job {
|
||||
}
|
||||
}
|
||||
|
||||
// initRun initializes the repair run, setting up necessary configurations, checks and caches
|
||||
func (r *Repair) initRun(ctx context.Context) {
|
||||
if r.useWebdav {
|
||||
// Webdav use is enabled, initialize debrid torrent caches
|
||||
|
||||
171
pkg/sabnzbd/config.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package sabnzbd
|
||||
|
||||
// ConfigResponse represents configuration response
|
||||
type ConfigResponse struct {
|
||||
Config *Config `json:"config"`
|
||||
}
|
||||
|
||||
type ConfigNewzbin struct {
|
||||
Username string `json:"username"`
|
||||
BookmarkRate int `json:"bookmark_rate"`
|
||||
Url string `json:"url"`
|
||||
Bookmarks int `json:"bookmarks"`
|
||||
Password string `json:"password"`
|
||||
Unbookmark int `json:"unbookmark"`
|
||||
}
|
||||
|
||||
// Category represents a SABnzbd category
|
||||
type Category struct {
|
||||
Name string `json:"name"`
|
||||
Order int `json:"order"`
|
||||
Pp string `json:"pp"`
|
||||
Script string `json:"script"`
|
||||
Dir string `json:"dir"`
|
||||
NewzBin string `json:"newzbin"`
|
||||
Priority string `json:"priority"`
|
||||
}
|
||||
|
||||
// Server represents a usenet server
|
||||
type Server struct {
|
||||
Name string `json:"name"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Connections int `json:"connections"`
|
||||
Retention int `json:"retention"`
|
||||
Priority int `json:"priority"`
|
||||
SSL bool `json:"ssl"`
|
||||
Optional bool `json:"optional"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Misc MiscConfig `json:"misc"`
|
||||
Categories []Category `json:"categories"`
|
||||
Servers []Server `json:"servers"`
|
||||
}
|
||||
|
||||
type MiscConfig struct {
|
||||
// Directory Configuration
|
||||
CompleteDir string `json:"complete_dir"`
|
||||
DownloadDir string `json:"download_dir"`
|
||||
AdminDir string `json:"admin_dir"`
|
||||
NzbBackupDir string `json:"nzb_backup_dir"`
|
||||
ScriptDir string `json:"script_dir"`
|
||||
EmailDir string `json:"email_dir"`
|
||||
WebDir string `json:"web_dir"`
|
||||
|
||||
// Processing Options
|
||||
ParOption string `json:"par_option"`
|
||||
ParOptionConvert string `json:"par_option_convert"`
|
||||
ParOptionDuplicate string `json:"par_option_duplicate"`
|
||||
DirectUnpack string `json:"direct_unpack"`
|
||||
FlatUnpack string `json:"flat_unpack"`
|
||||
EnableRecursiveUnpack string `json:"enable_recursive_unpack"`
|
||||
OverwriteFiles string `json:"overwrite_files"`
|
||||
IgnoreWrongUnrar string `json:"ignore_wrong_unrar"`
|
||||
IgnoreUnrarDates string `json:"ignore_unrar_dates"`
|
||||
PreCheck string `json:"pre_check"`
|
||||
|
||||
// File Handling
|
||||
Permissions string `json:"permissions"`
|
||||
FolderRename string `json:"folder_rename"`
|
||||
FileRename string `json:"file_rename"`
|
||||
ReplaceIllegal string `json:"replace_illegal"`
|
||||
ReplaceDots string `json:"replace_dots"`
|
||||
ReplaceSpaces string `json:"replace_spaces"`
|
||||
SanitizeSafe string `json:"sanitize_safe"`
|
||||
IgnoreSamples string `json:"ignore_samples"`
|
||||
UnwantedExtensions []string `json:"unwanted_extensions"`
|
||||
ActionOnUnwanted string `json:"action_on_unwanted"`
|
||||
ActionOnDuplicate string `json:"action_on_duplicate"`
|
||||
BackupForDuplicates string `json:"backup_for_duplicates"`
|
||||
CleanupList []string `json:"cleanup_list"`
|
||||
DeobfuscateFinalFilenames string `json:"deobfuscate_final_filenames"`
|
||||
|
||||
// Scripts and Processing
|
||||
PreScript string `json:"pre_script"`
|
||||
PostScript string `json:"post_script"`
|
||||
EmptyPostproc string `json:"empty_postproc"`
|
||||
PauseOnPostProcessing string `json:"pause_on_post_processing"`
|
||||
|
||||
// System Resources
|
||||
Nice string `json:"nice"`
|
||||
NiceUnpack string `json:"nice_unpack"`
|
||||
Ionice string `json:"ionice"`
|
||||
Fsync string `json:"fsync"`
|
||||
|
||||
// Bandwidth and Performance
|
||||
BandwidthMax string `json:"bandwidth_max"`
|
||||
BandwidthPerc string `json:"bandwidth_perc"`
|
||||
RefreshRate string `json:"refresh_rate"`
|
||||
DirscanSpeed string `json:"dirscan_speed"`
|
||||
FolderMaxLength string `json:"folder_max_length"`
|
||||
PropagationDelay string `json:"propagation_delay"`
|
||||
|
||||
// Storage Management
|
||||
DownloadFree string `json:"download_free"`
|
||||
CompleteFree string `json:"complete_free"`
|
||||
|
||||
// Queue Management
|
||||
QueueComplete string `json:"queue_complete"`
|
||||
QueueCompletePers string `json:"queue_complete_pers"`
|
||||
AutoSort string `json:"auto_sort"`
|
||||
NewNzbOnFailure string `json:"new_nzb_on_failure"`
|
||||
PauseOnPwrar string `json:"pause_on_pwrar"`
|
||||
WarnedOldQueue string `json:"warned_old_queue"`
|
||||
|
||||
// Web Interface
|
||||
WebHost string `json:"web_host"`
|
||||
WebPort string `json:"web_port"`
|
||||
WebUsername string `json:"web_username"`
|
||||
WebPassword string `json:"web_password"`
|
||||
WebColor string `json:"web_color"`
|
||||
WebColor2 string `json:"web_color2"`
|
||||
AutoBrowser string `json:"auto_browser"`
|
||||
Autobrowser string `json:"autobrowser"` // Duplicate field - may need to resolve
|
||||
|
||||
// HTTPS Configuration
|
||||
EnableHTTPS string `json:"enable_https"`
|
||||
EnableHTTPSVerification string `json:"enable_https_verification"`
|
||||
HTTPSPort string `json:"https_port"`
|
||||
HTTPSCert string `json:"https_cert"`
|
||||
HTTPSKey string `json:"https_key"`
|
||||
HTTPSChain string `json:"https_chain"`
|
||||
|
||||
// Security and API
|
||||
APIKey string `json:"api_key"`
|
||||
NzbKey string `json:"nzb_key"`
|
||||
HostWhitelist string `json:"host_whitelist"`
|
||||
LocalRanges []string `json:"local_ranges"`
|
||||
InetExposure string `json:"inet_exposure"`
|
||||
APILogging string `json:"api_logging"`
|
||||
APIWarnings string `json:"api_warnings"`
|
||||
|
||||
// Logging
|
||||
LogLevel string `json:"log_level"`
|
||||
LogSize string `json:"log_size"`
|
||||
MaxLogSize string `json:"max_log_size"`
|
||||
LogBackups string `json:"log_backups"`
|
||||
LogNew string `json:"log_new"`
|
||||
|
||||
// Notifications
|
||||
MatrixUsername string `json:"matrix_username"`
|
||||
MatrixPassword string `json:"matrix_password"`
|
||||
MatrixServer string `json:"matrix_server"`
|
||||
MatrixRoom string `json:"matrix_room"`
|
||||
|
||||
// Miscellaneous
|
||||
ConfigLock string `json:"config_lock"`
|
||||
Language string `json:"language"`
|
||||
CheckNewRel string `json:"check_new_rel"`
|
||||
RSSFilenames string `json:"rss_filenames"`
|
||||
IPv6Hosting string `json:"ipv6_hosting"`
|
||||
EnableBonjour string `json:"enable_bonjour"`
|
||||
Cherryhost string `json:"cherryhost"`
|
||||
WinMenu string `json:"win_menu"`
|
||||
AMPM string `json:"ampm"`
|
||||
NotifiedNewSkin string `json:"notified_new_skin"`
|
||||
HelpURI string `json:"helpuri"`
|
||||
SSDURI string `json:"ssduri"`
|
||||
}
|
||||
121
pkg/sabnzbd/context.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package sabnzbd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
"github.com/sirrobot01/decypharr/pkg/store"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/sirrobot01/decypharr/pkg/arr"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const (
|
||||
apiKeyKey contextKey = "apikey"
|
||||
modeKey contextKey = "mode"
|
||||
arrKey contextKey = "arr"
|
||||
categoryKey contextKey = "category"
|
||||
)
|
||||
|
||||
func getMode(ctx context.Context) string {
|
||||
if mode, ok := ctx.Value(modeKey).(string); ok {
|
||||
return mode
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s *SABnzbd) categoryContext(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
category := r.URL.Query().Get("category")
|
||||
if category == "" {
|
||||
// Check form data
|
||||
_ = r.ParseForm()
|
||||
category = r.Form.Get("category")
|
||||
}
|
||||
if category == "" {
|
||||
category = r.FormValue("category")
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), categoryKey, strings.TrimSpace(category))
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func getArrFromContext(ctx context.Context) *arr.Arr {
|
||||
if a, ok := ctx.Value(arrKey).(*arr.Arr); ok {
|
||||
return a
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getCategory(ctx context.Context) string {
|
||||
if category, ok := ctx.Value(categoryKey).(string); ok {
|
||||
return category
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// modeContext extracts the mode parameter from the request
|
||||
func (s *SABnzbd) modeContext(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mode := r.URL.Query().Get("mode")
|
||||
if mode == "" {
|
||||
// Check form data
|
||||
_ = r.ParseForm()
|
||||
mode = r.Form.Get("mode")
|
||||
}
|
||||
|
||||
// Extract category for Arr integration
|
||||
category := r.URL.Query().Get("cat")
|
||||
if category == "" {
|
||||
category = r.Form.Get("cat")
|
||||
}
|
||||
|
||||
// Create a default Arr instance for the category
|
||||
downloadUncached := false
|
||||
a := arr.New(category, "", "", false, false, &downloadUncached, "", "auto")
|
||||
|
||||
ctx := context.WithValue(r.Context(), modeKey, strings.TrimSpace(mode))
|
||||
ctx = context.WithValue(ctx, arrKey, a)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
// authContext creates a middleware that extracts the Arr host and token from the Authorization header
|
||||
// and adds it to the request context.
|
||||
// This is used to identify the Arr instance for the request.
|
||||
// Only a valid host and token will be added to the context/config. The rest are manual
|
||||
func (s *SABnzbd) authContext(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
host := r.FormValue("ma_username")
|
||||
token := r.FormValue("ma_password")
|
||||
category := getCategory(r.Context())
|
||||
arrs := store.Get().Arr()
|
||||
// Check if arr exists
|
||||
a := arrs.Get(category)
|
||||
if a == nil {
|
||||
// Arr is not configured, create a new one
|
||||
downloadUncached := false
|
||||
a = arr.New(category, "", "", false, false, &downloadUncached, "", "auto")
|
||||
}
|
||||
host = strings.TrimSpace(host)
|
||||
if host != "" {
|
||||
a.Host = host
|
||||
}
|
||||
token = strings.TrimSpace(token)
|
||||
if token != "" {
|
||||
a.Token = token
|
||||
}
|
||||
a.Source = "auto"
|
||||
if err := utils.ValidateServiceURL(a.Host); err != nil {
|
||||
// Return silently, no need to raise a problem. Just do not add the Arr to the context/config.json
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
arrs.AddOrUpdate(a)
|
||||
ctx := context.WithValue(r.Context(), arrKey, a)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
476
pkg/sabnzbd/handlers.go
Normal file
@@ -0,0 +1,476 @@
|
||||
package sabnzbd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/sirrobot01/decypharr/internal/request"
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
"github.com/sirrobot01/decypharr/pkg/arr"
|
||||
"github.com/sirrobot01/decypharr/pkg/usenet"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// handleAPI is the main handler for all SABnzbd API requests
|
||||
func (s *SABnzbd) handleAPI(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
mode := getMode(ctx)
|
||||
|
||||
switch mode {
|
||||
case ModeQueue:
|
||||
s.handleQueue(w, r)
|
||||
case ModeHistory:
|
||||
s.handleHistory(w, r)
|
||||
case ModeConfig:
|
||||
s.handleConfig(w, r)
|
||||
case ModeStatus, ModeFullStatus:
|
||||
s.handleStatus(w, r)
|
||||
case ModeGetConfig:
|
||||
s.handleConfig(w, r)
|
||||
case ModeAddURL:
|
||||
s.handleAddURL(w, r)
|
||||
case ModeAddFile:
|
||||
s.handleAddFile(w, r)
|
||||
case ModeVersion:
|
||||
s.handleVersion(w, r)
|
||||
case ModeGetCats:
|
||||
s.handleGetCategories(w, r)
|
||||
case ModeGetScripts:
|
||||
s.handleGetScripts(w, r)
|
||||
case ModeGetFiles:
|
||||
s.handleGetFiles(w, r)
|
||||
default:
|
||||
// Default to queue if no mode specified
|
||||
s.logger.Warn().Str("mode", mode).Msg("Unknown API mode, returning 404")
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SABnzbd) handleQueue(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.FormValue("name")
|
||||
if name == "" {
|
||||
s.handleListQueue(w, r)
|
||||
return
|
||||
}
|
||||
name = strings.ToLower(strings.TrimSpace(name))
|
||||
switch name {
|
||||
case "delete":
|
||||
s.handleQueueDelete(w, r)
|
||||
case "pause":
|
||||
s.handleQueuePause(w, r)
|
||||
case "resume":
|
||||
s.handleQueueResume(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// handleResume handles resume operations
|
||||
func (s *SABnzbd) handleQueueResume(w http.ResponseWriter, r *http.Request) {
|
||||
response := StatusResponse{Status: true}
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleDelete handles delete operations
|
||||
func (s *SABnzbd) handleQueueDelete(w http.ResponseWriter, r *http.Request) {
|
||||
nzoIDs := r.FormValue("value")
|
||||
if nzoIDs == "" {
|
||||
s.writeError(w, "No NZB IDs provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var successCount int
|
||||
var errors []string
|
||||
|
||||
for _, nzoID := range strings.Split(nzoIDs, ",") {
|
||||
nzoID = strings.TrimSpace(nzoID)
|
||||
if nzoID == "" {
|
||||
continue // Skip empty IDs
|
||||
}
|
||||
|
||||
s.logger.Info().Str("nzo_id", nzoID).Msg("Deleting NZB")
|
||||
|
||||
// Use atomic delete operation
|
||||
if err := s.usenet.Store().AtomicDelete(nzoID); err != nil {
|
||||
s.logger.Error().
|
||||
Err(err).
|
||||
Str("nzo_id", nzoID).
|
||||
Msg("Failed to delete NZB")
|
||||
errors = append(errors, fmt.Sprintf("Failed to delete %s: %v", nzoID, err))
|
||||
} else {
|
||||
successCount++
|
||||
}
|
||||
}
|
||||
|
||||
// Return response with success/error information
|
||||
if len(errors) > 0 {
|
||||
if successCount == 0 {
|
||||
// All deletions failed
|
||||
s.writeError(w, fmt.Sprintf("All deletions failed: %s", strings.Join(errors, "; ")), http.StatusInternalServerError)
|
||||
return
|
||||
} else {
|
||||
// Partial success
|
||||
s.logger.Warn().
|
||||
Int("success_count", successCount).
|
||||
Int("error_count", len(errors)).
|
||||
Strs("errors", errors).
|
||||
Msg("Partial success in queue deletion")
|
||||
}
|
||||
}
|
||||
|
||||
response := StatusResponse{
|
||||
Status: true,
|
||||
Error: "", // Could add error details here if needed
|
||||
}
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handlePause handles pause operations
|
||||
func (s *SABnzbd) handleQueuePause(w http.ResponseWriter, r *http.Request) {
|
||||
response := StatusResponse{Status: true}
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleQueue returns the current download queue
|
||||
func (s *SABnzbd) handleListQueue(w http.ResponseWriter, r *http.Request) {
|
||||
nzbs := s.usenet.Store().GetQueue()
|
||||
|
||||
queue := Queue{
|
||||
Version: Version,
|
||||
Slots: []QueueSlot{},
|
||||
}
|
||||
|
||||
// Convert NZBs to queue slots
|
||||
for _, nzb := range nzbs {
|
||||
if nzb.ETA <= 0 {
|
||||
nzb.ETA = 0 // Ensure ETA is non-negative
|
||||
}
|
||||
var timeLeft string
|
||||
if nzb.ETA == 0 {
|
||||
timeLeft = "00:00:00" // If ETA is 0, set TimeLeft to "00:00:00"
|
||||
} else {
|
||||
// Convert ETA from seconds to "HH:MM:SS" format
|
||||
duration := time.Duration(nzb.ETA) * time.Second
|
||||
timeLeft = duration.String()
|
||||
}
|
||||
slot := QueueSlot{
|
||||
Status: s.mapNZBStatus(nzb.Status),
|
||||
Mb: nzb.TotalSize,
|
||||
Filename: nzb.Name,
|
||||
Cat: nzb.Category,
|
||||
MBLeft: 0,
|
||||
Percentage: nzb.Percentage,
|
||||
NzoId: nzb.ID,
|
||||
Size: nzb.TotalSize,
|
||||
TimeLeft: timeLeft, // This is in "00:00:00" format
|
||||
}
|
||||
queue.Slots = append(queue.Slots, slot)
|
||||
}
|
||||
|
||||
response := QueueResponse{
|
||||
Queue: queue,
|
||||
Status: true,
|
||||
Version: Version,
|
||||
}
|
||||
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleHistory returns the download history
|
||||
func (s *SABnzbd) handleHistory(w http.ResponseWriter, r *http.Request) {
|
||||
limitStr := r.FormValue("limit")
|
||||
if limitStr == "" {
|
||||
limitStr = "0"
|
||||
}
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Msg("Invalid limit parameter for history")
|
||||
s.writeError(w, "Invalid limit parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if limit < 0 {
|
||||
limit = 0
|
||||
}
|
||||
history := s.getHistory(r.Context(), limit)
|
||||
|
||||
response := HistoryResponse{
|
||||
History: history,
|
||||
}
|
||||
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleConfig returns the configuration
|
||||
func (s *SABnzbd) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
response := ConfigResponse{
|
||||
Config: s.config,
|
||||
}
|
||||
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleAddURL handles adding NZB by URL
|
||||
func (s *SABnzbd) handleAddURL(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
_arr := getArrFromContext(ctx)
|
||||
cat := getCategory(ctx)
|
||||
|
||||
if _arr == nil {
|
||||
// If Arr is not in context, create a new one with default values
|
||||
_arr = arr.New(cat, "", "", false, false, nil, "", "")
|
||||
}
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
s.logger.Warn().Str("method", r.Method).Msg("Invalid method")
|
||||
s.writeError(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
url := r.FormValue("name")
|
||||
action := r.FormValue("action")
|
||||
downloadDir := r.FormValue("download_dir")
|
||||
if action == "" {
|
||||
action = "symlink"
|
||||
}
|
||||
if downloadDir == "" {
|
||||
downloadDir = s.config.Misc.DownloadDir
|
||||
}
|
||||
|
||||
if url == "" {
|
||||
s.writeError(w, "URL is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
nzoID, err := s.addNZBURL(ctx, url, _arr, action, downloadDir)
|
||||
if err != nil {
|
||||
s.writeError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if nzoID == "" {
|
||||
s.writeError(w, "Failed to add NZB", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response := AddNZBResponse{
|
||||
Status: true,
|
||||
NzoIds: []string{nzoID},
|
||||
}
|
||||
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleAddFile handles NZB file uploads
|
||||
func (s *SABnzbd) handleAddFile(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
_arr := getArrFromContext(ctx)
|
||||
cat := getCategory(ctx)
|
||||
|
||||
if _arr == nil {
|
||||
// If Arr is not in context, create a new one with default values
|
||||
_arr = arr.New(cat, "", "", false, false, nil, "", "")
|
||||
}
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
s.writeError(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse multipart form
|
||||
err := r.ParseMultipartForm(32 << 20) // 32 MB limit
|
||||
if err != nil {
|
||||
s.writeError(w, "Failed to parse multipart form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("name")
|
||||
if err != nil {
|
||||
s.writeError(w, "No file uploaded", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Read file content
|
||||
content, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
s.writeError(w, "Failed to read file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
action := r.FormValue("action")
|
||||
downloadDir := r.FormValue("download_dir")
|
||||
if action == "" {
|
||||
action = "symlink"
|
||||
}
|
||||
if downloadDir == "" {
|
||||
downloadDir = s.config.Misc.DownloadDir
|
||||
}
|
||||
|
||||
// Process NZB file
|
||||
nzbID, err := s.addNZBFile(ctx, content, header.Filename, _arr, action, downloadDir)
|
||||
if err != nil {
|
||||
s.writeError(w, fmt.Sprintf("Failed to add NZB file: %s", err.Error()), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if nzbID == "" {
|
||||
s.writeError(w, "Failed to add NZB file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response := AddNZBResponse{
|
||||
Status: true,
|
||||
NzoIds: []string{nzbID},
|
||||
}
|
||||
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleVersion returns version information
|
||||
func (s *SABnzbd) handleVersion(w http.ResponseWriter, r *http.Request) {
|
||||
response := VersionResponse{
|
||||
Version: Version,
|
||||
}
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleGetCategories returns available categories
|
||||
func (s *SABnzbd) handleGetCategories(w http.ResponseWriter, r *http.Request) {
|
||||
categories := s.getCategories()
|
||||
request.JSONResponse(w, categories, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleGetScripts returns available scripts
|
||||
func (s *SABnzbd) handleGetScripts(w http.ResponseWriter, r *http.Request) {
|
||||
scripts := []string{"None"}
|
||||
request.JSONResponse(w, scripts, http.StatusOK)
|
||||
}
|
||||
|
||||
// handleGetFiles returns files for a specific NZB
|
||||
func (s *SABnzbd) handleGetFiles(w http.ResponseWriter, r *http.Request) {
|
||||
nzoID := r.FormValue("value")
|
||||
var files []string
|
||||
|
||||
if nzoID != "" {
|
||||
nzb := s.usenet.Store().Get(nzoID)
|
||||
if nzb != nil {
|
||||
for _, file := range nzb.Files {
|
||||
files = append(files, file.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
request.JSONResponse(w, files, http.StatusOK)
|
||||
}
|
||||
|
||||
func (s *SABnzbd) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
type status struct {
|
||||
CompletedDir string `json:"completed_dir"`
|
||||
}
|
||||
response := struct {
|
||||
Status status `json:"status"`
|
||||
}{
|
||||
Status: status{
|
||||
CompletedDir: s.config.Misc.DownloadDir,
|
||||
},
|
||||
}
|
||||
request.JSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
|
||||
func (s *SABnzbd) getHistory(ctx context.Context, limit int) History {
|
||||
cat := getCategory(ctx)
|
||||
items := s.usenet.Store().GetHistory(cat, limit)
|
||||
slots := make([]HistorySlot, 0, len(items))
|
||||
history := History{
|
||||
Version: Version,
|
||||
Paused: false,
|
||||
}
|
||||
for _, item := range items {
|
||||
slot := HistorySlot{
|
||||
Status: s.mapNZBStatus(item.Status),
|
||||
Name: item.Name,
|
||||
NZBName: item.Name,
|
||||
NzoId: item.ID,
|
||||
Category: item.Category,
|
||||
FailMessage: item.FailMessage,
|
||||
Bytes: item.TotalSize,
|
||||
Storage: item.Storage,
|
||||
}
|
||||
slots = append(slots, slot)
|
||||
}
|
||||
history.Slots = slots
|
||||
return history
|
||||
}
|
||||
|
||||
func (s *SABnzbd) writeError(w http.ResponseWriter, message string, status int) {
|
||||
response := StatusResponse{
|
||||
Status: false,
|
||||
Error: message,
|
||||
}
|
||||
request.JSONResponse(w, response, status)
|
||||
}
|
||||
|
||||
func (s *SABnzbd) mapNZBStatus(status string) string {
|
||||
switch status {
|
||||
case "downloading":
|
||||
return StatusDownloading
|
||||
case "completed":
|
||||
return StatusCompleted
|
||||
case "paused":
|
||||
return StatusPaused
|
||||
case "error", "failed":
|
||||
return StatusFailed
|
||||
case "processing":
|
||||
return StatusProcessing
|
||||
case "verifying":
|
||||
return StatusVerifying
|
||||
case "repairing":
|
||||
return StatusRepairing
|
||||
case "extracting":
|
||||
return StatusExtracting
|
||||
case "moving":
|
||||
return StatusMoving
|
||||
case "running":
|
||||
return StatusRunning
|
||||
default:
|
||||
return StatusQueued
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SABnzbd) addNZBURL(ctx context.Context, url string, arr *arr.Arr, action, downloadDir string) (string, error) {
|
||||
if url == "" {
|
||||
return "", fmt.Errorf("URL is required")
|
||||
}
|
||||
// Download NZB content
|
||||
filename, content, err := utils.DownloadFile(url)
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Str("url", url).Msg("Failed to download NZB from URL")
|
||||
return "", fmt.Errorf("failed to download NZB from URL: %w", err)
|
||||
}
|
||||
|
||||
if len(content) == 0 {
|
||||
s.logger.Warn().Str("url", url).Msg("Downloaded content is empty")
|
||||
return "", fmt.Errorf("downloaded content is empty")
|
||||
}
|
||||
return s.addNZBFile(ctx, content, filename, arr, action, downloadDir)
|
||||
}
|
||||
|
||||
func (s *SABnzbd) addNZBFile(ctx context.Context, content []byte, filename string, arr *arr.Arr, action, downloadDir string) (string, error) {
|
||||
if s.usenet == nil {
|
||||
return "", fmt.Errorf("store not initialized")
|
||||
}
|
||||
req := &usenet.ProcessRequest{
|
||||
NZBContent: content,
|
||||
Name: filename,
|
||||
Arr: arr,
|
||||
Action: action,
|
||||
DownloadDir: downloadDir,
|
||||
}
|
||||
nzb, err := s.usenet.ProcessNZB(ctx, req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to process NZB: %w", err)
|
||||
}
|
||||
return nzb.ID, nil
|
||||
}
|
||||
24
pkg/sabnzbd/routes.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package sabnzbd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func (s *SABnzbd) Routes() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(s.categoryContext)
|
||||
r.Use(s.authContext)
|
||||
|
||||
// SABnzbd API endpoints - all under /api with mode parameter
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
r.Use(s.modeContext)
|
||||
|
||||
// Queue operations
|
||||
r.Get("/", s.handleAPI)
|
||||
r.Post("/", s.handleAPI)
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
||||
116
pkg/sabnzbd/sabnzbd.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package sabnzbd
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/internal/logger"
|
||||
"github.com/sirrobot01/decypharr/pkg/store"
|
||||
"github.com/sirrobot01/decypharr/pkg/usenet"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type SABnzbd struct {
|
||||
downloadFolder string
|
||||
config *Config
|
||||
refreshInterval int
|
||||
logger zerolog.Logger
|
||||
usenet usenet.Usenet
|
||||
defaultCategories []string
|
||||
}
|
||||
|
||||
func New(usenetClient usenet.Usenet) *SABnzbd {
|
||||
_cfg := config.Get()
|
||||
cfg := _cfg.SABnzbd
|
||||
var defaultCategories []string
|
||||
for _, cat := range _cfg.SABnzbd.Categories {
|
||||
if cat != "" {
|
||||
defaultCategories = append(defaultCategories, cat)
|
||||
}
|
||||
}
|
||||
sb := &SABnzbd{
|
||||
downloadFolder: cfg.DownloadFolder,
|
||||
refreshInterval: cfg.RefreshInterval,
|
||||
logger: logger.New("sabnzbd"),
|
||||
usenet: usenetClient,
|
||||
defaultCategories: defaultCategories,
|
||||
}
|
||||
sb.SetConfig(_cfg)
|
||||
return sb
|
||||
}
|
||||
|
||||
func (s *SABnzbd) SetConfig(cfg *config.Config) {
|
||||
sabnzbdConfig := &Config{
|
||||
Misc: MiscConfig{
|
||||
CompleteDir: s.downloadFolder,
|
||||
DownloadDir: s.downloadFolder,
|
||||
AdminDir: s.downloadFolder,
|
||||
WebPort: cfg.Port,
|
||||
Language: "en",
|
||||
RefreshRate: "1",
|
||||
QueueComplete: "0",
|
||||
ConfigLock: "0",
|
||||
Autobrowser: "1",
|
||||
CheckNewRel: "1",
|
||||
},
|
||||
Categories: s.getCategories(),
|
||||
}
|
||||
if cfg.Usenet != nil || len(cfg.Usenet.Providers) == 0 {
|
||||
for _, provider := range cfg.Usenet.Providers {
|
||||
if provider.Host == "" || provider.Port == 0 {
|
||||
continue
|
||||
}
|
||||
sabnzbdConfig.Servers = append(sabnzbdConfig.Servers, Server{
|
||||
Name: provider.Name,
|
||||
Host: provider.Host,
|
||||
Port: provider.Port,
|
||||
Username: provider.Username,
|
||||
Password: provider.Password,
|
||||
Connections: provider.Connections,
|
||||
SSL: provider.SSL,
|
||||
})
|
||||
}
|
||||
}
|
||||
s.config = sabnzbdConfig
|
||||
}
|
||||
|
||||
func (s *SABnzbd) getCategories() []Category {
|
||||
_store := store.Get()
|
||||
arrs := _store.Arr().GetAll()
|
||||
categories := make([]Category, 0, len(arrs))
|
||||
added := map[string]struct{}{}
|
||||
|
||||
for i, a := range arrs {
|
||||
if _, ok := added[a.Name]; ok {
|
||||
continue // Skip if category already added
|
||||
}
|
||||
categories = append(categories, Category{
|
||||
Name: a.Name,
|
||||
Order: i + 1,
|
||||
Pp: "3",
|
||||
Script: "None",
|
||||
Dir: filepath.Join(s.downloadFolder, a.Name),
|
||||
Priority: PriorityNormal,
|
||||
})
|
||||
}
|
||||
|
||||
// Add default categories if not already present
|
||||
for _, defaultCat := range s.defaultCategories {
|
||||
if _, ok := added[defaultCat]; ok {
|
||||
continue // Skip if default category already added
|
||||
}
|
||||
categories = append(categories, Category{
|
||||
Name: defaultCat,
|
||||
Order: len(categories) + 1,
|
||||
Pp: "3",
|
||||
Script: "None",
|
||||
Dir: filepath.Join(s.downloadFolder, defaultCat),
|
||||
Priority: PriorityNormal,
|
||||
})
|
||||
added[defaultCat] = struct{}{}
|
||||
}
|
||||
|
||||
return categories
|
||||
}
|
||||
|
||||
func (s *SABnzbd) Reset() {
|
||||
}
|
||||
150
pkg/sabnzbd/types.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package sabnzbd
|
||||
|
||||
// SABnzbd API response types based on official documentation
|
||||
|
||||
var (
|
||||
Version = "4.5.0"
|
||||
)
|
||||
|
||||
// QueueResponse represents the queue status response
|
||||
type QueueResponse struct {
|
||||
Queue Queue `json:"queue"`
|
||||
Status bool `json:"status"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// Queue represents the download queue
|
||||
type Queue struct {
|
||||
Version string `json:"version"`
|
||||
Slots []QueueSlot `json:"slots"`
|
||||
}
|
||||
|
||||
// QueueSlot represents a download in the queue
|
||||
type QueueSlot struct {
|
||||
Status string `json:"status"`
|
||||
TimeLeft string `json:"timeleft"`
|
||||
Mb int64 `json:"mb"`
|
||||
Filename string `json:"filename"`
|
||||
Priority string `json:"priority"`
|
||||
Cat string `json:"cat"`
|
||||
MBLeft int64 `json:"mbleft"`
|
||||
Percentage float64 `json:"percentage"`
|
||||
NzoId string `json:"nzo_id"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// HistoryResponse represents the history response
|
||||
type HistoryResponse struct {
|
||||
History History `json:"history"`
|
||||
}
|
||||
|
||||
// History represents the download history
|
||||
type History struct {
|
||||
Version string `json:"version"`
|
||||
Paused bool `json:"paused"`
|
||||
Slots []HistorySlot `json:"slots"`
|
||||
}
|
||||
|
||||
// HistorySlot represents a completed download
|
||||
type HistorySlot struct {
|
||||
Status string `json:"status"`
|
||||
Name string `json:"name"`
|
||||
NZBName string `json:"nzb_name"`
|
||||
NzoId string `json:"nzo_id"`
|
||||
Category string `json:"category"`
|
||||
FailMessage string `json:"fail_message"`
|
||||
Bytes int64 `json:"bytes"`
|
||||
Storage string `json:"storage"`
|
||||
}
|
||||
|
||||
// StageLog represents processing stages
|
||||
type StageLog struct {
|
||||
Name string `json:"name"`
|
||||
Actions []string `json:"actions"`
|
||||
}
|
||||
|
||||
// VersionResponse represents version information
|
||||
type VersionResponse struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// StatusResponse represents general status
|
||||
type StatusResponse struct {
|
||||
Status bool `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// FullStatusResponse represents the full status response with queue and history
|
||||
type FullStatusResponse struct {
|
||||
Queue Queue `json:"queue"`
|
||||
History History `json:"history"`
|
||||
Status bool `json:"status"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// AddNZBRequest represents the request to add an NZB
|
||||
type AddNZBRequest struct {
|
||||
Name string `json:"name"`
|
||||
Cat string `json:"cat"`
|
||||
Script string `json:"script"`
|
||||
Priority string `json:"priority"`
|
||||
PP string `json:"pp"`
|
||||
Password string `json:"password"`
|
||||
NZBData []byte `json:"nzb_data"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// AddNZBResponse represents the response when adding an NZB
|
||||
type AddNZBResponse struct {
|
||||
Status bool `json:"status"`
|
||||
NzoIds []string `json:"nzo_ids"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// API Mode constants
|
||||
const (
|
||||
ModeQueue = "queue"
|
||||
ModeHistory = "history"
|
||||
ModeConfig = "config"
|
||||
ModeGetConfig = "get_config"
|
||||
ModeAddURL = "addurl"
|
||||
ModeAddFile = "addfile"
|
||||
ModeVersion = "version"
|
||||
ModePause = "pause"
|
||||
ModeResume = "resume"
|
||||
ModeDelete = "delete"
|
||||
ModeShutdown = "shutdown"
|
||||
ModeRestart = "restart"
|
||||
ModeGetCats = "get_cats"
|
||||
ModeGetScripts = "get_scripts"
|
||||
ModeGetFiles = "get_files"
|
||||
ModeRetry = "retry"
|
||||
ModeStatus = "status"
|
||||
ModeFullStatus = "fullstatus"
|
||||
)
|
||||
|
||||
// Status constants
|
||||
const (
|
||||
StatusQueued = "Queued"
|
||||
StatusPaused = "Paused"
|
||||
StatusDownloading = "downloading"
|
||||
StatusProcessing = "Processing"
|
||||
StatusCompleted = "Completed"
|
||||
StatusFailed = "Failed"
|
||||
StatusGrabbing = "Grabbing"
|
||||
StatusPropagating = "Propagating"
|
||||
StatusVerifying = "Verifying"
|
||||
StatusRepairing = "Repairing"
|
||||
StatusExtracting = "Extracting"
|
||||
StatusMoving = "Moving"
|
||||
StatusRunning = "Running"
|
||||
)
|
||||
|
||||
// Priority constants
|
||||
const (
|
||||
PriorityForced = "2"
|
||||
PriorityHigh = "1"
|
||||
PriorityNormal = "0"
|
||||
PriorityLow = "-1"
|
||||
PriorityStop = "-2"
|
||||
)
|
||||
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/sirrobot01/decypharr/internal/nntp"
|
||||
"github.com/sirrobot01/decypharr/internal/request"
|
||||
debridTypes "github.com/sirrobot01/decypharr/pkg/debrid/types"
|
||||
"github.com/sirrobot01/decypharr/pkg/store"
|
||||
@@ -99,75 +100,40 @@ func (s *Server) handleStats(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
clients := debrids.Clients()
|
||||
caches := debrids.Caches()
|
||||
debridStats := make([]debridTypes.Stats, 0)
|
||||
profiles := make([]*debridTypes.Profile, 0)
|
||||
for debridName, client := range clients {
|
||||
debridStat := debridTypes.Stats{}
|
||||
libraryStat := debridTypes.LibraryStats{}
|
||||
profile, err := client.GetProfile()
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Str("debrid", debridName).Msg("Failed to get debrid profile")
|
||||
profile = &debridTypes.Profile{
|
||||
Name: debridName,
|
||||
}
|
||||
}
|
||||
profile.Name = debridName
|
||||
debridStat.Profile = profile
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to get debrid profile")
|
||||
continue
|
||||
}
|
||||
cache, ok := caches[debridName]
|
||||
if ok {
|
||||
// Get torrent data
|
||||
libraryStat.Total = cache.TotalTorrents()
|
||||
libraryStat.Bad = len(cache.GetListing("__bad__"))
|
||||
libraryStat.ActiveLinks = cache.GetTotalActiveDownloadLinks()
|
||||
profile.LibrarySize = cache.TotalTorrents()
|
||||
profile.BadTorrents = len(cache.GetListing("__bad__"))
|
||||
profile.ActiveLinks = cache.GetTotalActiveDownloadLinks()
|
||||
|
||||
}
|
||||
debridStat.Library = libraryStat
|
||||
|
||||
// Get detailed account information
|
||||
accounts := client.Accounts().All()
|
||||
accountDetails := make([]map[string]any, 0)
|
||||
for _, account := range accounts {
|
||||
// Mask token - show first 8 characters and last 4 characters
|
||||
maskedToken := ""
|
||||
if len(account.Token) > 12 {
|
||||
maskedToken = account.Token[:8] + "****" + account.Token[len(account.Token)-4:]
|
||||
} else if len(account.Token) > 8 {
|
||||
maskedToken = account.Token[:4] + "****" + account.Token[len(account.Token)-2:]
|
||||
} else {
|
||||
maskedToken = "****"
|
||||
}
|
||||
|
||||
accountDetail := map[string]any{
|
||||
"order": account.Order,
|
||||
"disabled": account.Disabled,
|
||||
"token_masked": maskedToken,
|
||||
"username": account.Username,
|
||||
"traffic_used": account.TrafficUsed,
|
||||
"links_count": account.LinksCount(),
|
||||
"debrid": account.Debrid,
|
||||
}
|
||||
accountDetails = append(accountDetails, accountDetail)
|
||||
}
|
||||
debridStat.Accounts = accountDetails
|
||||
debridStats = append(debridStats, debridStat)
|
||||
profiles = append(profiles, profile)
|
||||
}
|
||||
stats["debrids"] = debridStats
|
||||
stats["debrids"] = profiles
|
||||
|
||||
// Add rclone stats if available
|
||||
if rcManager := store.Get().RcloneManager(); rcManager != nil && rcManager.IsReady() {
|
||||
rcStats, err := rcManager.GetStats()
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to get rclone stats")
|
||||
stats["rclone"] = map[string]interface{}{
|
||||
"enabled": true,
|
||||
"server_ready": false,
|
||||
}
|
||||
} else {
|
||||
stats["rclone"] = rcStats
|
||||
}
|
||||
} else {
|
||||
stats["rclone"] = map[string]interface{}{
|
||||
"enabled": false,
|
||||
"server_ready": false,
|
||||
if s.usenet != nil {
|
||||
if client := s.usenet.Client(); client != nil {
|
||||
usenetsData := make([]map[string]interface{}, 0)
|
||||
client.Pools().Range(func(key string, value *nntp.Pool) bool {
|
||||
if value != nil {
|
||||
providerData := make(map[string]interface{})
|
||||
providerData["name"] = key
|
||||
providerData["active_connections"] = value.ActiveConnections()
|
||||
providerData["total_connections"] = value.ConnectionCount()
|
||||
usenetsData = append(usenetsData, providerData)
|
||||
}
|
||||
return true
|
||||
})
|
||||
stats["usenet"] = usenetsData
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,18 +9,19 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/internal/logger"
|
||||
"github.com/sirrobot01/decypharr/pkg/usenet"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
router *chi.Mux
|
||||
logger zerolog.Logger
|
||||
usenet usenet.Usenet
|
||||
}
|
||||
|
||||
func New(handlers map[string]http.Handler) *Server {
|
||||
func New(usenet usenet.Usenet, handlers map[string]http.Handler) *Server {
|
||||
l := logger.New("http")
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Recoverer)
|
||||
@@ -29,6 +30,7 @@ func New(handlers map[string]http.Handler) *Server {
|
||||
|
||||
s := &Server{
|
||||
logger: l,
|
||||
usenet: usenet,
|
||||
}
|
||||
|
||||
r.Route(cfg.URLBase, func(r chi.Router) {
|
||||
@@ -37,12 +39,11 @@ func New(handlers map[string]http.Handler) *Server {
|
||||
}
|
||||
|
||||
//logs
|
||||
r.Get("/logs", s.getLogs) // deprecated, use /debug/logs
|
||||
r.Get("/logs", s.getLogs)
|
||||
|
||||
//debugs
|
||||
r.Route("/debug", func(r chi.Router) {
|
||||
r.Get("/stats", s.handleStats)
|
||||
r.Get("/logs", s.getLogs)
|
||||
r.Get("/logs/rclone", s.getRcloneLogs)
|
||||
r.Get("/ingests", s.handleIngests)
|
||||
r.Get("/ingests/{debrid}", s.handleIngestsByDebrid)
|
||||
})
|
||||
@@ -77,7 +78,7 @@ func (s *Server) Start(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (s *Server) getLogs(w http.ResponseWriter, r *http.Request) {
|
||||
logFile := filepath.Join(logger.GetLogPath(), "decypharr.log")
|
||||
logFile := logger.GetLogPath()
|
||||
|
||||
// Open and read the file
|
||||
file, err := os.Open(logFile)
|
||||
@@ -100,42 +101,5 @@ func (s *Server) getLogs(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Expires", "0")
|
||||
|
||||
// Stream the file
|
||||
if _, err := io.Copy(w, file); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Error streaming log file")
|
||||
http.Error(w, "Error streaming log file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) getRcloneLogs(w http.ResponseWriter, r *http.Request) {
|
||||
// Rclone logs resides in the same directory as the application logs
|
||||
logFile := filepath.Join(logger.GetLogPath(), "rclone.log")
|
||||
// Open and read the file
|
||||
file, err := os.Open(logFile)
|
||||
if err != nil {
|
||||
http.Error(w, "Error reading log file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Msg("Error closing log file")
|
||||
return
|
||||
|
||||
}
|
||||
}(file)
|
||||
|
||||
// Set headers
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.Header().Set("Content-Disposition", "inline; filename=application.log")
|
||||
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
w.Header().Set("Expires", "0")
|
||||
|
||||
// Stream the file
|
||||
if _, err := io.Copy(w, file); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Error streaming log file")
|
||||
http.Error(w, "Error streaming log file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_, _ = io.Copy(w, file)
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ func (s *Store) downloadFiles(torrent *Torrent, debridTorrent *types.Torrent, pa
|
||||
func (s *Store) processSymlink(torrent *Torrent, debridTorrent *types.Torrent) (string, error) {
|
||||
files := debridTorrent.Files
|
||||
if len(files) == 0 {
|
||||
return "", fmt.Errorf("no valid files found")
|
||||
return "", fmt.Errorf("no video files found")
|
||||
}
|
||||
s.logger.Info().Msgf("Checking symlinks for %d files...", len(files))
|
||||
rCloneBase := debridTorrent.MountPath
|
||||
|
||||
@@ -3,8 +3,6 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -27,51 +25,58 @@ func (s *Store) addToQueue(importReq *ImportRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) StartQueueWorkers(ctx context.Context) error {
|
||||
// This function is responsible for starting the scheduled tasks
|
||||
func (s *Store) StartQueueSchedule(ctx context.Context) error {
|
||||
// Start the slots processing in a separate goroutine
|
||||
go func() {
|
||||
if err := s.processSlotsQueue(ctx); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Error processing slots queue")
|
||||
}
|
||||
}()
|
||||
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
// Start the remove stalled torrents processing in a separate goroutine
|
||||
go func() {
|
||||
if err := s.processRemoveStalledTorrents(ctx); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Error processing remove stalled torrents")
|
||||
}
|
||||
}()
|
||||
|
||||
s.scheduler.RemoveByTags("decypharr-store")
|
||||
return nil
|
||||
}
|
||||
|
||||
if jd, err := utils.ConvertToJobDef("30s"); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to convert slots tracking interval to job definition")
|
||||
} else {
|
||||
// Schedule the job
|
||||
if _, err := s.scheduler.NewJob(jd, gocron.NewTask(func() {
|
||||
func (s *Store) processSlotsQueue(ctx context.Context) error {
|
||||
s.trackAvailableSlots(ctx) // Initial tracking of available slots
|
||||
|
||||
ticker := time.NewTicker(30 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
s.trackAvailableSlots(ctx)
|
||||
}), gocron.WithContext(ctx)); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to create slots tracking job")
|
||||
} else {
|
||||
s.logger.Trace().Msgf("Download link refresh job scheduled for every %s", "30s")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if s.removeStalledAfter > 0 {
|
||||
// Stalled torrents removal job
|
||||
if jd, err := utils.ConvertToJobDef("1m"); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to convert remove stalled torrents interval to job definition")
|
||||
} else {
|
||||
// Schedule the job
|
||||
if _, err := s.scheduler.NewJob(jd, gocron.NewTask(func() {
|
||||
err := s.removeStalledTorrents(ctx)
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to process remove stalled torrents")
|
||||
}
|
||||
}), gocron.WithContext(ctx)); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to create remove stalled torrents job")
|
||||
} else {
|
||||
s.logger.Trace().Msgf("Remove stalled torrents job scheduled for every %s", "1m")
|
||||
func (s *Store) processRemoveStalledTorrents(ctx context.Context) error {
|
||||
if s.removeStalledAfter <= 0 {
|
||||
return nil // No need to remove stalled torrents if the duration is not set
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if err := s.removeStalledTorrents(ctx); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Error removing stalled torrents")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start the scheduler
|
||||
s.scheduler.Start()
|
||||
s.logger.Debug().Msg("Store worker started")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) trackAvailableSlots(ctx context.Context) {
|
||||
@@ -91,9 +96,7 @@ func (s *Store) trackAvailableSlots(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
for name, slots := range availableSlots {
|
||||
|
||||
s.logger.Debug().Msgf("Available slots for %s: %d", name, slots)
|
||||
for _, slots := range availableSlots {
|
||||
// If slots are available, process the next import request from the queue
|
||||
for slots > 0 {
|
||||
select {
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/internal/logger"
|
||||
"github.com/sirrobot01/decypharr/pkg/arr"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid"
|
||||
"github.com/sirrobot01/decypharr/pkg/rclone"
|
||||
"github.com/sirrobot01/decypharr/pkg/repair"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -19,7 +16,6 @@ type Store struct {
|
||||
repair *repair.Repair
|
||||
arr *arr.Storage
|
||||
debrid *debrid.Storage
|
||||
rcloneManager *rclone.Manager
|
||||
importsQueue *ImportQueue // Queued import requests(probably from too_many_active_downloads)
|
||||
torrents *TorrentStorage
|
||||
logger zerolog.Logger
|
||||
@@ -27,7 +23,6 @@ type Store struct {
|
||||
skipPreCache bool
|
||||
downloadSemaphore chan struct{}
|
||||
removeStalledAfter time.Duration // Duration after which stalled torrents are removed
|
||||
scheduler gocron.Scheduler
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -38,36 +33,24 @@ var (
|
||||
// Get returns the singleton instance
|
||||
func Get() *Store {
|
||||
once.Do(func() {
|
||||
cfg := config.Get()
|
||||
qbitCfg := cfg.QBitTorrent
|
||||
|
||||
// Create rclone manager if enabled
|
||||
var rcManager *rclone.Manager
|
||||
if cfg.Rclone.Enabled {
|
||||
rcManager = rclone.NewManager()
|
||||
}
|
||||
|
||||
// Create services with dependencies
|
||||
arrs := arr.NewStorage()
|
||||
deb := debrid.NewStorage(rcManager)
|
||||
|
||||
scheduler, err := gocron.NewScheduler(gocron.WithLocation(time.Local), gocron.WithGlobalJobOptions(gocron.WithTags("decypharr-store")))
|
||||
if err != nil {
|
||||
scheduler, _ = gocron.NewScheduler(gocron.WithGlobalJobOptions(gocron.WithTags("decypharr-store")))
|
||||
}
|
||||
|
||||
deb := debrid.NewStorage()
|
||||
cfg := config.Get()
|
||||
instance = &Store{
|
||||
repair: repair.New(arrs, deb),
|
||||
arr: arrs,
|
||||
debrid: deb,
|
||||
rcloneManager: rcManager,
|
||||
torrents: newTorrentStorage(cfg.TorrentsFile()),
|
||||
logger: logger.Default(), // Use default logger [decypharr]
|
||||
refreshInterval: time.Duration(cmp.Or(qbitCfg.RefreshInterval, 30)) * time.Second,
|
||||
skipPreCache: qbitCfg.SkipPreCache,
|
||||
downloadSemaphore: make(chan struct{}, cmp.Or(qbitCfg.MaxDownloads, 5)),
|
||||
importsQueue: NewImportQueue(context.Background(), 1000),
|
||||
scheduler: scheduler,
|
||||
refreshInterval: 10 * time.Minute, // Default refresh interval
|
||||
skipPreCache: false, // Default skip pre-cache
|
||||
downloadSemaphore: make(chan struct{}, 5), // Default max concurrent downloads
|
||||
}
|
||||
if cfg.QBitTorrent != nil {
|
||||
instance.refreshInterval = time.Duration(cfg.QBitTorrent.RefreshInterval) * time.Minute
|
||||
instance.skipPreCache = cfg.QBitTorrent.SkipPreCache
|
||||
instance.downloadSemaphore = make(chan struct{}, cfg.QBitTorrent.MaxDownloads)
|
||||
}
|
||||
if cfg.RemoveStalledAfter != "" {
|
||||
removeStalledAfter, err := time.ParseDuration(cfg.RemoveStalledAfter)
|
||||
@@ -85,22 +68,11 @@ func Reset() {
|
||||
instance.debrid.Reset()
|
||||
}
|
||||
|
||||
if instance.rcloneManager != nil {
|
||||
instance.rcloneManager.Stop()
|
||||
}
|
||||
|
||||
if instance.importsQueue != nil {
|
||||
instance.importsQueue.Close()
|
||||
}
|
||||
if instance.downloadSemaphore != nil {
|
||||
// Close the semaphore channel to
|
||||
close(instance.downloadSemaphore)
|
||||
}
|
||||
|
||||
if instance.scheduler != nil {
|
||||
_ = instance.scheduler.StopJobs()
|
||||
_ = instance.scheduler.Shutdown()
|
||||
}
|
||||
close(instance.downloadSemaphore)
|
||||
}
|
||||
once = sync.Once{}
|
||||
instance = nil
|
||||
@@ -118,10 +90,3 @@ func (s *Store) Repair() *repair.Repair {
|
||||
func (s *Store) Torrents() *TorrentStorage {
|
||||
return s.torrents
|
||||
}
|
||||
func (s *Store) RcloneManager() *rclone.Manager {
|
||||
return s.rcloneManager
|
||||
}
|
||||
|
||||
func (s *Store) Scheduler() gocron.Scheduler {
|
||||
return s.scheduler
|
||||
}
|
||||
|
||||
@@ -61,8 +61,9 @@ func (s *Store) processFiles(torrent *Torrent, debridTorrent *types.Torrent, imp
|
||||
_arr := importReq.Arr
|
||||
backoff := time.NewTimer(s.refreshInterval)
|
||||
defer backoff.Stop()
|
||||
|
||||
for debridTorrent.Status != "downloaded" {
|
||||
s.logger.Debug().Msgf("%s <- (%s) Download Progress: %.2f%%", debridTorrent.Debrid, debridTorrent.Name, debridTorrent.Progress)
|
||||
|
||||
dbT, err := client.CheckStatus(debridTorrent)
|
||||
if err != nil {
|
||||
s.logger.Error().
|
||||
@@ -89,15 +90,16 @@ func (s *Store) processFiles(torrent *Torrent, debridTorrent *types.Torrent, imp
|
||||
torrent = s.partialTorrentUpdate(torrent, debridTorrent)
|
||||
|
||||
// Exit the loop for downloading statuses to prevent memory buildup
|
||||
if debridTorrent.Status == "downloaded" || !utils.Contains(downloadingStatuses, debridTorrent.Status) {
|
||||
exitCondition1 := debridTorrent.Status == "downloaded"
|
||||
exitCondition2 := !utils.Contains(downloadingStatuses, debridTorrent.Status)
|
||||
|
||||
if exitCondition1 || exitCondition2 {
|
||||
break
|
||||
}
|
||||
select {
|
||||
case <-backoff.C:
|
||||
// Increase interval gradually, cap at max
|
||||
nextInterval := min(s.refreshInterval*2, 30*time.Second)
|
||||
backoff.Reset(nextInterval)
|
||||
}
|
||||
<-backoff.C
|
||||
// Increase interval gradually, cap at max
|
||||
nextInterval := min(s.refreshInterval*2, 30*time.Second)
|
||||
backoff.Reset(nextInterval)
|
||||
}
|
||||
var torrentSymlinkPath string
|
||||
var err error
|
||||
@@ -115,7 +117,6 @@ func (s *Store) processFiles(torrent *Torrent, debridTorrent *types.Torrent, imp
|
||||
}()
|
||||
s.logger.Error().Err(err).Msgf("Error occured while processing torrent %s", debridTorrent.Name)
|
||||
importReq.markAsFailed(err, torrent, debridTorrent)
|
||||
return
|
||||
}
|
||||
|
||||
onSuccess := func(torrentSymlinkPath string) {
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package store
|
||||
|
||||
import "context"
|
||||
|
||||
func (s *Store) StartWorkers(ctx context.Context) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
// Start debrid workers
|
||||
if err := s.Debrid().StartWorker(ctx); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to start debrid worker")
|
||||
} else {
|
||||
s.logger.Debug().Msg("Started debrid worker")
|
||||
}
|
||||
|
||||
// Cache workers
|
||||
for _, cache := range s.Debrid().Caches() {
|
||||
if cache == nil {
|
||||
continue
|
||||
}
|
||||
go func() {
|
||||
if err := cache.StartWorker(ctx); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to start debrid cache worker")
|
||||
} else {
|
||||
s.logger.Debug().Msgf("Started debrid cache worker for %s", cache.GetConfig().Name)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Store queue workers
|
||||
if err := s.StartQueueWorkers(ctx); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to start store worker")
|
||||
} else {
|
||||
s.logger.Debug().Msg("Started store worker")
|
||||
}
|
||||
|
||||
// Arr workers
|
||||
if err := s.Arr().StartWorker(ctx); err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to start Arr worker")
|
||||
} else {
|
||||
s.logger.Debug().Msg("Started Arr worker")
|
||||
}
|
||||
}
|
||||
141
pkg/usenet/cache.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package usenet
|
||||
|
||||
import (
|
||||
"github.com/chrisfarms/yenc"
|
||||
"github.com/puzpuzpuz/xsync/v4"
|
||||
"github.com/rs/zerolog"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SegmentCache provides intelligent caching for NNTP segments
|
||||
type SegmentCache struct {
|
||||
cache *xsync.Map[string, *CachedSegment]
|
||||
logger zerolog.Logger
|
||||
maxSize int64
|
||||
currentSize atomic.Int64
|
||||
}
|
||||
|
||||
// CachedSegment represents a cached segment with metadata
|
||||
type CachedSegment struct {
|
||||
MessageID string `json:"message_id"`
|
||||
Data []byte `json:"data"`
|
||||
DecodedSize int64 `json:"decoded_size"` // Actual size after yEnc decoding
|
||||
DeclaredSize int64 `json:"declared_size"` // Size declared in NZB
|
||||
CachedAt time.Time `json:"cached_at"`
|
||||
AccessCount int64 `json:"access_count"`
|
||||
LastAccess time.Time `json:"last_access"`
|
||||
FileBegin int64 `json:"file_begin"` // Start byte offset in the file
|
||||
FileEnd int64 `json:"file_end"` // End byte offset in the file
|
||||
}
|
||||
|
||||
// NewSegmentCache creates a new segment cache
|
||||
func NewSegmentCache(logger zerolog.Logger) *SegmentCache {
|
||||
sc := &SegmentCache{
|
||||
cache: xsync.NewMap[string, *CachedSegment](),
|
||||
logger: logger.With().Str("component", "segment_cache").Logger(),
|
||||
maxSize: 50 * 1024 * 1024, // Default max size 100MB
|
||||
}
|
||||
|
||||
return sc
|
||||
}
|
||||
|
||||
// Get retrieves a segment from cache
|
||||
func (sc *SegmentCache) Get(messageID string) (*CachedSegment, bool) {
|
||||
segment, found := sc.cache.Load(messageID)
|
||||
if !found {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
segment.AccessCount++
|
||||
segment.LastAccess = time.Now()
|
||||
|
||||
return segment, true
|
||||
}
|
||||
|
||||
// Put stores a segment in cache with intelligent size management
|
||||
func (sc *SegmentCache) Put(messageID string, data *yenc.Part, declaredSize int64) {
|
||||
dataSize := data.Size
|
||||
|
||||
currentSize := sc.currentSize.Load()
|
||||
// Check if we need to make room
|
||||
wouldExceed := (currentSize + dataSize) > sc.maxSize
|
||||
|
||||
if wouldExceed {
|
||||
sc.evictLRU(dataSize)
|
||||
}
|
||||
|
||||
segment := &CachedSegment{
|
||||
MessageID: messageID,
|
||||
Data: make([]byte, data.Size),
|
||||
DecodedSize: dataSize,
|
||||
DeclaredSize: declaredSize,
|
||||
CachedAt: time.Now(),
|
||||
AccessCount: 1,
|
||||
LastAccess: time.Now(),
|
||||
}
|
||||
|
||||
copy(segment.Data, data.Body)
|
||||
|
||||
sc.cache.Store(messageID, segment)
|
||||
|
||||
sc.currentSize.Add(dataSize)
|
||||
}
|
||||
|
||||
// evictLRU evicts least recently used segments to make room
|
||||
func (sc *SegmentCache) evictLRU(neededSpace int64) {
|
||||
if neededSpace <= 0 {
|
||||
return // No need to evict if no space is needed
|
||||
}
|
||||
if sc.cache.Size() == 0 {
|
||||
return // Nothing to evict
|
||||
}
|
||||
|
||||
// Create a sorted list of segments by last access time
|
||||
type segmentInfo struct {
|
||||
key string
|
||||
segment *CachedSegment
|
||||
lastAccess time.Time
|
||||
}
|
||||
|
||||
segments := make([]segmentInfo, 0, sc.cache.Size())
|
||||
sc.cache.Range(func(key string, value *CachedSegment) bool {
|
||||
segments = append(segments, segmentInfo{
|
||||
key: key,
|
||||
segment: value,
|
||||
lastAccess: value.LastAccess,
|
||||
})
|
||||
return true // continue iteration
|
||||
})
|
||||
|
||||
// Sort by last access time (oldest first)
|
||||
for i := 0; i < len(segments)-1; i++ {
|
||||
for j := i + 1; j < len(segments); j++ {
|
||||
if segments[i].lastAccess.After(segments[j].lastAccess) {
|
||||
segments[i], segments[j] = segments[j], segments[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Evict segments until we have enough space
|
||||
freedSpace := int64(0)
|
||||
for _, seg := range segments {
|
||||
if freedSpace >= neededSpace {
|
||||
break
|
||||
}
|
||||
|
||||
sc.cache.Delete(seg.key)
|
||||
freedSpace += int64(len(seg.segment.Data))
|
||||
}
|
||||
}
|
||||
|
||||
// Clear removes all cached segments
|
||||
func (sc *SegmentCache) Clear() {
|
||||
sc.cache.Clear()
|
||||
sc.currentSize.Store(0)
|
||||
}
|
||||
|
||||
// Delete removes a specific segment from cache
|
||||
func (sc *SegmentCache) Delete(messageID string) {
|
||||
sc.cache.Delete(messageID)
|
||||
}
|
||||
281
pkg/usenet/downloader.go
Normal file
@@ -0,0 +1,281 @@
|
||||
package usenet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/internal/logger"
|
||||
"github.com/sirrobot01/decypharr/internal/nntp"
|
||||
"github.com/sirrobot01/decypharr/internal/utils"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DownloadWorker manages concurrent NZB downloads
|
||||
type DownloadWorker struct {
|
||||
client *nntp.Client
|
||||
processor *Processor
|
||||
logger zerolog.Logger
|
||||
skipPreCache bool // Skip pre-caching for faster processing
|
||||
mountFolder string // Folder where downloads are mounted
|
||||
}
|
||||
|
||||
// DownloadJob represents a download job for an NZB
|
||||
type DownloadJob struct {
|
||||
NZB *NZB
|
||||
Action string
|
||||
Priority int
|
||||
Callback func(*NZB, error)
|
||||
DownloadDir string
|
||||
}
|
||||
|
||||
// NewDownloadWorker creates a new download worker
|
||||
func NewDownloadWorker(config *config.Usenet, client *nntp.Client, processor *Processor) *DownloadWorker {
|
||||
|
||||
dw := &DownloadWorker{
|
||||
processor: processor,
|
||||
client: client,
|
||||
logger: logger.New("usenet-download-worker"),
|
||||
skipPreCache: config.SkipPreCache,
|
||||
mountFolder: config.MountFolder,
|
||||
}
|
||||
return dw
|
||||
}
|
||||
|
||||
func (dw *DownloadWorker) CheckAvailability(ctx context.Context, job *DownloadJob) error {
|
||||
dw.logger.Debug().
|
||||
Str("nzb_id", job.NZB.ID).
|
||||
Msg("Checking NZB availability")
|
||||
|
||||
// Grab first file to extract message IDs
|
||||
firstFile := job.NZB.Files[0]
|
||||
if len(firstFile.Segments) == 0 {
|
||||
return fmt.Errorf("no segments found in first file of NZB")
|
||||
}
|
||||
|
||||
segments := firstFile.Segments
|
||||
|
||||
// Smart sampling: check first, last, and some middle segments
|
||||
samplesToCheck := dw.getSampleSegments(segments)
|
||||
|
||||
// Create error group for concurrent checking
|
||||
g, gCtx := errgroup.WithContext(ctx)
|
||||
|
||||
// Limit concurrent goroutines to prevent overwhelming the NNTP server
|
||||
maxConcurrency := len(samplesToCheck)
|
||||
if maxConns := dw.client.MinimumMaxConns(); maxConns < maxConcurrency {
|
||||
maxConcurrency = maxConns
|
||||
}
|
||||
g.SetLimit(maxConcurrency)
|
||||
|
||||
// Check each segment concurrently
|
||||
for i, segment := range samplesToCheck {
|
||||
segment := segment // capture loop variable
|
||||
segmentNum := i + 1
|
||||
|
||||
g.Go(func() error {
|
||||
select {
|
||||
case <-gCtx.Done():
|
||||
return gCtx.Err() // Return if context is canceled
|
||||
default:
|
||||
}
|
||||
conn, cleanup, err := dw.client.GetConnection(gCtx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get NNTP connection: %w", err)
|
||||
}
|
||||
defer cleanup() // Ensure connection is returned to the pool
|
||||
// Check segment availability
|
||||
seg, err := conn.GetSegment(segment.MessageID, segmentNum)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check segment %d availability: %w", segmentNum, err)
|
||||
}
|
||||
if seg == nil {
|
||||
return fmt.Errorf("segment %d not found", segmentNum)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Wait for all checks to complete
|
||||
if err := g.Wait(); err != nil {
|
||||
return fmt.Errorf("availability check failed: %w", err)
|
||||
}
|
||||
|
||||
// Update storage with availability info
|
||||
if err := dw.processor.store.Update(job.NZB); err != nil {
|
||||
dw.logger.Warn().Err(err).Msg("Failed to update NZB with availability info")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dw *DownloadWorker) Process(ctx context.Context, job *DownloadJob) error {
|
||||
var (
|
||||
finalPath string
|
||||
err error
|
||||
)
|
||||
|
||||
defer func(err error) {
|
||||
if job.Callback != nil {
|
||||
job.Callback(job.NZB, err)
|
||||
}
|
||||
}(err)
|
||||
|
||||
switch job.Action {
|
||||
case "download":
|
||||
finalPath, err = dw.downloadNZB(ctx, job)
|
||||
case "symlink":
|
||||
finalPath, err = dw.symlinkNZB(ctx, job)
|
||||
case "none":
|
||||
return nil
|
||||
default:
|
||||
// Use symlink as default action
|
||||
finalPath, err = dw.symlinkNZB(ctx, job)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if finalPath == "" {
|
||||
err = fmt.Errorf("final path is empty after processing job: %s", job.Action)
|
||||
return err
|
||||
}
|
||||
|
||||
// Use atomic transition to completed state
|
||||
return dw.processor.store.MarkAsCompleted(job.NZB.ID, finalPath)
|
||||
}
|
||||
|
||||
// downloadNZB downloads an NZB to the specified directory
|
||||
func (dw *DownloadWorker) downloadNZB(ctx context.Context, job *DownloadJob) (string, error) {
|
||||
dw.logger.Info().
|
||||
Str("nzb_id", job.NZB.ID).
|
||||
Str("download_dir", job.DownloadDir).
|
||||
Msg("Starting NZB download")
|
||||
|
||||
// TODO: implement download logic
|
||||
|
||||
return job.DownloadDir, nil
|
||||
}
|
||||
|
||||
// getSampleMessageIDs returns a smart sample of message IDs to check
|
||||
func (dw *DownloadWorker) getSampleSegments(segments []NZBSegment) []NZBSegment {
|
||||
totalSegments := len(segments)
|
||||
|
||||
// For small NZBs, check all segments
|
||||
if totalSegments <= 2 {
|
||||
return segments
|
||||
}
|
||||
|
||||
var samplesToCheck []NZBSegment
|
||||
// Always check the first and last segments
|
||||
samplesToCheck = append(samplesToCheck, segments[0]) // First segment
|
||||
samplesToCheck = append(samplesToCheck, segments[totalSegments-1]) // Last segment
|
||||
return samplesToCheck
|
||||
}
|
||||
|
||||
func (dw *DownloadWorker) symlinkNZB(ctx context.Context, job *DownloadJob) (string, error) {
|
||||
dw.logger.Info().
|
||||
Str("nzb_id", job.NZB.ID).
|
||||
Str("symlink_dir", job.DownloadDir).
|
||||
Msg("Creating symlinks for NZB")
|
||||
if job.NZB == nil {
|
||||
return "", fmt.Errorf("NZB is nil")
|
||||
}
|
||||
|
||||
mountFolder := filepath.Join(dw.mountFolder, job.NZB.Name) // e.g. /mnt/rclone/usenet/__all__/TV_SHOW
|
||||
if mountFolder == "" {
|
||||
return "", fmt.Errorf("mount folder is empty")
|
||||
}
|
||||
symlinkPath := filepath.Join(job.DownloadDir, job.NZB.Name) // e.g. /mnt/symlinks/usenet/sonarr/TV_SHOW
|
||||
if err := os.MkdirAll(symlinkPath, 0755); err != nil {
|
||||
return "", fmt.Errorf("failed to create symlink directory: %w", err)
|
||||
}
|
||||
|
||||
return dw.createSymlinksWebdav(job.NZB, mountFolder, symlinkPath)
|
||||
}
|
||||
|
||||
func (dw *DownloadWorker) createSymlinksWebdav(nzb *NZB, mountPath, symlinkPath string) (string, error) {
|
||||
files := nzb.GetFiles()
|
||||
remainingFiles := make(map[string]NZBFile)
|
||||
for _, file := range files {
|
||||
remainingFiles[file.Name] = file
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
timeout := time.After(30 * time.Minute)
|
||||
filePaths := make([]string, 0, len(files))
|
||||
maxLogCount := 10 // Limit the number of log messages to avoid flooding
|
||||
|
||||
for len(remainingFiles) > 0 {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
entries, err := os.ReadDir(mountPath)
|
||||
if err != nil {
|
||||
if maxLogCount > 0 && !errors.Is(err, os.ErrNotExist) {
|
||||
// Only log if it's not a "not found" error
|
||||
// This is due to the fact the mount path may not exist YET
|
||||
dw.logger.Warn().
|
||||
Err(err).
|
||||
Str("mount_path", mountPath).
|
||||
Msg("Failed to read directory, retrying")
|
||||
maxLogCount--
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Check which files exist in this batch
|
||||
for _, entry := range entries {
|
||||
filename := entry.Name()
|
||||
dw.logger.Info().
|
||||
Str("filename", filename).
|
||||
Msg("Checking file existence in mount path")
|
||||
|
||||
if file, exists := remainingFiles[filename]; exists {
|
||||
fullFilePath := filepath.Join(mountPath, filename)
|
||||
fileSymlinkPath := filepath.Join(symlinkPath, file.Name)
|
||||
|
||||
if err := os.Symlink(fullFilePath, fileSymlinkPath); err != nil && !os.IsExist(err) {
|
||||
dw.logger.Debug().Msgf("Failed to create symlink: %s: %v", fileSymlinkPath, err)
|
||||
} else {
|
||||
filePaths = append(filePaths, fileSymlinkPath)
|
||||
delete(remainingFiles, filename)
|
||||
dw.logger.Info().Msgf("File is ready: %s", file.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case <-timeout:
|
||||
dw.logger.Warn().Msgf("Timeout waiting for files, %d files still pending", len(remainingFiles))
|
||||
return symlinkPath, fmt.Errorf("timeout waiting for files")
|
||||
}
|
||||
}
|
||||
|
||||
if dw.skipPreCache {
|
||||
return symlinkPath, nil
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
dw.logger.Error().
|
||||
Interface("panic", r).
|
||||
Str("nzbName", nzb.Name).
|
||||
Msg("Recovered from panic in pre-cache goroutine")
|
||||
}
|
||||
}()
|
||||
if err := utils.PreCacheFile(filePaths); err != nil {
|
||||
dw.logger.Error().Msgf("Failed to pre-cache file: %s", err)
|
||||
} else {
|
||||
dw.logger.Debug().Msgf("Pre-cached %d files", len(filePaths))
|
||||
}
|
||||
}() // Pre-cache the files in the background
|
||||
// Pre-cache the first 256KB and 1MB of the file
|
||||
return symlinkPath, nil
|
||||
}
|
||||
353
pkg/usenet/errors.go
Normal file
@@ -0,0 +1,353 @@
|
||||
package usenet
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrConnectionFailed = errors.New("failed to connect to NNTP server")
|
||||
ErrServerUnavailable = errors.New("NNTP server unavailable")
|
||||
ErrRateLimitExceeded = errors.New("rate limit exceeded")
|
||||
ErrDownloadTimeout = errors.New("download timeout")
|
||||
)
|
||||
|
||||
// ErrInvalidNZBf creates a formatted error for NZB validation failures
|
||||
func ErrInvalidNZBf(format string, args ...interface{}) error {
|
||||
return fmt.Errorf("invalid NZB: "+format, args...)
|
||||
}
|
||||
|
||||
// Error represents a structured usenet error
|
||||
type Error struct {
|
||||
Code string
|
||||
Message string
|
||||
Err error
|
||||
ServerAddr string
|
||||
Timestamp time.Time
|
||||
Retryable bool
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
if e.ServerAddr != "" {
|
||||
return fmt.Sprintf("usenet error [%s] on %s: %s", e.Code, e.ServerAddr, e.Message)
|
||||
}
|
||||
return fmt.Sprintf("usenet error [%s]: %s", e.Code, e.Message)
|
||||
}
|
||||
|
||||
func (e *Error) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
func (e *Error) Is(target error) bool {
|
||||
if target == nil {
|
||||
return false
|
||||
}
|
||||
return e.Err != nil && errors.Is(e.Err, target)
|
||||
}
|
||||
|
||||
// NewUsenetError creates a new UsenetError
|
||||
func NewUsenetError(code, message string, err error) *Error {
|
||||
return &Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Err: err,
|
||||
Timestamp: time.Now(),
|
||||
Retryable: isRetryableError(err),
|
||||
}
|
||||
}
|
||||
|
||||
// NewServerError creates a new UsenetError with server address
|
||||
func NewServerError(code, message, serverAddr string, err error) *Error {
|
||||
return &Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Err: err,
|
||||
ServerAddr: serverAddr,
|
||||
Timestamp: time.Now(),
|
||||
Retryable: isRetryableError(err),
|
||||
}
|
||||
}
|
||||
|
||||
// isRetryableError determines if an error is retryable
|
||||
func isRetryableError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Network errors are generally retryable
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) {
|
||||
return netErr.Timeout()
|
||||
}
|
||||
|
||||
// DNS errors are retryable
|
||||
var dnsErr *net.DNSError
|
||||
if errors.As(err, &dnsErr) {
|
||||
return dnsErr.Temporary()
|
||||
}
|
||||
|
||||
// Connection refused is retryable
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Check error message for retryable conditions
|
||||
errMsg := strings.ToLower(err.Error())
|
||||
retryableMessages := []string{
|
||||
"connection refused",
|
||||
"connection reset",
|
||||
"connection timed out",
|
||||
"network is unreachable",
|
||||
"host is unreachable",
|
||||
"temporary failure",
|
||||
"service unavailable",
|
||||
"server overloaded",
|
||||
"rate limit",
|
||||
"too many connections",
|
||||
}
|
||||
|
||||
for _, msg := range retryableMessages {
|
||||
if strings.Contains(errMsg, msg) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// RetryConfig defines retry behavior
|
||||
type RetryConfig struct {
|
||||
MaxRetries int
|
||||
InitialDelay time.Duration
|
||||
MaxDelay time.Duration
|
||||
BackoffFactor float64
|
||||
RetryableErrors []error
|
||||
}
|
||||
|
||||
// DefaultRetryConfig returns a default retry configuration
|
||||
func DefaultRetryConfig() *RetryConfig {
|
||||
return &RetryConfig{
|
||||
MaxRetries: 3,
|
||||
InitialDelay: 1 * time.Second,
|
||||
MaxDelay: 30 * time.Second,
|
||||
BackoffFactor: 2.0,
|
||||
RetryableErrors: []error{
|
||||
ErrConnectionFailed,
|
||||
ErrServerUnavailable,
|
||||
ErrRateLimitExceeded,
|
||||
ErrDownloadTimeout,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ShouldRetry determines if an error should be retried
|
||||
func (rc *RetryConfig) ShouldRetry(err error, attempt int) bool {
|
||||
if attempt >= rc.MaxRetries {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if it's a retryable UsenetError
|
||||
var usenetErr *Error
|
||||
if errors.As(err, &usenetErr) {
|
||||
return usenetErr.Retryable
|
||||
}
|
||||
|
||||
// Check if it's in the list of retryable errors
|
||||
for _, retryableErr := range rc.RetryableErrors {
|
||||
if errors.Is(err, retryableErr) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return isRetryableError(err)
|
||||
}
|
||||
|
||||
// GetDelay calculates the delay for the next retry
|
||||
func (rc *RetryConfig) GetDelay(attempt int) time.Duration {
|
||||
if attempt <= 0 {
|
||||
return rc.InitialDelay
|
||||
}
|
||||
|
||||
delay := time.Duration(float64(rc.InitialDelay) * float64(attempt) * rc.BackoffFactor)
|
||||
if delay > rc.MaxDelay {
|
||||
delay = rc.MaxDelay
|
||||
}
|
||||
|
||||
return delay
|
||||
}
|
||||
|
||||
// RetryWithBackoff retries a function with exponential backoff
|
||||
func RetryWithBackoff(config *RetryConfig, operation func() error) error {
|
||||
var lastErr error
|
||||
|
||||
for attempt := 0; attempt <= config.MaxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
delay := config.GetDelay(attempt)
|
||||
time.Sleep(delay)
|
||||
}
|
||||
|
||||
err := operation()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
lastErr = err
|
||||
|
||||
if !config.ShouldRetry(err, attempt) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return lastErr
|
||||
}
|
||||
|
||||
// CircuitBreakerConfig defines circuit breaker behavior
|
||||
type CircuitBreakerConfig struct {
|
||||
MaxFailures int
|
||||
ResetTimeout time.Duration
|
||||
CheckInterval time.Duration
|
||||
FailureCallback func(error)
|
||||
}
|
||||
|
||||
// CircuitBreaker implements a circuit breaker pattern for NNTP connections
|
||||
type CircuitBreaker struct {
|
||||
config *CircuitBreakerConfig
|
||||
failures int
|
||||
lastFailure time.Time
|
||||
state string // "closed", "open", "half-open"
|
||||
mu *sync.RWMutex
|
||||
}
|
||||
|
||||
// NewCircuitBreaker creates a new circuit breaker
|
||||
func NewCircuitBreaker(config *CircuitBreakerConfig) *CircuitBreaker {
|
||||
if config == nil {
|
||||
config = &CircuitBreakerConfig{
|
||||
MaxFailures: 5,
|
||||
ResetTimeout: 60 * time.Second,
|
||||
CheckInterval: 10 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
return &CircuitBreaker{
|
||||
config: config,
|
||||
state: "closed",
|
||||
mu: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
// Execute executes an operation through the circuit breaker
|
||||
func (cb *CircuitBreaker) Execute(operation func() error) error {
|
||||
cb.mu.RLock()
|
||||
state := cb.state
|
||||
failures := cb.failures
|
||||
lastFailure := cb.lastFailure
|
||||
cb.mu.RUnlock()
|
||||
|
||||
// Check if we should attempt reset
|
||||
if state == "open" && time.Since(lastFailure) > cb.config.ResetTimeout {
|
||||
cb.mu.Lock()
|
||||
cb.state = "half-open"
|
||||
cb.mu.Unlock()
|
||||
state = "half-open"
|
||||
}
|
||||
|
||||
if state == "open" {
|
||||
return NewUsenetError("circuit_breaker_open",
|
||||
fmt.Sprintf("circuit breaker is open (failures: %d)", failures),
|
||||
ErrServerUnavailable)
|
||||
}
|
||||
|
||||
err := operation()
|
||||
|
||||
cb.mu.Lock()
|
||||
defer cb.mu.Unlock()
|
||||
|
||||
if err != nil {
|
||||
cb.failures++
|
||||
cb.lastFailure = time.Now()
|
||||
|
||||
if cb.failures >= cb.config.MaxFailures {
|
||||
cb.state = "open"
|
||||
}
|
||||
|
||||
if cb.config.FailureCallback != nil {
|
||||
go func() {
|
||||
cb.config.FailureCallback(err)
|
||||
}()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Success - reset if we were in half-open state
|
||||
if cb.state == "half-open" {
|
||||
cb.state = "closed"
|
||||
cb.failures = 0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetState returns the current circuit breaker state
|
||||
func (cb *CircuitBreaker) GetState() string {
|
||||
cb.mu.RLock()
|
||||
defer cb.mu.RUnlock()
|
||||
return cb.state
|
||||
}
|
||||
|
||||
// Reset manually resets the circuit breaker
|
||||
func (cb *CircuitBreaker) Reset() {
|
||||
cb.mu.Lock()
|
||||
defer cb.mu.Unlock()
|
||||
cb.state = "closed"
|
||||
cb.failures = 0
|
||||
}
|
||||
|
||||
// ValidationError represents validation errors
|
||||
type ValidationError struct {
|
||||
Field string
|
||||
Value interface{}
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *ValidationError) Error() string {
|
||||
return fmt.Sprintf("validation error for field '%s': %s", e.Field, e.Message)
|
||||
}
|
||||
|
||||
// ValidateNZBContent validates NZB content
|
||||
func ValidateNZBContent(content []byte) error {
|
||||
if len(content) == 0 {
|
||||
return &ValidationError{
|
||||
Field: "content",
|
||||
Value: len(content),
|
||||
Message: "NZB content cannot be empty",
|
||||
}
|
||||
}
|
||||
|
||||
if len(content) > 100*1024*1024 { // 100MB limit
|
||||
return &ValidationError{
|
||||
Field: "content",
|
||||
Value: len(content),
|
||||
Message: "NZB content exceeds maximum size limit (100MB)",
|
||||
}
|
||||
}
|
||||
|
||||
contentStr := string(content)
|
||||
if !strings.Contains(contentStr, "<nzb") {
|
||||
maxLen := 100
|
||||
if len(contentStr) < maxLen {
|
||||
maxLen = len(contentStr)
|
||||
}
|
||||
return &ValidationError{
|
||||
Field: "content",
|
||||
Value: contentStr[:maxLen],
|
||||
Message: "content does not appear to be valid NZB format",
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
83
pkg/usenet/misc.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package usenet
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Streamer) isSkippableError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// EOF is usually expected/skippable
|
||||
if err == io.EOF {
|
||||
return true
|
||||
}
|
||||
|
||||
errMsg := strings.ToLower(err.Error())
|
||||
|
||||
// Client disconnection errors
|
||||
if strings.Contains(errMsg, "client disconnected") ||
|
||||
strings.Contains(errMsg, "broken pipe") ||
|
||||
strings.Contains(errMsg, "connection reset") ||
|
||||
strings.Contains(errMsg, "write failed") ||
|
||||
strings.Contains(errMsg, "writer is nil") ||
|
||||
strings.Contains(errMsg, "closed pipe") ||
|
||||
strings.Contains(errMsg, "context canceled") ||
|
||||
strings.Contains(errMsg, "operation timed out") ||
|
||||
strings.Contains(errMsg, "eof") {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func RecalculateSegmentBoundaries(
|
||||
segments []NZBSegment,
|
||||
actualSizes map[string]int64,
|
||||
) []NZBSegment {
|
||||
if len(segments) == 0 {
|
||||
return segments
|
||||
}
|
||||
|
||||
result := make([]NZBSegment, len(segments))
|
||||
var currentOffset int64
|
||||
|
||||
for i, seg := range segments {
|
||||
// Copy original segment metadata
|
||||
result[i] = seg
|
||||
result[i].StartOffset = currentOffset
|
||||
|
||||
// Determine which size to use: actual decoded size, or fall back
|
||||
var size int64
|
||||
if actual, ok := actualSizes[seg.MessageID]; ok {
|
||||
size = actual
|
||||
} else {
|
||||
// decoded size as computed by parser (EndOffset-StartOffset)
|
||||
size = seg.EndOffset - seg.StartOffset
|
||||
}
|
||||
|
||||
result[i].EndOffset = currentOffset + size
|
||||
currentOffset += size
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetSegmentActualSizes extracts actual decoded sizes from cache
|
||||
func GetSegmentActualSizes(segments []NZBSegment, cache *SegmentCache) map[string]int64 {
|
||||
actualSizes := make(map[string]int64)
|
||||
|
||||
if cache == nil {
|
||||
return actualSizes
|
||||
}
|
||||
|
||||
for _, segment := range segments {
|
||||
if cached, found := cache.Get(segment.MessageID); found {
|
||||
actualSizes[segment.MessageID] = int64(len(cached.Data))
|
||||
}
|
||||
}
|
||||
|
||||
return actualSizes
|
||||
}
|
||||
152
pkg/usenet/nzb.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package usenet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SegmentRange struct {
|
||||
Segment NZBSegment // Reference to the segment
|
||||
ByteStart int64 // Start offset within this segment
|
||||
ByteEnd int64 // End offset within this segment
|
||||
TotalStart int64 // Absolute start position in file
|
||||
TotalEnd int64 // Absolute end position in file
|
||||
}
|
||||
|
||||
func (nzb *NZB) GetFileByName(name string) *NZBFile {
|
||||
for i := range nzb.Files {
|
||||
f := nzb.Files[i]
|
||||
if f.IsDeleted {
|
||||
continue
|
||||
}
|
||||
if nzb.Files[i].Name == name {
|
||||
return &nzb.Files[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nzb *NZB) MarkFileAsRemoved(fileName string) error {
|
||||
for i, file := range nzb.Files {
|
||||
if file.Name == fileName {
|
||||
// Mark the file as deleted
|
||||
nzb.Files[i].IsDeleted = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("file %s not found in NZB %s", fileName, nzb.ID)
|
||||
}
|
||||
|
||||
func (nf *NZBFile) GetSegmentsInRange(segmentSize int64, start, end int64) []SegmentRange {
|
||||
if end == -1 {
|
||||
end = nf.Size - 1
|
||||
}
|
||||
|
||||
var segmentRanges []SegmentRange
|
||||
var cumulativeSize int64
|
||||
|
||||
for i, segment := range nf.Segments {
|
||||
// Use the file's segment size (uniform)
|
||||
if segmentSize <= 0 {
|
||||
segmentSize = segment.Bytes // Fallback to actual segment size if not set
|
||||
}
|
||||
|
||||
// Handle last segment which might be smaller
|
||||
if i == len(nf.Segments)-1 {
|
||||
segmentSize = segment.Bytes // Last segment uses actual size
|
||||
}
|
||||
|
||||
cumulativeSize += segmentSize
|
||||
|
||||
// Skip segments that end before our start position
|
||||
if cumulativeSize <= start {
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate this segment's boundaries
|
||||
segmentStart := cumulativeSize - segmentSize
|
||||
segmentEnd := cumulativeSize - 1
|
||||
|
||||
// Calculate intersection with requested range
|
||||
rangeStart := max(start, segmentStart)
|
||||
rangeEnd := min(end, segmentEnd)
|
||||
|
||||
segmentRange := SegmentRange{
|
||||
Segment: segment,
|
||||
ByteStart: rangeStart - segmentStart, // Offset within segment
|
||||
ByteEnd: rangeEnd - segmentStart, // End offset within segment
|
||||
TotalStart: rangeStart, // Absolute position
|
||||
TotalEnd: rangeEnd, // Absolute position
|
||||
}
|
||||
|
||||
segmentRanges = append(segmentRanges, segmentRange)
|
||||
|
||||
// Stop if we've covered the entire requested range
|
||||
if cumulativeSize >= end+1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return segmentRanges
|
||||
}
|
||||
|
||||
func (nf *NZBFile) ConvertToSegmentRanges(segments []NZBSegment) []SegmentRange {
|
||||
var segmentRanges []SegmentRange
|
||||
var cumulativeSize int64
|
||||
|
||||
for i, segment := range segments {
|
||||
// Use the file's segment size (uniform)
|
||||
segmentSize := nf.SegmentSize
|
||||
|
||||
// Handle last segment which might be smaller
|
||||
if i == len(segments)-1 {
|
||||
segmentSize = segment.Bytes // Last segment uses actual size
|
||||
}
|
||||
|
||||
cumulativeSize += segmentSize
|
||||
|
||||
segmentRange := SegmentRange{
|
||||
Segment: segment,
|
||||
ByteStart: 0, // Always starts at 0 within the segment
|
||||
ByteEnd: segmentSize - 1, // Ends at segment size - 1
|
||||
TotalStart: cumulativeSize - segmentSize, // Absolute start position
|
||||
TotalEnd: cumulativeSize - 1, // Absolute end position
|
||||
}
|
||||
|
||||
segmentRanges = append(segmentRanges, segmentRange)
|
||||
}
|
||||
|
||||
return segmentRanges
|
||||
}
|
||||
|
||||
func (nf *NZBFile) GetCacheKey() string {
|
||||
return fmt.Sprintf("rar_%s_%d", nf.Name, nf.Size)
|
||||
}
|
||||
|
||||
func (nzb *NZB) GetFiles() []NZBFile {
|
||||
files := make([]NZBFile, 0, len(nzb.Files))
|
||||
for _, file := range nzb.Files {
|
||||
if !file.IsDeleted {
|
||||
files = append(files, file)
|
||||
}
|
||||
}
|
||||
return files[:len(files):len(files)] // Return a slice to avoid aliasing
|
||||
}
|
||||
|
||||
// ValidateNZB performs basic validation on NZB content
|
||||
func ValidateNZB(content []byte) error {
|
||||
if len(content) == 0 {
|
||||
return fmt.Errorf("empty NZB content")
|
||||
}
|
||||
|
||||
// Check for basic XML structure
|
||||
if !strings.Contains(string(content), "<nzb") {
|
||||
return fmt.Errorf("invalid NZB format: missing <nzb> tag")
|
||||
}
|
||||
|
||||
if !strings.Contains(string(content), "<file") {
|
||||
return fmt.Errorf("invalid NZB format: no files found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||