forwarder: support curl like interface parameter

This commit is contained in:
nadoo 2018-08-19 01:49:52 +08:00
parent c21a7d8c4d
commit c9e9ebd287
3 changed files with 69 additions and 26 deletions

View File

@ -222,6 +222,8 @@ Forwarder option scheme: FORWARD_URL#OPTIONS
Examples: Examples:
socks5://1.1.1.1:1080#priority=100 socks5://1.1.1.1:1080#priority=100
vmess://[security:]uuid@host:port?alterID=num#priority=200 vmess://[security:]uuid@host:port?alterID=num#priority=200
vmess://[security:]uuid@host:port?alterID=num#priority=200&interface=192.168.1.99
vmess://[security:]uuid@host:port?alterID=num#priority=200&interface=eth0
Config file format(see `glider.conf.example` as an example): Config file format(see `glider.conf.example` as an example):
# COMMENT LINE # COMMENT LINE

View File

@ -8,51 +8,62 @@ import (
// Direct proxy // Direct proxy
type Direct struct { type Direct struct {
*net.Dialer iface *net.Interface
addr net.Addr ip net.IP
} }
// Default dialer // Default dialer
var Default = &Direct{Dialer: &net.Dialer{}} var Default = &Direct{}
// NewDirect returns a Direct dialer // NewDirect returns a Direct dialer
func NewDirect(intface string) *Direct { func NewDirect(intface string) (*Direct, error) {
d := &Direct{} if intface == "" {
dialer := &net.Dialer{} return &Direct{}, nil
}
ip := net.ParseIP(intface) ip := net.ParseIP(intface)
if ip == nil { if ip != nil {
iface, err := net.InterfaceByName(intface) return &Direct{ip: ip}, nil
if err != nil {
return nil
}
addrs, err := iface.Addrs()
if err != nil {
d.addr = addrs[0]
}
} }
d.addr = &net.TCPAddr{ iface, err := net.InterfaceByName(intface)
IP: ip, if err != nil {
Port: 0, return nil, err
} }
dialer.LocalAddr = d.addr return &Direct{iface: iface}, nil
return d
} }
// Addr returns forwarder's address // Addr returns forwarder's address
func (d *Direct) Addr() string { return "DIRECT" } func (d *Direct) Addr() string { return "DIRECT" }
// Dial connects to the address addr on the network net // Dial connects to the address addr on the network net
func (d *Direct) Dial(network, addr string) (net.Conn, error) { 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)
if err == nil {
break
}
}
return
}
func (d *Direct) dial(network, addr string, localIP net.IP) (net.Conn, error) {
if network == "uot" { if network == "uot" {
network = "udp" network = "udp"
} }
c, err := d.Dialer.Dial(network, addr) var localAddr net.Addr
switch network {
case "tcp":
localAddr = &net.TCPAddr{IP: localIP}
case "udp":
localAddr = &net.UDPAddr{IP: localIP}
}
dialer := &net.Dialer{LocalAddr: localAddr}
c, err := dialer.Dial(network, addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -66,7 +77,8 @@ func (d *Direct) Dial(network, addr string) (net.Conn, error) {
// DialUDP connects to the given address // DialUDP connects to the given address
func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) { func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
pc, err := net.ListenPacket(network, d.Dialer.LocalAddr.String()) // TODO: support specifying local interface
pc, err := net.ListenPacket(network, "")
if err != nil { if err != nil {
log.F("ListenPacket error: %s", err) log.F("ListenPacket error: %s", err)
return nil, nil, err return nil, nil, err
@ -78,3 +90,27 @@ func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error)
// NextDialer returns the next dialer // NextDialer returns the next dialer
func (d *Direct) NextDialer(dstAddr string) Dialer { return d } 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
}
ipnets, err := d.iface.Addrs()
if err != nil {
return
}
for _, ipnet := range ipnets {
ips = append(ips, ipnet.(*net.IPNet).IP)
}
return
}

View File

@ -31,7 +31,12 @@ func ForwarderFromURL(s string) (f *Forwarder, err error) {
err = f.parseOption(ss[1]) err = f.parseOption(ss[1])
} }
var d Dialer = NewDirect(f.intface) var d Dialer
d, err = NewDirect(f.intface)
if err != nil {
return nil, err
}
for _, url := range strings.Split(ss[0], ",") { for _, url := range strings.Split(ss[0], ",") {
d, err = DialerFromURL(url, d) d, err = DialerFromURL(url, d)
if err != nil { if err != nil {