Files
simple-rust-udp-proxy/src/config.rs
2020-09-19 11:56:45 +08:00

105 lines
3.9 KiB
Rust

use std::fs::read_to_string;
use std::path::Path;
use serde::{ Deserialize, Serialize };
use rust_util::XResult;
use rust_util::util_file::read_config;
use rust_util::util_file::get_absolute_path;
// read config order:
// param assigned
// ./udp_listen_config.json
// ~/udp_listen_config.json
// /etc/udp_listen_config.json
const UDP_LISTEN_CONFIG_JSON: &str = "udp_listen_config.json";
const HOME_UDP_LISTEN_CONFIG_JSON: &str = "~/udp_listen_config.json";
const ETC_UDP_LISTEN_CONFIG_JSON: &str = "/etc/udp_listen_config.json";
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UdpListenConfig {
pub listen: String,
pub backend: String,
pub allow_ips: Option<Vec<String>>,
}
pub fn read_udp_listen_config(config_file: Option<String>) -> XResult<Option<UdpListenConfig>> {
match read_config(config_file, &[ UDP_LISTEN_CONFIG_JSON.into(),
HOME_UDP_LISTEN_CONFIG_JSON.into(),
ETC_UDP_LISTEN_CONFIG_JSON.into()
]) {
None => {
failure!("Cannot find udp listen config file!");
Ok(None)
},
Some(config_path) => {
information!("Found config file: {:?}", config_path);
let config_content = read_to_string(config_path)?;
Ok(serde_json::from_str(&config_content)?)
},
}
}
// pub fn parse_udp_listen_config(p_config_file: &str) -> Option<UdpListenConfig> {
// let (udp_listen_config_file, udp_listen_config_json) = match read_config_file_content(p_config_file) {
// Some(udp_listen_config_json) => udp_listen_config_json, None => return None,
// };
// let udp_listen_config: UdpListenConfig = match serde_json::from_str(&udp_listen_config_json) {
// Ok(udp_listen_config) => udp_listen_config, Err(e) => {
// failure!("Parse config file: {:?}, failed: {}", &udp_listen_config_file, e);
// return None;
// },
// };
// // if udp_listen_config.udp_listens.is_empty() {
// // failure!("Cannot find any udp listen config in file: {:?}", &udp_listen_config_file);
// // return None;
// // }
// Some(udp_listen_config)
// }
// fn read_config_file_content(p_config_file: &str) -> Option<(String, String)> {
// let config_file = match get_config_file(p_config_file) {
// Some(config_file) => config_file, None => {
// failure!("Udp listen config file not found!");
// return None;
// },
// };
// let udp_listen_config_file = Path::new(&config_file);
// if udp_listen_config_file.exists() {
// information!("Read udp listen config from file: {:?}", &udp_listen_config_file);
// } else {
// failure!("Udp listen config file not exists: {:?}", &udp_listen_config_file);
// return None;
// }
// let udp_listen_config_json = match read_to_string(udp_listen_config_file) {
// Ok(udp_listen_config_json) => udp_listen_config_json, Err(e) => {
// failure!("Read file: {:?}, failed: {}", &udp_listen_config_file, e);
// return None;
// },
// };
// Some((config_file, udp_listen_config_json))
// }
// fn get_config_file(p_config_file: &str) -> Option<String> {
// if p_config_file.is_empty() {
// get_default_config_file()
// } else {
// Some(p_config_file.to_owned())
// }
// }
// fn get_default_config_file() -> Option<String> {
// if Path::new(UDP_LISTEN_CONFIG_JSON).exists() {
// return Some(UDP_LISTEN_CONFIG_JSON.to_owned());
// }
// if let Some(path) = get_absolute_path(HOME_UDP_LISTEN_CONFIG_JSON) {
// if let (true, Some(file_path)) = (path.exists(), path.to_str()) {
// return Some(file_path.to_owned());
// }
// }
// if Path::new(ETC_UDP_LISTEN_CONFIG_JSON).exists() {
// return Some(ETC_UDP_LISTEN_CONFIG_JSON.to_owned());
// }
// None
// }