Clash Premium 實作了基於 Python3 的指令碼功能,允許使用者以動態靈活的方式為資料包選擇策略。
您可以使用單一 Python 指令碼控制整個規則比對引擎,也可以定義 Shortcuts 捷徑並搭配一般規則使用。本頁介紹後者;如需瞭解前者,請參閱指令碼。
此功能可在 rules 模式中使用指令碼。預設會在 SCRIPT 規則中進行 DNS 解析;若要略過解析,可在規則後方加入 no-resolve(例如:SCRIPT,quic,DIRECT,no-resolve)。
yaml
mode: Rule
script:
engine: expr # or starlark (10x to 20x slower)
shortcuts:
quic: network == 'udp' and dst_port == 443
curl: resolve_process_name() == 'curl'
# curl: resolve_process_path() == '/usr/bin/curl'
rules:
- SCRIPT,quic,REJECT評估引擎
Expr 是 Script Shortcuts 的預設引擎,效能約為 Starlark 的 10 至 20 倍。
Starlark 是一種類似 Python 的設定語言,也可用於 Script Shortcuts。
變數
- network: string
- type: string
- src_ip: string
- dst_ip: string
- src_port: uint16
- dst_port: uint16
- inbound_port: uint16
- host: string
- process_path: string
警告
Starlark 目前不包含 process_path 變數。
函數
ts
type resolve_ip = (host: string) => string // ip string
type in_cidr = (ip: string, cidr: string) => boolean // ip in cidr
type in_ipset = (name: string, ip: string) => boolean // ip in ipset
type geoip = (ip: string) => string // country code
type match_provider = (name: string) => boolean // in rule provider
type resolve_process_name = () => string // find process name (curl .e.g)
type resolve_process_path = () => string // find process path (/usr/bin/curl .e.g)