mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
direct: support dial timeout and relay timeout (#97)
This commit is contained in:
1 parent
3392db41de
commit
665d722d2c
7 files changed
+65
-32
No files matched your search
+31
-25
@@ -10,46 +10,48 @@ import (
|
||||
|
||||
// Direct proxy
|
||||
type Direct struct {
|
||||
iface *net.Interface // interface specified by user
|
||||
ip net.IP
|
||||
iface *net.Interface // interface specified by user
|
||||
ip net.IP
|
||||
dialTimeout time.Duration
|
||||
relayTimeout time.Duration
|
||||
}
|
||||
|
||||
// Default dialer
|
||||
var Default = &Direct{}
|
||||
var Default = &Direct{dialTimeout: time.Second * 3}
|
||||
|
||||
// NewDirect returns a Direct dialer
|
||||
func NewDirect(intface string) (*Direct, error) {
|
||||
if intface == "" {
|
||||
return &Direct{}, nil
|
||||
// NewDirect returns a Direct dialer.
|
||||
func NewDirect(intface string, dialTimeout, relayTimeout time.Duration) (*Direct, error) {
|
||||
d := &Direct{dialTimeout: dialTimeout, relayTimeout: relayTimeout}
|
||||
|
||||
if intface != "" {
|
||||
if ip := net.ParseIP(intface); ip != nil {
|
||||
d.ip = net.ParseIP(intface)
|
||||
} else {
|
||||
iface, err := net.InterfaceByName(intface)
|
||||
if err != nil {
|
||||
return nil, errors.New(err.Error() + ": " + intface)
|
||||
}
|
||||
d.iface = iface
|
||||
}
|
||||
}
|
||||
|
||||
ip := net.ParseIP(intface)
|
||||
if ip != nil {
|
||||
return &Direct{ip: ip}, nil
|
||||
}
|
||||
|
||||
iface, err := net.InterfaceByName(intface)
|
||||
if err != nil {
|
||||
return nil, errors.New(err.Error() + ": " + intface)
|
||||
}
|
||||
|
||||
return &Direct{iface: iface}, nil
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// Addr returns forwarder's address
|
||||
// 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) (c net.Conn, err error) {
|
||||
if d.iface == nil || d.ip != nil {
|
||||
c, err = dial(network, addr, d.ip)
|
||||
c, err = d.dial(network, addr, d.ip)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, ip := range d.IFaceIPs() {
|
||||
c, err = dial(network, addr, ip)
|
||||
c, err = d.dial(network, addr, ip)
|
||||
if err == nil {
|
||||
d.ip = ip
|
||||
break
|
||||
@@ -64,7 +66,7 @@ func (d *Direct) Dial(network, addr string) (c net.Conn, err error) {
|
||||
return c, err
|
||||
}
|
||||
|
||||
func dial(network, addr string, localIP net.IP) (net.Conn, error) {
|
||||
func (d *Direct) dial(network, addr string, localIP net.IP) (net.Conn, error) {
|
||||
if network == "uot" {
|
||||
network = "udp"
|
||||
}
|
||||
@@ -77,7 +79,7 @@ func dial(network, addr string, localIP net.IP) (net.Conn, error) {
|
||||
la = &net.UDPAddr{IP: localIP}
|
||||
}
|
||||
|
||||
dialer := &net.Dialer{LocalAddr: la, Timeout: time.Second * 3}
|
||||
dialer := &net.Dialer{LocalAddr: la, Timeout: d.dialTimeout}
|
||||
c, err := dialer.Dial(network, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -87,10 +89,14 @@ func dial(network, addr string, localIP net.IP) (net.Conn, error) {
|
||||
c.SetKeepAlive(true)
|
||||
}
|
||||
|
||||
if d.relayTimeout != 0 {
|
||||
c.SetDeadline(time.Now().Add(d.relayTimeout))
|
||||
}
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
// DialUDP connects to the given address
|
||||
// DialUDP connects to the given address.
|
||||
func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
|
||||
// TODO: support specifying local interface
|
||||
var la string
|
||||
@@ -108,7 +114,7 @@ func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error)
|
||||
return pc, uAddr, err
|
||||
}
|
||||
|
||||
// IFaceIPs returns ip addresses according to the specified interface
|
||||
// IFaceIPs returns ip addresses according to the specified interface.
|
||||
func (d *Direct) IFaceIPs() (ips []net.IP) {
|
||||
ipnets, err := d.iface.Addrs()
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user