Handling Deep Link URLs (clash://, clash-verge://) in Clash Verge Rev
Clash Verge Rev uses Tauri's deep-link plugin to intercept custom URL schemes and automatically import subscription profiles from external sources.
The clash-verge-rev/clash-verge-rev repository implements a robust cross-platform pipeline that processes clash:// and clash-verge:// URLs. When a user clicks these links, the application parses the embedded subscription URL, fetches the remote profile, and refreshes the running Clash core configuration without manual intervention.
How Deep Link Registration Works in Tauri
Clash Verge Rev leverages tauri-plugin-deep-link to register custom URL schemes at the operating system level. This registration allows browsers and other applications to launch Clash Verge Rev using protocol handlers.
Configuring URL Schemes in tauri.conf.json
The supported schemes are declared in the Tauri configuration file. The plugin instructs the OS to route any clash:// or clash-verge:// URL to the application.
{
"plugins": {
"deep-link": {
"desktop": {
"schemes": ["clash", "clash-verge"]
}
}
}
}
The Deep Link Processing Pipeline
When the OS delivers a deep-link URL, Clash Verge Rev executes a multi-stage resolution pipeline implemented across several Rust modules.
Step 1: OS to Tauri Callback (lib.rs)
The setup_deep_links function in src-tauri/src/lib.rs registers the callback that receives URLs from the OS. It uses app.deep_link().on_open_url to handle incoming requests asynchronously.
// src-tauri/src/lib.rs
pub fn setup_deep_links(app: &tauri::App) {
#[cfg(any(target_os = "linux", all(debug_assertions, windows)))]
{
logging!(info, Type::Setup, "注册深层链接...");
let _ = app.deep_link().register_all();
}
app.deep_link().on_open_url(|event| {
let urls = event.urls();
AsyncHandler::spawn(move || async move {
if let Some(url) = urls.first()
&& let Err(e) = resolve::resolve_scheme(url.as_ref()).await
{
logging!(error, Type::Setup, "Failed to resolve scheme: {}", e);
}
});
});
}
Step 2: Embedded HTTP Server Routing (server.rs)
The callback forwards the URL to an embedded Warp server running on a singleton port. The src-tauri/src/utils/server.rs module exposes a /commands/scheme endpoint that accepts the URL as a query parameter.
// src-tauri/src/utils/server.rs (excerpt)
let scheme = warp::path!("commands" / "scheme")
.and(warp::query::<QueryParam>())
.and_then(|query: QueryParam| async move {
AsyncHandler::spawn(|| async move {
logging_error!(Type::Setup, resolve::resolve_scheme(&query.param).await);
});
Ok::<_, warp::Rejection>(warp::reply::with_status(
"ok".to_string(),
warp::http::StatusCode::OK,
))
});
Step 3: URL Parsing and Validation (scheme.rs)
The resolve_scheme function in src-tauri/src/utils/resolve/scheme.rs performs strict validation. It ensures the scheme is either clash or clash-verge, extracts the url query parameter (percent-decoded), and optionally captures a name parameter for the profile.
// src-tauri/src/utils/resolve/scheme.rs (excerpt)
fn extract_subscription_info(link_parsed: &Url) -> Option<(String, Option<String>)> {
if !matches!(link_parsed.scheme(), "clash" | "clash-verge") {
return None;
}
let name = link_parsed
.query_pairs()
.find(|(key, _)| key == "name")
.map(|(_, v)| v.into_owned().into());
let url = extract_subscription_url(link_parsed)?;
Some((url, name))
}
Step 4: Subscription Import and Core Refresh
Once validated, the import_subscription function fetches the remote profile using PrfItem::from_url, persists it to the local profile store, and triggers UI updates. If the imported profile becomes the active configuration, refresh_core_config updates the running Clash core without requiring a restart.
Handling Singleton Mode and Running Instances
Clash Verge Rev implements singleton detection to prevent multiple instances from processing the same deep link. The check_singleton function in src-tauri/src/utils/server.rs detects if the application is already running. If so, it forwards the command-line argument (the deep-link URL) to the existing instance via an HTTP request to the embedded server.
// src-tauri/src/utils/server.rs (excerpt)
if param.starts_with("clash:") {
client
.get(format!("http://127.0.0.1:{port}/commands/scheme?param={param}"))
.send()
.await?;
}
This ensures that clicking a deep link while the app is running simply imports the subscription into the active window rather than spawning a duplicate process.
Deep Link URL Format and Examples
Clash Verge Rev expects deep links to follow a specific query parameter structure. The URL must contain a url parameter with a percent-encoded subscription link, and optionally a name parameter to preset the profile name.
Example deep-link URL:
clash://?url=https%3A%2F%2Fexample.com%2Fmy-subscription.yaml&name=My%20VPN
When a user clicks this link, Clash Verge Rev:
- Validates the
clash://scheme - Decodes the subscription URL (
https://example.com/my-subscription.yaml) - Imports the profile with the display name "My VPN"
- Refreshes the core configuration if this becomes the active profile
Summary
- Tauri deep-link plugin registers
clash://andclash-verge://schemes intauri.conf.json, enabling OS-level URL handling. - Callback registration in
src-tauri/src/lib.rscaptures incoming URLs and forwards them to the resolution pipeline. - Embedded Warp server in
src-tauri/src/utils/server.rsexposes a/commands/schemeendpoint that processes URLs asynchronously. - URL validation in
src-tauri/src/utils/resolve/scheme.rsensures only valid schemes are accepted, extracts percent-encoded subscription URLs, and handles the optionalnameparameter. - Singleton mode prevents duplicate processing by forwarding deep links from secondary instances to the primary running application via HTTP requests.
- Automatic core refresh updates the running Clash configuration immediately after importing a new subscription profile.
Frequently Asked Questions
What URL schemes does Clash Verge Rev support for deep linking?
Clash Verge Rev registers two custom URL schemes: clash:// and clash-verge://. Both schemes trigger the same internal handling logic and are configured in the tauri.conf.json file under the deep-link plugin settings. You can use either scheme interchangeably when generating subscription links.
How does Clash Verge Rev handle deep links when the application is already running?
When the application is already active, the singleton detection mechanism in src-tauri/src/utils/server.rs intercepts the new instance. Instead of launching a duplicate window, the new process sends the deep-link URL via an HTTP GET request to the embedded Warp server running on the singleton port. The primary instance receives the request at /commands/scheme and processes the subscription import normally.
What parameters are required in a Clash Verge Rev deep link?
The only required parameter is url, which must contain a percent-encoded link to a valid Clash subscription configuration (YAML or base64-encoded). The optional name parameter allows you to preset the display name for the imported profile. For example: clash://?url=https%3A%2F%2Fexample.com%2Fsub.yaml&name=WorkVPN. The application validates the scheme, decodes the URL safely, and rejects malformed inputs.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →