Lax / Learn-iOS-Swift-by-Examples

精心收集并分类整理的Swift开发学习资源,包括Apple官方提供的示例代码和文档,以及github上的项目和国内外开发者的技术博客。欢迎提交pull-request一起维护。https://t.me/SwiftCN QQ交流群 32958950 申请请注明开发经验

Home Page:https://lax.github.com/SwiftBeginnersGuide/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple VPN

ServusJon opened this issue · comments

Hi! I found your VPN example and I tried to simplify it:

import UIKit
import NetworkExtension

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let manager = NEVPNManager.shared()
        
        
        manager.loadFromPreferences { error in
            let vpnhost = "test"
            
            let p = NEVPNProtocolIKEv2()
            p.username = "username"
            p.localIdentifier = "username"
            p.serverAddress = ""
            p.remoteIdentifier = vpnhost
            p.authenticationMethod = .none
            p.useExtendedAuthentication = true
            p.serverCertificateIssuerCommonName = vpnhost
            p.disconnectOnSleep = false

            
            let onDemandRule = NEOnDemandRuleEvaluateConnection()
            onDemandRule.interfaceTypeMatch = .any
            let evaluateRule = NEEvaluateConnectionRule(matchDomains: ["*.google.de", "*.google.com"], andAction: .connectIfNeeded)
//            evaluateRule.useDNSServers = [""]
            evaluateRule.probeURL = URL(string: "about:blank")
            onDemandRule.connectionRules = [evaluateRule]

            
            manager.localizedDescription = "My VPN"
            manager.protocolConfiguration = p
            manager.onDemandRules = [onDemandRule]
            manager.isOnDemandEnabled = true
            manager.isEnabled = true
            manager.saveToPreferences { error in
                guard error == nil else {
                    print("NEVPNManager.saveToPreferencesWithCompletionHandler failed: \(error!.localizedDescription)")
                    return
                }
                
                try! manager.connection.startVPNTunnel()
            }
        }
    }
}

I want to redirect when google.de and google.com are loaded. I also don't want to use an actual vpn server. I don't get any errors currently and the sites are loading :-(

Any ideas?