mirror of https://git.suyu.dev/suyu/suyu
Merge pull request #8685 from FearlessTobi/multiplayer-part2
core, network: Add ability to proxy socket packetsmerge-requests/60/head
commit
6f931d49c7
@ -0,0 +1,51 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace Network {
|
||||
|
||||
/// Address families
|
||||
enum class Domain : u8 {
|
||||
INET, ///< Address family for IPv4
|
||||
};
|
||||
|
||||
/// Socket types
|
||||
enum class Type {
|
||||
STREAM,
|
||||
DGRAM,
|
||||
RAW,
|
||||
SEQPACKET,
|
||||
};
|
||||
|
||||
/// Protocol values for sockets
|
||||
enum class Protocol : u8 {
|
||||
ICMP,
|
||||
TCP,
|
||||
UDP,
|
||||
};
|
||||
|
||||
/// Shutdown mode
|
||||
enum class ShutdownHow {
|
||||
RD,
|
||||
WR,
|
||||
RDWR,
|
||||
};
|
||||
|
||||
/// Array of IPv4 address
|
||||
using IPv4Address = std::array<u8, 4>;
|
||||
|
||||
/// Cross-platform sockaddr structure
|
||||
struct SockAddrIn {
|
||||
Domain family;
|
||||
IPv4Address ip;
|
||||
u16 portno;
|
||||
};
|
||||
|
||||
constexpr u32 FLAG_MSG_PEEK = 0x2;
|
||||
constexpr u32 FLAG_MSG_DONTWAIT = 0x80;
|
||||
constexpr u32 FLAG_O_NONBLOCK = 0x800;
|
||||
|
||||
} // namespace Network
|
@ -0,0 +1,284 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "common/assert.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "core/internal_network/network.h"
|
||||
#include "core/internal_network/network_interface.h"
|
||||
#include "core/internal_network/socket_proxy.h"
|
||||
|
||||
namespace Network {
|
||||
|
||||
ProxySocket::ProxySocket(RoomNetwork& room_network_) noexcept : room_network{room_network_} {}
|
||||
|
||||
ProxySocket::~ProxySocket() {
|
||||
if (fd == INVALID_SOCKET) {
|
||||
return;
|
||||
}
|
||||
fd = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) {
|
||||
if (protocol != packet.protocol || local_endpoint.portno != packet.remote_endpoint.portno ||
|
||||
closed) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard guard(packets_mutex);
|
||||
received_packets.push(packet);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Errno ProxySocket::SetSockOpt(SOCKET fd_, int option, T value) {
|
||||
LOG_DEBUG(Network, "(STUBBED) called");
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
Errno ProxySocket::Initialize(Domain domain, Type type, Protocol socket_protocol) {
|
||||
protocol = socket_protocol;
|
||||
SetSockOpt(fd, SO_TYPE, type);
|
||||
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
std::pair<ProxySocket::AcceptResult, Errno> ProxySocket::Accept() {
|
||||
LOG_WARNING(Network, "(STUBBED) called");
|
||||
return {AcceptResult{}, Errno::SUCCESS};
|
||||
}
|
||||
|
||||
Errno ProxySocket::Connect(SockAddrIn addr_in) {
|
||||
LOG_WARNING(Network, "(STUBBED) called");
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
std::pair<SockAddrIn, Errno> ProxySocket::GetPeerName() {
|
||||
LOG_WARNING(Network, "(STUBBED) called");
|
||||
return {SockAddrIn{}, Errno::SUCCESS};
|
||||
}
|
||||
|
||||
std::pair<SockAddrIn, Errno> ProxySocket::GetSockName() {
|
||||
LOG_WARNING(Network, "(STUBBED) called");
|
||||
return {SockAddrIn{}, Errno::SUCCESS};
|
||||
}
|
||||
|
||||
Errno ProxySocket::Bind(SockAddrIn addr) {
|
||||
if (is_bound) {
|
||||
LOG_WARNING(Network, "Rebinding Socket is unimplemented!");
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
local_endpoint = addr;
|
||||
is_bound = true;
|
||||
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
Errno ProxySocket::Listen(s32 backlog) {
|
||||
LOG_WARNING(Network, "(STUBBED) called");
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
Errno ProxySocket::Shutdown(ShutdownHow how) {
|
||||
LOG_WARNING(Network, "(STUBBED) called");
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
std::pair<s32, Errno> ProxySocket::Recv(int flags, std::vector<u8>& message) {
|
||||
LOG_WARNING(Network, "(STUBBED) called");
|
||||
ASSERT(flags == 0);
|
||||
ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max()));
|
||||
|
||||
return {static_cast<s32>(0), Errno::SUCCESS};
|
||||
}
|
||||
|
||||
std::pair<s32, Errno> ProxySocket::RecvFrom(int flags, std::vector<u8>& message, SockAddrIn* addr) {
|
||||
ASSERT(flags == 0);
|
||||
ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max()));
|
||||
|
||||
// TODO (flTobi): Verify the timeout behavior and break when connection is lost
|
||||
const auto timestamp = std::chrono::steady_clock::now();
|
||||
// When receive_timeout is set to zero, the socket is supposed to wait indefinitely until a
|
||||
// packet arrives. In order to prevent lost packets from hanging the emulation thread, we set
|
||||
// the timeout to 5s instead
|
||||
const auto timeout = receive_timeout == 0 ? 5000 : receive_timeout;
|
||||
while (true) {
|
||||
{
|
||||
std::lock_guard guard(packets_mutex);
|
||||
if (received_packets.size() > 0) {
|
||||
return ReceivePacket(flags, message, addr, message.size());
|
||||
}
|
||||
}
|
||||
|
||||
if (!blocking) {
|
||||
return {-1, Errno::AGAIN};
|
||||
}
|
||||
|
||||
std::this_thread::yield();
|
||||
|
||||
const auto time_diff = std::chrono::steady_clock::now() - timestamp;
|
||||
const auto time_diff_ms =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(time_diff).count();
|
||||
|
||||
if (time_diff_ms > timeout) {
|
||||
return {-1, Errno::TIMEDOUT};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<s32, Errno> ProxySocket::ReceivePacket(int flags, std::vector<u8>& message,
|
||||
SockAddrIn* addr, std::size_t max_length) {
|
||||
ProxyPacket& packet = received_packets.front();
|
||||
if (addr) {
|
||||
addr->family = Domain::INET;
|
||||
addr->ip = packet.local_endpoint.ip; // The senders ip address
|
||||
addr->portno = packet.local_endpoint.portno; // The senders port number
|
||||
}
|
||||
|
||||
bool peek = (flags & FLAG_MSG_PEEK) != 0;
|
||||
std::size_t read_bytes;
|
||||
if (packet.data.size() > max_length) {
|
||||
read_bytes = max_length;
|
||||
message.clear();
|
||||
std::copy(packet.data.begin(), packet.data.begin() + read_bytes,
|
||||
std::back_inserter(message));
|
||||
message.resize(max_length);
|
||||
|
||||
if (protocol == Protocol::UDP) {
|
||||
if (!peek) {
|
||||
received_packets.pop();
|
||||
}
|
||||
return {-1, Errno::MSGSIZE};
|
||||
} else if (protocol == Protocol::TCP) {
|
||||
std::vector<u8> numArray(packet.data.size() - max_length);
|
||||
std::copy(packet.data.begin() + max_length, packet.data.end(),
|
||||
std::back_inserter(numArray));
|
||||
packet.data = numArray;
|
||||
}
|
||||
} else {
|
||||
read_bytes = packet.data.size();
|
||||
message.clear();
|
||||
std::copy(packet.data.begin(), packet.data.end(), std::back_inserter(message));
|
||||
message.resize(max_length);
|
||||
if (!peek) {
|
||||
received_packets.pop();
|
||||
}
|
||||
}
|
||||
|
||||
return {static_cast<u32>(read_bytes), Errno::SUCCESS};
|
||||
}
|
||||
|
||||
std::pair<s32, Errno> ProxySocket::Send(const std::vector<u8>& message, int flags) {
|
||||
LOG_WARNING(Network, "(STUBBED) called");
|
||||
ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max()));
|
||||
ASSERT(flags == 0);
|
||||
|
||||
return {static_cast<s32>(0), Errno::SUCCESS};
|
||||
}
|
||||
|
||||
void ProxySocket::SendPacket(ProxyPacket& packet) {
|
||||
if (auto room_member = room_network.GetRoomMember().lock()) {
|
||||
if (room_member->IsConnected()) {
|
||||
room_member->SendProxyPacket(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<s32, Errno> ProxySocket::SendTo(u32 flags, const std::vector<u8>& message,
|
||||
const SockAddrIn* addr) {
|
||||
ASSERT(flags == 0);
|
||||
|
||||
if (!is_bound) {
|
||||
LOG_ERROR(Network, "ProxySocket is not bound!");
|
||||
return {static_cast<s32>(message.size()), Errno::SUCCESS};
|
||||
}
|
||||
|
||||
if (auto room_member = room_network.GetRoomMember().lock()) {
|
||||
if (!room_member->IsConnected()) {
|
||||
return {static_cast<s32>(message.size()), Errno::SUCCESS};
|
||||
}
|
||||
}
|
||||
|
||||
ProxyPacket packet;
|
||||
packet.local_endpoint = local_endpoint;
|
||||
packet.remote_endpoint = *addr;
|
||||
packet.protocol = protocol;
|
||||
packet.broadcast = broadcast;
|
||||
|
||||
auto& ip = local_endpoint.ip;
|
||||
auto ipv4 = Network::GetHostIPv4Address();
|
||||
// If the ip is all zeroes (INADDR_ANY) or if it matches the hosts ip address,
|
||||
// replace it with a "fake" routing address
|
||||
if (std::all_of(ip.begin(), ip.end(), [](u8 i) { return i == 0; }) || (ipv4 && ipv4 == ip)) {
|
||||
if (auto room_member = room_network.GetRoomMember().lock()) {
|
||||
packet.local_endpoint.ip = room_member->GetFakeIpAddress();
|
||||
}
|
||||
}
|
||||
|
||||
packet.data.clear();
|
||||
std::copy(message.begin(), message.end(), std::back_inserter(packet.data));
|
||||
|
||||
SendPacket(packet);
|
||||
|
||||
return {static_cast<s32>(message.size()), Errno::SUCCESS};
|
||||
}
|
||||
|
||||
Errno ProxySocket::Close() {
|
||||
fd = INVALID_SOCKET;
|
||||
closed = true;
|
||||
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetLinger(bool enable, u32 linger) {
|
||||
struct Linger {
|
||||
u16 linger_enable;
|
||||
u16 linger_time;
|
||||
} values;
|
||||
values.linger_enable = enable ? 1 : 0;
|
||||
values.linger_time = static_cast<u16>(linger);
|
||||
|
||||
return SetSockOpt(fd, SO_LINGER, values);
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetReuseAddr(bool enable) {
|
||||
return SetSockOpt<u32>(fd, SO_REUSEADDR, enable ? 1 : 0);
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetBroadcast(bool enable) {
|
||||
broadcast = enable;
|
||||
return SetSockOpt<u32>(fd, SO_BROADCAST, enable ? 1 : 0);
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetSndBuf(u32 value) {
|
||||
return SetSockOpt(fd, SO_SNDBUF, value);
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetKeepAlive(bool enable) {
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetRcvBuf(u32 value) {
|
||||
return SetSockOpt(fd, SO_RCVBUF, value);
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetSndTimeo(u32 value) {
|
||||
send_timeout = value;
|
||||
return SetSockOpt(fd, SO_SNDTIMEO, static_cast<int>(value));
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetRcvTimeo(u32 value) {
|
||||
receive_timeout = value;
|
||||
return SetSockOpt(fd, SO_RCVTIMEO, static_cast<int>(value));
|
||||
}
|
||||
|
||||
Errno ProxySocket::SetNonBlock(bool enable) {
|
||||
blocking = !enable;
|
||||
return Errno::SUCCESS;
|
||||
}
|
||||
|
||||
bool ProxySocket::IsOpened() const {
|
||||
return fd != INVALID_SOCKET;
|
||||
}
|
||||
|
||||
} // namespace Network
|
@ -0,0 +1,97 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
#include "common/common_funcs.h"
|
||||
#include "core/internal_network/sockets.h"
|
||||
#include "network/network.h"
|
||||
|
||||
namespace Network {
|
||||
|
||||
class ProxySocket : public SocketBase {
|
||||
public:
|
||||
YUZU_NON_COPYABLE(ProxySocket);
|
||||
YUZU_NON_MOVEABLE(ProxySocket);
|
||||
|
||||
explicit ProxySocket(RoomNetwork& room_network_) noexcept;
|
||||
~ProxySocket() override;
|
||||
|
||||
void HandleProxyPacket(const ProxyPacket& packet) override;
|
||||
|
||||
Errno Initialize(Domain domain, Type type, Protocol socket_protocol) override;
|
||||
|
||||
Errno Close() override;
|
||||
|
||||
std::pair<AcceptResult, Errno> Accept() override;
|
||||
|
||||
Errno Connect(SockAddrIn addr_in) override;
|
||||
|
||||
std::pair<SockAddrIn, Errno> GetPeerName() override;
|
||||
|
||||
std::pair<SockAddrIn, Errno> GetSockName() override;
|
||||
|
||||
Errno Bind(SockAddrIn addr) override;
|
||||
|
||||
Errno Listen(s32 backlog) override;
|
||||
|
||||
Errno Shutdown(ShutdownHow how) override;
|
||||
|
||||
std::pair<s32, Errno> Recv(int flags, std::vector<u8>& message) override;
|
||||
|
||||
std::pair<s32, Errno> RecvFrom(int flags, std::vector<u8>& message, SockAddrIn* addr) override;
|
||||
|
||||
std::pair<s32, Errno> ReceivePacket(int flags, std::vector<u8>& message, SockAddrIn* addr,
|
||||
std::size_t max_length);
|
||||
|
||||
std::pair<s32, Errno> Send(const std::vector<u8>& message, int flags) override;
|
||||
|
||||
void SendPacket(ProxyPacket& packet);
|
||||
|
||||
std::pair<s32, Errno> SendTo(u32 flags, const std::vector<u8>& message,
|
||||
const SockAddrIn* addr) override;
|
||||
|
||||
Errno SetLinger(bool enable, u32 linger) override;
|
||||
|
||||
Errno SetReuseAddr(bool enable) override;
|
||||
|
||||
Errno SetBroadcast(bool enable) override;
|
||||
|
||||
Errno SetKeepAlive(bool enable) override;
|
||||
|
||||
Errno SetSndBuf(u32 value) override;
|
||||
|
||||
Errno SetRcvBuf(u32 value) override;
|
||||
|
||||
Errno SetSndTimeo(u32 value) override;
|
||||
|
||||
Errno SetRcvTimeo(u32 value) override;
|
||||
|
||||
Errno SetNonBlock(bool enable) override;
|
||||
|
||||
template <typename T>
|
||||
Errno SetSockOpt(SOCKET fd, int option, T value);
|
||||
|
||||
bool IsOpened() const override;
|
||||
|
||||
private:
|
||||
bool broadcast = false;
|
||||
bool closed = false;
|
||||
u32 send_timeout = 0;
|
||||
u32 receive_timeout = 0;
|
||||
bool is_bound = false;
|
||||
SockAddrIn local_endpoint{};
|
||||
bool blocking = true;
|
||||
std::queue<ProxyPacket> received_packets;
|
||||
Protocol protocol;
|
||||
|
||||
std::mutex packets_mutex;
|
||||
|
||||
RoomNetwork& room_network;
|
||||
};
|
||||
|
||||
} // namespace Network
|
@ -0,0 +1,27 @@
|
||||
# SPDX-FileCopyrightText: 2017 Citra Emulator Project
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/CMakeModules)
|
||||
|
||||
add_executable(yuzu-room
|
||||
yuzu_room.cpp
|
||||
yuzu_room.rc
|
||||
)
|
||||
|
||||
create_target_directory_groups(yuzu-room)
|
||||
|
||||
target_link_libraries(yuzu-room PRIVATE common core network)
|
||||
if (ENABLE_WEB_SERVICE)
|
||||
target_compile_definitions(yuzu-room PRIVATE -DENABLE_WEB_SERVICE)
|
||||
target_link_libraries(yuzu-room PRIVATE web_service)
|
||||
endif()
|
||||
|
||||
target_link_libraries(yuzu-room PRIVATE mbedtls)
|
||||
if (MSVC)
|
||||
target_link_libraries(yuzu-room PRIVATE getopt)
|
||||
endif()
|
||||
target_link_libraries(yuzu-room PRIVATE ${PLATFORM_LIBRARIES} Threads::Threads)
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
install(TARGETS yuzu-room RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
|
||||
endif()
|
@ -0,0 +1,375 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#ifdef _WIN32
|
||||
// windows.h needs to be included before shellapi.h
|
||||
#include <windows.h>
|
||||
|
||||
#include <shellapi.h>
|
||||
#endif
|
||||
|
||||
#include <mbedtls/base64.h>
|
||||
#include "common/common_types.h"
|
||||
#include "common/detached_tasks.h"
|
||||
#include "common/fs/file.h"
|
||||
#include "common/fs/fs.h"
|
||||
#include "common/fs/path_util.h"
|
||||
#include "common/logging/backend.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/scm_rev.h"
|
||||
#include "common/settings.h"
|
||||
#include "common/string_util.h"
|
||||
#include "core/announce_multiplayer_session.h"
|
||||
#include "core/core.h"
|
||||
#include "network/network.h"
|
||||
#include "network/room.h"
|
||||
#include "network/verify_user.h"
|
||||
|
||||
#ifdef ENABLE_WEB_SERVICE
|
||||
#include "web_service/verify_user_jwt.h"
|
||||
#endif
|
||||
|
||||
#undef _UNICODE
|
||||
#include <getopt.h>
|
||||
#ifndef _MSC_VER
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
static void PrintHelp(const char* argv0) {
|
||||
LOG_INFO(Network,
|
||||
"Usage: {}"
|
||||
" [options] <filename>\n"
|
||||
"--room-name The name of the room\n"
|
||||
"--room-description The room description\n"
|
||||
"--port The port used for the room\n"
|
||||
"--max_members The maximum number of players for this room\n"
|
||||
"--password The password for the room\n"
|
||||
"--preferred-game The preferred game for this room\n"
|
||||
"--preferred-game-id The preferred game-id for this room\n"
|
||||
"--username The username used for announce\n"
|
||||
"--token The token used for announce\n"
|
||||
"--web-api-url yuzu Web API url\n"
|
||||
"--ban-list-file The file for storing the room ban list\n"
|
||||
"--log-file The file for storing the room log\n"
|
||||
"--enable-yuzu-mods Allow yuzu Community Moderators to moderate on your room\n"
|
||||
"-h, --help Display this help and exit\n"
|
||||
"-v, --version Output version information and exit\n",
|
||||
argv0);
|
||||
}
|
||||
|
||||
static void PrintVersion() {
|
||||
LOG_INFO(Network, "yuzu dedicated room {} {} Libnetwork: {}", Common::g_scm_branch,
|
||||
Common::g_scm_desc, Network::network_version);
|
||||
}
|
||||
|
||||
/// The magic text at the beginning of a yuzu-room ban list file.
|
||||
static constexpr char BanListMagic[] = "YuzuRoom-BanList-1";
|
||||
|
||||
static constexpr char token_delimiter{':'};
|
||||
|
||||
static std::string UsernameFromDisplayToken(const std::string& display_token) {
|
||||
std::size_t outlen;
|
||||
|
||||
std::array<unsigned char, 512> output{};
|
||||
mbedtls_base64_decode(output.data(), output.size(), &outlen,
|
||||
reinterpret_cast<const unsigned char*>(display_token.c_str()),
|
||||
display_token.length());
|
||||
std::string decoded_display_token(reinterpret_cast<char*>(&output), outlen);
|
||||
return decoded_display_token.substr(0, decoded_display_token.find(token_delimiter));
|
||||
}
|
||||
|
||||
static std::string TokenFromDisplayToken(const std::string& display_token) {
|
||||
std::size_t outlen;
|
||||
|
||||
std::array<unsigned char, 512> output{};
|
||||
mbedtls_base64_decode(output.data(), output.size(), &outlen,
|
||||
reinterpret_cast<const unsigned char*>(display_token.c_str()),
|
||||
display_token.length());
|
||||
std::string decoded_display_token(reinterpret_cast<char*>(&output), outlen);
|
||||
return decoded_display_token.substr(decoded_display_token.find(token_delimiter) + 1);
|
||||
}
|
||||
|
||||
static Network::Room::BanList LoadBanList(const std::string& path) {
|
||||
std::ifstream file;
|
||||
Common::FS::OpenFileStream(file, path, std::ios_base::in);
|
||||
if (!file || file.eof()) {
|
||||
LOG_ERROR(Network, "Could not open ban list!");
|
||||
return {};
|
||||
}
|
||||
std::string magic;
|
||||
std::getline(file, magic);
|
||||
if (magic != BanListMagic) {
|
||||
LOG_ERROR(Network, "Ban list is not valid!");
|
||||
return {};
|
||||
}
|
||||
|
||||
// false = username ban list, true = ip ban list
|
||||
bool ban_list_type = false;
|
||||
Network::Room::UsernameBanList username_ban_list;
|
||||
Network::Room::IPBanList ip_ban_list;
|
||||
while (!file.eof()) {
|
||||
std::string line;
|
||||
std::getline(file, line);
|
||||
line.erase(std::remove(line.begin(), line.end(), '\0'), line.end());
|
||||
line = Common::StripSpaces(line);
|
||||
if (line.empty()) {
|
||||
// An empty line marks start of the IP ban list
|
||||
ban_list_type = true;
|
||||
continue;
|
||||
}
|
||||
if (ban_list_type) {
|
||||
ip_ban_list.emplace_back(line);
|
||||
} else {
|
||||
username_ban_list.emplace_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
return {username_ban_list, ip_ban_list};
|
||||
}
|
||||
|
||||
static void SaveBanList(const Network::Room::BanList& ban_list, const std::string& path) {
|
||||
std::ofstream file;
|
||||
Common::FS::OpenFileStream(file, path, std::ios_base::out);
|
||||
if (!file) {
|
||||
LOG_ERROR(Network, "Could not save ban list!");
|
||||
return;
|
||||
}
|
||||
|
||||
file << BanListMagic << "\n";
|
||||
|
||||
// Username ban list
|
||||
for (const auto& username : ban_list.first) {
|
||||
file << username << "\n";
|
||||
}
|
||||
file << "\n";
|
||||
|
||||
// IP ban list
|
||||
for (const auto& ip : ban_list.second) {
|
||||
file << ip << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
static void InitializeLogging(const std::string& log_file) {
|
||||
Common::Log::Initialize();
|
||||
Common::Log::SetColorConsoleBackendEnabled(true);
|
||||
Common::Log::Start();
|
||||
}
|
||||
|
||||
/// Application entry point
|
||||
int main(int argc, char** argv) {
|
||||
Common::DetachedTasks detached_tasks;
|
||||
int option_index = 0;
|
||||
char* endarg;
|
||||
|
||||
std::string room_name;
|
||||
std::string room_description;
|
||||
std::string password;
|
||||
std::string preferred_game;
|
||||
std::string username;
|
||||
std::string token;
|
||||
std::string web_api_url;
|
||||
std::string ban_list_file;
|
||||
std::string log_file = "yuzu-room.log";
|
||||
u64 preferred_game_id = 0;
|
||||
u32 port = Network::DefaultRoomPort;
|
||||
u32 max_members = 16;
|
||||
bool enable_yuzu_mods = false;
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"room-name", required_argument, 0, 'n'},
|
||||
{"room-description", required_argument, 0, 'd'},
|
||||
{"port", required_argument, 0, 'p'},
|
||||
{"max_members", required_argument, 0, 'm'},
|
||||
{"password", required_argument, 0, 'w'},
|
||||
{"preferred-game", required_argument, 0, 'g'},
|
||||
{"preferred-game-id", required_argument, 0, 'i'},
|
||||
{"username", optional_argument, 0, 'u'},
|
||||
{"token", required_argument, 0, 't'},
|
||||
{"web-api-url", required_argument, 0, 'a'},
|
||||
{"ban-list-file", required_argument, 0, 'b'},
|
||||
{"log-file", required_argument, 0, 'l'},
|
||||
{"enable-yuzu-mods", no_argument, 0, 'e'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"version", no_argument, 0, 'v'},
|
||||
{0, 0, 0, 0},
|
||||
};
|
||||
|
||||
InitializeLogging(log_file);
|
||||
|
||||
while (optind < argc) {
|
||||
int arg = getopt_long(argc, argv, "n:d:p:m:w:g:u:t:a:i:l:hv", long_options, &option_index);
|
||||
if (arg != -1) {
|
||||
switch (static_cast<char>(arg)) {
|
||||
case 'n':
|
||||
room_name.assign(optarg);
|
||||
break;
|
||||
case 'd':
|
||||
room_description.assign(optarg);
|
||||
break;
|
||||
case 'p':
|
||||
port = strtoul(optarg, &endarg, 0);
|
||||
break;
|
||||
case 'm':
|
||||
max_members = strtoul(optarg, &endarg, 0);
|
||||
break;
|
||||
case 'w':
|
||||
password.assign(optarg);
|
||||
break;
|
||||
case 'g':
|
||||
preferred_game.assign(optarg);
|
||||
break;
|
||||
case 'i':
|
||||
preferred_game_id = strtoull(optarg, &endarg, 16);
|
||||
break;
|
||||
case 'u':
|
||||
username.assign(optarg);
|
||||
break;
|
||||
case 't':
|
||||
token.assign(optarg);
|
||||
break;
|
||||
case 'a':
|
||||
web_api_url.assign(optarg);
|
||||
break;
|
||||
case 'b':
|
||||
ban_list_file.assign(optarg);
|
||||
break;
|
||||
case 'l':
|
||||
log_file.assign(optarg);
|
||||
break;
|
||||
case 'e':
|
||||
enable_yuzu_mods = true;
|
||||
break;
|
||||
case 'h':
|
||||
PrintHelp(argv[0]);
|
||||
return 0;
|
||||
case 'v':
|
||||
PrintVersion();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (room_name.empty()) {
|
||||
LOG_ERROR(Network, "Room name is empty!");
|
||||
PrintHelp(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
if (preferred_game.empty()) {
|
||||
LOG_ERROR(Network, "Preferred game is empty!");
|
||||
PrintHelp(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
if (preferred_game_id == 0) {
|
||||
LOG_ERROR(Network,
|
||||
"preferred-game-id not set!\nThis should get set to allow users to find your "
|
||||
"room.\nSet with --preferred-game-id id");
|
||||
}
|
||||
if (max_members > Network::MaxConcurrentConnections || max_members < 2) {
|
||||
LOG_ERROR(Network, "max_members needs to be in the range 2 - {}!",
|
||||
Network::MaxConcurrentConnections);
|
||||
PrintHelp(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
if (port > UINT16_MAX) {
|
||||
LOG_ERROR(Network, "Port needs to be in the range 0 - 65535!");
|
||||
PrintHelp(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
if (ban_list_file.empty()) {
|
||||
LOG_ERROR(Network, "Ban list file not set!\nThis should get set to load and save room ban "
|
||||
"list.\nSet with --ban-list-file <file>");
|
||||
}
|
||||
bool announce = true;
|
||||
if (token.empty() && announce) {
|
||||
announce = false;
|
||||
LOG_INFO(Network, "Token is empty: Hosting a private room");
|
||||
}
|
||||
if (web_api_url.empty() && announce) {
|
||||
announce = false;
|
||||
LOG_INFO(Network, "Endpoint url is empty: Hosting a private room");
|
||||
}
|
||||
if (announce) {
|
||||
if (username.empty()) {
|
||||
LOG_INFO(Network, "Hosting a public room");
|
||||
Settings::values.web_api_url = web_api_url;
|
||||
Settings::values.yuzu_username = UsernameFromDisplayToken(token);
|
||||
username = Settings::values.yuzu_username.GetValue();
|
||||
Settings::values.yuzu_token = TokenFromDisplayToken(token);
|
||||
} else {
|
||||
LOG_INFO(Network, "Hosting a public room");
|
||||
Settings::values.web_api_url = web_api_url;
|
||||
Settings::values.yuzu_username = username;
|
||||
Settings::values.yuzu_token = token;
|
||||
}
|
||||
}
|
||||
if (!announce && enable_yuzu_mods) {
|
||||
enable_yuzu_mods = false;
|
||||
LOG_INFO(Network, "Can not enable yuzu Moderators for private rooms");
|
||||
}
|
||||
|
||||
// Load the ban list
|
||||
Network::Room::BanList ban_list;
|
||||
if (!ban_list_file.empty()) {
|
||||
ban_list = LoadBanList(ban_list_file);
|
||||
}
|
||||
|
||||
std::unique_ptr<Network::VerifyUser::Backend> verify_backend;
|
||||
if (announce) {
|
||||
#ifdef ENABLE_WEB_SERVICE
|
||||
verify_backend =
|
||||
std::make_unique<WebService::VerifyUserJWT>(Settings::values.web_api_url.GetValue());
|
||||
#else
|
||||
LOG_INFO(Network,
|
||||
"yuzu Web Services is not available with this build: validation is disabled.");
|
||||
verify_backend = std::make_unique<Network::VerifyUser::NullBackend>();
|
||||
#endif
|
||||
} else {
|
||||
verify_backend = std::make_unique<Network::VerifyUser::NullBackend>();
|
||||
}
|
||||
|
||||
Network::RoomNetwork network{};
|
||||
network.Init();
|
||||
if (auto room = network.GetRoom().lock()) {
|
||||
AnnounceMultiplayerRoom::GameInfo preferred_game_info{.name = preferred_game,
|
||||
.id = preferred_game_id};
|
||||
if (!room->Create(room_name, room_description, "", port, password, max_members, username,
|
||||
preferred_game_info, std::move(verify_backend), ban_list,
|
||||
enable_yuzu_mods)) {
|
||||
LOG_INFO(Network, "Failed to create room: ");
|
||||
return -1;
|
||||
}
|
||||
LOG_INFO(Network, "Room is open. Close with Q+Enter...");
|
||||
auto announce_session = std::make_unique<Core::AnnounceMultiplayerSession>(network);
|
||||
if (announce) {
|
||||
announce_session->Start();
|
||||
}
|
||||
while (room->GetState() == Network::Room::State::Open) {
|
||||
std::string in;
|
||||
std::cin >> in;
|
||||
if (in.size() > 0) {
|
||||
break;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
if (announce) {
|
||||
announce_session->Stop();
|
||||
}
|
||||
announce_session.reset();
|
||||
// Save the ban list
|
||||
if (!ban_list_file.empty()) {
|
||||
SaveBanList(room->GetBanList(), ban_list_file);
|
||||
}
|
||||
room->Destroy();
|
||||
}
|
||||
network.Shutdown();
|
||||
detached_tasks.WaitForAllTasks();
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "winresrc.h"
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Icon
|
||||
//
|
||||
|
||||
// Icon with lowest ID value placed first to ensure application icon
|
||||
// remains consistent on all systems.
|
||||
YUZU_ICON ICON "../../dist/yuzu.ico"
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RT_MANIFEST
|
||||
//
|
||||
|
||||
0 RT_MANIFEST "../../dist/yuzu.manifest"
|
Loading…
Reference in New Issue