Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPv4-only and IPv6-only endpoints #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/bin/ipify-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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
Expand Down Expand Up @@ -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(),
},
}
}
Expand Down