Skip to content

Commit

Permalink
Fix #39
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Dec 8, 2023
1 parent 83055c9 commit 1856e3e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"method": "none",
"password": "password",
"tunnel_path": "/secret-tunnel-path/",

"server_settings": {
"disable_tls": false,
"manage_clients": {
Expand All @@ -19,14 +18,15 @@
"listen_host": "0.0.0.0",
"listen_port": 443
},

"client_settings": {
"disable_tls": false,
"client_id": "33959370-71e0-401d-9746-cda471fc5926",
"server_host": "123.45.67.89",
"server_port": 443,
"server_domain": "example.com",
"cafile": "",
"listen_user": "",
"listen_password": "",
"listen_host": "127.0.0.1",
"listen_port": 1080
}
Expand Down
27 changes: 25 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use bytes::BytesMut;
use futures_util::{SinkExt, StreamExt};
use socks5_impl::{
protocol::{Address, Reply},
server::{auth::NoAuth, connection::connect::NeedReply, ClientConnection, Connect, IncomingConnection, Server},
server::{
auth::{NoAuth, UserKeyAuth},
connection::connect::NeedReply,
AuthAdaptor, ClientConnection, Connect, IncomingConnection, Server,
},
};
use std::{
net::SocketAddr,
Expand Down Expand Up @@ -42,9 +46,28 @@ where
log::trace!("with following settings:");
log::trace!("{}", serde_json::to_string_pretty(config)?);

let client = config.client.as_ref().ok_or("client")?;

let listen_user = client.listen_user.as_deref().filter(|s| !s.is_empty());
if let Some(user) = listen_user {
let listen_password = client.listen_password.as_deref().unwrap_or("");
let key = UserKeyAuth::new(user, listen_password);
_run_client(config, Arc::new(key), exiting_flag, callback).await?;
} else {
_run_client(config, Arc::new(NoAuth), exiting_flag, callback).await?;
}
Ok(())
}

async fn _run_client<F, O>(config: &Config, auth: AuthAdaptor<O>, exiting_flag: Option<Arc<AtomicBool>>, callback: Option<F>) -> Result<()>
where
F: FnOnce(SocketAddr) + Send + Sync + 'static,
O: Send + Sync + 'static,
{
let client = config.client.as_ref().ok_or("client")?;
let addr = SocketAddr::new(client.listen_host.parse()?, client.listen_port);
let server = Server::bind(addr, std::sync::Arc::new(NoAuth)).await?;

let server = Server::<O>::bind(addr, auth).await?;

if let Some(callback) = callback {
callback(server.local_addr()?);
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub struct Client {
pub cafile: Option<PathBuf>,
pub listen_host: String,
pub listen_port: u16,
pub listen_user: Option<String>,
pub listen_password: Option<String>,
#[serde(skip)]
pub cache_dns: bool,
}
Expand Down

0 comments on commit 1856e3e

Please sign in to comment.