mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
add public IPv4/6 runtime options and detect functions
This commit is contained in:
@@ -32,6 +32,8 @@ type args struct {
|
|||||||
DontKill bool `arg:"-k" help:"don't kill server on signal"`
|
DontKill bool `arg:"-k" help:"don't kill server on signal"`
|
||||||
UI bool `arg:"-u" help:"open torrserver page in browser"`
|
UI bool `arg:"-u" help:"open torrserver page in browser"`
|
||||||
TorrentsDir string `arg:"-t" help:"autoload torrents from dir"`
|
TorrentsDir string `arg:"-t" help:"autoload torrents from dir"`
|
||||||
|
IPv4 string `arg:"-4" help:"set public IPv4 addr"`
|
||||||
|
IPv6 string `arg:"-6" help:"set public IPv6 addr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (args) Version() string {
|
func (args) Version() string {
|
||||||
@@ -72,6 +74,14 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if params.IPv4 != "" {
|
||||||
|
settings.PubIPv4 = params.IPv4
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.IPv6 != "" {
|
||||||
|
settings.PubIPv6 = params.IPv6
|
||||||
|
}
|
||||||
|
|
||||||
if params.TorrentsDir != "" {
|
if params.TorrentsDir != "" {
|
||||||
go watchTDir(params.TorrentsDir)
|
go watchTDir(params.TorrentsDir)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ var (
|
|||||||
Port string
|
Port string
|
||||||
ReadOnly bool
|
ReadOnly bool
|
||||||
HttpAuth bool
|
HttpAuth bool
|
||||||
|
PubIPv4 string
|
||||||
|
PubIPv6 string
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitSets(readOnly bool) {
|
func InitSets(readOnly bool) {
|
||||||
|
|||||||
@@ -118,8 +118,30 @@ func (bt *BTServer) configure() {
|
|||||||
|
|
||||||
log.Println("Client config:", settings.BTsets)
|
log.Println("Client config:", settings.BTsets)
|
||||||
|
|
||||||
|
// set public IPv4
|
||||||
|
if settings.PubIPv4 != "" {
|
||||||
|
ip4 := net.ParseIP(settings.PubIPv4)
|
||||||
|
if ip4.To4 != nil {
|
||||||
|
bt.config.PublicIp4 = ip4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if bt.config.PublicIp4 == nil {
|
||||||
|
bt.config.PublicIp4 = getPublicIp4()
|
||||||
|
}
|
||||||
|
if bt.config.PublicIp4 != nil {
|
||||||
|
log.Println("PublicIp4:", bt.config.PublicIp4)
|
||||||
|
}
|
||||||
|
|
||||||
// set public IPv6
|
// set public IPv6
|
||||||
bt.config.PublicIp6 = getPublicIp6()
|
if settings.PubIPv6 != "" {
|
||||||
|
ip6 := net.ParseIP(settings.PubIPv6)
|
||||||
|
if ip6.To4 == nil && ip6.To16 != nil {
|
||||||
|
bt.config.PublicIp6 = ip6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if bt.config.PublicIp6 == nil {
|
||||||
|
bt.config.PublicIp6 = getPublicIp6()
|
||||||
|
}
|
||||||
if bt.config.PublicIp6 != nil {
|
if bt.config.PublicIp6 != nil {
|
||||||
log.Println("PublicIp6:", bt.config.PublicIp6)
|
log.Println("PublicIp6:", bt.config.PublicIp6)
|
||||||
}
|
}
|
||||||
@@ -146,6 +168,32 @@ func (bt *BTServer) RemoveTorrent(hash torrent.InfoHash) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPublicIp4() net.IP {
|
||||||
|
ifaces, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error get public IPv4")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, i := range ifaces {
|
||||||
|
addrs, _ := i.Addrs()
|
||||||
|
if i.Flags&net.FlagUp == net.FlagUp {
|
||||||
|
for _, addr := range addrs {
|
||||||
|
var ip net.IP
|
||||||
|
switch v := addr.(type) {
|
||||||
|
case *net.IPNet:
|
||||||
|
ip = v.IP
|
||||||
|
case *net.IPAddr:
|
||||||
|
ip = v.IP
|
||||||
|
}
|
||||||
|
if !ip.IsLoopback() && !ip.IsPrivate() && ip.To4 != nil {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func getPublicIp6() net.IP {
|
func getPublicIp6() net.IP {
|
||||||
ifaces, err := net.Interfaces()
|
ifaces, err := net.Interfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user