Skip to content

clash-cli: read and edit profiles, override rules, and per-domain DNS - #102

Open
zifengjiang wants to merge 2 commits into
ProjectClash:mainfrom
zifengjiang:app-control-p0
Open

zifengjiang wants to merge 2 commits into
ProjectClash:mainfrom
zifengjiang:app-control-p0

Conversation

@zifengjiang

Copy link
Copy Markdown

Why

clash-cli controls the runtime but not the app's configuration store: a profile's override rules and per-domain DNS can only be edited in the UI. This adds the client half so those settings can be scripted, and makes the app pick the change up on the next connect without a manual rebuild.

Depends on the kernel side (ProjectClash/Clash#13), which serves /clash/v1/app/* and advertises capability app-store-v1.

What

New commands:

clash-cli profiles
clash-cli rules append  'DOMAIN-SUFFIX,corp.example.com,DIRECT' [--profile <id|label>]
clash-cli rules remove  'DOMAIN-SUFFIX,corp.example.com,DIRECT' [--profile <id|label>]
clash-cli dns policy [--profile <id|label>]
clash-cli dns policy set '+.example.com=10.0.0.1,10.0.0.2'
clash-cli dns policy clear [--fake-ip-filter]
  • --profile defaults to the active profile, by id or label.
  • DNS writes are read-merge-write, because the endpoint replaces the keys it is given.
  • Against an app without the capability the commands print what is missing and exit with code 5, the same way the other commands handle an unsupported server.

App side: the published config is derived from the profile store, and was only regenerated when the app decided to. ProfileInputsStamp records a fingerprint of the inputs a published revision was built from; prepareForStart compares it before validating and rebuilds when it differs, so an edit made through the controller lands on the next connect. A missing stamp is treated as a change once, and recorded.

Verification

  • go build ./... && go test ./... — 25 tests for the new commands (parsing, profile resolution, merge semantics, capability-missing path).
  • End to end against a running app on macOS and on iPhone over the LAN controller: rules append and dns policy set, then reconnect — clash-cli rule match <domain> reports the new rule and clash-cli dns query returns the address from the policy's nameserver.

@TokenPLS

Copy link
Copy Markdown
Member

Thank you for this, and sorry for the slow reply. Letting scripts and AI agents drive Clash through clash-cli is a direction we want to support, so we're treating this as more than a one-off change.

Before it can go in, two things need solving:

  1. Writes from two processes. profiles.json is written by the App, and with this change also by the tunnel extension, which runs in a separate process. The App's write lock only covers its own process, so a CLI write and an App write at the same moment can overwrite each other. The App also doesn't notice the file changed, so saving something in the UI afterwards can undo the CLI's edit. Both sides need to share one cross-process lock, and the App needs to pick up the change.
  2. Who can write. The new write endpoints are also reachable from a LAN controller client. Anyone on the network with the secret could then permanently change a profile's rules, or point a domain's DNS at any server. We'd start with local-only writes (the App Group socket) and look at the LAN case separately, after a security review.

The App's config store has also changed quite a bit since this branch was cut, so it will need bringing up to date. We'd like to bring the change into our tree and work through these two points; we'll follow up here, and on ProjectClash/Clash#13, with what we change.

jeffjiang added 2 commits November 23, 2026 07:50
profiles, rules append/remove and dns policy drive the App's configuration
store through the new app-store-v1 controller capability.

The store only accepts writes on the App Group socket, so the client gained
--socket/CLASH_SOCKET: a controller listener stays read-only for these
endpoints, and a refusal says so instead of blaming the secret.
The app-store endpoints write config inputs (override rules and the runtime
DNS patch) straight into the profile store. Without this, the derived files
are only rebuilt when the app decides to regenerate them, so an edit made
over the controller stayed invisible until an unrelated change forced a
rebuild. A stamp keeps a fingerprint of the inputs the published revision
was built from, and the activation preflight compares it before starting.

profiles.json now has one writer protocol on both sides of the process
boundary: the App and the kernel flock <store>/profiles.lock around a
read-modify-write, and write a new file before renaming it over the old one.
The App's read-then-save sections hold that same lock, and a directory watch
re-reads the store when another process changed it, republishing when the
active profile's inputs moved. The lock file is the kernel's; neither side
keeps a second copy of the state.
zifengjiang pushed a commit to zifengjiang/Clash that referenced this pull request Nov 22, 2026
The App's profile store is a file the App also edits from its own process,
so writes here take the same cross-process lock the App takes
(<store>/profiles.lock, flock, 5s wait) and replace the file with a
temp-then-rename write; a refusal is reported as 503 instead of a generic
failure.

Writes are local-only: they are accepted on the App Group socket and refused
on every controller listener, including the loopback one, with a 403 and an
X-Clash-Refusal header. Reads work on both. LAN writes are left off pending
a security review (see ProjectClash/Clash-Client#102); the policy lives in
one guard rather than a setting.
@zifengjiang

Copy link
Copy Markdown
Author

Both points are addressed, and the branch is rebased onto current main (733de1f). Two commits: the clash-cli commands, and the App-side store/activation work.

One lock across processes. Writing the store is now one protocol on both sides of the process boundary: flock <store>/profiles.lock, re-read profiles.json, then write a new file and rename it over the old one. The kernel's app-store writes take that lock with a bounded wait (5 s, then 503). ProfileStore takes the same lock for every write, and the read-then-save sections in the UI hold it across the whole read-modify-write, so a CLI write cannot land between a load and a save. The lock file is shared with the kernel; there is no second source of truth, and readers only ever see the previous or the new file.

Picking up external changes. The App watches the store directory and re-reads it when it changed on disk, skipping the reload when the re-read matches what it already has (its own writes). If the stored inputs of the active profile now differ from the ones the published revision was built from — the existing inputs stamp over override rules and the runtime patch — the profile is republished, so the tunnel stops running the old rules.

Local-only writes. The mutating endpoints are accepted on the App Group socket only. Every controller listener, the loopback one included, answers a write with 403 and X-Clash-Refusal: app-store-local-writes-only; reads are unchanged on all of them. The LAN write policy is one guard with a comment pointing at this discussion — LAN writes are read-only pending your security review, with no setting to enable them. clash-cli gained --socket / CLASH_SOCKET to reach the App Group socket, and a refused write now names the socket instead of reporting an authentication problem.

Verification on this branch: both Apple targets build (iOS and macOS); a real second process concurrently writing through the App's store path cannot lose an update, and the test fails when the lock is removed; the same holds mixing an App-path writer with a kernel-path writer; the App-side directory watch sees a write from another process. Existing CLI and kernel tests pass. The App UI wiring itself (view-model reload and republish) is exercised only by the build, not by a UI test.

@TokenPLS

Copy link
Copy Markdown
Member

Thank you for the thorough update — the shared lock, picking up outside changes, and keeping writes on the App Group socket only all go straight at the two points we raised, and the cross-process tests are exactly what we'd want to see.

To be straight with you about timing: we've put this feature on hold on our side for now. It isn't scheduled, so we won't review or merge it in the near term. When we pick it up, we'll start from this version of the branch and follow up here first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants