Tun2SocksKit

repository·main·Indexed 40 days ago

https://github.com/ebrahimtahernejad/tun2sockskit

A Swift wrapper and build workflow for the hev-socks5-tunnel project that enables running a SOCKS5 tunnel on Apple platforms. It provides the Socks5Tunnel API for running tunnels via configuration files or strings, supports non-blocking mode with completion handlers, and allows retrieval of real-time upstream and downstream traffic statistics.

Tokens
835
Snippets
5
Records
5
Agent score
50%

What's inside Tun2SocksKit

  1. Configure Tun2SocksKit via YAML

    main

    Tun2SocksKit uses a YAML configuration format. The configuration is divided into three main sections: tunnel, socks5, and misc.

    Configuration Schema

    SectionKeyDescription
    tunnelmtuMaximum Transmission Unit
    socks5portThe port to listen on
    socks5addressThe IP address to bind to
    socks5udpUDP mode setting (e.g., 'udp')
    misctask-stack-sizeStack size for tasks
    misctcp-buffer-sizeTCP buffer size
    miscmax-session-countMaximum number of concurrent sessions
    miscconnect-timeoutConnection timeout in milliseconds
    miscread-write-timeoutRead/write timeout in milliseconds
    misclog-fileDestination for logs (e.g., stderr)
    misclog-levelLogging verbosity (e.g., error)
    misclimit-nofileLimit for open files

    Example configuration:

    tunnel:
      mtu: 9000
    
    socks5:
      port: 7890
      address: ::1
      udp: 'udp'
    
    misc:
      task-stack-size: 24576
      tcp-buffer-size: 4096
      max-session-count: 768
      connect-timeout: 5000
      read-write-timeout: 60000
      log-file: stderr
      log-level: error
      limit-nofile: 65535
  2. Run Socks5Tunnel with a configuration file or string

    main

    You can start the tunnel using Socks5Tunnel.run(withConfig:). The withConfig parameter accepts two types of configurations:

    1. File URL: Pass a file URL pointing to a configuration file.
    2. String: Pass the raw contents of a configuration file as a string.

    By default, these calls are blocking.

    // Run using a file URL
    let code = Socks5Tunnel.run(withConfig: .file(path: localConfigFileURL))
    
    // Run using a configuration string
    let code = Socks5Tunnel.run(withConfig: .string(content: stringConfigContent))
  3. Run Socks5Tunnel in non-blocking mode

    main

    To run the tunnel without blocking the current thread, provide a completion handler to Socks5Tunnel.run(withConfig:completion:). The closure is executed with the resulting code once the tunnel process finishes.

    Socks5Tunnel.run(withConfig: .string(content: stringConfigContent)) { code in
        // Do stuff with code
    }
  4. Retrieve tunnel statistics

    main

    Access real-time statistics via Socks5Tunnel.stats. The returned object provides data for both upstream (up) and downstream (down) traffic, including packet counts and byte counts.

    let stats = Socks5Tunnel.stats
    
    print(stats.up.packets)
    print(stats.up.bytes)
    print(stats.down.packets)
    print(stats.down.bytes)