feat: add host and port parameters
This commit is contained in:
parent
df81e1a2d4
commit
6a8e6095e5
@ -1,8 +1,9 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "echo"
|
name = "echo-ip"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = "2.33.1"
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
use std::net::{TcpListener, TcpStream};
|
|
||||||
use std::io::prelude::*;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let listener = TcpListener::bind("0.0.0.0:8888").unwrap();
|
|
||||||
for stream in listener.incoming() {
|
|
||||||
let stream = stream.unwrap();
|
|
||||||
handle_connection(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_connection(mut stream: TcpStream) {
|
|
||||||
let mut buffer = [0; 1024];
|
|
||||||
let remote_addr = stream.peer_addr().unwrap().ip().to_string();
|
|
||||||
|
|
||||||
stream.read(&mut buffer).unwrap();
|
|
||||||
|
|
||||||
let response = "HTTP/1.1 200 OK\r\n\r\n";
|
|
||||||
|
|
||||||
stream.write(response.as_bytes()).unwrap();
|
|
||||||
stream.write(remote_addr.as_bytes()).unwrap();
|
|
||||||
stream.write("\r\n".as_bytes()).unwrap();
|
|
||||||
stream.flush().unwrap();
|
|
||||||
}
|
|
64
src/main.rs
Normal file
64
src/main.rs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
extern crate clap;
|
||||||
|
use clap::{App, Arg};
|
||||||
|
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::net::{TcpListener, TcpStream};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let matches = App::new("EchoIP")
|
||||||
|
.version("1.0")
|
||||||
|
.author("Yaser Hsueh <xueye404@icloud.com>")
|
||||||
|
.about("https://www.simaek.com")
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("port")
|
||||||
|
.short("p")
|
||||||
|
.long("port")
|
||||||
|
.value_name("port")
|
||||||
|
.help("listening port")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("server")
|
||||||
|
.short("s")
|
||||||
|
.long("server")
|
||||||
|
.value_name("host")
|
||||||
|
.help("listening address")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
|
.get_matches();
|
||||||
|
|
||||||
|
let host = matches.value_of("server").unwrap_or("0.0.0.0");
|
||||||
|
let port = matches.value_of("port").unwrap_or("8888");
|
||||||
|
let bind_address = host.to_owned() + ":" + port;
|
||||||
|
|
||||||
|
let listener = TcpListener::bind(&bind_address);
|
||||||
|
|
||||||
|
match listener {
|
||||||
|
Ok(n) => {
|
||||||
|
println!("-----------------------------------------------------------");
|
||||||
|
println!("Server listening on {}", &port);
|
||||||
|
println!("-----------------------------------------------------------");
|
||||||
|
for stream in n.incoming() {
|
||||||
|
let stream = stream.unwrap();
|
||||||
|
handle_connection(stream);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_e) => println!("Address already in use: {}", bind_address),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_connection(mut stream: TcpStream) {
|
||||||
|
let mut buffer = [0; 1024];
|
||||||
|
let remote_addr = stream.peer_addr().unwrap().ip().to_string();
|
||||||
|
|
||||||
|
stream.read(&mut buffer).unwrap();
|
||||||
|
|
||||||
|
let response = "HTTP/1.1 200 OK\r\n\r\n";
|
||||||
|
|
||||||
|
stream.write(response.as_bytes()).unwrap();
|
||||||
|
stream.write(remote_addr.as_bytes()).unwrap();
|
||||||
|
stream.write("\r\n".as_bytes()).unwrap();
|
||||||
|
stream.flush().unwrap();
|
||||||
|
|
||||||
|
println!("echo {}", remote_addr);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user