mirror of
https://github.com/Astatin3/unshell-nodes-rs.git
synced 2026-06-08 16:18:08 -06:00
Move files around, add UI
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "unshell-rs-lib"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
log = "0.4.27"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.140"
|
||||
@@ -0,0 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CampignConfig {
|
||||
name: String,
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum LayerConfig {}
|
||||
@@ -0,0 +1,14 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::config::layers::LayerConfig;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum ListenerConfig {
|
||||
Tcp {
|
||||
enabled: bool,
|
||||
name: String,
|
||||
remote_host: String,
|
||||
port: u16,
|
||||
layers: Vec<LayerConfig>,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub mod campaign;
|
||||
pub mod layers;
|
||||
pub mod listeners;
|
||||
@@ -0,0 +1,18 @@
|
||||
use crate::layers::Layer;
|
||||
use base64;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
pub struct Base64;
|
||||
|
||||
impl Layer for Base64 {
|
||||
fn encode(&mut self, data: &[u8]) -> Vec<u8> {
|
||||
#[allow(deprecated)]
|
||||
base64::encode(str::from_utf8(data).unwrap()).into_bytes()
|
||||
}
|
||||
|
||||
fn decode(&mut self, data: &[u8]) -> Vec<u8> {
|
||||
#[allow(deprecated)]
|
||||
base64::decode(str::from_utf8(data).unwrap()).unwrap()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
pub trait Layer: Serialize + Deserialize<'static> + Sized {
|
||||
fn encode(&mut self, data: &[u8]) -> Vec<u8>;
|
||||
fn decode(&mut self, data: &[u8]) -> Vec<u8>;
|
||||
}
|
||||
|
||||
pub mod base64;
|
||||
|
||||
pub use base64::Base64;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -0,0 +1,8 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
pub mod config;
|
||||
pub mod layers;
|
||||
pub mod listeners;
|
||||
pub mod networkers;
|
||||
pub mod packets;
|
||||
@@ -0,0 +1,9 @@
|
||||
pub struct Client<C> {
|
||||
pub stream: C,
|
||||
}
|
||||
|
||||
impl<C> Client<C> {
|
||||
pub fn new(stream: C) -> Self {
|
||||
Self { stream }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use crate::layers::Layer;
|
||||
|
||||
mod client;
|
||||
mod server;
|
||||
|
||||
pub use server::Listener;
|
||||
@@ -0,0 +1,42 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::{
|
||||
listeners::client::Client,
|
||||
networkers::{Connection, ServerTrait},
|
||||
};
|
||||
|
||||
pub struct Listener<S, C> {
|
||||
pub server: Arc<Mutex<S>>,
|
||||
pub clients: Arc<Mutex<Vec<Client<C>>>>,
|
||||
}
|
||||
|
||||
impl<S, C> Listener<S, C> {
|
||||
pub fn new(server: S) -> Self {
|
||||
Self {
|
||||
server: Arc::new(Mutex::new(server)),
|
||||
clients: Arc::new(Mutex::new(Vec::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_listener(&mut self) -> Result<(), Box<dyn std::error::Error>>
|
||||
where
|
||||
S: ServerTrait<C>,
|
||||
C: Connection + 'static,
|
||||
S::Error: std::error::Error + 'static,
|
||||
C::Error: std::error::Error + 'static,
|
||||
{
|
||||
loop {
|
||||
let mut conn_lock = self.server.lock().unwrap();
|
||||
|
||||
match conn_lock.accept() {
|
||||
Ok(conn) => {
|
||||
let mut clients_lock = self.clients.lock().unwrap();
|
||||
clients_lock.push(Client::new(conn));
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to accept connection: {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/// This is the lowset-level data transmission type
|
||||
|
||||
pub trait Connection: Send + Sync {
|
||||
type Error: std::fmt::Debug;
|
||||
|
||||
fn read(&mut self) -> Result<String, Self::Error>;
|
||||
fn write(&mut self, data: &str) -> Result<(), Self::Error>;
|
||||
}
|
||||
|
||||
pub trait ServerTrait<C: Connection> {
|
||||
type Error: std::fmt::Debug;
|
||||
|
||||
fn accept(&mut self) -> Result<C, Self::Error>;
|
||||
fn bind(address: &str) -> Result<Self, Self::Error>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait ClientTrait<C: Connection> {
|
||||
type Error: std::fmt::Debug;
|
||||
|
||||
fn connect(address: &str) -> Result<C, Self::Error>;
|
||||
}
|
||||
|
||||
mod tcp;
|
||||
|
||||
pub use tcp::TCPClient;
|
||||
pub use tcp::TCPConnection;
|
||||
pub use tcp::TCPServer;
|
||||
@@ -0,0 +1,57 @@
|
||||
use std::{
|
||||
io::{self, BufRead, BufReader, Write},
|
||||
net::{TcpListener, TcpStream},
|
||||
};
|
||||
|
||||
use crate::networkers::{ClientTrait, Connection, ServerTrait};
|
||||
|
||||
pub struct TCPConnection {
|
||||
stream: TcpStream,
|
||||
reader: BufReader<TcpStream>,
|
||||
}
|
||||
|
||||
impl Connection for TCPConnection {
|
||||
type Error = io::Error;
|
||||
|
||||
fn read(&mut self) -> Result<String, Self::Error> {
|
||||
let mut line = String::new();
|
||||
self.reader.read_line(&mut line)?;
|
||||
Ok(line.trim_end().to_string())
|
||||
}
|
||||
|
||||
fn write(&mut self, data: &str) -> Result<(), Self::Error> {
|
||||
writeln!(self.stream, "{}", data)?;
|
||||
self.stream.flush()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TCPServer {
|
||||
listener: TcpListener,
|
||||
}
|
||||
|
||||
impl ServerTrait<TCPConnection> for TCPServer {
|
||||
type Error = io::Error;
|
||||
|
||||
fn accept(&mut self) -> Result<TCPConnection, Self::Error> {
|
||||
let (stream, _) = self.listener.accept()?;
|
||||
let reader = BufReader::new(stream.try_clone()?);
|
||||
Ok(TCPConnection { stream, reader })
|
||||
}
|
||||
|
||||
fn bind(address: &str) -> Result<Self, Self::Error> {
|
||||
let listener = TcpListener::bind(address)?;
|
||||
Ok(Self { listener })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TCPClient;
|
||||
|
||||
impl ClientTrait<TCPConnection> for TCPClient {
|
||||
type Error = io::Error;
|
||||
|
||||
fn connect(address: &str) -> Result<TCPConnection, Self::Error> {
|
||||
let stream = TcpStream::connect(address)?;
|
||||
let reader = BufReader::new(stream.try_clone()?);
|
||||
Ok(TCPConnection { stream, reader })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
mod sysinfo;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum Packet {
|
||||
Heartbeat,
|
||||
Sysinfo(sysinfo::Sysinfo),
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Sysinfo {
|
||||
hostname: String,
|
||||
}
|
||||
Reference in New Issue
Block a user