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

 

 

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

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

内容概要:本文系统研究了电力系统短期负荷预测问题,提出并实现了基于极限学习机(ELM)及其智能优化改进模型的预测方法。研究涵盖标准ELM、白鲸优化算法(BWO)优化ELM和鹭鹰优化算法(IBOA)优化ELM三种模型,重点通过智能优化算法对ELM的输入权重与偏置参数进行全局寻优,有效克服了传统ELM因参数随机初始化导致的不稳定性和泛化能力不足的问题。文章完整呈现了从数据预处理、特征选择、模型构建、参数优化到预测结果对比分析的全流程,利用Matlab编程实现各模型的仿真验证,显著提升了预测精度与模型鲁棒性,为电力系统调度决策提供了可靠的技术支撑。; 适合人群:具备电力系统基础知识、时间序列预测理论及Matlab编程能力的高校研究生、科研机构研究人员以及电力公司从事负荷预测、电网调度与规划工作的技术人员。; 使用场景及目标:①应用于实际电力系统短期负荷预测业务,提升电网运行调度的精细化与智能化水平;②作为智能优化算法与神经网络融合的经典案例,服务于学术论文撰写、科研项目申报及算法性能对比研究;③应对新能源大规模接入背景下负荷波动加剧的挑战,为构建高精度、强鲁棒性的现代负荷预测体系提供解决方案。; 阅读建议:建议读者结合所提供的Matlab代码进行动手实践,深入理解ELM网络结构与优化算法的集成机制,重点对比分析不同优化策略在收敛速度、预测误差(如MAE、RMSE、MAPE)等方面的性能差异,进而掌握智能优化技术在提升预测模型性能方面的关键作用。

1,423

社区成员

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

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