Skip to content

Commit b68412c

Browse files
authored
ssl (#23)
* ssl * update * update flag * update ssl client * add customize ssl
1 parent 1d55695 commit b68412c

File tree

11 files changed

+317
-80
lines changed

11 files changed

+317
-80
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
platform/apple/Podfile.lock
2+
platform/apple/Pods
13
example/example
24
libezyfox-client.a
35
.idea/

platform/apple/Podfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Uncomment the next line to define a global platform for your project
2+
# platform :ios, '9.0'
3+
4+
target 'ezyfox-client' do
5+
# Comment the next line if you don't want to use dynamic frameworks
6+
use_frameworks!
7+
8+
# Pods for ezyfox-client
9+
pod 'OpenSSL-Universal'
10+
11+
end

platform/apple/ezyfox-client.xcodeproj/project.pbxproj

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

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

144145
/* Begin PBXFileReference section */
146+
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>"; };
147+
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>"; };
148+
E2DC52FFA04E527CDBBE42F6 /* Pods_ezyfox_client.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ezyfox_client.framework; sourceTree = BUILT_PRODUCTS_DIR; };
145149
F18F950D266B536A004790E8 /* EzyEncryption.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = EzyEncryption.cpp; path = ../../src/codec/EzyEncryption.cpp; sourceTree = "<group>"; };
146150
F18F950F266B5384004790E8 /* EzyNull.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = EzyNull.cpp; path = ../../src/entity/EzyNull.cpp; sourceTree = "<group>"; };
147151
F18F9510266B5384004790E8 /* EzyByteArray.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = EzyByteArray.cpp; path = ../../src/entity/EzyByteArray.cpp; sourceTree = "<group>"; };
@@ -272,12 +276,30 @@
272276
isa = PBXFrameworksBuildPhase;
273277
buildActionMask = 2147483647;
274278
files = (
279+
736BEADA5A555DAFD0BCD53D /* Pods_ezyfox_client.framework in Frameworks */,
275280
);
276281
runOnlyForDeploymentPostprocessing = 0;
277282
};
278283
/* End PBXFrameworksBuildPhase section */
279284

280285
/* Begin PBXGroup section */
286+
0F85B5F77AD705EBA21C74D6 /* Pods */ = {
287+
isa = PBXGroup;
288+
children = (
289+
AE7F4B06E14A7D65AC9DC3E0 /* Pods-ezyfox-client.debug.xcconfig */,
290+
82976BB90FA64B0DB31EFF1C /* Pods-ezyfox-client.release.xcconfig */,
291+
);
292+
path = Pods;
293+
sourceTree = "<group>";
294+
};
295+
5C0BAAFCE54B1B12FDA22EE7 /* Frameworks */ = {
296+
isa = PBXGroup;
297+
children = (
298+
E2DC52FFA04E527CDBBE42F6 /* Pods_ezyfox_client.framework */,
299+
);
300+
name = Frameworks;
301+
sourceTree = "<group>";
302+
};
281303
F195504C260608FD00402511 = {
282304
isa = PBXGroup;
283305
children = (
@@ -288,6 +310,8 @@
288310
F195516C26060B9500402511 /* README.md */,
289311
F195516D26060B9500402511 /* src */,
290312
F1955056260608FD00402511 /* Products */,
313+
0F85B5F77AD705EBA21C74D6 /* Pods */,
314+
5C0BAAFCE54B1B12FDA22EE7 /* Frameworks */,
291315
);
292316
sourceTree = "<group>";
293317
};
@@ -624,6 +648,7 @@
624648
isa = PBXNativeTarget;
625649
buildConfigurationList = F195505E260608FD00402511 /* Build configuration list for PBXNativeTarget "ezyfox-client" */;
626650
buildPhases = (
651+
05D09CB14E9BCE551512AA4F /* [CP] Check Pods Manifest.lock */,
627652
F195512226060A9F00402511 /* Headers */,
628653
F1955051260608FD00402511 /* Sources */,
629654
F1955052260608FD00402511 /* Frameworks */,
@@ -669,6 +694,31 @@
669694
};
670695
/* End PBXProject section */
671696

697+
/* Begin PBXShellScriptBuildPhase section */
698+
05D09CB14E9BCE551512AA4F /* [CP] Check Pods Manifest.lock */ = {
699+
isa = PBXShellScriptBuildPhase;
700+
buildActionMask = 2147483647;
701+
files = (
702+
);
703+
inputFileListPaths = (
704+
);
705+
inputPaths = (
706+
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
707+
"${PODS_ROOT}/Manifest.lock",
708+
);
709+
name = "[CP] Check Pods Manifest.lock";
710+
outputFileListPaths = (
711+
);
712+
outputPaths = (
713+
"$(DERIVED_FILE_DIR)/Pods-ezyfox-client-checkManifestLockResult.txt",
714+
);
715+
runOnlyForDeploymentPostprocessing = 0;
716+
shellPath = /bin/sh;
717+
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";
718+
showEnvVarsInLog = 0;
719+
};
720+
/* End PBXShellScriptBuildPhase section */
721+
672722
/* Begin PBXSourcesBuildPhase section */
673723
F1955051260608FD00402511 /* Sources */ = {
674724
isa = PBXSourcesBuildPhase;
@@ -852,9 +902,14 @@
852902
};
853903
F195505F260608FD00402511 /* Debug */ = {
854904
isa = XCBuildConfiguration;
905+
baseConfigurationReference = AE7F4B06E14A7D65AC9DC3E0 /* Pods-ezyfox-client.debug.xcconfig */;
855906
buildSettings = {
856907
CODE_SIGN_STYLE = Automatic;
857908
DEVELOPMENT_TEAM = 76JURWHY44;
909+
GCC_PREPROCESSOR_DEFINITIONS = (
910+
"$(inherited)",
911+
"COCOAPODS=1",
912+
);
858913
HEADER_SEARCH_PATHS = (
859914
"$(SRCROOT)/../../src",
860915
"$(SRCROOT)/../../src/handler",
@@ -882,9 +937,14 @@
882937
};
883938
F1955060260608FD00402511 /* Release */ = {
884939
isa = XCBuildConfiguration;
940+
baseConfigurationReference = 82976BB90FA64B0DB31EFF1C /* Pods-ezyfox-client.release.xcconfig */;
885941
buildSettings = {
886942
CODE_SIGN_STYLE = Automatic;
887943
DEVELOPMENT_TEAM = 76JURWHY44;
944+
GCC_PREPROCESSOR_DEFINITIONS = (
945+
"$(inherited)",
946+
"COCOAPODS=1",
947+
);
888948
HEADER_SEARCH_PATHS = (
889949
"$(SRCROOT)/../../src",
890950
"$(SRCROOT)/../../src/handler",

platform/apple/ezyfox-client.xcworkspace/contents.xcworkspacedata

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

src/EzyClient.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ socket::EzySocketClient* EzyClient::newTcpSocketClient() {
110110
return new socket::EzyTcpSocketClient();
111111
}
112112

113+
void EzyClient::close() {
114+
disconnect();
115+
}
116+
113117
void EzyClient::disconnect(int reason) {
114118
mSocketClient->disconnect(reason);
115119
}

src/EzyClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class EzyClient : public socket::EzySender {
5151
EZY_SYNTHESIZE(entity::EzyZone*, Zone)
5252
EZY_SYNTHESIZE(constant::EzyConnectionStatus, Status);
5353
EZY_SYNTHESIZE(constant::EzyConnectionStatus, UdpStatus);
54+
EZY_SYNTHESIZE(std::string, PrivateKey);
5455
EZY_SYNTHESIZE_READONLY(std::string, Name);
5556
EZY_SYNTHESIZE_READONLY(int64_t, SessionId);
5657
EZY_SYNTHESIZE_READONLY(std::string, SessionToken);
@@ -74,6 +75,7 @@ class EzyClient : public socket::EzySender {
7475
setup::EzySetup* setup();
7576
void connect(std::string host, int port);
7677
bool reconnect();
78+
void close();
7779
void disconnect(int reason = -1);
7880
void send(request::EzyRequest* request, bool encrypted = false);
7981
void send(constant::EzyCommand cmd, entity::EzyArray* data, bool encrypted = false);
@@ -88,7 +90,6 @@ class EzyClient : public socket::EzySender {
8890
void destroy();
8991
bool isConnected();
9092
bool isUdpConnected();
91-
bool isEnableSSL();
9293
public:
9394
virtual void udpConnect(int port);
9495
virtual void udpConnect(std::string host, int port);

src/handler/EzyDataHandler.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "../handler/EzyAppDataHandlers.h"
1818
#include "../handler/EzyPluginDataHandlers.h"
1919
#include "../socket/EzyUTSocketClient.h"
20+
#include "../codec/EzyEncryption.h"
2021

2122
EZY_NAMESPACE_START_WITH(handler)
2223

@@ -49,13 +50,45 @@ EzyHandshakeHandler::~EzyHandshakeHandler() {
4950
void EzyHandshakeHandler::handle(entity::EzyArray* data) {
5051
mPingSchedule->start();
5152
preHandle(data);
53+
doHandle(data);
5254
handleLogin(data);
5355
postHandle(data);
5456
}
5557

5658
void EzyHandshakeHandler::preHandle(entity::EzyArray *data) {
59+
}
60+
61+
void EzyHandshakeHandler::doHandle(entity::EzyArray *data) {
5762
mClient->setSessionId(data->getInt(2));
5863
mClient->setSessionToken(data->getString(1));
64+
auto encyptedSessionKey = data->size() <= 3
65+
? ""
66+
: data->getString(3);
67+
mClient->setSessionKey(decrypteSessionKey(encyptedSessionKey));
68+
}
69+
70+
std::string EzyHandshakeHandler::decrypteSessionKey(std::string encyptedSessionKey) {
71+
std::string sessionKey = "";
72+
#ifdef EZY_SSL_ENABLE
73+
if(encyptedSessionKey.empty()) {
74+
#ifdef EZY_DEBUG
75+
return "";
76+
#else
77+
logger::log("maybe server was not enable SSL, you must enable SSL on server or disable SSL on your client or enable debug mode");
78+
mClient->close();
79+
return "";
80+
#endif
81+
}
82+
auto sessionKeySize = 0;
83+
auto privateKey = mClient->getPrivateKey();
84+
auto rsa = codec::EzyRSA::getInstance();
85+
auto sessionKeyData = rsa->decrypt(encyptedSessionKey.c_str(),
86+
(int) encyptedSessionKey.size(),
87+
privateKey,
88+
sessionKeySize);
89+
sessionKey = std::string(sessionKeyData, sessionKeySize);
90+
#endif
91+
return sessionKey;
5992
}
6093

6194
void EzyHandshakeHandler::handleLogin(entity::EzyArray* data) {

src/handler/EzyDataHandler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ class EzyHandshakeHandler : public EzyDataHandler {
5353
protected:
5454
virtual void setClient(EzyClient* client);
5555
virtual void preHandle(entity::EzyArray* data);
56+
virtual void doHandle(entity::EzyArray* data);
5657
virtual void postHandle(entity::EzyArray* data);
58+
virtual std::string decrypteSessionKey(std::string encyptedSessionKey);
5759
virtual request::EzyRequest* getLoginRequest() = 0;
5860
public:
5961
EzyHandshakeHandler();

src/handler/EzyEventHandler.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../constant/EzyDisconnectReason.h"
44
#include "../request/EzyRequest.h"
55
#include "../logger/EzyLogger.h"
6+
#include "../codec/EzyEncryption.h"
67
#include "../config/EzyClientConfig.h"
78
#include "../EzyClient.h"
89
#include "EzyEventHandler.h"
@@ -52,6 +53,12 @@ std::string EzyConnectionSuccessHandler::getClientId() {
5253
}
5354

5455
std::string EzyConnectionSuccessHandler::generateClientKey() {
56+
#ifdef EZY_SSL_ENABLE
57+
auto keypair = codec::EzyRSA::getInstance()->generateKeyPair();
58+
mClient->setPrivateKey(keypair->getPrivateKey());
59+
auto publicKey = keypair->getPublicKey();
60+
return publicKey;
61+
#endif
5562
return "";
5663
}
5764

0 commit comments

Comments
 (0)