feat: add show_myip
This commit is contained in:
1073
scripts/show_myip/Cargo.lock
generated
Normal file
1073
scripts/show_myip/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
scripts/show_myip/Cargo.toml
Normal file
12
scripts/show_myip/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "show_myip"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||
rust_util = { version = "0.6" }
|
||||
32
scripts/show_myip/src/main.rs
Executable file
32
scripts/show_myip/src/main.rs
Executable file
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env runrs
|
||||
|
||||
//! ```cargo
|
||||
//! [dependencies]
|
||||
//! serde = { version = "1.0", features = ["derive"] }
|
||||
//! serde_json = "1.0"
|
||||
//! reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||
//! rust_util = { version = "0.6" }
|
||||
//! ```
|
||||
|
||||
use rust_util::{failure_and_exit, success};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct IpResponse {
|
||||
status: u16,
|
||||
message: String,
|
||||
ip: String,
|
||||
#[serde(rename = "userAgent")]
|
||||
user_agent: Option<String>,
|
||||
}
|
||||
|
||||
const GET_IP_URL: &'static str = "https://hatter.ink/ip/ip.jsonp";
|
||||
|
||||
fn main() {
|
||||
let ip_response: IpResponse = reqwest::blocking::get(GET_IP_URL).unwrap_or_else(|e| {
|
||||
failure_and_exit!("Send request to: {} failed: {}", GET_IP_URL, e);
|
||||
}).json().unwrap_or_else(|e| {
|
||||
failure_and_exit!("Parse response from: {}, failed: {}", GET_IP_URL, e);
|
||||
});
|
||||
success!("Your IP address is: {}", ip_response.ip);
|
||||
}
|
||||
Reference in New Issue
Block a user