mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
direct: support dial timeout and relay timeout (#97)
This commit is contained in:
parent
3392db41de
commit
665d722d2c
@ -101,6 +101,8 @@ glider -h
|
|||||||
proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80 (default "www.apple.com")
|
proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80 (default "www.apple.com")
|
||||||
-config string
|
-config string
|
||||||
config file path
|
config file path
|
||||||
|
-dialtimeout int
|
||||||
|
dial timeout(seconds) (default 3)
|
||||||
-dns string
|
-dns string
|
||||||
local dns server listen address
|
local dns server listen address
|
||||||
-dnsalwaystcp
|
-dnsalwaystcp
|
||||||
@ -125,6 +127,8 @@ glider -h
|
|||||||
listen url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS
|
listen url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS
|
||||||
-maxfailures int
|
-maxfailures int
|
||||||
max failures to change forwarder status to disabled (default 3)
|
max failures to change forwarder status to disabled (default 3)
|
||||||
|
-relaytimeout int
|
||||||
|
relay timeout(seconds)
|
||||||
-rulefile value
|
-rulefile value
|
||||||
rule file path
|
rule file path
|
||||||
-rules-dir string
|
-rules-dir string
|
||||||
|
2
conf.go
2
conf.go
@ -45,6 +45,8 @@ func confInit() {
|
|||||||
flag.IntVar(&conf.StrategyConfig.CheckTimeout, "checktimeout", 10, "proxy check timeout(seconds)")
|
flag.IntVar(&conf.StrategyConfig.CheckTimeout, "checktimeout", 10, "proxy check timeout(seconds)")
|
||||||
flag.BoolVar(&conf.StrategyConfig.CheckDisabledOnly, "checkdisabledonly", false, "check disabled fowarders only")
|
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.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.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface")
|
||||||
|
|
||||||
flag.StringSliceUniqVar(&conf.RuleFile, "rulefile", nil, "rule file path")
|
flag.StringSliceUniqVar(&conf.RuleFile, "rulefile", nil, "rule file path")
|
||||||
|
@ -150,6 +150,20 @@ listen=socks5://:1080
|
|||||||
# Destination Hashing mode: dh
|
# Destination Hashing mode: dh
|
||||||
strategy=rr
|
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
|
# FORWARDERS CHECK
|
||||||
# ----------------
|
# ----------------
|
||||||
@ -171,7 +185,7 @@ checkdisabledonly=false
|
|||||||
|
|
||||||
# DNS FORWARDING SERVER
|
# 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
|
# Setup a dns forwarding server
|
||||||
dns=:53
|
dns=:53
|
||||||
|
@ -12,44 +12,46 @@ import (
|
|||||||
type Direct struct {
|
type Direct struct {
|
||||||
iface *net.Interface // interface specified by user
|
iface *net.Interface // interface specified by user
|
||||||
ip net.IP
|
ip net.IP
|
||||||
|
dialTimeout time.Duration
|
||||||
|
relayTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default dialer
|
// Default dialer
|
||||||
var Default = &Direct{}
|
var Default = &Direct{dialTimeout: time.Second * 3}
|
||||||
|
|
||||||
// NewDirect returns a Direct dialer
|
// NewDirect returns a Direct dialer.
|
||||||
func NewDirect(intface string) (*Direct, error) {
|
func NewDirect(intface string, dialTimeout, relayTimeout time.Duration) (*Direct, error) {
|
||||||
if intface == "" {
|
d := &Direct{dialTimeout: dialTimeout, relayTimeout: relayTimeout}
|
||||||
return &Direct{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ip := net.ParseIP(intface)
|
|
||||||
if ip != nil {
|
|
||||||
return &Direct{ip: ip}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if intface != "" {
|
||||||
|
if ip := net.ParseIP(intface); ip != nil {
|
||||||
|
d.ip = net.ParseIP(intface)
|
||||||
|
} else {
|
||||||
iface, err := net.InterfaceByName(intface)
|
iface, err := net.InterfaceByName(intface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(err.Error() + ": " + intface)
|
return nil, errors.New(err.Error() + ": " + intface)
|
||||||
}
|
}
|
||||||
|
d.iface = iface
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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" }
|
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) (c net.Conn, err error) {
|
func (d *Direct) Dial(network, addr string) (c net.Conn, err error) {
|
||||||
if d.iface == nil || d.ip != nil {
|
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 {
|
if err == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ip := range d.IFaceIPs() {
|
for _, ip := range d.IFaceIPs() {
|
||||||
c, err = dial(network, addr, ip)
|
c, err = d.dial(network, addr, ip)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
d.ip = ip
|
d.ip = ip
|
||||||
break
|
break
|
||||||
@ -64,7 +66,7 @@ func (d *Direct) Dial(network, addr string) (c net.Conn, err error) {
|
|||||||
return c, err
|
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" {
|
if network == "uot" {
|
||||||
network = "udp"
|
network = "udp"
|
||||||
}
|
}
|
||||||
@ -77,7 +79,7 @@ func dial(network, addr string, localIP net.IP) (net.Conn, error) {
|
|||||||
la = &net.UDPAddr{IP: localIP}
|
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)
|
c, err := dialer.Dial(network, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -87,10 +89,14 @@ func dial(network, addr string, localIP net.IP) (net.Conn, error) {
|
|||||||
c.SetKeepAlive(true)
|
c.SetKeepAlive(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if d.relayTimeout != 0 {
|
||||||
|
c.SetDeadline(time.Now().Add(d.relayTimeout))
|
||||||
|
}
|
||||||
|
|
||||||
return c, err
|
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) {
|
func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
|
||||||
// TODO: support specifying local interface
|
// TODO: support specifying local interface
|
||||||
var la string
|
var la string
|
||||||
@ -108,7 +114,7 @@ func (d *Direct) DialUDP(network, addr string) (net.PacketConn, net.Addr, error)
|
|||||||
return pc, uAddr, err
|
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) {
|
func (d *Direct) IFaceIPs() (ips []net.IP) {
|
||||||
ipnets, err := d.iface.Addrs()
|
ipnets, err := d.iface.Addrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -38,6 +38,8 @@ func NewConfFromFile(ruleFile string) (*Config, error) {
|
|||||||
f.IntVar(&p.StrategyConfig.CheckTimeout, "checktimeout", 10, "proxy check timeout(seconds)")
|
f.IntVar(&p.StrategyConfig.CheckTimeout, "checktimeout", 10, "proxy check timeout(seconds)")
|
||||||
f.BoolVar(&p.StrategyConfig.CheckDisabledOnly, "checkdisabledonly", false, "check disabled fowarders only")
|
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.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.StringVar(&p.StrategyConfig.IntFace, "interface", "", "source ip or source interface")
|
||||||
|
|
||||||
f.StringSliceUniqVar(&p.DNSServers, "dnsserver", nil, "remote dns server")
|
f.StringSliceUniqVar(&p.DNSServers, "dnsserver", nil, "remote dns server")
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/nadoo/glider/common/log"
|
"github.com/nadoo/glider/common/log"
|
||||||
"github.com/nadoo/glider/proxy"
|
"github.com/nadoo/glider/proxy"
|
||||||
@ -28,7 +29,7 @@ type Forwarder struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ForwarderFromURL parses `forward=` command value and returns a new forwarder.
|
// 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{}
|
f = &Forwarder{}
|
||||||
|
|
||||||
ss := strings.Split(s, "#")
|
ss := strings.Split(s, "#")
|
||||||
@ -42,7 +43,7 @@ func ForwarderFromURL(s, intface string) (f *Forwarder, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var d proxy.Dialer
|
var d proxy.Dialer
|
||||||
d, err = proxy.NewDirect(iface)
|
d, err = proxy.NewDirect(iface, dialTimeout, relayTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -64,8 +65,8 @@ func ForwarderFromURL(s, intface string) (f *Forwarder, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DirectForwarder returns a direct forwarder.
|
// DirectForwarder returns a direct forwarder.
|
||||||
func DirectForwarder(intface string) *Forwarder {
|
func DirectForwarder(intface string, dialTimeout, relayTimeout time.Duration) *Forwarder {
|
||||||
d, err := proxy.NewDirect(intface)
|
d, err := proxy.NewDirect(intface, dialTimeout, relayTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,8 @@ type Config struct {
|
|||||||
CheckTimeout int
|
CheckTimeout int
|
||||||
CheckDisabledOnly bool
|
CheckDisabledOnly bool
|
||||||
MaxFailures int
|
MaxFailures int
|
||||||
|
DialTimeout int
|
||||||
|
RelayTimeout int
|
||||||
IntFace string
|
IntFace string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +50,8 @@ type Proxy struct {
|
|||||||
func NewProxy(s []string, c *Config) *Proxy {
|
func NewProxy(s []string, c *Config) *Proxy {
|
||||||
var fwdrs []*Forwarder
|
var fwdrs []*Forwarder
|
||||||
for _, chain := range s {
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -58,7 +61,8 @@ func NewProxy(s []string, c *Config) *Proxy {
|
|||||||
|
|
||||||
if len(fwdrs) == 0 {
|
if len(fwdrs) == 0 {
|
||||||
// direct forwarder
|
// 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"
|
c.Strategy = "rr"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user