From f074c485b93acd28fa2fed770b10f0727568ad8b Mon Sep 17 00:00:00 2001 From: kony <2312708932@qq.com> Date: Tue, 31 Mar 2026 09:50:34 +0800 Subject: [PATCH] Add version compatibility check function to config package and update usage in local and remote handlers. This change enhances version management by allowing compatibility checks based on major and minor version segments, improving error handling for version mismatches. --- config/version.go | 14 ++++++++++++++ pro/local.go | 4 ++-- pro/remote.go | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/config/version.go b/config/version.go index e5d015b..08aded4 100644 --- a/config/version.go +++ b/config/version.go @@ -1,5 +1,7 @@ package config +import "strings" + var ( m_version string ) @@ -11,3 +13,15 @@ func SetVersion(v string) { func GetVersion() string { return m_version } + +// VersionsCompatible reports whether two X.Y.Z style version strings are +// wire-compatible: the first two dot-separated segments must match. If either +// string has fewer than two segments, falls back to exact string equality. +func VersionsCompatible(a, b string) bool { + pa := strings.Split(a, ".") + pb := strings.Split(b, ".") + if len(pa) >= 2 && len(pb) >= 2 { + return pa[0] == pb[0] && pa[1] == pb[1] + } + return a == b +} diff --git a/pro/local.go b/pro/local.go index 642e0fe..b120df9 100644 --- a/pro/local.go +++ b/pro/local.go @@ -70,8 +70,8 @@ func handleState0_RegisterSession(sessionID string, redisJson *RedisJsonType, co func handleState1_ProcessRemoteAddr(sessionID string, redisJson *RedisJsonType, conn *net.UDPConn, addr *tun.AddrType, conn_type int, tun_active **tun.TunActive, tun_passive **tun.TunPassive) error { log.Printf("State 1: 收到Remote端地址: %v", redisJson.RemoteAddr) - // 版本兼容性检查 - if redisJson.RemoteVersion != config.GetVersion() { + // 版本兼容性检查(主版本.次版本一致即可) + if !config.VersionsCompatible(config.GetVersion(), redisJson.RemoteVersion) { log.Printf("两端版本不兼容: Local: %s => Remote: %s", config.GetVersion(), redisJson.RemoteVersion) UpdateStartButtonStatue(TagStatusVersionMismatch) RedisSessionDel(sessionID) diff --git a/pro/remote.go b/pro/remote.go index 99c6cac..5c3797c 100644 --- a/pro/remote.go +++ b/pro/remote.go @@ -27,8 +27,8 @@ func handleState1_SendRemoteAddr(sessionID string, redisJson *RedisJsonType, tun redisJson.SocketTimeOut = time.Duration(config.Arg_p2p_timeout) * time.Second redisJson.RedisTimeOut = redisJson.SocketTimeOut * 3 - // 版本兼容性检查 - if redisJson.LocalVersion != config.GetVersion() { + // 版本兼容性检查(主版本.次版本一致即可) + if !config.VersionsCompatible(config.GetVersion(), redisJson.LocalVersion) { log.Printf("会话 %s 两端版本不兼容: Local: %s => Remote: %s", sessionID, redisJson.LocalVersion, config.GetVersion()) redisJson.State = -1 // 设置版本不一致状态,告知Local端 RedisSessionSet(sessionID, redisJson.SocketTimeOut*3, redisJson)