From 88b4ff05033623f2b47563c70dc66f65b54ed79f Mon Sep 17 00:00:00 2001 From: Kevin Sumner Date: Tue, 28 Mar 2023 03:06:28 -0400 Subject: [PATCH] Add IPv4-only and IPv6-only endpoints api4.ipify.org has only DNS A records and api6.ipify.org has only DNS AAAA records, so they ensure only IPv4 and IPv6 info, respectively. api64.ipify.org has both A and AAAA records, which means it will give v4 or v6 address based on preference of the client, which isn't always desirable. Currently, api.ipify.org is a CNAME record for api4.ipify.org, so it is technically IPv4-only. --- src/bin/ipify-cli/main.rs | 6 ++++-- src/lib.rs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/bin/ipify-cli/main.rs b/src/bin/ipify-cli/main.rs index 2501838..69827a6 100644 --- a/src/bin/ipify-cli/main.rs +++ b/src/bin/ipify-cli/main.rs @@ -56,17 +56,19 @@ fn main() -> Result<(), ()> { let mut op = Op::IPv6; if opts.ipv4 { - op = Op::IPv4; + op = Op::ForcedIPv4; } if opts.ipv6 { - op = Op::IPv6; + op = Op::ForcedIPv6; } if opts.json { op = match op { Op::IPv4 | Op::IPv4J => Op::IPv4J, Op::IPv6 | Op::IPv6J => Op::IPv6J, + Op::ForcedIPv4 | Op::ForcedIPv4J => Op::ForcedIPv4J, + Op::ForcedIPv6 | Op::ForcedIPv6J => Op::ForcedIPv6J, }; } diff --git a/src/lib.rs b/src/lib.rs index 3898ef5..1115e26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,10 +19,18 @@ use clap::{crate_name, crate_version}; const ENDPOINT4: &str = "https://api.ipify.org"; /// IPv6 endpoint, plain text const ENDPOINT6: &str = "https://api64.ipify.org"; +/// Forced IPv4 endpoint, plain text +const ENDPOINTFORCED4: &str = "https://api4.ipify.org"; +/// Forced IPv6 endpoint, plain text +const ENDPOINTFORCED6: &str = "https://api6.ipify.org"; /// IPv4 endpoint, JSON const ENDPOINT4J: &str = "https://api.ipify.org?format=json"; /// IPv6 endpoint, JSON const ENDPOINT6J: &str = "https://api64.ipify.org?format=json"; +/// Forced IPv4 endpoint, JSON +const ENDPOINTFORCED4J: &str = "https://api4.ipify.org?format=json"; +/// Forced IPv6 endpoint, JSON +const ENDPOINTFORCED6J: &str = "https://api6.ipify.org?format=json"; /// Minimalistic API /// @@ -45,10 +53,18 @@ pub enum Op { IPv4, /// Plain text (default) IPv6, + /// Plain text + ForcedIPv4, + /// Plain text + ForcedIPv6, /// Json output IPv4J, /// Json output IPv6J, + /// Json output + ForcedIPv4J, + /// Json output + ForcedIPv6J, } /// The main API struct @@ -105,8 +121,12 @@ impl Ipify { endp: match op { Op::IPv4 => ENDPOINT4.to_owned(), Op::IPv6 => ENDPOINT6.to_owned(), + Op::ForcedIPv4 => ENDPOINTFORCED4.to_owned(), + Op::ForcedIPv6 => ENDPOINTFORCED6.to_owned(), Op::IPv4J => ENDPOINT4J.to_owned(), Op::IPv6J => ENDPOINT6J.to_owned(), + Op::ForcedIPv4J => ENDPOINTFORCED4J.to_owned(), + Op::ForcedIPv6J => ENDPOINTFORCED6J.to_owned(), }, } }