feat: add config

This commit is contained in:
2020-09-19 11:56:45 +08:00
parent 96b3ca62ea
commit ff22020a79
4 changed files with 193 additions and 3 deletions

86
Cargo.lock generated
View File

@@ -142,6 +142,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "itoa"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@@ -154,6 +160,24 @@ version = "0.2.76"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3"
[[package]]
name = "proc-macro2"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "175c513d55719db99da20232b06cda8bab6b83ec2d04e3283edf0213c37c1a29"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.3.23" version = "0.3.23"
@@ -232,16 +256,55 @@ dependencies = [
[[package]] [[package]]
name = "rust_util" name = "rust_util"
version = "0.6.7" version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47a20a379d025a6f0da1bc84d6284137eea58c493484bf6912144a864f53c3de" checksum = "d72591d92aa26f51aa81812b9bc15cc6e82d8f1e99d5bf14c72a0c9ef204adc9"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"libc", "libc",
"serde",
"serde_json",
"term", "term",
"term_size", "term_size",
] ]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "serde"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "simple-rust-udp-proxy" name = "simple-rust-udp-proxy"
version = "0.1.0" version = "0.1.0"
@@ -249,6 +312,8 @@ dependencies = [
"clap", "clap",
"rand 0.3.23", "rand 0.3.23",
"rust_util", "rust_util",
"serde",
"serde_json",
] ]
[[package]] [[package]]
@@ -257,6 +322,17 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "963f7d3cc59b59b9325165add223142bbf1df27655d07789f109896d353d8350"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]] [[package]]
name = "term" name = "term"
version = "0.5.2" version = "0.5.2"
@@ -293,6 +369,12 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.2" version = "0.8.2"

View File

@@ -9,5 +9,7 @@ description = "Very simple rust UDP proxy"
[dependencies] [dependencies]
rand = "0.3" rand = "0.3"
rust_util = "0.6" rust_util = { version = "0.6", features = ["use_serde"] }
clap = "2.33" clap = "2.33"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

104
src/config.rs Normal file
View File

@@ -0,0 +1,104 @@
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
// }

View File

@@ -2,6 +2,8 @@ extern crate rand;
#[macro_use] #[macro_use]
extern crate rust_util; extern crate rust_util;
mod config;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::net::UdpSocket; use std::net::UdpSocket;