Skip to content

Commit

Permalink
Merge pull request #30 from youngmonkeys/master
Browse files Browse the repository at this point in the history
add ssl encryption
  • Loading branch information
vu-luong authored Aug 4, 2023
2 parents c6e81d4 + bb75ca9 commit b9a7895
Show file tree
Hide file tree
Showing 40 changed files with 809 additions and 167 deletions.
28 changes: 23 additions & 5 deletions EzyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,32 @@ public interface EzyClient

bool reconnect();

void send(EzyRequest request);
void send(EzyRequest request, bool encrypted = false);

void send(EzyCommand cmd, EzyArray data);
void send(EzyCommand cmd, EzyArray data, bool encrypted = false);

void disconnect(int reason = (int)EzyDisconnectReason.CLOSE);
void disconnect(int reason = (int)EzyDisconnectReason.CLOSE);

void close();

void processEvents();

void udpConnect(int port);

void udpConnect(String host, int port);

void udpSend(EzyRequest request);
void udpSend(EzyRequest request, bool encrypted = false);

void udpSend(EzyCommand cmd, EzyArray data);
void udpSend(EzyCommand cmd, EzyArray data, bool encrypted = false);

String getName();

EzyClientConfig getConfig();

bool isEnableSSL();

bool isEnableDebug();

EzyUser getMe();

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

void setSessionToken(String token);

void setSessionKey(byte[] sessionKey);

byte[] getSessionKey();

void setPrivateKey(byte[] privateKey);

byte[] getPrivateKey();

void setPublicKey(byte[] publicKey);

byte[] getPublicKey();

EzyISocketClient getSocket();

EzyApp getApp();
Expand Down
97 changes: 84 additions & 13 deletions EzyTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class EzyTcpClient :
protected EzyUser me;
protected EzyZone zone;
protected long sessionId;
protected byte[] publicKey;
protected byte[] privateKey;
protected byte[] sessionKey;
protected String sessionToken;
protected readonly String name;
protected readonly EzySetup settingUp;
Expand Down Expand Up @@ -127,8 +130,10 @@ public bool reconnect()
}
preconnect();
bool success = socketClient.reconnect();
if (success)
setStatus(EzyConnectionStatus.RECONNECTING);
if (success)
{
setStatus(EzyConnectionStatus.RECONNECTING);
}
return success;
}

Expand All @@ -143,24 +148,46 @@ public void disconnect(int reason)
socketClient.disconnect(reason);
}

public void send(EzyRequest request)
public void close()
{
disconnect((int) EzyDisconnectReason.CLOSE);
}

public void send(EzyRequest request, bool encrypted)
{
Object cmd = request.getCommand();
EzyData data = request.serialize();
send((EzyCommand)cmd, (EzyArray)data);
send((EzyCommand)cmd, (EzyArray)data, encrypted);
}

public void send(EzyCommand cmd, EzyArray data)
{
public void send(EzyCommand cmd, EzyArray data, bool encrypted)
{
bool shouldEncrypted = encrypted;
if (encrypted && sessionKey == null)
{
if (config.isEnableDebug())
{
shouldEncrypted = false;
}
else
{
throw new ArgumentException(
"can not send command: " + cmd + " " +
"you must enable SSL or enable debug mode by configuration " +
"when you create the client"
);
}

}
EzyArray array = requestSerializer.serialize(cmd, data);
if(socketClient != null)
if (socketClient != null)
{
socketClient.sendMessage(array);
socketClient.sendMessage(array, shouldEncrypted);
printSentData(cmd, data);
}
}
}

public void processEvents()
public void processEvents()
{
socketClient.processEventMessages();
}
Expand All @@ -175,7 +202,17 @@ public EzyClientConfig getConfig()
return config;
}

public EzyZone getZone()
public bool isEnableSSL()
{
return config.isEnableSSL();
}

public bool isEnableDebug()
{
return config.isEnableDebug();
}

public EzyZone getZone()
{
return zone;
}
Expand Down Expand Up @@ -237,6 +274,38 @@ public void setSessionToken(String token)
this.socketClient.setSessionToken(sessionToken);
}

public void setSessionKey(byte[] sessionKey)
{
this.sessionKey = sessionKey;
this.socketClient.setSessionKey(sessionKey);
}

public byte[] getSessionKey()
{
return sessionKey;
}

public void setPublicKey(byte[] publicKey)
{
this.publicKey = publicKey;
}

public byte[] getPublicKey()
{
return publicKey;
}

public void setPrivateKey(byte[] privateKey)
{
this.privateKey = privateKey;
}

public byte[] getPrivateKey()
{
return privateKey;
}


public EzyISocketClient getSocket()
{
return socketClient;
Expand Down Expand Up @@ -303,7 +372,9 @@ public EzyHandlerManager getHandlerManager()
protected void printSentData(EzyCommand cmd, EzyArray data)
{
if (!unloggableCommands.Contains(cmd))
{
logger.debug("send command: " + cmd + " and data: " + data);
}
}

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

public virtual void udpSend(EzyRequest request)
public virtual void udpSend(EzyRequest request, bool encrypted)
{
throw new InvalidOperationException("only support TCP, use EzyUTClient instead");
}

public virtual void udpSend(EzyCommand cmd, EzyArray data)
public virtual void udpSend(EzyCommand cmd, EzyArray data, bool encrypted)
{
throw new InvalidOperationException("only support TCP, use EzyUTClient instead");
}
Expand Down
25 changes: 21 additions & 4 deletions EzyUTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,36 @@ public override void udpConnect(String host, int port)
((EzyUTSocketClient)socketClient).udpConnect(host, port);
}

public override void udpSend(EzyRequest request)
public override void udpSend(EzyRequest request, bool encrypted)
{
Object cmd = request.getCommand();
EzyData data = request.serialize();
send((EzyCommand)cmd, (EzyArray)data);
udpSend((EzyCommand)cmd, (EzyArray)data, encrypted);
}

public override void udpSend(EzyCommand cmd, EzyArray data)
public override void udpSend(EzyCommand cmd, EzyArray data, bool encrypted)
{
bool shouldEncrypted = encrypted;
if (encrypted && sessionKey == null)
{
if (config.isEnableDebug())
{
shouldEncrypted = false;
}
else
{
throw new ArgumentException(
"can not send command: " + cmd + " " +
"you must enable SSL or enable debug mode by configuration " +
"when you create the client"
);
}

}
EzyArray array = requestSerializer.serialize(cmd, data);
if (socketClient != null)
{
((EzyUTSocketClient)socketClient).udpSendMessage(array);
((EzyUTSocketClient)socketClient).udpSendMessage(array, shouldEncrypted);
printSentData(cmd, data);
}
}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ csharp, unity client for ezyfox server

csharp, unity client for ezyfox server

# For Unity

Please move to the [unity-client branch](https://github.com/youngmonkeys/ezyfox-server-csharp-client/tree/unity-client).

# Documentation

[https://youngmonkeys.org/ezyfox-csharp-client-sdk/](https://youngmonkeys.org/ezyfox-csharp-client-sdk/)
Expand Down
8 changes: 7 additions & 1 deletion builder/EzyArrayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public EzyArrayBuilder append(Object value)
return this;
}

public EzyArrayBuilder append<T>(EzyBuilder<T> builder)
public EzyArrayBuilder append(byte[] value)
{
product.add(value);
return this;
}

public EzyArrayBuilder append<T>(EzyBuilder<T> builder)
{
product.add(builder);
return this;
Expand Down
2 changes: 1 addition & 1 deletion codec/EzyByteToObjectDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace com.tvd12.ezyfoxserver.client.codec

public interface EzyByteToObjectDecoder
{
Object decode(EzyMessage message);
Object decode(EzyMessage message, byte[] decryptionKey);

void decode(EzyByteBuffer bytes, Queue<EzyMessage> queue);
}
Expand Down
6 changes: 5 additions & 1 deletion codec/EzyObjectToByteEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ namespace com.tvd12.ezyfoxserver.client.codec
public interface EzyObjectToByteEncoder
{
byte[] encode(Object msg);
}

byte[] toMessageContent(Object data);

byte[] encryptMessageContent(byte[] messageContent, byte[] encryptionKey);
}
}
6 changes: 5 additions & 1 deletion codec/EzyObjectToMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ namespace com.tvd12.ezyfoxserver.client.codec
public interface EzyObjectToMessage
{
EzyMessage convert(Object value);
}

byte[] convertToMessageContent(Object value);

EzyMessage packToMessage(byte[] content, bool encrypted);
}
}
14 changes: 8 additions & 6 deletions codec/EzySimpleMessageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class EzySimpleMessageHeader : EzyMessageHeader
protected bool rawBytes;
protected bool udpHandshake;

public EzySimpleMessageHeader(bool bigSize,
bool encrypted,
bool compressed,
bool text,
bool rawBytes,
bool udpHandshake)
public EzySimpleMessageHeader(
bool bigSize,
bool encrypted,
bool compressed,
bool text,
bool rawBytes,
bool udpHandshake
)
{
this.bigSize = bigSize;
this.encrypted = encrypted;
Expand Down
33 changes: 26 additions & 7 deletions codec/MsgPackByteToObjectDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
using System;
using System.Collections.Generic;
using com.tvd12.ezyfoxserver.client.io;
using com.tvd12.ezyfoxserver.client.security;
using static com.tvd12.ezyfoxserver.client.codec.EzyDecodeState;

namespace com.tvd12.ezyfoxserver.client.codec
{
public class MsgPackByteToObjectDecoder : EzyByteToObjectDecoder
{

protected Handlers handlers;
protected EzyMessageDeserializer deserializer;
protected readonly Handlers handlers;
protected readonly EzyAesCrypt cryptor;
protected readonly EzyMessageDeserializer deserializer;

public MsgPackByteToObjectDecoder(
EzyMessageDeserializer deserializer, int maxSize)
EzyMessageDeserializer deserializer,
int maxSize
)
{
this.deserializer = deserializer;
this.handlers = Handlers.builder()
this.cryptor = EzyAesCrypt.getDefault();
this.handlers = Handlers.builder()
.setMaxSize(maxSize)
.build();
}

public Object decode(EzyMessage message)
public Object decode(EzyMessage message, byte[] decryptionKey)
{
return deserializer.deserialize<Object>(message.getContent());
byte[] encryptedContent = message.getContent();
if (message.getHeader().isEncrypted())
{
encryptedContent = this.decryptMessageContent(encryptedContent, decryptionKey);
}
return deserializer.deserialize<Object>(encryptedContent);
}

public void decode(EzyByteBuffer bytes, Queue<EzyMessage> queue)
{
handlers.handle(bytes, queue);
}

}
protected byte[] decryptMessageContent(byte[] content, byte[] decryptionKey)
{
if (decryptionKey == null)
{
return content;
}
return cryptor.decrypt(content, decryptionKey);
}

}

public abstract class AbstractHandler : EzyDecodeHandler
{
Expand Down
Loading

0 comments on commit b9a7895

Please sign in to comment.