140 lines
3.6 KiB
Rust
140 lines
3.6 KiB
Rust
mod p2prc;
|
|
|
|
use std::fmt::Debug;
|
|
use std::process::Command;
|
|
use std::fs::{self, File};
|
|
use std::io::{self, Write};
|
|
use uuid::Uuid;
|
|
use crate::p2prc::*;
|
|
use tokio::time::{sleep, Duration};
|
|
// Struct representing NodeInfo
|
|
#[derive(Debug)]
|
|
struct NodeInfo {
|
|
ipv4: String,
|
|
server_port: String,
|
|
}
|
|
|
|
// Struct representing Process
|
|
#[derive(Debug)]
|
|
struct Process {
|
|
id: String,
|
|
internal_port_no: String,
|
|
external_address: String,
|
|
node_id: String,
|
|
task_name: String,
|
|
command_to_run_script: String,
|
|
command_to_kill_script: String,
|
|
status: bool,
|
|
domain_name: String,
|
|
}
|
|
|
|
// Simulate mapping a port to a public address
|
|
fn p2prc_map_port(port: &str, domain: &str, server_address: &str) -> String {
|
|
format!("{}:{}", domain, port) // Simulate mapping
|
|
}
|
|
|
|
// Simulate saving a process to memory (e.g., to a file or in-memory list)
|
|
fn add_process_to_memory(process: &Process) {
|
|
println!("Process added to memory: {:?}", process);
|
|
}
|
|
|
|
// Simulate saving the process to disk (e.g., JSON file)
|
|
fn save_process(process: &Process) -> io::Result<()> {
|
|
let mut file = File::create("process_saved.txt")?;
|
|
writeln!(file, "{:?}", process)?;
|
|
Ok(())
|
|
}
|
|
|
|
// The SpinProcess function
|
|
fn spin_process(mut process: Process) -> Process {
|
|
// 1. Run the script
|
|
let _ = Command::new("bash")
|
|
.arg(&process.command_to_run_script)
|
|
.status()
|
|
.expect("Failed to start script");
|
|
|
|
// 2. Generate server address
|
|
let server_address = format!("{}:{}", "0.0.0.0", "1245");
|
|
|
|
// 3. Map to external address
|
|
process.external_address = p2prc_map_port(&process.internal_port_no, &process.domain_name, &server_address);
|
|
|
|
// 4. Generate UUID
|
|
process.id = Uuid::new_v4().to_string();
|
|
|
|
// 5. Set status
|
|
process.status = true;
|
|
|
|
// 6. Save process to memory and disk
|
|
add_process_to_memory(&process);
|
|
// save_process(&process).expect("Failed to save process");
|
|
|
|
process
|
|
}
|
|
|
|
// Main function
|
|
#[tokio::main]
|
|
async fn main() {
|
|
// Start p2prc in the background
|
|
tokio::spawn(async {
|
|
p2prc_start_server().await;
|
|
});
|
|
|
|
// list servers on the network
|
|
println!("{:#?}", p2prc_list_server_command());
|
|
|
|
// Before next instruction sleep for 2 seconds
|
|
sleep(Duration::from_secs(4)).await;
|
|
|
|
// 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());
|
|
|
|
// p2prc_default_config();
|
|
|
|
// println!("{:#?}", p2prc_read_config());
|
|
|
|
println!("{:#?}", p2prc_get_specs("217.76.63.222:32921".to_string()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// println!("{}", p2prc_map_port_command("3000", "0.0.0.0",""));
|
|
// println!("Starting SpinProcess...");
|
|
//
|
|
// let node_info = NodeInfo {
|
|
// ipv4: "192.168.1.20".to_string(),
|
|
// server_port: "9000".to_string(),
|
|
// };
|
|
//
|
|
// let process = Process {
|
|
// id: String::new(),
|
|
// internal_port_no: "".to_string(),
|
|
// external_address: "".to_string(),
|
|
// node_id: "Test".to_string(),
|
|
// task_name: "ExampleTask".to_string(),
|
|
// command_to_run_script: "./run_script.sh".to_string(),
|
|
// command_to_kill_script: "./kill_script.sh".to_string(),
|
|
// status: false,
|
|
// domain_name: "example.com".to_string(),
|
|
// };
|
|
//
|
|
// let updated = spin_process(process);
|
|
//
|
|
// println!("Final Process: {:?}", updated);
|
|
|
|
// Start P2PRC as a server to test if the background process works
|
|
|
|
} |