New protocol
This commit is contained in:
@@ -1,2 +1,27 @@
|
|||||||
# ghostnet-rs
|
# ghostnet-rs
|
||||||
A rust library for the Ghost Net protocol
|
A rust library for the Ghost Net protocol
|
||||||
|
|
||||||
|
|
||||||
|
## Protocol
|
||||||
|
**Client connects to the router**
|
||||||
|
Client -> `[10, 7, 1, 21, 3000]` -> Router
|
||||||
|
|
||||||
|
**Client route message to `10.7.1.21`**
|
||||||
|
Client -> `[<message-bytes>]` -> Router -> (Client / Router) -> `10.7.1.21`
|
||||||
|
|
||||||
|
**`10.7.1.21` Responds back**
|
||||||
|
`10.7.1.21` -> `[<message-bytes>]` -> (Client / Router) -> Client
|
||||||
|
|
||||||
|
### How a client routes
|
||||||
|
Router -> `[10, 7, 1, 21, 3000]` -> Client
|
||||||
|
|
||||||
|
Router -> `[<message-bytes>]` -> Client
|
||||||
|
|
||||||
|
Client -> `[<message-bytes>]` -> Router
|
||||||
|
|
||||||
|
## What is a router?
|
||||||
|
A router manages the clients and routes messages from one client to another to ensure security.
|
||||||
|
|
||||||
|
## Todo
|
||||||
|
- End-To-End Encryption
|
||||||
|
- Mask the communications like **HTTPS**
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
use std::{
|
|
||||||
io::{Read, Write},
|
|
||||||
net::{Shutdown, TcpListener, TcpStream},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::route_tcp;
|
|
||||||
|
|
||||||
pub fn router() -> std::io::Result<()> {
|
|
||||||
let listener = TcpListener::bind("0.0.0.0:7879")?;
|
|
||||||
|
|
||||||
for stream in listener.incoming() {
|
|
||||||
match stream {
|
|
||||||
Ok(mut client_recv) => {
|
|
||||||
std::thread::spawn(|| -> std::io::Result<()> {
|
|
||||||
let mut address = [0; 6];
|
|
||||||
let bytes_read = client_recv.read(&mut address)?;
|
|
||||||
if bytes_read == 0 {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let address = format!(
|
|
||||||
"{}.{}.{}.{}:{}",
|
|
||||||
address[0],
|
|
||||||
address[1],
|
|
||||||
address[2],
|
|
||||||
address[3],
|
|
||||||
u16::from_be_bytes([address[4], address[5]])
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut client_send = client_recv.try_clone()?;
|
|
||||||
let mut server_recv = TcpStream::connect(&address)?;
|
|
||||||
let mut server_send = server_recv.try_clone()?;
|
|
||||||
|
|
||||||
// Client -> Server
|
|
||||||
std::thread::spawn(move || -> std::io::Result<()> {
|
|
||||||
while !route_tcp(&mut client_recv, &mut server_send)? {}
|
|
||||||
server_send.shutdown(Shutdown::Both)?;
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
// Server -> Client
|
|
||||||
while !route_tcp(&mut server_recv, &mut client_send)? {}
|
|
||||||
client_send.shutdown(Shutdown::Both)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(current_ip: (u8, u8, u8, u8)) -> std::io::Result<()> {
|
|
||||||
let listener = TcpListener::bind("0.0.0.0:7878")?;
|
|
||||||
let mut connections: Vec<[u8; 6]> = Vec::new();
|
|
||||||
|
|
||||||
std::thread::spawn(router);
|
|
||||||
|
|
||||||
for stream in listener.incoming() {
|
|
||||||
match stream {
|
|
||||||
Ok(mut stream) => {
|
|
||||||
let mut address = [0; 6];
|
|
||||||
let bytes_read = stream.read(&mut address)?;
|
|
||||||
if bytes_read == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(con) = connections.last() {
|
|
||||||
stream.write_all(con)?;
|
|
||||||
} else {
|
|
||||||
let default_addr = [
|
|
||||||
current_ip.0,
|
|
||||||
current_ip.1,
|
|
||||||
current_ip.2,
|
|
||||||
current_ip.3,
|
|
||||||
0x1E,
|
|
||||||
0xC7,
|
|
||||||
];
|
|
||||||
stream.write_all(&default_addr)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
connections.push(address);
|
|
||||||
|
|
||||||
while stream.read(&mut [])? != 0 {}
|
|
||||||
|
|
||||||
stream.shutdown(Shutdown::Both)?;
|
|
||||||
if let Some(v) = connections.iter().position(|v| v == &address) {
|
|
||||||
connections.remove(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
+45
-28
@@ -3,46 +3,50 @@ use std::{
|
|||||||
net::TcpStream,
|
net::TcpStream,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod gateway;
|
pub mod router;
|
||||||
|
|
||||||
pub struct GhostStream(TcpStream);
|
pub type IpAddr = (u8, u8, u8, u8);
|
||||||
|
|
||||||
impl GhostStream {
|
pub struct GhostClient(TcpStream);
|
||||||
pub fn connect(ip: (u8, u8, u8, u8), port: u16) -> std::io::Result<Self> {
|
|
||||||
let addr = get_addr()?;
|
|
||||||
let mut stream = TcpStream::connect(addr)?;
|
|
||||||
let bep = port.to_be_bytes();
|
|
||||||
stream.write_all(&[ip.0, ip.1, ip.2, ip.3, bep[0], bep[1]])?;
|
|
||||||
|
|
||||||
Ok(Self(stream))
|
impl GhostClient {
|
||||||
}
|
pub fn connect(router: &str, host: IpAddr, port: u16) -> std::io::Result<Self> {
|
||||||
|
let mut stream = TcpStream::connect(router)?;
|
||||||
|
|
||||||
pub fn write(&mut self, data: &[u8]) -> std::io::Result<()> {
|
let port = port.to_be_bytes();
|
||||||
self.0.write_all(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read(&mut self, data: &mut [u8]) -> std::io::Result<usize> {
|
stream.write_all(&[host.0, host.1, host.2, host.3, port[0], port[1]])?;
|
||||||
self.0.read(data)
|
|
||||||
|
Ok(GhostClient(stream))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_addr() -> std::io::Result<String> {
|
impl GhostClient {
|
||||||
let mut stream = TcpStream::connect("0.0.0.0:7878")?;
|
pub fn send(&mut self, buf: &[u8]) -> std::io::Result<()> {
|
||||||
|
self.0.write_all(buf)
|
||||||
|
}
|
||||||
|
|
||||||
stream.write_all(&[0, 0, 0, 0, 0x1E, 0xC7])?;
|
pub fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||||
|
self.0.read(buf)
|
||||||
|
}
|
||||||
|
|
||||||
let mut address = [0; 6];
|
pub fn read_bytes(&mut self, size: usize) -> std::io::Result<Vec<u8>> {
|
||||||
|
let mut buf = vec![0u8; size];
|
||||||
|
let n = self.0.read(&mut buf)?;
|
||||||
|
buf.truncate(n);
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
stream.read(&mut address)?;
|
pub fn format_ip_bytes(buf: [u8; 6]) -> String {
|
||||||
|
format!(
|
||||||
Ok(format!(
|
|
||||||
"{}.{}.{}.{}:{}",
|
"{}.{}.{}.{}:{}",
|
||||||
address[0],
|
buf[0],
|
||||||
address[1],
|
buf[1],
|
||||||
address[2],
|
buf[2],
|
||||||
address[3],
|
buf[3],
|
||||||
u16::from_be_bytes([address[4], address[5]])
|
u16::from_be_bytes([buf[4], buf[5]])
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn route_tcp(from: &mut TcpStream, to: &mut TcpStream) -> std::io::Result<bool> {
|
pub fn route_tcp(from: &mut TcpStream, to: &mut TcpStream) -> std::io::Result<bool> {
|
||||||
@@ -57,3 +61,16 @@ pub fn route_tcp(from: &mut TcpStream, to: &mut TcpStream) -> std::io::Result<bo
|
|||||||
|
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn route_tcp2(from: &mut TcpStream, to: &mut TcpStream) -> std::io::Result<bool> {
|
||||||
|
let mut data = [0; 2048];
|
||||||
|
|
||||||
|
let bytes_read = from.read(&mut data)?;
|
||||||
|
if bytes_read == 0 {
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
to.write_all(&[&[0u8], &data[..bytes_read]].concat())?;
|
||||||
|
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
|||||||
+8
-39
@@ -1,41 +1,10 @@
|
|||||||
use std::{
|
use ghostnet_rs::GhostClient;
|
||||||
io::{Read, Write},
|
|
||||||
net::TcpStream,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() {
|
||||||
let mut stream = TcpStream::connect("ipv4.icanhazip.com:80")?;
|
// ghostnet_rs::router::router();
|
||||||
|
let mut con = GhostClient::connect("0.0.0.0:443", (0, 0, 0, 0), 7525).unwrap();
|
||||||
// Send raw HTTP request
|
con.send("Hello from client!".as_bytes()).unwrap();
|
||||||
let request = "GET / HTTP/1.1\r\n\
|
let response = con.read_bytes(1024).unwrap();
|
||||||
Host: ipv4.icanhazip.com\r\n\
|
println!("From server: {}", String::from_utf8(response).unwrap());
|
||||||
Connection: close\r\n\
|
println!("From server 2: {:?}", con.read_bytes(1024).unwrap());
|
||||||
\r\n";
|
|
||||||
stream.write_all(request.as_bytes())?;
|
|
||||||
|
|
||||||
// Read full response
|
|
||||||
let mut response = String::new();
|
|
||||||
stream.read_to_string(&mut response)?;
|
|
||||||
|
|
||||||
// Extract body after headers
|
|
||||||
if let Some(pos) = response.find("\r\n\r\n") {
|
|
||||||
let body = response[pos + 4..].trim();
|
|
||||||
|
|
||||||
// Parse into tuple (u8, u8, u8, u8)
|
|
||||||
let octets: Vec<u8> = body
|
|
||||||
.split('.')
|
|
||||||
.filter_map(|s| s.parse::<u8>().ok())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if octets.len() == 4 {
|
|
||||||
let ip_tuple = (octets[0], octets[1], octets[2], octets[3]);
|
|
||||||
ghostnet_rs::gateway::run(ip_tuple)?;
|
|
||||||
} else {
|
|
||||||
eprintln!("Invalid IPv4 address format: {}", body);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
eprintln!("Invalid HTTP response");
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
use std::{
|
||||||
|
io::{Read, Write},
|
||||||
|
net::{TcpListener, TcpStream},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{format_ip_bytes, route_tcp, route_tcp2};
|
||||||
|
|
||||||
|
pub fn router() {
|
||||||
|
let server = TcpListener::bind("0.0.0.0:443").unwrap();
|
||||||
|
let mut clients: Vec<TcpStream> = Vec::new();
|
||||||
|
|
||||||
|
for stream in server.incoming() {
|
||||||
|
match stream {
|
||||||
|
Ok(mut client_recv) => {
|
||||||
|
let mut addr = [0; 6];
|
||||||
|
|
||||||
|
client_recv.read_exact(&mut addr).unwrap();
|
||||||
|
clients.push(client_recv.try_clone().unwrap());
|
||||||
|
|
||||||
|
let i = clients.len().wrapping_sub(2);
|
||||||
|
|
||||||
|
if let Some(server_recv) = clients.get_mut(i) {
|
||||||
|
let mut server_send = server_recv.try_clone().unwrap();
|
||||||
|
let mut client_send = client_recv.try_clone().unwrap();
|
||||||
|
|
||||||
|
server_send
|
||||||
|
.write_all(&[&[1u8], &addr[..]].concat())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
while !route_tcp2(&mut client_recv, &mut server_send).unwrap() {}
|
||||||
|
});
|
||||||
|
|
||||||
|
while !route_tcp2(server_recv, &mut client_send).unwrap() {}
|
||||||
|
} else {
|
||||||
|
let mut server_recv = TcpStream::connect(format_ip_bytes(addr)).unwrap();
|
||||||
|
let mut server_send = server_recv.try_clone().unwrap();
|
||||||
|
let mut client_send = client_recv.try_clone().unwrap();
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
while !route_tcp(&mut client_recv, &mut server_send).unwrap() {}
|
||||||
|
});
|
||||||
|
|
||||||
|
while !route_tcp(&mut server_recv, &mut client_send).unwrap() {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user