2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-06-22 22:29:01 +00:00
|
|
|
|
2022-06-09 00:14:37 +00:00
|
|
|
//go:build !js
|
2022-06-11 19:00:12 +00:00
|
|
|
|
2022-06-09 00:14:37 +00:00
|
|
|
// (no raw sockets in JS/WASM)
|
2021-07-22 20:22:58 +00:00
|
|
|
|
2021-06-22 22:29:01 +00:00
|
|
|
package portmapper
|
|
|
|
|
|
|
|
import (
|
2021-08-03 05:09:50 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-06-22 22:29:01 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-07-20 22:32:34 +00:00
|
|
|
"math/rand"
|
2023-01-13 00:57:02 +00:00
|
|
|
"net"
|
2021-08-03 05:09:50 +00:00
|
|
|
"net/http"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2021-06-22 22:29:01 +00:00
|
|
|
"net/url"
|
2021-08-03 05:09:50 +00:00
|
|
|
"strings"
|
2021-06-22 22:29:01 +00:00
|
|
|
"time"
|
|
|
|
|
2021-08-03 05:09:50 +00:00
|
|
|
"github.com/tailscale/goupnp"
|
2021-06-22 22:29:01 +00:00
|
|
|
"github.com/tailscale/goupnp/dcps/internetgateway2"
|
|
|
|
"tailscale.com/control/controlknobs"
|
2021-08-03 05:09:50 +00:00
|
|
|
"tailscale.com/net/netns"
|
|
|
|
"tailscale.com/types/logger"
|
2021-06-22 22:29:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// References:
|
|
|
|
//
|
|
|
|
// WANIP Connection v2: http://upnp.org/specs/gw/UPnP-gw-WANIPConnection-v2-Service.pdf
|
|
|
|
|
|
|
|
// upnpMapping is a port mapping over the upnp protocol. After being created it is immutable,
|
|
|
|
// but the client field may be shared across mapping instances.
|
|
|
|
type upnpMapping struct {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
gw netip.Addr
|
|
|
|
external netip.AddrPort
|
|
|
|
internal netip.AddrPort
|
2021-06-22 22:29:01 +00:00
|
|
|
goodUntil time.Time
|
|
|
|
renewAfter time.Time
|
|
|
|
|
|
|
|
// client is a connection to a upnp device, and may be reused across different UPnP mappings.
|
|
|
|
client upnpClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *upnpMapping) GoodUntil() time.Time { return u.goodUntil }
|
|
|
|
func (u *upnpMapping) RenewAfter() time.Time { return u.renewAfter }
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (u *upnpMapping) External() netip.AddrPort { return u.external }
|
2021-06-22 22:29:01 +00:00
|
|
|
func (u *upnpMapping) Release(ctx context.Context) {
|
|
|
|
u.client.DeletePortMapping(ctx, "", u.external.Port(), "udp")
|
|
|
|
}
|
|
|
|
|
|
|
|
// upnpClient is an interface over the multiple different clients exported by goupnp,
|
2021-08-03 05:09:50 +00:00
|
|
|
// exposing the functions we need for portmapping. Those clients are auto-generated from XML-specs,
|
|
|
|
// which is why they're not very idiomatic.
|
2021-06-22 22:29:01 +00:00
|
|
|
type upnpClient interface {
|
|
|
|
AddPortMapping(
|
|
|
|
ctx context.Context,
|
|
|
|
|
|
|
|
// remoteHost is the remote device sending packets to this device, in the format of x.x.x.x.
|
|
|
|
// The empty string, "", means any host out on the internet can send packets in.
|
|
|
|
remoteHost string,
|
|
|
|
|
|
|
|
// externalPort is the exposed port of this port mapping. Visible during NAT operations.
|
|
|
|
// 0 will let the router select the port, but there is an additional call,
|
|
|
|
// `AddAnyPortMapping`, which is available on 1 of the 3 possible protocols,
|
|
|
|
// which should be used if available. See `addAnyPortMapping` below, which calls this if
|
|
|
|
// `AddAnyPortMapping` is not supported.
|
|
|
|
externalPort uint16,
|
|
|
|
|
|
|
|
// protocol is whether this is over TCP or UDP. Either "tcp" or "udp".
|
|
|
|
protocol string,
|
|
|
|
|
|
|
|
// internalPort is the port that the gateway device forwards the traffic to.
|
|
|
|
internalPort uint16,
|
|
|
|
// internalClient is the IP address that packets will be forwarded to for this mapping.
|
|
|
|
// Internal client is of the form "x.x.x.x".
|
|
|
|
internalClient string,
|
|
|
|
|
|
|
|
// enabled is whether this portmapping should be enabled or disabled.
|
|
|
|
enabled bool,
|
|
|
|
// portMappingDescription is a user-readable description of this portmapping.
|
|
|
|
portMappingDescription string,
|
|
|
|
// leaseDurationSec is the duration of this portmapping. The value of this argument must be
|
|
|
|
// greater than 0. From the spec, it appears if it is set to 0, it will switch to using
|
|
|
|
// 604800 seconds, but not sure why this is desired. The recommended time is 3600 seconds.
|
|
|
|
leaseDurationSec uint32,
|
2021-08-03 05:09:50 +00:00
|
|
|
) error
|
2021-06-22 22:29:01 +00:00
|
|
|
|
|
|
|
DeletePortMapping(ctx context.Context, remoteHost string, externalPort uint16, protocol string) error
|
|
|
|
GetExternalIPAddress(ctx context.Context) (externalIPAddress string, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// tsPortMappingDesc gets sent to UPnP clients as a human-readable label for the portmapping.
|
|
|
|
// It is not used for anything other than labelling.
|
|
|
|
const tsPortMappingDesc = "tailscale-portmap"
|
|
|
|
|
|
|
|
// addAnyPortMapping abstracts over different UPnP client connections, calling the available
|
|
|
|
// AddAnyPortMapping call if available for WAN IP connection v2, otherwise defaulting to the old
|
|
|
|
// behavior of calling AddPortMapping with port = 0 to specify a wildcard port.
|
2021-07-20 22:32:34 +00:00
|
|
|
// It returns the new external port (which may not be identical to the external port specified),
|
|
|
|
// or an error.
|
2021-08-03 05:09:50 +00:00
|
|
|
//
|
|
|
|
// TODO(bradfitz): also returned the actual lease duration obtained. and check it regularly.
|
2021-06-22 22:29:01 +00:00
|
|
|
func addAnyPortMapping(
|
|
|
|
ctx context.Context,
|
|
|
|
upnp upnpClient,
|
|
|
|
externalPort uint16,
|
|
|
|
internalPort uint16,
|
|
|
|
internalClient string,
|
|
|
|
leaseDuration time.Duration,
|
|
|
|
) (newPort uint16, err error) {
|
|
|
|
if upnp, ok := upnp.(*internetgateway2.WANIPConnection2); ok {
|
|
|
|
return upnp.AddAnyPortMapping(
|
|
|
|
ctx,
|
|
|
|
"",
|
|
|
|
externalPort,
|
|
|
|
"udp",
|
|
|
|
internalPort,
|
|
|
|
internalClient,
|
|
|
|
true,
|
|
|
|
tsPortMappingDesc,
|
|
|
|
uint32(leaseDuration.Seconds()),
|
|
|
|
)
|
|
|
|
}
|
2021-07-20 22:32:34 +00:00
|
|
|
for externalPort == 0 {
|
|
|
|
externalPort = uint16(rand.Intn(65535))
|
|
|
|
}
|
2021-06-22 22:29:01 +00:00
|
|
|
err = upnp.AddPortMapping(
|
|
|
|
ctx,
|
|
|
|
"",
|
|
|
|
externalPort,
|
|
|
|
"udp",
|
|
|
|
internalPort,
|
|
|
|
internalClient,
|
|
|
|
true,
|
|
|
|
tsPortMappingDesc,
|
|
|
|
uint32(leaseDuration.Seconds()),
|
|
|
|
)
|
2021-07-20 22:32:34 +00:00
|
|
|
return externalPort, err
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 05:09:50 +00:00
|
|
|
// getUPnPClient gets a client for interfacing with UPnP, ignoring the underlying protocol for
|
2021-06-22 22:29:01 +00:00
|
|
|
// now.
|
|
|
|
// Adapted from https://github.com/huin/goupnp/blob/master/GUIDE.md.
|
2021-08-03 05:09:50 +00:00
|
|
|
//
|
|
|
|
// The gw is the detected gateway.
|
|
|
|
//
|
|
|
|
// The meta is the most recently parsed UDP discovery packet response
|
|
|
|
// from the Internet Gateway Device.
|
|
|
|
//
|
|
|
|
// The provided ctx is not retained in the returned upnpClient, but
|
|
|
|
// its associated HTTP client is (if set via goupnp.WithHTTPClient).
|
2023-03-02 23:05:30 +00:00
|
|
|
func getUPnPClient(ctx context.Context, logf logger.Logf, debug DebugKnobs, gw netip.Addr, meta uPnPDiscoResponse) (client upnpClient, err error) {
|
|
|
|
if controlknobs.DisableUPnP() || debug.DisableUPnP {
|
2021-06-22 22:29:01 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2021-08-03 05:09:50 +00:00
|
|
|
|
|
|
|
if meta.Location == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 23:05:30 +00:00
|
|
|
if debug.VerboseLogs {
|
2021-08-03 05:09:50 +00:00
|
|
|
logf("fetching %v", meta.Location)
|
|
|
|
}
|
|
|
|
u, err := url.Parse(meta.Location)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-26 03:55:44 +00:00
|
|
|
ipp, err := netip.ParseAddrPort(u.Host)
|
2021-08-03 05:09:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unexpected host %q in %q", u.Host, meta.Location)
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if ipp.Addr() != gw {
|
2023-01-13 00:57:02 +00:00
|
|
|
// https://github.com/tailscale/tailscale/issues/5502
|
|
|
|
logf("UPnP discovered root %q does not match gateway IP %v; repointing at gateway which is assumed to be floating",
|
2021-08-03 05:09:50 +00:00
|
|
|
meta.Location, gw)
|
2023-01-13 00:57:02 +00:00
|
|
|
u.Host = net.JoinHostPort(gw.String(), u.Port())
|
2021-08-03 05:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're fetching a smallish XML document over plain HTTP
|
|
|
|
// across the local LAN, without using DNS. There should be
|
|
|
|
// very few round trips and low latency, so one second is a
|
|
|
|
// long time.
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
2021-06-22 22:29:01 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-08-03 05:09:50 +00:00
|
|
|
// This part does a network fetch.
|
|
|
|
root, err := goupnp.DeviceByURL(ctx, u)
|
2021-06-22 22:29:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-03 05:09:50 +00:00
|
|
|
defer func() {
|
|
|
|
if client == nil {
|
|
|
|
return
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
2021-08-03 05:09:50 +00:00
|
|
|
logf("saw UPnP type %v at %v; %v (%v)",
|
|
|
|
strings.TrimPrefix(fmt.Sprintf("%T", client), "*internetgateway2."),
|
|
|
|
meta.Location, root.Device.FriendlyName, root.Device.Manufacturer)
|
2021-06-22 22:29:01 +00:00
|
|
|
}()
|
|
|
|
|
2021-08-03 05:09:50 +00:00
|
|
|
// These parts don't do a network fetch.
|
|
|
|
// Pick the best service type available.
|
|
|
|
if cc, _ := internetgateway2.NewWANIPConnection2ClientsFromRootDevice(ctx, root, u); len(cc) > 0 {
|
|
|
|
return cc[0], nil
|
|
|
|
}
|
|
|
|
if cc, _ := internetgateway2.NewWANIPConnection1ClientsFromRootDevice(ctx, root, u); len(cc) > 0 {
|
|
|
|
return cc[0], nil
|
|
|
|
}
|
|
|
|
if cc, _ := internetgateway2.NewWANPPPConnection1ClientsFromRootDevice(ctx, root, u); len(cc) > 0 {
|
|
|
|
return cc[0], nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) upnpHTTPClientLocked() *http.Client {
|
|
|
|
if c.uPnPHTTPClient == nil {
|
|
|
|
c.uPnPHTTPClient = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
2021-11-18 20:18:02 +00:00
|
|
|
DialContext: netns.NewDialer(c.logf).DialContext,
|
2021-08-03 05:09:50 +00:00
|
|
|
IdleConnTimeout: 2 * time.Second, // LAN is cheap
|
|
|
|
},
|
|
|
|
}
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
2021-08-03 05:09:50 +00:00
|
|
|
return c.uPnPHTTPClient
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getUPnPPortMapping attempts to create a port-mapping over the UPnP protocol. On success,
|
|
|
|
// it will return the externally exposed IP and port. Otherwise, it will return a zeroed IP and
|
|
|
|
// port and an error.
|
|
|
|
func (c *Client) getUPnPPortMapping(
|
|
|
|
ctx context.Context,
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
gw netip.Addr,
|
|
|
|
internal netip.AddrPort,
|
2021-06-22 22:29:01 +00:00
|
|
|
prevPort uint16,
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
) (external netip.AddrPort, ok bool) {
|
2023-03-02 23:05:30 +00:00
|
|
|
if controlknobs.DisableUPnP() || c.debug.DisableUPnP {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.AddrPort{}, false
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
2023-03-02 23:05:30 +00:00
|
|
|
|
2021-06-22 22:29:01 +00:00
|
|
|
now := time.Now()
|
|
|
|
upnp := &upnpMapping{
|
|
|
|
gw: gw,
|
|
|
|
internal: internal,
|
|
|
|
}
|
|
|
|
|
|
|
|
var client upnpClient
|
|
|
|
var err error
|
|
|
|
c.mu.Lock()
|
|
|
|
oldMapping, ok := c.mapping.(*upnpMapping)
|
2021-08-03 05:09:50 +00:00
|
|
|
meta := c.uPnPMeta
|
|
|
|
httpClient := c.upnpHTTPClientLocked()
|
2021-06-22 22:29:01 +00:00
|
|
|
c.mu.Unlock()
|
|
|
|
if ok && oldMapping != nil {
|
|
|
|
client = oldMapping.client
|
|
|
|
} else {
|
2021-08-03 05:09:50 +00:00
|
|
|
ctx := goupnp.WithHTTPClient(ctx, httpClient)
|
2023-03-02 23:05:30 +00:00
|
|
|
client, err = getUPnPClient(ctx, c.logf, c.debug, gw, meta)
|
|
|
|
if c.debug.VerboseLogs {
|
2021-08-03 05:09:50 +00:00
|
|
|
c.logf("getUPnPClient: %T, %v", client, err)
|
|
|
|
}
|
2021-06-22 22:29:01 +00:00
|
|
|
if err != nil {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.AddrPort{}, false
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if client == nil {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.AddrPort{}, false
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var newPort uint16
|
|
|
|
newPort, err = addAnyPortMapping(
|
|
|
|
ctx,
|
|
|
|
client,
|
|
|
|
prevPort,
|
|
|
|
internal.Port(),
|
2022-07-25 03:08:42 +00:00
|
|
|
internal.Addr().String(),
|
2021-06-22 22:29:01 +00:00
|
|
|
time.Second*pmpMapLifetimeSec,
|
|
|
|
)
|
2023-03-02 23:05:30 +00:00
|
|
|
if c.debug.VerboseLogs {
|
|
|
|
c.logf("addAnyPortMapping: %v, err=%q", newPort, err)
|
2021-08-03 05:09:50 +00:00
|
|
|
}
|
2021-06-22 22:29:01 +00:00
|
|
|
if err != nil {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.AddrPort{}, false
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
|
|
|
// TODO cache this ip somewhere?
|
|
|
|
extIP, err := client.GetExternalIPAddress(ctx)
|
2023-03-02 23:05:30 +00:00
|
|
|
if c.debug.VerboseLogs {
|
2021-08-03 05:09:50 +00:00
|
|
|
c.logf("client.GetExternalIPAddress: %v, %v", extIP, err)
|
|
|
|
}
|
2021-06-22 22:29:01 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO this doesn't seem right
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.AddrPort{}, false
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
2022-07-26 03:55:44 +00:00
|
|
|
externalIP, err := netip.ParseAddr(extIP)
|
2021-06-22 22:29:01 +00:00
|
|
|
if err != nil {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.AddrPort{}, false
|
2021-06-22 22:29:01 +00:00
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
upnp.external = netip.AddrPortFrom(externalIP, newPort)
|
2021-06-22 22:29:01 +00:00
|
|
|
d := time.Duration(pmpMapLifetimeSec) * time.Second
|
|
|
|
upnp.goodUntil = now.Add(d)
|
|
|
|
upnp.renewAfter = now.Add(d / 2)
|
|
|
|
upnp.client = client
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.mapping = upnp
|
|
|
|
c.localPort = newPort
|
|
|
|
return upnp.external, true
|
|
|
|
}
|
2021-08-03 05:09:50 +00:00
|
|
|
|
|
|
|
type uPnPDiscoResponse struct {
|
|
|
|
Location string
|
2021-08-10 21:50:33 +00:00
|
|
|
// Server describes what version the UPnP is, such as MiniUPnPd/2.x.x
|
|
|
|
Server string
|
|
|
|
// USN is the serial number of the device, which also contains
|
|
|
|
// what kind of UPnP service is being offered, i.e. InternetGatewayDevice:2
|
|
|
|
USN string
|
2021-08-03 05:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseUPnPDiscoResponse parses a UPnP HTTP-over-UDP discovery response.
|
|
|
|
func parseUPnPDiscoResponse(body []byte) (uPnPDiscoResponse, error) {
|
|
|
|
var r uPnPDiscoResponse
|
|
|
|
res, err := http.ReadResponse(bufio.NewReaderSize(bytes.NewReader(body), 128), nil)
|
|
|
|
if err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
r.Location = res.Header.Get("Location")
|
2021-08-10 21:50:33 +00:00
|
|
|
r.Server = res.Header.Get("Server")
|
|
|
|
r.USN = res.Header.Get("Usn")
|
2021-08-03 05:09:50 +00:00
|
|
|
return r, nil
|
|
|
|
}
|