Skip to content

Commit

Permalink
Introdced Function to get IP Address in integer format
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigRazor authored Jul 9, 2021
1 parent 55641c7 commit 2ecf767
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 73 deletions.
69 changes: 40 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,54 @@ The repository contains 2 folder. The first one *Rust_ipify_lib* contains the so
The example is provided under the `Rust_ipify_lib_test/src/` folder.
The file contains an example of usage of the API of the library to retrive the global IP:


extern crate ipify_lib;
use ipify_lib::ipify::get_ip_json;
use ipify_lib::ipify::get_ip_string;
use ipify_lib::ipify::get_ip4_json;
use ipify_lib::ipify::get_ip4_string;
extern crate rust_ipify;
use rust_ipify::ipify::get_ip_json;
use rust_ipify::ipify::get_ip_string;
use rust_ipify::ipify::get_ip4_json;
use rust_ipify::ipify::get_ip4_string;
use rust_ipify::ipify::get_ip4_i32;
use rust_ipify::ipify::get_ip_i128;

fn main() {
println!("inside main of test ");
println!("inside main of test ");

let result_ip64_json = get_ip_json();
let result_ip64_json = get_ip_json();

match result_ip64_json {
Ok(r) => println!("Result IP ( JSON ): \n{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}
match result_ip64_json {
Ok(r) => println!("Result IP ( JSON ): \n{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip64_string = get_ip_string();
match result_ip64_string {
Ok(r) => println!("Result IP ( String ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}
let result_ip64_string = get_ip_string();
match result_ip64_string {
Ok(r) => println!("Result IP ( String ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip4_json = get_ip4_json();
let result_ip4_json = get_ip4_json();

match result_ip4_json {
Ok(r) => println!("Result IP v4 ( JSON ): \n{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}
match result_ip4_json {
Ok(r) => println!("Result IP v4 ( JSON ): \n{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip4_string = get_ip4_string();
match result_ip4_string {
Ok(r) => println!("Result IP v4 ( String ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}
let result_ip4_string = get_ip4_string();
match result_ip4_string {
Ok(r) => println!("Result IP v4 ( String ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip4_i32 = get_ip4_i32();
match result_ip4_i32 {
Ok(r) => println!("Result IP v4 ( i32 ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip_i128 = get_ip_i128();
match result_ip_i128 {
Ok(r) => println!("Result IP ( i128 ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}



## How to contribute
[![GitHub contributors](https://img.shields.io/github/contributors/ZigRazor/rust_ipify.svg)](https://GitHub.com/ZigRazor/rust_ipify/graphs/contributors/)
Expand Down
5 changes: 3 additions & 2 deletions Rust_ipify_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust_ipify"
version = "1.0.2"
version = "1.1.0"
authors = ["ZigRazor"]
edition = "2018"
license-file = "LICENSE"
Expand All @@ -11,4 +11,5 @@ readme = "README.md"
keywords = ["ipify", "ip","internet", "library"]
categories = ["api-bindings", "development-tools","web-programming"]
[dependencies]
reqwest = { version = "0.11", features = ["blocking","json"] }
reqwest = { version = "0.11", features = ["blocking","json"] }
regex = "1"
69 changes: 40 additions & 29 deletions Rust_ipify_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,54 @@ The repository contains 2 folder. The first one *Rust_ipify_lib* contains the so
The example is provided under the `Rust_ipify_lib_test/src/` folder.
The file contains an example of usage of the API of the library to retrive the global IP:


extern crate ipify_lib;
use ipify_lib::ipify::get_ip_json;
use ipify_lib::ipify::get_ip_string;
use ipify_lib::ipify::get_ip4_json;
use ipify_lib::ipify::get_ip4_string;
extern crate rust_ipify;
use rust_ipify::ipify::get_ip_json;
use rust_ipify::ipify::get_ip_string;
use rust_ipify::ipify::get_ip4_json;
use rust_ipify::ipify::get_ip4_string;
use rust_ipify::ipify::get_ip4_i32;
use rust_ipify::ipify::get_ip_i128;

fn main() {
println!("inside main of test ");
println!("inside main of test ");

let result_ip64_json = get_ip_json();
let result_ip64_json = get_ip_json();

match result_ip64_json {
Ok(r) => println!("Result IP ( JSON ): \n{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}
match result_ip64_json {
Ok(r) => println!("Result IP ( JSON ): \n{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip64_string = get_ip_string();
match result_ip64_string {
Ok(r) => println!("Result IP ( String ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip64_string = get_ip_string();
match result_ip64_string {
Ok(r) => println!("Result IP ( String ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}
let result_ip4_json = get_ip4_json();

let result_ip4_json = get_ip4_json();
match result_ip4_json {
Ok(r) => println!("Result IP v4 ( JSON ): \n{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

match result_ip4_json {
Ok(r) => println!("Result IP v4 ( JSON ): \n{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}
let result_ip4_string = get_ip4_string();
match result_ip4_string {
Ok(r) => println!("Result IP v4 ( String ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip4_string = get_ip4_string();
match result_ip4_string {
Ok(r) => println!("Result IP v4 ( String ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}
let result_ip4_i32 = get_ip4_i32();
match result_ip4_i32 {
Ok(r) => println!("Result IP v4 ( i32 ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}



let result_ip_i128 = get_ip_i128();
match result_ip_i128 {
Ok(r) => println!("Result IP ( i128 ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

## How to contribute
[![GitHub contributors](https://img.shields.io/github/contributors/ZigRazor/rust_ipify.svg)](https://GitHub.com/ZigRazor/rust_ipify/graphs/contributors/)
Expand Down
45 changes: 37 additions & 8 deletions Rust_ipify_lib/src/ipify.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate reqwest;
use std::collections::HashMap;
use regex::Regex;

pub fn get_ip4_json() -> Result<HashMap<String, String>, Box<dyn std::error::Error>> {
let body = reqwest::blocking::get("https://api.ipify.org?format=json")?
Expand All @@ -12,9 +13,9 @@ pub fn get_ip4_string() -> Result<String, Box<dyn std::error::Error>> {
Ok(body)
}

pub fn get_ip4_i64() -> Result<i64, Box<dyn std::error::Error>> {
pub fn get_ip4_i32() -> Result<i32, Box<dyn std::error::Error>> {
let body = reqwest::blocking::get("https://api.ipify.org")?.text()?;
let result = ip_to_int(body);
let result = ipv4_to_int(body);
Ok(result)
}

Expand All @@ -29,16 +30,44 @@ pub fn get_ip_string() -> Result<String, Box<dyn std::error::Error>> {
Ok(body)
}

fn ip_to_int(ip :String) -> i64 {
let mut result : i64 = 0;
pub fn get_ip_i128() -> Result<i128, Box<dyn std::error::Error>> {
let result : i128;
let body = reqwest::blocking::get("https://api.ipify.org")?.text()?;
let re = Regex::new(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$").unwrap();
if re.is_match(body.as_str()){
result = ipv4_to_int(body) as i128;
}else{
result = ipv6_to_int(body);
}
Ok(result)
}


pub fn ipv4_to_int(ip :String) -> i32 {
let mut result : i32 = 0;
let splitted_ip : Vec<&str> = ip.split('.').collect();
if splitted_ip.len() == 4 {
let first_block : i64 = splitted_ip[0].parse::<i64>().unwrap();
let second_block : i64 = splitted_ip[1].parse::<i64>().unwrap();
let third_block : i64 = splitted_ip[2].parse::<i64>().unwrap();
let fourth_block : i64 = splitted_ip[3].parse::<i64>().unwrap();
let first_block : i32 = splitted_ip[0].parse::<i32>().unwrap();
let second_block : i32 = splitted_ip[1].parse::<i32>().unwrap();
let third_block : i32 = splitted_ip[2].parse::<i32>().unwrap();
let fourth_block : i32 = splitted_ip[3].parse::<i32>().unwrap();

result = (first_block<<24)+(second_block<<16)+(third_block<<8)+(fourth_block<<0);
}
result
}

pub fn ipv6_to_int(ip :String) -> i128 {
let mut result : i128 = 0;
let splitted_ip : Vec<&str> = ip.split('.').collect();

let i : i32 = 7;
for token in splitted_ip.iter() {
let token_int = i128::from_str_radix(token, 16);
match token_int{
Ok(o) => {result += o<<16*i;},
Err(_e) => {},
}
}
result
}
2 changes: 1 addition & 1 deletion Rust_ipify_lib_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "test_for_ipify_lib"
version = "1.0.0"
version = "1.1.0"
authors = ["ZigRazor"]
edition = "2018"
[dependencies]
Expand Down
15 changes: 11 additions & 4 deletions Rust_ipify_lib_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use rust_ipify::ipify::get_ip_json;
use rust_ipify::ipify::get_ip_string;
use rust_ipify::ipify::get_ip4_json;
use rust_ipify::ipify::get_ip4_string;
use rust_ipify::ipify::get_ip4_i64;
use rust_ipify::ipify::get_ip4_i32;
use rust_ipify::ipify::get_ip_i128;
fn main() {
println!("inside main of test ");

Expand Down Expand Up @@ -33,9 +34,15 @@ fn main() {
Err(e) => println!("error in request {:?}", e),
}

let result_ip4_i64 = get_ip4_i64();
match result_ip4_i64 {
Ok(r) => println!("Result IP v4 ( i64 ): \n\t{:#?}\n", r),
let result_ip4_i32 = get_ip4_i32();
match result_ip4_i32 {
Ok(r) => println!("Result IP v4 ( i32 ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

let result_ip_i128 = get_ip_i128();
match result_ip_i128 {
Ok(r) => println!("Result IP ( i128 ): \n\t{:#?}\n", r),
Err(e) => println!("error in request {:?}", e),
}

Expand Down

0 comments on commit 2ecf767

Please sign in to comment.