mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
Auto detect public IPs if not provided by user
adopted code from anacrolix/confluence 3c7a0c445d
This commit is contained in:
@@ -12,6 +12,7 @@ require (
|
|||||||
github.com/anacrolix/dms v1.4.0
|
github.com/anacrolix/dms v1.4.0
|
||||||
github.com/anacrolix/log v0.13.1
|
github.com/anacrolix/log v0.13.1
|
||||||
github.com/anacrolix/missinggo v1.3.0
|
github.com/anacrolix/missinggo v1.3.0
|
||||||
|
github.com/anacrolix/publicip v0.2.0
|
||||||
github.com/anacrolix/torrent v1.43.1
|
github.com/anacrolix/torrent v1.43.1
|
||||||
github.com/gin-contrib/cors v1.3.1
|
github.com/gin-contrib/cors v1.3.1
|
||||||
github.com/gin-contrib/location v0.0.2
|
github.com/gin-contrib/location v0.0.2
|
||||||
|
|||||||
@@ -66,6 +66,8 @@ github.com/anacrolix/mmsg v1.0.0 h1:btC7YLjOn29aTUAExJiVUhQOuf/8rhm+/nWCMAnL3Hg=
|
|||||||
github.com/anacrolix/mmsg v1.0.0/go.mod h1:x8kRaJY/dCrY9Al0PEcj1mb/uFHwP6GCJ9fLl4thEPc=
|
github.com/anacrolix/mmsg v1.0.0/go.mod h1:x8kRaJY/dCrY9Al0PEcj1mb/uFHwP6GCJ9fLl4thEPc=
|
||||||
github.com/anacrolix/multiless v0.3.0 h1:5Bu0DZncjE4e06b9r1Ap2tUY4Au0NToBP5RpuEngSis=
|
github.com/anacrolix/multiless v0.3.0 h1:5Bu0DZncjE4e06b9r1Ap2tUY4Au0NToBP5RpuEngSis=
|
||||||
github.com/anacrolix/multiless v0.3.0/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
|
github.com/anacrolix/multiless v0.3.0/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
|
||||||
|
github.com/anacrolix/publicip v0.2.0 h1:n/BmRxXRlOT/wQFd6Xhu57r9uTU+Xvb9MyEkLooh3TU=
|
||||||
|
github.com/anacrolix/publicip v0.2.0/go.mod h1:67G1lVkLo8UjdEcJkwScWVTvlJ35OCDsRJoWXl/wi4g=
|
||||||
github.com/anacrolix/stm v0.2.0/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg=
|
github.com/anacrolix/stm v0.2.0/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg=
|
||||||
github.com/anacrolix/stm v0.3.0 h1:peQncJSNJtk1YBrFbW0DLKYqll+sa0kOk8EvXRcO+wA=
|
github.com/anacrolix/stm v0.3.0 h1:peQncJSNJtk1YBrFbW0DLKYqll+sa0kOk8EvXRcO+wA=
|
||||||
github.com/anacrolix/stm v0.3.0/go.mod h1:spImf/rXwiAUoYYJK1YCZeWkpaHZ3kzjGFjwK5OStfU=
|
github.com/anacrolix/stm v0.3.0/go.mod h1:spImf/rXwiAUoYYJK1YCZeWkpaHZ3kzjGFjwK5OStfU=
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
package torr
|
package torr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/anacrolix/publicip"
|
||||||
"github.com/anacrolix/torrent"
|
"github.com/anacrolix/torrent"
|
||||||
"github.com/anacrolix/torrent/metainfo"
|
"github.com/anacrolix/torrent/metainfo"
|
||||||
|
|
||||||
@@ -58,7 +60,7 @@ func (bt *BTServer) Connect() error {
|
|||||||
bt.mu.Lock()
|
bt.mu.Lock()
|
||||||
defer bt.mu.Unlock()
|
defer bt.mu.Unlock()
|
||||||
var err error
|
var err error
|
||||||
bt.configure()
|
bt.configure(context.TODO())
|
||||||
bt.client, err = torrent.NewClient(bt.config)
|
bt.client, err = torrent.NewClient(bt.config)
|
||||||
bt.torrents = make(map[metainfo.Hash]*Torrent)
|
bt.torrents = make(map[metainfo.Hash]*Torrent)
|
||||||
InitApiHelper(bt)
|
InitApiHelper(bt)
|
||||||
@@ -75,7 +77,7 @@ func (bt *BTServer) Disconnect() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bt *BTServer) configure() {
|
func (bt *BTServer) configure(ctx context.Context) (err error) {
|
||||||
blocklist, _ := utils.ReadBlockedIP()
|
blocklist, _ := utils.ReadBlockedIP()
|
||||||
bt.config = torrent.NewDefaultClientConfig()
|
bt.config = torrent.NewDefaultClientConfig()
|
||||||
|
|
||||||
@@ -147,7 +149,10 @@ func (bt *BTServer) configure() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if bt.config.PublicIp4 == nil {
|
if bt.config.PublicIp4 == nil {
|
||||||
bt.config.PublicIp4 = getPublicIp4()
|
bt.config.PublicIp4, err = publicip.Get4(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error getting public ipv4 address: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if bt.config.PublicIp4 != nil {
|
if bt.config.PublicIp4 != nil {
|
||||||
log.Println("PublicIp4:", bt.config.PublicIp4)
|
log.Println("PublicIp4:", bt.config.PublicIp4)
|
||||||
@@ -160,11 +165,15 @@ func (bt *BTServer) configure() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if bt.config.PublicIp6 == nil {
|
if bt.config.PublicIp6 == nil {
|
||||||
bt.config.PublicIp6 = getPublicIp6()
|
bt.config.PublicIp6, err = publicip.Get6(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error getting public ipv6 address: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if bt.config.PublicIp6 != nil {
|
if bt.config.PublicIp6 != nil {
|
||||||
log.Println("PublicIp6:", bt.config.PublicIp6)
|
log.Println("PublicIp6:", bt.config.PublicIp6)
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bt *BTServer) GetTorrent(hash torrent.InfoHash) *Torrent {
|
func (bt *BTServer) GetTorrent(hash torrent.InfoHash) *Torrent {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func Start(port string) {
|
|||||||
log.TLogln("Start TorrServer")
|
log.TLogln("Start TorrServer")
|
||||||
ips := getLocalIps()
|
ips := getLocalIps()
|
||||||
if len(ips) > 0 {
|
if len(ips) > 0 {
|
||||||
log.TLogln("IPs:", ips)
|
log.TLogln("Local IPs:", ips)
|
||||||
}
|
}
|
||||||
err := BTS.Connect()
|
err := BTS.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -102,7 +102,7 @@ func getLocalIps() []string {
|
|||||||
case *net.IPAddr:
|
case *net.IPAddr:
|
||||||
ip = v.IP
|
ip = v.IP
|
||||||
}
|
}
|
||||||
if !ip.IsLoopback() {
|
if !ip.IsLoopback() && !ip.IsLinkLocalUnicast() && !ip.IsLinkLocalMulticast() {
|
||||||
list = append(list, ip.String())
|
list = append(list, ip.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user