如何解决无法使用 SharedSecret 建立 VPN 连接
我正在尝试使用 Swift
连接到 vpn。我创建了类 VpnHandler
并且我正在使用钥匙串 Swift
来保持钥匙串引用。
我的代码如下所示:
import Foundation
import NetworkExtension
import KeychainSwift
final class VPNHandler {
let vpnManager = NEVPNManager.shared()
func initVPNTunnelProviderManager(serverAdress: String,remoteIdentifier : String,sharedSecred:String) {
let sharedKey = sharedSecred.data(using: .utf8)
let keychain = KeychainSwift()
guard let sharedKey = sharedKey else { return }
keychain.set(sharedKey,forKey: "shared_secret")
vpnManager.loadFromPreferences { error in
if let error = error {
print(error.localizedDescription)
return
}
let IKEv2Protocol = NEVPNProtocolIKEv2()
IKEv2Protocol.username = nil
IKEv2Protocol.localIdentifier = nil
IKEv2Protocol.serverAddress = serverAdress
IKEv2Protocol.remoteIdentifier = remoteIdentifier
IKEv2Protocol.authenticationMethod = .sharedSecret
IKEv2Protocol.disconnectOnSleep = false
IKEv2Protocol.useExtendedAuthentication = false
IKEv2Protocol.sharedSecretReference = keychain.getData("shared_secret",asReference: true)
IKEv2Protocol.passwordReference = nil
var rules = [NEondemandRule]()
let rule = NEondemandRuleConnect()
rule.interfaceTypeMatch = .any
rules.append(rule)
self.vpnManager.localizedDescription = "My VPN"
self.vpnManager.protocolConfiguration = IKEv2Protocol
self.vpnManager.ondemandRules = rules
self.vpnManager.isondemandEnabled = true
self.vpnManager.isEnabled = true
print("SAVE TO PREFERENCES...")
self.vpnManager.savetoPreferences { error in
if (error != nil) {
print(error!)
return
}
print("CALL LOAD TO PREFERENCES AGAIN...")
self.vpnManager.loadFromPreferences { error in
if let error = error {
print(error.localizedDescription)
return
}
do {
try self.vpnManager.connection.startVPNTunnel()
print("Starting VPN...")
} catch let error {
print("can't connect VPN'")
print(error.localizedDescription)
}
}
}
}
}
}
当我调用函数 initVPNTunnelProviderManager
时,会创建电话设置中的 vpn 配置。我们的应用程序开始连接到 vpn,但随后立即断开连接。当我们在电话设置中连接 vpn 配置时,它正在工作。
我不知道是什么问题。
感谢任何帮助。
提前致谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。