Jelajahi Sumber

add force param in get host function

Joyit 1 Minggu lalu
induk
melakukan
2c3087306d
2 mengubah file dengan 9 tambahan dan 7 penghapusan
  1. 8 6
      hub.go
  2. 1 1
      type.go

+ 8 - 6
hub.go

@@ -669,7 +669,7 @@ func (h *Hub) BindForServer(host *conn.HostInfo) (err error) {
 // 代理连接,在已经连接的基础上进行代理连接
 func (h *Hub) ClientProxyConnect(line *Line, remoteChannel string) (err error) {
 	// 获取主机地址
-	host, err := h.connectHostFunc(remoteChannel, HostTypeDirect)
+	host, err := h.connectHostFunc(remoteChannel, HostTypeDirect, true)
 	if err != nil {
 		return err
 	}
@@ -726,7 +726,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
 			return errors.New("not connect host func found")
 		}
 		// 获取服务地址等信息
-		host, err = h.connectHostFunc(remoteChannel, HostTypeBoth)
+		host, err = h.connectHostFunc(remoteChannel, HostTypeBoth, true)
 		if err != nil {
 			return err
 		}
@@ -767,6 +767,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
 				}
 			}
 			line = ln
+			line.started = time.Now()
 			line.proxyChannel = proxyChannel
 			line.Start(remoteChannel, connect, host)
 			return false
@@ -803,15 +804,16 @@ func (h *Hub) ConnectToServerX(channel string, force bool, host *conn.HostInfo)
 			} else {
 				hostType = HostTypeProxy
 			}
-			// 循环不同类型,避免一直使用代理
-			count = (count + 1) % 8
-			hx, err := h.connectHostFunc(channel, hostType)
+
+			hx, err := h.connectHostFunc(channel, hostType, count != 0)
 			if err == nil {
 				err := h.ConnectToServer(channel, force, hx, true)
 				if err == nil {
 					return
 				}
 			}
+			// 循环不同类型,避免一直使用代理
+			count = (count + 1) % 8
 			log.Println("[ConnectToServerX] connectHostFunc error with channel:", channel, err)
 		} else {
 			err := h.ConnectToServer(channel, force, host, true)
@@ -844,7 +846,7 @@ func (h *Hub) checkConnect() {
 			now := time.Now().UnixMilli()
 			h.lines.Range(func(id int, line *Line) bool {
 				if line.host != nil && line.state == StateProxied && now-line.started.UnixMilli() > int64(h.cf.ProxyTimeout) {
-					host, err := h.connectHostFunc(line.channel, HostTypeDirect)
+					host, err := h.connectHostFunc(line.channel, HostTypeDirect, true)
 					if err != nil {
 						log.Println("[proxyTicker connectHostFunc]", err)
 						return false

+ 1 - 1
type.go

@@ -132,7 +132,7 @@ type GetMsg struct {
 }
 
 // 获取对应频道的一个连接地址
-type ConnectHostFunc func(channel string, hostType HostType) (hostInfo *conn.HostInfo, err error)
+type ConnectHostFunc func(channel string, hostType HostType, force bool) (hostInfo *conn.HostInfo, err error)
 
 // 获取认证信息
 type AuthFunc func(host *conn.HostInfo, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte)