feat: update stun proxy logic

This commit is contained in:
Simon Ding
2025-05-08 16:13:30 +08:00
parent bb2c567da7
commit 992fa7ddd0
12 changed files with 310 additions and 73 deletions

View File

@@ -5,7 +5,8 @@ import (
"polaris/ent/downloadclients"
"polaris/pkg/nat"
"polaris/pkg/qbittorrent"
"strconv"
"github.com/pion/stun/v3"
)
func (s *Engine) stunProxyDownloadClient() error {
@@ -13,6 +14,9 @@ func (s *Engine) stunProxyDownloadClient() error {
if err != nil {
return err
}
if !e.UseNatTraversal {
return nil
}
if e.Implementation != downloadclients.ImplementationQbittorrent {
return nil
}
@@ -20,22 +24,17 @@ func (s *Engine) stunProxyDownloadClient() error {
if !ok {
return nil
}
n, err := nat.NewNatTraversal()
if err != nil {
return err
}
addr, err := n.StunAddr()
if err != nil {
return err
}
err = d.SetListenPort(addr.Port)
if err != nil {
return err
}
u, err := url.Parse(d.URL)
if err != nil {
return err
}
return n.StartProxy(u.Hostname() + ":" + strconv.Itoa(addr.Port))
n, err := nat.NewNatTraversal(func(xa stun.XORMappedAddress) error {
return d.SetListenPort(xa.Port)
}, u.Hostname())
if err != nil {
return err
}
n.StartProxy()
return nil
}