lib: More contextification (#6343)
This commit is contained in:
+22
-5
@@ -15,7 +15,9 @@ import (
|
||||
|
||||
"github.com/jackpal/gateway"
|
||||
"github.com/jackpal/go-nat-pmp"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/nat"
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -23,7 +25,12 @@ func init() {
|
||||
}
|
||||
|
||||
func Discover(ctx context.Context, renewal, timeout time.Duration) []nat.Device {
|
||||
ip, err := gateway.DiscoverGateway()
|
||||
var ip net.IP
|
||||
err := util.CallWithContext(ctx, func() error {
|
||||
var err error
|
||||
ip, err = gateway.DiscoverGateway()
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
l.Debugln("Failed to discover gateway", err)
|
||||
return nil
|
||||
@@ -81,14 +88,19 @@ func (w *wrapper) GetLocalIPAddress() net.IP {
|
||||
return w.localIP
|
||||
}
|
||||
|
||||
func (w *wrapper) AddPortMapping(protocol nat.Protocol, internalPort, externalPort int, description string, duration time.Duration) (int, error) {
|
||||
func (w *wrapper) AddPortMapping(ctx context.Context, protocol nat.Protocol, internalPort, externalPort int, description string, duration time.Duration) (int, error) {
|
||||
// NAT-PMP says that if duration is 0, the mapping is actually removed
|
||||
// Swap the zero with the renewal value, which should make the lease for the
|
||||
// exact amount of time between the calls.
|
||||
if duration == 0 {
|
||||
duration = w.renewal
|
||||
}
|
||||
result, err := w.client.AddPortMapping(strings.ToLower(string(protocol)), internalPort, externalPort, int(duration/time.Second))
|
||||
var result *natpmp.AddPortMappingResult
|
||||
err := util.CallWithContext(ctx, func() error {
|
||||
var err error
|
||||
result, err = w.client.AddPortMapping(strings.ToLower(string(protocol)), internalPort, externalPort, int(duration/time.Second))
|
||||
return err
|
||||
})
|
||||
port := 0
|
||||
if result != nil {
|
||||
port = int(result.MappedExternalPort)
|
||||
@@ -96,8 +108,13 @@ func (w *wrapper) AddPortMapping(protocol nat.Protocol, internalPort, externalPo
|
||||
return port, err
|
||||
}
|
||||
|
||||
func (w *wrapper) GetExternalIPAddress() (net.IP, error) {
|
||||
result, err := w.client.GetExternalAddress()
|
||||
func (w *wrapper) GetExternalIPAddress(ctx context.Context) (net.IP, error) {
|
||||
var result *natpmp.GetExternalAddressResult
|
||||
err := util.CallWithContext(ctx, func() error {
|
||||
var err error
|
||||
result, err = w.client.GetExternalAddress()
|
||||
return err
|
||||
})
|
||||
ip := net.IPv4zero
|
||||
if result != nil {
|
||||
ip = net.IPv4(
|
||||
|
||||
Reference in New Issue
Block a user