@@ -21,19 +21,25 @@ import (
2121 "fmt"
2222 "net"
2323 "strings"
24+ "sync"
2425 "time"
2526
2627 "github.com/huin/goupnp"
2728 "github.com/huin/goupnp/dcps/internetgateway1"
2829 "github.com/huin/goupnp/dcps/internetgateway2"
2930)
3031
31- const soapRequestTimeout = 3 * time .Second
32+ const (
33+ soapRequestTimeout = 3 * time .Second
34+ rateLimit = 200 * time .Millisecond
35+ )
3236
3337type upnp struct {
34- dev * goupnp.RootDevice
35- service string
36- client upnpClient
38+ dev * goupnp.RootDevice
39+ service string
40+ client upnpClient
41+ mu sync.Mutex
42+ lastReqTime time.Time
3743}
3844
3945type upnpClient interface {
@@ -43,8 +49,23 @@ type upnpClient interface {
4349 GetNATRSIPStatus () (sip bool , nat bool , err error )
4450}
4551
52+ func (n * upnp ) natEnabled () bool {
53+ var ok bool
54+ var err error
55+ n .withRateLimit (func () error {
56+ _ , ok , err = n .client .GetNATRSIPStatus ()
57+ return err
58+ })
59+ return err == nil && ok
60+ }
61+
4662func (n * upnp ) ExternalIP () (addr net.IP , err error ) {
47- ipString , err := n .client .GetExternalIPAddress ()
63+ var ipString string
64+ n .withRateLimit (func () error {
65+ ipString , err = n .client .GetExternalIPAddress ()
66+ return err
67+ })
68+
4869 if err != nil {
4970 return nil , err
5071 }
@@ -63,7 +84,10 @@ func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, li
6384 protocol = strings .ToUpper (protocol )
6485 lifetimeS := uint32 (lifetime / time .Second )
6586 n .DeleteMapping (protocol , extport , intport )
66- return n .client .AddPortMapping ("" , uint16 (extport ), protocol , uint16 (intport ), ip .String (), true , desc , lifetimeS )
87+
88+ return n .withRateLimit (func () error {
89+ return n .client .AddPortMapping ("" , uint16 (extport ), protocol , uint16 (intport ), ip .String (), true , desc , lifetimeS )
90+ })
6791}
6892
6993func (n * upnp ) internalAddress () (net.IP , error ) {
@@ -90,36 +114,51 @@ func (n *upnp) internalAddress() (net.IP, error) {
90114}
91115
92116func (n * upnp ) DeleteMapping (protocol string , extport , intport int ) error {
93- return n .client .DeletePortMapping ("" , uint16 (extport ), strings .ToUpper (protocol ))
117+ return n .withRateLimit (func () error {
118+ return n .client .DeletePortMapping ("" , uint16 (extport ), strings .ToUpper (protocol ))
119+ })
94120}
95121
96122func (n * upnp ) String () string {
97123 return "UPNP " + n .service
98124}
99125
126+ func (n * upnp ) withRateLimit (fn func () error ) error {
127+ n .mu .Lock ()
128+ defer n .mu .Unlock ()
129+
130+ lastreq := time .Since (n .lastReqTime )
131+ if lastreq < rateLimit {
132+ time .Sleep (rateLimit - lastreq )
133+ }
134+ err := fn ()
135+ n .lastReqTime = time .Now ()
136+ return err
137+ }
138+
100139// discoverUPnP searches for Internet Gateway Devices
101140// and returns the first one it can find on the local network.
102141func discoverUPnP () Interface {
103142 found := make (chan * upnp , 2 )
104143 // IGDv1
105- go discover (found , internetgateway1 .URN_WANConnectionDevice_1 , func (dev * goupnp. RootDevice , sc goupnp.ServiceClient ) * upnp {
144+ go discover (found , internetgateway1 .URN_WANConnectionDevice_1 , func (sc goupnp.ServiceClient ) * upnp {
106145 switch sc .Service .ServiceType {
107146 case internetgateway1 .URN_WANIPConnection_1 :
108- return & upnp {dev , "IGDv1-IP1" , & internetgateway1.WANIPConnection1 {ServiceClient : sc }}
147+ return & upnp {service : "IGDv1-IP1" , client : & internetgateway1.WANIPConnection1 {ServiceClient : sc }}
109148 case internetgateway1 .URN_WANPPPConnection_1 :
110- return & upnp {dev , "IGDv1-PPP1" , & internetgateway1.WANPPPConnection1 {ServiceClient : sc }}
149+ return & upnp {service : "IGDv1-PPP1" , client : & internetgateway1.WANPPPConnection1 {ServiceClient : sc }}
111150 }
112151 return nil
113152 })
114153 // IGDv2
115- go discover (found , internetgateway2 .URN_WANConnectionDevice_2 , func (dev * goupnp. RootDevice , sc goupnp.ServiceClient ) * upnp {
154+ go discover (found , internetgateway2 .URN_WANConnectionDevice_2 , func (sc goupnp.ServiceClient ) * upnp {
116155 switch sc .Service .ServiceType {
117156 case internetgateway2 .URN_WANIPConnection_1 :
118- return & upnp {dev , "IGDv2-IP1" , & internetgateway2.WANIPConnection1 {ServiceClient : sc }}
157+ return & upnp {service : "IGDv2-IP1" , client : & internetgateway2.WANIPConnection1 {ServiceClient : sc }}
119158 case internetgateway2 .URN_WANIPConnection_2 :
120- return & upnp {dev , "IGDv2-IP2" , & internetgateway2.WANIPConnection2 {ServiceClient : sc }}
159+ return & upnp {service : "IGDv2-IP2" , client : & internetgateway2.WANIPConnection2 {ServiceClient : sc }}
121160 case internetgateway2 .URN_WANPPPConnection_1 :
122- return & upnp {dev , "IGDv2-PPP1" , & internetgateway2.WANPPPConnection1 {ServiceClient : sc }}
161+ return & upnp {service : "IGDv2-PPP1" , client : & internetgateway2.WANPPPConnection1 {ServiceClient : sc }}
123162 }
124163 return nil
125164 })
@@ -134,7 +173,7 @@ func discoverUPnP() Interface {
134173// finds devices matching the given target and calls matcher for all
135174// advertised services of each device. The first non-nil service found
136175// is sent into out. If no service matched, nil is sent.
137- func discover (out chan <- * upnp , target string , matcher func (* goupnp. RootDevice , goupnp.ServiceClient ) * upnp ) {
176+ func discover (out chan <- * upnp , target string , matcher func (goupnp.ServiceClient ) * upnp ) {
138177 devs , err := goupnp .DiscoverDevices (target )
139178 if err != nil {
140179 out <- nil
@@ -157,16 +196,17 @@ func discover(out chan<- *upnp, target string, matcher func(*goupnp.RootDevice,
157196 Service : service ,
158197 }
159198 sc .SOAPClient .HTTPClient .Timeout = soapRequestTimeout
160- upnp := matcher (devs [ i ]. Root , sc )
199+ upnp := matcher (sc )
161200 if upnp == nil {
162201 return
163202 }
203+ upnp .dev = devs [i ].Root
204+
164205 // check whether port mapping is enabled
165- if _ , nat , err := upnp .client .GetNATRSIPStatus (); err != nil || ! nat {
166- return
206+ if upnp .natEnabled () {
207+ out <- upnp
208+ found = true
167209 }
168- out <- upnp
169- found = true
170210 })
171211 }
172212 if ! found {
0 commit comments