[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 viascript:namein[Host], returning IP addresses or upstream DNS servers based on$domain. - Rule (
rule): Invoked viaSCRIPT,name,policyin[Rule], returning{matched: true/false}based on$request. - Generic (
generic): Invoked manually by name via the management APIPOST /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:
[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 asscripts/dns/rewrite; leading and trailing whitespace is trimmed, and whitespace-only names are rejected). Enclosenamein 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=valuepairs based on script type.
Common Options
The following options apply to all script types:
type(Enum, defaultgeneric) Script trigger type determining lifecycle and execution timing. Available values:http-request: Triggered before an HTTP request is forwarded to the upstream serverhttp-response: Triggered before an upstream HTTP response is returned to the clientdns: 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 invocationcron: Scheduled task, triggered periodically by Cron expressionevent: Triggered upon network environment or default interface changes
initype=http-requestscript-path(Local file path or HTTP/HTTPS URL, required) Location of the script file. Supports relative paths, absolute paths, or remotehttp://andhttps://URLs.iniscript-path=./scripts/app.jsupdate-interval(Integer, unit: seconds, default86400, remote only) Background auto-update interval, valid only for remotehttp:///https://scripts. Defaults to86400seconds when unset; setting<= 0(such as0or-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.iniupdate-interval=86400timeout(Positive integer 1–86400, unit: seconds, default5) Maximum execution timeout per invocation (up to 24 hours).initimeout=10debug(Boolean, defaultfalse) Outputs runtime execution details and duration in debug logs when enabled.inidebug=trueargument(String, unset) Static argument passed into the script's global execution environment.iniargument="mode=fast"engine(Enum, defaultauto) JavaScript execution engine. Normally usequick(aliases:quickjs,jsc) orwebview, or useautoto select by trigger type. Values are case-insensitive.iniengine=quick
| Value | Execution behavior |
|---|---|
auto or omitted | http-request and http-response use webview; dns, rule, generic, cron, and event use quick. |
quick / quickjs / jsc | A small, fast engine suited to short, latency-sensitive scripts. Uses the default built-in implementation (currently QuickJS), canonically formatted as quick. |
webview | Higher 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.inipattern=^https?://api\.example\.com/requires-body(Boolean, defaultfalse) Controls whether to capture and pass the complete HTTP body to the script.inirequires-body=truemax-size(Non-zero integer, unit: bytes, default131072/ 128 KB) Maximum captured body size. A positive integer (> 0) limits byte size; a negative value (< 0, e.g.,-1) removes the size limit.0is forbidden (max-size=0fails validation).inimax-size=262144binary-body-mode(Boolean, defaultfalse) Passes the message body as a binary byte sequence (e.g., Uint8Array) to the script.inibinary-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.inicronexp="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 tonetwork-changed. Triggered when network interfaces, default routes, or connectivity states change.inievent-name=network-changed
Configuration Examples
Basic Configuration
[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.jsDNS, Rule, and Generic Scripts
[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,DIRECTDNS 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).
