mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
forwarder: support curl like interface
parameter
This commit is contained in:
parent
c21a7d8c4d
commit
c9e9ebd287
@ -222,6 +222,8 @@ Forwarder option scheme: FORWARD_URL#OPTIONS
|
||||
Examples:
|
||||
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&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):
|
||||
# COMMENT LINE
|
||||
|
@ -8,51 +8,62 @@ import (
|
||||
|
||||
// Direct proxy
|
||||
type Direct struct {
|
||||
*net.Dialer
|
||||
addr net.Addr
|
||||
iface *net.Interface
|
||||
ip net.IP
|
||||
}
|
||||
|
||||
// Default dialer
|
||||
var Default = &Direct{Dialer: &net.Dialer{}}
|
||||
var Default = &Direct{}
|
||||
|
||||
// NewDirect returns a Direct dialer
|
||||
func NewDirect(intface string) *Direct {
|
||||
d := &Direct{}
|
||||
dialer := &net.Dialer{}
|
||||
func NewDirect(intface string) (*Direct, error) {
|
||||
if intface == "" {
|
||||
return &Direct{}, nil
|
||||
}
|
||||
|
||||
ip := net.ParseIP(intface)
|
||||
if ip == nil {
|
||||
if ip != nil {
|
||||
return &Direct{ip: ip}, nil
|
||||
}
|
||||
|
||||
iface, err := net.InterfaceByName(intface)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
d.addr = addrs[0]
|
||||
}
|
||||
}
|
||||
|
||||
d.addr = &net.TCPAddr{
|
||||
IP: ip,
|
||||
Port: 0,
|
||||
}
|
||||
|
||||
dialer.LocalAddr = d.addr
|
||||
|
||||
return d
|
||||
return &Direct{iface: iface}, nil
|
||||
}
|
||||
|
||||
// Addr returns forwarder's address
|
||||
func (d *Direct) Addr() string { return "DIRECT" }
|
||||
|
||||
// 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" {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -66,7 +77,8 @@ func (d *Direct) Dial(network, addr string) (net.Conn, error) {
|
||||
|
||||
// DialUDP connects to the given address
|
||||
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 {
|
||||
log.F("ListenPacket error: %s", 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
|
||||
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
|
||||
}
|
||||
|
@ -31,7 +31,12 @@ func ForwarderFromURL(s string) (f *Forwarder, err error) {
|
||||
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], ",") {
|
||||
d, err = DialerFromURL(url, d)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user