aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2020-05-09 15:33:02 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2020-05-09 15:33:02 +0200
commit643d6980e5c0f04928edf05219f735495e5589a5 (patch)
tree47bf57a2a1f1834f545903f155b8758f3e17a595
parent13cd2375d84e656fd6531c86175f9a87259f7070 (diff)
downloaddiplonat-643d6980e5c0f04928edf05219f735495e5589a5.tar.gz
diplonat-643d6980e5c0f04928edf05219f735495e5589a5.zip
Start to modularize
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock119
-rw-r--r--Cargo.toml3
-rw-r--r--src/config.rs49
-rw-r--r--src/main.rs55
5 files changed, 178 insertions, 49 deletions
diff --git a/.gitignore b/.gitignore
index 2f7896d..855a698 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
target/
+*.swp
diff --git a/Cargo.lock b/Cargo.lock
index 2d50bdd..9970039 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,6 +1,15 @@
# This file is automatically @generated by Cargo.
# 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]]
name = "anyhow"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -18,6 +27,17 @@ dependencies = [
]
[[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]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -105,7 +125,10 @@ name = "diplonat"
version = "0.1.0"
dependencies = [
"igd",
+ "log",
+ "pretty_env_logger",
"reqwest",
+ "serde",
"tokio",
]
@@ -125,6 +148,19 @@ dependencies = [
]
[[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]]
name = "fnv"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -298,6 +334,15 @@ dependencies = [
]
[[package]]
+name = "hermit-abi"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4"
+dependencies = [
+ "libc",
+]
+
+[[package]]
name = "http"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -336,6 +381,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "hyper"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -664,6 +718,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "proc-macro-hack"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -690,6 +754,12 @@ dependencies = [
]
[[package]]
+name = "quick-error"
+version = "1.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
+
+[[package]]
name = "quote"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -783,6 +853,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "remove_dir_all"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -866,9 +954,9 @@ dependencies = [
[[package]]
name = "serde"
-version = "1.0.104"
+version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
+checksum = "eba7550f2cdf88ffc23ab0f1607133486c390a8c0f89b57e589b9654ee15e04d"
[[package]]
name = "serde_json"
@@ -937,6 +1025,24 @@ dependencies = [
]
[[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]]
name = "time"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1241,6 +1347,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 7a6266c..7771e70 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,4 +9,7 @@ edition = "2018"
[dependencies]
reqwest = { version = "0.10", features = ["json"] }
igd = { version = "0.10.0", features = ["aio"] }
+log = "0.4"
+pretty_env_logger = "0.4"
tokio = "0.2.11"
+serde = "1.0.107"
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..950f4fe
--- /dev/null
+++ b/src/config.rs
@@ -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);
+}
+
+
diff --git a/src/main.rs b/src/main.rs
index 09e9ecd..4a08ecc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,58 +1,18 @@
-use std::env;
use std::net::SocketAddrV4;
+//use std::collections::HashMap;
+
+use log::*;
use igd::aio::search_gateway;
use igd::PortMappingProtocol;
-use std::collections::HashMap;
-
-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);
-}
+mod config;
#[tokio::main]
async fn main() {
- let config = match fetch_configuration() {
+ pretty_env_logger::init();
+
+ let config = match config::load_env() {
Ok(val) => val,
Err(e) => return println!("unable to build configuration: {}", e),
};
@@ -69,6 +29,7 @@ async fn main() {
Ok(g) => g,
Err(err) => return println!("Faild to find IGD: {}", err),
};
+ info!("Gateway: {}", gateway);
let service = format!("{}:{}", config.private_ip, 1234);
let service: SocketAddrV4 = service.parse().expect("Invalid socket address");