So this is a simple proxy service on localhost:8000, how can I make all clients using the proxy think that the domain example.com is locally on the machines IP (192.x.x.x)
Like if you added the domain to the hosts file. All other request should just do the actual request.
use std::net::SocketAddr;
use tower::make::Shared;
use hyper::{ service::service_fn, Body, Client, Request, Response, Server };
async fn log(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
let path = req.uri().path();
let host = req.uri().host();
if host.unwrap() == "example.com" {
// make example.com's ip be localhost:3000
}
handle(req).await
}
async fn handle(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
let client = Client::new();
client.request(req).await
}
#[tokio::main]
async fn main() {
let make_service = Shared::new(service_fn(log));
let addr = SocketAddr::from(([127, 0, 0, 1], 8000));
let server = Server::bind(&addr).serve(make_service);
if let Err(e) = server.await {
println!("error: {}", e);
}
}
Cargo.toml
[package]
name = "my-package"
version = "0.1.0"
edition = "2021"
[dependencies]
hyper = { version = "0.14.25", features = ["full"] }
tokio = { version = "1.26.0", features = ["full"] }
tower = { version = "0.4", features = ["full"] }