diff --git a/.gitignore b/.gitignore index 869df07..16d5636 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -Cargo.lock \ No newline at end of file +Cargo.lock +.DS_Store diff --git a/Cargo.toml b/Cargo.toml index 16f6302..88ca44d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,29 @@ -[workspace] -members = [ - "crates/reverseproxy", - "crates/reverseproxy-config", - "crates/reverseproxy-logger", - "crates/reverseproxy-tcp", - "crates/reverseproxy-tor", - "crates/reverseproxy-util", -] \ No newline at end of file +[package] +name = "reverseproxy" +version = "0.1.0" +edition = "2021" +description = "TCP Reverse Proxy written in Rust" +authors = ["Yuki Kishimoto "] +homepage = "https://github.com/yukibtc/reverseproxy" +repository = "https://github.com/yukibtc/reverseproxy.git" +license = "MIT" +readme = "README.md" + +[features] +default = [] +tor = ["libtor"] + +[dependencies] +anyhow = "1.0.58" +clap = { version = "3.2.6", features = ["derive"] } +env_logger = "0.9.0" +futures = "0.3.21" +libtor = { version = "47.8.0+0.4.7.x", features=["vendored-openssl"], optional = true } +log = "0.4.14" +rand = "0.8.5" +tokio = { version = "1.19.2", features = ["full"] } +tokio-socks = "0.5.1" + +[profile.release] +lto = true +codegen-units = 1 diff --git a/Makefile b/Makefile index 9929328..73050a1 100644 --- a/Makefile +++ b/Makefile @@ -40,4 +40,4 @@ clean: $(Q)cargo clean loc: - $(Q)echo "--- Counting lines of .rs files (LOC):" && find crates/ -type f -name "*.rs" -exec cat {} \; | wc -l + $(Q)echo "--- Counting lines of .rs files (LOC):" && find src/ -type f -name "*.rs" -exec cat {} \; | wc -l diff --git a/README.md b/README.md index 4e49783..583220a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ # TCP Reverse Proxy written in Rust -## Install +## Getting started + +### Install from [crates.io](https://crates.io/crates/reverseproxy) + +``` +cargo install reverseproxy +``` + +### Install from source * [Build from source](doc/build.md) @@ -10,24 +18,28 @@ Does NOT support TLS yet! * Forward from local address to Tor hidden service (.onion) using socks5 proxy -``` -reverseproxy 127.0.0.1:8080 torhiddenservice.onion:80 --socks5-proxy 127.0.0.1:9050 -``` - -* Forward from local address to Tor hidden service (.onion) using embedded Tor client (feature in alpha stage) - -``` -reverseproxy 127.0.0.1:8080 torhiddenservice.onion:80 --use-tor -``` + ``` + reverseproxy 127.0.0.1:8080 torhiddenservice.onion:80 --socks5-proxy 127.0.0.1:9050 + ``` * Forward from local address to local network address -``` -reverseproxy 127.0.0.1:8080 othercomputer.local:80 -``` + ``` + reverseproxy 127.0.0.1:8080 othercomputer.local:80 + ``` To get more info use `reverseproxy --help` +### Experimental + +* Forward from local address to Tor hidden service (.onion) using embedded Tor client + + To enable this feature, build the binary using `cargo build --release --features tor` or `cargo install --features tor` + + ``` + reverseproxy 127.0.0.1:8080 torhiddenservice.onion:80 --use-tor + ``` + ## License This project is distributed under the MIT software license - see the [LICENSE](LICENSE) file for details \ No newline at end of file diff --git a/crates/reverseproxy-config/Cargo.toml b/crates/reverseproxy-config/Cargo.toml deleted file mode 100644 index 84de872..0000000 --- a/crates/reverseproxy-config/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "reverseproxy-config" -version = "1.0.0" -edition = "2021" -authors = ["Yuki Kishimoto "] -license = "MIT" - -[features] -default = ["tor"] -tor = [] - -[dependencies] -clap = { version = "3.2.6", features = ["derive"]} \ No newline at end of file diff --git a/crates/reverseproxy-logger/Cargo.toml b/crates/reverseproxy-logger/Cargo.toml deleted file mode 100644 index 9010573..0000000 --- a/crates/reverseproxy-logger/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "reverseproxy-logger" -version = "1.0.0" -edition = "2021" -authors = ["Yuki Kishimoto "] -license = "MIT" - -[dependencies] -env_logger = "0.9.0" -log = "0.4.14" \ No newline at end of file diff --git a/crates/reverseproxy-tcp/Cargo.toml b/crates/reverseproxy-tcp/Cargo.toml deleted file mode 100644 index 6da27ad..0000000 --- a/crates/reverseproxy-tcp/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "reverseproxy-tcp" -version = "1.0.0" -edition = "2021" -authors = ["Yuki Kishimoto "] -license = "MIT" - -[dependencies] -anyhow = "1.0.58" -futures = "0.3.21" -log = "0.4.14" -reverseproxy-util = { path = "../reverseproxy-util" } -tokio = { version = "1.19.2", features = ["full"] } -tokio-socks = "0.5.1" diff --git a/crates/reverseproxy-tor/Cargo.toml b/crates/reverseproxy-tor/Cargo.toml deleted file mode 100644 index e00f4ec..0000000 --- a/crates/reverseproxy-tor/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "reverseproxy-tor" -version = "1.0.0" -edition = "2021" -authors = ["Yuki Kishimoto "] -license = "MIT" - -[dependencies] -libtor = { version = "47.8.0+0.4.7.x", features=["vendored-openssl"] } -log = "0.4.14" diff --git a/crates/reverseproxy-util/Cargo.toml b/crates/reverseproxy-util/Cargo.toml deleted file mode 100644 index 3aff42b..0000000 --- a/crates/reverseproxy-util/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "reverseproxy-util" -version = "1.0.0" -edition = "2021" -authors = ["Yuki Kishimoto "] -license = "MIT" - -[dependencies] -rand = "0.8.5" \ No newline at end of file diff --git a/crates/reverseproxy/Cargo.toml b/crates/reverseproxy/Cargo.toml deleted file mode 100644 index 33d567e..0000000 --- a/crates/reverseproxy/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "reverseproxy" -version = "1.0.0" -edition = "2021" -description = "TCP Reverse Proxy written in Rust" -authors = ["Yuki Kishimoto "] -license = "MIT" -homepage = "https://gitlab.com/p2kishimoto/reverseproxy" -repository = "https://gitlab.com/p2kishimoto/reverseproxy.git" -readme = "README.md" - -[dependencies] -anyhow = "1.0.58" -log = "0.4.14" -reverseproxy-config = { path = "../reverseproxy-config" } -reverseproxy-logger = { path = "../reverseproxy-logger" } -reverseproxy-tcp = { path = "../reverseproxy-tcp" } -reverseproxy-tor = { path = "../reverseproxy-tor" } -tokio = "1.19.2" \ No newline at end of file diff --git a/doc/build.md b/doc/build.md index 27005e6..d242876 100644 --- a/doc/build.md +++ b/doc/build.md @@ -3,7 +3,7 @@ ## Download source code ``` -git clone https://gitlab.com/p2kishimoto/reverseproxy.git && cd reverseproxy +git clone https://github.com/yukibtc/reverseproxy.git && cd reverseproxy ``` ## Verify commits diff --git a/doc/reverseproxy@.service b/doc/reverseproxy@.service index 2634ec9..3464886 100644 --- a/doc/reverseproxy@.service +++ b/doc/reverseproxy@.service @@ -4,7 +4,7 @@ After=network.target [Service] EnvironmentFile=/etc/reverseproxy/%i.conf -ExecStart=/usr/local/bin/reverseproxy --server ${REVERSE_PROXY_SERVER} --forward ${REVERSE_PROXY_FORWARD} --use-tor +ExecStart=/usr/local/bin/reverseproxy ${REVERSE_PROXY_SERVER} ${REVERSE_PROXY_FORWARD} --socks5-proxy 127.0.0.1:9050 Type=simple KillMode=process Restart=always diff --git a/crates/reverseproxy-config/src/lib.rs b/src/config.rs similarity index 100% rename from crates/reverseproxy-config/src/lib.rs rename to src/config.rs diff --git a/crates/reverseproxy-logger/src/lib.rs b/src/logger.rs similarity index 100% rename from crates/reverseproxy-logger/src/lib.rs rename to src/logger.rs diff --git a/crates/reverseproxy/src/main.rs b/src/main.rs similarity index 62% rename from crates/reverseproxy/src/main.rs rename to src/main.rs index 1055366..bcf784f 100644 --- a/crates/reverseproxy/src/main.rs +++ b/src/main.rs @@ -2,24 +2,34 @@ // Distributed under the MIT software license use std::net::SocketAddr; +#[cfg(feature = "tor")] use std::net::{Ipv4Addr, SocketAddrV4}; -use anyhow::Result; -use reverseproxy_config::{Args, Parser}; -use reverseproxy_logger::Logger; -use reverseproxy_tcp::TcpReverseProxy; -use reverseproxy_tor::Tor; +mod config; +mod logger; +mod tcp; +#[cfg(feature = "tor")] +mod tor; +mod util; + +use self::config::{Args, Parser}; +use self::logger::Logger; +use self::tcp::TcpReverseProxy; +#[cfg(feature = "tor")] +use self::tor::Tor; #[tokio::main] -async fn main() -> Result<()> { +async fn main() -> anyhow::Result<()> { let args: Args = Args::parse(); Logger::init(); + #[cfg(feature = "tor")] if args.use_tor { let tor = Tor::new("/tmp/reverseproxy".into(), 19054); tokio::task::spawn_blocking(move || tor.start()); } + #[cfg(feature = "tor")] let socks5_proxy: Option = if args.use_tor { Some(SocketAddr::V4(SocketAddrV4::new( Ipv4Addr::LOCALHOST, @@ -29,6 +39,9 @@ async fn main() -> Result<()> { args.socks5_proxy }; + #[cfg(not(feature = "tor"))] + let socks5_proxy: Option = args.socks5_proxy; + TcpReverseProxy::new(args.local_addr, args.forward_addr, socks5_proxy) .run() .await diff --git a/crates/reverseproxy-tcp/src/lib.rs b/src/tcp/mod.rs similarity index 97% rename from crates/reverseproxy-tcp/src/lib.rs rename to src/tcp/mod.rs index 67321a7..5ab5f03 100644 --- a/crates/reverseproxy-tcp/src/lib.rs +++ b/src/tcp/mod.rs @@ -6,13 +6,13 @@ use std::sync::Arc; use anyhow::Result; use futures::FutureExt; -use reverseproxy_util::random_id; use tokio::io::{copy, split, AsyncWriteExt}; use tokio::net::{TcpListener, TcpStream}; mod socks; -use socks::TpcSocks5Stream; +use self::socks::TpcSocks5Stream; +use crate::util::random_id; pub struct TcpReverseProxy { local_addr: SocketAddr, diff --git a/crates/reverseproxy-tcp/src/socks.rs b/src/tcp/socks.rs similarity index 100% rename from crates/reverseproxy-tcp/src/socks.rs rename to src/tcp/socks.rs diff --git a/crates/reverseproxy-tor/src/lib.rs b/src/tor.rs similarity index 100% rename from crates/reverseproxy-tor/src/lib.rs rename to src/tor.rs diff --git a/crates/reverseproxy-util/src/lib.rs b/src/util.rs similarity index 100% rename from crates/reverseproxy-util/src/lib.rs rename to src/util.rs