Skip to content

Commit

Permalink
Merge pull request #1 from gauravssnl/develop
Browse files Browse the repository at this point in the history
Add command line flags for the app
  • Loading branch information
gauravssnl authored Dec 23, 2022
2 parents c1821cb + c682ad2 commit 85bf562
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 201 deletions.
104 changes: 103 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "rserver"
version = "0.1.5"
version = "0.1.6"
authors = ["Gaurav <[email protected]>"]
edition = "2018"
license = "MIT"
description = "A TCP server for intercepting requests, modifying request headers, and replacing responses."
description = "Asynchronous TCP server for intercepting requests, modifying request headers, and replacing responses."
readme = "README.md"
repository = "https://github.com/gauravssnl/rserver"
keywords = ["server", "socket", "TCP"]
Expand All @@ -15,4 +15,5 @@ categories = ["web-programming::http-server", "command-line-utilities"]
[dependencies]
futures = "0.3.24"
tokio = { version = "1.21.0", features = ["full"] }
urlparse = "0.7.3"
urlparse = "0.7.3"
structopt = { version = "0.3", default-features = false }
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,47 @@ cargo install rserver

# Example

How to use the RServer to intercept/sniff TCP requests. RServer currently runs on 8081 port by default.
How to use the RServer to intercept/sniff TCP requests. RServer currently runs on 8080 port by default.

To run RServer, use the folowing command :

```shell
rserver
```

Please set the browser/system proxy as host 127.0.0.1 and port 8081 (default port of RServer) to use Rserver for intercepting all requests.
Please set the browser/system proxy as host 127.0.0.1 and port 8080 (default port of RServer) to use Rserver for intercepting all requests.

If you directly want to test RServer installation without doing the above step, please run the below command :
```shell
https_proxy=127.0.0.1:8081 curl https://www.google.com
https_proxy=127.0.0.1:8080 curl https://www.google.com
```

# Usage

rserver [OPTIONS]

FLAGS:
--help Prints help information
-V, --version Prints version information

OPTIONS:
--enable-proxy <enable-proxy> Enable proxy flag [default: false]
-h, --host <host> Server host [default: 127.0.0.1]
-p, --port <port> Server port [default: 8080]
--proxy-host <proxy-host> Proxy host
--proxy-port <proxy-port> Proxy port

#### To use rsever Rust library , please see the below example :

```rust
use rserver::config::Config;
use rserver::Config;
use rserver::Server;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::default();
rserver::start_server(&config).await
let server = Server::new(config);
server.start().await
}

```
Expand Down Expand Up @@ -94,7 +111,7 @@ fn read_stream(stream: &mut TcpStream) -> (Vec<u8>, usize) {


To-Do
- [] Add command line flags for server config

- [ ] Modifying/replacing Request Headers
- [ ] Modifying/replacing Reponse Headers

34 changes: 34 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(
name = "rserver",
about = "Asynchronous TCP server for intercepting requests, modifying request headers, and replacing responses"
)]
pub struct CliOption {
/// Server host
#[structopt(short, long, default_value = "127.0.0.1")]
pub host: String,

/// Server port
#[structopt(short, long, default_value = "8080")]
pub port: i32,

/// Enable proxy flag
#[structopt(long, default_value = "false")]
pub enable_proxy: String,

/// Proxy host
#[structopt(long, required_if("enable-proxy", "true"))]
pub proxy_host: Option<String>,

/// Proxy port
#[structopt(long, required_if("enable-proxy", "true"))]
pub proxy_port: Option<i32>,
}

impl CliOption {
pub fn parse() -> Self {
Self::from_args()
}
}
26 changes: 24 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::CliOption;

/// RServer's Config struct.
#[derive(Debug, Clone)]
pub struct Config {
pub host: String,
pub port: i32,
Expand All @@ -7,7 +11,7 @@ pub struct Config {
}

impl Config {
fn new(
pub fn new(
host: String,
port: i32,
enable_proxy: bool,
Expand All @@ -28,10 +32,28 @@ impl Default for Config {
fn default() -> Self {
Self {
host: String::from("127.0.0.1"),
port: 8081,
port: 8080,
enable_proxy: false,
proxy_host: String::default(),
proxy_port: i32::default(),
}
}
}

impl From<CliOption> for Config {
fn from(cli_option: CliOption) -> Self {
Self {
host: cli_option.host,
port: cli_option.port,
enable_proxy: matches!(cli_option.enable_proxy.as_str(), "true"),
proxy_host: match cli_option.proxy_host {
Some(p) => p,
_ => String::default(),
},
proxy_port: match cli_option.proxy_port {
Some(p) => p,
_ => i32::default(),
},
}
}
}
Loading

0 comments on commit 85bf562

Please sign in to comment.