Skip to content

[Script] Extension Scripts ​

This section configures JavaScript scripts to execute custom logic during DNS resolution, rule matching, HTTP request/response processing, scheduled tasks, network events, or manual invocations.

The system supports seven trigger types:

  • HTTP Request (http-request): Triggered before a request is sent to the upstream server. Scripts can inspect or modify the URL, headers, and body, or return a mock response directly.
  • HTTP Response (http-response): Triggered before an upstream response is returned to the client. Scripts can inspect or modify status codes, response headers, and bodies.
  • DNS (dns): Invoked via script:name in [Host], returning IP addresses or upstream DNS servers based on $domain.
  • Rule (rule): Invoked via SCRIPT,name,policy in [Rule], returning {matched: true/false} based on $request.
  • Generic (generic): Invoked manually by name via the management API POST /scripts/evaluate.
  • Scheduled Cron (cron): Triggered periodically based on a Cron expression.
  • Network Event (event): Triggered when system network environments or default interfaces change.

HTTPS traffic must first be decrypted via MITM before plaintext messages can be processed by scripts.

Syntax Declaration ​

Each line declares a named script instance with comma-separated key-value parameters:

ini
[Script]
# General structure
name = type=..., script-path=..., [options...]

# HTTP request script
name = type=http-request, pattern=..., script-path=..., [requires-body=...], [max-size=...], [binary-body-mode=...], [options...]

# HTTP response script
name = type=http-response, pattern=..., script-path=..., [requires-body=...], [max-size=...], [binary-body-mode=...], [options...]

# Scheduled cron script
name = type=cron, cronexp=..., script-path=..., [options...]

# Network event script
name = type=event, event-name=..., script-path=..., [options...]

# DNS and rule scripts, referenced by Host or Rule
name = type=dns, script-path=..., [options...]
name = type=rule, script-path=..., [options...]

# Generic script, defaults to generic when type is omitted
name = type=generic, script-path=..., [options...]

Component breakdown:

  • name: Unique identifier for the script within the profile. Supports any non-empty, single-line string without control characters (including spaces, parentheses, colons, and forward-slash relative paths such as scripts/dns/rewrite; leading and trailing whitespace is trimmed, and whitespace-only names are rejected). Enclose name in double quotes "..." if it contains commas (,), equals signs (=), inline comment triggers (whitespace followed by # or ;), starts with #, ;, or [, or equals %APPEND% or %INSERT% (escape embedded quotes and backslashes as \" and \\). Quote the script name when referencing names with commas or equals signs in [Rule] or [Host].
  • =: Key-value delimiter.
  • type: Script trigger type.
  • script-path: Local path or remote HTTP/HTTPS URL for the script file.
  • Optional parameters: Additional options configured as key=value pairs based on script type.

Common Options ​

The following options apply to all script types:

  • type (Enum, default generic) Script trigger type determining lifecycle and execution timing. Available values:

    • http-request: Triggered before an HTTP request is forwarded to the upstream server
    • http-response: Triggered before an upstream HTTP response is returned to the client
    • dns: Triggered during DNS resolution when invoked by [Host]
    • rule: Triggered during rule evaluation when invoked by [Rule]
    • generic: Generic script, triggered solely via manual API invocation
    • cron: Scheduled task, triggered periodically by Cron expression
    • event: Triggered upon network environment or default interface changes
    ini
    type=http-request
  • script-path (Local file path or HTTP/HTTPS URL, required) Location of the script file. Supports relative paths, absolute paths, or remote http:// and https:// URLs.

    ini
    script-path=./scripts/app.js
  • update-interval (Integer, unit: seconds, default 86400, remote only) Background auto-update interval, valid only for remote http:// / https:// scripts. Defaults to 86400 seconds when unset; setting <= 0 (such as 0 or -1) disables scheduled and background auto-updates (a missing local cache is still fetched once, and manual refreshes are unaffected). Configuring this option on a local file script fails validation.

    ini
    update-interval=86400
  • timeout (Positive integer 1–86400, unit: seconds, default 5) Maximum execution timeout per invocation (up to 24 hours).

    ini
    timeout=10
  • debug (Boolean, default false) Outputs runtime execution details and duration in debug logs when enabled.

    ini
    debug=true
  • argument (String, unset) Static argument passed into the script's global execution environment.

    ini
    argument="mode=fast"
  • engine (Enum, default auto) JavaScript execution engine. Normally use quick (aliases: quickjs, jsc) or webview, or use auto to select by trigger type. Values are case-insensitive.

    ini
    engine=quick
ValueExecution behavior
auto or omittedhttp-request and http-response use webview; dns, rule, generic, cron, and event use quick.
quick / quickjs / jscA small, fast engine suited to short, latency-sensitive scripts. Uses the default built-in implementation (currently QuickJS), canonically formatted as quick.
webviewHigher execution performance, suited to heavier scripts and HTTP body processing. Interprocess communication (IPC) adds call latency, making it unsuitable for short, latency-sensitive tasks. Execution fails if the host does not provide it; another engine is never substituted.

Unrecognized engine values are treated as auto.

Type-Specific Options ​

HTTP Scripts (http-request / http-response) ​

When type is http-request or http-response, the script intercepts HTTP transactions matching a URL pattern:

  • pattern (Regular expression string, required) Regular expression matching the full target URL.

    ini
    pattern=^https?://api\.example\.com/
  • requires-body (Boolean, default false) Controls whether to capture and pass the complete HTTP body to the script.

    ini
    requires-body=true
  • max-size (Non-zero integer, unit: bytes, default 131072 / 128 KB) Maximum captured body size. A positive integer (> 0) limits byte size; a negative value (< 0, e.g., -1) removes the size limit. 0 is forbidden (max-size=0 fails validation).

    ini
    max-size=262144
  • binary-body-mode (Boolean, default false) Passes the message body as a binary byte sequence (e.g., Uint8Array) to the script.

    ini
    binary-body-mode=true

Scheduled Cron Scripts (cron) ​

When type is cron, the script runs according to a schedule:

  • cronexp (Cron expression string, required) Cron schedule expression supporting standard 5-segment or 6-segment formats; enclose in quotes if it contains spaces.

    ini
    cronexp="0 */2 * * *"

Network Event Scripts (event) ​

When type is event, the script listens for system network changes:

  • event-name (Enum, required) Network state event name, fixed to network-changed. Triggered when network interfaces, default routes, or connectivity states change.

    ini
    event-name=network-changed

Configuration Examples ​

Basic Configuration ​

ini
[Script]
# HTTP request script
SignRequest = type=http-request, pattern=^https?://api\.example\.com/v1/trade, script-path=./scripts/sign.js, requires-body=true, timeout=10

# HTTP response script
DecryptResponse = type=http-response, pattern=^https?://api\.example\.com/v1/data, script-path=https://resource.example.com/scripts/decrypt.js, update-interval=86400, requires-body=true

# Scheduled cron script
HealthCheck = type=cron, cronexp="0 */2 * * *", script-path=./scripts/cron_check.js, timeout=30

# Network event script
NetworkWatcher = type=event, event-name=network-changed, script-path=./scripts/network_event.js

DNS, Rule, and Generic Scripts ​

ini
[Script]
scripts/dns/rewrite = type=dns, script-path=./scripts/resolver.js
"Rule Judge (v2)" = type=rule, script-path=./scripts/judge.js
"Name, With = Punctuation" = script-path=./scripts/manual.js, argument=example

[Host]
example.com = script:scripts/dns/rewrite

[Rule]
SCRIPT,"Rule Judge (v2)",DIRECT
FINAL,DIRECT

DNS scripts return addresses via $done({addresses:["192.0.2.1"],ttl:60}); calling $done({}) falls back to standard resolution. Rule scripts use $done({matched:$request.hostname === "example.com"}) to determine rule matching, with the outbound policy defined in the rule line. Generic scripts do not execute automatically; when invoked by name via the control API, $trigger is set to http-api, and results are returned via $done(value).