-
Notifications
You must be signed in to change notification settings - Fork 1
[tunnel] --port flag for explicit destination port #78
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,12 +19,13 @@ import ( | |||||||||||||||||||||||||
| type ProtocolHandler func(stack.TransportEndpointID, *stack.PacketBuffer) bool | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // TCPForwarder forwards TCP connections to an upstream network. | ||||||||||||||||||||||||||
| func TCPForwarder(ctx context.Context, ipstack *stack.Stack, upstream network.Network) ProtocolHandler { | ||||||||||||||||||||||||||
| // If overridePort is not empty, all connections will be forwarded to that port instead of the original destination port. | ||||||||||||||||||||||||||
| func TCPForwarder(ctx context.Context, ipstack *stack.Stack, upstream network.Network, overridePort string) ProtocolHandler { | ||||||||||||||||||||||||||
| tcpForwarder := tcp.NewForwarder( | ||||||||||||||||||||||||||
| ipstack, | ||||||||||||||||||||||||||
| 0, /* rcvWnd (0 - default) */ | ||||||||||||||||||||||||||
| 65535, /* maxInFlight */ | ||||||||||||||||||||||||||
| tcpHandler(ctx, upstream), | ||||||||||||||||||||||||||
| tcpHandler(ctx, upstream, overridePort), | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return tcpForwarder.HandlePacket | ||||||||||||||||||||||||||
|
|
@@ -51,15 +52,24 @@ func Unmap4in6(addr netip.Addr) netip.Addr { | |||||||||||||||||||||||||
| return v4addr | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func tcpHandler(ctx context.Context, upstream network.Network) func(req *tcp.ForwarderRequest) { | ||||||||||||||||||||||||||
| func tcpHandler(ctx context.Context, upstream network.Network, overridePort string) func(req *tcp.ForwarderRequest) { | ||||||||||||||||||||||||||
| return func(req *tcp.ForwarderRequest) { | ||||||||||||||||||||||||||
| reqDetails := req.ID() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| srcAddrPort := netip.AddrPortFrom(addrFromNetstackIP(reqDetails.RemoteAddress), reqDetails.RemotePort) | ||||||||||||||||||||||||||
| dstAddrPort := netip.AddrPortFrom( | ||||||||||||||||||||||||||
| Unmap4in6(addrFromNetstackIP(reqDetails.LocalAddress)), | ||||||||||||||||||||||||||
| reqDetails.LocalPort, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Extract IPv4 address from IPv6-mapped address | ||||||||||||||||||||||||||
| dstAddr := Unmap4in6(addrFromNetstackIP(reqDetails.LocalAddress)) | ||||||||||||||||||||||||||
| dstPort := reqDetails.LocalPort | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
Comment on lines
+60
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
These comments are not needed |
||||||||||||||||||||||||||
| // Override port if specified | ||||||||||||||||||||||||||
| if overridePort != "" { | ||||||||||||||||||||||||||
| if port, err := netip.ParseAddrPort("127.0.0.1:" + overridePort); err == nil { | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can just strconv.Atoi instead - this is confusing (why is there 127.0.0.1 in there?) Also it swallows the error |
||||||||||||||||||||||||||
| dstPort = port.Port() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| dstAddrPort := netip.AddrPortFrom(dstAddr, dstPort) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| logger := slog.With( | ||||||||||||||||||||||||||
| slog.String("src", srcAddrPort.String()), | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Port should be a uint