diff --git a/README.md b/README.md index 9feb379..3883ea9 100644 --- a/README.md +++ b/README.md @@ -217,7 +217,7 @@ Available forward strategies: Forwarder option scheme: FORWARD_URL#OPTIONS priority: set the priority of that forwarder, default:0 - interface: set local interface or ip address used to connect remote forwarder + interface: set local interface or ip address used to connect remote server - Examples: socks5://1.1.1.1:1080#priority=100 diff --git a/conf.go b/conf.go index 4dbc722..3963298 100644 --- a/conf.go +++ b/conf.go @@ -193,10 +193,13 @@ func usage() { fmt.Fprintf(os.Stderr, "Forwarder option scheme: FORWARD_URL#OPTIONS\n") fmt.Fprintf(os.Stderr, " priority: set the priority of that forwarder, default:0\n") + fmt.Fprintf(os.Stderr, " interface: set local interface or ip address used to connect remote server\n") fmt.Fprintf(os.Stderr, " -\n") fmt.Fprintf(os.Stderr, " Examples:\n") fmt.Fprintf(os.Stderr, " socks5://1.1.1.1:1080#priority=100\n") fmt.Fprintf(os.Stderr, " vmess://[security:]uuid@host:port?alterID=num#priority=200\n") + fmt.Fprintf(os.Stderr, " vmess://[security:]uuid@host:port?alterID=num#priority=200&interface=192.168.1.99\n") + fmt.Fprintf(os.Stderr, " vmess://[security:]uuid@host:port?alterID=num#priority=200&interface=eth0\n") fmt.Fprintf(os.Stderr, "\n") fmt.Fprintf(os.Stderr, "Config file format(see `"+app+".conf.example` as an example):\n") diff --git a/proxy/direct.go b/proxy/direct.go index 18ba309..637a33c 100644 --- a/proxy/direct.go +++ b/proxy/direct.go @@ -8,7 +8,7 @@ import ( // Direct proxy type Direct struct { - iface *net.Interface + iface *net.Interface // interface specified by user ip net.IP } @@ -39,30 +39,36 @@ func (d *Direct) Addr() string { return "DIRECT" } // Dial connects to the address addr on the network net func (d *Direct) Dial(network, addr string) (c net.Conn, err error) { - for _, ip := range d.LocalIPs() { - c, err = d.dial(network, addr, ip) - // log.F("dial %s using ip: %s", addr, ip) + c, err = dial(network, addr, d.ip) + if err == nil { + return + } + + for _, ip := range d.IFaceIPs() { + c, err = dial(network, addr, ip) if err == nil { + d.ip = ip break } } + return } -func (d *Direct) dial(network, addr string, localIP net.IP) (net.Conn, error) { +func dial(network, addr string, localIP net.IP) (net.Conn, error) { if network == "uot" { network = "udp" } - var localAddr net.Addr + var la net.Addr switch network { case "tcp": - localAddr = &net.TCPAddr{IP: localIP} + la = &net.TCPAddr{IP: localIP} case "udp": - localAddr = &net.UDPAddr{IP: localIP} + la = &net.UDPAddr{IP: localIP} } - dialer := &net.Dialer{LocalAddr: localAddr} + dialer := &net.Dialer{LocalAddr: la} c, err := dialer.Dial(network, addr) if err != nil { return nil, err @@ -78,7 +84,12 @@ func (d *Direct) dial(network, addr string, localIP net.IP) (net.Conn, error) { // DialUDP connects to the given address func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) { // TODO: support specifying local interface - pc, err := net.ListenPacket(network, "") + la := "" + if d.ip != nil { + la = d.ip.String() + ":0" + } + + pc, err := net.ListenPacket(network, la) if err != nil { log.F("ListenPacket error: %s", err) return nil, nil, err @@ -91,18 +102,8 @@ func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) // NextDialer returns the next dialer func (d *Direct) NextDialer(dstAddr string) Dialer { return d } -// LocalIPs returns ip addresses according to the specified interface -func (d *Direct) LocalIPs() (ips []net.IP) { - if d.ip != nil { - ips = []net.IP{d.ip} - return - } - - if d.iface == nil { - ips = []net.IP{nil} - return - } - +// IFaceIPs returns ip addresses according to the specified interface +func (d *Direct) IFaceIPs() (ips []net.IP) { ipnets, err := d.iface.Addrs() if err != nil { return