6 "github.com/anacrolix/log"
7 "github.com/anacrolix/upnp"
10 func (cl *Client) addPortMapping(d upnp.Device, proto upnp.Protocol, internalPort int, upnpID string) {
11 externalPort, err := d.AddPortMapping(proto, internalPort, internalPort, upnpID, 0)
13 cl.logger.WithValues(log.Warning).Printf("error adding %s port mapping: %s", proto, err)
14 } else if externalPort != internalPort {
15 cl.logger.WithValues(log.Warning).Printf("external port %d does not match internal port %d in port mapping", externalPort, internalPort)
17 cl.logger.WithValues(log.Info).Printf("forwarded external %s port %d", proto, externalPort)
21 func (cl *Client) forwardPort() {
24 if cl.config.NoDefaultPortForwarding {
28 ds := upnp.Discover(0, 2*time.Second, cl.logger.WithValues("upnp-discover"))
30 cl.logger.Printf("discovered %d upnp devices", len(ds))
31 port := cl.incomingPeerPort()
32 id := cl.config.UpnpID
34 for _, d := range ds {
35 go cl.addPortMapping(d, upnp.TCP, port, id)
36 go cl.addPortMapping(d, upnp.UDP, port, id)