heiher/hev-socks5-tunnel

Compile static library for iOS

Closed this issue · 21 comments

Hey!

So, I'm kinda new to this. How do I compile this into an xcframework for iphoneos with a specific min ios version (arm64 only)?

@daemooon

@daemooon
Thank you

I've encountered another problem now, would you please be so kind as to help me with it?

let (fd, name) = self.tunnelFileDescriptor!
let config = """
socks5:
  port: 1080
  address: 192.168.0.3
  udp: 'udp'
misc:
  log-level: error
  log-file: \(Logger.vpnLogFile!.path)
"""
try! config.write(to: FileManager.default.tun2socksConf!, atomically: true, encoding: .utf8)
let cfg = strdup(FileManager.default.tun2socksConf!.path)
hev_socks5_tunnel_main(cfg, fd)
free(cfg)

This is inside my PacketTunnelProvider

after setTunnelNetworkSettings I execute this code, but nothing gets proxied (I get direct connection)

Why would you think that is?

If you have a PacketTunnelProvider class that works, is it possible to share it here? I'm trying to connect to local xray eventually, but first I need to pass packets through socks5.

Thank you again

arror commented

@daemooon Thank you

I've encountered another problem now, would you please be so kind as to help me with it?

let (fd, name) = self.tunnelFileDescriptor!
let config = """
socks5:
  port: 1080
  address: 192.168.0.3
  udp: 'udp'
misc:
  log-level: error
  log-file: \(Logger.vpnLogFile!.path)
"""
try! config.write(to: FileManager.default.tun2socksConf!, atomically: true, encoding: .utf8)
let cfg = strdup(FileManager.default.tun2socksConf!.path)
hev_socks5_tunnel_main(cfg, fd)
free(cfg)

This is inside my PacketTunnelProvider

after setTunnelNetworkSettings I execute this code, but nothing gets proxied (I get direct connection)

Why would you think that is?

If you have a PacketTunnelProvider class that works, is it possible to share it here? I'm trying to connect to local xray eventually, but first I need to pass packets through socks5.

Thank you again

The tunnel in the configuration does not seem to be ignored. Provide the log please.

public static func start(at port: Int) throws {
    let config = """
    tunnel:
      mtu: 9000
    
    socks5:
      port: \(port)
      address: ::1
      udp: 'udp'

    misc:
      task-stack-size: 20480
      connect-timeout: 5000
      read-write-timeout: 60000
      log-file: stderr
      log-level: debug
      limit-nofile: 65535
    """
    let cache = URL(filePath: NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0], directoryHint: .isDirectory)
    let file = cache.appending(component: "\(UUID().uuidString).yml", directoryHint: .notDirectory)
    try config.write(to: file, atomically: true, encoding: .utf8)
    DispatchQueue.global(qos: .userInitiated).async {
        guard let fd = self.tunnelFileDescriptor else {
            fatalError()
        }
        NSLog("HEV_SOCKS5_TUNNEL_MAIN: \(hev_socks5_tunnel_main(file.path(percentEncoded: false), fd))")
    }
}

XRay Config:

"inbounds":[{"protocol":"socks","settings":{"udp":true,"auth":"noauth"},"tag":"socks-in","port":10864}]

Tun2Socks+Xray:

func setupTunnel(config: Data, completionHandler: @escaping (Error?) -> Void) {
    xrayQueue.async {
        var error: NSError?
        Logger.log("Initializing", to: Logger.vpnLogFile)
        XRayStart(config, self, &error)
        if let error = error {
            Logger.log("ERROR", to: Logger.vpnLogFile)
            Logger.log((error as? Universeerror)?.error() ?? "-", to: Logger.vpnLogFile)
            completionHandler(error)
            return
        }
    }
    let (fd, _) = self.tunnelFileDescriptor!
    let config = """
    tunnel:
      mtu: 1500
      ipv4:
        address: 198.18.0.20
        gateway: 198.18.0.2
        prefix: 24
    socks5:
      port: 10864
      address: ::1
      udp: 'udp'
    misc:
      log-level: debug
      log-file: \(Logger.vpnLogFile!.path)
      task-stack-size: 20480
      connect-timeout: 5000
      read-write-timeout: 60000
      limit-nofile: 65535
    """
    Logger.log(FileManager.default.tun2socksConf!.path, to: Logger.vpnLogFile)
    try! config.write(to: FileManager.default.tun2socksConf!, atomically: true, encoding: .utf8)
    Logger.log(config, to: Logger.vpnLogFile)
    Logger.log("FD: \(fd)", to: Logger.vpnLogFile)
    xrayQueue.async {
        hev_socks5_tunnel_main(FileManager.default.tun2socksConf!.path, fd)
    }
    completionHandler(nil)
}

Tunnel settings

let settings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "198.18.0.2")
settings.mtu = 1500
settings.ipv4Settings = {
    let settings = NEIPv4Settings(addresses: ["198.18.0.20"], subnetMasks: ["255.255.255.0"])
    settings.includedRoutes = [NEIPv4Route.default()]
    return settings
}()
settings.ipv6Settings = {
    let settings = NEIPv6Settings(addresses: ["fd6e:a81b:704f:1211::1"], networkPrefixLengths: [64])
    settings.includedRoutes = [NEIPv6Route.default()]
    return settings
}()
let dns = NEDNSSettings(servers: ["8.8.8.8"])
settings.dnsSettings = dns

Logs

[2023-03-02 09:08:59] [D] socks5 tunnel init
[2023-03-02 09:08:59] [D] socks5 tunnel run
[2023-03-02 09:08:59] [D] socks5 tunnel lwip task run
[2023-03-02 09:08:59] [D] socks5 tunnel timer task run
[2023-03-02 09:08:59] [D] socks5 tunnel event task run
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 construct
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 client construct
[2023-03-02 09:08:59] [I] 0x123d0b2d0 socks5 client udp construct
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 session udp construct
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 session udp new
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 session run
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 client connect [::1]:10864
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 client connect server
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 client connect server fd 18
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 client handshake
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 client write request
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 client read response
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 session udp splice
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:08:59] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:00] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:00] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:00] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:09:00] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:09:00] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:00] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:02] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:09:02] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:09:02] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:02] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:06] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:09:06] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:06] [D] 0x123d0b2d0 socks5 udp recvfrom udp
[2023-03-02 09:09:06] [D] 0x123d0b2d0 socks5 udp sendto
[2023-03-02 09:10:06] [I] 0x123d0b2d0 io timeout
[2023-03-02 09:10:06] [D] 0x123d0b2d0 socks5 udp read
[2023-03-02 09:10:06] [D] 0x123d0b2d0 socks5 session udp fwd b recv
[2023-03-02 09:10:06] [I] 0x123d0b2d0 io timeout
[2023-03-02 09:10:06] [D] 0x123d0b2d0 socks5 session udp destruct
[2023-03-02 09:10:06] [D] 0x123d0b2d0 socks5 client udp destruct
[2023-03-02 09:10:06] [D] 0x123d0b2d0 socks5 client destruct
[2023-03-02 09:10:06] [D] 0x123d0b2d0 socks5 destruct

I can use my phone's xray instance from my PC

heiher commented

what about the logs of the socks5 server? and trying TCP without DNS.

@heiher

I don't get anything in logs, current version doesn't even work with TCP (I previously changed a version of your code and ran with the same config and the issue was with the DNS, now it seems it doesn't even reach the server)

My logs when I try to connect to an XRay in my LAN:

[2023-03-02 16:22:44] [D] socks5 tunnel init
[2023-03-02 16:22:44] [D] socks5 tunnel run
[2023-03-02 16:22:44] [D] socks5 tunnel lwip task run
[2023-03-02 16:22:44] [D] socks5 tunnel timer task run
[2023-03-02 16:22:44] [D] socks5 tunnel event task run
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 construct
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 client construct
[2023-03-02 16:22:49] [I] 0x107808b90 socks5 client udp construct
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 session udp construct
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 session udp new
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 session run
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 client connect [192.168.0.3]:1080
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 client connect server
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 client connect server fd 18
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 client handshake
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 client write request
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 client read response
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 session udp splice
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 udp sendto
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 udp write udp
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 session udp fwd f send
[2023-03-02 16:22:49] [D] 0x107808b90 socks5 udp recvfrom udp
heiher commented

Is the firewall blocking UDP packets between hev-socks5-tunnel and xray?

show more details:

diff --git a/src/hev-socks5-udp.c b/src/hev-socks5-udp.c
index 3f81046..7790762 100644
--- a/src/hev-socks5-udp.c
+++ b/src/hev-socks5-udp.c
@@ -86,7 +86,7 @@ hev_socks5_udp_sendto (HevSocks5UDP *self, const void *buf, size_t len,
     res = hev_task_io_socket_sendmsg (hev_socks5_udp_get_fd (self), &mh,
                                       MSG_WAITALL, task_io_yielder, self);
     if (res <= 0) {
-        LOG_D ("%p socks5 udp write udp", self);
+        LOG_D ("%p socks5 udp write udp %s", self, strerror (errno));
         return -1;
     }
 

@heiher I was able to fix it now I can connect to my XRay instance

Logs:

[2023-03-02 17:32:58] tcp:192.168.0.6:56771 accepted [tcp:[2a00:1450:4001:830::200e]:443] tcp:clients4.google.com:443 [socks-in -> proxy]
[2023-03-02 17:33:13] tcp:[::1]:55516 accepted [tcp:meet.google.com:443]  [socks-in -> proxy]
[2023-03-02 17:33:13] [Warning] [3608480642] app/dispatcher: taking detour [proxy] for [tcp:meet.google.com:443]
[2023-03-02 17:33:21] [Warning] [651408407] app/dispatcher: taking detour [proxy] for [tcp:[2001:4860:4802:32::78]:443] tcp:www.google.com:443
[2023-03-02 17:33:21] tcp:192.168.0.6:56775 accepted [tcp:[2001:4860:4802:32::78]:443] tcp:www.google.com:443 [socks-in -> proxy]
[2023-03-02 17:33:23] [Warning] [3926481077] app/dispatcher: taking detour [proxy] for [tcp:[2a00:1450:400f:800::200e]:443] tcp:safebrowsing.google.com:443
[2023-03-02 17:33:23] tcp:192.168.0.6:56778 accepted [tcp:[2a00:1450:400f:800::200e]:443] tcp:safebrowsing.google.com:443 [socks-in -> proxy]
[2023-03-02 17:33:39] [Warning] [2131765386] app/dispatcher: taking detour [proxy] for [tcp:[2a00:1450:400f:801::200e]:443] tcp:encrypted-tbn0.gstatic.com:443
[2023-03-02 17:33:39] tcp:192.168.0.6:56784 accepted [tcp:[2a00:1450:400f:801::200e]:443] tcp:encrypted-tbn0.gstatic.com:443 [socks-in -> proxy]
[2023-03-02 17:34:03] [Warning] [2801650158] app/dispatcher: taking detour [proxy] for [tcp:[2a00:1450:400f:80a::200a]:443] tcp:optimizationguide-pa.googleapis.com:443
[2023-03-02 17:34:03] tcp:192.168.0.6:56789 accepted [tcp:[2a00:1450:400f:80a::200a]:443] tcp:optimizationguide-pa.googleapis.com:443 [socks-in -> proxy]
[2023-03-02 17:34:09] [Warning] [3934305063] app/dispatcher: taking detour [proxy] for [tcp:[2001:1670:0:111::8600:d898]:443] tcp:static-global-s-msn-com.akamaized.net:443
[2023-03-02 17:34:09] tcp:192.168.0.6:56792 accepted [tcp:[2001:1670:0:111::8600:d898]:443] tcp:static-global-s-msn-com.akamaized.net:443 [socks-in -> proxy]
[2023-03-02 17:34:12] [Warning] [4210337342] app/dispatcher: taking detour [proxy] for [tcp:[2a02:26f0:480:e::210:f106]:443] tcp:static-global-s-msn-com.akamaized.net:443
[2023-03-02 17:34:12] tcp:192.168.0.6:56794 accepted [tcp:[2a02:26f0:480:e::210:f106]:443] tcp:static-global-s-msn-com.akamaized.net:443 [socks-in -> proxy]
[2023-03-02 17:34:12] [Warning] [2538088050] app/dispatcher: taking detour [proxy] for [tcp:[2a02:26f0:480:e::210:f106]:443] 
[2023-03-02 17:34:12] tcp:192.168.0.6:56796 accepted [tcp:[2a02:26f0:480:e::210:f106]:443]  [socks-in -> proxy]
[2023-03-02 17:34:16] [Warning] [4051668439] app/dispatcher: taking detour [proxy] for [tcp:[2606:2800:233:c444:743f:6277:3872:fbef]:443] tcp:at.atwola.com:443
[2023-03-02 17:34:16] tcp:192.168.0.6:56798 accepted [tcp:[2606:2800:233:c444:743f:6277:3872:fbef]:443] tcp:at.atwola.com:443 [socks-in -> proxy]
[2023-03-02 17:34:19] tcp:192.168.0.6:56803 accepted [tcp:[2a00:1450:4001:827::200a]:443] tcp:content-autofill.googleapis.com:443 [socks-in -> proxy]
[2023-03-02 17:34:19] [Warning] [1710137436] app/dispatcher: taking detour [proxy] for [tcp:[2a00:1450:4001:827::200a]:443] tcp:content-autofill.googleapis.com:443
[2023-03-02 17:34:22] [Warning] [2854878384] app/dispatcher: taking detour [proxy] for [tcp:[2a00:1288:80:807::1]:443] tcp:26.at.atwola.com:443
[2023-03-02 17:34:22] tcp:192.168.0.6:56804 accepted [tcp:[2a00:1288:80:807::1]:443] tcp:26.at.atwola.com:443 [socks-in -> proxy]
[2023-03-02 17:34:22] [Warning] [3777700667] app/dispatcher: taking detour [proxy] for [tcp:[2a00:1288:80:807::2]:443] tcp:26.at.atwola.com:443
[2023-03-02 17:34:22] tcp:192.168.0.6:56805 accepted [tcp:[2a00:1288:80:807::2]:443] tcp:26.at.atwola.com:443 [socks-in -> proxy]

192.168.0.6 is my phone's ip

telegram works and gets tunneled through (blocked in my country), however, when I open "what's my ip", I get my real ip :(

interesting,
I open a what's my ip site, and it does log in XRay

[2023-03-02 17:44:03] tcp:192.168.0.6:57103 accepted [tcp:[2606:6e00:c000:1000::513]:443] tcp:ds6.whatismyipaddress.com:443 [socks-in -> proxy]

however, IT SHOWS MY REAL IP?!!!!!! 😆

UPDATE:

Found something.
It only tunnels my IPv6 requests, somehow, ipv4 requests are passed directly

heiher commented

Emm, check your ipv4 address and route of tun devices.

I just tried to use @daemooon 's Mango (basically copied the packet tunnel's code)

let settings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "254.1.1.1")
settings.mtu = 9000

settings.ipv4Settings = {
    guard self.isIPv4Enable else {
        return nil
    }
    let settings = NEIPv4Settings(addresses: ["198.18.0.1"], subnetMasks: ["255.255.0.0"])
    settings.includedRoutes = [NEIPv4Route.default()]
    return settings
}()
settings.ipv6Settings = {
    guard self.isIPv6Enable else {
        return nil
    }
    let settings = NEIPv6Settings(addresses: ["fd6e:a81b:704f:1211::1"], networkPrefixLengths: [64])
    settings.includedRoutes = [NEIPv6Route.default()]
    return settings
}()
settings.dnsSettings = NEDNSSettings(servers: self.dnsServers)
return settings

this is exatly Tunnel.swift:

public static func start(port: Int) throws {
    let config = """
    tunnel:
      mtu: 9000
      name: \(interfaceName!)
      ipv4:
        address: 198.18.0.1
        gateway: 198.18.0.1
        prefix: 16
    socks5:
      port: 1080
      address: 192.168.0.3
      udp: 'udp'
    misc:
      task-stack-size: 20480
      connect-timeout: 5000
      read-write-timeout: 60000
      log-file: \(Logger.vpnLogFile!.path)
      log-level: debug
      limit-nofile: 65535
    """
    let file = FileManager.default.tun2socksConf!
    try config.write(to: file, atomically: true, encoding: .utf8)
    DispatchQueue.global(qos: .userInitiated).async {
        guard let fd = self.tunnelFileDescriptor else {
            fatalError()
        }
        Logger.log("FD: \(fd)", to: Logger.vpnLogFile)
        Logger.log("TNAME: \(interfaceName!)", to: Logger.vpnLogFile)
        Logger.log("HEV_SOCKS5_TUNNEL_MAIN: \(hev_socks5_tunnel_main(file.path, fd))", to: Logger.vpnLogFile)
    }
}

I got the same issue
only routes IPv6

Emm, check your ipv4 address and route of tun devices.

there is not much you can do with iOS

heiher commented

Is it possible that 198.18.0.0/15 is not routable to the internet on iOS? (It works fine on Linux/Android)

Is it possible that 198.18.0.0/15 is not routable to the internet on iOS? (It works fine on Linux/Android)

I'm using 192.168.23.0/24 I don't think that's the issue

My logs:

[2023-03-03 04:35:46] [D] socks5 tunnel init
[2023-03-03 04:35:46] [D] socks5 tunnel run
[2023-03-03 04:35:46] [D] socks5 tunnel lwip task run
[2023-03-03 04:35:46] [D] socks5 tunnel timer task run
[2023-03-03 04:35:46] [D] socks5 tunnel event task run
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 construct
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 client construct
[2023-03-03 04:35:47] [I] 0x10460c140 socks5 client udp construct
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 session udp construct
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 session udp new
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 session run
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 client connect [192.168.1.206]:1080
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 client connect server
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 client connect server fd 10
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 client handshake
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 client write request
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 client read response
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 session udp splice
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 udp sendto
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 udp write udp
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 session udp fwd f send
[2023-03-03 04:35:47] [D] 0x10460c140 socks5 udp recvfrom udp
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 construct
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 client construct
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 client tcp construct ip
[2023-03-03 04:35:56] [I] 0x10450c200 socks5 client tcp -> [2606:6e00:c000:1000::513]:443
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 session tcp construct
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 session tcp new
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 session run
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 client connect [192.168.1.206]:1080
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 client connect server
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 client connect server fd 13
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 client handshake
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 client write request
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 client read response
[2023-03-03 04:35:56] [D] 0x10450c200 socks5 session tcp splice
[2023-03-03 04:36:02] [D] 0x10460c400 socks5 construct
[2023-03-03 04:36:02] [D] 0x10460c400 socks5 client construct
[2023-03-03 04:36:02] [I] 0x10460c400 socks5 client udp construct
[2023-03-03 04:36:02] [D] 0x10460c400 socks5 session udp construct
[2023-03-03 04:36:02] [D] 0x10460c400 socks5 session udp new
[2023-03-03 04:36:02] [D] 0x10460c400 socks5 session run
[2023-03-03 04:36:02] [D] 0x10460c400 socks5 client connect [192.168.1.206]:1080
[2023-03-03 04:36:02] [D] 0x10460c400 socks5 client connect server
[2023-03-03 04:36:02] [D] 0x10460c400 socks5 client connect server fd 14
heiher commented

I'm out of ideas.

I'm out of ideas.

honestly, I'm so tired I would even pay people to fix this 🤦

@heiher @daemooon
please send me your crypto wallets (preferably USDT-TRC20) if you have one.
I want to buy you guys a cup of coffee or two.