Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Nov 25, 2022
1 parent 786f5d7 commit 4d97f59
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 109 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
Cargo.lock
.DS_Store
38 changes: 29 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
[workspace]
members = [
"crates/reverseproxy",
"crates/reverseproxy-config",
"crates/reverseproxy-logger",
"crates/reverseproxy-tcp",
"crates/reverseproxy-tor",
"crates/reverseproxy-util",
]
[package]
name = "reverseproxy"
version = "0.1.0"
edition = "2021"
description = "TCP Reverse Proxy written in Rust"
authors = ["Yuki Kishimoto <[email protected]>"]
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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
13 changes: 0 additions & 13 deletions crates/reverseproxy-config/Cargo.toml

This file was deleted.

10 changes: 0 additions & 10 deletions crates/reverseproxy-logger/Cargo.toml

This file was deleted.

14 changes: 0 additions & 14 deletions crates/reverseproxy-tcp/Cargo.toml

This file was deleted.

10 changes: 0 additions & 10 deletions crates/reverseproxy-tor/Cargo.toml

This file was deleted.

9 changes: 0 additions & 9 deletions crates/reverseproxy-util/Cargo.toml

This file was deleted.

19 changes: 0 additions & 19 deletions crates/reverseproxy/Cargo.toml

This file was deleted.

2 changes: 1 addition & 1 deletion doc/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 19 additions & 6 deletions crates/reverseproxy/src/main.rs → src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketAddr> = if args.use_tor {
Some(SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::LOCALHOST,
Expand All @@ -29,6 +39,9 @@ async fn main() -> Result<()> {
args.socks5_proxy
};

#[cfg(not(feature = "tor"))]
let socks5_proxy: Option<SocketAddr> = args.socks5_proxy;

TcpReverseProxy::new(args.local_addr, args.forward_addr, socks5_proxy)
.run()
.await
Expand Down
4 changes: 2 additions & 2 deletions crates/reverseproxy-tcp/src/lib.rs → src/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4d97f59

Please sign in to comment.