Skip to content

Instantly share code, notes, and snippets.

@sdressler
Created May 15, 2021 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdressler/b31408bd89949f3d11b67f19076aee95 to your computer and use it in GitHub Desktop.
Save sdressler/b31408bd89949f3d11b67f19076aee95 to your computer and use it in GitHub Desktop.
use std::error::Error;
use std::net::{Shutdown, TcpListener, TcpStream};
use std::thread::spawn;
fn main() {
let server = TcpListener::bind("127.0.0.1:15432").unwrap();
for client in server.incoming() {
let client = client.unwrap();
client.set_nodelay(true).unwrap();
println!("New conn...");
spawn(move || {
handle_connection(client).unwrap();
});
}
}
fn handle_connection(mut client: TcpStream) -> Result<(), Box<dyn Error>> {
let mut upstream = TcpStream::connect("127.0.0.1:5432")?;
upstream.set_nodelay(true)?;
let regexes = vec![
Regex::new(r"^select \* from cmdb where sys_id = (?P<p1>'[a-z0-9]{32}');$").unwrap()
];
let mut cache = Cache::new(&regexes);
let mut dummy_cache = Cache::new(&regexes);
let mut client_clone = client.try_clone()?;
let mut upstream_clone = upstream.try_clone()?;
let t1 = spawn(move || {
std::io::copy(&mut client, &mut upstream)?;
upstream.shutdown(Shutdown::Both)
});
let t2 = spawn(move || {
std::io::copy(&mut upstream_clone, &mut client_clone)?;
client_clone.shutdown(Shutdown::Both)
});
t1.join().unwrap()?;
t2.join().unwrap()?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment