Created
May 15, 2021 15:29
-
-
Save sdressler/b31408bd89949f3d11b67f19076aee95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®exes); | |
let mut dummy_cache = Cache::new(®exes); | |
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