Joyit пре 1 месец
родитељ
комит
f08e267b1a
5 измењених фајлова са 48 додато и 23 уклоњено
  1. 0 6
      conn/tcp2/tcp2.go
  2. 42 9
      conn/util.go
  3. 1 7
      conn/ws2/ws2.go
  4. 2 1
      hub.go
  5. 3 0
      line.go

+ 0 - 6
conn/tcp2/tcp2.go

@@ -311,12 +311,6 @@ func (c *Tcp2) WriteAuthInfo(channel string, auth []byte) (err error) {
 
 // 获取Auth信息
 func (c *Tcp2) ReadAuthInfo() (proto string, version uint8, channel string, auth []byte, err error) {
-	defer func() {
-		if r := recover(); r != nil {
-			err = fmt.Errorf("recovered from panic: %v", r)
-			return
-		}
-	}()
 	msg, recycle, err := c.ReadRawPackage(c.cf.ReadWait)
 	if err != nil {
 		return

+ 42 - 9
conn/util.go

@@ -42,6 +42,7 @@ func AuthPackageEncode(proto string, version uint8, channel string, auth []byte,
 	buf[index] = version
 	index++
 
+	// 指示是否支持压缩
 	if compress {
 		buf[index] = 0x01
 	} else {
@@ -61,9 +62,17 @@ func AuthPackageEncode(proto string, version uint8, channel string, auth []byte,
 // 验证包解码
 // id(65502)+proto(string)+version(uint8)+option(byte)+channel(string)+auth([]byte)
 func AuthPackageDecode(msg []byte, recycle bool) (proto string, version uint8, compress bool, channel string, auth []byte, err error) {
-	if recycle {
-		defer pool.Put(msg)
-	}
+	// 避免错误的数据包,出现数组越界的错误
+	defer func() {
+		if recycle {
+			pool.Put(msg)
+		}
+		if r := recover(); r != nil {
+			err = fmt.Errorf("recovered from panic: %v", r)
+			return
+		}
+	}()
+
 	msgLen := len(msg)
 	if msgLen < 9 {
 		err = errors.New("wrong message length")
@@ -211,19 +220,43 @@ func PackageDecode(msg []byte, recycle bool, compress bool, cf *config.Config) (
 	if recycle {
 		defer pool.Put(msg)
 	}
-	msgLen := len(msg)
 	id = binary.BigEndian.Uint16(msg[0:2])
-	// ping信息
-	if msgLen == 2 {
-		msgType = PingMsg
-		return
-	}
 
 	if id > config.ID_MAX {
+		switch id {
+		case config.ID_PROXY_CONNECT: // 代理连接请求
+			msgType = ProxyConnectMsg
+			// 请求代理的地址
+			if recycle {
+				data = slices.Clone(msg[2:])
+			} else {
+				data = msg[2:]
+			}
+			return
+		case config.ID_PROXY_RESULT: // 代理请求执行结果
+			msgType = ProxyResultMsg
+			// 错误信息
+			if recycle {
+				data = slices.Clone(msg[2:])
+			} else {
+				data = msg[2:]
+			}
+			// 如果没有错误信息则表示成功
+			if len(data) <= 0 {
+				state = 1
+			}
+			return
+		}
 		err = fmt.Errorf("wrong message id: %d", id)
 		return
 	}
 
+	// ping信息
+	if len(msg) == 2 {
+		msgType = PingMsg
+		return
+	}
+
 	cmdx := msg[2]
 	if (cmdx & 0x80) == 0 {
 		// 请求包

+ 1 - 7
conn/ws2/ws2.go

@@ -245,12 +245,6 @@ func (c *Ws2) WriteAuthInfo(channel string, auth []byte) (err error) {
 // 获取Auth信息
 // id(65502)+proto(string)+version(uint8)+option(byte)+channel(string)+auth([]byte)
 func (c *Ws2) ReadAuthInfo() (proto string, version uint8, channel string, auth []byte, err error) {
-	defer func() {
-		if r := recover(); r != nil {
-			err = fmt.Errorf("recovered from panic: %v", r)
-			return
-		}
-	}()
 	msg, recycle, err := c.ReadRawPackage(c.cf.ReadWait)
 	if err != nil {
 		return
@@ -260,7 +254,7 @@ func (c *Ws2) ReadAuthInfo() (proto string, version uint8, channel string, auth
 	if err != nil {
 		return
 	}
-	if proto != PROTO {
+	if proto != PROTO && proto != PROTO_STL {
 		err = fmt.Errorf("wrong proto: %s", proto)
 		return
 	}

+ 2 - 1
hub.go

@@ -698,8 +698,9 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo, autoRe
 	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(h.cf.ConnectTimeout))
 	defer cancel()
 	taskCh := make(chan bool)
-	done := false
+	done := false // 指示是否已经完成
 
+	// 发送连接请求
 	go func() {
 		if host.Version == ws2.VERSION && (host.Proto == ws2.PROTO || host.Proto == ws2.PROTO_STL) {
 			runProto = ws2.PROTO

+ 3 - 0
line.go

@@ -25,6 +25,9 @@ type Line struct {
 	pingID         uint16       // 只有客户端使用
 	pingWrongCount uint8        // 记录 ping id 反馈错误次数,超过3次则重新连接
 
+	proxyConn conn.Connect // 代理转发连接
+	proxyHost *HostInfo    // 代理地址
+
 	Extra sync.Map // 附加临时信息,由应用端决定具体内容,断线会自动清理
 	Keep  sync.Map // 附加固定信息,由应用端决定具体内容,断线不会自动清理,不过也不能保证一直保持有值,断线后可能会被系统清理