Import Tun2SocksKit
mainTo use the library in your Swift project, import the Tun2SocksKit module.
import Tun2SocksKitrepository·main·Indexed 40 days ago
https://github.com/ebrahimtahernejad/tun2sockskitA 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.
To use the library in your Swift project, import the Tun2SocksKit module.
import Tun2SocksKitTun2SocksKit uses a YAML configuration format. The configuration is divided into three main sections: tunnel, socks5, and misc.
| Section | Key | Description |
|---|---|---|
| tunnel | mtu | Maximum Transmission Unit |
| socks5 | port | The port to listen on |
| socks5 | address | The IP address to bind to |
| socks5 | udp | UDP mode setting (e.g., 'udp') |
| misc | task-stack-size | Stack size for tasks |
| misc | tcp-buffer-size | TCP buffer size |
| misc | max-session-count | Maximum number of concurrent sessions |
| misc | connect-timeout | Connection timeout in milliseconds |
| misc | read-write-timeout | Read/write timeout in milliseconds |
| misc | log-file | Destination for logs (e.g., stderr) |
| misc | log-level | Logging verbosity (e.g., error) |
| misc | limit-nofile | Limit 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: 65535You can start the tunnel using Socks5Tunnel.run(withConfig:). The withConfig parameter accepts two types of configurations:
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))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
}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)