2024-05-03 12:00:27 +08:00
|
|
|
use std::time::Duration;
|
2023-05-13 19:01:27 +08:00
|
|
|
|
2024-05-03 12:00:27 +08:00
|
|
|
use anyhow::{anyhow, bail};
|
2023-05-13 19:01:27 +08:00
|
|
|
use clap::Parser;
|
2024-05-03 12:00:27 +08:00
|
|
|
use futures_util::StreamExt;
|
|
|
|
use log::{debug, error};
|
|
|
|
use tokio::time::sleep;
|
|
|
|
use tokio_tungstenite::{
|
|
|
|
connect_async,
|
|
|
|
tungstenite::{protocol::{frame::coding::CloseCode, CloseFrame}, Message},
|
2023-05-21 14:12:48 +08:00
|
|
|
};
|
2023-05-13 19:01:27 +08:00
|
|
|
|
|
|
|
mod clash_conn_msg;
|
|
|
|
mod statistics;
|
2023-05-14 11:55:43 +08:00
|
|
|
mod udp_server;
|
2024-03-29 18:24:58 +08:00
|
|
|
mod wan;
|
2023-05-13 19:01:27 +08:00
|
|
|
|
2024-03-29 18:24:58 +08:00
|
|
|
use wan::poll_wan_traffic;
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
2023-05-13 19:01:27 +08:00
|
|
|
#[clap(
|
|
|
|
version = "0.1.0",
|
|
|
|
author = "Ivan Li",
|
|
|
|
about = "Watch Clash network traffic"
|
|
|
|
)]
|
|
|
|
struct Args {
|
|
|
|
#[clap(
|
|
|
|
short,
|
2024-03-29 18:24:58 +08:00
|
|
|
env,
|
2023-05-13 19:01:27 +08:00
|
|
|
long,
|
2024-03-29 18:24:58 +08:00
|
|
|
default_value = "ws://192.168.1.1:9090/connections?token=123456"
|
2023-05-13 19:01:27 +08:00
|
|
|
)]
|
|
|
|
clash_url: String,
|
2024-03-29 18:24:58 +08:00
|
|
|
#[clap(short = 'p', long, env, default_value = "17890")]
|
2023-05-13 19:01:27 +08:00
|
|
|
listen_port: u16,
|
2024-03-29 18:24:58 +08:00
|
|
|
#[clap(short, long, env, default_value = "http://192.168.1.1/cgi-bin/luci")]
|
|
|
|
luci_url: String,
|
|
|
|
#[clap(short = 'u', long, env, default_value = "root")]
|
|
|
|
luci_username: String,
|
|
|
|
#[clap(short = 'P', long, env, default_value = "123456")]
|
|
|
|
luci_password: String,
|
2023-05-13 19:01:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2024-03-29 18:24:58 +08:00
|
|
|
dotenvy::dotenv().ok();
|
2023-05-14 11:55:43 +08:00
|
|
|
env_logger::init();
|
|
|
|
|
2023-05-13 19:01:27 +08:00
|
|
|
println!("Hello, world!");
|
|
|
|
|
|
|
|
let args = Args::parse();
|
|
|
|
|
2024-03-29 18:24:58 +08:00
|
|
|
tokio::spawn(async move {
|
2024-05-02 13:06:47 +08:00
|
|
|
loop {
|
|
|
|
if let Err(err) = poll_wan_traffic(
|
|
|
|
args.luci_url.as_str(),
|
|
|
|
args.luci_username.as_str(),
|
|
|
|
args.luci_password.as_str(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
error!("error: {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(Duration::from_secs(1)).await;
|
|
|
|
error!("restart poll_wan_traffic!");
|
|
|
|
}
|
2024-03-29 18:24:58 +08:00
|
|
|
});
|
|
|
|
|
2023-05-13 19:01:27 +08:00
|
|
|
let connect_addr = args.clash_url;
|
|
|
|
|
2023-05-21 14:12:48 +08:00
|
|
|
loop {
|
2024-05-02 13:06:47 +08:00
|
|
|
if let Err(err) = pipe(connect_addr.clone()).await {
|
|
|
|
error!("{}", err);
|
|
|
|
};
|
2023-05-21 14:12:48 +08:00
|
|
|
sleep(Duration::from_secs(1)).await;
|
2024-05-02 13:06:47 +08:00
|
|
|
error!("restart clash!");
|
2023-05-21 14:12:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 13:06:47 +08:00
|
|
|
async fn pipe(connect_addr: String) -> anyhow::Result<()> {
|
|
|
|
let url = url::Url::parse(&connect_addr).map_err(|err| anyhow::anyhow!(err))?;
|
2023-05-13 19:01:27 +08:00
|
|
|
|
2024-05-03 12:00:27 +08:00
|
|
|
let (mut ws_stream, _) = connect_async(url)
|
2024-05-02 13:06:47 +08:00
|
|
|
.await
|
|
|
|
.map_err(|err| anyhow::anyhow!(err))?;
|
2023-05-13 19:01:27 +08:00
|
|
|
println!("WebSocket handshake has been successfully completed");
|
|
|
|
|
2024-05-03 12:00:27 +08:00
|
|
|
while let Some(message) = ws_stream.next().await {
|
|
|
|
let message = message?;
|
|
|
|
|
|
|
|
match message {
|
|
|
|
Message::Text(text) => {
|
|
|
|
let data = text.into_bytes();
|
|
|
|
|
|
|
|
let wrapper =
|
|
|
|
serde_json::from_slice::<clash_conn_msg::ClashConnectionsWrapper>(&data);
|
|
|
|
|
|
|
|
if let Err(err) = wrapper {
|
|
|
|
error!("parse message failed. {}", err);
|
|
|
|
ws_stream
|
|
|
|
.close(Some(CloseFrame {
|
|
|
|
code: CloseCode::Unsupported,
|
|
|
|
reason: "parse message failed".into(),
|
|
|
|
})).await
|
|
|
|
.map_err(|err| anyhow!(err))?;
|
|
|
|
bail!(err);
|
|
|
|
}
|
|
|
|
let wrapper = wrapper.unwrap();
|
|
|
|
|
|
|
|
let statistics_manager = statistics::StatisticsManager::global().await;
|
|
|
|
|
|
|
|
statistics_manager.update(&wrapper).await;
|
|
|
|
|
|
|
|
let state = statistics_manager.get_state().await;
|
|
|
|
|
|
|
|
debug!("len: {},speed_upload: {}, speed_download: {}, update_direct_speed: {}, download_direct_speed: {}, update_proxy_speed: {}, download_proxy_speed: {}",
|
|
|
|
state.connections,
|
|
|
|
state.speed_upload,
|
|
|
|
state.speed_download,
|
|
|
|
state.direct_upload_speed,
|
|
|
|
state.direct_download_speed,
|
|
|
|
state.proxy_upload_speed,
|
|
|
|
state.proxy_download_speed,
|
|
|
|
);
|
|
|
|
|
|
|
|
let udp_server = udp_server::UdpServer::global().await;
|
|
|
|
let mut buf = [0; 32];
|
|
|
|
buf[0..8].copy_from_slice(&state.direct_upload_speed.to_le_bytes());
|
|
|
|
buf[8..16].copy_from_slice(&state.direct_download_speed.to_le_bytes());
|
|
|
|
buf[16..24].copy_from_slice(&state.proxy_upload_speed.to_le_bytes());
|
|
|
|
buf[24..32].copy_from_slice(&state.proxy_download_speed.to_le_bytes());
|
|
|
|
udp_server.publish_clash(&buf).await;
|
2024-05-02 13:06:47 +08:00
|
|
|
}
|
2024-05-03 12:00:27 +08:00
|
|
|
Message::Close(_) => {
|
|
|
|
println!("Server requested close");
|
|
|
|
break;
|
2023-05-13 19:01:27 +08:00
|
|
|
}
|
2024-05-03 12:00:27 +08:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-05-02 13:06:47 +08:00
|
|
|
|
|
|
|
Ok(())
|
2023-05-13 19:01:27 +08:00
|
|
|
}
|