Made it possible to run ALFIS without settings file. It will be created if not found.
This commit is contained in:
+45
-11
@@ -1,19 +1,26 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[allow(unused_imports)]
|
||||
use log::{debug, error, info, LevelFilter, trace, warn};
|
||||
|
||||
use crate::Bytes;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Settings {
|
||||
#[serde(default)]
|
||||
pub origin: String,
|
||||
#[serde(default)]
|
||||
pub key_file: String,
|
||||
#[serde(default = "default_listen")]
|
||||
pub listen: String,
|
||||
#[serde(default)]
|
||||
pub public: bool,
|
||||
#[serde(default)]
|
||||
pub peers: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub dns: Dns
|
||||
pub dns: Dns,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
@@ -21,17 +28,27 @@ impl Settings {
|
||||
serde_json::from_str(&settings.into())
|
||||
}
|
||||
|
||||
pub fn load(file_name: &str) -> Option<Settings> {
|
||||
match File::open(file_name) {
|
||||
pub fn load(filename: &str) -> Settings {
|
||||
match File::open(filename) {
|
||||
Ok(mut file) => {
|
||||
let mut text = String::new();
|
||||
file.read_to_string(&mut text).unwrap();
|
||||
if let Ok(settings) = toml::from_str(&text) {
|
||||
return Some(settings);
|
||||
return settings;
|
||||
}
|
||||
None
|
||||
},
|
||||
Err(..) => None
|
||||
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!"); }
|
||||
}
|
||||
settings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,22 +61,39 @@ impl Settings {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
origin: "".to_string(),
|
||||
key_file: "".to_string(),
|
||||
listen: String::from("[::]:4244"),
|
||||
public: false,
|
||||
peers: vec![],
|
||||
dns: Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Dns {
|
||||
#[serde(default = "default_listen")]
|
||||
#[serde(default = "default_listen_dns")]
|
||||
pub listen: String,
|
||||
#[serde(default = "default_threads")]
|
||||
pub threads: usize,
|
||||
pub forwarders: Vec<String>
|
||||
pub forwarders: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for Dns {
|
||||
fn default() -> Self {
|
||||
Dns { listen: String::from("0.0.0.0:53"), threads: 20, forwarders: Vec::new() }
|
||||
Dns { listen: String::from("0.0.0.0:53"), threads: 20, forwarders: vec!["94.140.14.14:53".to_owned(), "94.140.15.15:53".to_owned()] }
|
||||
}
|
||||
}
|
||||
|
||||
fn default_listen() -> String {
|
||||
String::from("[::]:4244")
|
||||
}
|
||||
|
||||
fn default_listen_dns() -> String {
|
||||
String::from("0.0.0.0:53")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user