【问题标题】:Create VPN connection创建 VPN 连接
【发布时间】: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 权限。

感谢您的帮助!

【问题讨论】:

    标签: swift macos cocoa vpn


    【解决方案1】:

    我终于想出了如何创建 VPN 连接/服务。但我仍然必须弄清楚应该如何使用选项和首选项。

    SCPreferencesCreate 之前已被 SCPreferencesCreateWithAuthorization 和一些授权代码替换。这是更新后的代码。

        func createVPNConnection() {
            let flags : AuthorizationFlags = [.interactionAllowed, .extendRights, .preAuthorize]
            var authRef: AuthorizationRef?
            AuthorizationCreate(nil, nil, flags, &authRef)
    
            let options = ["" : ""]
            let prefs = SCPreferencesCreateWithAuthorization(nil, "TheVPN" as CFString, nil, authRef)
            // 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!, ("Test 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!)
    
            // Let's apply all configuration to the PPP interface
            // Specifically, the servername, account username and password
            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()))")
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 2011-12-16
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 2018-07-11
      • 2021-02-09
      • 2010-09-24
      • 2020-06-18
      相关资源
      最近更新 更多