use std::path::PathBuf;
use anyhow::Result;
use clap::{Parser, Subcommand};
use tracing::{info, warn};
use anyhow::Context;
use ilink_hub::bridge::{
builtin, default_local_credential_path, resolve_hub_connection, run_bridge, BridgeApp,
BridgeStop,
};
use ilink_hub::paths::default_bridge_config_path;
#[derive(Parser)]
#[command(name = "ilink-hub-bridge")]
#[command(
version,
about = "Bridge WeChat (via iLink Hub) to a local coding CLI (Claude Code, Codex, …)"
)]
struct Cli {
#[arg(long, env = "WEIXIN_BASE_URL", default_value = "http://127.0.0.1:8765", global = true)]
hub_url: String,
#[arg(long, env = "WEIXIN_TOKEN", global = true)]
token: Option<String>,
#[arg(long, env = "ILINKHUB_BRIDGE_CREDS", global = true)]
cred_file: Option<String>,
#[arg(long, default_value_t = false, global = true)]
pair: bool,
#[arg(long, env = "ILINKHUB_BRIDGE_REGISTER_NAME", global = true)]
register_name: Option<String>,
#[arg(long, default_value_t = false, global = true)]
force_register: bool,
#[arg(long)]
config: Option<PathBuf>,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Profile {
#[arg(value_name = "TYPE")]
profile_type: String,
},
}
fn explicit_token(cli: &Cli) -> Option<&str> {
cli.token
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::from_default_env()
.add_directive("ilink_hub=info".parse()?),
)
.init();
let cli = Cli::parse();
match &cli.command {
Some(Commands::Profile { profile_type }) => {
builtin::run_builtin_profile(profile_type).await
}
None => {
let config_path = cli
.config
.clone()
.unwrap_or_else(default_bridge_config_path);
let app = BridgeApp::load(&config_path)?;
info!(config_path = %config_path.display(), "loaded bridge config");
let cred_path = cli
.cred_file
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
.map(std::path::PathBuf::from)
.unwrap_or_else(default_local_credential_path);
let using_explicit_token = explicit_token(&cli).is_some();
'reconnect: loop {
let (hub_url, token) = resolve_hub_connection(
&cli.hub_url,
explicit_token(&cli),
cli.cred_file.as_deref(),
cli.pair,
cli.register_name.as_deref(),
cli.force_register,
Some(config_path.as_path()),
)
.await?;
info!(%hub_url, "using Hub base URL for downstream");
let mut handle = tokio::spawn(run_bridge(hub_url, token, app.clone()));
loop {
tokio::select! {
_ = tokio::signal::ctrl_c() => {
handle.abort();
let _ = handle.await;
info!("exit");
return Ok(());
}
result = &mut handle => {
match result {
Ok(BridgeStop::TokenRejected) if using_explicit_token => {
anyhow::bail!(
"Hub 拒绝了 WEIXIN_TOKEN / --token(未注册或已失效)。\
请重新执行 `ilink-hub register` 或 `ilink-hub-bridge --force-register`。"
);
}
Ok(BridgeStop::TokenRejected) => {
warn!(
path = %cred_path.display(),
"hub token revoked at runtime; removing credentials and re-registering"
);
let _ = tokio::fs::remove_file(&cred_path).await;
continue 'reconnect;
}
Err(e) => {
return Err(e).context("bridge task panicked or failed");
}
}
}
}
}
}
}
}
}