Start to modularize
This commit is contained in:
parent
13cd2375d8
commit
643d6980e5
5 changed files with 178 additions and 49 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
target/
|
target/
|
||||||
|
*.swp
|
||||||
|
|
119
Cargo.lock
generated
119
Cargo.lock
generated
|
@ -1,5 +1,14 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "0.7.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.26"
|
version = "1.0.26"
|
||||||
|
@ -17,6 +26,17 @@ dependencies = [
|
||||||
"url 1.7.2",
|
"url 1.7.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "atty"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"winapi 0.3.8",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
|
@ -105,7 +125,10 @@ name = "diplonat"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"igd",
|
"igd",
|
||||||
|
"log",
|
||||||
|
"pretty_env_logger",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -124,6 +147,19 @@ dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_logger"
|
||||||
|
version = "0.7.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"humantime",
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
|
"termcolor",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fnv"
|
name = "fnv"
|
||||||
version = "1.0.6"
|
version = "1.0.6"
|
||||||
|
@ -297,6 +333,15 @@ dependencies = [
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.1.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "0.1.21"
|
version = "0.1.21"
|
||||||
|
@ -335,6 +380,15 @@ version = "1.3.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
|
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "humantime"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
|
||||||
|
dependencies = [
|
||||||
|
"quick-error",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hyper"
|
name = "hyper"
|
||||||
version = "0.13.2"
|
version = "0.13.2"
|
||||||
|
@ -663,6 +717,16 @@ version = "0.2.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
|
checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pretty_env_logger"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
|
||||||
|
dependencies = [
|
||||||
|
"env_logger",
|
||||||
|
"log",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro-hack"
|
name = "proc-macro-hack"
|
||||||
version = "0.5.11"
|
version = "0.5.11"
|
||||||
|
@ -689,6 +753,12 @@ dependencies = [
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quick-error"
|
||||||
|
version = "1.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
|
@ -782,6 +852,24 @@ version = "0.1.56"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
|
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.3.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a6020f034922e3194c711b82a627453881bc4682166cabb07134a10c26ba7692"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
"thread_local",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.6.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7fe5bd57d1d7414c6b5ed48563a2c855d995ff777729dcd91c369ec7fea395ae"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "remove_dir_all"
|
name = "remove_dir_all"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
|
@ -866,9 +954,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.104"
|
version = "1.0.107"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
|
checksum = "eba7550f2cdf88ffc23ab0f1607133486c390a8c0f89b57e589b9654ee15e04d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
|
@ -936,6 +1024,24 @@ dependencies = [
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.8",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thread_local"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.1.42"
|
version = "0.1.42"
|
||||||
|
@ -1240,6 +1346,15 @@ version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||||
|
dependencies = [
|
||||||
|
"winapi 0.3.8",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|
|
@ -9,4 +9,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
reqwest = { version = "0.10", features = ["json"] }
|
reqwest = { version = "0.10", features = ["json"] }
|
||||||
igd = { version = "0.10.0", features = ["aio"] }
|
igd = { version = "0.10.0", features = ["aio"] }
|
||||||
|
log = "0.4"
|
||||||
|
pretty_env_logger = "0.4"
|
||||||
tokio = "0.2.11"
|
tokio = "0.2.11"
|
||||||
|
serde = "1.0.107"
|
||||||
|
|
49
src/config.rs
Normal file
49
src/config.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
use log::*;
|
||||||
|
|
||||||
|
pub struct DiplonatConfig {
|
||||||
|
pub private_ip: String,
|
||||||
|
pub consul_node_name: String,
|
||||||
|
pub refresh_time: u32,
|
||||||
|
pub expiration_time: u32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_env() -> Result<DiplonatConfig, String> {
|
||||||
|
let env_private_ip = "DIPLONAT_PRIVATE_IP";
|
||||||
|
let private_ip = match env::var(env_private_ip) {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_private_ip, e)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let env_refresh_time = "DIPLONAT_REFRESH_TIME";
|
||||||
|
let refresh_time: u32 = match env::var(env_refresh_time) {
|
||||||
|
Ok(val) => val.parse().unwrap(),
|
||||||
|
Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_refresh_time,e))
|
||||||
|
};
|
||||||
|
|
||||||
|
let env_expiration_time = "DIPLONAT_EXPIRATION_TIME";
|
||||||
|
let expiration_time: u32 = match env::var(env_expiration_time) {
|
||||||
|
Ok(val) => val.parse().unwrap(),
|
||||||
|
Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_expiration_time,e))
|
||||||
|
};
|
||||||
|
|
||||||
|
let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME";
|
||||||
|
let consul_node_name = match env::var(env_consul_node_name) {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_consul_node_name,e))
|
||||||
|
};
|
||||||
|
|
||||||
|
if refresh_time * 2 > expiration_time {
|
||||||
|
return Err(format!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", expiration_time, refresh_time))
|
||||||
|
}
|
||||||
|
|
||||||
|
let config = DiplonatConfig { private_ip: private_ip, refresh_time: refresh_time, expiration_time: expiration_time, consul_node_name: consul_node_name };
|
||||||
|
info!("Consul node name: {}", config.consul_node_name);
|
||||||
|
info!("Private IP address: {}", config.private_ip);
|
||||||
|
info!("Refresh time: {} seconds", config.refresh_time);
|
||||||
|
info!("Expiration time: {} seconds", config.expiration_time);
|
||||||
|
return Ok(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
55
src/main.rs
55
src/main.rs
|
@ -1,58 +1,18 @@
|
||||||
use std::env;
|
|
||||||
use std::net::SocketAddrV4;
|
use std::net::SocketAddrV4;
|
||||||
|
//use std::collections::HashMap;
|
||||||
|
|
||||||
|
use log::*;
|
||||||
|
|
||||||
use igd::aio::search_gateway;
|
use igd::aio::search_gateway;
|
||||||
use igd::PortMappingProtocol;
|
use igd::PortMappingProtocol;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
mod config;
|
||||||
|
|
||||||
struct DiplonatConfig {
|
|
||||||
private_ip: String,
|
|
||||||
consul_node_name: String,
|
|
||||||
refresh_time: u32,
|
|
||||||
expiration_time: u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fetch_configuration() -> Result<DiplonatConfig, String> {
|
|
||||||
let env_private_ip = "DIPLONAT_PRIVATE_IP";
|
|
||||||
let private_ip = match env::var(env_private_ip) {
|
|
||||||
Ok(val) => val,
|
|
||||||
Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_private_ip, e)),
|
|
||||||
};
|
|
||||||
|
|
||||||
let env_refresh_time = "DIPLONAT_REFRESH_TIME";
|
|
||||||
let refresh_time: u32 = match env::var(env_refresh_time) {
|
|
||||||
Ok(val) => val.parse().unwrap(),
|
|
||||||
Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_refresh_time,e))
|
|
||||||
};
|
|
||||||
|
|
||||||
let env_expiration_time = "DIPLONAT_EXPIRATION_TIME";
|
|
||||||
let expiration_time: u32 = match env::var(env_expiration_time) {
|
|
||||||
Ok(val) => val.parse().unwrap(),
|
|
||||||
Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_expiration_time,e))
|
|
||||||
};
|
|
||||||
|
|
||||||
let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME";
|
|
||||||
let consul_node_name = match env::var(env_consul_node_name) {
|
|
||||||
Ok(val) => val,
|
|
||||||
Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_consul_node_name,e))
|
|
||||||
};
|
|
||||||
|
|
||||||
if refresh_time * 2 > expiration_time {
|
|
||||||
return Err(format!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", expiration_time, refresh_time))
|
|
||||||
}
|
|
||||||
|
|
||||||
let config = DiplonatConfig { private_ip: private_ip, refresh_time: refresh_time, expiration_time: expiration_time, consul_node_name: consul_node_name };
|
|
||||||
println!("\tConsul node name: {}", config.consul_node_name);
|
|
||||||
println!("\tPrivate IP address: {}", config.private_ip);
|
|
||||||
println!("\tRefresh time: {} seconds", config.refresh_time);
|
|
||||||
println!("\tExpiration time: {} seconds", config.expiration_time);
|
|
||||||
return Ok(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let config = match fetch_configuration() {
|
pretty_env_logger::init();
|
||||||
|
|
||||||
|
let config = match config::load_env() {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(e) => return println!("unable to build configuration: {}", e),
|
Err(e) => return println!("unable to build configuration: {}", e),
|
||||||
};
|
};
|
||||||
|
@ -69,6 +29,7 @@ async fn main() {
|
||||||
Ok(g) => g,
|
Ok(g) => g,
|
||||||
Err(err) => return println!("Faild to find IGD: {}", err),
|
Err(err) => return println!("Faild to find IGD: {}", err),
|
||||||
};
|
};
|
||||||
|
info!("Gateway: {}", gateway);
|
||||||
|
|
||||||
let service = format!("{}:{}", config.private_ip, 1234);
|
let service = format!("{}:{}", config.private_ip, 1234);
|
||||||
let service: SocketAddrV4 = service.parse().expect("Invalid socket address");
|
let service: SocketAddrV4 = service.parse().expect("Invalid socket address");
|
||||||
|
|
Loading…
Reference in a new issue