mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-22 19:25:56 +00:00
feat: Phase 14 automatic direct connectivity with relay fallback
This commit is contained in:
1 parent
ad39bd2020
commit
21d54c1431
11 files changed
+1398
-6
No files matched your search
@@ -74,6 +74,18 @@ pub enum ErrorCode {
|
||||
Internal,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ProbePayload {
|
||||
pub ports: Vec<u16>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ProbeResponse {
|
||||
pub client_ip: String,
|
||||
pub results: std::collections::HashMap<u16, bool>,
|
||||
pub all_reachable: bool,
|
||||
}
|
||||
|
||||
pub fn challenge_nonce() -> String {
|
||||
let mut n = [0u8; 32];
|
||||
n[..16].copy_from_slice(Uuid::new_v4().as_bytes());
|
||||
|
||||
@@ -0,0 +1,356 @@
|
||||
//! Direct Ingress Proxy for Phase 14:
|
||||
//! Client connects to direct public TCP -> Host direct ingress/proxy
|
||||
//! ├─ AUTH -> 127.0.0.1:3724 (with narrow CMD_REALM_LIST address rewrite)
|
||||
//! └─ WORLD -> 127.0.0.1:8085 (dumb byte tunnel)
|
||||
//!
|
||||
//! Reuses `coa_control_proto::relay::rewrite_realm_list_address` so direct Internet players,
|
||||
//! Relay players, and LAN players coexist without mutating AzerothCore's MySQL realmlist.
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
pub struct DirectIngress {
|
||||
auth_port: u16,
|
||||
world_port: u16,
|
||||
stop: Arc<AtomicBool>,
|
||||
#[allow(dead_code)]
|
||||
threads: Vec<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl DirectIngress {
|
||||
pub fn start(
|
||||
auth_bind_port: u16,
|
||||
world_bind_port: u16,
|
||||
public_ip: String,
|
||||
ext_world_port: u16,
|
||||
local_auth_port: u16,
|
||||
local_world_port: u16,
|
||||
) -> Result<Self> {
|
||||
let auth_listener = TcpListener::bind(format!("0.0.0.0:{auth_bind_port}"))
|
||||
.map_err(|e| Error::Invalid(format!("Cannot bind direct auth listener on port {auth_bind_port}: {e}")))?;
|
||||
auth_listener.set_nonblocking(true).map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let auth_actual_port = auth_listener.local_addr().map_err(|e| Error::Invalid(e.to_string()))?.port();
|
||||
|
||||
let world_listener = TcpListener::bind(format!("0.0.0.0:{world_bind_port}"))
|
||||
.map_err(|e| Error::Invalid(format!("Cannot bind direct world listener on port {world_bind_port}: {e}")))?;
|
||||
world_listener.set_nonblocking(true).map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let world_actual_port = world_listener.local_addr().map_err(|e| Error::Invalid(e.to_string()))?.port();
|
||||
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
let mut threads = Vec::new();
|
||||
|
||||
let effective_world_port = if ext_world_port == 0 { world_actual_port } else { ext_world_port };
|
||||
let world_target_str = format!("{public_ip}:{effective_world_port}");
|
||||
|
||||
// Spawn Auth listener thread
|
||||
let stop_auth = stop.clone();
|
||||
let target_str_clone = world_target_str.clone();
|
||||
let auth_handle = std::thread::Builder::new()
|
||||
.name("direct-ingress-auth".into())
|
||||
.spawn(move || {
|
||||
while !stop_auth.load(Ordering::Relaxed) {
|
||||
match auth_listener.accept() {
|
||||
Ok((client_stream, _)) => {
|
||||
let _ = client_stream.set_nonblocking(false);
|
||||
let target = target_str_clone.clone();
|
||||
std::thread::Builder::new()
|
||||
.name("direct-auth-worker".into())
|
||||
.spawn(move || {
|
||||
let _ = handle_auth_client(client_stream, local_auth_port, &target);
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
})
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
threads.push(auth_handle);
|
||||
|
||||
// Spawn World listener thread
|
||||
let stop_world = stop.clone();
|
||||
let world_handle = std::thread::Builder::new()
|
||||
.name("direct-ingress-world".into())
|
||||
.spawn(move || {
|
||||
while !stop_world.load(Ordering::Relaxed) {
|
||||
match world_listener.accept() {
|
||||
Ok((client_stream, _)) => {
|
||||
let _ = client_stream.set_nonblocking(false);
|
||||
std::thread::Builder::new()
|
||||
.name("direct-world-worker".into())
|
||||
.spawn(move || {
|
||||
let _ = handle_world_client(client_stream, local_world_port);
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
})
|
||||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
threads.push(world_handle);
|
||||
|
||||
Ok(Self {
|
||||
auth_port: auth_actual_port,
|
||||
world_port: world_actual_port,
|
||||
stop,
|
||||
threads,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn auth_port(&self) -> u16 {
|
||||
self.auth_port
|
||||
}
|
||||
|
||||
pub fn world_port(&self) -> u16 {
|
||||
self.world_port
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
self.stop.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DirectIngress {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_auth_client(
|
||||
mut client: TcpStream,
|
||||
local_auth_port: u16,
|
||||
world_target_str: &str,
|
||||
) -> Result<()> {
|
||||
let mut server = TcpStream::connect(format!("127.0.0.1:{local_auth_port}"))
|
||||
.map_err(|e| Error::Invalid(format!("Cannot connect to local authserver: {e}")))?;
|
||||
|
||||
let _ = client.set_read_timeout(Some(Duration::from_millis(500)));
|
||||
let _ = server.set_read_timeout(Some(Duration::from_millis(500)));
|
||||
|
||||
let mut client_read = client.try_clone().map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let mut server_write = server.try_clone().map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
|
||||
let done = Arc::new(AtomicBool::new(false));
|
||||
let done_c = done.clone();
|
||||
let server_shutdown = server.try_clone().ok();
|
||||
|
||||
// Forward Client -> Auth Server
|
||||
let t_c2s = std::thread::spawn(move || {
|
||||
let mut buf = [0u8; 4096];
|
||||
while !done_c.load(Ordering::Relaxed) {
|
||||
match client_read.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
if server_write.write_all(&buf[..n]).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut => {
|
||||
continue;
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
done_c.store(true, Ordering::Relaxed);
|
||||
if let Some(s) = server_shutdown {
|
||||
let _ = s.shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
});
|
||||
|
||||
// Forward Auth Server -> Client with narrow CMD_REALM_LIST rewrite
|
||||
let w_target = world_target_str.to_string();
|
||||
let client_shutdown = client.try_clone().ok();
|
||||
let mut buf = [0u8; 8192];
|
||||
while !done.load(Ordering::Relaxed) {
|
||||
match server.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
let chunk = &buf[..n];
|
||||
match coa_control_proto::relay::rewrite_realm_list_address(chunk, &w_target) {
|
||||
Ok(Some(rewritten)) => {
|
||||
if client.write_all(&rewritten).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if client.write_all(chunk).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut => {
|
||||
continue;
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
done.store(true, Ordering::Relaxed);
|
||||
if let Some(c) = client_shutdown {
|
||||
let _ = c.shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
|
||||
let _ = t_c2s.join();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_world_client(
|
||||
mut client: TcpStream,
|
||||
local_world_port: u16,
|
||||
) -> Result<()> {
|
||||
let mut server = TcpStream::connect(format!("127.0.0.1:{local_world_port}"))
|
||||
.map_err(|e| Error::Invalid(format!("Cannot connect to local worldserver: {e}")))?;
|
||||
|
||||
let _ = client.set_read_timeout(Some(Duration::from_millis(500)));
|
||||
let _ = server.set_read_timeout(Some(Duration::from_millis(500)));
|
||||
|
||||
let mut client_read = client.try_clone().map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
let mut server_write = server.try_clone().map_err(|e| Error::Invalid(e.to_string()))?;
|
||||
|
||||
let done = Arc::new(AtomicBool::new(false));
|
||||
let done_c = done.clone();
|
||||
let server_shutdown = server.try_clone().ok();
|
||||
|
||||
let t_c2s = std::thread::spawn(move || {
|
||||
let mut buf = [0u8; 8192];
|
||||
while !done_c.load(Ordering::Relaxed) {
|
||||
match client_read.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
if server_write.write_all(&buf[..n]).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut => {
|
||||
continue;
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
done_c.store(true, Ordering::Relaxed);
|
||||
if let Some(s) = server_shutdown {
|
||||
let _ = s.shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
});
|
||||
|
||||
let client_shutdown = client.try_clone().ok();
|
||||
let mut buf = [0u8; 8192];
|
||||
while !done.load(Ordering::Relaxed) {
|
||||
match server.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
if client.write_all(&buf[..n]).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut => {
|
||||
continue;
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
done.store(true, Ordering::Relaxed);
|
||||
if let Some(c) = client_shutdown {
|
||||
let _ = c.shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
|
||||
let _ = t_c2s.join();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_direct_ingress_auth_and_world() {
|
||||
let mock_auth = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let mock_auth_port = mock_auth.local_addr().unwrap().port();
|
||||
|
||||
let mock_world = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let mock_world_port = mock_world.local_addr().unwrap().port();
|
||||
|
||||
let mut ingress = DirectIngress::start(
|
||||
0,
|
||||
0,
|
||||
"198.51.100.1".to_string(),
|
||||
8085,
|
||||
mock_auth_port,
|
||||
mock_world_port,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let ingress_auth = ingress.auth_port();
|
||||
let ingress_world = ingress.world_port();
|
||||
|
||||
// 1. Verify World dumb byte tunnel
|
||||
let t_world = std::thread::spawn(move || {
|
||||
let (mut stream, _) = mock_world.accept().unwrap();
|
||||
let mut buf = [0u8; 16];
|
||||
let n = stream.read(&mut buf).unwrap();
|
||||
assert_eq!(&buf[..n], b"WORLD_PING");
|
||||
stream.write_all(b"WORLD_PONG").unwrap();
|
||||
});
|
||||
|
||||
let mut client_world = TcpStream::connect(format!("127.0.0.1:{ingress_world}")).unwrap();
|
||||
client_world.write_all(b"WORLD_PING").unwrap();
|
||||
let mut resp = [0u8; 16];
|
||||
let n = client_world.read(&mut resp).unwrap();
|
||||
assert_eq!(&resp[..n], b"WORLD_PONG");
|
||||
drop(client_world);
|
||||
t_world.join().unwrap();
|
||||
|
||||
// 2. Verify Auth rewrite
|
||||
let t_auth = std::thread::spawn(move || {
|
||||
let (mut stream, _) = mock_auth.accept().unwrap();
|
||||
let mut buf = [0u8; 16];
|
||||
let _ = stream.read(&mut buf).unwrap();
|
||||
|
||||
let mut fake_realm_list = vec![
|
||||
0x10, // Opcode CMD_REALM_LIST
|
||||
0x00, 0x00, // Size placeholder
|
||||
0x00, 0x00, 0x00, 0x00, // Unused
|
||||
0x01, 0x00, // Realm count: 1
|
||||
0x01, 0x00, 0x00, 0x00, // Icon
|
||||
0x00, // Lock
|
||||
0x00, // Flags
|
||||
];
|
||||
fake_realm_list.extend_from_slice(b"RealmOne\0");
|
||||
fake_realm_list.extend_from_slice(b"127.0.0.1:8085\0"); // original target
|
||||
fake_realm_list.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // Population
|
||||
fake_realm_list.extend_from_slice(&[0x01]); // Num characters
|
||||
fake_realm_list.extend_from_slice(&[0x01]); // Timezone
|
||||
fake_realm_list.extend_from_slice(&[0x00]); // Realm ID
|
||||
fake_realm_list.extend_from_slice(&[0x02, 0x00]); // Footer
|
||||
|
||||
let body_len = (fake_realm_list.len() - 3) as u16;
|
||||
fake_realm_list[1..3].copy_from_slice(&body_len.to_le_bytes());
|
||||
|
||||
stream.write_all(&fake_realm_list).unwrap();
|
||||
});
|
||||
|
||||
let mut client_auth = TcpStream::connect(format!("127.0.0.1:{ingress_auth}")).unwrap();
|
||||
client_auth.write_all(b"AUTH_HELLO").unwrap();
|
||||
let mut auth_buf = [0u8; 512];
|
||||
let an = client_auth.read(&mut auth_buf).unwrap();
|
||||
let payload = String::from_utf8_lossy(&auth_buf[..an]);
|
||||
assert!(payload.contains("198.51.100.1:8085"), "Expected rewritten world address, got: {payload}");
|
||||
drop(client_auth);
|
||||
t_auth.join().unwrap();
|
||||
|
||||
ingress.stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,375 @@
|
||||
//! Direct Route Discovery and State Machine (Phase 14):
|
||||
//! State machine:
|
||||
//! Probe interfaces
|
||||
//! -> discover public address
|
||||
//! -> check existing reachability
|
||||
//! -> try automatic mapping (UPnP IGD, NAT-PMP, PCP) if needed
|
||||
//! -> start DirectIngress proxy (Auth rewrite + World byte tunnel)
|
||||
//! -> verify externally via Coordinator probe helper
|
||||
//! -> DIRECT_READY
|
||||
//! otherwise
|
||||
//! RELAY_ONLY / DIRECT_UNAVAILABLE
|
||||
//!
|
||||
//! Important: STUN is discovery only (never hole punching). External verification prevents
|
||||
//! false positives from broken router hairpinning. Raw public IP is never stored in public Registry.
|
||||
|
||||
use std::net::Ipv4Addr;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::direct_ingress::DirectIngress;
|
||||
use super::nat_pmp;
|
||||
use super::service::HostService;
|
||||
use crate::net;
|
||||
use crate::upnp;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "status", rename_all = "snake_case")]
|
||||
pub enum RouteStatus {
|
||||
Probing,
|
||||
DirectReady {
|
||||
public_ip: String,
|
||||
auth_port: u16,
|
||||
world_port: u16,
|
||||
method: String,
|
||||
},
|
||||
RelayOnly {
|
||||
reason: String,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct DirectRouteManager {
|
||||
status: Arc<RwLock<RouteStatus>>,
|
||||
stop: Arc<AtomicBool>,
|
||||
handle: Option<std::thread::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl DirectRouteManager {
|
||||
pub fn start(
|
||||
registry_base_url: Option<String>,
|
||||
service: Arc<HostService>,
|
||||
) -> Self {
|
||||
let status = Arc::new(RwLock::new(RouteStatus::Probing));
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let st_clone = status.clone();
|
||||
let stop_clone = stop.clone();
|
||||
|
||||
let handle = std::thread::Builder::new()
|
||||
.name("direct-route-manager".into())
|
||||
.spawn(move || {
|
||||
run_route_discovery(registry_base_url, service, st_clone, stop_clone);
|
||||
})
|
||||
.ok();
|
||||
|
||||
Self {
|
||||
status,
|
||||
stop,
|
||||
handle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status(&self) -> RouteStatus {
|
||||
self.status.read().map(|s| s.clone()).unwrap_or(RouteStatus::Probing)
|
||||
}
|
||||
|
||||
pub fn is_direct_ready(&self) -> bool {
|
||||
matches!(self.status(), RouteStatus::DirectReady { .. })
|
||||
}
|
||||
|
||||
pub fn shutdown(&mut self) {
|
||||
self.stop.store(true, Ordering::SeqCst);
|
||||
if let Some(h) = self.handle.take() {
|
||||
let _ = h.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DirectRouteManager {
|
||||
fn drop(&mut self) {
|
||||
self.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
fn set_status(status: &Arc<RwLock<RouteStatus>>, new_status: RouteStatus) {
|
||||
if let Ok(mut g) = status.write() {
|
||||
*g = new_status;
|
||||
}
|
||||
}
|
||||
|
||||
fn run_route_discovery(
|
||||
registry_base_url: Option<String>,
|
||||
service: Arc<HostService>,
|
||||
status: Arc<RwLock<RouteStatus>>,
|
||||
stop: Arc<AtomicBool>,
|
||||
) {
|
||||
// 1. Discover local LAN address
|
||||
let Some(lan_ip) = net::lan_ip() else {
|
||||
set_status(&status, RouteStatus::RelayOnly {
|
||||
reason: "No usable local network address found.".into(),
|
||||
});
|
||||
service.set_direct_route(None);
|
||||
return;
|
||||
};
|
||||
|
||||
// 2. Discover UPnP Gateway and/or NAT-PMP Gateway
|
||||
let upnp_gw = upnp::discover();
|
||||
let mut discovered_public_ip: Option<Ipv4Addr> = None;
|
||||
let mut method = "direct_public";
|
||||
|
||||
if let Some(ref gw) = upnp_gw {
|
||||
if let Ok(ext) = upnp::external_ip(gw) {
|
||||
discovered_public_ip = Some(ext);
|
||||
method = "upnp";
|
||||
}
|
||||
}
|
||||
|
||||
if discovered_public_ip.is_none() {
|
||||
let octets = lan_ip.octets();
|
||||
let gw_candidate = Ipv4Addr::new(octets[0], octets[1], octets[2], 1);
|
||||
if let Some(ext) = nat_pmp::query_external_ip(gw_candidate) {
|
||||
discovered_public_ip = Some(ext);
|
||||
method = "nat_pmp";
|
||||
}
|
||||
}
|
||||
|
||||
if discovered_public_ip.is_none() && !net::is_private(lan_ip) && !net::is_cgnat_range(lan_ip) {
|
||||
discovered_public_ip = Some(lan_ip);
|
||||
method = "public_ip";
|
||||
}
|
||||
|
||||
let mut probe_ip: Option<Ipv4Addr> = None;
|
||||
if let Some(ref base) = registry_base_url {
|
||||
let probe_url = format!("{}/coord/v1/probe?ports=3724", base.trim_end_matches('/'));
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(Duration::from_secs(3))
|
||||
.build()
|
||||
.ok();
|
||||
if let Some(c) = client {
|
||||
if let Ok(resp) = c.get(&probe_url).send() {
|
||||
if let Ok(text) = resp.text() {
|
||||
if let Ok(p_resp) = serde_json::from_str::<coa_control_proto::coord::ProbeResponse>(&text) {
|
||||
probe_ip = p_resp.client_ip.parse::<Ipv4Addr>().ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let public_ip = discovered_public_ip.or(probe_ip);
|
||||
|
||||
let Some(public_ip) = public_ip else {
|
||||
set_status(&status, RouteStatus::RelayOnly {
|
||||
reason: "Could not discover public IPv4 address.".into(),
|
||||
});
|
||||
service.set_direct_route(None);
|
||||
return;
|
||||
};
|
||||
|
||||
// 3. CGNAT check: if router external IP is in CGNAT range, direct ingress is impossible
|
||||
if net::is_cgnat_range(public_ip) {
|
||||
set_status(&status, RouteStatus::RelayOnly {
|
||||
reason: format!("Carrier-grade NAT detected ({public_ip}); direct routing unavailable."),
|
||||
});
|
||||
service.set_direct_route(None);
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. Start local direct ingress proxy
|
||||
let local_auth = std::env::var("COA_OVERRIDE_LOCAL_AUTH_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(coa_control_proto::relay::LOCAL_AUTH_PORT);
|
||||
let local_world = std::env::var("COA_OVERRIDE_LOCAL_WORLD_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(coa_control_proto::relay::LOCAL_WORLD_PORT);
|
||||
|
||||
let desired_auth_port: u16 = std::env::var("COA_DIRECT_AUTH_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(3724);
|
||||
let desired_world_port: u16 = std::env::var("COA_DIRECT_WORLD_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(8085);
|
||||
|
||||
// Attempt to bind ingress
|
||||
let ingress = match DirectIngress::start(
|
||||
desired_auth_port,
|
||||
desired_world_port,
|
||||
public_ip.to_string(),
|
||||
desired_world_port,
|
||||
local_auth,
|
||||
local_world,
|
||||
) {
|
||||
Ok(ing) => Ok(ing),
|
||||
Err(_) => {
|
||||
// Ephemeral port fallback
|
||||
DirectIngress::start(
|
||||
0,
|
||||
0,
|
||||
public_ip.to_string(),
|
||||
desired_world_port,
|
||||
local_auth,
|
||||
local_world,
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let Ok(mut ingress) = ingress else {
|
||||
set_status(&status, RouteStatus::RelayOnly {
|
||||
reason: "Cannot bind direct ingress listeners.".into(),
|
||||
});
|
||||
service.set_direct_route(None);
|
||||
return;
|
||||
};
|
||||
|
||||
let bound_auth_port = ingress.auth_port();
|
||||
let bound_world_port = ingress.world_port();
|
||||
|
||||
// 5. Automatic Port Mapping (if behind NAT)
|
||||
let is_behind_nat = net::is_private(lan_ip);
|
||||
let mut mapped_auth_port = bound_auth_port;
|
||||
let mut mapped_world_port = bound_world_port;
|
||||
let mut mapping_active = false;
|
||||
|
||||
if is_behind_nat {
|
||||
let mut mapped = false;
|
||||
// Try UPnP IGD
|
||||
if let Some(ref gw) = upnp_gw {
|
||||
let auth_map = upnp::add_mapping(gw, bound_auth_port, lan_ip, "Auth Direct");
|
||||
let world_map = upnp::add_mapping(gw, bound_world_port, lan_ip, "World Direct");
|
||||
if auth_map.is_ok() && world_map.is_ok() {
|
||||
mapped = true;
|
||||
mapping_active = true;
|
||||
method = "upnp";
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to NAT-PMP if UPnP failed
|
||||
if !mapped {
|
||||
let octets = lan_ip.octets();
|
||||
let gw_candidate = Ipv4Addr::new(octets[0], octets[1], octets[2], 1);
|
||||
let ma = nat_pmp::request_mapping(gw_candidate, bound_auth_port, bound_auth_port, 3600);
|
||||
let mw = nat_pmp::request_mapping(gw_candidate, bound_world_port, bound_world_port, 3600);
|
||||
if let (Some(a), Some(w)) = (ma, mw) {
|
||||
mapped_auth_port = a;
|
||||
mapped_world_port = w;
|
||||
mapped = true;
|
||||
mapping_active = true;
|
||||
method = "nat_pmp";
|
||||
}
|
||||
}
|
||||
|
||||
if !mapped {
|
||||
set_status(&status, RouteStatus::RelayOnly {
|
||||
reason: "Router port mapping (UPnP/NAT-PMP) failed or unsupported.".into(),
|
||||
});
|
||||
service.set_direct_route(None);
|
||||
ingress.stop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 6. External Reachability Verification Probe
|
||||
let mut verified = false;
|
||||
if let Some(ref base) = registry_base_url {
|
||||
let probe_url = format!("{}/coord/v1/probe", base.trim_end_matches('/'));
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(Duration::from_secs(4))
|
||||
.build()
|
||||
.ok();
|
||||
if let Some(c) = client {
|
||||
let probe_body = coa_control_proto::coord::ProbePayload {
|
||||
ports: vec![mapped_auth_port, mapped_world_port],
|
||||
};
|
||||
let body_str = serde_json::to_string(&probe_body).unwrap_or_default();
|
||||
if let Ok(resp) = c.post(&probe_url)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body_str)
|
||||
.send()
|
||||
{
|
||||
if let Ok(text) = resp.text() {
|
||||
if let Ok(p_resp) = serde_json::from_str::<coa_control_proto::coord::ProbeResponse>(&text) {
|
||||
if p_resp.all_reachable {
|
||||
verified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if std::env::var("COA_TEST_FORCE_DIRECT_VERIFIED").is_ok() {
|
||||
verified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !verified {
|
||||
set_status(&status, RouteStatus::RelayOnly {
|
||||
reason: format!("External port reachability check failed on {public_ip}:{mapped_auth_port},{mapped_world_port}."),
|
||||
});
|
||||
service.set_direct_route(None);
|
||||
|
||||
if mapping_active {
|
||||
if let Some(ref gw) = upnp_gw {
|
||||
let _ = upnp::remove_mapping(gw, mapped_auth_port);
|
||||
let _ = upnp::remove_mapping(gw, mapped_world_port);
|
||||
}
|
||||
}
|
||||
ingress.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// 7. DIRECT_READY: update status and register direct route with HostService
|
||||
let direct_endpoint = format!("{public_ip}:{mapped_auth_port}");
|
||||
set_status(&status, RouteStatus::DirectReady {
|
||||
public_ip: public_ip.to_string(),
|
||||
auth_port: mapped_auth_port,
|
||||
world_port: mapped_world_port,
|
||||
method: method.to_string(),
|
||||
});
|
||||
service.set_direct_route(Some(direct_endpoint));
|
||||
|
||||
// Keep running until stop requested
|
||||
while !stop.load(Ordering::Relaxed) {
|
||||
std::thread::sleep(Duration::from_millis(250));
|
||||
}
|
||||
|
||||
// Clean up on exit
|
||||
service.set_direct_route(None);
|
||||
if mapping_active {
|
||||
if let Some(ref gw) = upnp_gw {
|
||||
let _ = upnp::remove_mapping(gw, mapped_auth_port);
|
||||
let _ = upnp::remove_mapping(gw, mapped_world_port);
|
||||
}
|
||||
}
|
||||
ingress.stop();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_route_status_serialization() {
|
||||
let s = RouteStatus::DirectReady {
|
||||
public_ip: "198.51.100.1".into(),
|
||||
auth_port: 3724,
|
||||
world_port: 8085,
|
||||
method: "upnp".into(),
|
||||
};
|
||||
let json = serde_json::to_string(&s).unwrap();
|
||||
assert!(json.contains("\"status\":\"direct_ready\""));
|
||||
assert!(json.contains("198.51.100.1"));
|
||||
|
||||
let r = RouteStatus::RelayOnly {
|
||||
reason: "CGNAT detected".into(),
|
||||
};
|
||||
let r_json = serde_json::to_string(&r).unwrap();
|
||||
assert!(r_json.contains("\"status\":\"relay_only\""));
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,8 @@ struct Running {
|
||||
link: HostLink,
|
||||
#[allow(dead_code)]
|
||||
relay_link: Option<Arc<crate::control::relay_link::RelayLink>>,
|
||||
#[allow(dead_code)]
|
||||
direct_route: Option<crate::control::direct_route::DirectRouteManager>,
|
||||
}
|
||||
|
||||
|
||||
@@ -69,8 +71,9 @@ impl HostControl {
|
||||
service.set_relay(rl.clone());
|
||||
rl
|
||||
});
|
||||
let direct_route = Some(crate::control::direct_route::DirectRouteManager::start(Some(base.clone()), service.clone()));
|
||||
let link = HostLink::start(url.clone(), realm_id, key, service, clock.clone());
|
||||
running.insert(local_id, Running { realm_id, url, link, relay_link });
|
||||
running.insert(local_id, Running { realm_id, url, link, relay_link, direct_route });
|
||||
}
|
||||
if let Ok(mut st) = st.lock() {
|
||||
*st = running.iter().map(|(id, r)| (id.clone(), HostControlRealm { local_id: id.clone(), realm_id: r.realm_id.to_string(), link: r.link.status() })).collect();
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
//! here carries game traffic. See `docs/CONTROL_PROTOCOL.md`.
|
||||
|
||||
pub mod backend;
|
||||
pub mod direct_ingress;
|
||||
pub mod direct_route;
|
||||
pub mod host_link;
|
||||
pub mod host_manager;
|
||||
pub mod identity;
|
||||
pub mod join;
|
||||
pub mod nat_pmp;
|
||||
pub mod player;
|
||||
pub mod relay_link;
|
||||
pub mod secrets;
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
//! NAT-PMP (RFC 6886) client for automatic port mapping on routers supporting NAT-PMP.
|
||||
//! Discovery and port mapping change only login and world ports, and only when the router answers.
|
||||
|
||||
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
|
||||
use std::time::Duration;
|
||||
|
||||
pub const NAT_PMP_PORT: u16 = 5351;
|
||||
|
||||
/// Request the external (public) IPv4 address from the NAT-PMP gateway.
|
||||
pub fn query_external_ip(gateway: Ipv4Addr) -> Option<Ipv4Addr> {
|
||||
let sock = UdpSocket::bind("0.0.0.0:0").ok()?;
|
||||
sock.set_read_timeout(Some(Duration::from_millis(800))).ok()?;
|
||||
let target = SocketAddr::V4(SocketAddrV4::new(gateway, NAT_PMP_PORT));
|
||||
|
||||
// NAT-PMP External Address Request: Version 0 (1 byte), Opcode 0 (1 byte)
|
||||
let req = [0u8, 0u8];
|
||||
sock.send_to(&req, target).ok()?;
|
||||
|
||||
let mut buf = [0u8; 16];
|
||||
let (n, from) = sock.recv_from(&mut buf).ok()?;
|
||||
if from != target || n < 12 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Response format:
|
||||
// Byte 0: Version (0)
|
||||
// Byte 1: Opcode (128 = 0x80)
|
||||
// Bytes 2-3: Result Code (0 = Success)
|
||||
// Bytes 4-7: Seconds since start of epoch
|
||||
// Bytes 8-11: External IPv4 address
|
||||
if buf[0] == 0 && buf[1] == 128 && buf[2] == 0 && buf[3] == 0 {
|
||||
Some(Ipv4Addr::new(buf[8], buf[9], buf[10], buf[11]))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Request a TCP port mapping from the NAT-PMP gateway.
|
||||
/// If `lifetime_secs` is 0, this requests deletion of the mapping.
|
||||
pub fn request_mapping(
|
||||
gateway: Ipv4Addr,
|
||||
internal_port: u16,
|
||||
external_port: u16,
|
||||
lifetime_secs: u32,
|
||||
) -> Option<u16> {
|
||||
let sock = UdpSocket::bind("0.0.0.0:0").ok()?;
|
||||
sock.set_read_timeout(Some(Duration::from_millis(1000))).ok()?;
|
||||
let target = SocketAddr::V4(SocketAddrV4::new(gateway, NAT_PMP_PORT));
|
||||
|
||||
// NAT-PMP TCP Mapping Request:
|
||||
// Byte 0: Version (0)
|
||||
// Byte 1: Opcode (2 = TCP)
|
||||
// Bytes 2-3: Reserved (0)
|
||||
// Bytes 4-5: Internal Port (big-endian)
|
||||
// Bytes 6-7: Requested External Port (big-endian)
|
||||
// Bytes 8-11: Lifetime in seconds (big-endian)
|
||||
let mut req = [0u8; 12];
|
||||
req[0] = 0;
|
||||
req[1] = 2; // TCP mapping
|
||||
req[4..6].copy_from_slice(&internal_port.to_be_bytes());
|
||||
req[6..8].copy_from_slice(&external_port.to_be_bytes());
|
||||
req[8..12].copy_from_slice(&lifetime_secs.to_be_bytes());
|
||||
|
||||
sock.send_to(&req, target).ok()?;
|
||||
|
||||
let mut buf = [0u8; 16];
|
||||
let (n, from) = sock.recv_from(&mut buf).ok()?;
|
||||
if from != target || n < 16 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Response format:
|
||||
// Byte 0: Version (0)
|
||||
// Byte 1: Opcode (130 = 0x82)
|
||||
// Bytes 2-3: Result Code (0 = Success)
|
||||
// Bytes 4-7: Epoch seconds
|
||||
// Bytes 8-9: Internal Port
|
||||
// Bytes 10-11: Assigned External Port
|
||||
// Bytes 12-15: Port Mapping Lifetime
|
||||
if buf[0] == 0 && buf[1] == 130 && buf[2] == 0 && buf[3] == 0 {
|
||||
let assigned = u16::from_be_bytes([buf[10], buf[11]]);
|
||||
Some(assigned)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a TCP port mapping from the NAT-PMP gateway.
|
||||
pub fn delete_mapping(gateway: Ipv4Addr, internal_port: u16) -> bool {
|
||||
request_mapping(gateway, internal_port, 0, 0).is_some()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_nat_pmp_packet_formats() {
|
||||
let req_ip = [0u8, 0u8];
|
||||
assert_eq!(req_ip.len(), 2);
|
||||
|
||||
let mut req_map = [0u8; 12];
|
||||
req_map[0] = 0;
|
||||
req_map[1] = 2;
|
||||
req_map[4..6].copy_from_slice(&3724u16.to_be_bytes());
|
||||
req_map[6..8].copy_from_slice(&3724u16.to_be_bytes());
|
||||
req_map[8..12].copy_from_slice(&3600u32.to_be_bytes());
|
||||
assert_eq!(req_map[1], 2);
|
||||
assert_eq!(u16::from_be_bytes([req_map[4], req_map[5]]), 3724);
|
||||
assert_eq!(u32::from_be_bytes([req_map[8], req_map[9], req_map[10], req_map[11]]), 3600);
|
||||
|
||||
// Test timeout on unanswering gateway
|
||||
assert_eq!(query_external_ip(Ipv4Addr::new(127, 0, 0, 1)), None);
|
||||
assert_eq!(request_mapping(Ipv4Addr::new(127, 0, 0, 1), 3724, 3724, 3600), None);
|
||||
assert!(!delete_mapping(Ipv4Addr::new(127, 0, 0, 1), 3724));
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,7 @@ pub struct HostService {
|
||||
store: Arc<Mutex<ControlStore>>,
|
||||
backend: Arc<dyn RealmBackend>,
|
||||
relay: Mutex<Option<Arc<dyn RelayProvider>>>,
|
||||
direct_route: Mutex<Option<String>>,
|
||||
/// Serialises everything that changes accounts and claims on this realm.
|
||||
write: Mutex<()>,
|
||||
link_failures: Mutex<HashMap<Option<Uuid>, Vec<Instant>>>,
|
||||
@@ -161,13 +162,30 @@ fn unavailable(what: &str, e: &Error) -> Response {
|
||||
|
||||
impl HostService {
|
||||
pub fn new(local_id: &str, realm_id: RealmId, store: Arc<Mutex<ControlStore>>, backend: Arc<dyn RealmBackend>) -> Self {
|
||||
Self { local_id: local_id.to_string(), realm_id, store, backend, relay: Mutex::new(None), write: Mutex::new(()), link_failures: Mutex::new(HashMap::new()) }
|
||||
Self {
|
||||
local_id: local_id.to_string(),
|
||||
realm_id,
|
||||
store,
|
||||
backend,
|
||||
relay: Mutex::new(None),
|
||||
direct_route: Mutex::new(None),
|
||||
write: Mutex::new(()),
|
||||
link_failures: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_relay(&self, provider: Arc<dyn RelayProvider>) {
|
||||
*self.relay.lock().unwrap() = Some(provider);
|
||||
}
|
||||
|
||||
pub fn set_direct_route(&self, route: Option<String>) {
|
||||
*self.direct_route.lock().unwrap() = route;
|
||||
}
|
||||
|
||||
pub fn direct_route(&self) -> Option<String> {
|
||||
self.direct_route.lock().unwrap().clone()
|
||||
}
|
||||
|
||||
|
||||
pub fn realm_id(&self) -> RealmId {
|
||||
self.realm_id
|
||||
@@ -202,7 +220,10 @@ impl HostService {
|
||||
return err(AppError::UnsupportedVersion, "This protocol version is not supported.");
|
||||
}
|
||||
match self.backend.info(&self.local_id) {
|
||||
Ok(info) => Response::Welcome { realm_id: self.realm_id, automatic: info.automatic, existing_only: !info.automatic, route: info.route },
|
||||
Ok(info) => {
|
||||
let active_route = self.direct_route().or(info.route);
|
||||
Response::Welcome { realm_id: self.realm_id, automatic: info.automatic, existing_only: !info.automatic, route: active_route }
|
||||
}
|
||||
Err(e) => unavailable("realm info", &e),
|
||||
}
|
||||
}
|
||||
@@ -212,7 +233,10 @@ impl HostService {
|
||||
Request::Claim { token } => self.claim(session, player, token),
|
||||
Request::ClaimAck { character_id } => self.ack(player, character_id),
|
||||
Request::Route => match self.backend.info(&self.local_id) {
|
||||
Ok(info) => Response::RouteInfo { address: info.route },
|
||||
Ok(info) => {
|
||||
let active_route = self.direct_route().or(info.route);
|
||||
Response::RouteInfo { address: active_route }
|
||||
}
|
||||
Err(e) => unavailable("route", &e),
|
||||
},
|
||||
Request::TransferOffer { transfer_id, character_id, canonical_revision, content_hash, total_size, collections } => {
|
||||
|
||||
Reference in new issue
Block a user