2024-05-19 21:36:22 +00:00
|
|
|
package torrent
|
2024-01-28 20:22:49 +00:00
|
|
|
|
|
|
|
import (
|
2024-08-15 08:23:44 +00:00
|
|
|
"crypto/rand"
|
2024-01-28 20:22:49 +00:00
|
|
|
"log/slog"
|
2024-08-15 08:23:44 +00:00
|
|
|
"os"
|
2024-01-28 20:22:49 +00:00
|
|
|
|
2024-08-15 08:23:44 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/config"
|
|
|
|
dlog "git.kmsign.ru/royalcat/tstor/src/log"
|
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"
|
2024-08-15 08:23:44 +00:00
|
|
|
"github.com/anacrolix/torrent/types/infohash"
|
2024-01-28 20:22:49 +00:00
|
|
|
)
|
|
|
|
|
2024-08-15 08:23:44 +00:00
|
|
|
func newClientConfig(st storage.ClientImpl, fis bep44.Store, cfg *config.TorrentClient, id [20]byte) *torrent.ClientConfig {
|
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-08-15 08:23:44 +00:00
|
|
|
// torrentCfg.AlwaysWantConns = true
|
|
|
|
// torrentCfg.DropMutuallyCompletePeers = true
|
|
|
|
// torrentCfg.TorrentPeersLowWater = 100
|
|
|
|
// torrentCfg.TorrentPeersHighWater = 1000
|
|
|
|
// torrentCfg.AcceptPeerConnections = true
|
2024-02-22 22:54:56 +00:00
|
|
|
torrentCfg.Seed = true
|
2024-08-15 08:23:44 +00:00
|
|
|
// torrentCfg.DisableAggressiveUpload = false
|
2024-01-28 20:22:49 +00:00
|
|
|
|
2024-08-15 08:23:44 +00:00
|
|
|
tl := tlog.NewLogger("torrent-client")
|
2024-01-28 20:22:49 +00:00
|
|
|
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-08-15 08:23:44 +00:00
|
|
|
l.With(peerAttrs(p)...).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) {
|
2024-08-15 08:23:44 +00:00
|
|
|
l.With(peerAttrs(p)...).Debug("peer closed")
|
2024-01-28 20:22:49 +00:00
|
|
|
})
|
2024-08-15 08:23:44 +00:00
|
|
|
torrentCfg.Callbacks.CompletedHandshake = func(pc *torrent.PeerConn, ih infohash.T) {
|
|
|
|
l.With(peerAttrs(&pc.Peer)...).Debug("completed handshake")
|
|
|
|
}
|
|
|
|
torrentCfg.Callbacks.PeerConnAdded = append(torrentCfg.Callbacks.PeerConnAdded, func(pc *torrent.PeerConn) {
|
|
|
|
l.With(peerAttrs(&pc.Peer)...).Debug("peer conn added")
|
|
|
|
})
|
|
|
|
torrentCfg.Callbacks.PeerConnClosed = func(pc *torrent.PeerConn) {
|
|
|
|
l.With(peerAttrs(&pc.Peer)...).Debug("peer conn closed")
|
|
|
|
}
|
2024-02-22 22:54:56 +00:00
|
|
|
|
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
|
|
|
}
|
2024-01-28 20:22:49 +00:00
|
|
|
|
2024-08-15 08:23:44 +00:00
|
|
|
return torrentCfg
|
|
|
|
}
|
|
|
|
|
|
|
|
var emptyBytes [20]byte
|
|
|
|
|
|
|
|
func getOrCreatePeerID(p string) ([20]byte, error) {
|
|
|
|
idb, err := os.ReadFile(p)
|
|
|
|
if err == nil {
|
|
|
|
var out [20]byte
|
|
|
|
copy(out[:], idb)
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return emptyBytes, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out [20]byte
|
|
|
|
_, err = rand.Read(out[:])
|
2024-07-16 20:58:06 +00:00
|
|
|
if err != nil {
|
2024-08-15 08:23:44 +00:00
|
|
|
return emptyBytes, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, os.WriteFile(p, out[:], 0755)
|
|
|
|
}
|
|
|
|
|
|
|
|
func peerAttrs(peer *torrent.Peer) []any {
|
|
|
|
out := []any{
|
|
|
|
slog.String("ip", peer.RemoteAddr.String()),
|
|
|
|
slog.String("discovery", string(peer.Discovery)),
|
|
|
|
slog.Int("max-requests", peer.PeerMaxRequests),
|
|
|
|
slog.Bool("prefers-encryption", peer.PeerPrefersEncryption),
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.Torrent() != nil {
|
|
|
|
out = append(out, slog.String("torrent", peer.Torrent().Name()))
|
2024-07-16 20:58:06 +00:00
|
|
|
}
|
|
|
|
|
2024-08-15 08:23:44 +00:00
|
|
|
return out
|
2024-01-28 20:22:49 +00:00
|
|
|
}
|