-
Notifications
You must be signed in to change notification settings - Fork 0
peer: manual port-forward override for UPnP-less networks #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
myleshorton
merged 4 commits into
fisk/peer-connection-events-A
from
fisk/peer-manual-portforward
May 30, 2026
+193
−92
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ab2976e
peer: read PeerManualPortKey setting alongside RADIANCE_PEER_EXTERNAL…
8667d26
portforward: extract manual port forwarder to its own file
myleshorton 217afc9
peer/portforward/settings: address Copilot review on #500
myleshorton 40f959d
peer/portforward: address Copilot review on #500 (round 2)
myleshorton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
| package portforward | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strconv" | ||
| ) | ||
|
|
||
| // ManualForwarder exposes the same Map/Unmap/StartRenewal/ExternalIP | ||
| // surface as Forwarder but does no UPnP work. The user is expected to | ||
| // have configured a port forward on their router by hand (single-port | ||
| // 1:1 NAT — every consumer router exposes port forwarding as a single | ||
| // port number) and supplied the port number out-of-band. | ||
| // | ||
| // Use case: networks where UPnP is disabled or unavailable (router has | ||
| // UPnP off for security, ISP-provided gateways without IGD, networks | ||
| // behind double-NAT). The UPnP-based Forwarder fails in those | ||
| // environments; this type lets callers bypass discovery entirely. | ||
| type ManualForwarder struct { | ||
| port uint16 | ||
| } | ||
|
|
||
| // NewManualForwarder builds a ManualForwarder for a pre-configured | ||
| // router port forward. port must be in 1..65535; the caller is | ||
| // responsible for validating its input before calling. | ||
| func NewManualForwarder(port uint16) *ManualForwarder { | ||
| return &ManualForwarder{port: port} | ||
| } | ||
|
|
||
| // ParseManualPort parses a string into a TCP port number. Values outside | ||
| // 1..65535 return an error so callers can log and fall through to UPnP | ||
| // discovery rather than register a non-listening port with the server. | ||
| func ParseManualPort(s string) (uint16, error) { | ||
| p, err := strconv.Atoi(s) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("parse %q: %w", s, err) | ||
| } | ||
| if p < 1 || p > 65535 { | ||
| return 0, fmt.Errorf("port %d out of range (1..65535)", p) | ||
| } | ||
| return uint16(p), nil | ||
| } | ||
|
|
||
| // MapPort reports the configured port as both external and internal. The | ||
| // router-side rule is already in place; nothing to do at the protocol | ||
| // layer. | ||
| func (m *ManualForwarder) MapPort(_ context.Context, _ uint16, _ string) (*Mapping, error) { | ||
| return &Mapping{ | ||
| ExternalPort: m.port, | ||
| InternalPort: m.port, | ||
| Method: "manual", | ||
| }, nil | ||
| } | ||
|
|
||
| // UnmapPort is a no-op: the user owns the router rule and is responsible | ||
| // for removing it. | ||
| func (m *ManualForwarder) UnmapPort(_ context.Context) error { return nil } | ||
|
|
||
| // StartRenewal is a no-op: manually-configured rules don't carry a UPnP | ||
| // lease and don't need refreshing. | ||
| func (m *ManualForwarder) StartRenewal(_ context.Context) {} | ||
|
|
||
| // ExternalIP returns the empty string deliberately. With a manual port | ||
| // forward we have no UPnP gateway to ask for the WAN address, and | ||
| // probing a public IP service from the client adds a network roundtrip | ||
| // for information lantern-cloud already has — the server observes the | ||
| // peer's source address on the register call and uses that as the | ||
| // canonical external IP when this field is empty. | ||
| func (m *ManualForwarder) ExternalIP(_ context.Context) (string, error) { | ||
| return "", nil | ||
| } |
This file contains hidden or 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,66 @@ | ||
| package portforward | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // ParseManualPort accepts 1..65535 verbatim and rejects everything else | ||
| // with an error so callers can log + fall through to UPnP discovery | ||
| // rather than register a non-listening port with lantern-cloud. | ||
| func TestParseManualPort(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want uint16 | ||
| wantErr bool | ||
| }{ | ||
| {"valid mid-range", "5698", 5698, false}, | ||
| {"valid low boundary", "1", 1, false}, | ||
| {"valid high boundary", "65535", 65535, false}, | ||
| {"empty", "", 0, true}, | ||
| {"non-numeric", "abc", 0, true}, | ||
| {"zero", "0", 0, true}, | ||
| {"negative", "-5", 0, true}, | ||
| {"above uint16", "65536", 0, true}, | ||
| {"way above uint16", "99999", 0, true}, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got, err := ParseManualPort(tc.input) | ||
| if tc.wantErr { | ||
| assert.Error(t, err) | ||
| assert.Equal(t, uint16(0), got) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // ManualForwarder satisfies the portForwarder contract: MapPort returns | ||
| // a Mapping with external==internal port and the "manual" method tag, | ||
| // UnmapPort and StartRenewal are no-ops, ExternalIP returns "" so the | ||
| // server substitutes the IP it observed on the register call. | ||
| func TestManualForwarder(t *testing.T) { | ||
| f := NewManualForwarder(5698) | ||
|
|
||
| m, err := f.MapPort(context.Background(), 30001, "ignored") | ||
| require.NoError(t, err) | ||
| assert.Equal(t, uint16(5698), m.ExternalPort) | ||
| assert.Equal(t, uint16(5698), m.InternalPort, "external==internal — user mapped them themselves") | ||
| assert.Equal(t, "manual", m.Method) | ||
|
|
||
| require.NoError(t, f.UnmapPort(context.Background()), "UnmapPort is a no-op for manual forwarders") | ||
|
|
||
| // StartRenewal must not panic or block. | ||
| f.StartRenewal(context.Background()) | ||
|
|
||
| ip, err := f.ExternalIP(context.Background()) | ||
| require.NoError(t, err) | ||
| assert.Empty(t, ip, "empty IP signals server to use observed source address") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.