-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package protonvpn | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/qdm12/gluetun/internal/natpmp" | ||
"github.com/qdm12/gluetun/internal/provider/utils" | ||
"inet.af/netaddr" | ||
) | ||
|
||
var ( | ||
ErrGatewayIPNotValid = errors.New("gateway IP address is not valid") | ||
ErrGatewayIPIsNil = errors.New("gateway IP address is nil") | ||
) | ||
|
||
// PortForward obtains a VPN server side port forwarded from ProtonVPN gateway. | ||
func (p *Provider) PortForward(ctx context.Context, _ *http.Client, | ||
logger utils.Logger, gateway net.IP, _ string) ( | ||
port uint16, err error) { | ||
gatewayAddress, ok := netaddr.FromStdIP(gateway) | ||
if !ok { | ||
return 0, fmt.Errorf("%w: %s", ErrGatewayIPNotValid, gateway) | ||
} | ||
|
||
if gatewayAddress.IsZero() { | ||
return 0, fmt.Errorf("%w", ErrGatewayIPIsNil) | ||
} | ||
|
||
client := natpmp.New() | ||
_, externalIPv4Address, err := client.ExternalAddress(ctx, | ||
gatewayAddress) | ||
if err != nil { | ||
return 0, fmt.Errorf("getting external IPv4 address: %w", err) | ||
} | ||
|
||
logger.Info("gateway external IPv4 address is " + externalIPv4Address.String()) | ||
const networkProtocol = "udp" | ||
const internalPort, externalPort = 0, 0 | ||
const lifetime = 60 * time.Second | ||
_, assignedInternalPort, assignedExternalPort, assignedLiftetime, err := | ||
client.AddPortMapping(ctx, gatewayAddress, networkProtocol, | ||
internalPort, externalPort, lifetime) | ||
if err != nil { | ||
return 0, fmt.Errorf("adding port mapping: %w", err) | ||
} | ||
|
||
if assignedLiftetime != lifetime { | ||
logger.Warn(fmt.Sprintf("assigned lifetime %s differs"+ | ||
" from requested lifetime %s", | ||
assignedLiftetime, lifetime)) | ||
} | ||
|
||
if assignedInternalPort != assignedExternalPort { | ||
logger.Warn(fmt.Sprintf("internal port assigned %d differs"+ | ||
" from external port assigned %d", | ||
assignedInternalPort, assignedExternalPort)) | ||
} | ||
|
||
port = assignedExternalPort | ||
return port, nil | ||
} | ||
|
||
func (p *Provider) KeepPortForward(ctx context.Context, port uint16, | ||
gateway net.IP, _ string, logger utils.Logger) (err error) { | ||
gatewayAddress, ok := netaddr.FromStdIP(gateway) | ||
if !ok { | ||
return fmt.Errorf("%w: %s", ErrGatewayIPNotValid, gateway) | ||
} | ||
|
||
client := natpmp.New() | ||
const refreshTimeout = 45 * time.Second | ||
timer := time.NewTimer(refreshTimeout) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-timer.C: | ||
} | ||
|
||
const networkProtocol = "udp" | ||
const internalPort = 0 | ||
const lifetime = 60 * time.Second | ||
_, assignedInternalPort, assignedExternalPort, assignedLiftetime, err := | ||
client.AddPortMapping(ctx, gatewayAddress, networkProtocol, | ||
internalPort, port, lifetime) | ||
if err != nil { | ||
return fmt.Errorf("adding port mapping: %w", err) | ||
} | ||
|
||
if assignedLiftetime != lifetime { | ||
logger.Warn(fmt.Sprintf("assigned lifetime %s differs"+ | ||
" from requested lifetime %s", | ||
assignedLiftetime, lifetime)) | ||
} | ||
|
||
if assignedInternalPort != assignedExternalPort { | ||
logger.Warn(fmt.Sprintf("internal port assigned %d differs"+ | ||
" from external port assigned %d", | ||
assignedInternalPort, assignedExternalPort)) | ||
} | ||
|
||
timer.Reset(refreshTimeout) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters