mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
general: add global setting interface
This commit is contained in:
1 parent
edd5964a1e
commit
8daa7784b0
8 files changed
+45
-26
No files matched your search
+5
-4
@@ -37,17 +37,18 @@ func RegisterDialer(name string, c DialerCreator) {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if dialer == nil {
|
||||
return nil, errors.New("DialerFromURL: dialer cannot be nil")
|
||||
}
|
||||
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
log.F("parse err: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if dialer == nil {
|
||||
dialer = Default
|
||||
}
|
||||
|
||||
c, ok := dialerMap[strings.ToLower(u.Scheme)]
|
||||
if ok {
|
||||
return c(s, dialer)
|
||||
|
||||
+7
-4
@@ -1,6 +1,7 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/nadoo/glider/common/log"
|
||||
@@ -28,7 +29,7 @@ func NewDirect(intface string) (*Direct, error) {
|
||||
|
||||
iface, err := net.InterfaceByName(intface)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.New(err.Error() + ": " + intface)
|
||||
}
|
||||
|
||||
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
|
||||
func (d *Direct) Dial(network, addr string) (c net.Conn, err error) {
|
||||
c, err = dial(network, addr, d.ip)
|
||||
if err == nil {
|
||||
return
|
||||
if d.iface == nil || d.ip != nil {
|
||||
c, err = dial(network, addr, d.ip)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, ip := range d.IFaceIPs() {
|
||||
|
||||
+13
-7
@@ -14,16 +14,17 @@ import (
|
||||
type Forwarder struct {
|
||||
Dialer
|
||||
Priority int
|
||||
addr string
|
||||
disabled uint32
|
||||
failures uint32
|
||||
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
|
||||
func ForwarderFromURL(s string) (f *Forwarder, err error) {
|
||||
func ForwarderFromURL(s, intface string) (f *Forwarder, err error) {
|
||||
f = &Forwarder{}
|
||||
|
||||
ss := strings.Split(s, "#")
|
||||
@@ -31,8 +32,13 @@ func ForwarderFromURL(s string) (f *Forwarder, err error) {
|
||||
err = f.parseOption(ss[1])
|
||||
}
|
||||
|
||||
iface := intface
|
||||
if f.intface != "" && f.intface != intface {
|
||||
iface = f.intface
|
||||
}
|
||||
|
||||
var d Dialer
|
||||
d, err = NewDirect(f.intface)
|
||||
d, err = NewDirect(iface)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+5
-4
@@ -27,7 +27,12 @@ func RegisterServer(name string, c ServerCreator) {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if dialer == nil {
|
||||
return nil, errors.New("ServerFromURL: dialer cannot be nil")
|
||||
}
|
||||
|
||||
if !strings.Contains(s, "://") {
|
||||
s = "mixed://" + s
|
||||
}
|
||||
@@ -38,10 +43,6 @@ func ServerFromURL(s string, dialer Dialer) (Server, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if dialer == nil {
|
||||
dialer = Default
|
||||
}
|
||||
|
||||
c, ok := serverMap[strings.ToLower(u.Scheme)]
|
||||
if ok {
|
||||
return c(s, dialer)
|
||||
|
||||
Reference in new issue
Block a user