iOS 1.0.3 source candidate only; no Apple build or installation acceptance
This commit is contained in:
+83
-31
@@ -65,9 +65,11 @@ final class AppState: ObservableObject {
|
||||
busy = true
|
||||
defer { busy = false }
|
||||
if vpn.isConnected {
|
||||
await vpn.disconnectAndWait()
|
||||
currentNode = nil
|
||||
message = "正在断开,系统将自动恢复原网络"
|
||||
do {
|
||||
try await vpn.disconnectAndWait()
|
||||
currentNode = nil
|
||||
message = "已断开,系统已撤销本应用的 VPN 连接"
|
||||
} catch { errorMessage = friendly(error) }
|
||||
return
|
||||
}
|
||||
guard let profile,
|
||||
@@ -77,7 +79,8 @@ final class AppState: ObservableObject {
|
||||
return
|
||||
}
|
||||
if route.id == "direct" {
|
||||
await vpn.disconnectAndWait()
|
||||
do { try await vpn.disconnectAndWait() }
|
||||
catch { errorMessage = friendly(error); return }
|
||||
currentNode = nil
|
||||
message = "当前为本地直连,不经过 VPN 节点"
|
||||
return
|
||||
@@ -91,44 +94,29 @@ final class AppState: ObservableObject {
|
||||
var excludedNodes: [String] = []
|
||||
var lastError: Error = UCVLError.connection("没有可用节点。")
|
||||
let maximumAttempts = max(1, min(profile.nodes.count, 3))
|
||||
// One explicit connect owns this state across all assigned nodes.
|
||||
// No timers or system restore path may promote a public connection.
|
||||
var transport = ProxyTransportSession()
|
||||
|
||||
for attempt in 1...maximumAttempts {
|
||||
var attemptedNode: VPNNode?
|
||||
do {
|
||||
let response = try await api.config(token: token, route: route.id, excluding: excludedNodes)
|
||||
attemptedNode = response.node
|
||||
let configJSON = try response.configJSONString()
|
||||
guard let remoteHost = TunnelConfigurationBuilder.remoteAddress(from: configJSON) else {
|
||||
throw UCVLError.invalidConfiguration("后台线路配置缺少节点域名。")
|
||||
}
|
||||
let gatewayHost = response.gateway?.host.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let pinnedGatewayIPv4 = response.gateway?.ipv4?.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let remoteAddress: String
|
||||
if let pinnedGatewayIPv4, NetworkAddress.isPublicIPv4(pinnedGatewayIPv4) {
|
||||
remoteAddress = pinnedGatewayIPv4
|
||||
} else {
|
||||
let hostToResolve = (gatewayHost?.isEmpty == false ? gatewayHost : nil) ?? remoteHost
|
||||
remoteAddress = try await api.resolvePublicIPv4(host: hostToResolve)
|
||||
}
|
||||
currentNode = response.node
|
||||
message = "正在连接 \(response.node.displayName)(第 \(attempt) 次验证)…"
|
||||
try await vpn.connect(
|
||||
configJSON: configJSON,
|
||||
resolvedRemoteAddress: remoteAddress,
|
||||
node: response.node,
|
||||
route: route
|
||||
)
|
||||
try await api.verifyProxyEgress()
|
||||
guard vpn.status == .connected else {
|
||||
throw UCVLError.connection("出口验证返回时 VPN 已断开,正在切换备用节点。")
|
||||
}
|
||||
message = "已连接 \(response.node.displayName),出口验证通过"
|
||||
try await connectAssigned(response, route: route, transport: &transport)
|
||||
return
|
||||
} catch {
|
||||
lastError = error
|
||||
let decision = ConnectionAttemptPolicy.decide(error: error, hasAssignedNode: attemptedNode != nil)
|
||||
currentNode = nil
|
||||
await vpn.disconnectAndWait()
|
||||
do { try await vpn.disconnectAndWait() }
|
||||
catch {
|
||||
errorMessage = friendly(error)
|
||||
message = "系统尚未完成断开,已停止切换线路"
|
||||
return
|
||||
}
|
||||
if decision == .revokeAuthorization {
|
||||
logoutLocal()
|
||||
errorMessage = friendly(error)
|
||||
@@ -149,11 +137,74 @@ final class AppState: ObservableObject {
|
||||
message = "所有可用节点均未通过验证,已恢复本地网络"
|
||||
}
|
||||
|
||||
private func connectAssigned(
|
||||
_ response: ClientConfigResponse, route: RouteMode, transport: inout ProxyTransportSession
|
||||
) async throws {
|
||||
let configJSON = try response.configJSONString()
|
||||
if !transport.domainOnly,
|
||||
let endpoint = ProxyTransportPolicy.authorizedInternalEndpoint(response: response) {
|
||||
let reachable = await InternalIPProbe.isReachable(endpoint.address)
|
||||
try Task.checkCancellation()
|
||||
if reachable {
|
||||
message = "正在通过内部 IP 连接 \(response.node.displayName)…"
|
||||
do {
|
||||
try await vpn.connect(
|
||||
configJSON: configJSON, resolvedRemoteAddress: endpoint.address,
|
||||
node: response.node, route: route, internalEndpoint: endpoint,
|
||||
configContract: response.configContract, connectionScope: response.connectionScope
|
||||
)
|
||||
try await verifyActiveEgress()
|
||||
message = "已连接 \(response.node.displayName) · 内部 IP,出口验证通过"
|
||||
return
|
||||
} catch {
|
||||
guard ConnectionAttemptPolicy.decide(error: error, hasAssignedNode: true) == .retryNode else {
|
||||
throw error
|
||||
}
|
||||
try await vpn.disconnectAndWait()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transport.commitPublicFallback()
|
||||
try Task.checkCancellation()
|
||||
// Resolve only when public transport is actually needed. DNS failure
|
||||
// must not prevent an otherwise usable authorized internal endpoint.
|
||||
guard let remoteHost = TunnelConfigurationBuilder.remoteAddress(from: configJSON) else {
|
||||
throw UCVLError.invalidConfiguration("后台线路配置缺少节点域名。")
|
||||
}
|
||||
let gatewayHost = response.gateway?.host.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let pinnedAddress = response.gateway?.ipv4?.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let publicAddress: String
|
||||
if let pinnedAddress, NetworkAddress.isPublicIPv4(pinnedAddress) {
|
||||
publicAddress = pinnedAddress
|
||||
} else {
|
||||
publicAddress = try await api.resolvePublicIPv4(
|
||||
host: (gatewayHost?.isEmpty == false ? gatewayHost : nil) ?? remoteHost
|
||||
)
|
||||
}
|
||||
message = "正在通过域名入口连接 \(response.node.displayName)…"
|
||||
try await vpn.connect(
|
||||
configJSON: configJSON, resolvedRemoteAddress: publicAddress, node: response.node, route: route
|
||||
)
|
||||
try await verifyActiveEgress()
|
||||
message = "已连接 \(response.node.displayName) · 域名入口,本次连接保持不切换"
|
||||
}
|
||||
|
||||
private func verifyActiveEgress() async throws {
|
||||
try Task.checkCancellation()
|
||||
try await api.verifyProxyEgress()
|
||||
try Task.checkCancellation()
|
||||
guard vpn.status == .connected else {
|
||||
throw UCVLError.connection("出口验证返回时 VPN 已断开,正在尝试备用入口。")
|
||||
}
|
||||
}
|
||||
|
||||
func logout() async {
|
||||
guard !busy else { return }
|
||||
busy = true
|
||||
defer { busy = false }
|
||||
await vpn.disconnectAndWait()
|
||||
do { try await vpn.disconnectAndWait() }
|
||||
catch { errorMessage = friendly(error); return }
|
||||
if let token = SecureStore.token() {
|
||||
await api.logout(token: token)
|
||||
}
|
||||
@@ -182,7 +233,8 @@ final class AppState: ObservableObject {
|
||||
apply(try await api.profile(token: token))
|
||||
message = "账号和线路已同步"
|
||||
} catch UCVLError.api(let status, let message) where status == 401 || status == 403 {
|
||||
await vpn.disconnectAndWait()
|
||||
do { try await vpn.disconnectAndWait() }
|
||||
catch { errorMessage = friendly(error) }
|
||||
SecureStore.clearToken()
|
||||
profile = nil
|
||||
errorMessage = message
|
||||
|
||||
Reference in New Issue
Block a user