tstor/src/sources/torrent/client.go

72 lines
1.9 KiB
Go
Raw Normal View History

2024-05-19 21:36:22 +00:00
package torrent
2024-01-28 20:22:49 +00:00
import (
"log/slog"
2024-05-13 16:56:20 +00:00
"github.com/anacrolix/dht/v2"
2024-01-28 20:22:49 +00:00
"github.com/anacrolix/dht/v2/bep44"
tlog "github.com/anacrolix/log"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/storage"
"git.kmsign.ru/royalcat/tstor/src/config"
dlog "git.kmsign.ru/royalcat/tstor/src/log"
)
2024-05-19 21:36:22 +00:00
func newClient(st storage.ClientImpl, fis bep44.Store, cfg *config.TorrentClient, id [20]byte) (*torrent.Client, error) {
2024-01-28 20:22:49 +00:00
l := slog.With("component", "torrent-client")
// TODO download and upload limits
torrentCfg := torrent.NewDefaultClientConfig()
torrentCfg.PeerID = string(id[:])
torrentCfg.DefaultStorage = st
2024-02-22 22:54:56 +00:00
torrentCfg.AlwaysWantConns = true
2024-07-10 09:26:17 +00:00
torrentCfg.DropMutuallyCompletePeers = true
torrentCfg.TorrentPeersLowWater = 100
torrentCfg.TorrentPeersHighWater = 1000
2024-02-22 22:54:56 +00:00
torrentCfg.AcceptPeerConnections = true
torrentCfg.Seed = true
2024-07-10 09:26:17 +00:00
torrentCfg.DisableAggressiveUpload = false
2024-01-28 20:22:49 +00:00
tl := tlog.NewLogger()
tl.SetHandlers(&dlog.Torrent{L: l})
2024-07-10 09:26:17 +00:00
2024-01-28 20:22:49 +00:00
torrentCfg.Logger = tl
torrentCfg.Callbacks.NewPeer = append(torrentCfg.Callbacks.NewPeer, func(p *torrent.Peer) {
2024-02-22 22:54:56 +00:00
l := l.With("ip", p.RemoteAddr.String())
if p.Torrent() != nil {
l = l.With("torrent", p.Torrent().Name())
}
l.Debug("new peer")
2024-01-28 20:22:49 +00:00
})
2024-02-22 22:54:56 +00:00
torrentCfg.Callbacks.PeerClosed = append(torrentCfg.Callbacks.PeerClosed, func(p *torrent.Peer) {
l := l.With("ip", p.RemoteAddr.String())
if p.Torrent() != nil {
l = l.With("torrent", p.Torrent().Name())
}
l.Debug("peer closed")
2024-01-28 20:22:49 +00:00
})
2024-02-22 22:54:56 +00:00
// torrentCfg.Callbacks.PeerConnClosed = append(torrentCfg.Callbacks.PeerConnClosed, func(c *torrent.PeerConn) {
// l.Debug("peer closed", "ip", c.RemoteAddr.String())
// })
2024-07-10 09:26:17 +00:00
torrentCfg.PeriodicallyAnnounceTorrentsToDht = true
2024-05-13 16:56:20 +00:00
torrentCfg.ConfigureAnacrolixDhtServer = func(cfg *dht.ServerConfig) {
cfg.Store = fis
2024-07-16 20:58:06 +00:00
cfg.Exp = dhtTTL
2024-05-13 16:56:20 +00:00
cfg.NoSecurity = false
}
2024-01-28 20:22:49 +00:00
2024-07-16 20:58:06 +00:00
t, err := torrent.NewClient(torrentCfg)
if err != nil {
return nil, err
}
return t, nil
2024-01-28 20:22:49 +00:00
}