Skip to content

Commit

Permalink
feat: add tls field for servers
Browse files Browse the repository at this point in the history
This allows clients to correctly connect with ws or wss.
  • Loading branch information
Jibbajabbafic committed Oct 27, 2023
1 parent c546d09 commit 30a00d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ use uuid::Uuid;
pub struct GameServer {
name: String,
ip: IpAddr,
tls: bool,
port: u16,
pub players: u32,
}

impl GameServer {
pub fn new(name: String, ip: IpAddr, port: u16) -> GameServer {
pub fn new(name: String, ip: IpAddr, tls: bool, port: u16) -> GameServer {
GameServer {
name,
ip,
tls,
port,
players: 0,
}
Expand All @@ -28,7 +30,7 @@ impl GameServer {
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GameMessage {
Connect { name: String, port: u16 },
Connect { name: String, tls: bool, port: u16 },
Status { players: u32 },
}

Expand Down Expand Up @@ -110,6 +112,7 @@ mod tests {
let server = GameServer::new(
String::from("Test"),
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
false,
12345,
);
let mut server_list = ServerList::new();
Expand All @@ -123,6 +126,7 @@ mod tests {
let server = GameServer::new(
String::from("Test"),
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
false,
12345,
);
let mut server_list = ServerList::new();
Expand All @@ -137,6 +141,7 @@ mod tests {
let server = GameServer::new(
String::from("Test"),
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
false,
12345,
);
let expected = server.clone();
Expand All @@ -152,6 +157,7 @@ mod tests {
let server = GameServer::new(
String::from("Test"),
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
false,
12345,
);
let mut server_list = ServerList::new();
Expand Down
14 changes: 10 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,17 @@ async fn handle_socket(

// loop until the first message is received, which should be the name
while let Some(Ok(msg)) = socket.recv().await {
tracing::debug!("got msg: {:?}", msg);
match msg {
Message::Text(txt) => {
if let Ok(GameMessage::Connect { name, port }) =
if let Ok(GameMessage::Connect { name, tls, port }) =
serde_json::from_str::<GameMessage>(&txt)
{
// if this IP is local then it's on the same host so
// replace the it with the server's public IP
let ip = if is_local_ipv4(ip) { server_ip } else { ip };

let server = GameServer::new(name, ip, port);
let server = GameServer::new(name, ip, tls, port);
tracing::info!("created new game server: {:?}", server);
game_id = server_list.add(server);
break;
Expand Down Expand Up @@ -224,8 +225,13 @@ fn remove_server(mut server_list: ServerList, game_id: &Uuid) {
fn parse_game_message(server_list: &ServerList, server_id: &Uuid, msg: &str) {
if let Ok(json) = serde_json::from_str::<GameMessage>(msg) {
match json {
GameMessage::Connect { name, port } => {
tracing::info!("new game connected with name: {} port: {}", name, port)
GameMessage::Connect { name, tls, port } => {
tracing::info!(
"new game connected with name: {} tls: {} port: {}",
name,
tls,
port
)
}
GameMessage::Status { players } => {
server_list.update(server_id, |game_server| {
Expand Down

0 comments on commit 30a00d3

Please sign in to comment.