From 7b156f757e2b7d5cfd8bda998a375be6ed6af878 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 26 May 2026 17:17:07 +0800 Subject: [PATCH] fix env support --- Tun/Configuration/SDLConfiguration.swift | 8 +++ Tun/Diagnostics/SDLLogger.swift | 16 ++++-- punchnet.xcodeproj/project.pbxproj | 8 +-- punchnet/Core/SystemConfig.swift | 49 ++++++++++++++----- punchnet/Core/VPNManager.swift | 8 +-- punchnet/Shared/Networking/SDLAPIClient.swift | 4 +- 6 files changed, 70 insertions(+), 23 deletions(-) diff --git a/Tun/Configuration/SDLConfiguration.swift b/Tun/Configuration/SDLConfiguration.swift index 331ead2..7df78c2 100644 --- a/Tun/Configuration/SDLConfiguration.swift +++ b/Tun/Configuration/SDLConfiguration.swift @@ -73,6 +73,8 @@ public class SDLConfiguration { let hostname: String let accessToken: String let identityId: UInt32 + let env: String + var acl: ACL var exitNode: ExitNode? @@ -85,6 +87,7 @@ public class SDLConfiguration { hostname: String, accessToken: String, identityId: UInt32, + env: String, acl: ACL, exitNode: ExitNode?) { self.version = version @@ -102,8 +105,11 @@ public class SDLConfiguration { self.accessToken = accessToken self.identityId = identityId self.acl = acl + self.env = env self.hostname = hostname self.exitNode = exitNode + + setenv("RUNTIME_ENV", env, 1) } } @@ -132,6 +138,7 @@ extension SDLConfiguration { let identityId = options["identity_id"] as? UInt32, let clientId = options["client_id"] as? String, let hostname = options["hostname"] as? String, + let env = options["env"] as? String, let networkAddressDict = options["network_address"] as? [String: NSObject] else { return nil } @@ -160,6 +167,7 @@ extension SDLConfiguration { hostname: hostname, accessToken: accessToken, identityId: identityId, + env: env, acl: acl, exitNode: exitNode) } diff --git a/Tun/Diagnostics/SDLLogger.swift b/Tun/Diagnostics/SDLLogger.swift index 1060228..43f6f24 100644 --- a/Tun/Diagnostics/SDLLogger.swift +++ b/Tun/Diagnostics/SDLLogger.swift @@ -9,9 +9,9 @@ import os public class SDLLogger: @unchecked Sendable { - public enum Subsystem: String, CaseIterable { - case debug = "com.jihe.punchnet.debug" - case trace = "com.jihe.punchnet.trace" + public enum Subsystem: CaseIterable { + case debug + case trace } private static let debugLoggingEnabled = ProcessInfo.processInfo.environment["SDL_DEBUG_LOG"] != "0" @@ -23,7 +23,15 @@ public class SDLLogger: @unchecked Sendable { private let log: Logger private init(subsystem: Subsystem) { - self.log = Logger(subsystem: subsystem.rawValue, category: "punchnet") + let env = ProcessInfo.processInfo.environment["RUNTIME_ENV"] ?? "prod" + var label: String + switch subsystem { + case .debug: + label = "com.jihe.punchnet-\(env).debug" + case .trace: + label = "com.jihe.punchnet-\(env).trace" + } + self.log = Logger(subsystem: label, category: "punchnet") } public func _log(_ message: String) { diff --git a/punchnet.xcodeproj/project.pbxproj b/punchnet.xcodeproj/project.pbxproj index 9a4613d..9507252 100644 --- a/punchnet.xcodeproj/project.pbxproj +++ b/punchnet.xcodeproj/project.pbxproj @@ -591,10 +591,10 @@ ); MACOSX_DEPLOYMENT_TARGET = 15.6; MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = com.jihe.punchnetmac; + PRODUCT_BUNDLE_IDENTIFIER = com.jihe.punchnetmac.dev; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; - "PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = MacPunchnetTest; + "PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = MacPunchnetTestDev; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_VERSION = 5.0; }; @@ -728,10 +728,10 @@ ); MACOSX_DEPLOYMENT_TARGET = 15.6; MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = com.jihe.punchnetmac.tun; + PRODUCT_BUNDLE_IDENTIFIER = com.jihe.punchnetmac.dev.tun; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; - "PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = MacPunchnetTunTest; + "PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = MacPunchnetTunTestDev; SKIP_INSTALL = YES; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_VERSION = 5.0; diff --git a/punchnet/Core/SystemConfig.swift b/punchnet/Core/SystemConfig.swift index 6e2e9d4..275cb16 100644 --- a/punchnet/Core/SystemConfig.swift +++ b/punchnet/Core/SystemConfig.swift @@ -8,6 +8,21 @@ import Foundation struct SystemConfig { + + struct EnvConfig { + let serverHost: String + let stunAssistHost: String + let tunBundleId: String + let localizedDescription: String + } + + enum Env: String { + case dev = "dev" + case prod = "prod" + } + + static let env: Env = .dev + // 版本设置 static let version: Int = 1 @@ -18,10 +33,20 @@ struct SystemConfig { // 渠道相关 static let channel = "appstore" - static let serverHost = "test.punchsky.com" - - // stun探测辅助服务器ip - static let stunAssistHost = "test.punchsky.com" + static let envConfigs: [Env: EnvConfig] = [ + .dev: EnvConfig( + serverHost: "test.punchsky.com", + stunAssistHost: "test.punchsky.com", + tunBundleId: "com.jihe.punchnetmac.dev.tun", + localizedDescription: "punchnetmac-dev" + ), + .prod: EnvConfig( + serverHost: "root.punchsky.com", + stunAssistHost: "root.punchsky.com", + tunBundleId: "com.jihe.punchnetmac.tun", + localizedDescription: "punchnetmac" + ) + ] // 获取系统信息 static let systemInfo: String = { @@ -29,6 +54,10 @@ struct SystemConfig { return "macOS \(version.majorVersion).\(version.minorVersion)" }() + static func getCurrentEnvConfig() -> EnvConfig { + return envConfigs[Self.env]! + } + static func getOptions(networkId: UInt32, networkDomain: String, ip: String, @@ -38,11 +67,8 @@ struct SystemConfig { hostname: String, acl: NetworkContext.ACL, exitNodeIp: String?) -> [String: NSObject] { -// guard let serverIp = DNSResolver.resolveAddrInfos(serverHost).first, -// let stunAssistIp = DNSResolver.resolveAddrInfos(stunAssistHost).first else { -// return nil -// } - + + let envConfig = getCurrentEnvConfig() let clientId = getClientId() let mac = getMacAddress() @@ -51,9 +77,10 @@ struct SystemConfig { "client_id": clientId as NSObject, "access_token": accessToken as NSObject, "identity_id": identityId as NSObject, - "server_host": serverHost as NSObject, - "stun_assist_host": stunAssistHost as NSObject, + "server_host": envConfig.serverHost as NSObject, + "stun_assist_host": envConfig.stunAssistHost as NSObject, "hostname": hostname as NSObject, + "env": Self.env.rawValue as NSObject, "acl": [ "tcp": acl.tcp as NSObject, "udp": acl.udp as NSObject diff --git a/punchnet/Core/VPNManager.swift b/punchnet/Core/VPNManager.swift index 6dbe654..a3a37bc 100644 --- a/punchnet/Core/VPNManager.swift +++ b/punchnet/Core/VPNManager.swift @@ -125,15 +125,17 @@ class VPNManager { private func loadAndCreateProviderManager() async throws -> NETunnelProviderManager { let managers = try await NETunnelProviderManager.loadAllFromPreferences() + let env = SystemConfig.getCurrentEnvConfig() + let manager = managers.first ?? NETunnelProviderManager() - manager.localizedDescription = "punchnetmac" + manager.localizedDescription = env.localizedDescription manager.isEnabled = true // 设置相关参数,在PacketTunnel中可以用 let protocolConfiguration = NETunnelProviderProtocol() - protocolConfiguration.serverAddress = "punchnetmac" + protocolConfiguration.serverAddress = env.localizedDescription protocolConfiguration.providerConfiguration = [String:AnyObject]() - protocolConfiguration.providerBundleIdentifier = "com.jihe.punchnetmac.tun" + protocolConfiguration.providerBundleIdentifier = env.tunBundleId manager.protocolConfiguration = protocolConfiguration manager.isOnDemandEnabled = false diff --git a/punchnet/Shared/Networking/SDLAPIClient.swift b/punchnet/Shared/Networking/SDLAPIClient.swift index 13c9ef6..c0e0267 100644 --- a/punchnet/Shared/Networking/SDLAPIClient.swift +++ b/punchnet/Shared/Networking/SDLAPIClient.swift @@ -21,7 +21,9 @@ struct SDLAPIError: Error, Decodable { struct SDLAPIClient { static var baseUrl: String { - return "https://\(SystemConfig.serverHost)/api" + let envConfig = SystemConfig.getCurrentEnvConfig() + + return "https://\(envConfig.serverHost)/api" } static private let token: String = "H6p*2RfEu4ITcL"