commit 308e492fe78ca179f4d543e71690b28c2bc0fbab Author: rrr-marble Date: Tue Oct 12 21:31:07 2021 +0300 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..9127008 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "myip" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b81568f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "myip" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/myip.code-workspace b/myip.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/myip.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..10c87a1 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,31 @@ +use std::io::prelude::*; +use std::net::{TcpListener, TcpStream}; + +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} + +fn handle_connection(mut stream: TcpStream) { + // make it 8k? + // https://stackoverflow.com/questions/686217/maximum-on-http-header-values + let mut buffer = [0; 1024]; + + stream.read(&mut buffer).unwrap(); + + let ip = stream.peer_addr().unwrap().ip().to_string(); + + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Lengh: {}\r\n\r\n{}", + ip.len(), + ip + ); + + stream.write(response.as_bytes()).unwrap(); + stream.flush().unwrap(); +}