direct: support dial timeout and relay timeout (#97)

This commit is contained in:
nadoo 2020-05-04 16:51:41 +08:00
parent 3392db41de
commit 665d722d2c
7 changed files with 65 additions and 32 deletions

View File

@ -101,6 +101,8 @@ glider -h
proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80 (default "www.apple.com")
-config string
config file path
-dialtimeout int
dial timeout(seconds) (default 3)
-dns string
local dns server listen address
-dnsalwaystcp
@ -125,6 +127,8 @@ glider -h
listen url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS
-maxfailures int
max failures to change forwarder status to disabled (default 3)
-relaytimeout int
relay timeout(seconds)
-rulefile value
rule file path
-rules-dir string

View File

@ -45,6 +45,8 @@ func confInit() {
flag.IntVar(&conf.StrategyConfig.CheckTimeout, "checktimeout", 10, "proxy check timeout(seconds)")
flag.BoolVar(&conf.StrategyConfig.CheckDisabledOnly, "checkdisabledonly", false, "check disabled fowarders only")
flag.IntVar(&conf.StrategyConfig.MaxFailures, "maxfailures", 3, "max failures to change forwarder status to disabled")
flag.IntVar(&conf.StrategyConfig.DialTimeout, "dialtimeout", 3, "dial timeout(seconds)")
flag.IntVar(&conf.StrategyConfig.RelayTimeout, "relaytimeout", 0, "relay timeout(seconds)")
flag.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface")
flag.StringSliceUniqVar(&conf.RuleFile, "rulefile", nil, "rule file path")

View File

@ -150,6 +150,20 @@ listen=socks5://:1080
# Destination Hashing mode: dh
strategy=rr
# FORWARDER SETTINGS
# ------------------
# We can set some parameters for forwarders.
# forwarder will be set to disabled on how many failures counted(both dial and relay).
maxfailures=3
# timeout for create a connection(seconds)
# dialtimeout=3
# timeout for relay data from proxy server and client(seconds)
# DO NOT change it if you don't know what will happen.
# relaytimeout=0
# FORWARDERS CHECK
# ----------------
@ -171,7 +185,7 @@ checkdisabledonly=false
# DNS FORWARDING SERVER
# ----------------
# we can specify different upstream dns server in rule file for different destinations
# we can specify different upstream dns server in rule file for different destinations.
# Setup a dns forwarding server
dns=:53

View File

@ -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 {

View File

@ -38,6 +38,8 @@ func NewConfFromFile(ruleFile string) (*Config, error) {
f.IntVar(&p.StrategyConfig.CheckTimeout, "checktimeout", 10, "proxy check timeout(seconds)")
f.BoolVar(&p.StrategyConfig.CheckDisabledOnly, "checkdisabledonly", false, "check disabled fowarders only")
f.IntVar(&p.StrategyConfig.MaxFailures, "maxfailures", 3, "max failures to change forwarder status to disabled")
f.IntVar(&p.StrategyConfig.DialTimeout, "dialtimeout", 3, "dial timeout(seconds)")
f.IntVar(&p.StrategyConfig.RelayTimeout, "relaytimeout", 0, "relay timeout(seconds)")
f.StringVar(&p.StrategyConfig.IntFace, "interface", "", "source ip or source interface")
f.StringSliceUniqVar(&p.DNSServers, "dnsserver", nil, "remote dns server")

View File

@ -6,6 +6,7 @@ import (
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/nadoo/glider/common/log"
"github.com/nadoo/glider/proxy"
@ -28,7 +29,7 @@ type Forwarder struct {
}
// ForwarderFromURL parses `forward=` command value and returns a new forwarder.
func ForwarderFromURL(s, intface string) (f *Forwarder, err error) {
func ForwarderFromURL(s, intface string, dialTimeout, relayTimeout time.Duration) (f *Forwarder, err error) {
f = &Forwarder{}
ss := strings.Split(s, "#")
@ -42,7 +43,7 @@ func ForwarderFromURL(s, intface string) (f *Forwarder, err error) {
}
var d proxy.Dialer
d, err = proxy.NewDirect(iface)
d, err = proxy.NewDirect(iface, dialTimeout, relayTimeout)
if err != nil {
return nil, err
}
@ -64,8 +65,8 @@ func ForwarderFromURL(s, intface string) (f *Forwarder, err error) {
}
// DirectForwarder returns a direct forwarder.
func DirectForwarder(intface string) *Forwarder {
d, err := proxy.NewDirect(intface)
func DirectForwarder(intface string, dialTimeout, relayTimeout time.Duration) *Forwarder {
d, err := proxy.NewDirect(intface, dialTimeout, relayTimeout)
if err != nil {
return nil
}

View File

@ -23,6 +23,8 @@ type Config struct {
CheckTimeout int
CheckDisabledOnly bool
MaxFailures int
DialTimeout int
RelayTimeout int
IntFace string
}
@ -48,7 +50,8 @@ type Proxy struct {
func NewProxy(s []string, c *Config) *Proxy {
var fwdrs []*Forwarder
for _, chain := range s {
fwdr, err := ForwarderFromURL(chain, c.IntFace)
fwdr, err := ForwarderFromURL(chain, c.IntFace,
time.Duration(c.DialTimeout)*time.Second, time.Duration(c.RelayTimeout)*time.Second)
if err != nil {
log.Fatal(err)
}
@ -58,7 +61,8 @@ func NewProxy(s []string, c *Config) *Proxy {
if len(fwdrs) == 0 {
// direct forwarder
fwdrs = append(fwdrs, DirectForwarder(c.IntFace))
fwdrs = append(fwdrs, DirectForwarder(c.IntFace,
time.Duration(c.DialTimeout)*time.Second, time.Duration(c.RelayTimeout)*time.Second))
c.Strategy = "rr"
}