Skip to content

Commit

Permalink
add public ips
Browse files Browse the repository at this point in the history
  • Loading branch information
umutbasal committed Aug 25, 2024
1 parent f520aab commit b370dc9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
28 changes: 17 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 55 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -61,11 +63,19 @@ async fn handle(req: Request<Body>, addr: SocketAddr) -> Result<Response<Body>,

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();
Expand All @@ -89,6 +99,50 @@ async fn handle(req: Request<Body>, addr: SocketAddr) -> Result<Response<Body>,
))))
}

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<Body>) -> bool {
req.uri().query().map_or(false, |q| q.contains('j'))
|| req.uri().path().contains('j')
Expand Down

0 comments on commit b370dc9

Please sign in to comment.