rust-nex/rnex-core/src/executables/common.rs

88 lines
2.8 KiB
Rust
Raw Normal View History

use once_cell::sync::Lazy;
2025-11-08 13:00:23 +00:00
use rnex_core::nex::account::Account;
use rnex_core::rmc::protocols::{RmcCallable, RmcConnection, new_rmc_gateway_connection};
use rnex_core::rmc::structures::RmcSerialize;
2026-01-20 20:26:44 +01:00
use rnex_core::rnex_proxy_common::ConnectionInitData;
use std::env;
use std::io::Cursor;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::sync::Arc;
2025-11-08 13:00:23 +00:00
use tokio::net::TcpListener;
use log::error;
2026-01-20 20:26:44 +01:00
use std::error::Error;
2025-11-08 13:00:23 +00:00
use crate::reggie::UnitPacketRead;
2025-11-06 22:55:41 +00:00
const IP_REQ_SERVICE_URL: &str = "https://ipinfo.io/ip";
2026-01-20 20:26:44 +01:00
pub fn try_get_ip() -> Result<Ipv4Addr, Box<dyn Error>> {
let mut req = ureq::get(IP_REQ_SERVICE_URL).call()?;
2025-11-13 09:51:05 +01:00
Ok(req.body_mut().read_to_string()?.parse()?)
2025-11-06 22:55:41 +00:00
}
pub static OWN_IP_PRIVATE: Lazy<Ipv4Addr> = Lazy::new(|| {
env::var("SERVER_IP")
.ok()
2025-11-05 22:47:06 +01:00
.map(|s| s.parse().expect("invalid ip address"))
.unwrap_or(Ipv4Addr::UNSPECIFIED)
});
2025-06-29 11:40:42 +02:00
pub static OWN_IP_PUBLIC: Lazy<Ipv4Addr> = Lazy::new(|| {
env::var("SERVER_IP_PUBLIC")
.ok()
2025-11-06 22:55:41 +00:00
.map(|s| s.parse().expect("invalid ip address"))
2026-01-20 20:26:44 +01:00
.unwrap_or_else(|| try_get_ip().unwrap())
2025-06-29 11:40:42 +02:00
});
pub static SERVER_PORT: Lazy<u16> = Lazy::new(|| {
env::var("SERVER_PORT")
.ok()
.and_then(|s| s.parse().ok())
2026-01-31 17:28:49 +01:00
.unwrap_or(10000)
});
pub static KERBEROS_SERVER_PASSWORD: Lazy<String> = Lazy::new(|| {
env::var("AUTH_SERVER_PASSWORD")
.ok()
.unwrap_or("password".to_owned())
});
pub static AUTH_SERVER_ACCOUNT: Lazy<Account> =
Lazy::new(|| Account::new(1, "Quazal Authentication", &KERBEROS_SERVER_PASSWORD));
pub static SECURE_SERVER_ACCOUNT: Lazy<Account> =
Lazy::new(|| Account::new(2, "Quazal Rendez-Vous", &KERBEROS_SERVER_PASSWORD));
2025-06-29 11:40:42 +02:00
2026-01-20 20:26:44 +01:00
pub async fn new_simple_backend<T: RmcCallable + Sync + Send + 'static, F>(mut creation_function: F)
2025-11-08 13:00:23 +00:00
where
F: FnMut(ConnectionInitData, RmcConnection) -> Arc<T>,
{
2026-01-20 20:26:44 +01:00
let listen = TcpListener::bind(SocketAddrV4::new(*OWN_IP_PRIVATE, *SERVER_PORT))
.await
.unwrap();
2025-11-08 13:00:23 +00:00
while let Ok((mut stream, _addr)) = listen.accept().await {
2026-01-20 20:26:44 +01:00
let buffer = match stream.read_buffer().await {
2025-11-08 13:00:23 +00:00
Ok(v) => v,
Err(e) => {
2026-01-20 20:26:44 +01:00
error!(
"an error ocurred whilest reading connection data buffer: {:?}",
e
);
2025-11-08 13:00:23 +00:00
continue;
}
};
let user_connection_data = ConnectionInitData::deserialize(&mut Cursor::new(buffer));
2025-09-21 15:59:27 +02:00
2026-01-20 20:26:44 +01:00
let user_connection_data = match user_connection_data {
2025-11-08 13:00:23 +00:00
Ok(v) => v,
Err(e) => {
error!("an error ocurred whilest reading connection data: {:?}", e);
continue;
}
};
let fun_ref = &mut creation_function;
2026-01-20 20:26:44 +01:00
new_rmc_gateway_connection(stream.into(), move |r| fun_ref(user_connection_data, r));
2025-11-08 13:00:23 +00:00
}
2026-01-20 20:26:44 +01:00
}