2021-02-19 16:41:43 +01:00
|
|
|
use std::fs::File;
|
2021-03-16 14:00:14 +01:00
|
|
|
use std::io::{Read, Write};
|
2021-02-19 16:41:43 +01:00
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-16 14:00:14 +01:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
|
use log::{debug, error, info, LevelFilter, trace, warn};
|
2021-02-19 16:41:43 +01:00
|
|
|
|
|
|
|
|
use crate::Bytes;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct Settings {
|
2021-03-16 14:00:14 +01:00
|
|
|
#[serde(default)]
|
2021-02-19 16:41:43 +01:00
|
|
|
pub origin: String,
|
2021-03-16 14:00:14 +01:00
|
|
|
#[serde(default)]
|
2021-02-19 16:41:43 +01:00
|
|
|
pub key_file: String,
|
2021-03-16 14:00:14 +01:00
|
|
|
#[serde(default)]
|
2021-03-31 16:50:22 +02:00
|
|
|
pub net: Net,
|
2021-02-19 16:41:43 +01:00
|
|
|
#[serde(default)]
|
2021-03-16 14:00:14 +01:00
|
|
|
pub dns: Dns,
|
2021-03-29 11:10:48 +02:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub mining: Mining,
|
2021-02-19 16:41:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
|
pub fn new<S: Into<String>>(settings: S) -> serde_json::Result<Settings> {
|
|
|
|
|
serde_json::from_str(&settings.into())
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 14:00:14 +01:00
|
|
|
pub fn load(filename: &str) -> Settings {
|
|
|
|
|
match File::open(filename) {
|
2021-02-19 16:41:43 +01:00
|
|
|
Ok(mut file) => {
|
|
|
|
|
let mut text = String::new();
|
|
|
|
|
file.read_to_string(&mut text).unwrap();
|
2021-03-06 22:40:19 +01:00
|
|
|
if let Ok(settings) = toml::from_str(&text) {
|
2021-03-16 14:00:14 +01:00
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
Settings::default()
|
|
|
|
|
}
|
|
|
|
|
Err(..) => {
|
|
|
|
|
let settings = Settings::default();
|
|
|
|
|
let string = toml::to_string(&settings).unwrap();
|
|
|
|
|
match File::create(filename) {
|
|
|
|
|
Ok(mut f) => {
|
|
|
|
|
f.write_all(string.as_bytes()).expect("Error saving settings!");
|
|
|
|
|
}
|
|
|
|
|
Err(_) => { error!("Error saving settings file!"); }
|
2021-02-19 16:41:43 +01:00
|
|
|
}
|
2021-03-16 14:00:14 +01:00
|
|
|
settings
|
|
|
|
|
}
|
2021-02-19 16:41:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_origin(&self) -> Bytes {
|
|
|
|
|
if self.origin.eq("") {
|
|
|
|
|
return Bytes::zero32();
|
|
|
|
|
}
|
|
|
|
|
let origin = crate::from_hex(&self.origin).expect("Wrong origin in settings");
|
|
|
|
|
Bytes::from_bytes(origin.as_slice())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 14:00:14 +01:00
|
|
|
impl Default for Settings {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
2021-03-31 16:50:22 +02:00
|
|
|
origin: String::from("00000102C2F9BFD2803284D93327F089D60FC72A06F19AF2384567F2646B8348"),
|
|
|
|
|
key_file: String::from("default.key"),
|
|
|
|
|
net: Net::default(),
|
2021-03-29 11:10:48 +02:00
|
|
|
dns: Default::default(),
|
|
|
|
|
mining: Mining::default()
|
2021-03-16 14:00:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 16:41:43 +01:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct Dns {
|
2021-03-16 14:00:14 +01:00
|
|
|
#[serde(default = "default_listen_dns")]
|
2021-03-06 22:54:17 +01:00
|
|
|
pub listen: String,
|
|
|
|
|
#[serde(default = "default_threads")]
|
|
|
|
|
pub threads: usize,
|
2021-03-16 14:00:14 +01:00
|
|
|
pub forwarders: Vec<String>,
|
2021-04-01 20:59:52 +02:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub hosts: Vec<String>,
|
2021-02-19 16:41:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Dns {
|
|
|
|
|
fn default() -> Self {
|
2021-03-31 16:50:22 +02:00
|
|
|
Dns {
|
|
|
|
|
listen: String::from("127.0.0.1:53"),
|
|
|
|
|
threads: 20,
|
2021-04-01 20:59:52 +02:00
|
|
|
forwarders: vec![String::from("94.140.14.14:53"), String::from("94.140.15.15:53")],
|
|
|
|
|
hosts: Vec::new()
|
2021-03-31 16:50:22 +02:00
|
|
|
}
|
2021-02-19 16:41:43 +01:00
|
|
|
}
|
2021-02-22 22:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-29 11:10:48 +02:00
|
|
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
|
|
|
|
pub struct Mining {
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub threads: usize
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 16:50:22 +02:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct Net {
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub peers: Vec<String>,
|
|
|
|
|
#[serde(default = "default_listen")]
|
|
|
|
|
pub listen: String,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub public: bool,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub yggdrasil_only: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Net {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Net {
|
|
|
|
|
peers: vec![String::from("test-ip4.alfis.name:4244"), String::from("test-ip6.alfis.name:4244")],
|
|
|
|
|
listen: String::from("[::]:4244"),
|
|
|
|
|
public: true,
|
|
|
|
|
yggdrasil_only: false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-06 22:54:17 +01:00
|
|
|
fn default_listen() -> String {
|
2021-03-16 14:00:14 +01:00
|
|
|
String::from("[::]:4244")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn default_listen_dns() -> String {
|
2021-03-06 22:54:17 +01:00
|
|
|
String::from("0.0.0.0:53")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn default_threads() -> usize {
|
|
|
|
|
20
|
2021-02-19 16:41:43 +01:00
|
|
|
}
|