From b370dc932aa7c8ce34e078c1cf61422f8cc0bb4e Mon Sep 17 00:00:00 2001 From: umut Date: Sun, 25 Aug 2024 21:11:49 +0300 Subject: [PATCH] add public ips --- Cargo.lock | 28 ++++++++++++++++----------- src/main.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e5f06d1..5c59776 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,11 +64,11 @@ checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" [[package]] name = "cc" -version = "1.0.82" +version = "1.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "305fe645edc1442a0fa8b6726ba61d422798d37a52e12eaecf4b022ebbb88f01" +checksum = "50d2eb3cd3d1bf4529e31c215ee6f93ec5a3d536d9f578f93d9d33ee19562932" dependencies = [ - "libc", + "shlex", ] [[package]] @@ -79,9 +79,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "core-foundation-sys" -version = "0.8.4" +version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "crossbeam-channel" @@ -300,9 +300,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.147" +version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" [[package]] name = "lock_api" @@ -527,6 +527,12 @@ dependencies = [ "serde", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -547,9 +553,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.11.0" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" @@ -573,9 +579,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.29" +version = "2.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" +checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" dependencies = [ "proc-macro2", "quote", diff --git a/src/main.rs b/src/main.rs index 442576f..18f8fee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,9 @@ use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Request, Response, Server}; use json_to_table::json_to_table; use std::collections::HashMap; -use std::net::IpAddr; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; +use std::process::Command; +use std::str::FromStr; use std::{convert::Infallible, net::SocketAddr}; use sysinfo::{System, SystemExt}; @@ -61,11 +63,19 @@ async fn handle(req: Request, addr: SocketAddr) -> Result, let remote_ip = addr.ip().to_string(); + let (ipv4, ipv6) = public_ips().await; + + let public_map = serde_json::json!({ + "ipv4": ipv4.to_string(), + "ipv6": ipv6.to_string(), + }); + let json_data = serde_json::json!({ "headers": headers_map, "environment": environment_map, "sysinfo": sys, "remote_ip": remote_ip, + "public_ips": public_map, }); let mut output = String::new(); @@ -89,6 +99,50 @@ async fn handle(req: Request, addr: SocketAddr) -> Result, )))) } +pub async fn public_ips() -> (Ipv4Addr, Ipv6Addr) { + let cmd = Command::new("sh") + .arg("-c") + .arg("curl -s https://ipv4.icanhazip.com") + .output() + .expect("failed to execute process"); + + let ipv4 = String::from_utf8(cmd.stdout) + .unwrap_or("".to_string()) + .trim() + .to_string(); + let ipv4 = Ipv4Addr::from_str(&ipv4).unwrap(); + + //dig +short AAAA ipv6.icanhazip.com + let cmd = Command::new("sh") + .arg("-c") + .arg("dig +short AAAA ipv6.icanhazip.com | head -n 1") + .output() + .expect("failed to execute process"); + + let aaaa = String::from_utf8(cmd.stdout) + .unwrap_or("".to_string()) + .trim() + .to_string(); + let ipv6 = Ipv6Addr::from_str(&aaaa).unwrap_or(Ipv6Addr::UNSPECIFIED); + + let cmd = Command::new("sh") + .arg("-c") + .arg(format!( + "curl -k -6 -s https://[{}] --header 'Host: ipv6.icanhazip.com'", + ipv6 + )) + .output() + .expect("failed to execute process"); + + let ipv6 = String::from_utf8(cmd.stdout) + .unwrap_or("".to_string()) + .trim() + .to_string(); + let ipv6 = Ipv6Addr::from_str(&ipv6).unwrap_or(Ipv6Addr::UNSPECIFIED); + + (ipv4, ipv6) +} + fn view_as_json(req: Request) -> bool { req.uri().query().map_or(false, |q| q.contains('j')) || req.uri().path().contains('j')