forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfwd.go
45 lines (40 loc) · 1.21 KB
/
portfwd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package torrent
import (
"fmt"
"time"
"github.com/anacrolix/log"
"github.com/anacrolix/upnp"
)
const UpnpDiscoverLogTag = "upnp-discover"
func (cl *Client) addPortMapping(d upnp.Device, proto upnp.Protocol, internalPort int, upnpID string) {
logger := cl.logger.WithContextText(fmt.Sprintf("UPnP device at %v: mapping internal %v port %v", d.GetLocalIPAddress(), proto, internalPort))
externalPort, err := d.AddPortMapping(proto, internalPort, internalPort, upnpID, 0)
if err != nil {
logger.WithDefaultLevel(log.Warning).Printf("error: %v", err)
return
}
level := log.Info
if externalPort != internalPort {
level = log.Warning
}
logger.WithDefaultLevel(level).Printf("success: external port %v", externalPort)
}
func (cl *Client) forwardPort() {
cl.lock()
defer cl.unlock()
if cl.config.NoDefaultPortForwarding {
return
}
cl.unlock()
ds := upnp.Discover(0, 2*time.Second, cl.logger.WithValues(UpnpDiscoverLogTag))
cl.lock()
cl.logger.WithDefaultLevel(log.Debug).Printf("discovered %d upnp devices", len(ds))
port := cl.incomingPeerPort()
id := cl.config.UpnpID
cl.unlock()
for _, d := range ds {
go cl.addPortMapping(d, upnp.TCP, port, id)
go cl.addPortMapping(d, upnp.UDP, port, id)
}
cl.lock()
}