Surgio Documentation

repository·master·Indexed 23 days ago

https://github.com/surgioproject/surgio

A framework for automating the generation of proxy configuration rules for clients such as Surge, Clash, and Quantumult. Surgio provides a unified workflow to parse subscription URLs or node lists and generate tailored configuration files using templates. It supports custom Providers with JavaScript, dynamic node switching via UserAgent detection or feature flags, and deployment options including Docker, Netlify Functions, and reverse proxies.

Tokens
36.1K
Snippets
102
Records
232
Agent score
83%

What's inside Surgio

  1. What is Surgio and how does it work?

    master

    Surgio is a tool designed to automate the creation of proxy configuration files for different clients. It solves the problem of managing multiple proxy subscriptions (from different providers) and multiple proxy clients (which often require different configuration formats) by providing a unified workflow.

    Core Workflow

    Surgio operates in two main stages:

    1. Parsing: It parses subscription URLs from various providers or parses your own maintained node lists (supporting protocols like Shadowsocks, Vmess, and Shadowsocksr).
    2. Generation: It uses templates to define rules and generates specific configuration files tailored for different proxy clients.

    Additionally, Surgio includes a utility to upload generated configurations to Alibaba Cloud OSS, enabling you to quickly create subscription links for your clients.

  2. Create an Asynchronous Custom Provider

    master

    In asynchronous mode, nodeList is a function that receives customParams. This allows you to generate dynamic node lists based on request parameters.

    If you are using Gateway, customParams includes all parameters from the request URL. Note that all URL parameters are provided as strings (e.g., mobile=true results in customParams.mobile === 'true'). The requestUserAgent is also included by default to help differentiate lists based on the client.

    const { defineCustomProvider } = require('surgio')
    
    module.exports = defineCustomProvider({
      nodeList: async (customParams) => {
        if (customParams.mobile === 'true') {
          return [
            {
              type: 'shadowsocks',
              // ...
            },
          ]
        } else {
          return [
            {
              type: 'trojan',
              // ...
            },
          ]
        }
      },
    })
  3. Generate sing-box configurations with getSingboxNodes and getSingboxEndpoints

    master

    For sing-box, nodes are handled differently depending on their type:

    1. Outbounds: Use getSingboxNodes(nodeList, filter?) to get an array of node information for outbound rules.
    2. Endpoints: sing-box treats Tailscale nodes as endpoints. Use getSingboxEndpoints(nodeList, filter?) to get an array of endpoint information for the endpoints field in the configuration.
    3. Node Names: Use getSingboxNodeNames(nodeList, filter?) to get an array of names that includes both outbounds and endpoints, making them easy to reference in selector or urltest blocks.

    Example for node names:

    getSingboxNodeNames(nodeList, netflixFilter);
  4. Why use Redis cache in Surgio

    master

    If you are using an API gateway and have multiple subscriptions or remote fragments, enabling Redis cache can significantly reduce cold start times.

    Standard local caching in Surgio uses files and memory. However, these are cleared whenever a Serverless platform is redeployed or when a process is suspended, leading to longer cold starts. Because Redis runs in an independent process, it persists across these events, maintaining cache availability.

  5. Configure Artifacts to generate configuration files

    master

    Surgio uses Artifact objects to determine how configuration files are generated. You can define multiple Artifacts to generate various configuration files in a single run. An Artifact requires a name, a template, and a provider.

    {
      name: 'SurgeV3.conf',
      template: 'surge_v3',
      provider: 'demo',
    }
  6. Use nodeConfig for individual node customization

    master

    The nodeConfig object allows you to customize properties for every individual node within a Provider. These settings are merged into the final configuration for each node.

    Common nodeConfig properties:

    • enable (boolean, default: true): Disables a specific node from being output.
    • tfo (boolean, default: false): Enables TCP Fast Open.
    • mptcp (boolean, default: false): Enables Multipath TCP (Surge only).
    • tls13 (boolean, default: false): Enables TLS 1.3 (requires server support).
    • skipCertVerify (boolean, default: false): Disables TLS certificate checking.
    • portHopping (string): Enables port hopping for Tuic and Hysteria (Surge, Sing-box, Stash, Mihomo). Supports comma/semicolon lists or hyphenated ranges (e.g., 5000,6000-7000).
    • portHoppingInterval (number): Interval for port hopping in seconds (Surge, Stash, Mihomo).
    • underlyingProxy (string): Sets a proxy jump (Surge only).
    • testUrl (string): Sets a specific test URL for the proxy (Surge only).
    • serverCertFingerprintSha256 (string): Verifies server certificate SHA256 fingerprint (Surge only).
    • ecn (boolean, default: false): Enables Explicit Congestion Notification (Surge only).
    • blockQuic (string): Blocks QUIC traffic to force fallback to HTTPS/TCP. Options: auto, on, off (Surge only).
    • shadowTls (object): Configures Shadow TLS (Surge and Stash only). Requires password and sni.
    {
      enable: false,
      type: 'shadowsocks',
      nodeName: '🇺🇸US',
      hostname: 'us.example.com',
      port: 10000,
      method: 'chacha20-ietf-poly1305',
      password: 'password',
    }
  7. Access request metadata in custom Providers

    master

    When writing an asynchronous nodeList function in a custom Provider, you can access metadata about the incoming subscription request via the customParams argument. This allows you to dynamically change the returned nodes based on who is requesting them.

    Key properties available in customParams:

    • requestUserAgent: The UserAgent string of the client making the request.
    • [key]: Any URL query parameters passed in the request URL (all values are strings). For example, if the URL is ?game=1, you can access it via customParams.game.
  8. Understand Surgio core concepts: Provider, Template, and Artifact

    master

    Surgio operates on three core abstractions to transform node data into usable configurations:

    • Provider: The source of nodes. This can be a subscription URL or a set of node configurations.
    • Template: The logic used by Surgio to render specific files based on the provided data.
    • Artifact: The final output or "product" generated by Surgio (the resulting rules/configurations).

    Relationship: Surgio takes nodes from a Provider and uses a Template to generate an Artifact.

  9. Optimize Apple service and CDN rules

    master

    When configuring proxy rules for Apple services, it is strongly recommended to use the built-in apple_rules.tpl template. This template is designed to effectively handle traffic splitting for both Apple service interfaces and Apple's global CDN.

    Note: While you can reference other remote snippets, Surgio specifically supports referencing Surge remote snippets.