Skip to content

Commit

Permalink
Refactor TLS to allow URL based hotswapping of lib
Browse files Browse the repository at this point in the history
- untested early commit
  • Loading branch information
getnamo committed Apr 19, 2022
1 parent ecfac5e commit ae78d70
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 26 deletions.
2 changes: 1 addition & 1 deletion SocketIOClient.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "2.1.2",
"VersionName": "2.1.3",
"EngineVersion" : "5.0.0",
"FriendlyName": "Socket.IO Client",
"Description": "Real-time networking library Socket.IO Client usable from blueprints and c++.",
Expand Down
8 changes: 4 additions & 4 deletions Source/SocketIOClient/Private/SocketIOClientComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ USocketIOClientComponent::USocketIOClientComponent(const FObjectInitializer &ini
bAutoActivate = true;

bShouldUseTLS = false;
bShouldSkipCertificateVerification = true; //until verification is implemented, this should default to true
bShouldVerifyTLSCertificate = false; //Until verification feature is implemented, this should default to false
bShouldAutoConnect = true;
NativeClient = nullptr;
bLimitConnectionToGameWorld = true;
Expand Down Expand Up @@ -66,14 +66,14 @@ void USocketIOClientComponent::InitializeNative()
{
if (bPluginScopedConnection)
{
NativeClient = ISocketIOClientModule::Get().ValidSharedNativePointer(PluginScopedId, bShouldUseTLS, bShouldSkipCertificateVerification);
NativeClient = ISocketIOClientModule::Get().ValidSharedNativePointer(PluginScopedId, bShouldUseTLS, bShouldVerifyTLSCertificate);

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

SetupCallbacks();
Expand Down Expand Up @@ -209,7 +209,7 @@ void USocketIOClientComponent::ClearCallbacks()
{
if (NativeClient.IsValid())
{
NativeClient->ClearCallbacks();
NativeClient->ClearAllCallbacks();
}
}

Expand Down
50 changes: 45 additions & 5 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(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification)
FSocketIONative::FSocketIONative(const bool bShouldUseTlsLibraries, const bool bShouldVerifyTLSCertificate)
{
PrivateClient = nullptr;
AddressAndPort = TEXT("http://localhost:3000"); //default to 127.0.0.1
Expand All @@ -20,14 +20,44 @@ FSocketIONative::FSocketIONative(const bool bShouldUseTlsLibraries, const bool b
ReconnectionDelay = 5000;
bCallbackOnGameThread = true;
bUnbindEventsOnDisconnect = false;

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

ClearAllCallbacks();
}


void FSocketIONative::InitPrivateClient(const bool bShouldUseTlsLibraries /*= false*/, const bool bShouldVerifyTLSCertificate /*= false*/)
{
bIsSetupForTLS = bShouldUseTlsLibraries;
bUsingTLSCertVerification = bShouldVerifyTLSCertificate;
PrivateClient = MakeShareable(new sio::client(bShouldUseTlsLibraries, bUsingTLSCertVerification));
}

void FSocketIONative::Connect(const FString& InAddressAndPort, const TSharedPtr<FJsonObject>& Query /*= nullptr*/, const TSharedPtr<FJsonObject>& Headers /*= nullptr*/, const FString& Path)
{
//check tls mode
if (IsTLSURL(InAddressAndPort))
{
//needs to swap to TLS
if (!bIsSetupForTLS)
{
ClearInternalCallbacks();
InitPrivateClient(true, bUsingTLSCertVerification);
SetupInternalCallbacks();
}
}
else
{
//Url not TLS, but we are setup for it
if (bIsSetupForTLS)
{
ClearInternalCallbacks();
InitPrivateClient(false, bUsingTLSCertVerification);
SetupInternalCallbacks();
}
}


std::string StdAddressString = USIOMessageConvert::StdString(InAddressAndPort);
if (InAddressAndPort.IsEmpty())
{
Expand Down Expand Up @@ -109,7 +139,7 @@ void FSocketIONative::Disconnect()
else
{
//only clear internal ones during close
PrivateClient->clear_socket_listeners();
ClearInternalCallbacks();
PrivateClient->close();
SetupInternalCallbacks();
}
Expand All @@ -130,7 +160,7 @@ void FSocketIONative::SyncDisconnect()
}
else
{
PrivateClient->clear_socket_listeners();
ClearInternalCallbacks();
PrivateClient->sync_close();
SetupInternalCallbacks();
}
Expand Down Expand Up @@ -383,6 +413,11 @@ void FSocketIONative::UnbindEvent(const FString& EventName, const FString& Names
EventFunctionMap.Remove(EventName);
}

void FSocketIONative::ClearInternalCallbacks()
{
PrivateClient->clear_socket_listeners();
}

void FSocketIONative::SetupInternalCallbacks()
{
PrivateClient->set_open_listener(sio::client::con_listener([&]()
Expand Down Expand Up @@ -568,4 +603,9 @@ void FSocketIONative::SetupInternalCallbacks()
}));
}

bool FSocketIONative::IsTLSURL(const FString& URL)
{
return URL.StartsWith(TEXT("https://"));
}


26 changes: 13 additions & 13 deletions Source/SocketIOClient/Public/SocketIOClientComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ class SOCKETIOCLIENT_API USocketIOClientComponent : public UActorComponent


/**
* 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.
*/
* 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)
*/
* 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 bShouldUseTLS;

/**
* If `Should Use TLS` is set to true, setting this to true
* will not verify the authenticity of the SSL certificate (i.e. asio::ssl::verify_none).
* NOTE: Certification verification is currently not implemented; setting to false will
* always fail verification.
*/
* If `Should Use TLS` is set to true, setting this to false
* will not verify the authenticity of the SSL certificate (i.e. asio::ssl::verify_none).
* NOTE: Certification verification is currently not implemented; setting to true will
* always fail verification.
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SocketIO Connection Properties")
bool bShouldSkipCertificateVerification;
bool bShouldVerifyTLSCertificate;

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

/** Set true if connection currently configured for TLS */
bool bIsSetupForTLS;

/** If true will attempt to verify certificate (NB: this currently doesn't work) */
bool bUsingTLSCertVerification;

/** If true all events are unbound on disconnect */
bool bUnbindEventsOnDisconnect;

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

/**
* Connect to a socket.io server, optional method if auto-connect is set to true.
Expand Down Expand Up @@ -374,7 +379,12 @@ class SOCKETIOCLIENT_API FSocketIONative
void UnbindEvent(const FString& EventName, const FString& Namespace = TEXT("/"));

protected:

void ClearInternalCallbacks();
void SetupInternalCallbacks();
bool IsTLSURL(const FString& URL);

void InitPrivateClient(const bool bShouldUseTlsLibraries = false, const bool bShouldVerifyTLSCertificate = false);

TSharedPtr<sio::client> PrivateClient;
};
4 changes: 2 additions & 2 deletions Source/SocketIOLib/Private/sio_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ namespace sio
{
}

client::client(const bool bShouldUseTlsLibraries, const bool bShouldSkipCertificateVerification)
client::client(const bool bShouldUseTlsLibraries, const bool bShouldVerifyCertificate)
{
if (bShouldUseTlsLibraries)
{
#if SIO_TLS
m_impl = new client_impl<client_type_tls>();

if (bShouldSkipCertificateVerification)
if (!bShouldVerifyCertificate)
{
m_impl->set_verify_mode(asio::ssl::verify_none);
}
Expand Down

0 comments on commit ae78d70

Please sign in to comment.