2025-06-29 11:40:42 +02:00
|
|
|
use std::net::SocketAddrV4;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
use futures::future::Remote;
|
|
|
|
|
use log::{error, warn};
|
|
|
|
|
use macros::rmc_struct;
|
2025-06-30 10:53:10 +02:00
|
|
|
use tokio::net::TcpStream;
|
2025-06-29 11:40:42 +02:00
|
|
|
use tokio::sync::RwLock;
|
2025-06-13 12:36:28 +02:00
|
|
|
use tokio::task;
|
2025-06-29 11:40:42 +02:00
|
|
|
use tokio::time::sleep;
|
2025-06-30 10:53:10 +02:00
|
|
|
use tokio_rustls::client::TlsStream;
|
|
|
|
|
use tokio_tungstenite::MaybeTlsStream;
|
2025-06-13 10:11:05 +02:00
|
|
|
use rust_nex::common::setup;
|
2025-07-30 21:39:54 +02:00
|
|
|
use rust_nex::executables::common::{AUTH_SERVER_ACCOUNT, FORWARD_DESTINATION, OWN_IP_PRIVATE, OWN_IP_PUBLIC, SECURE_EDGE_NODE_HOLDER, SECURE_SERVER_ACCOUNT, SERVER_PORT};
|
2025-06-13 12:36:28 +02:00
|
|
|
use rust_nex::prudp::packet::VirtualPort;
|
|
|
|
|
use rust_nex::prudp::router::Router;
|
|
|
|
|
use rust_nex::prudp::secure::Secure;
|
|
|
|
|
use rust_nex::prudp::unsecure::Unsecure;
|
2025-07-30 21:39:54 +02:00
|
|
|
use rust_nex::reggie::EdgeNodeHolderConnectOption::{DontRegister, Register};
|
2025-06-29 11:40:42 +02:00
|
|
|
use rust_nex::rmc::response::ErrorCode;
|
2025-06-13 12:36:28 +02:00
|
|
|
use rust_nex::rnex_proxy_common::ConnectionInitData;
|
2025-07-30 21:39:54 +02:00
|
|
|
use rust_nex::reggie::{RemoteEdgeNodeHolder, UnitPacketWrite};
|
2025-06-29 11:40:42 +02:00
|
|
|
use rust_nex::rmc::structures::RmcSerialize;
|
|
|
|
|
use rust_nex::reggie::UnitPacketRead;
|
2025-07-30 21:39:54 +02:00
|
|
|
use rust_nex::rmc::protocols::{new_rmc_gateway_connection, OnlyRemote, RemoteInstantiatable};
|
|
|
|
|
use rust_nex::util::SplittableBufferConnection;
|
2025-06-29 12:01:31 +02:00
|
|
|
|
2025-07-30 21:39:54 +02:00
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
|
|
|
|
setup();
|
2025-06-13 12:36:28 +02:00
|
|
|
|
2025-07-30 21:39:54 +02:00
|
|
|
let conn = tokio::net::TcpStream::connect(&*SECURE_EDGE_NODE_HOLDER).await.unwrap();
|
2025-06-29 11:40:42 +02:00
|
|
|
|
2025-07-30 21:39:54 +02:00
|
|
|
let conn: SplittableBufferConnection = conn.into();
|
2025-06-13 10:05:38 +02:00
|
|
|
|
2025-07-30 22:15:31 +02:00
|
|
|
conn.send(Register(SocketAddrV4::new(*OWN_IP_PUBLIC, *SERVER_PORT)).to_data()).await;
|
2025-06-13 12:36:28 +02:00
|
|
|
|
2025-07-30 21:39:54 +02:00
|
|
|
let conn = new_rmc_gateway_connection(conn, |r| Arc::new(OnlyRemote::<RemoteEdgeNodeHolder>::new(r)));
|
2025-06-13 10:05:38 +02:00
|
|
|
|
2025-06-29 11:40:42 +02:00
|
|
|
|
|
|
|
|
|
2025-06-13 12:36:28 +02:00
|
|
|
let (router_secure, _) = Router::new(SocketAddrV4::new(*OWN_IP_PRIVATE, *SERVER_PORT))
|
|
|
|
|
.await
|
|
|
|
|
.expect("unable to start router");
|
|
|
|
|
|
|
|
|
|
let mut socket_secure = router_secure
|
|
|
|
|
.add_socket(VirtualPort::new(1, 10), Secure(
|
|
|
|
|
"6f599f81",
|
2025-07-30 22:42:12 +02:00
|
|
|
SECURE_SERVER_ACCOUNT.clone()
|
2025-06-13 12:36:28 +02:00
|
|
|
))
|
|
|
|
|
.await
|
|
|
|
|
.expect("unable to add socket");
|
|
|
|
|
|
|
|
|
|
// let conn = socket_secure.connect(auth_sockaddr).await.unwrap();
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
let Some(mut conn) = socket_secure.accept().await else {
|
|
|
|
|
error!("server crashed");
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
task::spawn(async move {
|
2025-06-30 10:53:10 +02:00
|
|
|
let mut stream
|
2025-07-30 21:39:54 +02:00
|
|
|
= match TcpStream::connect(*FORWARD_DESTINATION).await {
|
2025-06-30 10:53:10 +02:00
|
|
|
Ok(v) => v,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("unable to connect: {}", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-06-30 10:47:48 +02:00
|
|
|
};
|
2025-06-13 12:36:28 +02:00
|
|
|
|
|
|
|
|
if let Err(e) = stream.send_buffer(&ConnectionInitData{
|
|
|
|
|
prudpsock_addr: conn.socket_addr,
|
|
|
|
|
pid: conn.user_id
|
|
|
|
|
}.to_data()).await{
|
|
|
|
|
error!("error connecting to backend: {}", e);
|
|
|
|
|
return;
|
|
|
|
|
};
|
2025-06-13 10:05:38 +02:00
|
|
|
|
2025-06-29 11:40:42 +02:00
|
|
|
|
|
|
|
|
|
2025-06-13 12:36:28 +02:00
|
|
|
loop {
|
|
|
|
|
tokio::select! {
|
|
|
|
|
data = conn.recv() => {
|
|
|
|
|
let Some(data) = data else {
|
|
|
|
|
break;
|
|
|
|
|
};
|
2025-06-13 10:05:38 +02:00
|
|
|
|
2025-06-13 12:36:28 +02:00
|
|
|
if let Err(e) = stream.send_buffer(&data[..]).await{
|
|
|
|
|
error!("error sending data to backend: {}", e);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
data = stream.read_buffer() => {
|
|
|
|
|
let data = match data{
|
|
|
|
|
Ok(d) => d,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("error reveiving data from backend: {}", e);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if conn.send(data).await == None{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-06-29 11:40:42 +02:00
|
|
|
_ = sleep(Duration::from_secs(10)) => {
|
|
|
|
|
conn.send([0,0,0,0,0].to_vec()).await;
|
|
|
|
|
}
|
2025-06-13 12:36:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-06-13 10:05:38 +02:00
|
|
|
}
|