general: add global setting interface

This commit is contained in:
nadoo 2018-08-20 22:23:00 +08:00
parent edd5964a1e
commit 8daa7784b0
8 changed files with 45 additions and 26 deletions

View File

@ -44,6 +44,7 @@ func confInit() {
// TODO: change to checkinterval // TODO: change to checkinterval
flag.IntVar(&conf.StrategyConfig.CheckInterval, "checkduration", 30, "proxy check interval(seconds)") flag.IntVar(&conf.StrategyConfig.CheckInterval, "checkduration", 30, "proxy check interval(seconds)")
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.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")
flag.StringVar(&conf.RulesDir, "rules-dir", "", "rule file folder") flag.StringVar(&conf.RulesDir, "rules-dir", "", "rule file folder")

View File

@ -37,17 +37,18 @@ func RegisterDialer(name string, c DialerCreator) {
} }
// DialerFromURL calls the registered creator to create dialers. // DialerFromURL calls the registered creator to create dialers.
// dialer is the default upstream dialer so cannot be nil, we can use Default when calling this function.
func DialerFromURL(s string, dialer Dialer) (Dialer, error) { func DialerFromURL(s string, dialer Dialer) (Dialer, error) {
if dialer == nil {
return nil, errors.New("DialerFromURL: dialer cannot be nil")
}
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
log.F("parse err: %s", err) log.F("parse err: %s", err)
return nil, err return nil, err
} }
if dialer == nil {
dialer = Default
}
c, ok := dialerMap[strings.ToLower(u.Scheme)] c, ok := dialerMap[strings.ToLower(u.Scheme)]
if ok { if ok {
return c(s, dialer) return c(s, dialer)

View File

@ -1,6 +1,7 @@
package proxy package proxy
import ( import (
"errors"
"net" "net"
"github.com/nadoo/glider/common/log" "github.com/nadoo/glider/common/log"
@ -28,7 +29,7 @@ func NewDirect(intface string) (*Direct, error) {
iface, err := net.InterfaceByName(intface) iface, err := net.InterfaceByName(intface)
if err != nil { if err != nil {
return nil, err return nil, errors.New(err.Error() + ": " + intface)
} }
return &Direct{iface: iface}, nil return &Direct{iface: iface}, nil
@ -39,9 +40,11 @@ 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) {
c, err = dial(network, addr, d.ip) if d.iface == nil || d.ip != nil {
if err == nil { c, err = dial(network, addr, d.ip)
return if err == nil {
return
}
} }
for _, ip := range d.IFaceIPs() { for _, ip := range d.IFaceIPs() {

View File

@ -14,16 +14,17 @@ import (
type Forwarder struct { type Forwarder struct {
Dialer Dialer
Priority int Priority int
addr string
disabled uint32
failures uint32
MaxFailures uint32 //maxfailures to set to Disabled MaxFailures uint32 //maxfailures to set to Disabled
latency int64
intface string // local interface or ip address addr string
disabled uint32
failures uint32
latency int64
intface string // local interface or ip address
} }
// ForwarderFromURL parses `forward=` command value and returns a new forwarder // ForwarderFromURL parses `forward=` command value and returns a new forwarder
func ForwarderFromURL(s string) (f *Forwarder, err error) { func ForwarderFromURL(s, intface string) (f *Forwarder, err error) {
f = &Forwarder{} f = &Forwarder{}
ss := strings.Split(s, "#") ss := strings.Split(s, "#")
@ -31,8 +32,13 @@ func ForwarderFromURL(s string) (f *Forwarder, err error) {
err = f.parseOption(ss[1]) err = f.parseOption(ss[1])
} }
iface := intface
if f.intface != "" && f.intface != intface {
iface = f.intface
}
var d Dialer var d Dialer
d, err = NewDirect(f.intface) d, err = NewDirect(iface)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -27,7 +27,12 @@ func RegisterServer(name string, c ServerCreator) {
} }
// ServerFromURL calls the registered creator to create proxy servers. // ServerFromURL calls the registered creator to create proxy servers.
// dialer is the default upstream dialer so cannot be nil, we can use Default when calling this function.
func ServerFromURL(s string, dialer Dialer) (Server, error) { func ServerFromURL(s string, dialer Dialer) (Server, error) {
if dialer == nil {
return nil, errors.New("ServerFromURL: dialer cannot be nil")
}
if !strings.Contains(s, "://") { if !strings.Contains(s, "://") {
s = "mixed://" + s s = "mixed://" + s
} }
@ -38,10 +43,6 @@ func ServerFromURL(s string, dialer Dialer) (Server, error) {
return nil, err return nil, err
} }
if dialer == nil {
dialer = Default
}
c, ok := serverMap[strings.ToLower(u.Scheme)] c, ok := serverMap[strings.ToLower(u.Scheme)]
if ok { if ok {
return c(s, dialer) return c(s, dialer)

View File

@ -36,6 +36,7 @@ func NewConfFromFile(ruleFile string) (*Config, error) {
f.StringVar(&p.StrategyConfig.CheckWebSite, "checkwebsite", "www.apple.com", "proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80") f.StringVar(&p.StrategyConfig.CheckWebSite, "checkwebsite", "www.apple.com", "proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80")
// TODO: change to checkinterval // TODO: change to checkinterval
f.IntVar(&p.StrategyConfig.CheckInterval, "checkduration", 30, "proxy check interval(seconds)") f.IntVar(&p.StrategyConfig.CheckInterval, "checkduration", 30, "proxy check interval(seconds)")
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")
f.StringVar(&p.IPSet, "ipset", "", "ipset name") f.StringVar(&p.IPSet, "ipset", "", "ipset name")

View File

@ -25,20 +25,20 @@ func NewDialer(rules []*Config, gDialer proxy.Dialer) *Dialer {
rd := &Dialer{gDialer: gDialer} rd := &Dialer{gDialer: gDialer}
for _, r := range rules { for _, r := range rules {
sDialer := strategy.NewDialer(r.Forward, &r.StrategyConfig) sd := strategy.NewDialer(r.Forward, &r.StrategyConfig)
rd.dialers = append(rd.dialers, sDialer) rd.dialers = append(rd.dialers, sd)
for _, domain := range r.Domain { for _, domain := range r.Domain {
rd.domainMap.Store(strings.ToLower(domain), sDialer) rd.domainMap.Store(strings.ToLower(domain), sd)
} }
for _, ip := range r.IP { for _, ip := range r.IP {
rd.ipMap.Store(ip, sDialer) rd.ipMap.Store(ip, sd)
} }
for _, s := range r.CIDR { for _, s := range r.CIDR {
if _, cidr, err := net.ParseCIDR(s); err == nil { if _, cidr, err := net.ParseCIDR(s); err == nil {
rd.cidrMap.Store(cidr, sDialer) rd.cidrMap.Store(cidr, sd)
} }
} }
} }

View File

@ -18,6 +18,7 @@ type Config struct {
CheckWebSite string CheckWebSite string
CheckInterval int CheckInterval int
MaxFailures int MaxFailures int
IntFace string
} }
// Checker is an interface of forwarder checker // Checker is an interface of forwarder checker
@ -29,7 +30,7 @@ type Checker interface {
func NewDialer(s []string, c *Config) proxy.Dialer { func NewDialer(s []string, c *Config) proxy.Dialer {
var fwdrs []*proxy.Forwarder var fwdrs []*proxy.Forwarder
for _, chain := range s { for _, chain := range s {
fwdr, err := proxy.ForwarderFromURL(chain) fwdr, err := proxy.ForwarderFromURL(chain, c.IntFace)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -38,7 +39,11 @@ func NewDialer(s []string, c *Config) proxy.Dialer {
} }
if len(fwdrs) == 0 { if len(fwdrs) == 0 {
return proxy.Default d, err := proxy.NewDirect(c.IntFace)
if err != nil {
log.Fatal(err)
}
return d
} }
if len(fwdrs) == 1 { if len(fwdrs) == 1 {
@ -139,6 +144,7 @@ func (rr *rrDialer) nextDialer(dstAddr string) *proxy.Forwarder {
} }
if !found { if !found {
rr.priority = 0
log.F("NO AVAILABLE PROXY FOUND! please check your network or proxy server settings.") log.F("NO AVAILABLE PROXY FOUND! please check your network or proxy server settings.")
} }