2025-06-13 12:36:28 +02:00
|
|
|
|
2025-06-30 10:47:48 +02:00
|
|
|
use rust_nex::reggie::{tls_connect_to, LocalProxy};
|
2025-06-13 10:05:38 +02:00
|
|
|
use std::env;
|
|
|
|
|
use std::ffi::CStr;
|
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
use std::net::{Ipv4Addr, SocketAddrV4, TcpListener, TcpStream};
|
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;
|
|
|
|
|
use tokio::net::TcpSocket;
|
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-06-29 12:01:31 +02:00
|
|
|
use rust_nex::executables::common::{OWN_IP_PRIVATE, OWN_IP_PUBLIC, 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-06-29 12:01:31 +02:00
|
|
|
use rust_nex::reggie::{establish_tls_connection_to, ProxyManagement, UnitPacketRead, UnitPacketWrite};
|
|
|
|
|
use rust_nex::reggie::ServerCluster::Auth;
|
|
|
|
|
use rust_nex::reggie::ServerType::Proxy;
|
2025-06-29 11:40:42 +02:00
|
|
|
use rust_nex::rmc::protocols::OnlyRemote;
|
|
|
|
|
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-06-13 10:05:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static FORWARD_DESTINATION: Lazy<String> =
|
2025-06-13 12:36:28 +02:00
|
|
|
Lazy::new(|| env::var("FORWARD_DESTINATION").expect("no forward destination given"));
|
|
|
|
|
static FORWARD_DESTINATION_NAME: Lazy<String> =
|
|
|
|
|
Lazy::new(|| env::var("FORWARD_DESTINATION_NAME").expect("no forward destination name given"));
|
2025-06-13 10:05:38 +02:00
|
|
|
|
2025-06-29 11:40:42 +02:00
|
|
|
#[rmc_struct(Proxy)]
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct DestinationHolder{
|
|
|
|
|
url: RwLock<String>
|
|
|
|
|
}
|
2025-06-13 10:05:38 +02:00
|
|
|
|
2025-06-29 11:40:42 +02:00
|
|
|
impl ProxyManagement for DestinationHolder{
|
|
|
|
|
async fn update_url(&self, new_url: String) -> Result<(), ErrorCode> {
|
|
|
|
|
println!("updating url");
|
2025-06-13 10:05:38 +02:00
|
|
|
|
2025-06-29 11:40:42 +02:00
|
|
|
let mut url = self.url.write().await;
|
2025-06-13 10:05:38 +02:00
|
|
|
|
2025-06-29 11:40:42 +02:00
|
|
|
*url = new_url;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-13 10:05:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
|
|
|
|
setup();
|
|
|
|
|
|
2025-06-29 11:40:42 +02:00
|
|
|
let conn =
|
|
|
|
|
rust_nex::reggie::rmc_connect_to(
|
|
|
|
|
"agmp-control.spfn.net",
|
|
|
|
|
Proxy {
|
|
|
|
|
addr: SocketAddrV4::new(*OWN_IP_PUBLIC, *SERVER_PORT),
|
|
|
|
|
cluster: Auth
|
|
|
|
|
},
|
|
|
|
|
|r| Arc::new(DestinationHolder::default())
|
|
|
|
|
).await;
|
|
|
|
|
let dest_holder = conn.unwrap();
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-29 11:40:42 +02:00
|
|
|
let dest_holder = dest_holder.clone();
|
|
|
|
|
|
2025-06-13 10:05:38 +02:00
|
|
|
task::spawn(async move {
|
2025-06-29 11:40:42 +02:00
|
|
|
let dest = dest_holder.url.read().await;
|
|
|
|
|
|
|
|
|
|
if *dest == ""{
|
|
|
|
|
warn!("no destination set yet but connection attempted");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:53:10 +02:00
|
|
|
let mut stream
|
|
|
|
|
= match tls_connect_to(&dest).await {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|