Joyit 1 hafta önce
ebeveyn
işleme
696c927a21
2 değiştirilmiş dosya ile 21 ekleme ve 4 silme
  1. 5 3
      conn/ws2/ws2.go
  2. 16 1
      util/util.go

+ 5 - 3
conn/ws2/ws2.go

@@ -124,7 +124,7 @@ func Server(cf *config.Config, bind string, path string, hash string, fn conn.Se
 			cf:       cf,
 			conn:     conn,
 			cipher:   cipher,
-			remoteIp: net.ParseIP(remoteIp),
+			remoteIp: util.StrToIP(remoteIp),
 		}
 		fn(ws)
 	})
@@ -316,8 +316,10 @@ func (c *Ws2) ReadMessage(deadline int) (msgType conn.MsgType, id uint16, cmd st
 
 // 获取远程的地址
 func (c *Ws2) RemoteIP() net.IP {
-	return c.remoteIp
-	// return util.AddrToIP(c.conn.RemoteAddr())
+	if c.remoteIp != nil {
+		return c.remoteIp
+	}
+	return util.AddrToIP(c.conn.RemoteAddr())
 }
 
 // 获取本地的地址

+ 16 - 1
util/util.go

@@ -1,6 +1,10 @@
 package util
 
-import "net"
+import (
+	"log"
+	"net"
+	"net/netip"
+)
 
 // addrToIP attempts to extract a net.IP from a net.Addr
 func AddrToIP(addr net.Addr) net.IP {
@@ -16,3 +20,14 @@ func AddrToIP(addr net.Addr) net.IP {
 		return nil
 	}
 }
+
+func StrToIP(input string) net.IP {
+	addrPort, err := netip.ParseAddrPort(input)
+	if err != nil {
+		log.Println("[netip.ParseAddrPort ERROR]", err)
+		return nil
+	}
+
+	ip := addrPort.Addr()
+	return ip.AsSlice()
+}