public ip
This commit is contained in:
parent
09afecf396
commit
0973bca227
@ -8,6 +8,8 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use crate::get_current_public_ipv4;
|
||||
|
||||
pub struct CloudflareClient {
|
||||
client: Client,
|
||||
domains: Vec<Arc<str>>,
|
||||
@ -15,21 +17,6 @@ pub struct CloudflareClient {
|
||||
api_key: Box<str>,
|
||||
zone_id: Box<str>,
|
||||
}
|
||||
// Some external site to check this
|
||||
async fn get_current_public_ipv4(client: &Client) -> Result<Ipv4Addr> {
|
||||
let response = client
|
||||
.get("https://api.ipify.org?format=json")
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<HashMap<String, String>>()
|
||||
.await?;
|
||||
|
||||
Ok(response
|
||||
.get("ip")
|
||||
.context("Field 'ip' wasn't found")?
|
||||
.parse()?)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
struct DnsRecord {
|
||||
@ -45,7 +32,7 @@ struct DnsRecord {
|
||||
zone_name: Box<str>,
|
||||
modified_on: Box<str>,
|
||||
created_on: Box<str>,
|
||||
meta: HashMap<String, serde_json::Value>,
|
||||
meta: HashMap<Box<str>, serde_json::Value>,
|
||||
}
|
||||
|
||||
impl CloudflareClient {
|
||||
|
@ -1,6 +1,7 @@
|
||||
mod cloudflare;
|
||||
mod config;
|
||||
pub use config::{Config, get_config_path, read_config};
|
||||
|
||||
mod public_ip;
|
||||
pub use config::{get_config_path, read_config, Config};
|
||||
pub use public_ip::get_current_public_ipv4;
|
||||
|
||||
pub const PROGRAM_NAME: &'static str = "dynip-cloudflare";
|
||||
|
44
src/public_ip.rs
Normal file
44
src/public_ip.rs
Normal file
@ -0,0 +1,44 @@
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::error;
|
||||
use reqwest::Client;
|
||||
use std::{collections::HashMap, net::Ipv4Addr};
|
||||
|
||||
async fn ipify_org(client: &Client) -> Result<Ipv4Addr> {
|
||||
let response = client
|
||||
.get("https://api.ipify.org?format=json")
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<HashMap<String, String>>()
|
||||
.await?;
|
||||
|
||||
Ok(response
|
||||
.get("ip")
|
||||
.context("Field 'ip' wasn't found")?
|
||||
.parse()?)
|
||||
}
|
||||
async fn ifconfig_me(client: &Client) -> Result<Ipv4Addr> {
|
||||
Ok(client
|
||||
.get("https://ifconfig.me")
|
||||
.header("user-agent", "curl/8.8.0")
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.text()
|
||||
.await?
|
||||
.parse()?)
|
||||
}
|
||||
pub async fn get_current_public_ipv4(client: &Client) -> Result<Ipv4Addr> {
|
||||
let e_ipify = match ipify_org(client).await {
|
||||
Ok(ipv4) => return Ok(ipv4),
|
||||
Err(e) => {
|
||||
error!("Failed to get ip from ipify.org: {:?}", &e);
|
||||
e
|
||||
}
|
||||
};
|
||||
|
||||
ifconfig_me(client).await.map_err(|e_ifconfig| {
|
||||
error!("Failed to get ip from ifconfig.me: {:?}", &e_ifconfig);
|
||||
anyhow!("Failed to get ip from ipify.org with error '{:?}', and ifconfig.me with error {:?}", &e_ipify, &e_ifconfig)
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user