1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-07-23 21:17:45 +00:00

Move send_custom_world_packet to send_helpers module

Instead of things importing it from lobby which doesn't make a ton
of sense.
This commit is contained in:
Joshua Goins 2025-07-17 23:27:53 -04:00
parent efbf45deea
commit 2b49c877b3
6 changed files with 61 additions and 56 deletions

View file

@ -9,11 +9,11 @@ use kawari::ipc::kawari::CustomIpcType;
use kawari::ipc::lobby::ServiceAccount;
use kawari::ipc::lobby::{ClientLobbyIpcData, ServerLobbyIpcSegment};
use kawari::lobby::LobbyConnection;
use kawari::lobby::send_custom_world_packet;
use kawari::packet::ConnectionType;
use kawari::packet::PacketSegment;
use kawari::packet::SegmentType;
use kawari::packet::oodle::OodleNetwork;
use kawari::packet::send_custom_world_packet;
use kawari::packet::{PacketState, SegmentData, send_keep_alive};
use std::fs;
use std::path::MAIN_SEPARATOR_STR;

View file

@ -8,8 +8,8 @@ use axum_extra::extract::CookieJar;
use axum_extra::extract::cookie::{Cookie, Expiration};
use kawari::config::get_config;
use kawari::ipc::kawari::{CustomIpcData, CustomIpcSegment, CustomIpcType};
use kawari::lobby::send_custom_world_packet;
use kawari::login::{LoginDatabase, LoginError};
use kawari::packet::send_custom_world_packet;
use minijinja::{Environment, context};
use serde::Deserialize;
use tower_http::cors::{Any, CorsLayer};

View file

@ -1,9 +1,8 @@
use std::cmp::min;
use tokio::{io::AsyncReadExt, net::TcpStream};
use tokio::net::TcpStream;
use crate::{
RECEIVE_BUFFER_SIZE,
blowfish::Blowfish,
common::timestamp_secs,
config::get_config,
@ -11,7 +10,7 @@ use crate::{
opcodes::ServerLobbyIpcType,
packet::{
CompressionType, ConnectionType, PacketSegment, PacketState, SegmentData, SegmentType,
generate_encryption_key, oodle::OodleNetwork, parse_packet, send_packet,
generate_encryption_key, parse_packet, send_custom_world_packet, send_packet,
},
};
@ -533,49 +532,3 @@ impl LobbyConnection {
}
}
}
/// Sends a custom IPC packet to the world server, meant for private server-to-server communication.
/// Returns the first custom IPC segment returned.
pub async fn send_custom_world_packet(segment: CustomIpcSegment) -> Option<CustomIpcSegment> {
let config = get_config();
let addr = config.world.get_public_socketaddr();
let mut stream = TcpStream::connect(addr).await.unwrap();
let mut packet_state = PacketState {
client_key: None,
serverbound_oodle: OodleNetwork::new(),
clientbound_oodle: OodleNetwork::new(),
};
let segment: PacketSegment<CustomIpcSegment> = PacketSegment {
segment_type: SegmentType::KawariIpc,
data: SegmentData::KawariIpc { data: segment },
..Default::default()
};
send_packet(
&mut stream,
&mut packet_state,
ConnectionType::None,
CompressionType::Uncompressed,
&[segment],
None,
)
.await;
// read response
let mut buf = vec![0; RECEIVE_BUFFER_SIZE];
let n = stream.read(&mut buf).await.expect("Failed to read data!");
if n != 0 {
let (segments, _) = parse_packet::<CustomIpcSegment>(&buf[..n], &mut packet_state);
return match &segments[0].data {
SegmentData::KawariIpc { data } => Some(data.clone()),
_ => None,
};
}
None
}

View file

@ -1,2 +1,2 @@
mod connection;
pub use connection::{LobbyConnection, send_custom_world_packet};
pub use connection::LobbyConnection;

View file

@ -20,7 +20,7 @@ pub mod oodle;
#[cfg(not(target_family = "wasm"))]
mod send_helpers;
#[cfg(not(target_family = "wasm"))]
pub use send_helpers::{send_keep_alive, send_packet};
pub use send_helpers::{send_custom_world_packet, send_keep_alive, send_packet};
mod scrambler;
pub use scrambler::{

View file

@ -1,13 +1,19 @@
use std::io::Cursor;
use binrw::BinWrite;
use tokio::{io::AsyncWriteExt, net::TcpStream};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpStream,
};
use crate::common::timestamp_msecs;
use crate::{
RECEIVE_BUFFER_SIZE, common::timestamp_msecs, config::get_config, ipc::kawari::CustomIpcSegment,
};
use super::{
CompressionType, ConnectionType, PacketHeader, PacketSegment, PacketState, ReadWriteIpcSegment,
ScramblerKeys, SegmentData, SegmentType, compression::compress,
ScramblerKeys, SegmentData, SegmentType, compression::compress, oodle::OodleNetwork,
parse_packet,
};
pub async fn send_packet<T: ReadWriteIpcSegment>(
@ -66,3 +72,49 @@ pub async fn send_keep_alive<T: ReadWriteIpcSegment>(
)
.await;
}
/// Sends a custom IPC packet to the world server, meant for private server-to-server communication.
/// Returns the first custom IPC segment returned.
pub async fn send_custom_world_packet(segment: CustomIpcSegment) -> Option<CustomIpcSegment> {
let config = get_config();
let addr = config.world.get_public_socketaddr();
let mut stream = TcpStream::connect(addr).await.unwrap();
let mut packet_state = PacketState {
client_key: None,
serverbound_oodle: OodleNetwork::new(),
clientbound_oodle: OodleNetwork::new(),
};
let segment: PacketSegment<CustomIpcSegment> = PacketSegment {
segment_type: SegmentType::KawariIpc,
data: SegmentData::KawariIpc { data: segment },
..Default::default()
};
send_packet(
&mut stream,
&mut packet_state,
ConnectionType::None,
CompressionType::Uncompressed,
&[segment],
None,
)
.await;
// read response
let mut buf = vec![0; RECEIVE_BUFFER_SIZE];
let n = stream.read(&mut buf).await.expect("Failed to read data!");
if n != 0 {
let (segments, _) = parse_packet::<CustomIpcSegment>(&buf[..n], &mut packet_state);
return match &segments[0].data {
SegmentData::KawariIpc { data } => Some(data.clone()),
_ => None,
};
}
None
}