feat(protonvpn): port forwarding support with NAT-PMP (#1543)

Co-authored-by: Nicholas Xavier <nicho@nicho.dev>
This commit is contained in:
Quentin McGaw
2023-06-30 20:09:44 +02:00
committed by GitHub
parent fae6544431
commit 8ad16cdc12
19 changed files with 1118 additions and 17 deletions

View File

@@ -0,0 +1,28 @@
package natpmp
import (
"context"
"encoding/binary"
"fmt"
"net/netip"
"time"
)
// ExternalAddress fetches the duration since the start of epoch and the external
// IPv4 address of the gateway.
// See https://www.ietf.org/rfc/rfc6886.html#section-3.2
func (c *Client) ExternalAddress(ctx context.Context, gateway netip.Addr) (
durationSinceStartOfEpoch time.Duration,
externalIPv4Address netip.Addr, err error) {
request := []byte{0, 0} // version 0, operationCode 0
const responseSize = 12
response, err := c.rpc(ctx, gateway, request, responseSize)
if err != nil {
return 0, externalIPv4Address, fmt.Errorf("executing remote procedure call: %w", err)
}
secondsSinceStartOfEpoch := binary.BigEndian.Uint32(response[4:8])
durationSinceStartOfEpoch = time.Duration(secondsSinceStartOfEpoch) * time.Second
externalIPv4Address = netip.AddrFrom4([4]byte{response[8], response[9], response[10], response[11]})
return durationSinceStartOfEpoch, externalIPv4Address, nil
}