-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(perfectprivacy): port forwarding support (#2378)
- Loading branch information
Showing
11 changed files
with
96 additions
and
4 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
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,43 @@ | ||
package perfectprivacy | ||
|
||
import ( | ||
"context" | ||
"net/netip" | ||
|
||
"github.com/qdm12/gluetun/internal/provider/utils" | ||
) | ||
|
||
// PortForward calculates and returns the VPN server side ports forwarded. | ||
func (p *Provider) PortForward(_ context.Context, | ||
objects utils.PortForwardObjects) (ports []uint16, err error) { | ||
if !objects.InternalIP.IsValid() { | ||
panic("internal ip is not set") | ||
} | ||
|
||
return internalIPToPorts(objects.InternalIP), nil | ||
} | ||
|
||
func (p *Provider) KeepPortForward(ctx context.Context, | ||
_ utils.PortForwardObjects) (err error) { | ||
<-ctx.Done() | ||
return ctx.Err() | ||
} | ||
|
||
// See https://www.perfect-privacy.com/en/faq section | ||
// How are the default forwarding ports being calculated? | ||
func internalIPToPorts(internalIP netip.Addr) (ports []uint16) { | ||
internalIPBytes := internalIP.AsSlice() | ||
// Convert the internal IP address to a bit string | ||
// and keep only the last 12 bits | ||
last16Bits := internalIPBytes[len(internalIPBytes)-2:] | ||
last12Bits := []byte{ | ||
last16Bits[0] & 0b00001111, // only keep 4 bits | ||
last16Bits[1], | ||
} | ||
basePort := uint16(last12Bits[0])<<8 + uint16(last12Bits[1]) //nolint:gomnd | ||
return []uint16{ | ||
10000 + basePort, | ||
20000 + basePort, | ||
30000 + basePort, | ||
} | ||
} |
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,33 @@ | ||
package perfectprivacy | ||
|
||
import ( | ||
"net/netip" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_internalIPToPorts(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
internalIP netip.Addr | ||
ports []uint16 | ||
}{ | ||
"example_case": { | ||
internalIP: netip.AddrFrom4([4]byte{10, 0, 203, 88}), | ||
ports: []uint16{12904, 22904, 32904}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ports := internalIPToPorts(testCase.internalIP) | ||
|
||
assert.Equal(t, testCase.ports, ports) | ||
}) | ||
} | ||
} |
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
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