Skip to content

Commit

Permalink
Don't attempt to unescape escaped unicode characters + removed depend…
Browse files Browse the repository at this point in the history
…ence on reqwest
  • Loading branch information
dpleshkov committed Sep 10, 2023
1 parent 9b6af53 commit 952d282
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 209 deletions.
173 changes: 1 addition & 172 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ edition = "2021"
tokio = { version = "1", features = ["full"] }
hyper-tungstenite = "0.11.1"
hyper = { version = "0.14", features = ["full"] }
hyper-tls = { version = "0.5.0", features = ["vendored"]}
tokio-tungstenite = {version = "0.20", features = ["default", "native-tls-vendored"]}
reqwest = { version = "0.11", features = ["json", "native-tls-vendored"], default-features = false }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }

futures-util = "0.3"
futures-enum = "0.1"
futures-core = "0.3"
Expand Down
20 changes: 14 additions & 6 deletions src/listener.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::fmt::Formatter;

use futures::{SinkExt, StreamExt};
Expand Down Expand Up @@ -335,8 +335,15 @@ async fn listener_main(address: String, proxy: Option<String>, game_id: u16, mut
println!("Connection closed to {}", game_id);
return Ok(())
}
Message::Text(msg) => {
let msg: GenericJSONMessage = serde_json::from_str(msg.as_str()).expect("failed parsing msg");
Message::Text(text) => {
let msg: GenericJSONMessage;
if let Ok(data) = serde_json::from_str::<GenericJSONMessage>(text.as_str()) {
msg = data;
} else {
dbg!(text.clone());
msg = serde_json::from_str(text.as_str().replace("\\u", "\\\\u").as_str()).expect("Failed parsing msg");
}
//let msg: GenericJSONMessage = serde_json::from_str(msg.as_str()).expect("failed parsing msg");
match msg.name.as_str() {
"player_name" => {
if let Ok(player_name) = serde_json::from_value::<GameDataPlayer>(msg.data) {
Expand Down Expand Up @@ -371,12 +378,12 @@ async fn listener_main(address: String, proxy: Option<String>, game_id: u16, mut
let mut packet = vec![1u8; encoded_byte_length];
let map_size = welcome_msg.mode.map_size;

let mut existing_ids: Vec<u8> = vec![0u8; 32];
let mut existing_ids = HashSet::<u8>::new();
let players = welcome_msg.players.as_mut().unwrap();

for i in (2..len).step_by(8) {
let id = buf[i];
existing_ids[(id >> 3) as usize] = existing_ids[(id >> 3) as usize] | (1 << (id & 0b111));
existing_ids.insert(id);
// is likely a more elegant way to do this im missing
let rx = if buf[i+1] > 127 {-(!buf[i+1] as i8)} else {buf[i+1] as i8};
let ry = if buf[i+2] > 127 {-(!buf[i+2] as i8)} else {buf[i+2] as i8};
Expand Down Expand Up @@ -439,8 +446,9 @@ async fn listener_main(address: String, proxy: Option<String>, game_id: u16, mut
packet[d+13] = (p).to_le_bytes()[0];
packet[d+14] = (p).to_le_bytes()[1];
}
// TODO: make this not use a whole hashset
for i in 0u8..=255 {
if existing_ids[(i >> 3) as usize] & (1 << (i & 0b111)) == 0 && players.contains_key(&i) {
if players.contains_key(&i) && !existing_ids.contains(&i) {
players.remove(&i);
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/listener_manager.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration};
use reqwest;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;
use tokio::time::sleep;

use hyper::{Client};
use hyper_tls::HttpsConnector;

use crate::utils::{get_join_packet_name, get_sim_status, to_wss_address, Location, System, get_ms_since_epoch};
use crate::listener::{GameData, Listener};

Expand Down Expand Up @@ -131,14 +133,14 @@ async fn listener_manager_task(rx: mpsc::Receiver<(ManagerRequest, oneshot::Send
let listeners: Arc<Mutex<HashMap<String, Arc<Listener>>>> = Arc::new(Mutex::new(HashMap::new()));
let custom_listeners: Arc<Mutex<HashMap<String, Arc<Listener>>>> = Arc::new(Mutex::new(HashMap::new()));
let mut custom_locations: Vec<Location> = vec![];
let client = reqwest::Client::new();
let client = Client::builder().build::<_, hyper::Body>(HttpsConnector::new());

let (sim_status_tx, sim_status_rx) = mpsc::channel::<Vec<Location>>(1);


println!("Fetching join packet...");

let join_packet_name = get_join_packet_name(Some(&client)).await.expect("Failed retrieving join packet");
let join_packet_name = get_join_packet_name(Some(client.clone())).await.expect("Failed retrieving join packet");

tokio::spawn(listener_signaling_task(rx, Arc::clone(&listeners), Arc::clone(&custom_listeners), sim_status_rx, join_packet_name.clone()));

Expand All @@ -147,10 +149,10 @@ async fn listener_manager_task(rx: mpsc::Receiver<(ManagerRequest, oneshot::Send
loop {
println!("Checking listeners...");

let mut sim_status_res = get_sim_status(Some(&client)).await;
let mut sim_status_res = get_sim_status(Some(client.clone())).await;
while sim_status_res.is_err() {
println!("Error fetching simstatus.json. Retrying...");
sim_status_res = get_sim_status(Some(&client)).await;
sim_status_res = get_sim_status(Some(client.clone())).await;
}

let mut sim_status = sim_status_res.unwrap();
Expand Down
Loading

0 comments on commit 952d282

Please sign in to comment.