From e920056ccb292957217feb59c846193055c4e76a Mon Sep 17 00:00:00 2001 From: kony <2312708932@qq.com> Date: Thu, 23 Jan 2025 19:49:55 +0800 Subject: [PATCH] add ipv6 and lan --- pro/local.go | 37 ++++++++++++++++++++++++++----------- pro/remote.go | 30 ++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/pro/local.go b/pro/local.go index 636803e..01d5afe 100644 --- a/pro/local.go +++ b/pro/local.go @@ -72,13 +72,21 @@ func GetLocalQuicConn(conn *net.UDPConn, addr *AddrType, conn_type int, count in tun_active = nil redisJson.LocalAddr = *addr - if redisJson.LocalAddr.WanIPv4 == redisJson.RemoteAddr.WanIPv4 { - RedisDel() - return tun_active, tun_passive, nil, nil, fmt.Errorf("和对端处在同一个公网IP, 退出") - } - tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.RemoteAddr.WanIPv4, redisJson.RemoteAddr.WanPort1, redisJson.RemoteAddr.WanPort2, redisJson.SendPortCount) - tun_passive.Start() + if redisJson.LocalAddr.IPv6 != "" && redisJson.RemoteAddr.IPv6 != "" { + utils.Log().Debug("IPv6直连") + tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.RemoteAddr.IPv6, redisJson.RemoteAddr.LocalPort, 0, redisJson.SendPortCount) + tun_passive.Start1() + + } else if redisJson.LocalAddr.WanIPv4 == redisJson.RemoteAddr.WanIPv4 { + utils.Log().Debug("内网直连") + tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.RemoteAddr.LocalIPv4, redisJson.RemoteAddr.LocalPort, 0, redisJson.SendPortCount) + tun_passive.Start1() + + } else { + tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.RemoteAddr.WanIPv4, redisJson.RemoteAddr.WanPort1, redisJson.RemoteAddr.WanPort2, redisJson.SendPortCount) + tun_passive.Start() + } redisJson.State = 2 utils.Log().DebugF("发送本端地址: %v", redisJson.LocalAddr) @@ -90,13 +98,20 @@ func GetLocalQuicConn(conn *net.UDPConn, addr *AddrType, conn_type int, count in } tun_passive = nil - if redisJson.LocalAddr.WanIPv4 == redisJson.RemoteAddr.WanIPv4 { - RedisDel() - return tun_active, tun_passive, nil, nil, fmt.Errorf("已经和对端处在同一个公网IP, 退出") + tun_active = tun.CreateTunActive([]byte(redisJson.SessionID), conn) + + if redisJson.LocalAddr.IPv6 != "" && redisJson.RemoteAddr.IPv6 != "" { + utils.Log().Debug("IPv6直连") + tun_active.Start1(redisJson.RemoteAddr.IPv6, redisJson.RemoteAddr.LocalPort, 0) + + } else if redisJson.LocalAddr.WanIPv4 == redisJson.RemoteAddr.WanIPv4 { + utils.Log().Debug("内网直连") + tun_active.Start1(redisJson.RemoteAddr.LocalIPv4, redisJson.RemoteAddr.LocalPort, 0) + + } else { + tun_active.Start(redisJson.LocalAddr.WanPort1, redisJson.LocalAddr.WanPort2, redisJson.RemoteAddr.WanIPv4, redisJson.RemoteAddr.WanPort1, redisJson.RemoteAddr.WanPort2) } - tun_active = tun.CreateTunActive([]byte(redisJson.SessionID), conn, 15*time.Second) - tun_active.Start(redisJson.LocalAddr.WanPort1, redisJson.LocalAddr.WanPort2, redisJson.RemoteAddr.WanIPv4, redisJson.RemoteAddr.WanPort1, redisJson.RemoteAddr.WanPort2, redisJson.SocketTimeOut) redisJson.State = 2 RedisSet(redisJson.RedisTimeOut, &redisJson) } diff --git a/pro/remote.go b/pro/remote.go index 384c4b5..6ed0f46 100644 --- a/pro/remote.go +++ b/pro/remote.go @@ -85,7 +85,7 @@ func GetRemoteQuicConn(conn *net.UDPConn, addr *AddrType, time_out time.Duration } tun_passive = nil - tun_active = tun.CreateTunActive([]byte(redisJson.SessionID), conn, time_out) + tun_active = tun.CreateTunActive([]byte(redisJson.SessionID), conn) tun_active_chain = tun_active.GetChain() redisJson.State = 1 @@ -102,8 +102,20 @@ func GetRemoteQuicConn(conn *net.UDPConn, addr *AddrType, time_out time.Duration } tun_active = nil - tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.LocalAddr.WanIPv4, redisJson.LocalAddr.WanPort1, redisJson.LocalAddr.WanPort2, 0x100) - tun_passive.Start() + if redisJson.LocalAddr.IPv6 != "" && redisJson.RemoteAddr.IPv6 != "" { + utils.Log().Debug("IPv6直连") + tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.LocalAddr.IPv6, redisJson.LocalAddr.LocalPort, 0, 0x100) + tun_passive.Start1() + + } else if redisJson.LocalAddr.WanIPv4 == redisJson.RemoteAddr.WanIPv4 { + utils.Log().Debug("内网直连") + tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.LocalAddr.LocalIPv4, redisJson.LocalAddr.LocalPort, 0, 0x100) + tun_passive.Start1() + + } else { + tun_passive = tun.CteateTunPassive([]byte(redisJson.SessionID), conn, redisJson.LocalAddr.WanIPv4, redisJson.LocalAddr.WanPort1, redisJson.LocalAddr.WanPort2, 0x100) + tun_passive.Start() + } tun_passive_chain = tun_passive.GetChain() @@ -116,7 +128,17 @@ func GetRemoteQuicConn(conn *net.UDPConn, addr *AddrType, time_out time.Duration switch conn_type { case 0: utils.Log().DebugF("收到对端地址: %v", redisJson.LocalAddr) - tun_active.Start(redisJson.RemoteAddr.WanPort1, redisJson.RemoteAddr.WanPort2, redisJson.LocalAddr.WanIPv4, redisJson.LocalAddr.WanPort1, redisJson.LocalAddr.WanPort2, redisJson.SocketTimeOut) + if redisJson.LocalAddr.IPv6 != "" && redisJson.RemoteAddr.IPv6 != "" { + utils.Log().Debug("IPv6直连") + tun_active.Start1(redisJson.LocalAddr.IPv6, redisJson.LocalAddr.LocalPort, 0) + + } else if redisJson.LocalAddr.WanIPv4 == redisJson.RemoteAddr.WanIPv4 { + utils.Log().Debug("内网直连") + tun_active.Start1(redisJson.LocalAddr.LocalIPv4, redisJson.LocalAddr.LocalPort, 0) + + } else { + tun_active.Start(redisJson.RemoteAddr.WanPort1, redisJson.RemoteAddr.WanPort2, redisJson.LocalAddr.WanIPv4, redisJson.LocalAddr.WanPort1, redisJson.LocalAddr.WanPort2) + } case 1: utils.Log().DebugF("收到对端地址, 等待连接: %v", redisJson.LocalAddr)