sock5 gui part

This commit is contained in:
open-trade
2022-01-05 18:34:30 +08:00
parent 12ab22e048
commit c0b6367137
7 changed files with 111 additions and 6 deletions

View File

@@ -89,6 +89,7 @@ pub enum Data {
NatType(Option<i32>),
ConfirmedKey(Option<(Vec<u8>, Vec<u8>)>),
RawMessage(Vec<u8>),
Socks(Option<config::Socks5Server>),
FS(FS),
Test,
}
@@ -192,6 +193,20 @@ async fn handle(data: Data, stream: &mut Connection) {
};
allow_err!(stream.send(&Data::ConfirmedKey(out)).await);
}
Data::Socks(s) => match s {
None => {
allow_err!(stream.send(&Data::Socks(Config::get_socks())).await);
}
Some(data) => {
if data.proxy.is_empty() {
Config::set_socks(None);
} else {
Config::set_socks(Some(data));
}
crate::rendezvous_mediator::RendezvousMediator::restart();
log::info!("socks updated");
}
},
Data::Config((name, value)) => match value {
None => {
let value;
@@ -467,6 +482,41 @@ pub async fn get_nat_type(ms_timeout: u64) -> i32 {
.unwrap_or(Config::get_nat_type())
}
#[inline]
async fn get_socks_(ms_timeout: u64) -> ResultType<Option<config::Socks5Server>> {
let mut c = connect(ms_timeout, "").await?;
c.send(&Data::Socks(None)).await?;
if let Some(Data::Socks(value)) = c.next_timeout(ms_timeout).await? {
Config::set_socks(value.clone());
Ok(value)
} else {
Ok(Config::get_socks())
}
}
pub async fn get_socks_async(ms_timeout: u64) -> Option<config::Socks5Server> {
get_socks_(ms_timeout).await.unwrap_or(Config::get_socks())
}
#[tokio::main(flavor = "current_thread")]
pub async fn get_socks() -> Option<config::Socks5Server> {
get_socks_async(1_000).await
}
#[tokio::main(flavor = "current_thread")]
pub async fn set_socks(value: config::Socks5Server) -> ResultType<()> {
Config::set_socks(if value.proxy.is_empty() {
None
} else {
Some(value.clone())
});
connect(1_000, "")
.await?
.send(&Data::Socks(Some(value)))
.await?;
Ok(())
}
/*
static mut SHARED_MEMORY: *mut i64 = std::ptr::null_mut();