How to Build a Simple HTTP Proxy in Rust in just 40 lines

Oct 1, 2023 ยท 3 min read

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.

  • With millions of high speed rotating proxies located all over the world,
  • With our automatic IP rotation
  • With our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)
  • With our automatic CAPTCHA solving technology,
  • 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.

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: