- carthage update --no-use-binaries --platform ios --use-xcframeworks
拉取代码报错:Task failed with exit code 65:
原因是拉取的第三方库太老,造成编译失败
由于对carthage不熟悉,使用传统方法解决
项目工程下,Carthage/Checkouts目录第三方库支持的最低版本改为ios11
- 将wireguard改为有连接时,连接中WiFi网络被关闭
wireguard默认是无连接通讯模式,为向openvn对齐,修改官方代码通讯模式,改为有连接通讯。当wireguard相关握手完成后,vn才正在连接完成
官方原startTunnel方法
{
let activationAttemptId = options?["activationAttemptId"] as? String
let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId)
Logger.configureGlobal(tagged: "NET", withFilePath: FileManager.logFileURL?.path)
wg_log(.info, message: "Starting tunnel from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app"))
guard let tunnelProviderProtocol = self.protocolConfiguration as? NETunnelProviderProtocol,
let tunnelConfiguration = tunnelProviderProtocol.asTunnelConfiguration() else {
errorNotifier.notify(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
completionHandler(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
return
}
// Start the tunnel
adapter.start(tunnelConfiguration: tunnelConfiguration) { adapterError in
guard let adapterError = adapterError else {
let interfaceName = self.adapter.interfaceName ?? "unknown"
wg_log(.info, message: "Tunnel interface is \(interfaceName)")
completionHandler(nil)
return
}
switch adapterError {
case .cannotLocateTunnelFileDescriptor:
wg_log(.error, staticMessage: "Starting tunnel failed: could not determine file descriptor")
errorNotifier.notify(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
completionHandler(PacketTunnelProviderError.couldNotDetermineFileDescriptor)
case .dnsResolution(let dnsErrors):
let hostnamesWithDnsResolutionFailure = dnsErrors.map { $0.address }
.joined(separator: ", ")
wg_log(.error, message: "DNS resolution failed for the following hostnames: \(hostnamesWithDnsResolutionFailure)")
errorNotifier.notify(PacketTunnelProviderError.dnsResolutionFailure)
completionHandler(PacketTunnelProviderError.dnsResolutionFailure)
case .setNetworkSettings(let error):
wg_log(.error, message: "Starting tunnel failed with setTunnelNetworkSettings returning \(error.localizedDescription)")
errorNotifier.notify(PacketTunnelProviderError.couldNotSetNetworkSettings)
completionHandler(PacketTunnelProviderError.couldNotSetNetworkSettings)
case .startWireGuardBackend(let errorCode):
wg_log(.error, message: "Starting tunnel failed with wgTurnOn returning \(errorCode)")
errorNotifier.notify(PacketTunnelProviderError.couldNotStartBackend)
completionHandler(PacketTunnelProviderError.couldNotStartBackend)
case .invalidState:
// Must never happen
fatalError()
}
}
}
修改后方法
{
guard let tp = tunnelProvider else {
return
}
// BEGIN: TunnelKit
guard let tunnelProviderProtocol = tp.protocolConfiguration as? NETunnelProviderProtocol else {
fatalError("Not a NETunnelProviderProtocol")
}
guard let providerConfiguration = tunnelProviderProtocol.providerConfiguration else {
fatalError("Missing providerConfiguration")
}
var tunnelConfiguration: WireGuardTunnelConfiguration
do {
cfg = try vpn_fromDictionary(MGWireGuard.ProviderConfiguration.self, providerConfiguration)
// mg_LogExtension("开始进行wireguard连接:\(providerConfiguration)")
var addressString: String?
if let saddr = cfg.serverAddress, let uuidStr = cfg.userUUidStr {
addressString = adapter.getWgSubnetIp(serverIp: saddr, clientPublicKey: cfg.configuration.publicKey, uuidStr: uuidStr)
}
// try await Task.sleep(nanoseconds: 1600 * NSEC_PER_MSEC)
tunnelConfiguration = cfg.configuration.tunnelConfiguration
guard let address = IPAddressRange(from: addressString ?? "") else {
// mg_LogExtension("wireguard 4无效地址: \(String(describing: addressString))")
completionHandler(TunnelKitWireGuardError.interfaceHasInvalidAddress)
return
}
mg_LogExtension("wireguard接收到的服务器ip: \(String(describing: cfg.serverAddress)), uuid=\(String(describing: cfg.userUUidStr)), address=\(address), publicKey=\(cfg.configuration.publicKey)")
tunnelConfiguration.interface.addresses = [address]
} catch {
completionHandler(TunnelKitWireGuardError.savedProtocolConfigurationIsInvalid)
return
}
// END: TunnelKit
//Connect callback
adapter.adapterCallback = {[weak self] status, msg in
mg_LogExtension("得到回调:\(String(describing: status))-\(String(describing: msg))")
if status == 1 { //握手成功,连接成功
completionHandler(nil)
}else if status == 2 { //握手失败,连接失败
completionHandler(TunnelKitWireGuardError.handshakeError)
} else{
// mg_LogExtension("关闭当前wireguard连接")
self?.tunnelProvider?.cancelTunnelWithError(TunnelKitWireGuardError.peerCloseError)
}
}
// Start the tunnel
adapter.start(tunnelConfiguration: tunnelConfiguration, scramble: cfg.scramble) { [weak self] adapterError in
guard let self else {
completionHandler(nil)
return
}
guard let adapterError = adapterError else {
let interfaceName = self.adapter.interfaceName ?? "unknown"
mg_LogExtension("wireguard Tunnel interface is \(interfaceName)")
self.tunnelQueue.async {
self.tunnelIsStarted = true
self.refreshDataCount()
}
return
}
switch adapterError {
case .cannotLocateTunnelFileDescriptor:
mg_LogExtension("wireguard Starting tunnel failed: could not determine file descriptor")
self.cfg._appexSetLastError(.couldNotDetermineFileDescriptor)
completionHandler(TunnelKitWireGuardError.couldNotDetermineFileDescriptor)
case .dnsResolution(let dnsErrors):
let hostnamesWithDnsResolutionFailure = dnsErrors.map(\.address)
.joined(separator: ", ")
mg_LogExtension("wireguard DNS resolution failed for the following hostnames: \(hostnamesWithDnsResolutionFailure)")
self.cfg._appexSetLastError(.dnsResolutionFailure)
completionHandler(TunnelKitWireGuardError.dnsResolutionFailure)
case .setNetworkSettings(let error):
mg_LogExtension("wireguard Starting tunnel failed with setTunnelNetworkSettings returning \(error.localizedDescription)")
self.cfg._appexSetLastError(.couldNotSetNetworkSettings)
completionHandler(TunnelKitWireGuardError.couldNotSetNetworkSettings)
case .startWireGuardBackend(let errorCode):
mg_LogExtension("wireguard Starting tunnel failed with wgTurnOn returning \(errorCode)")
self.cfg._appexSetLastError(.couldNotStartBackend)
completionHandler(TunnelKitWireGuardError.couldNotStartBackend)
case .invalidState:
mg_LogExtension("wireguard Starting tunnel failed with wgTurnOn returning: invalidState")
// Must never happen
fatalError()
}
}
}
上架一段时间后,发现wireguard的连接成功率大概是60%左右,实际应该达到80%以上。并且发现在连接过程中WiFi被关闭,连接成功后才能再次恢复WiFi
经过几天排查后,发现问题出现在
调用此方法会造成WiFi关闭