- Notifications
You must be signed in to change notification settings - Fork1k
fix: fix parsing of IPv6 addresses in coder port-forward#15627
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
98 changes: 44 additions & 54 deletionscli/portforward.go
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 |
---|---|---|
@@ -7,6 +7,7 @@ import ( | ||
"net/netip" | ||
"os" | ||
"os/signal" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
@@ -263,7 +264,7 @@ func parsePortForwards(tcpSpecs, udpSpecs []string) ([]portForwardSpec, error) { | ||
for _, specEntry := range tcpSpecs { | ||
for _, spec := range strings.Split(specEntry, ",") { | ||
ports, err := parseSrcDestPorts(strings.TrimSpace(spec)) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to parse TCP port-forward specification %q: %w", spec, err) | ||
} | ||
@@ -281,7 +282,7 @@ func parsePortForwards(tcpSpecs, udpSpecs []string) ([]portForwardSpec, error) { | ||
for _, specEntry := range udpSpecs { | ||
for _, spec := range strings.Split(specEntry, ",") { | ||
ports, err := parseSrcDestPorts(strings.TrimSpace(spec)) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to parse UDP port-forward specification %q: %w", spec, err) | ||
} | ||
@@ -326,63 +327,53 @@ type parsedSrcDestPort struct { | ||
local, remote netip.AddrPort | ||
} | ||
// specRegexp matches port specs. It handles all the following formats: | ||
// | ||
// 8000 | ||
// 8888:9999 | ||
// 1-5:6-10 | ||
// 8000-8005 | ||
// 127.0.0.1:4000:4000 | ||
// [::1]:8080:8081 | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// 127.0.0.1:4000-4005 | ||
// [::1]:4000-4001:5000-5001 | ||
// | ||
// Important capturing groups: | ||
// | ||
// 2: local IP address (including [] for IPv6) | ||
// 3: local port, or start of local port range | ||
// 5: end of local port range | ||
// 7: remote port, or start of remote port range | ||
// 9: end or remote port range | ||
var specRegexp = regexp.MustCompile(`^((\[[0-9a-fA-F:]+]|\d+\.\d+\.\d+\.\d+):)?(\d+)(-(\d+))?(:(\d+)(-(\d+))?)?$`) | ||
func parseSrcDestPorts(in string) ([]parsedSrcDestPort, error) { | ||
var ( | ||
err error | ||
localAddr = netip.AddrFrom4([4]byte{127, 0, 0, 1}) | ||
remoteAddr = netip.AddrFrom4([4]byte{127, 0, 0, 1}) | ||
) | ||
groups := specRegexp.FindStringSubmatch(in) | ||
if len(groups) == 0 { | ||
return nil, xerrors.Errorf("invalid port specification %q", in) | ||
} | ||
if groups[2] != "" { | ||
localAddr, err = netip.ParseAddr(strings.Trim(groups[2], "[]")) | ||
if err != nil { | ||
return nil, xerrors.Errorf("invalid IP address %q",groups[2]) | ||
} | ||
} | ||
local, err := parsePortRange(groups[3], groups[5]) | ||
if err != nil { | ||
return nil, xerrors.Errorf("parse local port range from %q: %w", in, err) | ||
} | ||
remote := local | ||
if groups[7] != "" { | ||
remote, err = parsePortRange(groups[7], groups[9]) | ||
if err != nil { | ||
return nil, xerrors.Errorf("parse remote port range from %q: %w", in, err) | ||
} | ||
} | ||
if len(local) != len(remote) { | ||
return nil, xerrors.Errorf("port ranges must be the same length, got %d ports forwarded to %d ports", len(local), len(remote)) | ||
@@ -397,18 +388,17 @@ func parseSrcDestPorts(in string) ([]parsedSrcDestPort, error) { | ||
return out, nil | ||
} | ||
func parsePortRange(s, e string) ([]uint16, error) { | ||
start, err := parsePort(s) | ||
if err != nil { | ||
return nil, xerrors.Errorf("parse range start port from %q: %w",s, err) | ||
} | ||
end := start | ||
if len(e) != 0 { | ||
end, err = parsePort(e) | ||
if err != nil { | ||
return nil, xerrors.Errorf("parse range end port from %q: %w", e, err) | ||
} | ||
} | ||
if end < start { | ||
return nil, xerrors.Errorf("range end port %v is less than start port %v", end, start) | ||
78 changes: 53 additions & 25 deletionscli/portforward_internal_test.go
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
10 changes: 10 additions & 0 deletionscli/portforward_test.go
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.