PacketTunnelProvider write ip packet to tun by packetFlow.writePackets not work

extension PacketTunnelProvider: Tun2socksPacketFlowProtocol{

        

    func proxyPackets() {

         self.packetFlow.readPackets {[weak self] (packets: [Data], protocols: [NSNumber]) in

             for packet in  packets {

                 autoreleasepool{

                     Tun2socksInputPacket(packet)

                 }

             }             

             self?.proxyPackets()

         }

     }

    

   

    public func writePacket(_ packet: Data?) {

         NSLog("write packet 1")

        self.packetFlow.writePackets([packet!], withProtocols: [AF_INET as NSNumber])

    }

}



extension NEPacketTunnelFlow: Tun2socksPacketFlowProtocol{

    public func writePacket(_ packet: Data?) {

        NSLog(self.description)

        //NSLog("write packet 2")

        os_log("write packet called !!!")

        let protocolNumber = protocolNumber(for: packet!)

        NSLog(protocolNumber.stringValue)

        let b = packet?.base64EncodedString()

        NSLog(b!)

        let v = self.writePackets([packet!], withProtocols: [protocolNumber])

        

        NSLog(v.description)

    }

    

    private func protocolNumber(for packet: Data) -> NSNumber {

        guard !packet.isEmpty else {

            return AF_INET as NSNumber

        }



        // 'packet' contains the decrypted incoming IP packet data

        // The first 4 bits identify the IP version

        let ipVersion = (packet[0] & 0xf0) >> 4

        return (ipVersion == 6) ? AF_INET6 as NSNumber : AF_INET as NSNumber

    }

}

What's causing this problem when calling writepackets returns true but the browser request keeps loading

iOS version 16.1.2 (20B110)

but the browser request keeps loading

I don’t have any easy answers for you here. My general advice is that you debug packet tunnel providers with a simple test app rather than using a web browser. So, create a small test app that uses BSD Sockets to open a TCP connection to an IP address, and then see you can get that working. Either way, that tells you something:

  • If things fail with your test app, you have a much simpler debugging problem.

  • If your test app works but the web browser fails, tweak your test app to use a higher-level API, like NSURLSession, and see if you can get that working.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you very much for your reply, I must give force to the inspiration, I have a problem, now my socks5 proxy server to monitor local (127.0.0.1) port, the data is encoded is forwarded to the remote server, is under this kind of circumstance how to configure NEPacketTunnelNetworkSettings, Could you provide an example of the configuration.


 func proxyPackets() {

        NSLog("proxyPackets")

         self.packetFlow.readPackets {[weak self] (packets: [Data], protocols: [NSNumber]) in

             NSLog("readPackets")

             var i = 0

             for packet in  packets {

                 

                 NSLog("读取:"+packet.debugDescription + " protocols:  " +  String(describing: protocols[i]))

                 i=i+1

                 //packet.

                 autoreleasepool{

                     Tun2socksInputPacket(packet)

                 }

             }             

             self?.proxyPackets()

         }

     }



When requesting an http address, the packet read from packetflow does not appear to be tcp or udp. The packet is as follows:

[debug]:Read :[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

[debug]:Read :[0 0 0 0 0 0 0 80 0 0 0 0 0 0 0 80 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

[debug]:Read :[184 122 166 231 1 0 0 0 3 0 0 0 0 0 0 0 32 0 0 0 0 0 0 0 12 0 0 0 0 0 0 240 112 114 111 120 121 80 97 99 107 101 116 115 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

[debug]:Read :[0 0 0 0 0 0 0 80 0 0 0 0 0 0 0 80 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

[debug]:Read :[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

[debug]:Read :[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

My doubt is that the http request should read the tcp packet, but it did not read, may I ask why this is

PacketTunnelProvider write ip packet to tun by packetFlow.writePackets not work
 
 
Q