111 lines
3.4 KiB
Plaintext
111 lines
3.4 KiB
Plaintext
//
|
||
// APIConfig.swift
|
||
// YuMi
|
||
//
|
||
// Created by AI on 2025-10-09.
|
||
// Copyright © 2025 YuMi. All rights reserved.
|
||
//
|
||
|
||
import Foundation
|
||
|
||
/// API 域名配置类
|
||
/// 使用 XOR + Base64 双重混淆防止反编译
|
||
@objc class APIConfig: NSObject {
|
||
|
||
// MARK: - Private Properties
|
||
|
||
/// XOR 加密密钥
|
||
private static let xorKey: UInt8 = 77
|
||
|
||
/// RELEASE 环境域名(加密后)
|
||
/// 原始域名:https://api.epartylive.com
|
||
private static let releaseEncodedParts: [String] = [
|
||
"JTk5PT53YmI=", // https:// (XOR 后 Base64)
|
||
"LD0kYw==", // api. (XOR 后 Base64)
|
||
"KD0sPzk0ISQ7KGMuIiA=", // epartylive.com (XOR 后 Base64)
|
||
]
|
||
|
||
// MARK: - Public Methods
|
||
|
||
/// 获取 API 基础域名
|
||
/// - Returns: 根据编译环境返回对应的域名
|
||
@objc static func baseURL() -> String {
|
||
#if DEBUG
|
||
// DEV 环境:临时使用硬编码(等 Bridging 修复后再改回 HttpRequestHelper)
|
||
// TODO: 修复后改为 return HttpRequestHelper.getHostUrl()
|
||
return getDevBaseURL()
|
||
#else
|
||
// RELEASE 环境:使用动态生成的新域名
|
||
let url = decodeURL(from: releaseEncodedParts)
|
||
|
||
// 验证解密结果
|
||
if url.isEmpty || !url.hasPrefix("http") {
|
||
NSLog("[APIConfig] 警告:域名解密失败,使用备用域名")
|
||
return backupURL()
|
||
}
|
||
|
||
return url
|
||
#endif
|
||
}
|
||
|
||
/// 获取 DEV 环境域名(临时方案)
|
||
/// - Returns: DEV 域名
|
||
private static func getDevBaseURL() -> String {
|
||
// 从 UserDefaults 读取(原 HttpRequestHelper 的逻辑)
|
||
#if DEBUG
|
||
let isProduction = UserDefaults.standard.string(forKey: "kIsProductionEnvironment")
|
||
if isProduction == "YES" {
|
||
return "https://api.epartylive.com" // 正式环境
|
||
} else {
|
||
return "https://test-api.yourdomain.com" // 测试环境(请替换为实际测试域名)
|
||
}
|
||
#else
|
||
return "https://api.epartylive.com"
|
||
#endif
|
||
}
|
||
|
||
/// 备用域名(降级方案)
|
||
/// - Returns: 原域名(仅在解密失败时使用)
|
||
@objc static func backupURL() -> String {
|
||
return getDevBaseURL()
|
||
}
|
||
|
||
// MARK: - Private Methods
|
||
|
||
/// 解密域名
|
||
/// - Parameter parts: 加密后的域名片段数组
|
||
/// - Returns: 解密后的完整域名
|
||
private static func decodeURL(from parts: [String]) -> String {
|
||
let decoded = parts.compactMap { part -> String? in
|
||
guard let data = Data(base64Encoded: part) else {
|
||
NSLog("[APIConfig] Base64 解码失败: \(part)")
|
||
return nil
|
||
}
|
||
let xored = data.map { $0 ^ xorKey }
|
||
return String(bytes: xored, encoding: .utf8)
|
||
}
|
||
|
||
let result = decoded.joined()
|
||
|
||
#if DEBUG
|
||
NSLog("[APIConfig] 解密后的域名: \(result)")
|
||
#endif
|
||
|
||
return result
|
||
}
|
||
}
|
||
|
||
// MARK: - Debug Helper
|
||
|
||
#if DEBUG
|
||
extension APIConfig {
|
||
/// 测试方法:验证域名加密/解密是否正常
|
||
@objc static func testEncryption() {
|
||
print("=== APIConfig 加密测试 ===")
|
||
print("Release 域名: \(decodeURL(from: releaseEncodedParts))")
|
||
print("当前环境域名: \(baseURL())")
|
||
print("备用域名: \(backupURL())")
|
||
}
|
||
}
|
||
#endif
|