Repository navigation
clash-cli: read and edit profiles, override rules, and per-domain DNS - #102
zifengjiang wants to merge 2 commits into
Conversation
|
Thank you for this, and sorry for the slow reply. Letting scripts and AI agents drive Clash through Before it can go in, two things need solving:
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. |
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.
f28a5f9 to
482e62d
Compare
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.
|
Both points are addressed, and the branch is rebased onto current One lock across processes. Writing the store is now one protocol on both sides of the process boundary: flock 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 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. |
|
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. |
Why
clash-clicontrols 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 capabilityapp-store-v1.What
New commands:
--profiledefaults to the active profile, by id or label.App side: the published config is derived from the profile store, and was only regenerated when the app decided to.
ProfileInputsStamprecords a fingerprint of the inputs a published revision was built from;prepareForStartcompares 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).rules appendanddns policy set, then reconnect —clash-cli rule match <domain>reports the new rule andclash-cli dns queryreturns the address from the policy's nameserver.