--torrent.staticpeers (#923)

This commit is contained in:
Alex Sharov 2023-03-08 15:29:42 +07:00 committed by GitHub
parent 51698bfc97
commit f985e94553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,9 +18,11 @@ package downloadercfg
import (
"io/ioutil"
"net"
"runtime"
"strings"
"github.com/anacrolix/dht/v2"
lg "github.com/anacrolix/log"
"github.com/anacrolix/torrent"
"github.com/c2h5oh/datasize"
@ -68,7 +70,7 @@ func Default() *torrent.ClientConfig {
return torrentConfig
}
func New(snapDir string, version string, verbosity lg.Level, downloadRate, uploadRate datasize.ByteSize, port, connsPerFile, downloadSlots int) (*Cfg, error) {
func New(snapDir string, version string, verbosity lg.Level, downloadRate, uploadRate datasize.ByteSize, port, connsPerFile, downloadSlots int, staticPeers []string) (*Cfg, error) {
torrentConfig := Default()
torrentConfig.ExtendedHandshakeClientVersion = version
@ -95,6 +97,42 @@ func New(snapDir string, version string, verbosity lg.Level, downloadRate, uploa
torrentConfig.Logger = lg.Default.FilterLevel(verbosity)
torrentConfig.Logger.Handlers = []lg.Handler{adapterHandler{}}
if len(staticPeers) > 0 {
torrentConfig.NoDHT = false
//defaultNodes := torrentConfig.DhtStartingNodes
torrentConfig.DhtStartingNodes = func(network string) dht.StartingNodesGetter {
return func() ([]dht.Addr, error) {
addrs, err := dht.GlobalBootstrapAddrs(network)
if err != nil {
return nil, err
}
for _, seed := range staticPeers {
if network == "udp" {
var addr *net.UDPAddr
addr, err := net.ResolveUDPAddr(network, seed+":80")
if err != nil {
log.Warn("[downloader] Cannot UDP resolve address", "network", network, "addr", seed)
continue
}
addrs = append(addrs, dht.NewAddr(addr))
}
if network == "tcp" {
var addr *net.TCPAddr
addr, err := net.ResolveTCPAddr(network, seed+":80")
if err != nil {
log.Warn("[downloader] Cannot TCP resolve address", "network", network, "addr", seed)
continue
}
addrs = append(addrs, dht.NewAddr(addr))
}
}
return addrs, nil
}
}
//staticPeers
}
return &Cfg{ClientConfig: torrentConfig, DownloadSlots: downloadSlots}, nil
}