From 5659445972a2b30adf2d0f90817e028f660d2afe Mon Sep 17 00:00:00 2001 From: nikk gitanes Date: Fri, 9 Feb 2024 00:57:30 +0300 Subject: [PATCH] add -m option --- server/cmd/main.go | 9 +++++++++ server/settings/settings.go | 1 + server/torr/stream.go | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/server/cmd/main.go b/server/cmd/main.go index de434cf..e4f1d41 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "runtime" + "strconv" "strings" "time" @@ -42,6 +43,7 @@ type args struct { PubIPv4 string `arg:"-4" help:"set public IPv4 addr"` PubIPv6 string `arg:"-6" help:"set public IPv6 addr"` SearchWA bool `arg:"-s" help:"search without auth"` + MaxSize string `arg:"-m" help:"max allowed stream size"` } func (args) Version() string { @@ -104,6 +106,13 @@ func main() { go watchTDir(params.TorrentsDir) } + if params.MaxSize != "" { + maxSize, err := strconv.ParseInt(params.MaxSize, 10, 64) + if err == nil { + settings.MaxSize = maxSize + } + } + server.Start(params.Port, params.SslPort, params.SslCert, params.SslKey, params.Ssl, params.RDB, params.SearchWA) log.TLogln(server.WaitServer()) log.Close() diff --git a/server/settings/settings.go b/server/settings/settings.go index 34e9bf9..13769f7 100644 --- a/server/settings/settings.go +++ b/server/settings/settings.go @@ -20,6 +20,7 @@ var ( PubIPv4 string PubIPv6 string TorAddr string + MaxSize int64 ) func InitSets(readOnly, searchWA bool) { diff --git a/server/torr/stream.go b/server/torr/stream.go index 6ad9ffc..240f771 100644 --- a/server/torr/stream.go +++ b/server/torr/stream.go @@ -47,6 +47,10 @@ func (t *Torrent) Stream(fileID int, req *http.Request, resp http.ResponseWriter if file == nil { return fmt.Errorf("file with id %v not found", fileID) } + if int64(sets.MaxSize) > 0 && file.Length() > int64(sets.MaxSize) { + log.Println("file", file.DisplayPath(), "size exceeded max allowed", sets.MaxSize, "bytes") + return fmt.Errorf("file size exceeded max allowed %d bytes", sets.MaxSize) + } reader := t.NewReader(file)