Skip to content

Instantly share code, notes, and snippets.

@micromaomao
Created March 15, 2023 16:20
Show Gist options
  • Save micromaomao/d71727bb44d84b453c903edaad833bf9 to your computer and use it in GitHub Desktop.
Save micromaomao/d71727bb44d84b453c903edaad833bf9 to your computer and use it in GitHub Desktop.
FROM rust
WORKDIR /usr/src/fakesas
COPY . .
RUN cargo build --release
CMD ["./target/release/fakesas"]
use std::io::Read;
use std::net::{TcpListener, TcpStream};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let tcpl = TcpListener::bind("0.0.0.0:6761")?;
loop {
let a = tcpl.accept();
if let Err(ref e) = a {
eprintln!("Error accepting connection: {}", e);
}
let (mut astream, peer) = a.unwrap();
println!("Accepted connection from {}", peer);
std::thread::spawn(move || {
let mut buf = vec![0u8; 1 << 16];
loop {
let res = astream.read(&mut buf);
if let Err(ref e) = res {
eprintln!("Error reading from {}: {}", peer, e);
break;
} else if res.unwrap() == 0 {
println!("Connection from {} closed", peer);
break;
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment