2025-03-15 20:36:39 -04:00
|
|
|
use std::cmp::min;
|
|
|
|
|
2025-03-21 21:26:32 -04:00
|
|
|
use tokio::{io::AsyncReadExt, net::TcpStream};
|
2025-03-15 20:36:39 -04:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
blowfish::Blowfish,
|
2025-03-21 21:26:32 -04:00
|
|
|
common::{
|
|
|
|
custom_ipc::{CustomIpcData, CustomIpcSegment, CustomIpcType},
|
2025-03-22 17:00:21 -04:00
|
|
|
get_world_name, timestamp_secs,
|
2025-03-21 21:26:32 -04:00
|
|
|
},
|
2025-03-22 16:47:21 -04:00
|
|
|
config::get_config,
|
2025-03-21 21:26:32 -04:00
|
|
|
oodle::OodleNetwork,
|
2025-03-15 20:36:39 -04:00
|
|
|
packet::{
|
2025-03-16 17:43:29 -04:00
|
|
|
CompressionType, ConnectionType, PacketSegment, PacketState, SegmentType,
|
|
|
|
generate_encryption_key, parse_packet, send_packet,
|
2025-03-15 20:36:39 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2025-03-21 21:26:32 -04:00
|
|
|
use super::ipc::{
|
|
|
|
CharacterDetails, LobbyCharacterList, LobbyServerList, LobbyServiceAccountList, Server,
|
|
|
|
ServerLobbyIpcData, ServerLobbyIpcSegment, ServerLobbyIpcType, ServiceAccount,
|
2025-03-16 17:43:29 -04:00
|
|
|
};
|
|
|
|
use crate::lobby::ipc::ClientLobbyIpcSegment;
|
2025-03-16 15:39:44 -04:00
|
|
|
|
2025-03-17 17:17:19 -04:00
|
|
|
/// Represents a single connection between an instance of the client and the lobby server.
|
2025-03-15 20:36:39 -04:00
|
|
|
pub struct LobbyConnection {
|
|
|
|
pub socket: TcpStream,
|
|
|
|
|
2025-03-17 16:58:48 -04:00
|
|
|
pub session_id: Option<String>,
|
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
pub state: PacketState,
|
2025-03-21 19:56:16 -04:00
|
|
|
|
|
|
|
pub stored_character_creation_name: String,
|
2025-03-15 20:36:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LobbyConnection {
|
2025-03-16 17:43:29 -04:00
|
|
|
pub async fn parse_packet(
|
|
|
|
&mut self,
|
|
|
|
data: &[u8],
|
|
|
|
) -> (Vec<PacketSegment<ClientLobbyIpcSegment>>, ConnectionType) {
|
2025-03-15 20:36:39 -04:00
|
|
|
parse_packet(data, &mut self.state).await
|
|
|
|
}
|
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
pub async fn send_segment(&mut self, segment: PacketSegment<ServerLobbyIpcSegment>) {
|
2025-03-15 20:36:39 -04:00
|
|
|
send_packet(
|
|
|
|
&mut self.socket,
|
|
|
|
&mut self.state,
|
2025-03-18 19:49:52 -04:00
|
|
|
ConnectionType::Lobby,
|
2025-03-15 20:36:39 -04:00
|
|
|
CompressionType::Uncompressed,
|
2025-03-18 19:49:52 -04:00
|
|
|
&[segment],
|
2025-03-15 20:36:39 -04:00
|
|
|
)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
2025-03-17 17:17:19 -04:00
|
|
|
/// Send an acknowledgement to the client that we generated a valid encryption key.
|
2025-03-15 20:36:39 -04:00
|
|
|
pub async fn initialize_encryption(&mut self, phrase: &str, key: &[u8; 4]) {
|
|
|
|
// Generate an encryption key for this client
|
|
|
|
self.state.client_key = Some(generate_encryption_key(key, phrase));
|
|
|
|
|
|
|
|
let mut data = 0xE0003C2Au32.to_le_bytes().to_vec();
|
|
|
|
data.resize(0x280, 0);
|
|
|
|
|
|
|
|
let blowfish = Blowfish::new(&self.state.client_key.unwrap());
|
|
|
|
blowfish.encrypt(&mut data);
|
|
|
|
|
|
|
|
self.send_segment(PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::InitializationEncryptionResponse { data },
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
2025-03-17 17:17:19 -04:00
|
|
|
/// Send the service account list to the client.
|
2025-03-15 20:36:39 -04:00
|
|
|
pub async fn send_account_list(&mut self) {
|
|
|
|
// send the client the service account list
|
|
|
|
let service_accounts = [ServiceAccount {
|
|
|
|
id: 0x002E4A2B,
|
|
|
|
unk1: 0,
|
|
|
|
index: 0,
|
|
|
|
name: "FINAL FANTASY XIV".to_string(),
|
|
|
|
}]
|
|
|
|
.to_vec();
|
|
|
|
|
2025-03-16 18:15:19 -04:00
|
|
|
let service_account_list =
|
|
|
|
ServerLobbyIpcData::LobbyServiceAccountList(LobbyServiceAccountList {
|
|
|
|
sequence: 0,
|
|
|
|
num_service_accounts: service_accounts.len() as u8,
|
|
|
|
unk1: 3,
|
|
|
|
unk2: 0x99,
|
|
|
|
service_accounts: service_accounts.to_vec(),
|
|
|
|
});
|
2025-03-15 20:36:39 -04:00
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let ipc = ServerLobbyIpcSegment {
|
2025-03-15 20:36:39 -04:00
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
op_code: ServerLobbyIpcType::LobbyServiceAccountList,
|
2025-03-15 20:36:39 -04:00
|
|
|
server_id: 0,
|
|
|
|
timestamp: timestamp_secs(),
|
|
|
|
data: service_account_list,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.send_segment(PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::Ipc { data: ipc },
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
2025-03-17 17:17:19 -04:00
|
|
|
/// Send the world, retainer and character list to the client.
|
2025-03-15 20:36:39 -04:00
|
|
|
pub async fn send_lobby_info(&mut self, sequence: u64) {
|
|
|
|
let mut packets = Vec::new();
|
|
|
|
// send them the server list
|
|
|
|
{
|
2025-03-22 17:00:21 -04:00
|
|
|
let config = get_config();
|
|
|
|
|
2025-03-15 20:36:39 -04:00
|
|
|
let mut servers = [Server {
|
2025-03-22 17:00:21 -04:00
|
|
|
id: config.world.world_id,
|
2025-03-15 20:36:39 -04:00
|
|
|
index: 0,
|
|
|
|
flags: 0,
|
|
|
|
icon: 0,
|
2025-03-22 17:00:21 -04:00
|
|
|
name: get_world_name(config.world.world_id),
|
2025-03-15 20:36:39 -04:00
|
|
|
}]
|
|
|
|
.to_vec();
|
|
|
|
// add any empty boys
|
|
|
|
servers.resize(6, Server::default());
|
|
|
|
|
2025-03-16 18:15:19 -04:00
|
|
|
let lobby_server_list = ServerLobbyIpcData::LobbyServerList(LobbyServerList {
|
2025-03-15 20:36:39 -04:00
|
|
|
sequence: 0,
|
|
|
|
unk1: 1,
|
|
|
|
offset: 0,
|
|
|
|
num_servers: 1,
|
|
|
|
servers,
|
2025-03-16 18:15:19 -04:00
|
|
|
});
|
2025-03-15 20:36:39 -04:00
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let ipc = ServerLobbyIpcSegment {
|
2025-03-15 20:36:39 -04:00
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
op_code: ServerLobbyIpcType::LobbyServerList,
|
2025-03-15 20:36:39 -04:00
|
|
|
server_id: 0,
|
|
|
|
timestamp: timestamp_secs(),
|
|
|
|
data: lobby_server_list,
|
|
|
|
};
|
|
|
|
|
|
|
|
let response_packet = PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::Ipc { data: ipc },
|
|
|
|
};
|
|
|
|
packets.push(response_packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
// send them the retainer list
|
|
|
|
{
|
2025-03-16 17:43:29 -04:00
|
|
|
let lobby_retainer_list = ServerLobbyIpcData::LobbyRetainerList { unk1: 1 };
|
2025-03-15 20:36:39 -04:00
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let ipc = ServerLobbyIpcSegment {
|
2025-03-15 20:36:39 -04:00
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
op_code: ServerLobbyIpcType::LobbyRetainerList,
|
2025-03-15 20:36:39 -04:00
|
|
|
server_id: 0,
|
|
|
|
timestamp: timestamp_secs(),
|
|
|
|
data: lobby_retainer_list,
|
|
|
|
};
|
|
|
|
|
|
|
|
let response_packet = PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::Ipc { data: ipc },
|
|
|
|
};
|
|
|
|
packets.push(response_packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
send_packet(
|
|
|
|
&mut self.socket,
|
|
|
|
&mut self.state,
|
2025-03-18 19:49:52 -04:00
|
|
|
ConnectionType::Lobby,
|
2025-03-15 20:36:39 -04:00
|
|
|
CompressionType::Uncompressed,
|
2025-03-18 19:49:52 -04:00
|
|
|
&packets,
|
2025-03-15 20:36:39 -04:00
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// now send them the character list
|
|
|
|
{
|
2025-03-21 21:26:32 -04:00
|
|
|
let charlist_request = CustomIpcSegment {
|
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
|
|
|
op_code: CustomIpcType::RequestCharacterList,
|
|
|
|
server_id: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
data: CustomIpcData::RequestCharacterList {
|
|
|
|
service_account_id: 0x1, // TODO: placeholder
|
|
|
|
},
|
2025-03-15 20:36:39 -04:00
|
|
|
};
|
|
|
|
|
2025-03-21 21:26:32 -04:00
|
|
|
let name_response = send_custom_world_packet(charlist_request)
|
|
|
|
.await
|
|
|
|
.expect("Failed to get name request packet!");
|
|
|
|
let CustomIpcData::RequestCharacterListRepsonse { characters } = &name_response.data
|
|
|
|
else {
|
|
|
|
panic!("Unexpedted custom IPC type!")
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut characters = characters.to_vec();
|
|
|
|
|
|
|
|
dbg!(&characters);
|
2025-03-15 20:36:39 -04:00
|
|
|
|
|
|
|
for i in 0..4 {
|
|
|
|
let mut characters_in_packet = Vec::new();
|
|
|
|
for _ in 0..min(characters.len(), 2) {
|
|
|
|
characters_in_packet.push(characters.swap_remove(0));
|
|
|
|
}
|
|
|
|
// add any empty boys
|
|
|
|
characters_in_packet.resize(2, CharacterDetails::default());
|
|
|
|
|
|
|
|
let lobby_character_list = if i == 3 {
|
|
|
|
// On the last packet, add the account-wide information
|
2025-03-16 18:15:19 -04:00
|
|
|
LobbyCharacterList {
|
2025-03-15 20:36:39 -04:00
|
|
|
sequence,
|
|
|
|
counter: (i * 4) + 1, // TODO: why the + 1 here?
|
|
|
|
num_in_packet: characters_in_packet.len() as u8,
|
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
|
|
|
unk3: 0,
|
|
|
|
unk4: 128,
|
|
|
|
unk5: [0; 7],
|
|
|
|
unk6: 0,
|
|
|
|
veteran_rank: 0,
|
|
|
|
unk7: 0,
|
2025-03-17 17:43:28 -04:00
|
|
|
days_subscribed: 30,
|
|
|
|
remaining_days: 30,
|
2025-03-15 20:36:39 -04:00
|
|
|
days_to_next_rank: 0,
|
|
|
|
unk8: 8,
|
|
|
|
max_characters_on_world: 2,
|
|
|
|
entitled_expansion: 4,
|
|
|
|
characters: characters_in_packet,
|
|
|
|
}
|
|
|
|
} else {
|
2025-03-16 18:15:19 -04:00
|
|
|
LobbyCharacterList {
|
2025-03-15 20:36:39 -04:00
|
|
|
sequence,
|
|
|
|
counter: i * 4,
|
|
|
|
num_in_packet: characters_in_packet.len() as u8,
|
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
|
|
|
unk3: 0,
|
|
|
|
unk4: 0,
|
|
|
|
unk5: [0; 7],
|
|
|
|
unk6: 0,
|
|
|
|
veteran_rank: 0,
|
|
|
|
unk7: 0,
|
|
|
|
days_subscribed: 0,
|
|
|
|
remaining_days: 0,
|
|
|
|
days_to_next_rank: 0,
|
|
|
|
max_characters_on_world: 0,
|
|
|
|
unk8: 0,
|
|
|
|
entitled_expansion: 0,
|
|
|
|
characters: characters_in_packet,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let ipc = ServerLobbyIpcSegment {
|
2025-03-15 20:36:39 -04:00
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
op_code: ServerLobbyIpcType::LobbyCharacterList,
|
2025-03-15 20:36:39 -04:00
|
|
|
server_id: 0,
|
|
|
|
timestamp: timestamp_secs(),
|
2025-03-16 18:15:19 -04:00
|
|
|
data: ServerLobbyIpcData::LobbyCharacterList(lobby_character_list),
|
2025-03-15 20:36:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
self.send_segment(PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::Ipc { data: ipc },
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-17 17:17:19 -04:00
|
|
|
/// Send the host information for the world server to the client.
|
2025-03-21 19:56:16 -04:00
|
|
|
pub async fn send_enter_world(&mut self, sequence: u64, content_id: u64, actor_id: u32) {
|
2025-03-22 16:47:21 -04:00
|
|
|
let config = get_config();
|
2025-03-15 20:36:39 -04:00
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let enter_world = ServerLobbyIpcData::LobbyEnterWorld {
|
2025-03-15 20:36:39 -04:00
|
|
|
sequence,
|
2025-03-21 19:56:16 -04:00
|
|
|
actor_id,
|
|
|
|
content_id,
|
|
|
|
token: String::new(),
|
2025-03-22 16:47:21 -04:00
|
|
|
port: config.world.port,
|
|
|
|
host: config.world.listen_address,
|
2025-03-15 20:36:39 -04:00
|
|
|
};
|
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let ipc = ServerLobbyIpcSegment {
|
2025-03-15 20:36:39 -04:00
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
op_code: ServerLobbyIpcType::LobbyEnterWorld,
|
2025-03-15 20:36:39 -04:00
|
|
|
server_id: 0,
|
|
|
|
timestamp: timestamp_secs(),
|
|
|
|
data: enter_world,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.send_segment(PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::Ipc { data: ipc },
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
2025-03-16 15:20:55 -04:00
|
|
|
|
2025-03-17 17:17:19 -04:00
|
|
|
/// Send a lobby error to the client.
|
2025-03-16 15:20:55 -04:00
|
|
|
pub async fn send_error(&mut self, sequence: u64, error: u32, exd_error: u16) {
|
2025-03-16 17:43:29 -04:00
|
|
|
let lobby_error = ServerLobbyIpcData::LobbyError {
|
2025-03-16 15:20:55 -04:00
|
|
|
sequence,
|
|
|
|
error,
|
|
|
|
value: 0,
|
|
|
|
exd_error_id: exd_error,
|
|
|
|
unk1: 1,
|
|
|
|
};
|
|
|
|
|
2025-03-16 17:43:29 -04:00
|
|
|
let ipc = ServerLobbyIpcSegment {
|
2025-03-16 15:20:55 -04:00
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
2025-03-16 17:43:29 -04:00
|
|
|
op_code: ServerLobbyIpcType::LobbyError,
|
2025-03-16 15:20:55 -04:00
|
|
|
server_id: 0,
|
|
|
|
timestamp: timestamp_secs(),
|
|
|
|
data: lobby_error,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.send_segment(PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::Ipc { data: ipc },
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
2025-03-15 20:36:39 -04:00
|
|
|
}
|
2025-03-21 21:26:32 -04:00
|
|
|
|
|
|
|
/// 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> {
|
2025-03-22 16:47:21 -04:00
|
|
|
let config = get_config();
|
|
|
|
|
|
|
|
let addr = config.world.get_socketaddr();
|
|
|
|
|
|
|
|
let mut stream = TcpStream::connect(addr).await.unwrap();
|
2025-03-21 21:26:32 -04:00
|
|
|
|
|
|
|
let mut packet_state = PacketState {
|
|
|
|
client_key: None,
|
|
|
|
serverbound_oodle: OodleNetwork::new(),
|
|
|
|
clientbound_oodle: OodleNetwork::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let segment: PacketSegment<CustomIpcSegment> = PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
|
|
|
segment_type: SegmentType::CustomIpc { data: segment },
|
|
|
|
};
|
|
|
|
|
|
|
|
send_packet(
|
|
|
|
&mut stream,
|
|
|
|
&mut packet_state,
|
|
|
|
ConnectionType::None,
|
|
|
|
CompressionType::Uncompressed,
|
|
|
|
&[segment],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let mut buf = [0; 10024]; // TODO: this large buffer is just working around these packets not being compressed, but they really should be!
|
|
|
|
let n = stream.read(&mut buf).await.expect("Failed to read data!");
|
|
|
|
|
|
|
|
println!("Got {n} bytes of response!");
|
|
|
|
|
|
|
|
let (segments, _) = parse_packet::<CustomIpcSegment>(&buf[..n], &mut packet_state).await;
|
|
|
|
|
|
|
|
match &segments[0].segment_type {
|
|
|
|
SegmentType::CustomIpc { data } => Some(data.clone()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|