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
import (
"fmt"
"net"
"strings"
"time"
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.TorrentPeersLowWater = 50 // default: 50
torrentConfig.ListenPort = port
torrentConfig.Seed = true
torrentConfig.DataDir = snapDir
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) {
case nil:
// No NAT interface, do nothing.
case nat.ExtIP:
// ExtIP doesn't block, set the IP right away.
ip, _ := natif.ExternalIP()
torrentConfig.PublicIp4 = ip
log.Info("[torrent] Public IP", "ip", torrentConfig.PublicIp4)
// how to set ipv6?
//torrentConfig.PublicIp6 = net.ParseIP(ip)
if ip != nil {
if ip.To4() != nil {
torrentConfig.PublicIp4 = ip
} else {
torrentConfig.PublicIp6 = ip
}
}
log.Info("[torrent] Public IP", "ip", ip)
default:
// Ask the router about the IP. This takes a while and blocks startup,
// do it in the background.
if ip, err := natif.ExternalIP(); err == nil {
torrentConfig.PublicIp4 = ip
log.Info("[torrent] Public IP", "ip", torrentConfig.PublicIp4)
if ip != nil {
if ip.To4() != nil {
torrentConfig.PublicIp4 = ip
} 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