heiher / hev-socks5-tunnel

A high-performance tun2socks for Linux/Android/FreeBSD/macOS/iOS/WSL2 (IPv4/IPv6/TCP/UDP)
MIT License
850 stars 158 forks source link

Hi, is there something wrong with my usage? #166

Closed dhp5 closed 1 week ago

dhp5 commented 2 weeks ago

Unable to forward traffic to 127.0.0.1:10808

    let socks5Config = """
tunnel:
  mtu: 1500

socks5:
  port: 10808
  address: 127.0.0.1
  udp: 'true'

misc:
  task-stack-size: 20480
  connect-timeout: 5000
  read-write-timeout: 60000
  log-file: stderr
  log-level: info
  limit-nofile: 65535
"""

import NetworkExtension import os.log import Tun2SocksKit class PacketTunnelProvider: NEPacketTunnelProvider {

override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
    os_log("startTunnel--->")
        // 启动Tun2Socks

    let socks5Config = """
tunnel:
  mtu: 1500

socks5:
  port: 10808
  address: 127.0.0.1
  udp: 'true'

misc:
  task-stack-size: 20480
  connect-timeout: 5000
  read-write-timeout: 60000
  log-file: stderr
  log-level: info
  limit-nofile: 65535
"""

    self.setTunnelNetworkSettings(getNetworkSettings(with: 1500)) { [socks5Config] error in
        if let error = error {
            completionHandler(error)
            return
        }

        Tun2SocksKit.Socks5Tunnel.run(withConfig: .string(content: socks5Config)) { code in
            if code == 0 {
                print("Tun2Socks started successfully")
            } else {
                print("Tun2Socks failed to start, error code: \(code)")
            }
        }

        completionHandler(nil)
    }
}

override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
        // Add code here to start the process of stopping the tunnel.
    os_log("stopTunnel--->")
    completionHandler()
}

override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
    os_log("handleAppMessage--->")
        // Add code here to handle the message.
    if let handler = completionHandler {
        handler(messageData)
    }
}

func getNetworkSettings(with mtu: Int) -> NEPacketTunnelNetworkSettings {
    let tunSet = NEPacketTunnelNetworkSettings.init(tunnelRemoteAddress: "240.0.0.1")
    tunSet.mtu = 1500
    tunSet.ipv4Settings = NEIPv4Settings.init(addresses: ["198.18.0.1"], subnetMasks: ["255.255.255.0"])
    tunSet.ipv4Settings?.includedRoutes = [NEIPv4Route.default()]
    tunSet.dnsSettings = NEDNSSettings(servers: ["8.8.8.8"])
    return tunSet
}

}

This is Log: [2024-09-25 19:02:52] [E] 0x10819cb70 socks5 session handshake [2024-09-25 19:02:52] [E] 0x10819c8d0 socks5 client res.rep 7 [2024-09-25 19:02:52] [E] 0x10819c8d0 socks5 session handshake [2024-09-25 19:02:52] [I] 0x10819c8d0 socks5 client udp construct [2024-09-25 19:02:52] [I] 0x10819cb70 socks5 client udp construct [2024-09-25 19:02:52] [E] 0x10819c8d0 socks5 client res.rep 7

What should I do?

heiher commented 2 weeks ago

It seems that your socks5 server doesn't support UDP association. I suggest debugging your app using hev-socks5-server.

dhp5 commented 2 weeks ago

Is there a demo for iOS?Could you please provide a sample code for iOS to call NetworkExtension to capture Tun traffic and transfer it to socks5://127.0.0.1:10808 through hev-socks5-server? Thank you very much for your patient guidance!

heiher commented 2 weeks ago

At the moment, there are no demo for iOS.