Premium · Clash documentation

Feature: Script Shortcuts

Define Script Shortcuts in Clash Premium Rule mode, compare Expr with Starlark, and review common variables, functions, and no-resolve usage.

  • Script Shortcuts
  • Expr
  • Starlark
  • no-resolve
  • SCRIPT
Premium

Clash Premium provides Python3-based scripting so users can select packet policies dynamically and flexibly.

You can control the entire rule-matching engine with a single Python script, or define Shortcuts and use them alongside regular rules. This page covers the latter; for the former, see Script.

This feature makes it possible to use scripts in rules mode. By default, SCRIPT rules perform DNS resolution. Append no-resolve to a rule to prevent resolution (for example, 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

Evaluation engines

Expr, the default engine for Script Shortcuts, is 10 to 20 times faster than Starlark.

Starlark is a Python-like configuration language that can also be used for Script Shortcuts.

Variables

  • network: string
  • type: string
  • src_ip: string
  • dst_ip: string
  • src_port: uint16
  • dst_port: uint16
  • inbound_port: uint16
  • host: string
  • process_path: string

WARNING

Starlark does not currently include the process_path variable.

Functions

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)