feat: adding OpenWRT WAN port speeds.
All checks were successful
Gitea Actions Demo / build (push) Successful in 3m37s

This commit is contained in:
Ivan Li 2024-03-29 18:24:58 +08:00
parent df8cb3bb66
commit 7f8f231edf
8 changed files with 740 additions and 218 deletions

View File

@ -3,7 +3,7 @@
{
"name": "Rust",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/rust:0-1-bullseye",
"image": "mcr.microsoft.com/devcontainers/rust:1-bullseye",
"features": {
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
@ -13,8 +13,9 @@
"extensions": [
"mhutchie.git-graph",
"donjayamanne.githistory",
"GitHub.copilot",
"eamodio.gitlens"
"eamodio.gitlens",
"rust-lang.rust-analyzer",
"Codeium.codeium"
]
}
}

45
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'network-monitor'",
"cargo": {
"args": [
"build",
"--bin=network-monitor",
"--package=network-monitor"
],
"filter": {
"name": "network-monitor",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'network-monitor'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=network-monitor",
"--package=network-monitor"
],
"filter": {
"name": "network-monitor",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"cSpell.words": [
"luci"
],
"lldb.library": "/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/LLDB"
}

653
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,13 +6,16 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.71"
clap = {version = "4.2.7", features = ["derive"]}
env_logger = "0.10.0"
futures-util = "0.3.28"
anyhow = "1.0.81"
clap = {version = "4.5.3", features = ["derive"]}
env_logger = "0.11.3"
futures-util = "0.3.30"
log = "0.4.17"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
tokio = {version = "1.11.0", features = ["full"]}
tokio-tungstenite = "0.18.0"
url = "2.3.1"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
tokio = {version = "1.36.0", features = ["full"]}
tokio-tungstenite = "0.21.0"
url = "2.5.0"
hyper = { version = "1.2.0", features = ["full"] }
http-body-util = "0.1"
hyper-util = { version = "0.1", features = ["full"] }

View File

@ -12,6 +12,9 @@ use tokio_tungstenite::connect_async;
mod clash_conn_msg;
mod statistics;
mod udp_server;
mod wan;
use wan::poll_wan_traffic;
#[derive(Parser)]
#[clap(
@ -26,8 +29,10 @@ struct Args {
default_value = "ws://192.168.31.1:9090/connections?token=123456"
)]
clash_url: String,
#[clap(short, long, default_value = "17890")]
#[clap(short = 'p', long, default_value = "17890")]
listen_port: u16,
#[clap(short, long, default_value = "http://192.168.31.1/cgi-bin/luci")]
luci_url: String,
}
#[tokio::main]
@ -38,6 +43,10 @@ async fn main() {
let args = Args::parse();
tokio::spawn(async move {
poll_wan_traffic(args.luci_url.as_str()).await.unwrap();
});
let connect_addr = args.clash_url;
loop {
@ -99,7 +108,7 @@ async fn pipe(connect_addr: String) {
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(&buf).await;
udp_server.publish_clash(&buf).await;
})
};

View File

@ -4,13 +4,13 @@ use std::{
sync::Arc,
};
use clap::Parser;
use log::{error, info};
use tokio::{
net::UdpSocket,
sync::{Mutex, OnceCell},
time::Instant,
};
use clap::Parser;
use crate::Args;
@ -24,6 +24,7 @@ pub struct UdpServer {
listen_addr: Ipv4Addr,
listen_port: u16,
clients: Arc<Mutex<HashMap<SocketAddr, Client>>>,
tx_buffer: Arc<Mutex<[u8; 48]>>,
}
impl UdpServer {
@ -54,6 +55,7 @@ impl UdpServer {
listen_addr,
listen_port,
clients: Arc::new(Mutex::new(HashMap::new())),
tx_buffer: Arc::new(Mutex::new([0u8; 48])),
}
}
@ -95,7 +97,25 @@ impl UdpServer {
}
}
pub async fn publish(&self, buf: &[u8]) {
pub async fn publish_clash(&self, buf: &[u8]) {
let mut tx_buffer = self.tx_buffer.lock().await;
tx_buffer[..32].copy_from_slice(buf);
let buf = tx_buffer.clone();
drop(tx_buffer);
self.publish(&buf).await;
}
pub async fn publish_wan(&self, buf: &[u8]) {
let mut tx_buffer = self.tx_buffer.lock().await;
tx_buffer[32..].copy_from_slice(buf);
let buf = tx_buffer.clone();
drop(tx_buffer);
self.publish(&buf).await;
}
async fn publish(&self, buf: &[u8]) {
let mut to_remove = Vec::new();
let mut clients = self.clients.lock().await;
@ -103,7 +123,7 @@ impl UdpServer {
if client.last_seen.elapsed().as_secs() > 10 {
to_remove.push(addr.clone());
} else {
if let Err(err) = client.socket.send(buf).await {
if let Err(err) = client.socket.send(&buf).await {
error!("Failed to send data to {}: {}", addr, err);
} else {
info!("Sent data to {}", addr);

187
src/wan.rs Normal file
View File

@ -0,0 +1,187 @@
use std::time::Duration;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Result;
use http_body_util::BodyExt;
use http_body_util::Empty;
use hyper::StatusCode;
use hyper::{
body::{Buf, Bytes},
Request,
};
use hyper_util::rt::TokioIo;
use log::debug;
use serde::{Deserialize, Serialize};
use tokio::net::TcpStream;
use tokio::time::sleep;
use crate::udp_server;
pub type OpenWRTIFaces = Vec<OpenWRTIFace>;
pub async fn poll_wan_traffic(luci_url: &str) -> Result<()> {
// Parse our URL...
let url = format!("{}/admin/network/iface_status/wan", luci_url).parse::<hyper::Uri>()?;
// Get the host and the port
let host = url.host().expect("uri has no host");
let port = url.port_u16().unwrap_or(80);
let address = format!("{}:{}", host, port);
let mut cookies_str = String::new();
// Open a TCP connection to the remote host
loop {
let stream = TcpStream::connect(address.clone()).await?;
// Use an adapter to access something implementing `tokio::io` traits as if they implement
// `hyper::rt` IO traits.
let io = TokioIo::new(stream);
// Create the Hyper client
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
// Spawn a task to poll the connection, driving the HTTP state
tokio::task::spawn(async move {
if let Err(err) = conn.await {
println!("Connection failed: {:?}", err);
}
});
loop {
if sender.is_closed() {
break;
}
if !sender.is_ready() {
sleep(Duration::from_millis(100)).await;
continue;
}
let poll_req = generate_poll_wan_req(luci_url, &cookies_str)?;
// Await the response...
let res = sender.send_request(poll_req.clone()).await?;
debug!("Response status: {}", res.status());
match res.status() {
StatusCode::FORBIDDEN | StatusCode::UNAUTHORIZED => {
cookies_str = login(luci_url).await?;
continue;
}
_ => {}
}
// asynchronously aggregate the chunks of the body
let body = res.collect().await?.aggregate();
// try to parse as json with serde_json
let interfaces: OpenWRTIFaces = serde_json::from_reader(body.reader())?;
// println!(
// "speed: ↑ {}, ↓ {}",
// interfaces.first().unwrap().tx_bytes,
// interfaces.first().unwrap().rx_bytes,
// );
let wan = interfaces.first().unwrap();
let udp_server = udp_server::UdpServer::global().await;
let mut buf = [0; 16];
buf[0..8].copy_from_slice(&wan.tx_bytes.to_le_bytes());
buf[8..16].copy_from_slice(&wan.rx_bytes.to_le_bytes());
udp_server.publish_wan(&buf).await;
sleep(Duration::from_secs(1)).await;
}
}
}
async fn login(luci_url: &str) -> Result<String> {
let url = format!("{}/admin/network/iface_status/wan", luci_url).parse::<hyper::Uri>()?;
let host = url.host().expect("uri has no host");
let port = url.port_u16().unwrap_or(80);
let address = format!("{}:{}", host, port);
let stream = TcpStream::connect(address).await?;
let io = TokioIo::new(stream);
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
tokio::task::spawn(async move {
if let Err(err) = conn.await {
println!("Connection failed: {:?}", err);
}
});
let login_req = generate_login_req(luci_url)?;
let res = sender.send_request(login_req.clone()).await?;
if res.status() == StatusCode::FORBIDDEN {
bail!("Login failed, got status: {}", res.status());
}
let cookies = res.headers().get_all(hyper::header::SET_COOKIE);
let cookies = cookies
.iter()
.map(|cookie| cookie.to_str().unwrap().split(';').next());
let cookies = cookies.filter_map(|cookie| cookie);
let cookies_str = cookies.collect::<Vec<_>>().join("; ");
Ok(cookies_str)
}
fn generate_poll_wan_req(luci_url: &str, cookie: &str) -> Result<Request<Empty<Bytes>>> {
let url = format!("{}/admin/network/iface_status/wan", luci_url).parse::<hyper::Uri>()?;
let authority = url.authority().unwrap().clone();
let target = url.path_and_query().unwrap().clone();
debug!("Polling WAN traffic. Cookie: {:?}", cookie);
return Request::builder()
.uri(target)
.header(hyper::header::HOST, authority.as_str())
.header(hyper::header::CONNECTION, "Keep-Alive")
.header(hyper::header::COOKIE, cookie)
.body(Empty::<Bytes>::new())
.map_err(|e| anyhow!(e));
}
fn generate_login_req(luci_url: &str) -> Result<Request<String>> {
let url = format!("{}/admin/network/iface_status/wan", luci_url).parse::<hyper::Uri>()?;
let authority = url.authority().unwrap().clone();
let target = url.path_and_query().unwrap().clone();
let username = "root";
let password = "adg094715";
let login_params = format!("luci_username={}&luci_password={}", username, password);
let body = login_params;
return Request::builder()
.uri(target)
.method("POST")
.header(hyper::header::HOST, authority.as_str())
.header(
hyper::header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header(hyper::header::CONNECTION, "Keep-Alive")
.body(body)
.map_err(|e| anyhow!(e));
}
#[derive(Serialize, Deserialize)]
pub struct OpenWRTIFace {
rx_bytes: i64,
tx_bytes: i64,
}
impl Default for OpenWRTIFace {
fn default() -> Self {
Self {
rx_bytes: 0,
tx_bytes: 0,
}
}
}