1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::{collections::HashMap, fs, path::PathBuf};

use serde::{Deserialize, Serialize};

#[cfg(feature = "bundle")]
pub const UDP_SERVICE_BIN: &[u8] = include_bytes!("../resources/udp-service.bin");

pub const UDP_SERVICE_BIN_NAME: &str = "udp-service.bin";
pub const UDP_SERVICE_R2_URL: &str = "https://r2.ntap.io/udp-service.bin";

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UdpService {
    pub port: u16,
    pub service_name: String,
}

impl UdpService {
    pub fn file_name() -> String {
        UDP_SERVICE_BIN_NAME.to_owned()
    }
    pub fn r2_url() -> String {
        UDP_SERVICE_R2_URL.to_owned()
    }
}

#[cfg(feature = "bundle")]
pub fn get_map() -> HashMap<u16, String> {
    let mut udp_map: HashMap<u16, String> = HashMap::new();
    let udp_services: Vec<UdpService> = bincode::deserialize(UDP_SERVICE_BIN).unwrap();
    for udp_service in udp_services {
        udp_map.insert(udp_service.port, udp_service.service_name);
    }
    udp_map
}

pub fn get_map_from_file(file_path: PathBuf) -> HashMap<u16, String> {
    let mut udp_map: HashMap<u16, String> = HashMap::new();
    match fs::read(file_path) {
        Ok(f) => {
            let udp_services: Vec<UdpService> = bincode::deserialize(&f).unwrap();
            for udp_service in udp_services {
                udp_map.insert(udp_service.port, udp_service.service_name);
            }
        }
        Err(e) => {
            eprintln!("Error reading file: {}", e);
        }
    }
    udp_map
}