7 "github.com/anacrolix/log"
8 "github.com/anacrolix/upnp"
11 const UpnpDiscoverLogTag = "upnp-discover"
13 func (cl *Client) addPortMapping(d upnp.Device, proto upnp.Protocol, internalPort int, upnpID string) {
14 logger := cl.logger.WithContextText(fmt.Sprintf("UPnP device at %v: mapping internal %v port %v", d.GetLocalIPAddress(), proto, internalPort))
15 externalPort, err := d.AddPortMapping(proto, internalPort, internalPort, upnpID, 0)
17 logger.WithDefaultLevel(log.Warning).Printf("error: %v", err)
21 if externalPort != internalPort {
24 logger.WithDefaultLevel(level).Printf("success: external port %v", externalPort)
27 func (cl *Client) forwardPort() {
30 if cl.config.NoDefaultPortForwarding {
34 ds := upnp.Discover(0, 2*time.Second, cl.logger.WithValues(UpnpDiscoverLogTag))
36 cl.logger.WithDefaultLevel(log.Debug).Printf("discovered %d upnp devices", len(ds))
37 port := cl.incomingPeerPort()
38 id := cl.config.UpnpID
40 for _, d := range ds {
41 go cl.addPortMapping(d, upnp.TCP, port, id)
42 go cl.addPortMapping(d, upnp.UDP, port, id)