feat: add connect-rs

This commit is contained in:
2025-10-13 22:58:53 +08:00
parent 85e442f114
commit c9928c65fe
6 changed files with 1990 additions and 0 deletions

81
connect-rs/src/main.rs Executable file
View File

@@ -0,0 +1,81 @@
#!/usr/bin/env runrs
//! ```cargo
//! [dependencies]
//! clap = { version = "4.5.48", features = ["derive"] }
//! reqwest = "0.12.23"
//! rust_util = "0.6.50"
//! ```
use clap::{Parser, Subcommand};
use rust_util::{failure, success, util_cmd};
use std::process::Command;
#[derive(Debug, Parser)]
#[command(name = "connect-rs", bin_name = "connect.rs")]
#[command(about = "Connect to the world", long_about = None)]
struct ConnectArgs {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Connect
#[command(short_flag = 'c')]
Connect,
/// Disconnect
#[command(short_flag = 'd')]
Disconnect,
/// Update
#[command(short_flag = 'u')]
Update,
}
fn main() {
let args = ConnectArgs::parse();
match args.command {
Commands::Connect => {
run_command(
"networksetup",
&["-setautoproxyurl", "Wi-Fi", "http://localhost:8888/pac"],
"setautoproxyurl proxy",
);
}
Commands::Disconnect => {
run_command(
"networksetup",
&["-setautoproxystate", "Wi-Fi", "off"],
"setautoproxystate off",
);
run_command(
"networksetup",
&["-setsocksfirewallproxystate", "Wi-Fi", "off"],
"setsocksfirewallproxystate off",
);
}
Commands::Update => {
// TODO ...
}
}
}
fn run_command(program: &str, args: &[&str], message: &str) {
let mut c2 = Command::new(program);
c2.args(args);
match util_cmd::run_command_and_wait(&mut c2) {
Ok(status) => {
if status.success() {
success!("{} success", message);
} else {
failure!("{} failed: {:?}", message, status.code());
}
}
Err(e) => {
failure!("{} failed: {}", message, e);
}
}
}
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20251013T225739+08:00.MEQCIDuRTM7sRjskoHHFG0BT
// VJBdG8FJwZzLSBGVQV1du3s4AiA7FJSyMmtQsq3Wxb8pDYmie8zc1m+r6AuUWJBAn8Pf8A==