diff --git a/pro/comm.go b/pro/comm.go index 0bb4daf..5b1f8ce 100644 --- a/pro/comm.go +++ b/pro/comm.go @@ -66,20 +66,24 @@ func Release(tun_active *tun.TunActive, tun_passive *tun.TunPassive) { } type RedisJsonType struct { - SessionID string `bson:"session_id" json:"session_id"` - State int `bson:"state" json:"state"` - SocketTimeOut time.Duration `bson:"SocketTimeOut" json:"SocketTimeOut"` - RedisTimeOut time.Duration `bson:"RedisTimeOut" json:"RedisTimeOut"` - SendPortCount int `bson:"send_port_count" json:"send_port_count"` - ConnectCount int `bson:"connect_count" json:"connect_count"` - RemoteIP string `bson:"remote_ip" json:"remote_ip"` - RemotePort0 int `bson:"remote_port0" json:"remote_port0"` - RemotePort1 int `bson:"remote_port1" json:"remote_port1"` - RemotePort2 int `bson:"remote_port2" json:"remote_port2"` - LocalIP string `bson:"local_ip" json:"local_ip"` - LocalPort0 int `bson:"local_port0" json:"local_port0"` - LocalPort1 int `bson:"local_port1" json:"local_port1"` - LocalPort2 int `bson:"local_port2" json:"local_port2"` + SessionID string `bson:"session_id" json:"session_id"` + State int `bson:"state" json:"state"` + SocketTimeOut time.Duration `bson:"SocketTimeOut" json:"SocketTimeOut"` + RedisTimeOut time.Duration `bson:"RedisTimeOut" json:"RedisTimeOut"` + SendPortCount int `bson:"send_port_count" json:"send_port_count"` + ConnectCount int `bson:"connect_count" json:"connect_count"` + RemoteWanIPv4 string `bson:"remote_ip" json:"remote_wan_ip_v4"` // Remote端, 外网IPv4地址 + RemoteLocalIPv4 string `bson:"remote_ip" json:"remote_local_ip_v4"` // Remote端, 本地IPv4地址 + RemoteIPv6 string `bson:"remote_ip" json:"remote_ip_v6"` // Remote端, IPv6地址 + RemoteLocalPort int `bson:"remote_port0" json:"remote_local_port"` // Remote端, 本地端口 + RemoteWanPort1 int `bson:"remote_port1" json:"remote_wan_port1"` // Remote端, 外网端口1 + RemoteWanPort2 int `bson:"remote_port2" json:"remote_wan_port2"` // Remote端, 外网端口2 + LocalWanIPv4 string `bson:"local_ip" json:"local_wan_ip_v4"` // Remote端, 外网IPv4地址 + LocalLocalIPv4 string `bson:"local_ip" json:"local_local_ip_v4"` // Local端, 本地IPv4地址 + LocalIPv6 string `bson:"local_ip" json:"local_ip_v6"` // Local端, IPv6地址 + LocalLocalPort int `bson:"local_port0" json:"local_local_port"` // Local端, 本地端口 + LocalWanPort1 int `bson:"local_port1" json:"local_wan_port1"` // Local端, 外网端口1 + LocalWanPort2 int `bson:"local_port2" json:"local_wan_port2"` // Local端, 外网端口2 } func RedisSet(time_out time.Duration, redisJson *RedisJsonType) error { diff --git a/pro/local.go b/pro/local.go index 9cc6c89..6dada74 100644 --- a/pro/local.go +++ b/pro/local.go @@ -29,9 +29,9 @@ func GetLocalQuicConn(conn_type int, count int) (*tun.TunActive, *tun.TunPassive } conn := utils.GetListenUDP("udp4") - redisJson.LocalPort0 = conn.LocalAddr().(*net.UDPAddr).Port - LocalIP, LocalPort1, LocalPort2 := stun2.GetWanIpPort2(conn) - if LocalPort1 == LocalPort2 { + redisJson.LocalLocalPort = conn.LocalAddr().(*net.UDPAddr).Port + WanIPv4, WanPort1, WanPort2 := stun2.GetWanIpPort2(conn) + if WanPort1 == WanPort2 { conn_type = 0 } @@ -45,7 +45,7 @@ func GetLocalQuicConn(conn_type int, count int) (*tun.TunActive, *tun.TunPassive RedisSet(15*time.Second, &redisJson) default: - redisJson.LocalIP, redisJson.LocalPort1, redisJson.LocalPort2 = LocalIP, LocalPort1, LocalPort2 + redisJson.LocalWanIPv4, redisJson.LocalWanPort1, redisJson.LocalWanPort2 = WanIPv4, WanPort1, WanPort2 redisJson.State = 0 utils.Log().DebugF("发送本端地址: %v", redisJson) RedisSet(15*time.Second, &redisJson) @@ -77,13 +77,13 @@ func GetLocalQuicConn(conn_type int, count int) (*tun.TunActive, *tun.TunPassive } tun_active = nil - redisJson.LocalIP, redisJson.LocalPort1, redisJson.LocalPort2 = LocalIP, LocalPort1, LocalPort2 - if redisJson.LocalIP == redisJson.RemoteIP { + redisJson.LocalWanIPv4, redisJson.LocalWanPort1, redisJson.LocalWanPort2 = WanIPv4, WanPort1, WanPort2 + if redisJson.LocalWanIPv4 == redisJson.RemoteWanIPv4 { RedisDel() - return tun_active, tun_passive, nil, nil, fmt.Errorf("已经和对端处在同一个公网下") + return tun_active, tun_passive, nil, nil, fmt.Errorf("和对端处在同一个公网IP, 退出") } - tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.RemoteIP, redisJson.RemotePort1, redisJson.RemotePort2, redisJson.SendPortCount) + tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.RemoteWanIPv4, redisJson.RemoteWanPort1, redisJson.RemoteWanPort2, redisJson.SendPortCount) tun_passive.Start() redisJson.State = 2 @@ -96,13 +96,13 @@ func GetLocalQuicConn(conn_type int, count int) (*tun.TunActive, *tun.TunPassive } tun_passive = nil - if redisJson.LocalIP == redisJson.RemoteIP { + if redisJson.LocalWanIPv4 == redisJson.RemoteWanIPv4 { RedisDel() - return tun_active, tun_passive, nil, nil, fmt.Errorf("已经和对端处在同一个公网下") + return tun_active, tun_passive, nil, nil, fmt.Errorf("已经和对端处在同一个公网IP, 退出") } tun_active = tun.CreateTunActive([]byte(redisJson.SessionID), conn, 15*time.Second) - tun_active.Start(redisJson.LocalPort1, redisJson.LocalPort2, redisJson.RemoteIP, redisJson.RemotePort1, redisJson.RemotePort2, redisJson.SocketTimeOut) + tun_active.Start(redisJson.LocalWanPort1, redisJson.LocalWanPort2, redisJson.RemoteWanIPv4, redisJson.RemoteWanPort1, redisJson.RemoteWanPort2, redisJson.SocketTimeOut) redisJson.State = 2 RedisSet(redisJson.RedisTimeOut, &redisJson) } diff --git a/pro/remote.go b/pro/remote.go index 0983ac9..c0b22f2 100644 --- a/pro/remote.go +++ b/pro/remote.go @@ -73,10 +73,10 @@ func GetRemoteQuicConn(time_out time.Duration) (*tun.TunActive, *tun.TunPassive, utils.Log().DebugF("收到对端请求: %v", redisJson) conn := utils.GetListenUDP("udp4") - redisJson.RemotePort0 = conn.LocalAddr().(*net.UDPAddr).Port - redisJson.RemoteIP, redisJson.RemotePort1, redisJson.RemotePort2 = stun2.GetWanIpPort2(conn) + redisJson.RemoteLocalPort = conn.LocalAddr().(*net.UDPAddr).Port + redisJson.RemoteWanIPv4, redisJson.RemoteWanPort1, redisJson.RemoteWanPort2 = stun2.GetWanIpPort2(conn) - switch redisJson.LocalPort1 { + switch redisJson.LocalWanPort1 { case 0: conn_type = 0 utils.Log().Debug("对端未发来IP") @@ -103,7 +103,7 @@ func GetRemoteQuicConn(time_out time.Duration) (*tun.TunActive, *tun.TunPassive, } tun_active = nil - tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.LocalIP, redisJson.LocalPort1, redisJson.LocalPort2, 0x100) + tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.LocalWanIPv4, redisJson.LocalWanPort1, redisJson.LocalWanPort2, 0x100) tun_passive.Start() tun_passive_chain = tun_passive.GetChain() @@ -117,7 +117,7 @@ func GetRemoteQuicConn(time_out time.Duration) (*tun.TunActive, *tun.TunPassive, switch conn_type { case 0: utils.Log().DebugF("收到对端地址: %v", redisJson) - tun_active.Start(redisJson.RemotePort1, redisJson.RemotePort2, redisJson.LocalIP, redisJson.LocalPort1, redisJson.LocalPort2, redisJson.SocketTimeOut) + tun_active.Start(redisJson.RemoteWanPort1, redisJson.RemoteWanPort2, redisJson.LocalWanIPv4, redisJson.LocalWanPort1, redisJson.LocalWanPort2, redisJson.SocketTimeOut) case 1: utils.Log().DebugF("收到对端地址, 等待连接: %v", redisJson) diff --git a/utils/net.go b/utils/net.go index bf7bb76..ac1508c 100644 --- a/utils/net.go +++ b/utils/net.go @@ -19,13 +19,13 @@ func GetListenUDP(level string) *net.UDPConn { return conn } -func GetListenUDP2(level string, port int) *net.UDPConn { - if addr, err := net.ResolveUDPAddr(level, fmt.Sprintf(":%d", port)); addr != nil && err == nil { - if conn, err := net.ListenUDP(level, addr); conn != nil && err == nil { - return conn - } +func GetListenUDPPort(level string, port int) *net.UDPConn { + addr, _ := net.ResolveUDPAddr(level, fmt.Sprintf(":%d", port)) + conn, err := net.ListenUDP(level, addr) + if err != nil { + Log().ErrorF("绑定端口失败: %v", err) } - return nil + return conn } func GetUDPLocalIPPort(level string) (string, int) {