Rust is a great language for network programming thanks to its focus on performance and safety. Here's how we can build a basic HTTP proxy in just 40 lines of Rust code.
First we import the crates we need:
use std::io::{copy, Error};
use std::net::TcpListener;
use std::thread;
use reqwest;
Next we create a handle_client function to process each client request:
fn handle_client(mut stream: TcpStream) -> Result<(), Error> {
// ... proxy logic
}
It will read the incoming request:
let mut buffer = [0; 1024];
stream.read(&mut buffer)?;
let request = String::from_utf8_lossy(&buffer[..]);
Extract the URL:
let url = request.split_whitespace()[1];
Make the proxied request:
let mut res = reqwest::get(url)?;
And pipe back the response:
copy(&mut res, &mut stream)?;
Our main function sets up the listener:
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
// accept connections and process
for stream in listener.incoming() {
handle_client(stream.unwrap());
}
}
The full proxy in 40 lines:
use std::io::{copy, Error};
use std::net::TcpListener;
use std::thread;
use reqwest;
fn handle_client(mut stream: TcpStream) -> Result<(), Error> {
let mut buffer = [0; 1024];
stream.read(&mut buffer)?;
let request = String::from_utf8_lossy(&buffer[..]);
let url = request.split_whitespace()[1];
println!("Request: {}", url);
let mut res = reqwest::get(url)?;
copy(&mut res, &mut stream)?;
Ok(())
}
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
for stream in listener.incoming() {
handle_client(stream.unwrap()).unwrap();
}
}
This gives you a basic Rust HTTP proxy. There is still room for improvement but it shows the power of Rust for network programming.
This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.
Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.
Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.
Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.
The whole thing can be accessed by a simple API like below in any programming language.
In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so:
curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"
We have a running offer of 1000 API calls completely free. Register and get your free API Key.