client.go | 16 ++++++++++++---- config.go | 1 + fs/torrentfs_test.go | 5 +++++ diff --git a/client.go b/client.go index e5466e22231b44e5d02f486f42d65e55f80be3fa..58458087e2f081186053aefd4ae7c6764129d2ee 100644 --- a/client.go +++ b/client.go @@ -134,6 +134,10 @@ dataWaits map[*torrent][]dataWait } +func (me *Client) PeerID() string { + return string(me.peerID[:]) +} + func (me *Client) ListenAddr() (addr net.Addr) { for _, l := range me.listeners { if addr != nil && l.Addr().String() != addr.String() { @@ -260,10 +264,14 @@ } cl.event.L = &cl.mu cl.mu.Init(2) - o := copy(cl.peerID[:], BEP20) - _, err = rand.Read(cl.peerID[o:]) - if err != nil { - panic("error generating peer id") + if cfg.PeerID != "" { + CopyExact(&cl.peerID, cfg.PeerID) + } else { + o := copy(cl.peerID[:], BEP20) + _, err = rand.Read(cl.peerID[o:]) + if err != nil { + panic("error generating peer id") + } } if cl.downloadStrategy == nil { diff --git a/config.go b/config.go index 6f954bc5d93591258b64f34a082b3005580f47d5..3b3b08378ff4ccab15ade5b3edfc3e5a8c169f96 100644 --- a/config.go +++ b/config.go @@ -7,4 +7,5 @@ DisableTrackers bool DownloadStrategy DownloadStrategy NoDHT bool NoUpload bool + PeerID string } diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go index 45e75acf25b51ced90d596b5b37fa601150b2d9a..8c48a00601603cf53012f93e08c1c4432380da87 100644 --- a/fs/torrentfs_test.go +++ b/fs/torrentfs_test.go @@ -152,6 +152,11 @@ DataDir: filepath.Join(layout.BaseDir, "download"), DownloadStrategy: torrent.NewResponsiveDownloadStrategy(0), DisableTrackers: true, NoDHT: true, + + // This can be used to check if clients can connect to other clients + // with the same ID. + + // PeerID: seeder.PeerID(), }) http.HandleFunc("/leecher", func(w http.ResponseWriter, req *http.Request) { leecher.WriteStatus(w)