| Crates.io | fortifynet_proxy |
| lib.rs | fortifynet_proxy |
| version | 2.0.0 |
| created_at | 2024-02-28 12:08:53.689368+00 |
| updated_at | 2025-01-14 16:58:12.975826+00 |
| description | A flexible asynchronous proxy server library written in Rust. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1156399 |
| size | 83,424 |
FortifyNet Proxy is a powerful and flexible asynchronous proxy server library written in Rust. It's designed to be a robust and reusable foundation for building various types of proxy servers, including HTTP, HTTPS, and SOCKS5, with a focus on performance, security, and ease of use.
tokio for handling numerous concurrent connections with optimal efficiency.hyper and tokio-rustls.tokio-socks, enabling advanced network configurations.warp for live monitoring of the server.ProxyConfig struct to customize various proxy server settings.To use FortifyNet Proxy in your Rust project, add the following line to your Cargo.toml file:
[dependencies]
fortifynet_proxy = "2.0.0" # Or the latest Version
tokio = { version = "1", features = ["full"] }
hyper = { version = "0.14", features = ["client","http1","server","tcp"] }
log = "0.4"
env_logger = "0.10"
thiserror = "1"
anyhow = "1"
rustls = "0.21"
tokio-rustls = "0.24"
tokio-socks = "0.3"
url = "2.5"
warp = "0.3"
rustls-pemfile = "1.1"
Here's how you can quickly set up a basic HTTP proxy server with FortifyNet Proxy:
Set up your main file (main.rs)
use fortifynet_proxy::{start_proxy_server, ProxyConfig};
use log::info;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Create a proxy configuration with default values
let config = ProxyConfig {
ip_address: "127.0.0.1".to_string(),
port: 8080,
authentication: false,
username: "admin".to_string(),
password: "password".to_string(),
cache_enabled: true,
socks5_address: None,
https_enabled: false,
certificate_path: None,
private_key_path: None,
target_address: Some("http://localhost".to_string()) // target for non-socks connection
};
info!("Starting Proxy server with configuration: {:?}", config);
// Start the proxy server with the provided configuration
start_proxy_server(config).await?;
Ok(())
}
Start the Proxy Server:
Run cargo run in your terminal. The proxy server will start listening for connections. You can monitor the server's output for logs and metrics.
Configure Your Client:
Web Browser:
127.0.0.18080 (or the port you configured)curl Command:
curl -v --proxy http://127.0.0.1:8080 http://www.example.com
HTTPS Proxy:
If you enable https_enabled then your proxy will be listening to https requests and you need to configure your client for https proxy, you also need to create the certificate and key files.
curl -v --proxy https://127.0.0.1:8080 https://www.example.com
Access Metrics Dashboard
Open a web browser and navigate to http://127.0.0.1:<port + 1000>. For the above example, this is http://127.0.0.1:9080.
To secure your proxy server with basic authentication:
let config = ProxyConfig {
ip_address: "127.0.0.1".to_string(),
port: 8080,
authentication: true, // Enable authentication
username: "admin".to_string(),
password: "password".to_string(),
cache_enabled: true,
socks5_address: None,
https_enabled: false,
certificate_path: None,
private_key_path: None,
target_address: None,
};
When authentication is enabled, the user will have to provide the authentication header.
To forward your requests through a SOCKS5 proxy:
let config = ProxyConfig {
ip_address: "127.0.0.1".to_string(),
port: 8080,
authentication: false,
username: "".to_string(),
password: "".to_string(),
cache_enabled: true,
socks5_address: Some("127.0.0.1:1080".to_string()), // Using SOCKS5
https_enabled: false,
certificate_path: None,
private_key_path: None,
target_address: None,
};
Use the curl command with the --socks5 option.
curl -v --socks5 127.0.0.1:1080 http://www.example.com
To enable HTTPS for secure connections, you need to specify the certificate and key file paths
let config = ProxyConfig {
ip_address: "127.0.0.1".to_string(),
port: 8080,
authentication: false,
username: "".to_string(),
password: "".to_string(),
cache_enabled: true,
socks5_address: None,
https_enabled: true, // Enable HTTPS
certificate_path: Some("cert.pem".to_string()),
private_key_path: Some("key.pem".to_string()),
target_address: None,
};
You will also need to generate your own certificates and key files.
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'
If you are not using SOCKS5 and want to use direct connection and forward your request to a specific address you can use the target_address field.
let config = ProxyConfig {
ip_address: "127.0.0.1".to_string(),
port: 8080,
authentication: false,
username: "".to_string(),
password: "".to_string(),
cache_enabled: true,
socks5_address: None,
https_enabled: false,
certificate_path: None,
private_key_path: None,
target_address: Some("http://www.google.com".to_string()),
};
The ProxyConfig struct offers several configuration options, allowing you to customize your proxy server:
ip_address: Binds the proxy to a specific IP address (e.g., "0.0.0.0" for all interfaces).port: Specifies the port on which the proxy server listens.authentication: Enables or disables basic authentication for the proxy.username and password: Set the username and password for authentication (if enabled).cache_enabled: Enables or disables response caching.socks5_address: Sets an optional SOCKS5 server address for routing traffic.https_enabled: Enables or disables HTTPS support.certificate_path and private_key_path: Set the paths to the SSL certificates and key file if HTTPS is enabled.target_address: Sets the target address for direct connections.http://127.0.0.1:<port + 1000> in your browser to view real-time metrics about the proxy server, including total requests, average response times, cache hit/miss rates, and error counts.anyhow and comprehensive logging.Licensed under the MIT License. See the LICENSE file for details.