use of com.tvd12.ezyfoxserver.event.EzyHandshakeEvent in project ezyfox-server by youngmonkeys.
the class EzyHandshakeController method handle.
@SuppressWarnings("AbbreviationAsWordInName")
@Override
public void handle(EzyServerContext ctx, EzyHandShakeRequest request) {
EzySession session = request.getSession();
EzyHandshakeParams params = request.getParams();
EzyHandshakeEvent event = newHandshakeEvent(session, params);
ctx.handleEvent(EzyEventType.CLIENT_HANDSHAKE, event);
handleSocketSSL(ctx, event);
updateSession(session, event);
EzyResponse response = newHandShakeResponse(session, event);
ctx.send(response, session, false);
event.release();
}
use of com.tvd12.ezyfoxserver.event.EzyHandshakeEvent in project ezyfox-server by youngmonkeys.
the class EzyHandshakeController method newHandShakeResponse.
protected EzyResponse newHandShakeResponse(EzySession session, EzyHandshakeEvent event) {
EzyHandShakeParams params = new EzyHandShakeParams();
params.setServerPublicKey(session.getPublicKey());
params.setReconnectToken(session.getToken());
params.setSessionId(session.getId());
params.setSessionKey(event.getEncryptedSessionKey());
return new EzyHandShakeResponse(params);
}
use of com.tvd12.ezyfoxserver.event.EzyHandshakeEvent in project ezyfox-server by youngmonkeys.
the class EzyHandShakeControllerTest method handleSocketSSLButSessionKeyNotNullTest.
@Test
public void handleSocketSSLButSessionKeyNotNullTest() {
// given
EzyHandshakeController sut = new EzyHandshakeController();
byte[] sessionKey = RandomUtil.randomShortAlphabetString().getBytes();
byte[] encryptedSessionKey = RandomUtil.randomShortAlphabetString().getBytes();
EzyServerContext serverContext = mock(EzyServerContext.class);
doAnswer(it -> {
EzyHandshakeEvent event = it.getArgumentAt(1, EzyHandshakeEvent.class);
event.setSessionKey(sessionKey);
event.setEncryptedSessionKey(encryptedSessionKey);
return null;
}).when(serverContext).handleEvent(any(EzyEventType.class), any(EzyHandshakeEvent.class));
EzyHandShakeRequest request = mock(EzyHandShakeRequest.class);
EzyHandshakeParams params = mock(EzyHandshakeParams.class);
when(request.getParams()).thenReturn(params);
EzySession session = spy(EzyAbstractSession.class);
when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
when(request.getSession()).thenReturn(session);
EzyServer server = mock(EzyServer.class);
EzySettings settings = mock(EzySettings.class);
EzySocketSetting socketSetting = mock(EzySocketSetting.class);
when(settings.getSocket()).thenReturn(socketSetting);
when(socketSetting.isSslActive()).thenReturn(true);
when(serverContext.getServer()).thenReturn(server);
when(server.getSettings()).thenReturn(settings);
String clientId = RandomUtil.randomShortHexString();
String clientType = RandomUtil.randomShortAlphabetString();
String clientVersion = RandomUtil.randomShortAlphabetString();
String reconnectToken = RandomUtil.randomShortHexString();
KeyPair keyPair = EzyKeysGenerator.builder().build().generate();
byte[] clientKey = keyPair.getPublic().getEncoded();
when(params.getClientId()).thenReturn(clientId);
when(params.getClientKey()).thenReturn(clientKey);
when(params.getClientType()).thenReturn(clientType);
when(params.getClientVersion()).thenReturn(clientVersion);
when(params.getReconnectToken()).thenReturn(reconnectToken);
when(params.isEnableEncryption()).thenReturn(true);
// when
sut.handle(serverContext, request);
// then
verify(serverContext, times(1)).handleEvent(any(EzyEventType.class), any(EzyHandshakeEvent.class));
verify(serverContext, times(1)).send(any(com.tvd12.ezyfoxserver.response.EzyResponse.class), any(EzySession.class), any(boolean.class));
verify(session, times(1)).setClientId(clientId);
verify(session, times(1)).setClientKey(clientKey);
verify(session, times(1)).setClientType(clientType);
verify(session, times(1)).setClientVersion(clientVersion);
verify(session, times(1)).setSessionKey(sessionKey);
}
use of com.tvd12.ezyfoxserver.event.EzyHandshakeEvent in project ezyfox-server by youngmonkeys.
the class EzyHandshakeController method handleSocketSSL.
@SuppressWarnings("AbbreviationAsWordInName")
protected void handleSocketSSL(EzyServerContext ctx, EzyHandshakeEvent event) {
EzySession session = event.getSession();
if (session.getConnectionType() == EzyConnectionType.WEBSOCKET) {
return;
}
boolean enableSSL = ctx.getServer().getSettings().getSocket().isSslActive();
if (!enableSSL) {
return;
}
if (!event.isEnableEncryption()) {
return;
}
byte[] clientKey = event.getClientKey();
byte[] sessionKey = event.getSessionKey();
if (sessionKey == null) {
sessionKey = EzyAesCrypt.randomKey();
event.setSessionKey(sessionKey);
}
byte[] encryptedSessionKey = event.getEncryptedSessionKey();
if (encryptedSessionKey == null) {
encryptedSessionKey = sessionKey;
try {
if (clientKey.length > 0) {
encryptedSessionKey = EzyAsyCrypt.builder().publicKey(clientKey).build().encrypt(sessionKey);
}
} catch (Exception e) {
logger.debug("cannot encrypt session key for session: {}", session, e);
}
event.setEncryptedSessionKey(encryptedSessionKey);
}
}
Aggregations