修订代码;将addr的赋值从各个proxy自己的方法中提出来

This commit is contained in:
hahahrfool
2022-03-23 18:54:38 +08:00
parent b2c6abe330
commit 8c6ed6377d
9 changed files with 46 additions and 25 deletions

View File

@@ -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:
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)
}
}

View File

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

View File

@@ -65,7 +65,6 @@ func (_ ServerCreator) NewServer(lc *proxy.ListenConf) (proxy.Server, error) {
return nil, e
}
s := &Server{
ProxyCommonStruct: proxy.ProxyCommonStruct{Addr: lc.GetAddr()}, //监听地址不要与TargetAddr混淆
targetAddr: ta,
}
return s, nil

View File

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

View File

@@ -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)
}

View File

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

View File

@@ -53,7 +53,6 @@ func (_ ClientCreator) NewClient(dc *proxy.DialConf) (proxy.Client, error) {
}
c := &Client{
ProxyCommonStruct: proxy.ProxyCommonStruct{Addr: dc.GetAddr()},
user: id,
}
v := dc.Version

View File

@@ -42,7 +42,6 @@ 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),
}

View File

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