2022-11-14 02:56:25 +00:00
|
|
|
// Gupax - GUI Uniting P2Pool And XMRig
|
|
|
|
//
|
|
|
|
// Copyright (c) 2022 hinto-janaiyo
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// This handles reading/writing the disk files:
|
|
|
|
// - [state.toml] -> [App] state
|
|
|
|
// - [nodes.toml] -> [Manual Nodes] list
|
|
|
|
// The TOML format is used. This struct hierarchy
|
|
|
|
// directly translates into the TOML parser:
|
|
|
|
// State/
|
|
|
|
// ├─ Gupax/
|
|
|
|
// │ ├─ ...
|
|
|
|
// ├─ P2pool/
|
|
|
|
// │ ├─ ...
|
|
|
|
// ├─ Xmrig/
|
|
|
|
// │ ├─ ...
|
|
|
|
// ├─ Version/
|
|
|
|
// ├─ ...
|
|
|
|
|
2022-11-16 19:40:25 +00:00
|
|
|
use std::{
|
|
|
|
fs,
|
|
|
|
fmt::Display,
|
|
|
|
path::PathBuf,
|
|
|
|
result::Result,
|
|
|
|
sync::{Arc,Mutex},
|
|
|
|
fmt::Write,
|
|
|
|
};
|
2022-11-14 02:56:25 +00:00
|
|
|
use serde::{Serialize,Deserialize};
|
|
|
|
use figment::Figment;
|
|
|
|
use figment::providers::{Format,Toml};
|
2022-11-23 04:21:46 +00:00
|
|
|
use crate::{
|
|
|
|
constants::*,
|
|
|
|
gupax::Ratio,
|
2022-12-06 03:33:35 +00:00
|
|
|
Tab,
|
2022-11-23 04:21:46 +00:00
|
|
|
};
|
2022-11-14 02:56:25 +00:00
|
|
|
use log::*;
|
|
|
|
|
2022-11-23 04:21:46 +00:00
|
|
|
//---------------------------------------------------------------------------------------------------- Const
|
|
|
|
// State file
|
2022-11-24 04:03:56 +00:00
|
|
|
const ERROR: &str = "Disk error";
|
|
|
|
const PATH_ERROR: &str = "PATH for state directory could not be not found";
|
2022-11-23 04:21:46 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
const DIRECTORY: &'static str = r#"Gupax\"#;
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
const DIRECTORY: &'static str = "Gupax/";
|
|
|
|
#[cfg(target_os = "linux")]
|
2022-11-24 04:03:56 +00:00
|
|
|
const DIRECTORY: &str = "gupax/";
|
2022-11-23 04:21:46 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
pub const DEFAULT_P2POOL_PATH: &'static str = r"P2Pool\p2pool.exe";
|
|
|
|
#[cfg(target_family = "unix")]
|
2022-11-24 04:03:56 +00:00
|
|
|
pub const DEFAULT_P2POOL_PATH: &str = "p2pool/p2pool";
|
2022-11-23 04:21:46 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
pub const DEFAULT_XMRIG_PATH: &'static str = r"XMRig\xmrig.exe";
|
|
|
|
#[cfg(target_family = "unix")]
|
2022-11-24 04:03:56 +00:00
|
|
|
pub const DEFAULT_XMRIG_PATH: &str = "xmrig/xmrig";
|
2022-11-23 04:21:46 +00:00
|
|
|
|
2022-11-14 02:56:25 +00:00
|
|
|
//---------------------------------------------------------------------------------------------------- General functions for all [File]'s
|
|
|
|
// get_file_path() | Return absolute path to OS data path + filename
|
|
|
|
// read_to_string() | Convert the file at a given path into a [String]
|
|
|
|
// create_new() | Write a default TOML Struct into the appropriate file (in OS data path)
|
|
|
|
// into_absolute_path() | Convert relative -> absolute path
|
|
|
|
|
2022-11-20 18:31:00 +00:00
|
|
|
pub fn get_gupax_data_path() -> Result<PathBuf, TomlError> {
|
2022-11-14 02:56:25 +00:00
|
|
|
// Get OS data folder
|
2022-11-20 18:31:00 +00:00
|
|
|
// Linux | $XDG_DATA_HOME or $HOME/.local/share/gupax | /home/alice/.local/state/gupax
|
|
|
|
// macOS | $HOME/Library/Application Support/Gupax | /Users/Alice/Library/Application Support/Gupax
|
|
|
|
// Windows | {FOLDERID_RoamingAppData}\Gupax | C:\Users\Alice\AppData\Roaming\Gupax
|
|
|
|
match dirs::data_dir() {
|
2022-11-14 02:56:25 +00:00
|
|
|
Some(mut path) => {
|
|
|
|
path.push(DIRECTORY);
|
2022-11-21 01:21:47 +00:00
|
|
|
info!("OS | Data path ... {}", path.display());
|
2022-11-20 19:20:25 +00:00
|
|
|
create_gupax_dir(&path)?;
|
2022-11-20 18:31:00 +00:00
|
|
|
Ok(path)
|
2022-11-14 02:56:25 +00:00
|
|
|
},
|
2022-11-20 18:31:00 +00:00
|
|
|
None => { error!("OS | Data path ... FAIL"); Err(TomlError::Path(PATH_ERROR.to_string())) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 19:20:25 +00:00
|
|
|
pub fn create_gupax_dir(path: &PathBuf) -> Result<(), TomlError> {
|
2022-11-14 02:56:25 +00:00
|
|
|
// Create directory
|
2022-11-20 19:20:25 +00:00
|
|
|
match fs::create_dir_all(path) {
|
|
|
|
Ok(_) => { info!("OS | Create data path ... OK"); Ok(()) },
|
|
|
|
Err(e) => { error!("OS | Create data path ... FAIL ... {}", e); Err(TomlError::Io(e)) },
|
|
|
|
}
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a [File] path to a [String]
|
|
|
|
pub fn read_to_string(file: File, path: &PathBuf) -> Result<String, TomlError> {
|
2022-11-24 04:03:56 +00:00
|
|
|
match fs::read_to_string(path) {
|
2022-11-14 02:56:25 +00:00
|
|
|
Ok(string) => {
|
|
|
|
info!("{:?} | Read ... OK", file);
|
|
|
|
Ok(string)
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
warn!("{:?} | Read ... FAIL", file);
|
|
|
|
Err(TomlError::Io(err))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 04:03:56 +00:00
|
|
|
// Write str to console with [info!] surrounded by "---"
|
2022-12-03 18:37:57 +00:00
|
|
|
pub fn print_dash(toml: &str) {
|
2022-11-14 02:56:25 +00:00
|
|
|
info!("{}", HORIZONTAL);
|
|
|
|
for i in toml.lines() { info!("{}", i); }
|
|
|
|
info!("{}", HORIZONTAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn relative paths into absolute paths
|
|
|
|
pub fn into_absolute_path(path: String) -> Result<PathBuf, TomlError> {
|
|
|
|
let path = PathBuf::from(path);
|
|
|
|
if path.is_relative() {
|
|
|
|
let mut dir = std::env::current_exe()?;
|
|
|
|
dir.pop();
|
|
|
|
dir.push(path);
|
|
|
|
Ok(dir)
|
|
|
|
} else {
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------- [State] Impl
|
|
|
|
impl State {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let max_threads = num_cpus::get();
|
2022-11-24 04:03:56 +00:00
|
|
|
let current_threads = if max_threads == 1 { 1 } else { max_threads / 2 };
|
2022-11-14 02:56:25 +00:00
|
|
|
Self {
|
|
|
|
gupax: Gupax {
|
2022-11-23 04:21:46 +00:00
|
|
|
simple: true,
|
2022-11-14 02:56:25 +00:00
|
|
|
auto_update: true,
|
|
|
|
auto_node: true,
|
|
|
|
ask_before_quit: true,
|
|
|
|
save_before_quit: true,
|
cargo/tor/p2pool: clean deps, warn macos arti, fix node overflow
Cargo: Cleanup unused dependencies, enable some build optimizations
Tor: Arti doesn't seem to work on macOS
Even a bare Arti+Hyper request doesn't seem to work, so it's
probably not something to do with Gupax. A lot of issues only
seem to popup in a VM (OpenGL, TLS) even though on bare metal
Gupax runs fine, so Tor might work fine on real macOS but I don't
have real macOS to test it. VM macOS can't create a circuit, so,
disable by default and add a warning that it's unstable.
P2Pool: Let selected_index start at 0, and only +1 when printing
to the user, this makes the overflow math when adding/deleting a
lot more simple because selected_index will match the actual index
of the node vector
2022-11-21 22:16:31 +00:00
|
|
|
#[cfg(not(target_os = "macos"))]
|
2022-11-14 02:56:25 +00:00
|
|
|
update_via_tor: true,
|
cargo/tor/p2pool: clean deps, warn macos arti, fix node overflow
Cargo: Cleanup unused dependencies, enable some build optimizations
Tor: Arti doesn't seem to work on macOS
Even a bare Arti+Hyper request doesn't seem to work, so it's
probably not something to do with Gupax. A lot of issues only
seem to popup in a VM (OpenGL, TLS) even though on bare metal
Gupax runs fine, so Tor might work fine on real macOS but I don't
have real macOS to test it. VM macOS can't create a circuit, so,
disable by default and add a warning that it's unstable.
P2Pool: Let selected_index start at 0, and only +1 when printing
to the user, this makes the overflow math when adding/deleting a
lot more simple because selected_index will match the actual index
of the node vector
2022-11-21 22:16:31 +00:00
|
|
|
#[cfg(target_os = "macos")] // Arti library has issues on macOS
|
|
|
|
update_via_tor: false,
|
2022-11-14 02:56:25 +00:00
|
|
|
p2pool_path: DEFAULT_P2POOL_PATH.to_string(),
|
|
|
|
xmrig_path: DEFAULT_XMRIG_PATH.to_string(),
|
|
|
|
absolute_p2pool_path: into_absolute_path(DEFAULT_P2POOL_PATH.to_string()).unwrap(),
|
|
|
|
absolute_xmrig_path: into_absolute_path(DEFAULT_XMRIG_PATH.to_string()).unwrap(),
|
2022-11-23 04:21:46 +00:00
|
|
|
selected_width: APP_DEFAULT_WIDTH as u16,
|
|
|
|
selected_height: APP_DEFAULT_HEIGHT as u16,
|
|
|
|
ratio: Ratio::Width,
|
2022-12-06 03:33:35 +00:00
|
|
|
tab: Tab::About,
|
2022-11-14 02:56:25 +00:00
|
|
|
},
|
|
|
|
p2pool: P2pool {
|
|
|
|
simple: true,
|
|
|
|
mini: true,
|
|
|
|
auto_node: true,
|
|
|
|
auto_select: true,
|
|
|
|
out_peers: 10,
|
|
|
|
in_peers: 10,
|
|
|
|
log_level: 3,
|
|
|
|
node: crate::NodeEnum::C3pool,
|
|
|
|
arguments: String::new(),
|
2022-12-03 18:37:57 +00:00
|
|
|
address: String::with_capacity(96),
|
2022-11-14 02:56:25 +00:00
|
|
|
name: "Local Monero Node".to_string(),
|
|
|
|
ip: "localhost".to_string(),
|
|
|
|
rpc: "18081".to_string(),
|
|
|
|
zmq: "18083".to_string(),
|
cargo/tor/p2pool: clean deps, warn macos arti, fix node overflow
Cargo: Cleanup unused dependencies, enable some build optimizations
Tor: Arti doesn't seem to work on macOS
Even a bare Arti+Hyper request doesn't seem to work, so it's
probably not something to do with Gupax. A lot of issues only
seem to popup in a VM (OpenGL, TLS) even though on bare metal
Gupax runs fine, so Tor might work fine on real macOS but I don't
have real macOS to test it. VM macOS can't create a circuit, so,
disable by default and add a warning that it's unstable.
P2Pool: Let selected_index start at 0, and only +1 when printing
to the user, this makes the overflow math when adding/deleting a
lot more simple because selected_index will match the actual index
of the node vector
2022-11-21 22:16:31 +00:00
|
|
|
selected_index: 0,
|
2022-11-14 02:56:25 +00:00
|
|
|
selected_name: "Local Monero Node".to_string(),
|
|
|
|
selected_ip: "localhost".to_string(),
|
|
|
|
selected_rpc: "18081".to_string(),
|
|
|
|
selected_zmq: "18083".to_string(),
|
|
|
|
},
|
|
|
|
xmrig: Xmrig {
|
|
|
|
simple: true,
|
2022-11-23 04:21:46 +00:00
|
|
|
pause: 0,
|
2022-12-03 18:37:57 +00:00
|
|
|
simple_rig: String::with_capacity(30),
|
|
|
|
arguments: String::with_capacity(300),
|
|
|
|
address: String::with_capacity(96),
|
2022-11-23 04:21:46 +00:00
|
|
|
name: "Local P2Pool".to_string(),
|
|
|
|
rig: "Gupax".to_string(),
|
|
|
|
ip: "localhost".to_string(),
|
|
|
|
port: "3333".to_string(),
|
|
|
|
selected_index: 0,
|
|
|
|
selected_name: "Local P2Pool".to_string(),
|
|
|
|
selected_ip: "localhost".to_string(),
|
|
|
|
selected_rig: "Gupax".to_string(),
|
|
|
|
selected_port: "3333".to_string(),
|
|
|
|
api_ip: "localhost".to_string(),
|
|
|
|
api_port: "18088".to_string(),
|
2022-11-27 03:41:08 +00:00
|
|
|
tls: false,
|
|
|
|
keepalive: false,
|
2022-11-14 02:56:25 +00:00
|
|
|
current_threads,
|
|
|
|
max_threads,
|
|
|
|
},
|
|
|
|
version: Arc::new(Mutex::new(Version {
|
2022-11-19 14:39:26 +00:00
|
|
|
gupax: GUPAX_VERSION.to_string(),
|
|
|
|
p2pool: P2POOL_VERSION.to_string(),
|
|
|
|
xmrig: XMRIG_VERSION.to_string(),
|
2022-11-14 02:56:25 +00:00
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 04:03:56 +00:00
|
|
|
// Convert [&str] to [State]
|
|
|
|
pub fn from_str(string: &str) -> Result<Self, TomlError> {
|
|
|
|
match toml::de::from_str(string) {
|
2022-11-14 02:56:25 +00:00
|
|
|
Ok(state) => {
|
2022-11-16 02:19:30 +00:00
|
|
|
info!("State | Parse ... OK");
|
2022-12-03 18:37:57 +00:00
|
|
|
print_dash(string);
|
2022-11-14 02:56:25 +00:00
|
|
|
Ok(state)
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2022-11-20 02:20:28 +00:00
|
|
|
warn!("State | String -> State ... FAIL ... {}", err);
|
2022-11-14 02:56:25 +00:00
|
|
|
Err(TomlError::Deserialize(err))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combination of multiple functions:
|
|
|
|
// 1. Attempt to read file from path into [String]
|
|
|
|
// |_ Create a default file if not found
|
|
|
|
// 2. Deserialize [String] into a proper [Struct]
|
|
|
|
// |_ Attempt to merge if deserialization fails
|
2022-11-20 18:31:00 +00:00
|
|
|
pub fn get(path: &PathBuf) -> Result<Self, TomlError> {
|
2022-11-14 02:56:25 +00:00
|
|
|
// Read
|
|
|
|
let file = File::State;
|
2022-11-24 04:03:56 +00:00
|
|
|
let string = match read_to_string(file, path) {
|
2022-11-14 02:56:25 +00:00
|
|
|
Ok(string) => string,
|
|
|
|
// Create
|
|
|
|
_ => {
|
2022-11-20 18:31:00 +00:00
|
|
|
Self::create_new(path)?;
|
2022-11-24 04:03:56 +00:00
|
|
|
match read_to_string(file, path) {
|
2022-11-20 02:20:28 +00:00
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
2022-11-14 02:56:25 +00:00
|
|
|
},
|
|
|
|
};
|
2022-11-20 02:20:28 +00:00
|
|
|
// Deserialize, attempt merge if failed
|
2022-11-24 04:03:56 +00:00
|
|
|
match Self::from_str(&string) {
|
2022-11-20 02:20:28 +00:00
|
|
|
Ok(s) => Ok(s),
|
cargo/tor/p2pool: clean deps, warn macos arti, fix node overflow
Cargo: Cleanup unused dependencies, enable some build optimizations
Tor: Arti doesn't seem to work on macOS
Even a bare Arti+Hyper request doesn't seem to work, so it's
probably not something to do with Gupax. A lot of issues only
seem to popup in a VM (OpenGL, TLS) even though on bare metal
Gupax runs fine, so Tor might work fine on real macOS but I don't
have real macOS to test it. VM macOS can't create a circuit, so,
disable by default and add a warning that it's unstable.
P2Pool: Let selected_index start at 0, and only +1 when printing
to the user, this makes the overflow math when adding/deleting a
lot more simple because selected_index will match the actual index
of the node vector
2022-11-21 22:16:31 +00:00
|
|
|
Err(_) => {
|
2022-11-20 02:20:28 +00:00
|
|
|
warn!("State | Attempting merge...");
|
2022-11-20 18:31:00 +00:00
|
|
|
Self::merge(string, path)
|
2022-11-20 02:20:28 +00:00
|
|
|
},
|
|
|
|
}
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Completely overwrite current [state.toml]
|
|
|
|
// with a new default version, and return [Self].
|
2022-11-20 18:31:00 +00:00
|
|
|
pub fn create_new(path: &PathBuf) -> Result<Self, TomlError> {
|
2022-11-14 02:56:25 +00:00
|
|
|
info!("State | Creating new default...");
|
|
|
|
let new = Self::new();
|
|
|
|
let string = match toml::ser::to_string(&new) {
|
2022-11-21 01:21:47 +00:00
|
|
|
Ok(o) => o,
|
2022-11-14 02:56:25 +00:00
|
|
|
Err(e) => { error!("State | Couldn't serialize default file: {}", e); return Err(TomlError::Serialize(e)) },
|
|
|
|
};
|
2022-11-24 04:03:56 +00:00
|
|
|
fs::write(path, &string)?;
|
2022-11-14 02:56:25 +00:00
|
|
|
info!("State | Write ... OK");
|
|
|
|
Ok(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save [State] onto disk file [gupax.toml]
|
2022-11-20 18:31:00 +00:00
|
|
|
pub fn save(&mut self, path: &PathBuf) -> Result<(), TomlError> {
|
2022-11-16 19:07:27 +00:00
|
|
|
info!("State | Saving to disk...");
|
2022-11-14 02:56:25 +00:00
|
|
|
// Convert path to absolute
|
|
|
|
self.gupax.absolute_p2pool_path = into_absolute_path(self.gupax.p2pool_path.clone())?;
|
|
|
|
self.gupax.absolute_xmrig_path = into_absolute_path(self.gupax.xmrig_path.clone())?;
|
|
|
|
let string = match toml::ser::to_string(&self) {
|
|
|
|
Ok(string) => {
|
2022-11-16 19:07:27 +00:00
|
|
|
info!("State | Parse ... OK");
|
2022-12-03 18:37:57 +00:00
|
|
|
print_dash(&string);
|
2022-11-14 02:56:25 +00:00
|
|
|
string
|
|
|
|
},
|
2022-11-16 19:07:27 +00:00
|
|
|
Err(err) => { error!("State | Couldn't parse TOML into string ... FAIL"); return Err(TomlError::Serialize(err)) },
|
2022-11-14 02:56:25 +00:00
|
|
|
};
|
|
|
|
match fs::write(path, string) {
|
2022-11-16 19:07:27 +00:00
|
|
|
Ok(_) => { info!("State | Save ... OK"); Ok(()) },
|
2022-11-24 04:03:56 +00:00
|
|
|
Err(err) => { error!("State | Couldn't overwrite TOML file ... FAIL"); Err(TomlError::Io(err)) },
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 02:20:28 +00:00
|
|
|
// Take [String] as input, merge it with whatever the current [default] is,
|
2022-11-14 02:56:25 +00:00
|
|
|
// leaving behind old keys+values and updating [default] with old valid ones.
|
|
|
|
// Automatically overwrite current file.
|
2022-11-20 18:31:00 +00:00
|
|
|
pub fn merge(old: String, path: &PathBuf) -> Result<Self, TomlError> {
|
2022-11-14 02:56:25 +00:00
|
|
|
let default = match toml::ser::to_string(&Self::new()) {
|
2022-11-20 02:20:28 +00:00
|
|
|
Ok(string) => { info!("State | Default TOML parse ... OK"); string },
|
|
|
|
Err(err) => { error!("State | Couldn't parse default TOML into string"); return Err(TomlError::Serialize(err)) },
|
2022-11-14 02:56:25 +00:00
|
|
|
};
|
|
|
|
let mut new: Self = match Figment::new().merge(Toml::string(&old)).merge(Toml::string(&default)).extract() {
|
2022-11-20 02:20:28 +00:00
|
|
|
Ok(new) => { info!("State | TOML merge ... OK"); new },
|
|
|
|
Err(err) => { error!("State | Couldn't merge default + old TOML"); return Err(TomlError::Merge(err)) },
|
2022-11-14 02:56:25 +00:00
|
|
|
};
|
|
|
|
// Attempt save
|
2022-11-20 18:31:00 +00:00
|
|
|
Self::save(&mut new, path)?;
|
2022-11-14 02:56:25 +00:00
|
|
|
Ok(new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------- [Node] Impl
|
|
|
|
impl Node {
|
|
|
|
pub fn localhost() -> Self {
|
|
|
|
Self {
|
|
|
|
ip: "localhost".to_string(),
|
|
|
|
rpc: "18081".to_string(),
|
|
|
|
zmq: "18083".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_vec() -> Vec<(String, Self)> {
|
2022-11-24 04:03:56 +00:00
|
|
|
vec![("Local Monero Node".to_string(), Self::localhost())]
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert [String] to [Node] Vec
|
2022-11-24 04:03:56 +00:00
|
|
|
pub fn from_str_to_vec(string: &str) -> Result<Vec<(String, Self)>, TomlError> {
|
|
|
|
let nodes: toml::map::Map<String, toml::Value> = match toml::de::from_str(string) {
|
2022-11-14 02:56:25 +00:00
|
|
|
Ok(map) => {
|
|
|
|
info!("Node | Parse ... OK");
|
|
|
|
map
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!("Node | String parse ... FAIL ... {}", err);
|
|
|
|
return Err(TomlError::Deserialize(err))
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let size = nodes.keys().len();
|
|
|
|
let mut vec = Vec::with_capacity(size);
|
|
|
|
for (key, values) in nodes.iter() {
|
2022-11-16 02:19:30 +00:00
|
|
|
let node = Node {
|
|
|
|
ip: values.get("ip").unwrap().as_str().unwrap().to_string(),
|
|
|
|
rpc: values.get("rpc").unwrap().as_str().unwrap().to_string(),
|
|
|
|
zmq: values.get("zmq").unwrap().as_str().unwrap().to_string(),
|
|
|
|
};
|
|
|
|
vec.push((key.clone(), node));
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert [Vec<(String, Self)>] into [String]
|
|
|
|
// that can be written as a proper TOML file
|
2022-11-24 04:03:56 +00:00
|
|
|
pub fn to_string(vec: &[(String, Self)]) -> Result<String, TomlError> {
|
2022-11-14 02:56:25 +00:00
|
|
|
let mut toml = String::new();
|
|
|
|
for (key, value) in vec.iter() {
|
|
|
|
write!(
|
|
|
|
toml,
|
2022-11-19 18:34:46 +00:00
|
|
|
"[\'{}\']\nip = {:#?}\nrpc = {:#?}\nzmq = {:#?}\n\n",
|
2022-11-14 02:56:25 +00:00
|
|
|
key,
|
|
|
|
value.ip,
|
|
|
|
value.rpc,
|
|
|
|
value.zmq,
|
2022-11-20 19:46:43 +00:00
|
|
|
)?;
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
2022-11-20 19:46:43 +00:00
|
|
|
Ok(toml)
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Combination of multiple functions:
|
|
|
|
// 1. Attempt to read file from path into [String]
|
|
|
|
// |_ Create a default file if not found
|
|
|
|
// 2. Deserialize [String] into a proper [Struct]
|
|
|
|
// |_ Attempt to merge if deserialization fails
|
2022-11-20 18:31:00 +00:00
|
|
|
pub fn get(path: &PathBuf) -> Result<Vec<(String, Self)>, TomlError> {
|
2022-11-14 02:56:25 +00:00
|
|
|
// Read
|
|
|
|
let file = File::Node;
|
2022-11-24 04:03:56 +00:00
|
|
|
let string = match read_to_string(file, path) {
|
2022-11-14 02:56:25 +00:00
|
|
|
Ok(string) => string,
|
|
|
|
// Create
|
|
|
|
_ => {
|
2022-11-20 18:31:00 +00:00
|
|
|
Self::create_new(path)?;
|
2022-11-24 04:03:56 +00:00
|
|
|
read_to_string(file, path)?
|
2022-11-14 02:56:25 +00:00
|
|
|
},
|
|
|
|
};
|
2022-11-20 02:20:28 +00:00
|
|
|
// Deserialize, attempt merge if failed
|
2022-11-24 04:03:56 +00:00
|
|
|
Self::from_str_to_vec(&string)
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Completely overwrite current [node.toml]
|
|
|
|
// with a new default version, and return [Vec<String, Self>].
|
2022-11-20 18:31:00 +00:00
|
|
|
pub fn create_new(path: &PathBuf) -> Result<Vec<(String, Self)>, TomlError> {
|
2022-11-14 02:56:25 +00:00
|
|
|
info!("Node | Creating new default...");
|
|
|
|
let new = Self::new_vec();
|
2022-11-20 19:46:43 +00:00
|
|
|
let string = Self::to_string(&Self::new_vec())?;
|
2022-11-24 04:03:56 +00:00
|
|
|
fs::write(path, &string)?;
|
2022-11-14 02:56:25 +00:00
|
|
|
info!("Node | Write ... OK");
|
|
|
|
Ok(new)
|
|
|
|
}
|
|
|
|
|
2022-11-19 18:34:46 +00:00
|
|
|
// Save [Node] onto disk file [node.toml]
|
2022-11-24 04:03:56 +00:00
|
|
|
pub fn save(vec: &[(String, Self)], path: &PathBuf) -> Result<(), TomlError> {
|
2022-11-19 18:34:46 +00:00
|
|
|
info!("Node | Saving to disk...");
|
2022-11-20 19:46:43 +00:00
|
|
|
let string = Self::to_string(vec)?;
|
2022-11-19 18:34:46 +00:00
|
|
|
match fs::write(path, string) {
|
|
|
|
Ok(_) => { info!("TOML save ... OK"); Ok(()) },
|
2022-11-20 02:20:28 +00:00
|
|
|
Err(err) => { error!("Couldn't overwrite TOML file"); Err(TomlError::Io(err)) },
|
2022-11-19 18:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 02:20:28 +00:00
|
|
|
// pub fn merge(old: &String) -> Result<Self, TomlError> {
|
|
|
|
// info!("Node | Starting TOML merge...");
|
2022-11-14 02:56:25 +00:00
|
|
|
// let default = match toml::ser::to_string(&Self::new()) {
|
2022-11-20 02:20:28 +00:00
|
|
|
// Ok(string) => { info!("Node | Default TOML parse ... OK"); string },
|
|
|
|
// Err(err) => { error!("Node | Couldn't parse default TOML into string"); return Err(TomlError::Serialize(err)) },
|
2022-11-14 02:56:25 +00:00
|
|
|
// };
|
|
|
|
// let mut new: Self = match Figment::new().merge(Toml::string(&old)).merge(Toml::string(&default)).extract() {
|
2022-11-20 02:20:28 +00:00
|
|
|
// Ok(new) => { info!("Node | TOML merge ... OK"); new },
|
|
|
|
// Err(err) => { error!("Node | Couldn't merge default + old TOML"); return Err(TomlError::Merge(err)) },
|
2022-11-14 02:56:25 +00:00
|
|
|
// };
|
|
|
|
// // Attempt save
|
|
|
|
// Self::save(&mut new)?;
|
|
|
|
// Ok(new)
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:21:46 +00:00
|
|
|
//---------------------------------------------------------------------------------------------------- [Pool] impl
|
|
|
|
impl Pool {
|
|
|
|
pub fn p2pool() -> Self {
|
|
|
|
Self {
|
|
|
|
rig: "Gupax".to_string(),
|
|
|
|
ip: "localhost".to_string(),
|
|
|
|
port: "3333".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_vec() -> Vec<(String, Self)> {
|
2022-11-24 04:03:56 +00:00
|
|
|
vec![("Local P2Pool".to_string(), Self::p2pool())]
|
2022-11-23 04:21:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 04:03:56 +00:00
|
|
|
pub fn from_str_to_vec(string: &str) -> Result<Vec<(String, Self)>, TomlError> {
|
|
|
|
let pools: toml::map::Map<String, toml::Value> = match toml::de::from_str(string) {
|
2022-11-23 04:21:46 +00:00
|
|
|
Ok(map) => {
|
|
|
|
info!("Pool | Parse ... OK");
|
|
|
|
map
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!("Pool | String parse ... FAIL ... {}", err);
|
|
|
|
return Err(TomlError::Deserialize(err))
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let size = pools.keys().len();
|
|
|
|
let mut vec = Vec::with_capacity(size);
|
|
|
|
for (key, values) in pools.iter() {
|
|
|
|
let pool = Pool {
|
|
|
|
rig: values.get("rig").unwrap().as_str().unwrap().to_string(),
|
|
|
|
ip: values.get("ip").unwrap().as_str().unwrap().to_string(),
|
|
|
|
port: values.get("port").unwrap().as_str().unwrap().to_string(),
|
|
|
|
};
|
|
|
|
vec.push((key.clone(), pool));
|
|
|
|
}
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
2022-11-24 04:03:56 +00:00
|
|
|
pub fn to_string(vec: &[(String, Self)]) -> Result<String, TomlError> {
|
2022-11-23 04:21:46 +00:00
|
|
|
let mut toml = String::new();
|
|
|
|
for (key, value) in vec.iter() {
|
|
|
|
write!(
|
|
|
|
toml,
|
|
|
|
"[\'{}\']\nrig = {:#?}\nip = {:#?}\nport = {:#?}\n\n",
|
|
|
|
key,
|
|
|
|
value.rig,
|
|
|
|
value.ip,
|
|
|
|
value.port,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(toml)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(path: &PathBuf) -> Result<Vec<(String, Self)>, TomlError> {
|
|
|
|
// Read
|
|
|
|
let file = File::Pool;
|
2022-11-24 04:03:56 +00:00
|
|
|
let string = match read_to_string(file, path) {
|
2022-11-23 04:21:46 +00:00
|
|
|
Ok(string) => string,
|
|
|
|
// Create
|
|
|
|
_ => {
|
|
|
|
Self::create_new(path)?;
|
2022-11-24 04:03:56 +00:00
|
|
|
read_to_string(file, path)?
|
2022-11-23 04:21:46 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
// Deserialize
|
2022-11-24 04:03:56 +00:00
|
|
|
Self::from_str_to_vec(&string)
|
2022-11-23 04:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_new(path: &PathBuf) -> Result<Vec<(String, Self)>, TomlError> {
|
|
|
|
info!("Pool | Creating new default...");
|
|
|
|
let new = Self::new_vec();
|
|
|
|
let string = Self::to_string(&Self::new_vec())?;
|
2022-11-24 04:03:56 +00:00
|
|
|
fs::write(path, &string)?;
|
2022-11-23 04:21:46 +00:00
|
|
|
info!("Pool | Write ... OK");
|
|
|
|
Ok(new)
|
|
|
|
}
|
|
|
|
|
2022-11-24 04:03:56 +00:00
|
|
|
pub fn save(vec: &[(String, Self)], path: &PathBuf) -> Result<(), TomlError> {
|
2022-11-23 04:21:46 +00:00
|
|
|
info!("Pool | Saving to disk...");
|
|
|
|
let string = Self::to_string(vec)?;
|
|
|
|
match fs::write(path, string) {
|
|
|
|
Ok(_) => { info!("TOML save ... OK"); Ok(()) },
|
|
|
|
Err(err) => { error!("Couldn't overwrite TOML file"); Err(TomlError::Io(err)) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:56:25 +00:00
|
|
|
//---------------------------------------------------------------------------------------------------- Custom Error [TomlError]
|
2022-11-20 02:20:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum TomlError {
|
|
|
|
Io(std::io::Error),
|
|
|
|
Path(String),
|
|
|
|
Serialize(toml::ser::Error),
|
|
|
|
Deserialize(toml::de::Error),
|
|
|
|
Merge(figment::Error),
|
2022-11-20 19:46:43 +00:00
|
|
|
Format(std::fmt::Error),
|
2022-11-20 02:20:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 02:56:25 +00:00
|
|
|
impl Display for TomlError {
|
2022-11-20 02:20:28 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2022-11-14 02:56:25 +00:00
|
|
|
use TomlError::*;
|
|
|
|
match self {
|
2022-11-20 02:20:28 +00:00
|
|
|
Io(err) => write!(f, "{}: IO | {}", ERROR, err),
|
|
|
|
Path(err) => write!(f, "{}: Path | {}", ERROR, err),
|
|
|
|
Serialize(err) => write!(f, "{}: Serialize | {}", ERROR, err),
|
|
|
|
Deserialize(err) => write!(f, "{}: Deserialize | {}", ERROR, err),
|
|
|
|
Merge(err) => write!(f, "{}: Merge | {}", ERROR, err),
|
2022-11-20 19:46:43 +00:00
|
|
|
Format(err) => write!(f, "{}: Format | {}", ERROR, err),
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for TomlError {
|
|
|
|
fn from(err: std::io::Error) -> Self {
|
|
|
|
TomlError::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 19:46:43 +00:00
|
|
|
impl From<std::fmt::Error> for TomlError {
|
|
|
|
fn from(err: std::fmt::Error) -> Self {
|
|
|
|
TomlError::Format(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:56:25 +00:00
|
|
|
//---------------------------------------------------------------------------------------------------- [File] Enum (for matching which file)
|
|
|
|
#[derive(Clone,Copy,Eq,PartialEq,Debug,Deserialize,Serialize)]
|
|
|
|
pub enum File {
|
2022-11-23 04:21:46 +00:00
|
|
|
State, // state.toml -> Gupax state
|
|
|
|
Node, // node.toml -> P2Pool manual node selector
|
|
|
|
Pool, // pool.toml -> XMRig manual pool selector
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------- [Node] Struct
|
|
|
|
#[derive(Clone,Eq,PartialEq,Debug,Deserialize,Serialize)]
|
|
|
|
pub struct Node {
|
|
|
|
pub ip: String,
|
|
|
|
pub rpc: String,
|
|
|
|
pub zmq: String,
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:21:46 +00:00
|
|
|
//---------------------------------------------------------------------------------------------------- [Pool] Struct
|
|
|
|
#[derive(Clone,Eq,PartialEq,Debug,Deserialize,Serialize)]
|
|
|
|
pub struct Pool {
|
|
|
|
pub rig: String,
|
|
|
|
pub ip: String,
|
|
|
|
pub port: String,
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:56:25 +00:00
|
|
|
//---------------------------------------------------------------------------------------------------- [State] Struct
|
|
|
|
#[derive(Clone,Debug,Deserialize,Serialize)]
|
|
|
|
pub struct State {
|
|
|
|
pub gupax: Gupax,
|
|
|
|
pub p2pool: P2pool,
|
|
|
|
pub xmrig: Xmrig,
|
|
|
|
pub version: Arc<Mutex<Version>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone,Eq,PartialEq,Debug,Deserialize,Serialize)]
|
|
|
|
pub struct Gupax {
|
2022-11-23 04:21:46 +00:00
|
|
|
pub simple: bool,
|
2022-11-14 02:56:25 +00:00
|
|
|
pub auto_update: bool,
|
|
|
|
pub auto_node: bool,
|
|
|
|
pub ask_before_quit: bool,
|
|
|
|
pub save_before_quit: bool,
|
|
|
|
pub update_via_tor: bool,
|
|
|
|
pub p2pool_path: String,
|
|
|
|
pub xmrig_path: String,
|
|
|
|
pub absolute_p2pool_path: PathBuf,
|
|
|
|
pub absolute_xmrig_path: PathBuf,
|
2022-11-23 04:21:46 +00:00
|
|
|
pub selected_width: u16,
|
|
|
|
pub selected_height: u16,
|
2022-12-06 03:33:35 +00:00
|
|
|
pub tab: Tab,
|
2022-11-23 04:21:46 +00:00
|
|
|
pub ratio: Ratio,
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone,Eq,PartialEq,Debug,Deserialize,Serialize)]
|
|
|
|
pub struct P2pool {
|
|
|
|
pub simple: bool,
|
|
|
|
pub mini: bool,
|
|
|
|
pub auto_node: bool,
|
|
|
|
pub auto_select: bool,
|
|
|
|
pub out_peers: u16,
|
|
|
|
pub in_peers: u16,
|
|
|
|
pub log_level: u8,
|
|
|
|
pub node: crate::node::NodeEnum,
|
|
|
|
pub arguments: String,
|
|
|
|
pub address: String,
|
|
|
|
pub name: String,
|
|
|
|
pub ip: String,
|
|
|
|
pub rpc: String,
|
|
|
|
pub zmq: String,
|
cargo/tor/p2pool: clean deps, warn macos arti, fix node overflow
Cargo: Cleanup unused dependencies, enable some build optimizations
Tor: Arti doesn't seem to work on macOS
Even a bare Arti+Hyper request doesn't seem to work, so it's
probably not something to do with Gupax. A lot of issues only
seem to popup in a VM (OpenGL, TLS) even though on bare metal
Gupax runs fine, so Tor might work fine on real macOS but I don't
have real macOS to test it. VM macOS can't create a circuit, so,
disable by default and add a warning that it's unstable.
P2Pool: Let selected_index start at 0, and only +1 when printing
to the user, this makes the overflow math when adding/deleting a
lot more simple because selected_index will match the actual index
of the node vector
2022-11-21 22:16:31 +00:00
|
|
|
pub selected_index: usize,
|
2022-11-14 02:56:25 +00:00
|
|
|
pub selected_name: String,
|
|
|
|
pub selected_ip: String,
|
|
|
|
pub selected_rpc: String,
|
|
|
|
pub selected_zmq: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone,Eq,PartialEq,Debug,Deserialize,Serialize)]
|
|
|
|
pub struct Xmrig {
|
|
|
|
pub simple: bool,
|
2022-11-23 04:21:46 +00:00
|
|
|
pub pause: u8,
|
2022-12-03 18:37:57 +00:00
|
|
|
pub simple_rig: String,
|
|
|
|
pub arguments: String,
|
2022-11-14 02:56:25 +00:00
|
|
|
pub tls: bool,
|
|
|
|
pub keepalive: bool,
|
|
|
|
pub max_threads: usize,
|
|
|
|
pub current_threads: usize,
|
|
|
|
pub address: String,
|
2022-11-23 04:21:46 +00:00
|
|
|
pub api_ip: String,
|
|
|
|
pub api_port: String,
|
|
|
|
pub name: String,
|
|
|
|
pub rig: String,
|
|
|
|
pub ip: String,
|
|
|
|
pub port: String,
|
|
|
|
pub selected_index: usize,
|
|
|
|
pub selected_name: String,
|
|
|
|
pub selected_rig: String,
|
|
|
|
pub selected_ip: String,
|
|
|
|
pub selected_port: String,
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone,Debug,Deserialize,Serialize)]
|
|
|
|
pub struct Version {
|
2022-11-19 14:39:26 +00:00
|
|
|
pub gupax: String,
|
|
|
|
pub p2pool: String,
|
|
|
|
pub xmrig: String,
|
2022-11-14 02:56:25 +00:00
|
|
|
}
|