From 81494f801ba67e289b3c902acdcbc4dceda1c846 Mon Sep 17 00:00:00 2001 From: neevek Date: Sat, 15 Apr 2023 22:44:32 +0800 Subject: [PATCH] bump the version to 0.4.0 and update README --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 124 +++++++++++++++++++++++++++++++++++++++++++--- src/bin/rstund.rs | 14 +++++- src/client.rs | 6 +-- src/server.rs | 8 +-- 6 files changed, 139 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c3ba1b..08b1463 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -959,7 +959,7 @@ dependencies = [ [[package]] name = "rstun" -version = "0.3.4" +version = "0.4.0" dependencies = [ "android_logger", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index f75a1c8..6cd9f0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rstun" -version = "0.3.4" +version = "0.4.0" edition = "2021" [lib] diff --git a/README.md b/README.md index 1d63051..952196d 100644 --- a/README.md +++ b/README.md @@ -22,23 +22,135 @@ Usage ``` rstund \ --addr 0.0.0.0:6060 \ - --downstream 8800 \ + --downstreams 8800 \ --password 123456 \ - --cert path/to/certificate.der \ - --key path/to/priv_key.der + --cert path/to/cert.der \ + --key path/to/key.der ``` -`addr` specifies the ip:port that the server is listening on, `downstream` specifies a TCP port which traffic from the client through the tunnel will be relayed to, this is applicable for `OUT` mode tunnels only. For `cert` and `key`, the requirement is that they must be in DER format, I use a self signed certificate for testing. Note currently the certificate is checked bytewise by the client, that is why the same certificate must be specified for the client. + - `addr` specifies the ip:port that the server is listening on. + - `downstreams` specifies a TCP port which traffic from the client through the tunnel will be relayed to on the server, this is applicable for `OUT` mode tunnels only, multiple space-separated downstreams can be set. Note this argument is optional, if it is not specified, all open ports of the server are exposed to the clients through the tunnel. So make sure to specify downstreams if exposing all open ports of the server is not desired. + - `password`, password of the server, the client `rstunc` is required to send the this password to successfully build a tunnel with the server. + - `cert` and `key` are certificate and private key for the domain of the server and they must be in `DER` format (binary format), self-signed certificate is allowed, but you will have to connect to the server using IP address and the certificate will also be required by `rstunc` for verification in this case (see below). Anyway, getting a certificate for your domain from a trusted CA and connecting to the server using domain name is always recommended. Note `cert` and `key` are optional, if they are not specified, the domain `localhost` is assumed and a self-signed certificate is generated on the fly, but this is for TEST only, Man-In-The-Middle attack can occur with such setting, so make sure **it is only used for TEST**! * Start the client + ``` rstunc --mode OUT \ --server-addr 1.2.3.4:6060 \ --password 123456 \ - --cert path/to/certificate.der \ + --cert path/to/cert.der \ --addr-mapping 0.0.0.0:9900^8800 ``` -For the arguments, I think only `addr-mapping` needs some explanation, this is an address mapping between two ip:port pairs separated by the `^` character, the format is `[ip:]port^[ip:]port`, in the example above, a local port `9900` is mapped to the remote port `8800` of the `1.2.3.4` host that runs `rstund`. + - `mode` here is `OUT` for `TunnelOut`, for securing traffic from local to the server through the tunnel. + - `server-addr`, a TCP server that runs locally for accepting requests and forwarding the all traffic to the server through the tunnel. + - `password`, same as that for the server. + - `cert`, see explanation above for the `rstund`. Note this is also optional if connecting to the server with a domain name, or the server `rstund` runs with a auto-generated self-signed certificate (see the TEST example below). + - `addr-mapping` is an address mapping between two `ip:port` pairs separated by the `^` character, the format is `[ip:]port^[ip:]port`, in the example above, a local port `9900` is mapped to the remote port `8800` of the `1.2.3.4` server that runs `rstund`. i.e. all traffic from the local port `9900` will be forwarded to the remote port `8800` through the tunnel. + +* Simple TEST example + +The following commands run a server and a client that connects to the server in their simplest ways: + + +``` +# Remote: run the server with auto-generated self-signed certificate +rstund -l 9000 -p 1234 + +# Local: connect to the server (127.0.0.1:9000) and bind the local port 6000 to remote port 7000 +rstunc -m OUT -r 127.0.0.1:9000 -p 1234 -a 0.0.0.0:6000^7000 + +``` + +* Complete options for `rstund` + +``` +rstun 0.4.0 + +USAGE: + rstund [OPTIONS] --addr --password + +OPTIONS: + -l, --addr + Address ([ip:]port pair) to listen on + + -d, --downstreams + Exposed downstreams as the receiving end of the tunnel, e.g. -d [ip:]port, The entire + local network is exposed through the tunnel if empty + + -p, --password + Password of the tunnel server + + -c, --cert + Path to the certificate file in DER format, if empty, a self-signed certificate with the + domain "localhost" will be used [default: ] + + -k, --key + Path to the key file in DER format, can be empty if no cert is provided [default: ] + + -t, --threads + Threads to run async tasks [default: 0] + + -w, --max-idle-timeout-ms + Max idle timeout for the connection [default: 40000] + + -L, --loglevel + [default: I] [possible values: T, D, I, W, E] + + -h, --help + Print help information + + -V, --version + Print version information +``` + +* Complete options for `rstunc` + +``` +rstun 0.4.0 + +USAGE: + rstunc [OPTIONS] --mode --server-addr --password --addr-mapping + +OPTIONS: + -m, --mode + Create a tunnel running in IN or OUT mode [possible values: IN, OUT] + + -r, --server-addr + Address ([:port] pair) of rstund, default port is 3515 + + -p, --password + Password to connect with rstund + + -a, --addr-mapping + LOCAL and REMOTE mapping in [ip:]port^[ip:]port format, e.g. 8080^0.0.0.0:9090 + + -c, --cert + Path to the certificate file in DER format, only needed for self signed certificate + [default: ] + + -e, --cipher + Preferred cipher suite [default: chacha20-poly1305] [possible values: chacha20-poly1305, + aes-256-gcm, aes-128-gcm] + + -t, --threads + Threads to run async tasks [default: 0] + + -w, --wait-before-retry-ms + Wait time before trying [default: 5000] + + -i, --max-idle-timeout-ms + Max idle timeout for the connection [default: 150000] + + -l, --loglevel + Log level [default: I] [possible values: T, D, I, W, E] + + -h, --help + Print help information + + -V, --version + Print version information +``` License ------- diff --git a/src/bin/rstund.rs b/src/bin/rstund.rs index 0b4cfba..1dbbbad 100644 --- a/src/bin/rstund.rs +++ b/src/bin/rstund.rs @@ -24,17 +24,27 @@ fn main() { .build() .unwrap() .block_on(async { - run(args).await.unwrap(); + run(args) + .await + .map_err(|e| { + error!("{}", e); + }) + .ok(); }) } async fn run(mut args: RstundArgs) -> Result<()> { + if !args.addr.contains(':') { + args.addr = format!("127.0.0.1:{}", args.addr); + } + let mut downstreams = Vec::::new(); for d in &mut args.downstreams { if d.starts_with("0.0.0.0:") { *d = d.replace("0.0.0.0:", "127.0.0.1:"); } + if !d.contains(':') { *d = format!("127.0.0.1:{}", d); } @@ -62,7 +72,7 @@ async fn run(mut args: RstundArgs) -> Result<()> { #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] struct RstundArgs { - /// Address (ip:port pair) to listen on + /// Address ([ip:]port pair) to listen on #[clap(short = 'l', long, display_order = 1)] addr: String, diff --git a/src/client.rs b/src/client.rs index 1fb15bf..4b05a99 100644 --- a/src/client.rs +++ b/src/client.rs @@ -243,7 +243,7 @@ impl Client { } async fn serve_outgoing(&mut self, local_conn_receiver: &mut Receiver>) { - self.post_tunnel_log("start serving in [Out] mode..."); + self.post_tunnel_log("start serving in [TunnelOut] mode..."); self.report_traffic_data_in_background(); @@ -612,8 +612,8 @@ impl rustls::client::ServerCertVerifier for InsecureCertVerifier { _ocsp_response: &[u8], _now: SystemTime, ) -> Result { - warn!("================================= WARNING ============================================"); - warn!("====== Connecting to a server without verifying its certificate is DANGEROUS!!! ======"); + warn!("======================================= WARNING ======================================"); + warn!("= Connecting to a server without verifying its certificate is DANGEROUS!!! ="); warn!("= Provide the self-signed certificate for verification or connect with a domain name ="); warn!("======================= Be cautious, this is for TEST only!!! ========================"); Ok(ServerCertVerified::assertion()) diff --git a/src/server.rs b/src/server.rs index 0d6f68d..0ec8191 100644 --- a/src/server.rs +++ b/src/server.rs @@ -304,10 +304,10 @@ impl Server { fn read_cert_and_key(cert_path: &str, key_path: &str) -> Result<(Certificate, PrivateKey)> { let (cert, key) = if cert_path.is_empty() { - warn!("=========================== WARNING ============================"); - warn!("No valid certificate path is provided, a self-signed certificate"); - warn!(" for the domain \"localhost\" is generated."); - warn!("============== Be cautious, this is for TEST only!!! ============"); + warn!("============================= WARNING =============================="); + warn!("= No valid certificate path is provided, a self-signed certificate ="); + warn!("= for the domain \"localhost\" is generated. ="); + warn!("============== Be cautious, this is for TEST only!!! ==============="); let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()])?; let key = cert.serialize_private_key_der(); let cert = cert.serialize_der()?;