|
@@ -621,14 +621,14 @@ func (h *Hub) BindForServer(info *conn.HostInfo) (err error) {
|
|
|
conn.Close()
|
|
conn.Close()
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- if !h.checkAuthFunc(false, proto, version, channel, auth) {
|
|
|
|
|
|
|
+ if !h.checkAuthFunc(nil, proto, version, channel, auth) {
|
|
|
err = fmt.Errorf("[server checkAuthFunc ERROR] in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
|
|
err = fmt.Errorf("[server checkAuthFunc ERROR] in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
|
|
|
log.Println(err)
|
|
log.Println(err)
|
|
|
conn.Close()
|
|
conn.Close()
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
// 发送频道信息
|
|
// 发送频道信息
|
|
|
- if err := conn.WriteAuthInfo(h.channel, h.authFunc(false, proto, version, channel, auth)); err != nil {
|
|
|
|
|
|
|
+ if err := conn.WriteAuthInfo(h.channel, h.authFunc(nil, proto, version, channel, auth)); err != nil {
|
|
|
log.Println("[WriteAuthInfo ERROR]", err)
|
|
log.Println("[WriteAuthInfo ERROR]", err)
|
|
|
conn.Close()
|
|
conn.Close()
|
|
|
return
|
|
return
|
|
@@ -661,6 +661,47 @@ func (h *Hub) BindForServer(info *conn.HostInfo) (err error) {
|
|
|
return errors.New("not connect protocol and version found")
|
|
return errors.New("not connect protocol and version found")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 代理连接,在已经连接的基础上进行代理连接
|
|
|
|
|
+func (h *Hub) ProxyConnect(connect conn.Connect, remoteChannel string) (err error) {
|
|
|
|
|
+ host, err := h.connectHostFunc(remoteChannel, HostTypeDirect)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ // 发送代理连接
|
|
|
|
|
+ buf := conn.ProxyConnectPackageEncode(host.Url())
|
|
|
|
|
+ if err := connect.WriteRawPackage(buf, true); err != nil {
|
|
|
|
|
+ log.Println("[ConnectToServer] WriteRawPackage error:", err)
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ // 读取代理结果
|
|
|
|
|
+ msgType, _, _, _, data, err := connect.ReadMessage(h.cf.LongReadWait)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ log.Println("[ConnectToServer] ReadRawPackage error:", err)
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ if msgType != conn.MsgProxyResult {
|
|
|
|
|
+ return errors.New("not found MsgProxyResult")
|
|
|
|
|
+ }
|
|
|
|
|
+ if len(data) > 0 {
|
|
|
|
|
+ log.Println("[ConnectToServer] proxy result error:", string(data))
|
|
|
|
|
+ return errors.New(string(data))
|
|
|
|
|
+ }
|
|
|
|
|
+ // 进行auth验证
|
|
|
|
|
+ needProxy, err := ClientCheckAuthInfo(connect, host, h.channel, remoteChannel, h.authFunc, h.checkAuthFunc)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ host.Errors++
|
|
|
|
|
+ host.Updated = time.Now()
|
|
|
|
|
+ return err
|
|
|
|
|
+ } else {
|
|
|
|
|
+ host.Errors = 0
|
|
|
|
|
+ host.Updated = time.Now()
|
|
|
|
|
+ }
|
|
|
|
|
+ if needProxy {
|
|
|
|
|
+ return errors.New("proxy with proxy")
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// 新建一个连接,不同的连接协议由底层自己选择
|
|
// 新建一个连接,不同的连接协议由底层自己选择
|
|
|
// channel: 要连接的频道信息,需要能表达频道关键信息的部分
|
|
// channel: 要连接的频道信息,需要能表达频道关键信息的部分
|
|
|
func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostInfo, autoReconnect bool) (err error) {
|
|
func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostInfo, autoReconnect bool) (err error) {
|
|
@@ -680,7 +721,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
return errors.New("not connect host func found")
|
|
return errors.New("not connect host func found")
|
|
|
}
|
|
}
|
|
|
// 获取服务地址等信息
|
|
// 获取服务地址等信息
|
|
|
- host, err = h.connectHostFunc(remoteChannel, Both)
|
|
|
|
|
|
|
+ host, err = h.connectHostFunc(remoteChannel, HostTypeBoth)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -690,64 +731,15 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- conn, err := DailWithTimeout(h.ctx, host, h.cf)
|
|
|
|
|
|
|
+ connect, err := DailWithTimeout(h.ctx, host, h.cf)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
log.Println("[DailWithTimeout] error:", err)
|
|
log.Println("[DailWithTimeout] error:", err)
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // var conn conn.Connect
|
|
|
|
|
- // // var rawProto string
|
|
|
|
|
- // // addr := net.JoinHostPort(host.Host, strconv.Itoa(int(host.Port)))
|
|
|
|
|
-
|
|
|
|
|
- // // 添加定时器
|
|
|
|
|
- // ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(h.cf.ConnectTimeout))
|
|
|
|
|
- // defer cancel()
|
|
|
|
|
- // taskCh := make(chan bool)
|
|
|
|
|
- // done := false // 指示是否已经完成
|
|
|
|
|
-
|
|
|
|
|
- // // 发送连接请求
|
|
|
|
|
- // go func() {
|
|
|
|
|
- // conn, err = Dail(host, h.cf)
|
|
|
|
|
- // if done {
|
|
|
|
|
- // if err != nil {
|
|
|
|
|
- // log.Println("[ConnectToServer] dial failed:", err)
|
|
|
|
|
- // }
|
|
|
|
|
- // if conn != nil {
|
|
|
|
|
- // conn.Close()
|
|
|
|
|
- // }
|
|
|
|
|
- // } else {
|
|
|
|
|
- // taskCh <- err == nil
|
|
|
|
|
- // }
|
|
|
|
|
- // }()
|
|
|
|
|
-
|
|
|
|
|
- // select {
|
|
|
|
|
- // case ok := <-taskCh:
|
|
|
|
|
- // cancel()
|
|
|
|
|
- // if !ok || err != nil || conn == nil {
|
|
|
|
|
- // log.Println("[ConnectToServer] connect failed:", err)
|
|
|
|
|
- // host.Errors++
|
|
|
|
|
- // host.Updated = time.Now()
|
|
|
|
|
- // if err == nil {
|
|
|
|
|
- // err = errors.New("unknown error")
|
|
|
|
|
- // }
|
|
|
|
|
- // return err
|
|
|
|
|
- // }
|
|
|
|
|
- // case <-ctx.Done():
|
|
|
|
|
- // done = true
|
|
|
|
|
- // return errors.New("timeout")
|
|
|
|
|
- // case <-h.ctx.Done():
|
|
|
|
|
- // return errors.New("quit")
|
|
|
|
|
- // }
|
|
|
|
|
-
|
|
|
|
|
- // 如果 host 是代理,将代理信息添加到channel中
|
|
|
|
|
- // localChannel := h.channel
|
|
|
|
|
- // if host.Proxy {
|
|
|
|
|
- // localChannel = localChannel + "?proxy=" + host.Host
|
|
|
|
|
- // }
|
|
|
|
|
- err = ClientCheckAuthInfo(conn, host, h.channel, remoteChannel, h.authFunc, h.checkAuthFunc)
|
|
|
|
|
|
|
+ needProxy, err := ClientCheckAuthInfo(connect, host, h.channel, remoteChannel, h.authFunc, h.checkAuthFunc)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- conn.Close()
|
|
|
|
|
|
|
+ connect.Close()
|
|
|
host.Errors++
|
|
host.Errors++
|
|
|
host.Updated = time.Now()
|
|
host.Updated = time.Now()
|
|
|
return err
|
|
return err
|
|
@@ -756,6 +748,16 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
host.Updated = time.Now()
|
|
host.Updated = time.Now()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 判断是否是代理节点,如果是代理节点还需要进行下一步的连接
|
|
|
|
|
+ if needProxy {
|
|
|
|
|
+ err := h.ProxyConnect(connect, remoteChannel)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ connect.Close()
|
|
|
|
|
+ log.Println("[ProxyConnect] error:", err)
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 将连接加入现有连接中
|
|
// 将连接加入现有连接中
|
|
|
done := false
|
|
done := false
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
@@ -769,7 +771,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- line.Start(remoteChannel, conn, host)
|
|
|
|
|
|
|
+ line.Start(remoteChannel, connect, host)
|
|
|
done = true
|
|
done = true
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
@@ -780,7 +782,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
}
|
|
}
|
|
|
// 新建一个连接
|
|
// 新建一个连接
|
|
|
if !done {
|
|
if !done {
|
|
|
- line := NewConnect(h.cf, h, remoteChannel, conn, host, autoReconnect)
|
|
|
|
|
|
|
+ line := NewConnect(h.cf, h, remoteChannel, connect, host, autoReconnect)
|
|
|
h.addLine(line)
|
|
h.addLine(line)
|
|
|
}
|
|
}
|
|
|
return nil
|
|
return nil
|
|
@@ -789,13 +791,24 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
// 重试方式连接服务
|
|
// 重试方式连接服务
|
|
|
// 将会一直阻塞直到连接成功
|
|
// 将会一直阻塞直到连接成功
|
|
|
func (h *Hub) ConnectToServerX(channel string, force bool, host *conn.HostInfo) {
|
|
func (h *Hub) ConnectToServerX(channel string, force bool, host *conn.HostInfo) {
|
|
|
|
|
+ count := 0
|
|
|
for {
|
|
for {
|
|
|
if host == nil {
|
|
if host == nil {
|
|
|
if h.connectHostFunc == nil {
|
|
if h.connectHostFunc == nil {
|
|
|
log.Println("[ConnectToServerX] not connect host func found")
|
|
log.Println("[ConnectToServerX] not connect host func found")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- hx, err := h.connectHostFunc(channel, Both)
|
|
|
|
|
|
|
+ // 通过尝试次数来获取不同类型的服务节点
|
|
|
|
|
+ var hostType HostType
|
|
|
|
|
+ if count < 2 {
|
|
|
|
|
+ hostType = HostTypeDirect
|
|
|
|
|
+ } else if count < 5 {
|
|
|
|
|
+ hostType = HostTypeBoth
|
|
|
|
|
+ } else {
|
|
|
|
|
+ hostType = HostTypeProxy
|
|
|
|
|
+ }
|
|
|
|
|
+ count++
|
|
|
|
|
+ hx, err := h.connectHostFunc(channel, hostType)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
err := h.ConnectToServer(channel, force, hx, true)
|
|
err := h.ConnectToServer(channel, force, hx, true)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
@@ -834,7 +847,7 @@ func (h *Hub) checkConnect() {
|
|
|
now := time.Now().UnixMilli()
|
|
now := time.Now().UnixMilli()
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
if line.host != nil && line.host.Proxy && now-line.updated.UnixMilli() > int64(h.cf.ProxyTimeout) {
|
|
if line.host != nil && line.host.Proxy && now-line.updated.UnixMilli() > int64(h.cf.ProxyTimeout) {
|
|
|
- host, err := h.connectHostFunc(line.channel, Direct)
|
|
|
|
|
|
|
+ host, err := h.connectHostFunc(line.channel, HostTypeDirect)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
log.Println("[proxyTicker connectHostFunc]", err)
|
|
log.Println("[proxyTicker connectHostFunc]", err)
|
|
|
return false
|
|
return false
|