Skip to content

Commit

Permalink
feat: OTCv8 proxy system #978
Browse files Browse the repository at this point in the history
OTCv8 Proxy System copied from https://github.com/OTCv8/otcv8-dev
  • Loading branch information
gesior authored Nov 29, 2024
1 parent bb2c984 commit 35886cb
Show file tree
Hide file tree
Showing 15 changed files with 1,252 additions and 6 deletions.
Binary file added data/images/topbuttons/debug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions modules/client/client.otmod
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Module
- client_options
- client_entergame
- client_terminal
- client_debug_info
- client_serverlist
59 changes: 59 additions & 0 deletions modules/client_debug_info/debug_info.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local debugInfoWindow = nil
local debugInfoButton = nil

local updateEvent = nil

function init()
debugInfoButton = modules.client_topmenu.addTopRightToggleButton('debugInfoButton', tr('Debug Info'),
'/images/topbuttons/debug', toggle)
debugInfoButton:setOn(false)

debugInfoWindow = g_ui.displayUI('debug_info')
debugInfoWindow:hide()

g_keyboard.bindKeyDown('Ctrl+Alt+D', toggle)

updateEvent = scheduleEvent(update, 2000)
end

function terminate()
debugInfoWindow:destroy()
debugInfoButton:destroy()

g_keyboard.unbindKeyDown('Ctrl+Alt+D')

removeEvent(updateEvent)
end

function onClose()
debugInfoButton:setOn(false)
end

function toggle()
if debugInfoButton:isOn() then
debugInfoWindow:hide()
debugInfoButton:setOn(false)
else
debugInfoWindow:show()
debugInfoWindow:raise()
debugInfoWindow:focus()
debugInfoButton:setOn(true)
end
end

function update()
updateEvent = scheduleEvent(update, 20)

if not debugInfoWindow:isVisible() then
return
end

if g_proxy then
local text = ""
local proxiesDebug = g_proxy.getProxiesDebugInfo()
for proxy_name, proxy_debug in pairs(proxiesDebug) do
text = text .. proxy_name .. " - " .. proxy_debug .. "\n"
end
debugInfoWindow.debugPanel.proxies:setText(text)
end
end
9 changes: 9 additions & 0 deletions modules/client_debug_info/debug_info.otmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Module
name: client_debug_info
description: Showing and sending debug/stats informations
author: [email protected]
sandboxed: true
scripts: [ debug_info ]
dependencies: [ client_topmenu ]
@onLoad: init()
@onUnload: terminate()
65 changes: 65 additions & 0 deletions modules/client_debug_info/debug_info.otui
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
DebugText < Label
font: terminus-10px
text-wrap: false
text-auto-resize: true
text-align: topleft
anchors.right: parent.right
anchors.left: parent.left
anchors.top: prev.bottom

DebugLabel < Label
text-wrap: false
text-auto-resize: false
text-align: center
anchors.right: parent.right
anchors.left: parent.left
anchors.top: prev.bottom

MainWindow
id: debugWindow
size: 600 150
!text: tr('Debug Info')
@onClose: modules.client_debug_info.onMiniWindowClose()
&save: false
margin: 0 0 0 0
padding: 25 3 3 3
opacity: 0.9
$mobile:
size: 600 150
@onEnter: modules.client_debug_info.toggle()
@onEscape: modules.client_debug_info.toggle()


ScrollablePanel
id: debugPanel
anchors.fill: parent
margin-bottom: 5
margin: 5 5 5 5
padding-left: 5
vertical-scrollbar: debugScroll

DebugLabel
!text: tr('Proxies')
anchors.top: parent.top

DebugText
id: proxies
text: -

VerticalScrollBar
id: debugScroll
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.right: parent.right
step: 48
pixels-scroll: true

ResizeBorder
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right

ResizeBorder
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ set(SOURCE_FILES
framework/stdext/qrcodegen.cpp
framework/util/color.cpp
framework/util/crypt.cpp
framework/proxy/proxy.cpp
framework/proxy/proxy_client.cpp

client/animatedtext.cpp
client/animator.cpp
Expand Down
9 changes: 8 additions & 1 deletion src/framework/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <framework/luaengine/luainterface.h>
#include <framework/platform/crashhandler.h>
#include <framework/platform/platform.h>
#include <framework/proxy/proxy.h>
#include <framework/graphics/drawpoolmanager.h>
#include "asyncdispatcher.h"

Expand Down Expand Up @@ -95,6 +96,9 @@ void Application::init(std::vector<std::string>& args, ApplicationContext* conte
// initialize lua
g_lua.init();
registerLuaFunctions();

// initalize proxy
g_proxy.init();
}

void Application::deinit()
Expand Down Expand Up @@ -134,6 +138,9 @@ void Application::terminate()
// terminate script environment
g_lua.terminate();

// terminate proxy
g_proxy.terminate();

m_terminated = true;

signal(SIGTERM, SIG_DFL);
Expand Down Expand Up @@ -200,4 +207,4 @@ std::string Application::getBuildRevision()
return ss.str();
}
std::string Application::getVersion() { return ADD_QUOTES(GIT_VERSION); }
std::string Application::getBuildCommit() { return ADD_QUOTES(GIT_BRANCH); }
std::string Application::getBuildCommit() { return ADD_QUOTES(GIT_BRANCH); }
13 changes: 12 additions & 1 deletion src/framework/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <framework/core/resourcemanager.h>
#include <framework/luaengine/luainterface.h>
#include <framework/platform/platform.h>
#include <framework/proxy/proxy.h>
#include <framework/stdext/net.h>
#include <framework/util/crypt.h>

Expand Down Expand Up @@ -255,6 +256,16 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_resources", "createArchive", &ResourceManager::createArchive, &g_resources);
g_lua.bindSingletonFunction("g_resources", "decompressArchive", &ResourceManager::decompressArchive, &g_resources);

// OTCv8 proxy system
g_lua.registerSingletonClass("g_proxy");
g_lua.bindSingletonFunction("g_proxy", "addProxy", &ProxyManager::addProxy, &g_proxy);
g_lua.bindSingletonFunction("g_proxy", "removeProxy", &ProxyManager::removeProxy, &g_proxy);
g_lua.bindSingletonFunction("g_proxy", "clear", &ProxyManager::clear, &g_proxy);
g_lua.bindSingletonFunction("g_proxy", "setMaxActiveProxies", &ProxyManager::setMaxActiveProxies, &g_proxy);
g_lua.bindSingletonFunction("g_proxy", "getProxies", &ProxyManager::getProxies, &g_proxy);
g_lua.bindSingletonFunction("g_proxy", "getProxiesDebugInfo", &ProxyManager::getProxiesDebugInfo, &g_proxy);
g_lua.bindSingletonFunction("g_proxy", "getPing", &ProxyManager::getPing, &g_proxy);

// Config
g_lua.registerClass<Config>();
g_lua.bindClassMemberFunction<Config>("save", &Config::save);
Expand Down Expand Up @@ -1015,4 +1026,4 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<SoundChannel>("isEnabled", &SoundChannel::isEnabled);
g_lua.bindClassMemberFunction<SoundChannel>("getId", &SoundChannel::getId);
#endif
}
}
81 changes: 80 additions & 1 deletion src/framework/net/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <framework/core/application.h>
#include "connection.h"

extern asio::io_service g_ioService;

Protocol::Protocol() :m_inputMessage(std::make_shared<InputMessage>()) {
inflateInit2(&m_zstream, -15);
}
Expand All @@ -40,19 +42,49 @@ Protocol::~Protocol()

void Protocol::connect(const std::string_view host, uint16_t port)
{
if (host == "proxy" || host == "0.0.0.0" || (host == "127.0.0.1" && g_proxy.isActive())) {
m_disconnected = false;
m_proxy = g_proxy.addSession(port,
std::bind(&Protocol::onProxyPacket, asProtocol(), std::placeholders::_1),
std::bind(&Protocol::onLocalDisconnected, asProtocol(), std::placeholders::_1));
return onConnect();
}

m_connection = std::make_shared<Connection>();
m_connection->setErrorCallback([capture0 = asProtocol()](auto&& PH1) { capture0->onError(std::forward<decltype(PH1)>(PH1)); });
m_connection->connect(host, port, [capture0 = asProtocol()] { capture0->onConnect(); });
}

void Protocol::disconnect()
{
m_disconnected = true;
if (m_proxy) {
g_proxy.removeSession(m_proxy);
return;
}

if (m_connection) {
m_connection->close();
m_connection.reset();
}
}

bool Protocol::isConnected()
{
if (m_proxy)
return !m_disconnected;

return m_connection && m_connection->isConnected();
}

bool Protocol::isConnecting()
{
if (m_proxy)
return false;

return m_connection && m_connection->isConnecting();
}

void Protocol::send(const OutputMessagePtr& outputMessage)
{
// encrypt
Expand All @@ -68,6 +100,13 @@ void Protocol::send(const OutputMessagePtr& outputMessage)
// write message size
outputMessage->writeMessageSize();

if (m_proxy) {
auto packet = std::make_shared<ProxyPacket>(outputMessage->getHeaderBuffer(), outputMessage->getWriteBuffer());
g_proxy.send(m_proxy, packet);
outputMessage->reset();
return;
}

// send
if (m_connection)
m_connection->write(outputMessage->getHeaderBuffer(), outputMessage->getMessageSize());
Expand All @@ -78,6 +117,10 @@ void Protocol::send(const OutputMessagePtr& outputMessage)

void Protocol::recv()
{
if (m_proxy) {
return;
}

m_inputMessage->reset();

// first update message header size
Expand Down Expand Up @@ -252,4 +295,40 @@ void Protocol::onError(const std::error_code& err)
{
callLuaField("onError", err.message(), err.value());
disconnect();
}
}

void Protocol::onProxyPacket(const std::shared_ptr<std::vector<uint8_t>>& packet)
{
if (m_disconnected)
return;
auto self(asProtocol());
asio::post(g_ioService, [&, self, packet] {
if (m_disconnected)
return;
m_inputMessage->reset();

// first update message header size
int headerSize = 2; // 2 bytes for message size
if (m_checksumEnabled)
headerSize += 4; // 4 bytes for checksum
if (m_xteaEncryptionEnabled)
headerSize += 2; // 2 bytes for XTEA encrypted message size
m_inputMessage->setHeaderSize(headerSize);
m_inputMessage->fillBuffer(packet->data(), 2);
m_inputMessage->readSize();
internalRecvData(packet->data() + 2, packet->size() - 2);
});
}

void Protocol::onLocalDisconnected(std::error_code ec)
{
if (m_disconnected)
return;
auto self(asProtocol());
asio::post(g_ioService, [&, self, ec] {
if (m_disconnected)
return;
m_disconnected = true;
onError(ec);
});
}
10 changes: 8 additions & 2 deletions src/framework/net/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "outputmessage.h"

#include <framework/luaengine/luaobject.h>
#include <framework/proxy/proxy.h>
#include <zlib.h>

// @bindclass
Expand All @@ -40,8 +41,8 @@ class Protocol : public LuaObject
void connect(const std::string_view host, uint16_t port);
void disconnect();

bool isConnected() { return m_connection && m_connection->isConnected(); }
bool isConnecting() { return m_connection && m_connection->isConnecting(); }
bool isConnected();
bool isConnecting();
ticks_t getElapsedTicksSinceLastRead() const { return m_connection ? m_connection->getElapsedTicksSinceLastRead() : -1; }

ConnectionPtr getConnection() { return m_connection; }
Expand All @@ -65,6 +66,11 @@ class Protocol : public LuaObject
virtual void onRecv(const InputMessagePtr& inputMessage);
virtual void onError(const std::error_code& err);

void onProxyPacket(const std::shared_ptr<std::vector<uint8_t>>& packet);
void onLocalDisconnected(std::error_code ec);
bool m_disconnected = false;
uint32_t m_proxy = 0;

std::array<uint32_t, 4> m_xteaKey{};
uint32_t m_packetNumber{ 0 };

Expand Down
Loading

0 comments on commit 35886cb

Please sign in to comment.