This commit is contained in:
Alex Sharov 2022-05-25 00:04:26 +07:00 committed by GitHub
parent 216a5c468f
commit b9bf97362b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,9 @@
package torrentcfg package torrentcfg
import ( import (
"fmt"
"net"
"strings"
"time" "time"
lg "github.com/anacrolix/log" lg "github.com/anacrolix/log"
@ -57,28 +60,51 @@ func New(snapDir string, verbosity lg.Level, natif nat.Interface, downloadRate,
torrentConfig.TorrentPeersHighWater = 500 // default: 500 torrentConfig.TorrentPeersHighWater = 500 // default: 500
torrentConfig.TorrentPeersLowWater = 50 // default: 50 torrentConfig.TorrentPeersLowWater = 50 // default: 50
torrentConfig.ListenPort = port
torrentConfig.Seed = true torrentConfig.Seed = true
torrentConfig.DataDir = snapDir torrentConfig.DataDir = snapDir
torrentConfig.UpnpID = torrentConfig.UpnpID + "leecher" torrentConfig.UpnpID = torrentConfig.UpnpID + "leecher"
torrentConfig.ListenPort = port
// check if ipv6 is enabled
torrentConfig.DisableIPv6 = true
l, err := net.Listen("tcp6", fmt.Sprintf(":%d", port))
if err != nil {
isDisabled := strings.Contains(err.Error(), "cannot assign requested address") || strings.Contains(err.Error(), "address family not supported by protocol")
if !isDisabled {
log.Warn("can't enable ipv6", "err", err)
}
} else {
l.Close()
torrentConfig.DisableIPv6 = false
}
switch natif.(type) { switch natif.(type) {
case nil: case nil:
// No NAT interface, do nothing. // No NAT interface, do nothing.
case nat.ExtIP: case nat.ExtIP:
// ExtIP doesn't block, set the IP right away. // ExtIP doesn't block, set the IP right away.
ip, _ := natif.ExternalIP() ip, _ := natif.ExternalIP()
if ip != nil {
if ip.To4() != nil {
torrentConfig.PublicIp4 = ip torrentConfig.PublicIp4 = ip
log.Info("[torrent] Public IP", "ip", torrentConfig.PublicIp4) } else {
// how to set ipv6? torrentConfig.PublicIp6 = ip
//torrentConfig.PublicIp6 = net.ParseIP(ip) }
}
log.Info("[torrent] Public IP", "ip", ip)
default: default:
// Ask the router about the IP. This takes a while and blocks startup, // Ask the router about the IP. This takes a while and blocks startup,
// do it in the background. // do it in the background.
if ip, err := natif.ExternalIP(); err == nil { if ip, err := natif.ExternalIP(); err == nil {
if ip != nil {
if ip.To4() != nil {
torrentConfig.PublicIp4 = ip torrentConfig.PublicIp4 = ip
log.Info("[torrent] Public IP", "ip", torrentConfig.PublicIp4) } else {
torrentConfig.PublicIp6 = ip
}
}
log.Info("[torrent] Public IP", "ip", ip)
} }
} }
// rates are divided by 2 - I don't know why it works, maybe bug inside torrent lib accounting // rates are divided by 2 - I don't know why it works, maybe bug inside torrent lib accounting