save config
This commit is contained in:
parent
4a590c1f7f
commit
b7791c0882
@ -1,12 +1,17 @@
|
|||||||
use std::{env, fs, io::Read, path::PathBuf};
|
use std::{
|
||||||
|
env, fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, Deserialize, Debug)]
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Bind on address address. eg. `127.0.0.1:1080`
|
/// Bind on address address. eg. `127.0.0.1:1080`
|
||||||
|
#[serde(default = "default_listen_addr")]
|
||||||
pub listen_addr: String,
|
pub listen_addr: String,
|
||||||
/// Request timeout
|
/// Request timeout
|
||||||
|
#[serde(default = "default_timeout")]
|
||||||
pub request_timeout: u64,
|
pub request_timeout: u64,
|
||||||
/// Authentication
|
/// Authentication
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
@ -29,8 +34,16 @@ fn default_true() -> bool {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_timeout() -> u64 {
|
||||||
|
60
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_listen_addr() -> String {
|
||||||
|
"127.0.0.1:1080".into()
|
||||||
|
}
|
||||||
|
|
||||||
/// Password authentication data
|
/// Password authentication data
|
||||||
#[derive(Clone, Deserialize, Debug)]
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
pub struct PasswordAuth {
|
pub struct PasswordAuth {
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
@ -39,8 +52,8 @@ pub struct PasswordAuth {
|
|||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Config {
|
Config {
|
||||||
listen_addr: "127.0.0.1:1080".into(),
|
listen_addr: default_listen_addr(),
|
||||||
request_timeout: 120,
|
request_timeout: default_timeout(),
|
||||||
auth: None,
|
auth: None,
|
||||||
skip_auth: false,
|
skip_auth: false,
|
||||||
dns_resolve: true,
|
dns_resolve: true,
|
||||||
@ -52,28 +65,29 @@ impl Default for Config {
|
|||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
const FILENAME: &'static str = "config.toml";
|
const FILENAME: &'static str = "config.toml";
|
||||||
fn read(filename: &str) -> Result<Self, &str> {
|
fn read<P: AsRef<Path>>(path: P) -> Result<Self, String> {
|
||||||
let mut file = fs::File::open(filename).map_err(|_| "can't open config")?;
|
let data = fs::read(path).map_err(|e| format!("can't read config: {:?}", e))?;
|
||||||
let mut data = vec![];
|
toml::from_slice(&data).map_err(|e| format!("can't parse config: {:?}", e))
|
||||||
file.read_to_end(&mut data)
|
|
||||||
.map_err(|_| "can't read config")?;
|
|
||||||
toml::from_slice(&data).map_err(|_| "can't parse config")
|
|
||||||
}
|
}
|
||||||
fn file_location() -> Result<PathBuf, &'static str> {
|
fn write<P: AsRef<Path>>(&self, path: P) -> Result<(), String> {
|
||||||
let mut res = env::current_exe().map_err(|_| "can't get current exe path")?;
|
let data = toml::to_vec(&self).map_err(|e| format!("can't serialize config: {:?}", e))?;
|
||||||
res.pop();
|
fs::write(path, &data).map_err(|e| format!("can't write config: {:?}", e))
|
||||||
res.push(Config::FILENAME);
|
}
|
||||||
|
fn file_location() -> Result<PathBuf, String> {
|
||||||
|
let res = env::current_exe()
|
||||||
|
.map_err(|e| format!("can't get current exe path: {:?}", e))?
|
||||||
|
.with_file_name(Config::FILENAME);
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
pub fn get() -> Self {
|
pub fn get() -> Self {
|
||||||
let path = Config::file_location();
|
let path = Config::file_location();
|
||||||
if path.is_err() {
|
if let Err(e) = path {
|
||||||
log::error!("Error: {}, using default config", path.err().unwrap());
|
log::error!("Error: {e}, using default config");
|
||||||
return Config::default();
|
return Config::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = path.unwrap();
|
let path = path.unwrap();
|
||||||
let cfg = Config::read(path.to_str().unwrap());
|
let cfg = Config::read(path);
|
||||||
match cfg {
|
match cfg {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Error: {e}, using default config");
|
log::error!("Error: {e}, using default config");
|
||||||
@ -82,4 +96,18 @@ impl Config {
|
|||||||
Ok(cfg) => cfg,
|
Ok(cfg) => cfg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn save(&self) {
|
||||||
|
let path = Config::file_location();
|
||||||
|
if let Err(ref e) = path {
|
||||||
|
log::error!("save error: {}", &e);
|
||||||
|
}
|
||||||
|
let path = path.unwrap();
|
||||||
|
|
||||||
|
let res = self.write(&path);
|
||||||
|
if let Err(e) = res {
|
||||||
|
log::error!("save error: {e}");
|
||||||
|
} else {
|
||||||
|
log::info!("config saved to: {}", path.to_str().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ enum Command {
|
|||||||
Stop,
|
Stop,
|
||||||
/// run service (by Windows)
|
/// run service (by Windows)
|
||||||
Run,
|
Run,
|
||||||
|
/// save default config
|
||||||
|
SaveConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SOCKS5 proxy windows service
|
/// SOCKS5 proxy windows service
|
||||||
@ -62,6 +64,10 @@ fn main() {
|
|||||||
Command::Run => service::run(),
|
Command::Run => service::run(),
|
||||||
Command::Start => service::start(),
|
Command::Start => service::start(),
|
||||||
Command::Stop => service::stop(),
|
Command::Stop => service::stop(),
|
||||||
|
Command::SaveConfig => {
|
||||||
|
config::Config::default().save();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = res {
|
if let Err(e) = res {
|
||||||
|
Loading…
Reference in New Issue
Block a user