Compare commits
3 Commits
9fa023f716
...
ssl
| Author | SHA1 | Date | |
|---|---|---|---|
| b482420d3a | |||
| 61d0ce3a47 | |||
| 5beb5e92a7 |
31
.github/workflows/ubuntu.yml
vendored
Normal file
31
.github/workflows/ubuntu.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name: Linux arm64
|
||||||
|
run-name: Build And Test
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
|
||||||
|
env:
|
||||||
|
XMAKE_ROOT: y
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out repository code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare XMake
|
||||||
|
uses: xmake-io/github-action-setup-xmake@v1
|
||||||
|
with:
|
||||||
|
xmake-version: latest
|
||||||
|
actions-cache-folder: '.xmake-cache'
|
||||||
|
actions-cache-key: 'ubuntu'
|
||||||
|
|
||||||
|
- name: XMake config
|
||||||
|
run: xmake f -p linux -y
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: xmake
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: xmake test
|
||||||
@@ -166,7 +166,7 @@ class DataBuffer {
|
|||||||
K newKey;
|
K newKey;
|
||||||
V newValue;
|
V newValue;
|
||||||
*this >> newKey >> newValue;
|
*this >> newKey >> newValue;
|
||||||
data.insert({newKey, newValue});
|
data.emplace(newKey, newValue);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ class VarInt {
|
|||||||
std::uint64_t m_Value;
|
std::uint64_t m_Value;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static const std::uint64_t MAX_VALUE = static_cast<std::uint64_t>(-1) >> 8;
|
||||||
|
|
||||||
VarInt() : m_Value(0) {}
|
VarInt() : m_Value(0) {}
|
||||||
/**
|
/**
|
||||||
* \brief Construct a variable integer from a value
|
* \brief Construct a variable integer from a value
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ template <>
|
|||||||
class MessageEncapsulator<option::ZlibCompress> {
|
class MessageEncapsulator<option::ZlibCompress> {
|
||||||
public:
|
public:
|
||||||
static DataBuffer Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option);
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option);
|
||||||
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length, const option::ZlibCompress& a_Option);
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const option::ZlibCompress& a_Option);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace io
|
} // namespace io
|
||||||
|
|||||||
41
include/sp/extensions/Encrypt.h
Normal file
41
include/sp/extensions/Encrypt.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file Encrypt.h
|
||||||
|
* \brief File containing encrypt utilities
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sp/common/DataBuffer.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace option {
|
||||||
|
|
||||||
|
struct TlsEncrypt {
|
||||||
|
bool m_Enabled = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace option
|
||||||
|
} // namespace sp
|
||||||
|
|
||||||
|
#include <sp/io/IOInterface.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace ssl {
|
||||||
|
|
||||||
|
// encrypt
|
||||||
|
|
||||||
|
// decrypt
|
||||||
|
|
||||||
|
} // namespace ssl
|
||||||
|
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class MessageEncapsulator<option::TlsEncrypt> {
|
||||||
|
public:
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const option::TlsEncrypt& a_Option);
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const option::TlsEncrypt& a_Option);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
@@ -17,7 +17,7 @@ template <typename TOption>
|
|||||||
class MessageEncapsulator {
|
class MessageEncapsulator {
|
||||||
public:
|
public:
|
||||||
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOption& a_Option);
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOption& a_Option);
|
||||||
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length, const TOption& a_Option);
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const TOption& a_Option);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
@@ -25,16 +25,23 @@ class Stream {
|
|||||||
protected:
|
protected:
|
||||||
MessageDispatcher m_Dispatcher;
|
MessageDispatcher m_Dispatcher;
|
||||||
IOInterface<IOTag> m_Interface;
|
IOInterface<IOTag> m_Interface;
|
||||||
|
std::tuple<TOptions...> m_Options;
|
||||||
|
|
||||||
using MessageBase = typename MessageDispatcher::MessageBaseType;
|
using MessageBase = typename MessageDispatcher::MessageBaseType;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Stream() {}
|
Stream() {}
|
||||||
Stream(IOInterface<IOTag>&& a_Interface);
|
Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options);
|
||||||
Stream(Stream&& a_Stream);
|
Stream(Stream&& a_Stream);
|
||||||
|
|
||||||
void RecieveMessages(const TOptions&... a_Options);
|
void RecieveMessages();
|
||||||
void SendMessage(const MessageBase& a_Message, const TOptions&... a_Options);
|
void SendMessage(const MessageBase& a_Message);
|
||||||
|
|
||||||
|
|
||||||
|
template <typename TOption>
|
||||||
|
TOption& GetOption() {
|
||||||
|
return std::get<TOption>(m_Options);
|
||||||
|
}
|
||||||
|
|
||||||
MessageDispatcher& GetDispatcher() {
|
MessageDispatcher& GetDispatcher() {
|
||||||
return m_Dispatcher;
|
return m_Dispatcher;
|
||||||
@@ -42,7 +49,7 @@ class Stream {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOptions&... a_Options);
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOptions&... a_Options);
|
||||||
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length, const TOptions&... a_Options);
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const TOptions&... a_Options);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace io
|
} // namespace io
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ struct MessageEncapsulatorPack<> {
|
|||||||
static DataBuffer Encapsulate(const DataBuffer& a_Data) {
|
static DataBuffer Encapsulate(const DataBuffer& a_Data) {
|
||||||
return a_Data;
|
return a_Data;
|
||||||
}
|
}
|
||||||
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length) {
|
static DataBuffer Decapsulate(DataBuffer& a_Data) {
|
||||||
return a_Data;
|
return a_Data;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -26,9 +26,9 @@ struct MessageEncapsulatorPack<TOption, TOptions...> {
|
|||||||
DataBuffer data = io::MessageEncapsulator<TOption>::Encapsulate(a_Data, a_Option);
|
DataBuffer data = io::MessageEncapsulator<TOption>::Encapsulate(a_Data, a_Option);
|
||||||
return MessageEncapsulatorPack<TOptions...>::Encapsulate(data, a_Options...);
|
return MessageEncapsulatorPack<TOptions...>::Encapsulate(data, a_Options...);
|
||||||
}
|
}
|
||||||
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length, const TOption& a_Option, const TOptions&... a_Options) {
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const TOption& a_Option, const TOptions&... a_Options) {
|
||||||
DataBuffer data = io::MessageEncapsulator<TOption>::Decapsulate(a_Data, a_Length, a_Option);
|
DataBuffer data = io::MessageEncapsulator<TOption>::Decapsulate(a_Data, a_Option);
|
||||||
return MessageEncapsulatorPack<TOptions...>::Decapsulate(data, data.GetSize(), a_Options...);
|
return MessageEncapsulatorPack<TOptions...>::Decapsulate(data, a_Options...);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -37,8 +37,8 @@ struct MessageEncapsulatorPack<TOption, TOptions...> {
|
|||||||
namespace io {
|
namespace io {
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(IOInterface<IOTag>&& a_Interface) :
|
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options) :
|
||||||
m_Interface(std::move(a_Interface)) {}
|
m_Interface(std::move(a_Interface)), m_Options(std::make_tuple<TOptions...>(std::move(a_Options)...)) {}
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(
|
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(
|
||||||
@@ -46,17 +46,18 @@ Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(
|
|||||||
m_Dispatcher(std::move(a_Stream.m_Dispatcher)), m_Interface(std::move(a_Stream.m_Interface)) {}
|
m_Dispatcher(std::move(a_Stream.m_Dispatcher)), m_Interface(std::move(a_Stream.m_Interface)) {}
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::SendMessage(
|
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::SendMessage(const MessageBase& a_Message) {
|
||||||
const MessageBase& a_Message, const TOptions&... a_Options) {
|
|
||||||
DataBuffer data = a_Message.Write();
|
DataBuffer data = a_Message.Write();
|
||||||
DataBuffer encapsulated = Encapsulate(data, a_Options...);
|
DataBuffer encapsulated = std::apply([&data](const auto&... a_Options) {
|
||||||
|
return Encapsulate(data, a_Options...);
|
||||||
|
}, m_Options);
|
||||||
DataBuffer finalData;
|
DataBuffer finalData;
|
||||||
finalData << VarInt{encapsulated.GetSize()} << encapsulated;
|
finalData << VarInt{encapsulated.GetSize()} << encapsulated;
|
||||||
m_Interface.Write(finalData);
|
m_Interface.Write(finalData);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessages(const TOptions&... a_Options) {
|
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessages() {
|
||||||
while (true) {
|
while (true) {
|
||||||
// reading the first VarInt part byte by byte
|
// reading the first VarInt part byte by byte
|
||||||
std::uint64_t lenghtValue = 0;
|
std::uint64_t lenghtValue = 0;
|
||||||
@@ -92,7 +93,9 @@ void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessa
|
|||||||
DataBuffer buffer;
|
DataBuffer buffer;
|
||||||
buffer = m_Interface.Read(lenghtValue);
|
buffer = m_Interface.Read(lenghtValue);
|
||||||
|
|
||||||
buffer = Decapsulate(buffer, lenghtValue, a_Options...);
|
buffer = std::apply([&buffer, lenghtValue](const auto&... a_Options) {
|
||||||
|
return Decapsulate(buffer, a_Options...);
|
||||||
|
}, m_Options);
|
||||||
|
|
||||||
VarInt packetType;
|
VarInt packetType;
|
||||||
buffer >> packetType;
|
buffer >> packetType;
|
||||||
@@ -117,8 +120,8 @@ DataBuffer Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Encaps
|
|||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
DataBuffer Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Decapsulate(
|
DataBuffer Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Decapsulate(
|
||||||
DataBuffer& a_Data, std::size_t a_Length, const TOptions&... a_Options) {
|
DataBuffer& a_Data, const TOptions&... a_Options) {
|
||||||
return details::MessageEncapsulatorPack<TOptions...>::Decapsulate(a_Data, a_Length, a_Options...);
|
return details::MessageEncapsulatorPack<TOptions...>::Decapsulate(a_Data, a_Options...);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace io
|
} // namespace io
|
||||||
|
|||||||
@@ -77,12 +77,12 @@ DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
|
|||||||
namespace io {
|
namespace io {
|
||||||
|
|
||||||
DataBuffer MessageEncapsulator<option::ZlibCompress>::Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option) {
|
DataBuffer MessageEncapsulator<option::ZlibCompress>::Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option) {
|
||||||
return a_Option.m_Enabled ? zlib::Compress(a_Data, a_Option.m_CompressionThreshold) : a_Data;
|
static constexpr std::size_t MAX_COMPRESS_THRESHOLD = VarInt::MAX_VALUE;
|
||||||
|
return zlib::Compress(a_Data, a_Option.m_Enabled ? a_Option.m_CompressionThreshold : MAX_COMPRESS_THRESHOLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBuffer MessageEncapsulator<option::ZlibCompress>::Decapsulate(
|
DataBuffer MessageEncapsulator<option::ZlibCompress>::Decapsulate(DataBuffer& a_Data, const option::ZlibCompress& a_Option) {
|
||||||
DataBuffer& a_Data, std::size_t a_Length, const option::ZlibCompress& a_Option) {
|
return zlib::Decompress(a_Data, a_Data.GetSize());
|
||||||
return a_Option.m_Enabled ? zlib::Decompress(a_Data, a_Length) : a_Data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace io
|
} // namespace io
|
||||||
|
|||||||
3
src/sp/extensions/Encrypt.cpp
Normal file
3
src/sp/extensions/Encrypt.cpp
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#include <sp/extensions/Encrypt.h>
|
||||||
|
|
||||||
|
#include <openssl/ssl.h>
|
||||||
@@ -22,21 +22,21 @@ using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::Pac
|
|||||||
int main() {
|
int main() {
|
||||||
auto handler = std::make_shared<CustomPacketHandler>();
|
auto handler = std::make_shared<CustomPacketHandler>();
|
||||||
|
|
||||||
FileStream stream(sp::io::File{"test.txt", sp::io::FileTag::In | sp::io::FileTag::Out});
|
FileStream stream(sp::io::File{"test.txt", sp::io::FileTag::In | sp::io::FileTag::Out}, {});
|
||||||
stream.GetDispatcher().RegisterHandler(PacketId::Disconnect, handler);
|
stream.GetDispatcher().RegisterHandler(PacketId::Disconnect, handler);
|
||||||
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
|
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
|
||||||
|
|
||||||
auto zlibOptions = sp::option::ZlibCompress{};
|
stream.SendMessage(KeepAlivePacket{96});
|
||||||
|
stream.SendMessage(KeepAlivePacket{69});
|
||||||
|
stream.SendMessage(DisconnectPacket{
|
||||||
|
"This is in the "
|
||||||
|
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
|
||||||
|
stream.GetOption<sp::option::ZlibCompress>().m_Enabled = false;
|
||||||
|
stream.SendMessage(DisconnectPacket{
|
||||||
|
"This is in the "
|
||||||
|
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
|
||||||
|
|
||||||
stream.SendMessage(KeepAlivePacket{96}, zlibOptions);
|
stream.RecieveMessages();
|
||||||
stream.SendMessage(KeepAlivePacket{69}, zlibOptions);
|
|
||||||
stream.SendMessage(
|
|
||||||
DisconnectPacket{
|
|
||||||
"This is in the "
|
|
||||||
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"},
|
|
||||||
zlibOptions);
|
|
||||||
|
|
||||||
stream.RecieveMessages(zlibOptions);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -6,9 +6,14 @@ local modules = {
|
|||||||
Compression = {
|
Compression = {
|
||||||
Option = "zlib",
|
Option = "zlib",
|
||||||
Deps = {"zlib"},
|
Deps = {"zlib"},
|
||||||
Packages = {"zlib"},
|
|
||||||
Includes = {"include/(sp/extensions/Compress.h)"},
|
Includes = {"include/(sp/extensions/Compress.h)"},
|
||||||
Sources = {"src/sp/extensions/Compress.cpp"}
|
Sources = {"src/sp/extensions/Compress.cpp"}
|
||||||
|
},
|
||||||
|
Encryption = {
|
||||||
|
Option = "ssl",
|
||||||
|
Deps = {"openssl"},
|
||||||
|
Includes = {"include/(sp/extensions/Encrypt.h)"},
|
||||||
|
Sources = {"src/sp/extensions/Encrypt.cpp"}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +42,7 @@ for name, module in table.orderpairs(modules) do
|
|||||||
for _, source in table.orderpairs(module.Sources) do
|
for _, source in table.orderpairs(module.Sources) do
|
||||||
add_files(source)
|
add_files(source)
|
||||||
end
|
end
|
||||||
for _, package in table.orderpairs(module.Packages) do
|
for _, package in table.orderpairs(module.Deps) do
|
||||||
add_packages(package)
|
add_packages(package)
|
||||||
end
|
end
|
||||||
set_group("Library")
|
set_group("Library")
|
||||||
|
|||||||
Reference in New Issue
Block a user