想用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()
            }
        }
    }
}

 

 

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

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

【风电功率预测】【多变量输入单步预测】基于BiTCN-LSTM的风电功率预测研究(Matlab代码实现)内容概要:本文研究了基于BiTCN-LSTM的风电功率预测模型,旨在通过多变量输入实现单步预测。该模型结合了双向时间卷积网络(BiTCN)和长短期记忆网络(LSTM),充分利用时间序列数据的前后依赖关系,提升了风电功率预测的精度与稳定性。文章详细介绍了模型的设计原理、结构组成及其实验验证过程,并在Matlab平台上实现了代码开发与仿真测试,展示了其相较于传统方法在处理非线性、波动性强的风电数据方面的优越性能。; 适合人群:具备一定机器学习基础和Matlab编程经验,从事新能源预测、电力系统分析或相关领域研究的研发人员及高校研究生。; 使用场景及目标:①应用于风电场日常运营的功率预测,提高电网调度的可靠性和经济性;②为研究人员提供一种有效的深度学习融合模型设计思路,用于解决复杂时间序列预测问题;③通过Matlab代码实现,便于快速复现实验并进行算法优化。; 阅读建议:建议读者结合文提供的Matlab代码,深入理解BiTCN与LSTM的网络架构及其协同工作机制,重点关注数据预处理、模型训练与评估流程,以便更好地掌握该方法在实际工程的应用技巧。

1,423

社区成员

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

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