added wrtie config bindings
This commit is contained in:
@@ -89,6 +89,19 @@ async fn main() {
|
||||
// Test map port output
|
||||
println!("{:#?}", p2prc_map_port_command("3000","test.akilan.io",""));
|
||||
|
||||
let mut config = p2prc_read_config();
|
||||
|
||||
// Change server port
|
||||
config.server_port = "1233".parse().unwrap();
|
||||
|
||||
p2prc_write_config(config);
|
||||
|
||||
println!("{:#?}", p2prc_read_config());
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// println!("{}", p2prc_map_port_command("3000", "0.0.0.0",""));
|
||||
// println!("Starting SpinProcess...");
|
||||
//
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::fs;
|
||||
use std::io::Error;
|
||||
use std::process::{Command, Output};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct IPAddressList {
|
||||
@@ -28,6 +30,56 @@ pub struct IPAddressEntry {
|
||||
pub CustomInformation: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Config {
|
||||
#[serde(rename = "MachineName")]
|
||||
pub machine_name: String,
|
||||
#[serde(rename = "IPTable")]
|
||||
pub iptable: String,
|
||||
#[serde(rename = "DockerContainers")]
|
||||
pub docker_containers: String,
|
||||
#[serde(rename = "DefaultDockerFile")]
|
||||
pub default_docker_file: String,
|
||||
#[serde(rename = "DockerRunLogs")]
|
||||
pub docker_run_logs: String,
|
||||
#[serde(rename = "SpeedTestFile")]
|
||||
pub speed_test_file: String,
|
||||
#[serde(rename = "IPV6Address")]
|
||||
pub ipv6address: String,
|
||||
#[serde(rename = "PluginPath")]
|
||||
pub plugin_path: String,
|
||||
#[serde(rename = "TrackContainersPath")]
|
||||
pub track_containers_path: String,
|
||||
#[serde(rename = "ServerPort")]
|
||||
pub server_port: String,
|
||||
#[serde(rename = "ProxyPort")]
|
||||
pub proxy_port: String,
|
||||
#[serde(rename = "GroupTrackContainersPath")]
|
||||
pub group_track_containers_path: String,
|
||||
#[serde(rename = "FRPServerPort")]
|
||||
pub frpserver_port: bool,
|
||||
#[serde(rename = "BehindNAT")]
|
||||
pub behind_nat: bool,
|
||||
#[serde(rename = "IPTableKey")]
|
||||
pub iptable_key: String,
|
||||
#[serde(rename = "PublicKeyFile")]
|
||||
pub public_key_file: String,
|
||||
#[serde(rename = "PrivateKeyFile")]
|
||||
pub private_key_file: String,
|
||||
#[serde(rename = "PemFile")]
|
||||
pub pem_file: String,
|
||||
#[serde(rename = "KeyFile")]
|
||||
pub key_file: String,
|
||||
#[serde(rename = "BareMetal")]
|
||||
pub bare_metal: bool,
|
||||
#[serde(rename = "UnsafeMode")]
|
||||
pub unsafe_mode: bool,
|
||||
#[serde(rename = "Test")]
|
||||
pub test: bool,
|
||||
#[serde(rename = "ConfigPath")]
|
||||
pub config_path: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct MapPort {
|
||||
#[serde(rename = "IPAddress")]
|
||||
@@ -39,13 +91,21 @@ pub struct MapPort {
|
||||
}
|
||||
|
||||
// Runs p2prc with custom arguments called.
|
||||
pub fn run_p2prc_command(arguments: &str) -> Result<Output, std::io::Error> {
|
||||
// Function implemented as an identity function
|
||||
pub fn run_p2prc_command<T>(arguments: &str) -> T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let command_str = format!("p2prc {}", arguments);
|
||||
|
||||
Command::new("sh")
|
||||
let output = Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(&command_str)
|
||||
.output()
|
||||
.expect("Failed to execute command");
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
serde_json::from_str(&stdout).expect("Failed to deserialize JSON output")
|
||||
}
|
||||
|
||||
// Runs p2prc map port command
|
||||
@@ -69,22 +129,34 @@ pub fn p2prc_map_port_command(local_port_no: &str, domain_name: &str,remote_serv
|
||||
|
||||
println!("{:#?}", command);
|
||||
|
||||
// let command = format!("--mp {}", local_port_no);
|
||||
let output = run_p2prc_command(&*command);
|
||||
let stdout = String::from_utf8_lossy(&output.unwrap().stdout).to_string();
|
||||
let parsed: MapPort = serde_json::from_str(&*stdout).unwrap();
|
||||
parsed
|
||||
let map_port: MapPort = run_p2prc_command(&*command);
|
||||
map_port
|
||||
}
|
||||
|
||||
// Runs p2prc list server command
|
||||
pub fn p2prc_list_server_command() -> IPAddressList {
|
||||
let output = run_p2prc_command(&*"--ls".to_string());
|
||||
let stdout = String::from_utf8_lossy(&output.unwrap().stdout).to_string();
|
||||
let parsed: IPAddressList = serde_json::from_str(&*stdout).unwrap();
|
||||
parsed
|
||||
let servers: IPAddressList = run_p2prc_command(&*"--ls".to_string());
|
||||
servers
|
||||
}
|
||||
|
||||
// Starts p2prc server in the background
|
||||
pub async fn p2prc_start_server() {
|
||||
let output = run_p2prc_command(&*"-s".to_string());
|
||||
let run_server: Option<bool> = run_p2prc_command(&*"-s".to_string());
|
||||
}
|
||||
|
||||
|
||||
// Read P2PRC config file
|
||||
pub fn p2prc_read_config() -> Config {
|
||||
let config: Config = run_p2prc_command(&*"--vc".to_string());
|
||||
config
|
||||
}
|
||||
|
||||
// Writes changes to the config file
|
||||
// write to the config path provided in the config type
|
||||
// unoptimised but reduces code complexity
|
||||
pub fn p2prc_write_config(config: Config) {
|
||||
// Convert config type to a string
|
||||
let json:String = serde_json::to_string(&config).unwrap().to_string();
|
||||
fs::write(config.config_path, json).expect("Panic message: ");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user