tun2socks Documentation

repository·main·Indexed 26 days ago

https://github.com/xjasonlyu/tun2socks

A high-performance user-space networking tool that transparently routes system or network traffic through proxy protocols (HTTP, SOCKS, Shadowsocks, SSH, and Relay) using a TUN interface and the gVisor network stack. Supports Linux, macOS, Windows, FreeBSD, and OpenBSD, featuring IPv6 compatibility, gateway mode, and a REST API for real-time traffic statistics and version information.

Tokens
792
Snippets
0
Records
9
Agent score
90%

What's inside tun2socks

  1. Overview of tun2socks features

    main

    tun2socks is a universal proxying tool that transparently routes all network traffic from any application through a proxy.

    Key capabilities include:

    • Multi-Protocol Support: Works with HTTP, SOCKS, Shadowsocks, SSH, and Relay proxies (with optional authentication).
    • Cross-Platform: Optimized for Linux, macOS, Windows, FreeBSD, and OpenBSD.
    • Gateway Mode: Can act as a Layer 3 gateway to route traffic from other devices on the same network.
    • IPv6 Compatibility: Native IPv6 support with seamless tunneling between IPv4 and IPv6.
    • User-Space Networking: Uses the gVisor network stack for performance and flexibility.
  2. Configure tun2socks via YAML file

    main
    You can provide a YAML configuration file using the -c or --config flag. The file content is unmarshaled directly into the engine.Key structure. This is useful for managing complex configurations including proxy protocols, MTU settings, and network stack tuning.
  3. Start the REST API server with Start()

    main
    The Start function initializes and runs the HTTP server. It sets up CORS with all origins allowed and implements authentication for all routes. The server listens on the provided addr and requires a token for authenticated requests.
  4. Get real-time traffic statistics via /traffic

    main

    The /traffic endpoint provides real-time network statistics (upload and download bytes). It supports both standard HTTP polling and WebSocket streaming.

    • HTTP Polling: Returns a JSON object containing up and down fields. The server flushes the response every second.
    • WebSocket Streaming: Upgrades the connection to a WebSocket. The server pushes a JSON message containing up and down every second.

    JSON Response Format:

    {
      "up": 12345,
      "down": 67890
    }
  5. Authenticate REST API requests

    main

    The REST API uses token-based authentication. Depending on the request type, you must provide the token in one of two ways:

    1. Standard HTTP Requests: Use the Authorization header with the Bearer scheme.

      • Header format: Authorization: Bearer <your-token>
    2. WebSocket Upgrades: Since browsers do not support custom headers for WebSocket connections, provide the token as a query parameter.

      • URL format: ws://<addr>/traffic?token=<your-token>
  6. Use tun2socks CLI flags

    main
    The tun2socks CLI provides several flags to configure the TUN device, proxy settings, network stack parameters, and lifecycle hooks. You can either provide these flags directly via the command line or use a YAML configuration file.