Skip to content

Commit 2ccc236

Browse files
committed
fix(store/stremthru): fix double nzb for own newznab nzb link
1 parent 1c133f8 commit 2ccc236

3 files changed

Lines changed: 38 additions & 14 deletions

File tree

internal/endpoint/newznab.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func handleNewznab(w http.ResponseWriter, r *http.Request) {
5858
}
5959
}
6060

61-
baseURL := shared.ExtractRequestBaseURL(r).JoinPath("/v0/newznab")
61+
baseURL := config.BaseURL.JoinPath("/v0/newznab")
6262

6363
switch t {
6464
case "search", "tvsearch", "movie":
@@ -165,7 +165,7 @@ func handleNewznabByIndexer(w http.ResponseWriter, r *http.Request) {
165165
sendZnabResponse(w, r, 200, znab.ErrorUnknownError(err.Error()), o)
166166
return
167167
}
168-
baseURL := shared.ExtractRequestBaseURL(r).JoinPath("/v0/newznab")
168+
baseURL := config.BaseURL.JoinPath("/v0/newznab")
169169
nzbLinkQuery := url.Values{
170170
"apikey": {r.URL.Query().Get("apikey")},
171171
"t": {"get"},
@@ -201,7 +201,7 @@ func handleNewznabGet(w http.ResponseWriter, r *http.Request, o string) {
201201
return
202202
}
203203

204-
indexerId, link, err := newznab.StremThruIndexer.UnwrapLink(nzbId)
204+
indexerId, link, err := newznab.StremThruIndexer.UnwrapLink(nzbId, true)
205205
if err != nil {
206206
sendZnabResponse(w, r, 200, znab.ErrorIncorrectParameter("invalid id"), o)
207207
return

internal/newznab/indexer.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func parseNZBId(nzbId string) (indexerId int64, downloadURL string, err error) {
286286
return indexerId, downloadURL, nil
287287
}
288288

289-
func (sti stremThruIndexer) UnwrapLink(id string) (int64, *url.URL, error) {
289+
func (sti stremThruIndexer) UnwrapLink(id string, checkRateLimit bool) (int64, *url.URL, error) {
290290
indexerId, link, err := parseNZBId(id)
291291
if err != nil {
292292
return 0, nil, err
@@ -300,16 +300,18 @@ func (sti stremThruIndexer) UnwrapLink(id string) (int64, *url.URL, error) {
300300
return indexerId, nil, errors.New("indexer not found")
301301
}
302302

303-
rl, err := indexer.GetRateLimiter()
304-
if err != nil {
305-
return indexerId, nil, err
306-
}
307-
if rl != nil {
308-
if result, err := rl.Try(); err != nil {
303+
if checkRateLimit {
304+
rl, err := indexer.GetRateLimiter()
305+
if err != nil {
309306
return indexerId, nil, err
310-
} else if !result.Allowed {
311-
newznab_stats.RecordRateLimited(indexerId, newznab_stats.OperationDownload)
312-
return indexerId, nil, errors.New("rate limit exceeded")
307+
}
308+
if rl != nil {
309+
if result, err := rl.Try(); err != nil {
310+
return indexerId, nil, err
311+
} else if !result.Allowed {
312+
newznab_stats.RecordRateLimited(indexerId, newznab_stats.OperationDownload)
313+
return indexerId, nil, errors.New("rate limit exceeded")
314+
}
313315
}
314316
}
315317

store/stremthru/store.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package stremthru
33
import (
44
"errors"
55
"net/http"
6+
"net/url"
67
"path/filepath"
78
"strings"
89
"time"
@@ -11,6 +12,7 @@ import (
1112
"github.com/MunifTanjim/stremthru/internal/cache"
1213
"github.com/MunifTanjim/stremthru/internal/config"
1314
"github.com/MunifTanjim/stremthru/internal/job/job_queue"
15+
"github.com/MunifTanjim/stremthru/internal/newznab"
1416
usenetmanager "github.com/MunifTanjim/stremthru/internal/usenet/manager"
1517
"github.com/MunifTanjim/stremthru/internal/usenet/nzb_info"
1618
usenet_pool "github.com/MunifTanjim/stremthru/internal/usenet/pool"
@@ -148,7 +150,27 @@ func (c *StoreClient) AddNewz(params *store.AddNewzParams) (*store.AddNewzData,
148150
return nil, err
149151
}
150152

151-
id, err := nzb_info.QueueJob(ba.Username, "", params.Link, "", 0, "", 0)
153+
link, indexerId := params.Link, int64(0)
154+
155+
if u, err := url.Parse(link); err != nil {
156+
err := core.NewStoreError("invalid link").WithCause(err)
157+
err.StoreName = string(store.StoreNameStremThru)
158+
err.StatusCode = http.StatusBadRequest
159+
return nil, err
160+
} else if u.Host == config.BaseURL.Host {
161+
nzbId := u.Query().Get("id")
162+
if nzbIndexerId, nzbLink, err := newznab.StremThruIndexer.UnwrapLink(nzbId, false); err != nil {
163+
err := core.NewStoreError("invalid link").WithCause(err)
164+
err.StoreName = string(store.StoreNameStremThru)
165+
err.StatusCode = http.StatusBadRequest
166+
return nil, err
167+
} else {
168+
link = nzbLink.String()
169+
indexerId = nzbIndexerId
170+
}
171+
}
172+
173+
id, err := nzb_info.QueueJob(ba.Username, "", link, "", 0, "", indexerId)
152174
if err != nil {
153175
return nil, err
154176
}

0 commit comments

Comments
 (0)