fix: auto reconnection x2.
All checks were successful
Gitea Actions Demo / build (push) Successful in 2m4s
All checks were successful
Gitea Actions Demo / build (push) Successful in 2m4s
This commit is contained in:
parent
0e77ffe9ba
commit
e3a6c971b4
119
src/main.rs
119
src/main.rs
@ -1,13 +1,14 @@
|
|||||||
use std::{format, time::Duration};
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use anyhow::{anyhow, bail};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use futures_util::{pin_mut, StreamExt};
|
use futures_util::StreamExt;
|
||||||
use log::{error, info};
|
use log::{debug, error};
|
||||||
use tokio::{
|
use tokio::time::sleep;
|
||||||
io::{stdout, AsyncWriteExt},
|
use tokio_tungstenite::{
|
||||||
time::sleep,
|
connect_async,
|
||||||
|
tungstenite::{protocol::{frame::coding::CloseCode, CloseFrame}, Message},
|
||||||
};
|
};
|
||||||
use tokio_tungstenite::connect_async;
|
|
||||||
|
|
||||||
mod clash_conn_msg;
|
mod clash_conn_msg;
|
||||||
mod statistics;
|
mod statistics;
|
||||||
@ -80,66 +81,64 @@ async fn main() {
|
|||||||
async fn pipe(connect_addr: String) -> anyhow::Result<()> {
|
async fn pipe(connect_addr: String) -> anyhow::Result<()> {
|
||||||
let url = url::Url::parse(&connect_addr).map_err(|err| anyhow::anyhow!(err))?;
|
let url = url::Url::parse(&connect_addr).map_err(|err| anyhow::anyhow!(err))?;
|
||||||
|
|
||||||
let (ws_stream, _) = connect_async(url)
|
let (mut ws_stream, _) = connect_async(url)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| anyhow::anyhow!(err))?;
|
.map_err(|err| anyhow::anyhow!(err))?;
|
||||||
println!("WebSocket handshake has been successfully completed");
|
println!("WebSocket handshake has been successfully completed");
|
||||||
|
|
||||||
let (_, read) = ws_stream.split();
|
while let Some(message) = ws_stream.next().await {
|
||||||
|
let message = message?;
|
||||||
|
|
||||||
let ws_to_stdout = {
|
match message {
|
||||||
read.for_each(|message| async {
|
Message::Text(text) => {
|
||||||
if let Err(err) = message {
|
let data = text.into_bytes();
|
||||||
error!("bad message. {}", err);
|
|
||||||
return;
|
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;
|
||||||
}
|
}
|
||||||
|
Message::Close(_) => {
|
||||||
let message = message.unwrap();
|
println!("Server requested close");
|
||||||
let data = message.into_data();
|
break;
|
||||||
|
|
||||||
let wrapper = serde_json::from_slice::<clash_conn_msg::ClashConnectionsWrapper>(&data);
|
|
||||||
|
|
||||||
if let Err(err) = wrapper {
|
|
||||||
error!("parse message failed. {}", err);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
let wrapper = wrapper.unwrap();
|
_ => {}
|
||||||
|
}
|
||||||
let statistics_manager = statistics::StatisticsManager::global().await;
|
}
|
||||||
|
|
||||||
statistics_manager.update(&wrapper).await;
|
|
||||||
|
|
||||||
let state = statistics_manager.get_state().await;
|
|
||||||
|
|
||||||
// stdout()
|
|
||||||
// .write_all(
|
|
||||||
// format!(
|
|
||||||
// "len: {},speed_upload: {}, speed_download: {}, update_direct_speed: {}, download_direct_speed: {}, update_proxy_speed: {}, download_proxy_speed: {}\n",
|
|
||||||
// state.connections,
|
|
||||||
// state.speed_upload,
|
|
||||||
// state.speed_download,
|
|
||||||
// state.direct_upload_speed,
|
|
||||||
// state.direct_download_speed,
|
|
||||||
// state.proxy_upload_speed,
|
|
||||||
// state.proxy_download_speed,
|
|
||||||
// )
|
|
||||||
// .as_bytes(),
|
|
||||||
// )
|
|
||||||
// .await
|
|
||||||
// .unwrap();
|
|
||||||
|
|
||||||
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;
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
pin_mut!(ws_to_stdout);
|
|
||||||
ws_to_stdout.await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user