想用NetworkExtension在iOS中实现配置hosts的功能

QBIT 2024-01-29 12:59:54

代码如下

APP_ID权限等设置已经打开了,在ios系统中也新建了vpn连接,但无法连接,请教大佬,有相关经验的,可否提供类似代码?

import UIKit
import NetworkExtension

@objcMembers
class ViewController: UIViewController {
    @IBOutlet var connectButton: UIButton!
    let vpnManager = NETunnelProviderManager.shared()
    private func initVPNTunnelProviderManager() {
        let tunnelProtocol = NETunnelProviderProtocol()
        tunnelProtocol.providerBundleIdentifier = "com.xx.xx"

        // 设置你的 hosts 或其他协议参数
        tunnelProtocol.serverAddress = "223.1.1.1"
        tunnelProtocol.username = "test"
        if let passwordData = "test".data(using: .utf8) {
            tunnelProtocol.passwordReference = passwordData
        } else {
            // Handle the case where conversion from string to data fails
            print("Failed to convert password string to data")
        }

        tunnelProtocol.providerConfiguration = [
            "protocol": [
                "L2TP": [
                    "serverAddress":"223.1.1.1",
                    "username": "test",
                    "passwordReference": "test",  // Data containing the password
                    "sharedSecretReference": "test",  // Data containing the shared secret
                ]
            ]
        ]


        vpnManager.protocolConfiguration = tunnelProtocol
        vpnManager.saveToPreferences { error in
            if let error = error {
                print("Error saving VPN configuration: \(error.localizedDescription)")
            } else {
                // Configuration saved successfully
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initVPNTunnelProviderManager()
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.VPNStatusDidChange(_:)), name: NSNotification.Name.NEVPNStatusDidChange, object: nil)

        // Do any additional setup after loading the view.
    }

    func VPNStatusDidChange(_ notification: Notification?) {
        print("VPN Status changed:")
        let status = self.vpnManager.connection.status
        switch status {
        case .connecting:
            print("Connecting...")
            connectButton.setTitle("Disconnect", for: .normal)
            break
        case .connected:
            print("Connected...")
            connectButton.setTitle("Disconnect", for: .normal)
            break
        case .disconnecting:
            print("Disconnecting...")
            break
        case .disconnected:
            print("Disconnected...")
            connectButton.setTitle("Connect", for: .normal)
            break
        case .invalid:
            print("Invliad")
            break
        case .reasserting:
            print("Reasserting...")
            break
        default:
            print("unknow Vpn Status...", status)
            break
        }
    }
    
    @IBAction func go(_ sender: UIButton, forEvent event: UIEvent) {
        print("Go!")

        self.vpnManager.loadFromPreferences { (error:Error?) in
            if let error = error {
                print(error)
            }
            if (sender.title(for: .normal) == "Connect") {
                do {
                    try self.vpnManager.connection.startVPNTunnel()
                } catch {
                    print(error)
                }
            } else {
                self.vpnManager.connection.stopVPNTunnel()
            }
        }
    }
}

 

 

...全文
7287 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
QBIT 2024-01-29
  • 打赏
  • 举报
回复

APP Stream IOS抓包其中一项功能就是配置hosts,我们想实现类似功能。Stream IOS抓包

1,424

社区成员

发帖
与我相关
我的任务
社区描述
Swift,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C共同运行于MAC OS和iOS平台,用于搭建基于苹果平台的应用程序。
swift 技术论坛(原bbs)
社区管理员
  • Swift
  • 网罗开发
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧