【发布时间】:2019-03-31 02:56:56
【问题描述】:
我对 macos 开发和 swift 完全陌生,所以请多多包涵...
我正在尝试在 Cocoa 应用中创建 VPN 连接。我的代码基于 macosvpn:https://github.com/halo/macosvpn/blob/master/macosvpn/Classes/VPNServiceCreator.swift
这是我目前所拥有的:
func createVPNService() {
let options = ["" : ""]
let prefs = SCPreferencesCreate(nil, "TheVPN" as CFString, nil)
// These variables will hold references to our new interfaces
let initialTopInterface: SCNetworkInterface!
let initialBottomInterface: SCNetworkInterface!
// L2TP on top of IPv4
initialBottomInterface = SCNetworkInterfaceCreateWithInterface(kSCNetworkInterfaceIPv4, kSCNetworkInterfaceTypeL2TP)
// PPP on top of L2TP
initialTopInterface = SCNetworkInterfaceCreateWithInterface(initialBottomInterface!, kSCNetworkInterfaceTypePPP)
let service = SCNetworkServiceCreate(prefs!, initialTopInterface!)
SCNetworkServiceSetName(service!, ("Service Name" as CFString))
// Because, if we would like to modify the interface, we first need to freshly fetch it from the service
// See https://lists.apple.com/archives/macnetworkprog/2013/Apr/msg00016.html
let topInterface = SCNetworkServiceGetInterface(service!)
SCNetworkInterfaceSetConfiguration(topInterface!, options as CFDictionary)
// Now let's apply the shared secret to the IPSec part of the L2TP/IPSec Interface
let thingy:CFString = "IPSec" as CFString
SCNetworkInterfaceSetExtendedConfiguration(topInterface!, thingy, options as CFDictionary)
SCNetworkServiceEstablishDefaultConfiguration(service!)
let networkSet = SCNetworkSetCopyCurrent(prefs!)
let serviceProtocol = SCNetworkServiceCopyProtocol(service!, kSCNetworkProtocolTypeIPv4)
SCNetworkProtocolSetConfiguration(serviceProtocol!, options as CFDictionary)
SCNetworkSetAddService(networkSet!, service!)
if !SCPreferencesCommitChanges(prefs!) {
print("Error: Could not commit preferences. (Code \(SCError()))")
}
if !SCPreferencesApplyChanges(prefs!) {
print("Error: Could not apply changes. (Code \(SCError()))")
}
}
我运行这个时没有创建网络服务。
我不知道真正应该使用哪些选项和首选项?它们现在或多或少是空的。
此外,SCPreferencesCommitChanges 和 SCPreferencesApplyChanges 失败并显示 1003 代码。我想他们需要 root 权限才能工作,而我一直无法弄清楚如何获得 root 权限。
感谢您的帮助!
【问题讨论】: