Skip to content

Commit b9a7895

Browse files
authored
Merge pull request #30 from youngmonkeys/master
add ssl encryption
2 parents c6e81d4 + bb75ca9 commit b9a7895

40 files changed

+809
-167
lines changed

EzyClient.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,32 @@ public interface EzyClient
2121

2222
bool reconnect();
2323

24-
void send(EzyRequest request);
24+
void send(EzyRequest request, bool encrypted = false);
2525

26-
void send(EzyCommand cmd, EzyArray data);
26+
void send(EzyCommand cmd, EzyArray data, bool encrypted = false);
2727

28-
void disconnect(int reason = (int)EzyDisconnectReason.CLOSE);
28+
void disconnect(int reason = (int)EzyDisconnectReason.CLOSE);
29+
30+
void close();
2931

3032
void processEvents();
3133

3234
void udpConnect(int port);
3335

3436
void udpConnect(String host, int port);
3537

36-
void udpSend(EzyRequest request);
38+
void udpSend(EzyRequest request, bool encrypted = false);
3739

38-
void udpSend(EzyCommand cmd, EzyArray data);
40+
void udpSend(EzyCommand cmd, EzyArray data, bool encrypted = false);
3941

4042
String getName();
4143

4244
EzyClientConfig getConfig();
4345

46+
bool isEnableSSL();
47+
48+
bool isEnableDebug();
49+
4450
EzyUser getMe();
4551

4652
EzyZone getZone();
@@ -61,6 +67,18 @@ public interface EzyClient
6167

6268
void setSessionToken(String token);
6369

70+
void setSessionKey(byte[] sessionKey);
71+
72+
byte[] getSessionKey();
73+
74+
void setPrivateKey(byte[] privateKey);
75+
76+
byte[] getPrivateKey();
77+
78+
void setPublicKey(byte[] publicKey);
79+
80+
byte[] getPublicKey();
81+
6482
EzyISocketClient getSocket();
6583

6684
EzyApp getApp();

EzyTcpClient.cs

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class EzyTcpClient :
2121
protected EzyUser me;
2222
protected EzyZone zone;
2323
protected long sessionId;
24+
protected byte[] publicKey;
25+
protected byte[] privateKey;
26+
protected byte[] sessionKey;
2427
protected String sessionToken;
2528
protected readonly String name;
2629
protected readonly EzySetup settingUp;
@@ -127,8 +130,10 @@ public bool reconnect()
127130
}
128131
preconnect();
129132
bool success = socketClient.reconnect();
130-
if (success)
131-
setStatus(EzyConnectionStatus.RECONNECTING);
133+
if (success)
134+
{
135+
setStatus(EzyConnectionStatus.RECONNECTING);
136+
}
132137
return success;
133138
}
134139

@@ -143,24 +148,46 @@ public void disconnect(int reason)
143148
socketClient.disconnect(reason);
144149
}
145150

146-
public void send(EzyRequest request)
151+
public void close()
152+
{
153+
disconnect((int) EzyDisconnectReason.CLOSE);
154+
}
155+
156+
public void send(EzyRequest request, bool encrypted)
147157
{
148158
Object cmd = request.getCommand();
149159
EzyData data = request.serialize();
150-
send((EzyCommand)cmd, (EzyArray)data);
160+
send((EzyCommand)cmd, (EzyArray)data, encrypted);
151161
}
152162

153-
public void send(EzyCommand cmd, EzyArray data)
154-
{
163+
public void send(EzyCommand cmd, EzyArray data, bool encrypted)
164+
{
165+
bool shouldEncrypted = encrypted;
166+
if (encrypted && sessionKey == null)
167+
{
168+
if (config.isEnableDebug())
169+
{
170+
shouldEncrypted = false;
171+
}
172+
else
173+
{
174+
throw new ArgumentException(
175+
"can not send command: " + cmd + " " +
176+
"you must enable SSL or enable debug mode by configuration " +
177+
"when you create the client"
178+
);
179+
}
180+
181+
}
155182
EzyArray array = requestSerializer.serialize(cmd, data);
156-
if(socketClient != null)
183+
if (socketClient != null)
157184
{
158-
socketClient.sendMessage(array);
185+
socketClient.sendMessage(array, shouldEncrypted);
159186
printSentData(cmd, data);
160187
}
161-
}
188+
}
162189

163-
public void processEvents()
190+
public void processEvents()
164191
{
165192
socketClient.processEventMessages();
166193
}
@@ -175,7 +202,17 @@ public EzyClientConfig getConfig()
175202
return config;
176203
}
177204

178-
public EzyZone getZone()
205+
public bool isEnableSSL()
206+
{
207+
return config.isEnableSSL();
208+
}
209+
210+
public bool isEnableDebug()
211+
{
212+
return config.isEnableDebug();
213+
}
214+
215+
public EzyZone getZone()
179216
{
180217
return zone;
181218
}
@@ -237,6 +274,38 @@ public void setSessionToken(String token)
237274
this.socketClient.setSessionToken(sessionToken);
238275
}
239276

277+
public void setSessionKey(byte[] sessionKey)
278+
{
279+
this.sessionKey = sessionKey;
280+
this.socketClient.setSessionKey(sessionKey);
281+
}
282+
283+
public byte[] getSessionKey()
284+
{
285+
return sessionKey;
286+
}
287+
288+
public void setPublicKey(byte[] publicKey)
289+
{
290+
this.publicKey = publicKey;
291+
}
292+
293+
public byte[] getPublicKey()
294+
{
295+
return publicKey;
296+
}
297+
298+
public void setPrivateKey(byte[] privateKey)
299+
{
300+
this.privateKey = privateKey;
301+
}
302+
303+
public byte[] getPrivateKey()
304+
{
305+
return privateKey;
306+
}
307+
308+
240309
public EzyISocketClient getSocket()
241310
{
242311
return socketClient;
@@ -303,7 +372,9 @@ public EzyHandlerManager getHandlerManager()
303372
protected void printSentData(EzyCommand cmd, EzyArray data)
304373
{
305374
if (!unloggableCommands.Contains(cmd))
375+
{
306376
logger.debug("send command: " + cmd + " and data: " + data);
377+
}
307378
}
308379

309380
public virtual void udpConnect(int port)
@@ -316,12 +387,12 @@ public virtual void udpConnect(String host, int port)
316387
throw new InvalidOperationException("only support TCP, use EzyUTClient instead");
317388
}
318389

319-
public virtual void udpSend(EzyRequest request)
390+
public virtual void udpSend(EzyRequest request, bool encrypted)
320391
{
321392
throw new InvalidOperationException("only support TCP, use EzyUTClient instead");
322393
}
323394

324-
public virtual void udpSend(EzyCommand cmd, EzyArray data)
395+
public virtual void udpSend(EzyCommand cmd, EzyArray data, bool encrypted)
325396
{
326397
throw new InvalidOperationException("only support TCP, use EzyUTClient instead");
327398
}

EzyUTClient.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,36 @@ public override void udpConnect(String host, int port)
2828
((EzyUTSocketClient)socketClient).udpConnect(host, port);
2929
}
3030

31-
public override void udpSend(EzyRequest request)
31+
public override void udpSend(EzyRequest request, bool encrypted)
3232
{
3333
Object cmd = request.getCommand();
3434
EzyData data = request.serialize();
35-
send((EzyCommand)cmd, (EzyArray)data);
35+
udpSend((EzyCommand)cmd, (EzyArray)data, encrypted);
3636
}
3737

38-
public override void udpSend(EzyCommand cmd, EzyArray data)
38+
public override void udpSend(EzyCommand cmd, EzyArray data, bool encrypted)
3939
{
40+
bool shouldEncrypted = encrypted;
41+
if (encrypted && sessionKey == null)
42+
{
43+
if (config.isEnableDebug())
44+
{
45+
shouldEncrypted = false;
46+
}
47+
else
48+
{
49+
throw new ArgumentException(
50+
"can not send command: " + cmd + " " +
51+
"you must enable SSL or enable debug mode by configuration " +
52+
"when you create the client"
53+
);
54+
}
55+
56+
}
4057
EzyArray array = requestSerializer.serialize(cmd, data);
4158
if (socketClient != null)
4259
{
43-
((EzyUTSocketClient)socketClient).udpSendMessage(array);
60+
((EzyUTSocketClient)socketClient).udpSendMessage(array, shouldEncrypted);
4461
printSentData(cmd, data);
4562
}
4663
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ csharp, unity client for ezyfox server
55

66
csharp, unity client for ezyfox server
77

8+
# For Unity
9+
10+
Please move to the [unity-client branch](https://github.com/youngmonkeys/ezyfox-server-csharp-client/tree/unity-client).
11+
812
# Documentation
913

1014
[https://youngmonkeys.org/ezyfox-csharp-client-sdk/](https://youngmonkeys.org/ezyfox-csharp-client-sdk/)

builder/EzyArrayBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public EzyArrayBuilder append(Object value)
2727
return this;
2828
}
2929

30-
public EzyArrayBuilder append<T>(EzyBuilder<T> builder)
30+
public EzyArrayBuilder append(byte[] value)
31+
{
32+
product.add(value);
33+
return this;
34+
}
35+
36+
public EzyArrayBuilder append<T>(EzyBuilder<T> builder)
3137
{
3238
product.add(builder);
3339
return this;

codec/EzyByteToObjectDecoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace com.tvd12.ezyfoxserver.client.codec
77

88
public interface EzyByteToObjectDecoder
99
{
10-
Object decode(EzyMessage message);
10+
Object decode(EzyMessage message, byte[] decryptionKey);
1111

1212
void decode(EzyByteBuffer bytes, Queue<EzyMessage> queue);
1313
}

codec/EzyObjectToByteEncoder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ namespace com.tvd12.ezyfoxserver.client.codec
55
public interface EzyObjectToByteEncoder
66
{
77
byte[] encode(Object msg);
8-
}
8+
9+
byte[] toMessageContent(Object data);
10+
11+
byte[] encryptMessageContent(byte[] messageContent, byte[] encryptionKey);
12+
}
913
}

codec/EzyObjectToMessage.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ namespace com.tvd12.ezyfoxserver.client.codec
55
public interface EzyObjectToMessage
66
{
77
EzyMessage convert(Object value);
8-
}
8+
9+
byte[] convertToMessageContent(Object value);
10+
11+
EzyMessage packToMessage(byte[] content, bool encrypted);
12+
}
913
}

codec/EzySimpleMessageHeader.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class EzySimpleMessageHeader : EzyMessageHeader
1212
protected bool rawBytes;
1313
protected bool udpHandshake;
1414

15-
public EzySimpleMessageHeader(bool bigSize,
16-
bool encrypted,
17-
bool compressed,
18-
bool text,
19-
bool rawBytes,
20-
bool udpHandshake)
15+
public EzySimpleMessageHeader(
16+
bool bigSize,
17+
bool encrypted,
18+
bool compressed,
19+
bool text,
20+
bool rawBytes,
21+
bool udpHandshake
22+
)
2123
{
2224
this.bigSize = bigSize;
2325
this.encrypted = encrypted;

codec/MsgPackByteToObjectDecoder.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,55 @@
11
using System;
22
using System.Collections.Generic;
33
using com.tvd12.ezyfoxserver.client.io;
4+
using com.tvd12.ezyfoxserver.client.security;
45
using static com.tvd12.ezyfoxserver.client.codec.EzyDecodeState;
56

67
namespace com.tvd12.ezyfoxserver.client.codec
78
{
89
public class MsgPackByteToObjectDecoder : EzyByteToObjectDecoder
910
{
1011

11-
protected Handlers handlers;
12-
protected EzyMessageDeserializer deserializer;
12+
protected readonly Handlers handlers;
13+
protected readonly EzyAesCrypt cryptor;
14+
protected readonly EzyMessageDeserializer deserializer;
1315

1416
public MsgPackByteToObjectDecoder(
15-
EzyMessageDeserializer deserializer, int maxSize)
17+
EzyMessageDeserializer deserializer,
18+
int maxSize
19+
)
1620
{
1721
this.deserializer = deserializer;
18-
this.handlers = Handlers.builder()
22+
this.cryptor = EzyAesCrypt.getDefault();
23+
this.handlers = Handlers.builder()
1924
.setMaxSize(maxSize)
2025
.build();
2126
}
2227

23-
public Object decode(EzyMessage message)
28+
public Object decode(EzyMessage message, byte[] decryptionKey)
2429
{
25-
return deserializer.deserialize<Object>(message.getContent());
30+
byte[] encryptedContent = message.getContent();
31+
if (message.getHeader().isEncrypted())
32+
{
33+
encryptedContent = this.decryptMessageContent(encryptedContent, decryptionKey);
34+
}
35+
return deserializer.deserialize<Object>(encryptedContent);
2636
}
2737

2838
public void decode(EzyByteBuffer bytes, Queue<EzyMessage> queue)
2939
{
3040
handlers.handle(bytes, queue);
3141
}
3242

33-
}
43+
protected byte[] decryptMessageContent(byte[] content, byte[] decryptionKey)
44+
{
45+
if (decryptionKey == null)
46+
{
47+
return content;
48+
}
49+
return cryptor.decrypt(content, decryptionKey);
50+
}
51+
52+
}
3453

3554
public abstract class AbstractHandler : EzyDecodeHandler
3655
{

0 commit comments

Comments
 (0)