Compare commits

...

2 Commits

Author SHA1 Message Date
c026351c48 feat: rename target bin 2024-10-05 11:18:00 +08:00
3a65b565f1 upgrade clap to 4.1.1 2023-01-18 10:44:15 +08:00
7 changed files with 346 additions and 40 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
/target
.idea
*.iml

293
Cargo.lock generated Normal file
View File

@ -0,0 +1,293 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
[[package]]
name = "anstyle-parse"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "clap"
version = "4.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7be5744db7978a28d9df86a214130d106a89ce49644cbc4e3f0c22c3fba30615"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5fbc17d3ef8278f55b282b2a2e75ae6f6c7d4bb70ed3d0382375104bfafdb4b"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]]
name = "colorchoice"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
[[package]]
name = "ctrlc"
version = "3.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3"
dependencies = [
"nix",
"windows-sys 0.59.0",
]
[[package]]
name = "echo-ip"
version = "0.1.1"
dependencies = [
"clap",
"ctrlc",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libc"
version = "0.2.159"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View File

@ -1,10 +1,28 @@
[package]
name = "echo-ip"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
rust-version = "1.64"
license = "MIT"
authors = ["Yezzi Hsueh"]
description = "Echo your IP address"
repository = "https://code.simaek.com/xueye/echo-ip-rust"
readme = "README.md"
documentation = "https://docs.rs/echo-ip-rust"
keywords = ["ipecho", "http"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "ipecho"
path = "src/bin/echo-ip.rs"
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
incremental = false
panic = "abort"
strip = true
[dependencies]
clap = "2.33.1"
clap = { version = "4.1.1", features = ["derive"] }
ctrlc = {version = "3.2.4", features=["termination"]}

View File

@ -1,5 +1,6 @@
FROM scratch
MAINTAINER Yaser Hsueh
LABEL maintainer="Yezzi Hsueh <xueye404@gmail.com>"
WORKDIR /
COPY target/x86_64-unknown-linux-musl/release/echo-ip .
ENTRYPOINT ["/echo-ip"]
COPY target/x86_64-unknown-linux-musl/release/ipecho .
EXPOSE 8888
ENTRYPOINT ["/ipecho"]

View File

@ -1,14 +1,14 @@
# echo-ip
usage:
### usage:
Start the server. Default: bind address 0.0.0.0 and port 8888.
```
./echo-ip -l 0.0.0.0 -p 8888
./ipecho -l 0.0.0.0 -p 8888
```
Send a HTTP request to the server, and you will receive an echo for you IP address.
Send an HTTP request to the server, and you will receive an echo for you IP address.
```
curl 10.20.30.40:8888

View File

@ -1,2 +1,3 @@
rustup target add x86_64-unknown-linux-musl
cargo build --release --target=x86_64-unknown-linux-musl
docker build -t ipecho:0.1.1 .

View File

@ -1,5 +1,5 @@
extern crate clap;
use clap::{App, Arg, ArgMatches};
//use clap::{App, Arg, ArgMatches};
use std::io::prelude::*;
use std::net::{TcpListener, TcpStream};
use std::process;
@ -9,15 +9,29 @@ use std::thread;
use ctrlc;
use echo_ip::ThreadPool;
use clap::Parser;
#[derive(Parser, Debug)]
#[command(name="echo-ip", author, version, about, long_about = None)]
struct Args {
/// address to listening
#[arg(short, long, default_value = "0.0.0.0", value_name = "IP")]
listen: String,
/// port to listening
#[arg(short, long, default_value = "8888")]
port: String,
}
fn main() {
let (tx, rx) = channel();
ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
.expect("Error setting Ctrl+C handler");
thread::spawn(move || {
let matches = handle_args();
let host = matches.value_of("server").unwrap_or("0.0.0.0");
let port = matches.value_of("port").unwrap_or("8888");
create_listener(host, port);
let args = Args::parse();
let host = args.listen;
let port = args.port;
create_listener(&host, &port);
});
rx.recv().expect("Could not receive from channel.");
println!("Exiting...");
@ -31,7 +45,7 @@ fn create_listener(host: &str, port: &str) {
match listener {
Ok(n) => {
println!("-----------------------------------------------------------");
println!("Server listening on {}", &port);
println!("Server listening on {}:{}", &host, &port);
println!("-----------------------------------------------------------");
for stream in n.incoming() {
let stream = stream.unwrap();
@ -44,30 +58,6 @@ fn create_listener(host: &str, port: &str) {
}
}
fn handle_args() -> ArgMatches<'static> {
return App::new("echo-ip")
.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();
}
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 1024];
let remote_addr = stream.peer_addr().unwrap().ip();
@ -77,7 +67,7 @@ fn handle_connection(mut stream: TcpStream) {
let (status_line, contents) = if buffer.starts_with(get) {
("HTTP/1.1 200 OK", remote_addr.to_string())
} else {
("HTTP/1.1 404 NOT FOUND", "URL ERROR".to_string())
("HTTP/1.1 404 NOT FOUND", "ERROR".to_string())
};
println!("echo {}", remote_addr);
let response = format!("{}\r\n\r\n{}\r\n", status_line, contents);