Skip to content

Commit

Permalink
fix compiler errors and get TLS swapping working
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeseese committed Mar 4, 2022
1 parent 8d6d08a commit 71c08f7
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 35 deletions.
12 changes: 6 additions & 6 deletions Source/SocketIOClient/Private/SocketIOClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class FSocketIOClientModule : public ISocketIOClientModule
{
public:
//virtual TSharedPtr<FSocketIONative> NewValidNativePointer() override;
virtual TSharedPtr<FSocketIONative> NewValidNativePointer() override;
virtual TSharedPtr<FSocketIONative> ValidSharedNativePointer(FString SharedId) override;
virtual TSharedPtr<FSocketIONative> NewValidNativePointer(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification) override;
virtual TSharedPtr<FSocketIONative> ValidSharedNativePointer(FString SharedId, const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification) override;
void ReleaseNativePointer(TSharedPtr<FSocketIONative> PointerToRelease) override;

/** IModuleInterface implementation */
Expand Down Expand Up @@ -79,9 +79,9 @@ void FSocketIOClientModule::ShutdownModule()
PluginNativePointers.Empty();
}

TSharedPtr<FSocketIONative> FSocketIOClientModule::NewValidNativePointer()
TSharedPtr<FSocketIONative> FSocketIOClientModule::NewValidNativePointer(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification)
{
TSharedPtr<FSocketIONative> NewPointer = MakeShareable(new FSocketIONative);
TSharedPtr<FSocketIONative> NewPointer = MakeShareable(new FSocketIONative(bShouldUseTlsLibraries, bShouldSkipCertificateVerification));

PluginNativePointers.Add(NewPointer);

Expand All @@ -90,7 +90,7 @@ TSharedPtr<FSocketIONative> FSocketIOClientModule::NewValidNativePointer()
return NewPointer;
}

TSharedPtr<FSocketIONative> FSocketIOClientModule::ValidSharedNativePointer(FString SharedId)
TSharedPtr<FSocketIONative> FSocketIOClientModule::ValidSharedNativePointer(FString SharedId, const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification)
{
//Found key? return it
if (SharedNativePointers.Contains(SharedId))
Expand All @@ -100,7 +100,7 @@ TSharedPtr<FSocketIONative> FSocketIOClientModule::ValidSharedNativePointer(FStr
//Otherwise request a new id and return it
else
{
TSharedPtr<FSocketIONative> NewNativePtr = NewValidNativePointer();
TSharedPtr<FSocketIONative> NewNativePtr = NewValidNativePointer(bShouldUseTlsLibraries, bShouldSkipCertificateVerification);
SharedNativePointers.Add(SharedId, NewNativePtr);
AllSharedPtrs.Add(NewNativePtr);
return NewNativePtr;
Expand Down
6 changes: 4 additions & 2 deletions Source/SocketIOClient/Private/SocketIOClientComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

USocketIOClientComponent::USocketIOClientComponent(const FObjectInitializer &init) : UActorComponent(init)
{
bShouldUseTlsLibraries = false;
bShouldSkipCertificateVerification = false;
bShouldAutoConnect = true;
bWantsInitializeComponent = true;
bAutoActivate = true;
Expand Down Expand Up @@ -63,14 +65,14 @@ void USocketIOClientComponent::InitializeNative()
{
if (bPluginScopedConnection)
{
NativeClient = ISocketIOClientModule::Get().ValidSharedNativePointer(PluginScopedId);
NativeClient = ISocketIOClientModule::Get().ValidSharedNativePointer(PluginScopedId, bShouldUseTlsLibraries, bShouldSkipCertificateVerification);

//Enforcement: This is the default FSocketIONative option value, but this component depends on it being true.
NativeClient->bCallbackOnGameThread = true;
}
else
{
NativeClient = ISocketIOClientModule::Get().NewValidNativePointer();
NativeClient = ISocketIOClientModule::Get().NewValidNativePointer(bShouldUseTlsLibraries, bShouldSkipCertificateVerification);
}

SetupCallbacks();
Expand Down
4 changes: 2 additions & 2 deletions Source/SocketIOClient/Private/SocketIONative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "sio_message.h"
#include "sio_socket.h"

FSocketIONative::FSocketIONative()
FSocketIONative::FSocketIONative(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification)
{
PrivateClient = nullptr;
AddressAndPort = TEXT("http://localhost:3000"); //default to 127.0.0.1
Expand All @@ -20,7 +20,7 @@ FSocketIONative::FSocketIONative()
ReconnectionDelay = 5000;
bCallbackOnGameThread = true;

PrivateClient = MakeShareable(new sio::client);
PrivateClient = MakeShareable(new sio::client(bShouldUseTlsLibraries, bShouldSkipCertificateVerification));

ClearCallbacks();
}
Expand Down
4 changes: 2 additions & 2 deletions Source/SocketIOClient/Public/SocketIOClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class SOCKETIOCLIENT_API ISocketIOClientModule : public IModuleInterface
/**
* Request a new plugin scoped pointer as a shared ptr.
*/
virtual TSharedPtr<FSocketIONative> NewValidNativePointer() { return nullptr; };
virtual TSharedPtr<FSocketIONative> NewValidNativePointer(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification) { return nullptr; };

/**
* Request a shared FSocketIONative instance for a given id. May allocate a new pointer.
*/
virtual TSharedPtr<FSocketIONative> ValidSharedNativePointer(FString SharedId) { return nullptr; };
virtual TSharedPtr<FSocketIONative> ValidSharedNativePointer(FString SharedId, const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification) { return nullptr; };

/**
* Releases the given plugin scoped pointer using a background thread
Expand Down
20 changes: 19 additions & 1 deletion Source/SocketIOClient/Public/SocketIOClientComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,28 @@ class SOCKETIOCLIENT_API USocketIOClientComponent : public UActorComponent
FSIOCEventSignature OnFail;


/** Default connection address string in form e.g. http://localhost:80. */
/**
* Default connection address string in form e.g. http://localhost:80.
* If HTTPS/WSS is provided and TLS/SSL libraries aren't compiled, HTTP/WS
* will be used.
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SocketIO Connection Properties")
FString AddressAndPort;

/**
* Whether or not to use the TLS/SSL libraries for the connection.
* Ignored if TLS/SSL libraries are not compiled in (SIO_TLS isn't defined)
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SocketIO Connection Properties")
bool bShouldUseTlsLibraries;

/**
* If `Should Use TLS Libraries` is set to true, setting this to true
* will not the authenticity of the SSL certificate (i.e. asio::ssl::verify_none)
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SocketIO Connection Properties")
bool bShouldSkipCertificateVerification;

/** If true will auto-connect on begin play to address specified in AddressAndPort. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SocketIO Connection Properties")
bool bShouldAutoConnect;
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketIOClient/Public/SocketIONative.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class SOCKETIOCLIENT_API FSocketIONative
/** If true, all callbacks and events will occur on game thread. Default true. */
bool bCallbackOnGameThread;

FSocketIONative();
FSocketIONative(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification);

/**
* Connect to a socket.io server, optional method if auto-connect is set to true.
Expand Down
24 changes: 17 additions & 7 deletions Source/SocketIOLib/Private/internal/sio_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ namespace sio
{
/*************************public:*************************/
template<typename client_type>
client_impl<client_type>::client_impl(const string& uri) :
m_base_url(uri),
client_impl<client_type>::client_impl() :
m_ping_interval(0),
m_ping_timeout(0),
m_network_thread(),
Expand Down Expand Up @@ -674,7 +673,7 @@ namespace sio

#if SIO_TLS
typedef websocketpp::lib::shared_ptr<asio::ssl::context> context_ptr;
static context_ptr on_tls_init(connection_hdl conn)
static context_ptr on_tls_init(int verify_mode, connection_hdl conn)
{
context_ptr ctx = context_ptr(new asio::ssl::context(asio::ssl::context::tlsv12));
asio::error_code ec;
Expand All @@ -686,15 +685,24 @@ namespace sio
cerr << "Init tls failed,reason:" << ec.message() << endl;
}

ctx->set_verify_mode(asio::ssl::verify_none);
if (verify_mode >= 0)
{
ctx->set_verify_mode(verify_mode);
}

return ctx;
}

template<typename client_type>
void client_impl<client_type>::set_verify_mode(int mode)
{
verify_mode = mode;
}

template<>
void client_impl<client_type_tls>::template_init()
{
m_client.set_tls_init_handler(&on_tls_init);
m_client.set_tls_init_handler(std::bind(&on_tls_init, verify_mode, std::placeholders::_1));
}
#endif

Expand All @@ -705,12 +713,14 @@ namespace sio
{
return false;
}
#if SIO_TLS
else if (uo.get_scheme() == "https" || uo.get_scheme() == "wss")
{
#if SIO_TLS
return true;
}
#else
return false;
#endif
}
else
{
throw std::runtime_error("unsupported URI scheme");
Expand Down
23 changes: 16 additions & 7 deletions Source/SocketIOLib/Private/internal/sio_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
typedef websocketpp::client<client_config_tls> client_type_tls;
#endif

struct client_impl_base {
class client_impl_base {

public:
enum con_state
Expand All @@ -97,6 +97,7 @@
};

client_impl_base() {}
virtual void template_init() {};

virtual ~client_impl_base() {}

Expand Down Expand Up @@ -134,15 +135,15 @@
virtual void set_logs_quiet() {};
virtual void set_logs_verbose() {};

virtual void set_logs_default() = 0;
virtual void set_logs_quiet() = 0;
virtual void set_logs_verbose() = 0;

virtual std::string const& get_current_url() const = 0;

// used for selecting whether or not to use TLS
static bool is_tls(const std::string& uri);

#if SIO_TLS
virtual void set_verify_mode(int mode) {};
#endif

protected:
// Wrap protected member functions of sio::socket because only client_impl_base is friended.
sio::socket* new_socket(std::string const&);
Expand All @@ -158,8 +159,8 @@
public:
typedef typename client_type::message_ptr message_ptr;

client_impl(const std::string& uri = std::string());
void template_init(); // template-specific initialization
client_impl();
void template_init() override; // template-specific initialization

~client_impl();

Expand Down Expand Up @@ -192,6 +193,10 @@

void set_logs_verbose();

#if SIO_TLS
void set_verify_mode(int mode) override;
#endif

public:
void send(packet& p);

Expand Down Expand Up @@ -295,6 +300,10 @@
//passthrough path of plugin
std::string m_path;

#if SIO_TLS
int verify_mode = -1;
#endif

friend class sio::client;
friend class sio::socket;
};
Expand Down
25 changes: 19 additions & 6 deletions Source/SocketIOLib/Private/sio_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,31 @@ namespace sio
{
}

client::client(const std::string& uri)
client::client(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification)
{
if (!client_impl_base::is_tls(uri))
if (bShouldUseTlsLibraries)
{
m_impl = new client_impl<client_type_no_tls>(uri);
}
#if SIO_TLS
m_impl = new client_impl<client_type_tls>();

if (bShouldSkipCertificateVerification)
{
m_impl->set_verify_mode(asio::ssl::verify_none);
}
else
{
m_impl->set_verify_mode(asio::ssl::verify_peer);
// TODO: add verify CA chain file
}
m_impl->template_init(); // reinitialize based on the new mode
#else
m_impl = new client_impl<client_type_no_tls>();
#endif
}
else
{
m_impl = new client_impl<client_type_tls>(uri);
m_impl = new client_impl<client_type_no_tls>();
}
#endif
}

client::~client()
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketIOLib/Public/sio_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace sio

client();

client(const std::string& uri);
client(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification);

~client();

Expand Down

0 comments on commit 71c08f7

Please sign in to comment.