file_transfer_system/
network.rs

1use std::{net::IpAddr, str::FromStr};
2use serde::{Deserialize, Serialize};
3use local_ip_address::local_ip;
4
5/// Enum representing various types of requests that can be made in the system.
6#[derive(Serialize, Deserialize)]
7pub enum Request {
8    /// Request to retrieve a file or directory located at a given path.
9    Get(String),
10    Upload,
11}
12
13/// Enum representing the types of IP addresses.
14pub enum IpType {
15    /// Represents an IPv4 address.
16    IPv4,
17    /// Represents an IPv6 address.
18    IPv6,
19}
20
21/// Gets the local IP address as a string.
22pub fn get_local_ip() -> anyhow::Result<IpAddr> {
23    Ok(local_ip()?)
24}
25
26/// Retrieves the public IP address of the specified type (IPv4 or IPv6).
27
28pub async fn get_public_ip(ip_type: IpType) -> anyhow::Result<IpAddr> {
29    match ip_type {
30        IpType::IPv4 => {
31            let ip = reqwest::get("https://api.ipify.org").await?.text().await?;
32            Ok(IpAddr::from_str(&ip)?)
33        },
34        IpType::IPv6 => {
35            let ip = reqwest::get("https://api64.ipify.org").await?.text().await?;
36            Ok(IpAddr::from_str(&ip)?)
37        }
38    }
39}