|
|
@@ -608,7 +608,7 @@ func (h *Hub) ConnectDuration(line *Line) time.Duration {
|
|
|
|
|
|
// 绑定端口,建立服务
|
|
|
// 需要程序运行时调用
|
|
|
-func (h *Hub) BindForServer(info *HostInfo) (err error) {
|
|
|
+func (h *Hub) BindForServer(info *conn.HostInfo) (err error) {
|
|
|
doConnectFunc := func(conn conn.Connect) {
|
|
|
proto, version, channel, auth, err := conn.ReadAuthInfo()
|
|
|
if err != nil {
|
|
|
@@ -663,7 +663,7 @@ func (h *Hub) BindForServer(info *HostInfo) (err error) {
|
|
|
|
|
|
// 新建一个连接,不同的连接协议由底层自己选择
|
|
|
// channel: 要连接的频道信息,需要能表达频道关键信息的部分
|
|
|
-func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo, autoReconnect bool) (err error) {
|
|
|
+func (h *Hub) ConnectToServer(channel string, force bool, host *conn.HostInfo, autoReconnect bool) (err error) {
|
|
|
h.connectMutex.Lock()
|
|
|
defer h.connectMutex.Unlock()
|
|
|
// 检查当前channel是否已经存在
|
|
|
@@ -691,8 +691,8 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo, autoRe
|
|
|
}
|
|
|
|
|
|
var conn conn.Connect
|
|
|
- var runProto string
|
|
|
- addr := net.JoinHostPort(host.Host, strconv.Itoa(int(host.Port)))
|
|
|
+ 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))
|
|
|
@@ -702,21 +702,7 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo, autoRe
|
|
|
|
|
|
// 发送连接请求
|
|
|
go func() {
|
|
|
- if host.Version == ws2.VERSION && (host.Proto == ws2.PROTO || host.Proto == ws2.PROTO_STL) {
|
|
|
- runProto = ws2.PROTO
|
|
|
- if h.cf.PrintMsg {
|
|
|
- log.Println("[ConnectToServer] connecting:", host.Proto, addr, host.Path, host.Hash)
|
|
|
- }
|
|
|
- conn, err = ws2.Dial(h.cf, host.Proto, addr, host.Path, host.Hash)
|
|
|
- } else if host.Version == tcp2.VERSION && host.Proto == tcp2.PROTO {
|
|
|
- runProto = tcp2.PROTO
|
|
|
- if h.cf.PrintMsg {
|
|
|
- log.Println("[ConnectToServer] connecting:", host.Proto, addr, host.Hash)
|
|
|
- }
|
|
|
- conn, err = tcp2.Dial(h.cf, addr, host.Hash)
|
|
|
- } else {
|
|
|
- err = fmt.Errorf("not correct protocol and version found in: %+v", host)
|
|
|
- }
|
|
|
+ rawProto, conn, err = Dail(host, h.cf)
|
|
|
if done {
|
|
|
if err != nil {
|
|
|
log.Println("[ConnectToServer] dial failed:", err)
|
|
|
@@ -754,7 +740,7 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo, autoRe
|
|
|
localChannel = localChannel + "?proxy=" + host.Host
|
|
|
}
|
|
|
// 发送验证信息
|
|
|
- if err := conn.WriteAuthInfo(localChannel, h.authFunc(true, runProto, host.Version, channel, nil)); err != nil {
|
|
|
+ if err := conn.WriteAuthInfo(localChannel, h.authFunc(true, rawProto, host.Version, channel, nil)); err != nil {
|
|
|
log.Println("[ConnectToServer] WriteAuthInfo failed:", err)
|
|
|
conn.Close()
|
|
|
host.Errors++
|
|
|
@@ -771,7 +757,7 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo, autoRe
|
|
|
return err
|
|
|
}
|
|
|
// 检查版本和协议是否一致
|
|
|
- if version != host.Version || proto != runProto {
|
|
|
+ if version != host.Version || proto != rawProto {
|
|
|
err = fmt.Errorf("[ConnectToServer] version or protocol wrong: %d, %s", version, proto)
|
|
|
log.Println(err)
|
|
|
conn.Close()
|
|
|
@@ -833,7 +819,7 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo, autoRe
|
|
|
|
|
|
// 重试方式连接服务
|
|
|
// 将会一直阻塞直到连接成功
|
|
|
-func (h *Hub) ConnectToServerX(channel string, force bool, host *HostInfo) {
|
|
|
+func (h *Hub) ConnectToServerX(channel string, force bool, host *conn.HostInfo) {
|
|
|
for {
|
|
|
if host == nil {
|
|
|
if h.connectHostFunc == nil {
|