fix: do not add external tracker to a private torrent (#1011)

This commit is contained in:
Charles Huang authored and GitHub committed 2025-04-23 22:53:15 +08:00
1 parent ff43a373cd
commit 3262e5a11b
1 file changed
+38 -24
+38 -24
View File
@@ -4,13 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"github.com/GopeedLab/gopeed/internal/controller"
"github.com/GopeedLab/gopeed/internal/fetcher"
"github.com/GopeedLab/gopeed/pkg/base"
"github.com/GopeedLab/gopeed/pkg/protocol/bt"
"github.com/GopeedLab/gopeed/pkg/util"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"io" "io"
"net/url" "net/url"
"os" "os"
@@ -19,6 +12,14 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/GopeedLab/gopeed/internal/controller"
"github.com/GopeedLab/gopeed/internal/fetcher"
"github.com/GopeedLab/gopeed/pkg/base"
"github.com/GopeedLab/gopeed/pkg/protocol/bt"
"github.com/GopeedLab/gopeed/pkg/util"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
) )
var ( var (
@@ -359,6 +360,7 @@ func (f *Fetcher) addTorrent(req *base.Request, fromUpload bool) (err error) {
return return
} }
schema := util.ParseSchema(req.URL) schema := util.ParseSchema(req.URL)
privateTorrent := false
if schema == "MAGNET" { if schema == "MAGNET" {
f.torrent, err = client.AddMagnet(req.URL) f.torrent, err = client.AddMagnet(req.URL)
} else { } else {
@@ -388,34 +390,46 @@ func (f *Fetcher) addTorrent(req *base.Request, fromUpload bool) (err error) {
if err != nil && !strings.Contains(err.Error(), "expected EOF") { if err != nil && !strings.Contains(err.Error(), "expected EOF") {
return err return err
} }
info, er := metaInfo.UnmarshalInfo()
if er != nil {
return er
}
if info.Private != nil && *info.Private {
privateTorrent = true
}
f.torrent, err = client.AddTorrent(metaInfo) f.torrent, err = client.AddTorrent(metaInfo)
} }
if err != nil { if err != nil {
return return
} }
// use map to deduplicate // Do not add external tracker to a private torrent.
trackers := make(map[string]bool) if !privateTorrent {
if req.Extra != nil { // use map to deduplicate
extra := req.Extra.(*bt.ReqExtra) trackers := make(map[string]bool)
if len(extra.Trackers) > 0 { if req.Extra != nil {
for _, tracker := range extra.Trackers { extra := req.Extra.(*bt.ReqExtra)
if len(extra.Trackers) > 0 {
for _, tracker := range extra.Trackers {
trackers[tracker] = true
}
}
}
if len(f.config.Trackers) > 0 {
for _, tracker := range f.config.Trackers {
trackers[tracker] = true trackers[tracker] = true
} }
} }
} if len(trackers) > 0 {
if len(f.config.Trackers) > 0 { announceList := make([][]string, 0)
for _, tracker := range f.config.Trackers { for tracker := range trackers {
trackers[tracker] = true announceList = append(announceList, []string{tracker})
}
f.torrent.AddTrackers(announceList)
} }
} }
if len(trackers) > 0 {
announceList := make([][]string, 0)
for tracker := range trackers {
announceList = append(announceList, []string{tracker})
}
f.torrent.AddTrackers(announceList)
}
<-f.torrent.GotInfo() <-f.torrent.GotInfo()
f.torrentReady.Store(true) f.torrentReady.Store(true)