3

我想在没有指定代理的情况下使用NEPacketTunnelProvider检查数据包。不幸的是,我的readPacketObjects完成处理程序从未被调用,我不明白为什么。我的常规互联网连接停止,Wireshark 在新界面上显然没有显示任何内容,而且我在 en0 上也看不到任何 HTTP 连接。

这可能吗?Apple Developer Forums建议它可能不是……但 Charles Proxy for iOS 似乎能够检查所有内容。

我的代码如下:

class TunnelProvider: NEPacketTunnelProvider {
override func startTunnel(options: [String : NSObject]? = nil, completionHandler: @escaping (Error?) -> Void) {
    os_log("starting tunnel")

    let settings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "127.0.0.1")

    settings.tunnelOverheadBytes = 80
    settings.mtu = 1200

    settings.ipv4Settings = NEIPv4Settings(addresses: ["127.0.0.1", "0.0.0.0"], subnetMasks: ["0.0.0.0", "0.0.0.0"]) // all addresses
    settings.ipv4Settings?.includedRoutes = [NEIPv4Route.default()] // all routes
    settings.ipv4Settings?.excludedRoutes = [NEIPv4Route(destinationAddress: "127.0.0.1", subnetMask: "255.255.255.255")] // avoid local routes

    settings.dnsSettings = NEDNSSettings(servers: ["1.1.1.1", "8.8.8.8", "8.8.4.4"])
    settings.dnsSettings?.matchDomains = [""] // if blank don't use this DNS, use system; if "" then use this

    NSLog("default/included route %@:%@", NEIPv4Route.default().destinationAddress, NEIPv4Route.default().destinationSubnetMask)

    self.setTunnelNetworkSettings(settings) { error in
        if let e = error {
            NSLog("Settings error %@", e.localizedDescription)
            completionHandler(e)
        } else {
            os_log("Settings set without error")

            completionHandler(nil)
        }
    }

    self.readPacketObjects()
}

private func readPacketObjects() {
    NSLog("Inside readPacketObjects %@", self.packetFlow)
    self.packetFlow.readPacketObjects() { packets in
        NSLog("Inside readPacketObjects completionHandler %@", packets)

        self.packetFlow.writePacketObjects(packets)

        self.readPacketObjects()
    }
}

控制台输出

default 10:31:03.177424 -0400   tunnel  starting tunnel
default 10:31:03.177796 -0400   tunnel  default/included route 0.0.0.0:0.0.0.0
default 10:31:03.178602 -0400   tunnel  Inside readPacketObjects <NEPacketTunnelFlow: 0x7f9a69d135e0>
default 10:31:03.356006 -0400   tunnel  Settings set without error

编辑 1在修改为具有更具体子网掩码的本地地址
后,我能够读取数据包。settings.ipv4Settings但是,互联网连接仍然断开并route get输出错误的地址。

编辑 2
我可以通过将 DNS 服务器添加到 excludeRoutes 来让 DNS 工作,如此处所示。不加修改地写入 packetFlow 仍然无法启用完整路由。

4

0 回答 0