Skip to content

Commit 7886071

Browse files
committed
更新客户端:使用加密协议与服务端通讯
1 parent 8f7cad2 commit 7886071

11 files changed

Lines changed: 106 additions & 11 deletions

File tree

‎kbe/bin/client/kbengine.xml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
1: blowfish
6363
2: rsa (res\key\kbengine_private.key)
6464
-->
65-
<encrypt_type> 0 </encrypt_type>
65+
<encrypt_type> 1 </encrypt_type>
6666
</channelCommon>
6767

6868
<ip> localhost </ip>
@@ -72,4 +72,12 @@
7272

7373
<useLastAccountName>false</useLastAccountName>
7474
<accountName>kbengine</accountName>
75+
76+
<!-- 加密登录信息
77+
可选加密:
78+
0: 无加密
79+
1: blowfish
80+
2: rsa (res\key\kbengine_private.key)
81+
-->
82+
<encrypt_login> 2 </encrypt_login>
7583
</root>

‎kbe/res/server/kbengine_defs.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
1: blowfish
7777
2: rsa (res\key\kbengine_private.key)
7878
-->
79-
<encrypt_type> 0 </encrypt_type>
79+
<encrypt_type> 1 </encrypt_type>
8080
</channelCommon>
8181

8282
<billingSystem>
@@ -208,7 +208,7 @@
208208
1: blowfish
209209
2: rsa (res\key\kbengine_private.key)
210210
-->
211-
<encrypt_login> 0 </encrypt_login>
211+
<encrypt_login> 2 </encrypt_login>
212212

213213
<!-- listen监听队列最大值 -->
214214
<SOMAXCONN> 128 </SOMAXCONN>

‎kbe/src/client/kbengine_dll/kbengine_client_dll.vcproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
Name="VCCLCompilerTool"
4343
Optimization="0"
4444
AdditionalIncludeDirectories="../../lib/python/PC;../../lib/python/Include;../../lib;../../common;../../lib/third_party/g3dlite;../../server;../../lib/third_party/log4cxx/include;../../lib/third_party;../../lib/third_party/openssl/include"
45-
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;CLIENT_DLL_EXPORTS;ENABLE_WATCHERS;Py_NO_ENABLE_SHARED;CODE_INLINE;KBE_USE_ASSERTS;USE_PYTHON;LOG4CXX_STATIC;KBE_CLIENT"
45+
PreprocessorDefinitions="KBE_CLIENT;USE_OPENSSL;WIN32;_DEBUG;_WINDOWS;_USRDLL;CLIENT_DLL_EXPORTS;ENABLE_WATCHERS;Py_NO_ENABLE_SHARED;CODE_INLINE;KBE_USE_ASSERTS;USE_PYTHON;LOG4CXX_STATIC"
4646
MinimalRebuild="true"
4747
BasicRuntimeChecks="3"
4848
RuntimeLibrary="1"
@@ -121,7 +121,7 @@
121121
Optimization="2"
122122
EnableIntrinsicFunctions="true"
123123
AdditionalIncludeDirectories="../../lib/python/PC;../../lib/python/Include;../../lib;../../common;../../lib/third_party/g3dlite;../../server;../../lib/third_party/log4cxx/include;../../lib/third_party;../../lib/third_party/openssl/include"
124-
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;CLIENT_DLL_EXPORTS;ENABLE_WATCHERS;Py_NO_ENABLE_SHARED;CODE_INLINE;KBE_USE_ASSERTS;USE_PYTHON;LOG4CXX_STATIC;KBE_CLIENT"
124+
PreprocessorDefinitions="KBE_CLIENT;USE_OPENSSL;WIN32;NDEBUG;_WINDOWS;_USRDLL;CLIENT_DLL_EXPORTS;ENABLE_WATCHERS;Py_NO_ENABLE_SHARED;CODE_INLINE;KBE_USE_ASSERTS;USE_PYTHON;LOG4CXX_STATIC;KBE_CLIENT"
125125
RuntimeLibrary="0"
126126
EnableFunctionLevelLinking="true"
127127
UsePrecompiledHeader="0"

‎kbe/src/client/kbengine_dll/main.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "client_lib/kbemain.hpp"
66
#include "server/serverconfig.hpp"
77
#include "cstdkbe/memorystream.hpp"
8+
#include "cstdkbe/kbekey.hpp"
89
#include "thread/threadtask.hpp"
910
#include "thread/concurrency.hpp"
1011
#include "helper/debug_helper.hpp"
@@ -189,6 +190,11 @@ bool kbe_init()
189190

190191
INFO_MSG( "-----------------------------------------------------------------------------------------\n\n\n");
191192

193+
#ifdef USE_OPENSSL
194+
if(KBEngine::KBEKey::getSingletonPtr() == NULL)
195+
KBEngine::KBEKey kbekey(KBEngine::Resmgr::getSingleton().matchPath("key/") + "kbengine_public.key", "");
196+
#endif
197+
192198
if(g_pScript == NULL)
193199
g_pScript = new KBEngine::script::Script();
194200

‎kbe/src/lib/client_lib/client_lib90.vcproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
Name="VCCLCompilerTool"
4343
Optimization="0"
4444
AdditionalIncludeDirectories="../python/PC;../python/Include;../third_party/g3dlite;../;../../common;../../lib/third_party/log4cxx/include;../../lib/third_party;../../lib/third_party/openssl/include"
45-
PreprocessorDefinitions="ENABLE_WATCHERS;WIN32;_DEBUG;_LIB;CODE_INLINE;KBE_USE_ASSERTS;LOG4CXX_STATIC"
45+
PreprocessorDefinitions="KBE_CLIENT;USE_OPENSSL;ENABLE_WATCHERS;WIN32;_DEBUG;_LIB;CODE_INLINE;KBE_USE_ASSERTS;LOG4CXX_STATIC"
4646
MinimalRebuild="true"
4747
BasicRuntimeChecks="3"
4848
RuntimeLibrary="1"
@@ -108,7 +108,7 @@
108108
Optimization="2"
109109
EnableIntrinsicFunctions="true"
110110
AdditionalIncludeDirectories="../python/PC;../python/Include;../third_party/g3dlite;../;../../common;../../lib/third_party/log4cxx/include;../../lib/third_party;../../lib/third_party/openssl/include"
111-
PreprocessorDefinitions="ENABLE_WATCHERS;WIN32;NDEBUG;_LIB;CODE_INLINE;KBE_USE_ASSERTS;LOG4CXX_STATIC"
111+
PreprocessorDefinitions="KBE_CLIENT;USE_OPENSSL;ENABLE_WATCHERS;WIN32;NDEBUG;_LIB;CODE_INLINE;KBE_USE_ASSERTS;LOG4CXX_STATIC"
112112
RuntimeLibrary="0"
113113
EnableFunctionLevelLinking="true"
114114
UsePrecompiledHeader="0"

‎kbe/src/lib/client_lib/clientapp.cpp‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
2323
#include "event.hpp"
2424
#include "entity.hpp"
2525
#include "config.hpp"
26+
#include "cstdkbe/kbeversion.hpp"
2627
#include "network/channel.hpp"
2728
#include "network/tcp_packet_receiver.hpp"
2829
#include "thread/threadpool.hpp"
@@ -60,6 +61,7 @@ componentID_(componentID),
6061
mainDispatcher_(dispatcher),
6162
networkInterface_(ninterface),
6263
pTCPPacketReceiver_(NULL),
64+
pBlowfishFilter_(NULL),
6365
time_(),
6466
timers_(),
6567
threadPool_(),
@@ -81,6 +83,7 @@ entryScript_()
8183
//-------------------------------------------------------------------------------------
8284
ClientApp::~ClientApp()
8385
{
86+
SAFE_RELEASE(pBlowfishFilter_);
8487
}
8588

8689
//-------------------------------------------------------------------------------------
@@ -388,7 +391,24 @@ bool ClientApp::login(std::string accountName, std::string passwd,
388391

389392
getNetworkInterface().dispatcher().registerFileDescriptor(*pServerChannel_->endpoint(), pTCPPacketReceiver_);
390393

391-
ret = ClientObjectBase::login();
394+
// 先握手然后等helloCB之后再进行登录
395+
Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool().createObject();
396+
(*pBundle).newMessage(LoginappInterface::hello);
397+
(*pBundle) << KBEVersion::versionString();
398+
399+
if(Mercury::g_channelExternalEncryptType == 1)
400+
{
401+
pBlowfishFilter_ = new Mercury::BlowfishFilter();
402+
(*pBundle).appendBlob(pBlowfishFilter_->key());
403+
}
404+
else
405+
{
406+
std::string key = "";
407+
(*pBundle).appendBlob(key);
408+
}
409+
410+
pServerChannel_->pushBundle(pBundle);
411+
//ret = ClientObjectBase::login();
392412
}
393413

394414
return ret;
@@ -398,6 +418,28 @@ bool ClientApp::login(std::string accountName, std::string passwd,
398418
void ClientApp::onHelloCB_(Mercury::Channel* pChannel, const std::string& verInfo,
399419
COMPONENT_TYPE componentType)
400420
{
421+
if(Mercury::g_channelExternalEncryptType == 1)
422+
{
423+
pServerChannel_->pFilter(pBlowfishFilter_);
424+
pBlowfishFilter_ = NULL;
425+
}
426+
427+
if(componentType == LOGINAPP_TYPE)
428+
{
429+
if(!ClientObjectBase::login())
430+
{
431+
WARNING_MSG("ClientApp::onHelloCB_: login is failed!\n");
432+
return;
433+
}
434+
}
435+
else
436+
{
437+
if(!ClientObjectBase::loginGateWay())
438+
{
439+
WARNING_MSG("ClientApp::onHelloCB_: login is failed!\n");
440+
return;
441+
}
442+
}
401443
}
402444

403445
//-------------------------------------------------------------------------------------
@@ -430,7 +472,25 @@ void ClientApp::onLoginSuccessfully(Mercury::Channel * pChannel, MemoryStream& s
430472

431473
getNetworkInterface().dispatcher().registerFileDescriptor(*pServerChannel_->endpoint(), pTCPPacketReceiver_);
432474

433-
ret = ClientObjectBase::loginGateWay();
475+
// 先握手然后等helloCB之后再进行登录
476+
Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool().createObject();
477+
(*pBundle).newMessage(BaseappInterface::hello);
478+
(*pBundle) << KBEVersion::versionString();
479+
480+
if(Mercury::g_channelExternalEncryptType == 1)
481+
{
482+
pBlowfishFilter_ = new Mercury::BlowfishFilter();
483+
(*pBundle).appendBlob(pBlowfishFilter_->key());
484+
pServerChannel_->pFilter(NULL);
485+
}
486+
else
487+
{
488+
std::string key = "";
489+
(*pBundle).appendBlob(key);
490+
}
491+
492+
pServerChannel_->pushBundle(pBundle);
493+
// ret = ClientObjectBase::loginGateWay();
434494
}
435495
}
436496

‎kbe/src/lib/client_lib/clientapp.hpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
3030
#include "cstdkbe/smartpointer.hpp"
3131
#include "cstdkbe/timer.hpp"
3232
#include "network/interfaces.hpp"
33+
#include "network/encryption_filter.hpp"
3334
#include "network/event_dispatcher.hpp"
3435
#include "network/network_interface.hpp"
3536
#include "thread/threadpool.hpp"
@@ -140,6 +141,7 @@ class ClientApp :
140141
Mercury::NetworkInterface& networkInterface_;
141142

142143
Mercury::TCPPacketReceiver* pTCPPacketReceiver_;
144+
Mercury::BlowfishFilter* pBlowfishFilter_;
143145

144146
GAME_TIME time_;
145147
Timers timers_;
@@ -148,6 +150,7 @@ class ClientApp :
148150
thread::ThreadPool threadPool_;
149151

150152
PyObjectPtr entryScript_;
153+
151154
};
152155

153156
}

‎kbe/src/lib/client_lib/clientobjectbase.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class ClientObjectBase : public script::ScriptObject
7777

7878
void tickSend();
7979

80-
Mercury::Channel* initLoginappChannel(std::string accountName, std::string passwd, std::string ip, KBEngine::uint32 port);
81-
Mercury::Channel* initBaseappChannel();
80+
virtual Mercury::Channel* initLoginappChannel(std::string accountName, std::string passwd, std::string ip, KBEngine::uint32 port);
81+
virtual Mercury::Channel* initBaseappChannel();
8282

8383
bool createAccount();
8484
bool login();

‎kbe/src/lib/client_lib/config.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tcp_SOMAXCONN_(5),
3434
port_(0),
3535
channelInternalTimeout_(60.0f),
3636
channelExternalTimeout_(60.0f),
37+
encrypt_login_(0),
3738
fileName_(),
3839
useLastAccountName_(false)
3940
{
@@ -157,6 +158,12 @@ bool Config::loadConfig(std::string fileName)
157158
Mercury::g_extReceiveWindowBytesOverflow = KBE_MAX(16, xml->getValInt(childnode1));
158159
}
159160
};
161+
162+
childnode = xml->enterNode(rootNode, "encrypt_type");
163+
if(childnode)
164+
{
165+
Mercury::g_channelExternalEncryptType = xml->getValInt(childnode);
166+
}
160167
}
161168

162169
rootNode = xml->getRootNode("gameUpdateHertz");
@@ -193,6 +200,12 @@ bool Config::loadConfig(std::string fileName)
193200
useLastAccountName_ = xml->getValStr(rootNode) != "false";
194201
}
195202

203+
rootNode = xml->getRootNode("encrypt_login");
204+
if(rootNode != NULL)
205+
{
206+
encrypt_login_ = xml->getValInt(rootNode);
207+
}
208+
196209
SAFE_RELEASE(xml);
197210
return true;
198211
}

‎kbe/src/lib/client_lib/config.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class Config : public Singleton<Config>
5656
void writeAccountName(const char* name);
5757

5858
bool useLastAccountName()const{ return useLastAccountName_; }
59+
60+
int8 encryptLogin()const { return encrypt_login_; }
5961
public:
6062
int16 gameUpdateHertz_;
6163

@@ -70,6 +72,8 @@ class Config : public Singleton<Config>
7072
float channelExternalTimeout_;
7173

7274
char accountName_[MAX_NAME];
75+
76+
int8 encrypt_login_;
7377

7478
std::string fileName_;
7579

0 commit comments

Comments
 (0)