Compare commits

...

3 Commits

Author SHA1 Message Date
dfb29398f6 chore: add Cargo.lock 2020-09-19 12:01:17 +08:00
4b41c0eea0 chore: resolve con 2020-09-19 11:59:11 +08:00
ff22020a79 feat: add config 2020-09-19 11:56:45 +08:00
4 changed files with 193 additions and 3 deletions

86
Cargo.lock generated
View File

@@ -142,6 +142,12 @@ dependencies = [
"libc",
]
[[package]]
name = "itoa"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "lazy_static"
version = "1.4.0"
@@ -154,6 +160,24 @@ version = "0.2.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
[[package]]
name = "proc-macro2"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c"
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]]
name = "rand"
version = "0.3.23"
@@ -232,16 +256,55 @@ dependencies = [
[[package]]
name = "rust_util"
version = "0.6.14"
version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7222f977acb4264fb55f1aa7cf11e09c735fe961b369aef92eea670949628498"
checksum = "d72591d92aa26f51aa81812b9bc15cc6e82d8f1e99d5bf14c72a0c9ef204adc9"
dependencies = [
"lazy_static",
"libc",
"serde",
"serde_json",
"term",
"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.116"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96fe57af81d28386a513cbc6858332abc6117cfdb5999647c6444b8f43a370a5"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.116"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f630a6370fd8e457873b4bd2ffdae75408bc291ba72be773772a4c2a065d9ae8"
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]]
name = "simple-rust-udp-proxy"
version = "0.1.0"
@@ -249,6 +312,8 @@ dependencies = [
"clap",
"rand 0.3.23",
"rust_util",
"serde",
"serde_json",
]
[[package]]
@@ -257,6 +322,17 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "term"
version = "0.5.2"
@@ -293,6 +369,12 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "vec_map"
version = "0.8.2"

View File

@@ -9,5 +9,7 @@ description = "Very simple rust UDP proxy"
[dependencies]
rand = "0.3"
rust_util = "0.6"
rust_util = { version = "0.6", features = ["use_serde"] }
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]
extern crate rust_util;
mod config;
use std::collections::HashMap;
use std::env;
use std::net::UdpSocket;