udp stun proxy

This commit is contained in:
Simon Ding
2025-05-07 22:09:35 +08:00
parent 9719c6a7c9
commit 2dae168cb2
3 changed files with 125 additions and 106 deletions

View File

@@ -11,6 +11,7 @@ import (
"polaris/pkg/tmdb"
"polaris/pkg/transmission"
"polaris/pkg/utils"
"time"
"github.com/pkg/errors"
"github.com/robfig/cron"
@@ -50,6 +51,12 @@ func (c *Engine) Init() {
go c.reloadTasks()
c.addSysCron()
go c.checkW500PosterOnStartup()
go func() {
time.Sleep(10*time.Second)
if err := c.stunProxyDownloadClient(); err != nil {
log.Errorf("stun proxy error: %v", err)
}
}()
}
func (c *Engine) GetTask(id int) (*Task, bool) {

41
engine/stun.go Normal file
View File

@@ -0,0 +1,41 @@
package engine
import (
"net/url"
"polaris/ent/downloadclients"
"polaris/pkg/nat"
"polaris/pkg/qbittorrent"
"strconv"
)
func (s *Engine) stunProxyDownloadClient() error {
downloader, e, err := s.GetDownloadClient()
if err != nil {
return err
}
if e.Implementation != downloadclients.ImplementationQbittorrent {
return nil
}
d, ok := downloader.(*qbittorrent.Client)
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))
}