|
|
@ -1,23 +1,8 @@ |
|
|
|
use { |
|
|
|
use bevy::{prelude::*, window::WindowResolution}; |
|
|
|
aeronet::{ |
|
|
|
|
|
|
|
io::{ |
|
|
|
|
|
|
|
connection::{DisconnectReason, Disconnected}, |
|
|
|
|
|
|
|
Session, SessionEndpoint, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
transport::TransportConfig, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
aeronet_webtransport::{ |
|
|
|
|
|
|
|
cert, |
|
|
|
|
|
|
|
cert::CertificateHash, |
|
|
|
|
|
|
|
client::{ClientConfig, WebTransportClient, WebTransportClientPlugin}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
bevy::{prelude::*, window::WindowResolution}, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn main() -> AppExit { |
|
|
|
fn main() -> AppExit { |
|
|
|
App::new() |
|
|
|
App::new() |
|
|
|
.add_plugins(( |
|
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin { |
|
|
|
DefaultPlugins.set(WindowPlugin { |
|
|
|
|
|
|
|
primary_window: Some(Window { |
|
|
|
primary_window: Some(Window { |
|
|
|
title: "gæmstone Client".into(), |
|
|
|
title: "gæmstone Client".into(), |
|
|
|
// Steam Deck: DPI appears pretty high, causing everything to be scaled up.
|
|
|
|
// Steam Deck: DPI appears pretty high, causing everything to be scaled up.
|
|
|
@ -30,104 +15,15 @@ fn main() -> AppExit { |
|
|
|
..default() |
|
|
|
..default() |
|
|
|
}), |
|
|
|
}), |
|
|
|
..default() |
|
|
|
..default() |
|
|
|
}), |
|
|
|
})) |
|
|
|
WebTransportClientPlugin, |
|
|
|
.add_systems(Startup, setup) |
|
|
|
)) |
|
|
|
|
|
|
|
.add_systems(Startup, (setup, connect_to_server)) |
|
|
|
|
|
|
|
.add_observer(on_connecting) |
|
|
|
|
|
|
|
.add_observer(on_connected) |
|
|
|
|
|
|
|
.add_observer(on_disconnected) |
|
|
|
|
|
|
|
.run() |
|
|
|
.run() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { |
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { |
|
|
|
commands.spawn(Camera2d::default()); |
|
|
|
commands.spawn(Camera2dBundle::default()); |
|
|
|
commands.spawn(Sprite::from_image(asset_server.load("heck.png"))); |
|
|
|
commands.spawn(SpriteBundle { |
|
|
|
} |
|
|
|
texture: asset_server.load("heck.png"), |
|
|
|
|
|
|
|
..default() |
|
|
|
fn connect_to_server(mut commands: Commands) { |
|
|
|
}); |
|
|
|
// When using a self-signed cert, its hash needs to be provided to the
|
|
|
|
|
|
|
|
// client for it to accept it. Works on native and WASM. On WASM we have
|
|
|
|
|
|
|
|
// the option to just accept the HTTPS server's certificate. On native we
|
|
|
|
|
|
|
|
// could theoretically disable cert checking entirely, but that kind of
|
|
|
|
|
|
|
|
// defeats the point.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Do not hardcode the server's certificate hash. Enter in a UI of some sort?
|
|
|
|
|
|
|
|
// For now, just manually replace the `todo!` with the hash that gets
|
|
|
|
|
|
|
|
// logged in the server output. It looks something like this:
|
|
|
|
|
|
|
|
// ************************
|
|
|
|
|
|
|
|
// SPKI FINGERPRINT
|
|
|
|
|
|
|
|
// 1YLqE3c3ZsRBos35nUrMETfZtCUVIxyIjcskEq0LFYE=
|
|
|
|
|
|
|
|
// CERTIFICATE HASH
|
|
|
|
|
|
|
|
// z3cWU+Pc209kffV440ksqcWxMcCTi9QO6qI7VjVOQfU=
|
|
|
|
|
|
|
|
// ************************
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let target = format!("https://[::1]:{}", common::WEB_TRANSPORT_PORT); |
|
|
|
|
|
|
|
let cert_hash = todo!(); |
|
|
|
|
|
|
|
let cert_hash = cert::hash_from_b64(cert_hash).unwrap(); |
|
|
|
|
|
|
|
let config = web_transport_config(Some(cert_hash)); |
|
|
|
|
|
|
|
commands |
|
|
|
|
|
|
|
.spawn(Name::new(target.clone())) |
|
|
|
|
|
|
|
.queue(WebTransportClient::connect(config, target)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(target_family = "wasm")] |
|
|
|
|
|
|
|
fn web_transport_config(cert_hash: Option<CertificateHash>) -> ClientConfig { |
|
|
|
|
|
|
|
use aeronet_webtransport::xwt_web_sys::{CertificateHash, HashAlgorithm}; |
|
|
|
|
|
|
|
let server_certificate_hashes = cert_hash |
|
|
|
|
|
|
|
.map(|hash| CertificateHash { |
|
|
|
|
|
|
|
algorithm: HashAlgorithm::Sha256, |
|
|
|
|
|
|
|
value: Vec::from(hash), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
ClientConfig { |
|
|
|
|
|
|
|
server_certificate_hashes, |
|
|
|
|
|
|
|
..Default::default() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_family = "wasm"))] |
|
|
|
|
|
|
|
fn web_transport_config(cert_hash: Option<CertificateHash>) -> ClientConfig { |
|
|
|
|
|
|
|
use {aeronet_webtransport::wtransport::tls::Sha256Digest, core::time::Duration}; |
|
|
|
|
|
|
|
let server_certificate_hashes = cert_hash |
|
|
|
|
|
|
|
.map(Sha256Digest::new) |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
ClientConfig::builder() |
|
|
|
|
|
|
|
.with_bind_default() |
|
|
|
|
|
|
|
.with_server_certificate_hashes(server_certificate_hashes) |
|
|
|
|
|
|
|
.keep_alive_interval(Some(Duration::from_secs(1))) |
|
|
|
|
|
|
|
.max_idle_timeout(Some(Duration::from_secs(5))) |
|
|
|
|
|
|
|
.unwrap() |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn on_connecting(trigger: Trigger<OnAdd, SessionEndpoint>, names: Query<&Name>) { |
|
|
|
|
|
|
|
let session = trigger.entity(); |
|
|
|
|
|
|
|
let name = names.get(session).unwrap(); |
|
|
|
|
|
|
|
info!("Connecting to '{name}' ..."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn on_connected(trigger: Trigger<OnAdd, Session>, mut commands: Commands) { |
|
|
|
|
|
|
|
let session = trigger.entity(); |
|
|
|
|
|
|
|
info!("Connected!"); |
|
|
|
|
|
|
|
commands.entity(session).insert((TransportConfig { |
|
|
|
|
|
|
|
max_memory_usage: 64 * 1024, |
|
|
|
|
|
|
|
send_bytes_per_sec: 4 * 1024, |
|
|
|
|
|
|
|
},)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn on_disconnected(trigger: Trigger<Disconnected>) { |
|
|
|
|
|
|
|
match &trigger.event().reason { |
|
|
|
|
|
|
|
DisconnectReason::User(reason) => { |
|
|
|
|
|
|
|
info!("Disconnected (user): {reason}") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
DisconnectReason::Peer(reason) => { |
|
|
|
|
|
|
|
info!("Disconnected (server): {reason}") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
DisconnectReason::Error(err) => { |
|
|
|
|
|
|
|
info!("Disconnected (error): {err:#}") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|