diff --git a/proxy/config_standard.go b/proxy/config_standard.go index 6a2ece8..dd48490 100644 --- a/proxy/config_standard.go +++ b/proxy/config_standard.go @@ -37,13 +37,38 @@ type CommonConf struct { Extra map[string]interface{} `toml:"extra"` //用于包含任意其它数据.虽然本包自己定义的协议肯定都是已知的,但是如果其他人使用了本包的话,那就有可能添加一些 新协议 特定的数据. } -func (cc *CommonConf) GetAddr() string { +func (cc *CommonConf) GetAddrStr() string { switch cc.Network { case "unix": return cc.Host default: - return cc.Host + ":" + strconv.Itoa(cc.Port) + if cc.Host != "" { + + return cc.Host + ":" + strconv.Itoa(cc.Port) + } else { + return cc.IP + ":" + strconv.Itoa(cc.Port) + + } + + } + +} + +//和 GetAddr的区别是,它优先使用ip,其次再使用host +func (cc *CommonConf) GetAddrStrForListenOrDial() string { + switch cc.Network { + case "unix": + return cc.Host + + default: + if cc.IP != "" { + return cc.IP + ":" + strconv.Itoa(cc.Port) + + } else { + return cc.Host + ":" + strconv.Itoa(cc.Port) + + } } diff --git a/proxy/creator.go b/proxy/creator.go index ddf48fb..6d362d9 100644 --- a/proxy/creator.go +++ b/proxy/creator.go @@ -231,13 +231,17 @@ func configCommon(ser ProxyCommon, cc *CommonConf) { } -//setNetwork, setIsDial(true),setDialConf(dc), call configCommon +//SetAddrStr, setNetwork, setIsDial(true),setDialConf(dc), call configCommon func configCommonForClient(cli ProxyCommon, dc *DialConf) { cli.setNetwork(dc.Network) cli.setIsDial(true) cli.setDialConf(dc) cli.setTag(dc.Tag) + if cli.Name() != "direct" { + cli.SetAddrStr(dc.GetAddrStrForListenOrDial()) + } + configCommon(cli, &dc.CommonConf) if dc.AdvancedLayer == "ws" { @@ -245,8 +249,9 @@ func configCommonForClient(cli ProxyCommon, dc *DialConf) { } } -//setNetwork, setTag, setCantRoute,setListenConf(lc), call configCommon +//SetAddrStr,setNetwork, setTag, setCantRoute,setListenConf(lc), call configCommon func configCommonForServer(ser ProxyCommon, lc *ListenConf) { + ser.SetAddrStr(lc.GetAddrStrForListenOrDial()) ser.setNetwork(lc.Network) ser.setListenConf(lc) ser.setTag(lc.Tag) diff --git a/proxy/dokodemo/server.go b/proxy/dokodemo/server.go index d87e51b..f4bbc3e 100644 --- a/proxy/dokodemo/server.go +++ b/proxy/dokodemo/server.go @@ -65,8 +65,7 @@ func (_ ServerCreator) NewServer(lc *proxy.ListenConf) (proxy.Server, error) { return nil, e } s := &Server{ - ProxyCommonStruct: proxy.ProxyCommonStruct{Addr: lc.GetAddr()}, //监听地址,不要与TargetAddr混淆 - targetAddr: ta, + targetAddr: ta, } return s, nil } diff --git a/proxy/http/server.go b/proxy/http/server.go index 65c9dd3..69dc8ef 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -19,9 +19,9 @@ func init() { proxy.RegisterServer(name, &ServerCreator{}) } -//只有地址和port需要配置,非常简单 type ServerCreator struct{} +//只有地址和port需要配置,非常简单 func (_ ServerCreator) NewServerFromURL(u *url.URL) (proxy.Server, error) { addr := u.Host //若不给出port,那就只有host名,这样不好,我们“默认”, 配置里肯定给了port @@ -36,9 +36,7 @@ func (_ ServerCreator) NewServerFromURL(u *url.URL) (proxy.Server, error) { func (_ ServerCreator) NewServer(dc *proxy.ListenConf) (proxy.Server, error) { - s := &Server{ - ProxyCommonStruct: proxy.ProxyCommonStruct{Addr: dc.GetAddr()}, - } + s := &Server{} return s, nil } diff --git a/proxy/proxy.go b/proxy/proxy.go index a00e604..b37a854 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -121,7 +121,7 @@ func prepareTLS_forClient(com ProxyCommon, dc *DialConf) error { func prepareTLS_forServer(com ProxyCommon, lc *ListenConf) error { // 这里直接不检查 字符串就直接传给 tlsLayer.NewServer // 所以要求 cert和 key 不在程序本身目录 的话,就要给出完整路径 - tlsserver, err := tlsLayer.NewServer(lc.GetAddr(), lc.Host, lc.TLSCert, lc.TLSKey, lc.Insecure) + tlsserver, err := tlsLayer.NewServer(lc.Host, lc.TLSCert, lc.TLSKey, lc.Insecure) if err == nil { com.setTLS_Server(tlsserver) } else { @@ -150,7 +150,7 @@ func prepareTLS_forProxyCommon_withURL(u *url.URL, isclient bool, com ProxyCommo hostAndPort := u.Host sni, _, _ := net.SplitHostPort(hostAndPort) - tlsserver, err := tlsLayer.NewServer(hostAndPort, sni, certFile, keyFile, insecure) + tlsserver, err := tlsLayer.NewServer(sni, certFile, keyFile, insecure) if err == nil { com.setTLS_Server(tlsserver) } else { @@ -317,7 +317,7 @@ func (s *ProxyCommonStruct) initWS_client() { } } - c, e := ws.NewClient(s.dialConf.GetAddr(), path) + c, e := ws.NewClient(s.dialConf.GetAddrStr(), path) if e != nil { log.Fatal("initWS_client failed", e) } diff --git a/proxy/socks5/server.go b/proxy/socks5/server.go index 46d7b6a..1fd8d90 100644 --- a/proxy/socks5/server.go +++ b/proxy/socks5/server.go @@ -27,7 +27,6 @@ type Server struct { //password string } -//只有地址和port需要配置,非常简单 type ServerCreator struct{} func (_ ServerCreator) NewServerFromURL(u *url.URL) (proxy.Server, error) { @@ -36,12 +35,11 @@ func (_ ServerCreator) NewServerFromURL(u *url.URL) (proxy.Server, error) { func (_ ServerCreator) NewServer(dc *proxy.ListenConf) (proxy.Server, error) { - s := &Server{ - ProxyCommonStruct: proxy.ProxyCommonStruct{Addr: dc.GetAddr()}, - } + s := &Server{} return s, nil } +//只有地址和port需要配置,非常简单 func NewServer(url *url.URL) (proxy.Server, error) { addr := url.Host //若不给出port,那就只有host名,这样不好,我们默认配置里肯定给了port diff --git a/proxy/vless/client.go b/proxy/vless/client.go index 37ce3d5..7ec0d4c 100644 --- a/proxy/vless/client.go +++ b/proxy/vless/client.go @@ -53,8 +53,7 @@ func (_ ClientCreator) NewClient(dc *proxy.DialConf) (proxy.Client, error) { } c := &Client{ - ProxyCommonStruct: proxy.ProxyCommonStruct{Addr: dc.GetAddr()}, - user: id, + user: id, } v := dc.Version if v >= 0 { diff --git a/proxy/vless/server.go b/proxy/vless/server.go index 98990e5..13078f1 100644 --- a/proxy/vless/server.go +++ b/proxy/vless/server.go @@ -42,9 +42,8 @@ func (_ ServerCreator) NewServer(lc *proxy.ListenConf) (proxy.Server, error) { return nil, err } s := &Server{ - ProxyCommonStruct: proxy.ProxyCommonStruct{Addr: lc.GetAddr()}, - userHashes: make(map[[16]byte]*proxy.V2rayUser), - userCRUMFURS: make(map[[16]byte]*CRUMFURS), + userHashes: make(map[[16]byte]*proxy.V2rayUser), + userCRUMFURS: make(map[[16]byte]*CRUMFURS), } fallbackStr := lc.Fallback diff --git a/tlsLayer/server.go b/tlsLayer/server.go index d1f21f5..f8dafee 100644 --- a/tlsLayer/server.go +++ b/tlsLayer/server.go @@ -8,18 +8,16 @@ import ( ) type Server struct { - addr string tlsConfig *tls.Config } -func NewServer(hostAndPort, host, certFile, keyFile string, isInsecure bool) (*Server, error) { +func NewServer(host, certFile, keyFile string, isInsecure bool) (*Server, error) { cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { return nil, err } s := &Server{ - addr: hostAndPort, tlsConfig: &tls.Config{ InsecureSkipVerify: isInsecure, ServerName: host,