2025-06-13 12:36:28 +02:00
|
|
|
|
2025-07-30 21:39:54 +02:00
|
|
|
use rust_nex::reggie::RemoteEdgeNodeHolder;
|
2025-06-13 10:05:38 +02:00
|
|
|
use std::env;
|
|
|
|
|
use std::ffi::CStr;
|
|
|
|
|
use std::io::{Read, Write};
|
2025-07-30 21:39:54 +02:00
|
|
|
use std::net::{Ipv4Addr, SocketAddrV4};
|
2025-06-29 11:40:42 +02:00
|
|
|
use std::sync::{Arc, OnceLock};
|
|
|
|
|
use std::time::Duration;
|
2025-06-13 10:05:38 +02:00
|
|
|
use bytemuck::{Pod, Zeroable};
|
|
|
|
|
use chacha20::{ChaCha20, Key};
|
|
|
|
|
use chacha20::cipher::{Iv, KeyIvInit, StreamCipher};
|
2025-06-29 11:40:42 +02:00
|
|
|
use log::{error, warn};
|
|
|
|
|
use macros::rmc_struct;
|
2025-06-13 10:05:38 +02:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
use rsa::pkcs8::{DecodePrivateKey, DecodePublicKey, Document};
|
|
|
|
|
use rsa::{BigUint, Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};
|
|
|
|
|
use rsa::pkcs1::EncodeRsaPublicKey;
|
|
|
|
|
use rsa::pss::BlindedSigningKey;
|
|
|
|
|
use rsa::signature::{RandomizedSigner, SignatureEncoding};
|
|
|
|
|
use sha2::Sha256;
|
2025-07-30 21:39:54 +02:00
|
|
|
use tokio::net::{TcpSocket, TcpStream};
|
2025-06-29 11:40:42 +02:00
|
|
|
use tokio::sync::RwLock;
|
2025-06-13 10:05:38 +02:00
|
|
|
use tokio::task;
|
2025-06-29 11:40:42 +02:00
|
|
|
use tokio::time::sleep;
|
2025-06-13 10:10:04 +02:00
|
|
|
use rust_nex::common::setup;
|
2025-07-30 21:39:54 +02:00
|
|
|
use rust_nex::executables::common::{FORWARD_DESTINATION, OWN_IP_PRIVATE, OWN_IP_PUBLIC, SECURE_EDGE_NODE_HOLDER, SERVER_PORT};
|
2025-06-13 10:10:04 +02:00
|
|
|
use rust_nex::prudp::packet::VirtualPort;
|
|
|
|
|
use rust_nex::prudp::router::Router;
|
2025-06-29 11:40:42 +02:00
|
|
|
use rust_nex::prudp::station_url::StationUrl;
|
2025-06-13 10:10:04 +02:00
|
|
|
use rust_nex::prudp::unsecure::Unsecure;
|
2025-07-30 21:39:54 +02:00
|
|
|
use rust_nex::reggie::{UnitPacketRead, UnitPacketWrite};
|
|
|
|
|
use rust_nex::reggie::EdgeNodeHolderConnectOption::{DontRegister, Register};
|
|
|
|
|
use rust_nex::rmc::protocols::{new_rmc_gateway_connection, OnlyRemote};
|
2025-06-29 11:40:42 +02:00
|
|
|
use rust_nex::rmc::response::ErrorCode;
|
2025-06-13 10:10:04 +02:00
|
|
|
use rust_nex::rmc::structures::RmcSerialize;
|
|
|
|
|
use rust_nex::rnex_proxy_common::ConnectionInitData;
|
2025-07-30 21:39:54 +02:00
|
|
|
use rust_nex::util::SplittableBufferConnection;
|
2025-06-13 10:05:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
|
|
|
|
setup();
|
|
|
|
|
|
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-07-30 22:15:31 +02:00
|
|
|
conn.send(Register(SocketAddrV4::new(*OWN_IP_PUBLIC, *SERVER_PORT)).to_data()).await;
|
2025-07-30 21:39:54 +02:00
|
|
|
|
|
|
|
|
let conn = new_rmc_gateway_connection(conn, |r| Arc::new(OnlyRemote::<RemoteEdgeNodeHolder>::new(r)));
|
2025-06-29 11:40:42 +02:00
|
|
|
|
2025-06-13 10:05:38 +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), Unsecure(
|
|
|
|
|
"6f599f81"
|
|
|
|
|
))
|
|
|
|
|
.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 10:05:38 +02:00
|
|
|
|
|
|
|
|
if let Err(e) = stream.send_buffer(&ConnectionInitData{
|
2025-06-13 12:36:28 +02:00
|
|
|
prudpsock_addr: conn.socket_addr,
|
|
|
|
|
pid: conn.user_id
|
2025-06-13 10:05:38 +02:00
|
|
|
}.to_data()).await{
|
|
|
|
|
error!("error connecting to backend: {}", e);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
tokio::select! {
|
|
|
|
|
data = conn.recv() => {
|
|
|
|
|
let Some(data) = data else {
|
|
|
|
|
break;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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 10:05:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|