-
Notifications
You must be signed in to change notification settings - Fork 189
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
Use the IP PacketConn to specify the local proxy IP #110
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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2022 Jigsaw Operations LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build darwin || linux | ||
|
||
package net | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net" | ||
"runtime" | ||
|
||
"golang.org/x/net/ipv4" | ||
"golang.org/x/net/ipv6" | ||
) | ||
|
||
// UDPAnyConn extends net.PacketConn to allow reporting the destination IP | ||
// of incoming packets, and setting the source IP of outgoing packets. This | ||
// is relevant for UDP connections that are bound to `0.0.0.0` or `::`. In | ||
// these cases, net.PacketConn is not sufficient to enable sending a reply | ||
// from the expected source IP. | ||
type UDPAnyConn interface { | ||
net.PacketConn | ||
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. Why does it need to implement PacketConn? Do we ever call the PacketConn methods outside this class? Seems like an easy mistake to make, let's prevent that. 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. At a minimum, we need Note that all the |
||
ReadToFrom(p []byte) (n int, src *net.UDPAddr, dst net.IP, err error) | ||
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. Do we need to couple read and write? I've been finding a lot cleaner to keep them separate when possible. See my comment about a Shadowsocks writer below. 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. As noted above, we also need a few other |
||
WriteToFrom(p []byte, dst *net.UDPAddr, src net.IP) (int, error) | ||
} | ||
|
||
type udpAnyConnV4 struct { | ||
net.PacketConn | ||
v4 ipv4.PacketConn | ||
} | ||
|
||
// ListenAnyUDP4 returns a UDPAnyConn that is listening on all IPv4 addresses | ||
// at the specified port. If `port` is zero, the kernel will choose an open port. | ||
func ListenAnyUDP4(port int) (UDPAnyConn, error) { | ||
conn, err := net.ListenUDP("udp4", &net.UDPAddr{Port: port}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
anyConn := &udpAnyConnV4{conn, *ipv4.NewPacketConn(conn)} | ||
if err = anyConn.v4.SetControlMessage(ipv4.FlagDst, true); err != nil { | ||
return nil, err | ||
} | ||
return anyConn, nil | ||
} | ||
|
||
func (c *udpAnyConnV4) ReadToFrom(p []byte) (n int, src *net.UDPAddr, dst net.IP, err error) { | ||
var cm *ipv4.ControlMessage | ||
var tmpSrc net.Addr | ||
if n, cm, tmpSrc, err = c.v4.ReadFrom(p); err != nil { | ||
return | ||
} | ||
if cm != nil { | ||
dst = cm.Dst | ||
} else if runtime.GOOS != "windows" { | ||
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 a better way to do this is to add a build header flag, like This way we force people to not use it on windows or js. They will have to pick another implementation, and that's a decision to be made at development time. 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. OK, done. Note that this means outline-ss-server no longer supports Windows at all, instead of degrading gracefully to the old behavior. |
||
err = errors.New("control data is missing") | ||
return | ||
} | ||
src = tmpSrc.(*net.UDPAddr) | ||
return | ||
} | ||
|
||
func (c *udpAnyConnV4) WriteToFrom(p []byte, dst *net.UDPAddr, src net.IP) (int, error) { | ||
cm := &ipv4.ControlMessage{Src: src} | ||
return c.v4.WriteTo(p, cm, dst) | ||
} | ||
|
||
type udpAnyConnV6 struct { | ||
net.PacketConn | ||
v6 ipv6.PacketConn | ||
} | ||
|
||
// ListenAnyUDP4 returns a UDPAnyConn that is listening on all IPv6 addresses | ||
// at the specified port. If `port` is zero, the kernel will choose an open port. | ||
func ListenAnyUDP6(port int) (UDPAnyConn, error) { | ||
conn, err := net.ListenUDP("udp6", &net.UDPAddr{Port: port}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
anyConn := &udpAnyConnV6{conn, *ipv6.NewPacketConn(conn)} | ||
if err = anyConn.v6.SetControlMessage(ipv6.FlagDst, true); err != nil { | ||
return nil, err | ||
} | ||
return anyConn, nil | ||
} | ||
|
||
func (c *udpAnyConnV6) ReadToFrom(p []byte) (n int, src *net.UDPAddr, dst net.IP, err error) { | ||
var cm *ipv6.ControlMessage | ||
var tmpSrc net.Addr | ||
if n, cm, tmpSrc, err = c.v6.ReadFrom(p); err != nil { | ||
return | ||
} | ||
if cm != nil { | ||
dst = cm.Dst | ||
} else if runtime.GOOS != "windows" { | ||
err = errors.New("control data is missing") | ||
return | ||
} | ||
src = tmpSrc.(*net.UDPAddr) | ||
return | ||
} | ||
|
||
func (c *udpAnyConnV6) WriteToFrom(p []byte, dst *net.UDPAddr, src net.IP) (int, error) { | ||
cm := &ipv6.ControlMessage{Src: src} | ||
return c.v6.WriteTo(p, cm, dst) | ||
} | ||
|
||
type boundWriter struct { | ||
conn UDPAnyConn | ||
dst *net.UDPAddr | ||
src net.IP | ||
} | ||
|
||
func (w boundWriter) Write(p []byte) (int, error) { | ||
return w.conn.WriteToFrom(p, w.dst, w.src) | ||
} | ||
|
||
// MakeBoundWriter returns a Writer that mimics the behavior of Write() on a | ||
// connected UDPConn. | ||
func MakeBoundWriter(conn UDPAnyConn, dst *net.UDPAddr, src net.IP) io.Writer { | ||
return boundWriter{conn, dst, src} | ||
} |
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.
uses: codecov/codecov-action@v2.1.0