Skip to content

Commit

Permalink
socket: Move to version 12
Browse files Browse the repository at this point in the history
- Remove other projects include support (__TGBOT__)
- Use OpenSSL again
- Move to session-based protocol (OPEN_SESSION, CLOSE_SESSION)
- Add HMAC, AES-GCM to header
- Add partial support for JSON payloads
  • Loading branch information
Royna2544 committed Dec 16, 2024
1 parent 73474a6 commit 1ee4ff3
Show file tree
Hide file tree
Showing 30 changed files with 909 additions and 404 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@
[submodule "src/third-party/googletest"]
path = src/third-party/googletest
url = https://github.com/google/googletest/
[submodule "src/hash/third-party/sha-2"]
path = src/hash/sha-2
url = https://github.com/amosnier/sha-2/
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ endfunction()

## Compiler specific settings
set(GLOBAL_COMPILE_OPTIONS)
set(GLOBAL_DEFINITIONS __TGBOT__)
set(GLOBAL_DEFINITIONS)
set(GLOBAL_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/include ${CMAKE_SOURCE_DIR}/src/)

# Test proper c++20 jthread, stop_token
Expand Down Expand Up @@ -424,7 +424,6 @@ endif()
add_subdirectory(src/api)
add_subdirectory(src/database)
add_subdirectory(src/random)
add_subdirectory(src/hash)
add_subdirectory(src/imagep)
add_subdirectory(src/stringres)
add_subdirectory(src/logging)
Expand Down
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- Handle forked process in forkandrun when the main process exits due to network.
- Better installer on Windows.
- dependencies resolving in kernelbuilder
- optional kernelbuilder
- optional kernelbuilder
- SocketDataHandler JSON version test
2 changes: 2 additions & 0 deletions src/api/TgBotApiImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <set>
#include <string>
#include <string_view>
#include <thread>
#include <utility>

#include "tgbot/net/CurlHttpClient.h"
Expand Down Expand Up @@ -266,6 +267,7 @@ void TgBotApiImpl::startPoll() {
// Start the long poll loop.
while (!SignalHandler::isSignaled()) {
longPoll->start();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/database/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ add_my_executable(
DBImpl
Socket
DBLoading
JsonCpp::JsonCpp
RELATION Socket
)
#####################################################################
92 changes: 85 additions & 7 deletions src/database/utils/SendMediaToChat.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
#include <Types.h>
#include <absl/log/log.h>
#include <fmt/format.h>
#include <json/json.h>

#include <AbslLogInit.hpp>
#include <TryParseStr.hpp>
#include <backends/ClientBackend.hpp>
#include <bot/PacketParser.hpp>
#include <cstdlib>
#include <cstring>
#include <database/bot/TgBotDatabaseImpl.hpp>
#include <backends/ClientBackend.hpp>
#include <iostream>
#include <memory>

#include "ConfigManager.hpp"
#include "TgBotSocket_Export.hpp"

[[noreturn]] static void usage(const char* argv0, const int exitCode) {
std::cerr << "Usage: " << argv0 << " <chat(Id/Name)> <medianame>"
<< std::endl;
exit(exitCode);
}

std::optional<Json::Value> parseAndCheck(
const void* buf, TgBotSocket::Packet::Header::length_type length,
const std::initializer_list<const char*> nodes) {
Json::Value root;
Json::Reader reader;
if (!reader.parse(std::string(static_cast<const char*>(buf), length),
root)) {
LOG(WARNING) << "Failed to parse json: "
<< reader.getFormattedErrorMessages();
return std::nullopt;
}
if (!root.isObject()) {
LOG(WARNING) << "Expected an object in json";
return std::nullopt;
}
for (const auto& node : nodes) {
if (!root.isMember(node)) {
LOG(WARNING) << fmt::format("Missing node '{}' in json", node);
return std::nullopt;
}
}
return root;
}

int main(int argc, char** argv) {
ChatId chatId = 0;
TgBotSocket::data::SendFileToChatId data = {};
Expand Down Expand Up @@ -55,14 +84,63 @@ int main(int argc, char** argv) {
LOG(INFO) << "Found, sending (fileid " << info->mediaId << ") to chat "
<< chatId;
}
copyTo(data.filePath, info->mediaId.c_str());
copyTo(data.filePath, info->mediaId);
data.chat = chatId;
data.fileType = TgBotSocket::data::FileType::TYPE_DOCUMENT;

struct TgBotSocket::Packet pkt(
TgBotSocket::Command::CMD_SEND_FILE_TO_CHAT_ID, data);
SocketClientWrapper wrapper;
wrapper.connect(TgBotSocket::Context::kTgBotHostPort, TgBotSocket::Context::hostPath());
wrapper->write(pkt);
backend->unload();
if (wrapper.connect(TgBotSocket::Context::kTgBotHostPort,
TgBotSocket::Context::hostPath())) {
using namespace TgBotSocket;
DLOG(INFO) << "Connected to server";
Packet openSession = createPacket(Command::CMD_OPEN_SESSION, nullptr, 0,
PayloadType::Binary, {});
wrapper->write(openSession);
DLOG(INFO) << "Wrote open session packet";
auto openSessionAck =
TgBotSocket::readPacket(wrapper.chosen_interface());
if (!openSessionAck ||
openSessionAck->header.cmd != Command::CMD_OPEN_SESSION_ACK) {
LOG(ERROR) << "Failed to open session";
return EXIT_FAILURE;
}
auto _root = parseAndCheck(openSessionAck->data.get(),
openSessionAck->data.size(),
{"session_token", "expiration_time"});
if (!_root) {
LOG(ERROR) << "Invalid open session ack json";
return EXIT_FAILURE;
}
auto root = *_root;
LOG(INFO) << "Opened session. Token: " << root["session_token"]
<< " expiration_time: " << root["expiration_time"];

std::string session_token_str = root["session_token"].asString();
Packet::Header::session_token_type session_token{};
copyTo(session_token, session_token_str);
auto pkt =
createPacket(TgBotSocket::Command::CMD_SEND_FILE_TO_CHAT_ID, &data,
sizeof(data), PayloadType::Binary, session_token);
if (!wrapper->write(pkt)) {
LOG(ERROR) << "Failed to write send file to chat id packet";
backend->unload();
return EXIT_FAILURE;
}
auto result = TgBotSocket::readPacket(wrapper.chosen_interface());
if (!result || result->header.cmd != Command::CMD_GENERIC_ACK) {
LOG(ERROR) << "Failed to send file to chat id";
backend->unload();
return EXIT_FAILURE;
}
TgBotSocket::callback::GenericAck genericAck;
result->data.assignTo(genericAck);
if (genericAck.result != TgBotSocket::callback::AckType::SUCCESS) {
LOG(ERROR) << "Failed to send file to chat id: "
<< genericAck.error_msg.data();
backend->unload();
return EXIT_FAILURE;
}
DLOG(INFO) << "File sent successfully";
backend->unload();
}
}
5 changes: 0 additions & 5 deletions src/hash/CMakeLists.txt

This file was deleted.

45 changes: 0 additions & 45 deletions src/hash/crc32.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions src/hash/crc32.hpp

This file was deleted.

1 change: 0 additions & 1 deletion src/hash/sha-2
Submodule sha-2 deleted from 565f65
10 changes: 0 additions & 10 deletions src/hash/sha256.cpp

This file was deleted.

26 changes: 8 additions & 18 deletions src/include/SharedMalloc.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#pragma once

#include <absl/log/check.h>
#include <trivial_helpers/_class_helper_macros.h>

#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <stdexcept>
#include <type_traits>

#include "trivial_helpers/_class_helper_macros.h"

#ifndef __cpp_concepts
#define requires(x)
#endif
Expand Down Expand Up @@ -72,41 +71,30 @@ struct SharedMalloc {
parent = std::make_shared<Parent>(size);
}
}
explicit SharedMalloc(std::nullptr_t) : SharedMalloc() {}
SharedMalloc() { parent = std::make_shared<Parent>(); }

template <typename T, std::enable_if_t<std::is_class_v<T>, bool> = true>
explicit SharedMalloc(T value) {
parent = std::make_shared<Parent>(sizeof(T));
assignFrom(value);
}
explicit SharedMalloc(std::nullptr_t /*value*/) {
parent = std::make_shared<Parent>();
}

explicit operator bool() const { return parent->size() != 0; }
bool operator!=(std::nullptr_t value) { return parent.get() != value; }

template <typename T>
explicit operator T() const {
T value;
assignTo(value);
return value;
}
[[nodiscard]] size_t size() const noexcept { return parent->size(); }
void resize(size_t newSize) const noexcept { parent->realloc(newSize); }

private:
// A fortify check.
inline void validateBoundsForSize(const size_t newSize) const {
DCHECK_LE(newSize, size())
<< ": Operation size exceeds allocated memory size";
if (newSize > size()) {
throw std::out_of_range(
"Operation size exceeds allocated memory size");
}
}

inline void offsetCheck(const size_t offset) const {
DCHECK_LE(offset, size()) << ": Offset exceeds allocated memory bounds";
if (offset > size()) {
throw std::out_of_range("Offset exceeds allocated memory bounds");
}
Expand Down Expand Up @@ -194,8 +182,10 @@ struct SharedMalloc {
template <typename T>
requires(!std::is_pointer_v<T>)
void assignFrom(const T &ref) {
CHECK_LE(sizeof(T), size())
<< ": *this Must have bigger size than sizeof(T)";
if (sizeof(T) > size()) {
throw std::out_of_range(
"Operation size exceeds allocated memory size");
}
assignFrom(&ref, sizeof(T));
}

Expand Down
2 changes: 1 addition & 1 deletion src/include/global_handlers/SpamBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <concepts>
#include <cstddef>
#include <mutex>
#include <socket/include/TgBotSocket_Export.hpp>
#include <socket/TgBotSocket_Export.hpp>
#include <unordered_map>

using TgBot::Chat;
Expand Down
11 changes: 6 additions & 5 deletions src/socket/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
find_package(OpenSSL REQUIRED)
add_subdirectory(selector)
add_subdirectory(hash)

find_package(Boost 1.70 CONFIG COMPONENTS system)

add_my_library(
NAME Socket
SRCS
Expand All @@ -12,17 +15,15 @@ add_my_library(
bot/FileHelperNew.cpp
backends/ClientBackend.cpp
TgBotCommandMap.cpp
PUBLIC_INC include interface
LIBS Utils minimalhash Boost::system
PUBLIC_INC interface
LIBS Utils minimalhash Boost::system OpenSSL::Crypto
LIBS_WIN32 wsock32 Ws2_32
)

find_package(ZLIB REQUIRED)

add_my_executable(
NAME SocketCli
SRCS TgBotSocketClient.cpp
LIBS Socket
LIBS Socket JsonCpp::JsonCpp
RELATION Socket
)

Expand Down
Loading

0 comments on commit 1ee4ff3

Please sign in to comment.