Skip to content

Commit

Permalink
ssl (#23)
Browse files Browse the repository at this point in the history
* ssl

* update

* update flag

* update ssl client

* add customize ssl
  • Loading branch information
tvd12 authored Oct 15, 2023
1 parent 1d55695 commit b68412c
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
platform/apple/Podfile.lock
platform/apple/Pods
example/example
libezyfox-client.a
.idea/
Expand Down
11 changes: 11 additions & 0 deletions platform/apple/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'ezyfox-client' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!

# Pods for ezyfox-client
pod 'OpenSSL-Universal'

end
60 changes: 60 additions & 0 deletions platform/apple/ezyfox-client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
736BEADA5A555DAFD0BCD53D /* Pods_ezyfox_client.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E2DC52FFA04E527CDBBE42F6 /* Pods_ezyfox_client.framework */; };
F18F950E266B536A004790E8 /* EzyEncryption.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F18F950D266B536A004790E8 /* EzyEncryption.cpp */; };
F18F9511266B5384004790E8 /* EzyNull.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F18F950F266B5384004790E8 /* EzyNull.cpp */; };
F18F9512266B5384004790E8 /* EzyByteArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F18F9510266B5384004790E8 /* EzyByteArray.cpp */; };
Expand Down Expand Up @@ -142,6 +143,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
82976BB90FA64B0DB31EFF1C /* Pods-ezyfox-client.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ezyfox-client.release.xcconfig"; path = "Target Support Files/Pods-ezyfox-client/Pods-ezyfox-client.release.xcconfig"; sourceTree = "<group>"; };
AE7F4B06E14A7D65AC9DC3E0 /* Pods-ezyfox-client.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ezyfox-client.debug.xcconfig"; path = "Target Support Files/Pods-ezyfox-client/Pods-ezyfox-client.debug.xcconfig"; sourceTree = "<group>"; };
E2DC52FFA04E527CDBBE42F6 /* Pods_ezyfox_client.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ezyfox_client.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F18F950D266B536A004790E8 /* EzyEncryption.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = EzyEncryption.cpp; path = ../../src/codec/EzyEncryption.cpp; sourceTree = "<group>"; };
F18F950F266B5384004790E8 /* EzyNull.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = EzyNull.cpp; path = ../../src/entity/EzyNull.cpp; sourceTree = "<group>"; };
F18F9510266B5384004790E8 /* EzyByteArray.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = EzyByteArray.cpp; path = ../../src/entity/EzyByteArray.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -272,12 +276,30 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
736BEADA5A555DAFD0BCD53D /* Pods_ezyfox_client.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
0F85B5F77AD705EBA21C74D6 /* Pods */ = {
isa = PBXGroup;
children = (
AE7F4B06E14A7D65AC9DC3E0 /* Pods-ezyfox-client.debug.xcconfig */,
82976BB90FA64B0DB31EFF1C /* Pods-ezyfox-client.release.xcconfig */,
);
path = Pods;
sourceTree = "<group>";
};
5C0BAAFCE54B1B12FDA22EE7 /* Frameworks */ = {
isa = PBXGroup;
children = (
E2DC52FFA04E527CDBBE42F6 /* Pods_ezyfox_client.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
F195504C260608FD00402511 = {
isa = PBXGroup;
children = (
Expand All @@ -288,6 +310,8 @@
F195516C26060B9500402511 /* README.md */,
F195516D26060B9500402511 /* src */,
F1955056260608FD00402511 /* Products */,
0F85B5F77AD705EBA21C74D6 /* Pods */,
5C0BAAFCE54B1B12FDA22EE7 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -624,6 +648,7 @@
isa = PBXNativeTarget;
buildConfigurationList = F195505E260608FD00402511 /* Build configuration list for PBXNativeTarget "ezyfox-client" */;
buildPhases = (
05D09CB14E9BCE551512AA4F /* [CP] Check Pods Manifest.lock */,
F195512226060A9F00402511 /* Headers */,
F1955051260608FD00402511 /* Sources */,
F1955052260608FD00402511 /* Frameworks */,
Expand Down Expand Up @@ -669,6 +694,31 @@
};
/* End PBXProject section */

/* Begin PBXShellScriptBuildPhase section */
05D09CB14E9BCE551512AA4F /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputFileListPaths = (
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-ezyfox-client-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
F1955051260608FD00402511 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down Expand Up @@ -852,9 +902,14 @@
};
F195505F260608FD00402511 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = AE7F4B06E14A7D65AC9DC3E0 /* Pods-ezyfox-client.debug.xcconfig */;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 76JURWHY44;
GCC_PREPROCESSOR_DEFINITIONS = (
"$(inherited)",
"COCOAPODS=1",
);
HEADER_SEARCH_PATHS = (
"$(SRCROOT)/../../src",
"$(SRCROOT)/../../src/handler",
Expand Down Expand Up @@ -882,9 +937,14 @@
};
F1955060260608FD00402511 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 82976BB90FA64B0DB31EFF1C /* Pods-ezyfox-client.release.xcconfig */;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 76JURWHY44;
GCC_PREPROCESSOR_DEFINITIONS = (
"$(inherited)",
"COCOAPODS=1",
);
HEADER_SEARCH_PATHS = (
"$(SRCROOT)/../../src",
"$(SRCROOT)/../../src/handler",
Expand Down
10 changes: 10 additions & 0 deletions platform/apple/ezyfox-client.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
4 changes: 4 additions & 0 deletions src/EzyClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ socket::EzySocketClient* EzyClient::newTcpSocketClient() {
return new socket::EzyTcpSocketClient();
}

void EzyClient::close() {
disconnect();
}

void EzyClient::disconnect(int reason) {
mSocketClient->disconnect(reason);
}
Expand Down
3 changes: 2 additions & 1 deletion src/EzyClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class EzyClient : public socket::EzySender {
EZY_SYNTHESIZE(entity::EzyZone*, Zone)
EZY_SYNTHESIZE(constant::EzyConnectionStatus, Status);
EZY_SYNTHESIZE(constant::EzyConnectionStatus, UdpStatus);
EZY_SYNTHESIZE(std::string, PrivateKey);
EZY_SYNTHESIZE_READONLY(std::string, Name);
EZY_SYNTHESIZE_READONLY(int64_t, SessionId);
EZY_SYNTHESIZE_READONLY(std::string, SessionToken);
Expand All @@ -74,6 +75,7 @@ class EzyClient : public socket::EzySender {
setup::EzySetup* setup();
void connect(std::string host, int port);
bool reconnect();
void close();
void disconnect(int reason = -1);
void send(request::EzyRequest* request, bool encrypted = false);
void send(constant::EzyCommand cmd, entity::EzyArray* data, bool encrypted = false);
Expand All @@ -88,7 +90,6 @@ class EzyClient : public socket::EzySender {
void destroy();
bool isConnected();
bool isUdpConnected();
bool isEnableSSL();
public:
virtual void udpConnect(int port);
virtual void udpConnect(std::string host, int port);
Expand Down
33 changes: 33 additions & 0 deletions src/handler/EzyDataHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../handler/EzyAppDataHandlers.h"
#include "../handler/EzyPluginDataHandlers.h"
#include "../socket/EzyUTSocketClient.h"
#include "../codec/EzyEncryption.h"

EZY_NAMESPACE_START_WITH(handler)

Expand Down Expand Up @@ -49,13 +50,45 @@ EzyHandshakeHandler::~EzyHandshakeHandler() {
void EzyHandshakeHandler::handle(entity::EzyArray* data) {
mPingSchedule->start();
preHandle(data);
doHandle(data);
handleLogin(data);
postHandle(data);
}

void EzyHandshakeHandler::preHandle(entity::EzyArray *data) {
}

void EzyHandshakeHandler::doHandle(entity::EzyArray *data) {
mClient->setSessionId(data->getInt(2));
mClient->setSessionToken(data->getString(1));
auto encyptedSessionKey = data->size() <= 3
? ""
: data->getString(3);
mClient->setSessionKey(decrypteSessionKey(encyptedSessionKey));
}

std::string EzyHandshakeHandler::decrypteSessionKey(std::string encyptedSessionKey) {
std::string sessionKey = "";
#ifdef EZY_SSL_ENABLE
if(encyptedSessionKey.empty()) {
#ifdef EZY_DEBUG
return "";
#else
logger::log("maybe server was not enable SSL, you must enable SSL on server or disable SSL on your client or enable debug mode");
mClient->close();
return "";
#endif
}
auto sessionKeySize = 0;
auto privateKey = mClient->getPrivateKey();
auto rsa = codec::EzyRSA::getInstance();
auto sessionKeyData = rsa->decrypt(encyptedSessionKey.c_str(),
(int) encyptedSessionKey.size(),
privateKey,
sessionKeySize);
sessionKey = std::string(sessionKeyData, sessionKeySize);
#endif
return sessionKey;
}

void EzyHandshakeHandler::handleLogin(entity::EzyArray* data) {
Expand Down
2 changes: 2 additions & 0 deletions src/handler/EzyDataHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class EzyHandshakeHandler : public EzyDataHandler {
protected:
virtual void setClient(EzyClient* client);
virtual void preHandle(entity::EzyArray* data);
virtual void doHandle(entity::EzyArray* data);
virtual void postHandle(entity::EzyArray* data);
virtual std::string decrypteSessionKey(std::string encyptedSessionKey);
virtual request::EzyRequest* getLoginRequest() = 0;
public:
EzyHandshakeHandler();
Expand Down
7 changes: 7 additions & 0 deletions src/handler/EzyEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../constant/EzyDisconnectReason.h"
#include "../request/EzyRequest.h"
#include "../logger/EzyLogger.h"
#include "../codec/EzyEncryption.h"
#include "../config/EzyClientConfig.h"
#include "../EzyClient.h"
#include "EzyEventHandler.h"
Expand Down Expand Up @@ -52,6 +53,12 @@ std::string EzyConnectionSuccessHandler::getClientId() {
}

std::string EzyConnectionSuccessHandler::generateClientKey() {
#ifdef EZY_SSL_ENABLE
auto keypair = codec::EzyRSA::getInstance()->generateKeyPair();
mClient->setPrivateKey(keypair->getPrivateKey());
auto publicKey = keypair->getPublicKey();
return publicKey;
#endif
return "";
}

Expand Down
Loading

0 comments on commit b68412c

Please sign in to comment.