added support for map port, list server and run server command

This commit is contained in:
2025-06-27 15:00:34 +01:00
parent f41119634a
commit e2ca1982f5
4 changed files with 759 additions and 12 deletions

View File

@@ -1,17 +1,90 @@
use std::io::Error;
use std::process::{Command, Output};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct IPAddressList {
pub ip_address: Vec<IPAddressEntry>,
}
// Maps p2prc --ls output to the following
// data structure
#[derive(Debug, Serialize, Deserialize)]
pub struct IPAddressEntry {
pub Name: String,
pub MachineUsername: String,
pub IPV4: String,
pub IPV6: String,
pub Latency: u64,
pub Download: u64,
pub Upload: u64,
pub ServerPort: String,
pub BareMetalSSHPort: String,
pub NAT: bool,
pub EscapeImplementation: String,
pub ProxyServer: bool,
pub UnSafeMode: bool,
pub PublicKey: String,
pub CustomInformation: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MapPort {
#[serde(rename = "IPAddress")]
pub ipaddress: String,
#[serde(rename = "PortNo")]
pub port_no: String,
#[serde(rename = "EntireAddress")]
pub entire_address: String,
}
// Runs p2prc with custom arguments called.
pub fn run_p2prc_command(arguments: &str) -> Result<Output, std::io::Error> {
Command::new("p2prc")
.arg(arguments)
let command_str = format!("p2prc {}", arguments);
Command::new("sh")
.arg("-c")
.arg(&command_str)
.output()
}
// Runs p2prc map port command
pub fn p2prc_map_port_command(local_port_no: &str, address: &str) -> String{
let output = run_p2prc_command(&*"--ls".to_string());
pub fn p2prc_map_port_command(local_port_no: &str, domain_name: &str,remote_server: &str) -> MapPort{
// Needs a reviews for a better representation
let mut command = vec!["".to_string()];
match !local_port_no.is_empty() {
true => command.push(format!("--mp {}", local_port_no)),
false => (),
}
match !domain_name.is_empty() {
true => command.push(format!("--dn {}", domain_name)),
false => (),
}
match !remote_server.is_empty() {
true => command.push(format!("--ra {}", remote_server)),
false => (),
}
let command = command.join(" ");
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();
stdout
let parsed: MapPort = serde_json::from_str(&*stdout).unwrap();
parsed
}
// 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
}
// Starts p2prc server in the background
pub async fn p2prc_start_server() {
let output = run_p2prc_command(&*"-s".to_string());
}