Search in sources :

Example 1 with EzyHandShakeParams

use of com.tvd12.ezyfoxserver.response.EzyHandShakeParams 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();
}
Also used : EzyHandshakeEvent(com.tvd12.ezyfoxserver.event.EzyHandshakeEvent) EzyHandshakeParams(com.tvd12.ezyfoxserver.request.EzyHandshakeParams) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession)

Example 2 with EzyHandShakeParams

use of com.tvd12.ezyfoxserver.response.EzyHandShakeParams 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);
}
Also used : EzyHandShakeResponse(com.tvd12.ezyfoxserver.response.EzyHandShakeResponse) EzyHandShakeParams(com.tvd12.ezyfoxserver.response.EzyHandShakeParams)

Example 3 with EzyHandShakeParams

use of com.tvd12.ezyfoxserver.response.EzyHandShakeParams in project ezyfox-server by youngmonkeys.

the class EzyHandShakeController2Test method test.

@Test
public void test() {
    EzyServerContext ctx = newServerContext();
    EzySession first = getSessionManager(ctx).provideSession(EzyConnectionType.SOCKET);
    System.err.println("first.token:    " + first);
    System.err.println("alive sessions: " + getSessionManager(ctx).getAliveSessions());
    assertTrue(getSessionManager(ctx).containsSession(first.getId()));
    EzySession session = getSessionManager(ctx).provideSession(EzyConnectionType.SOCKET);
    System.err.println("session: " + session);
    EzyArray data = newHandShakeData(first.getToken());
    EzySimpleHandshakeRequest request = new EzySimpleHandshakeRequest();
    request.deserializeParams(data);
    request.setSession(session);
    EzyHandshakeParams requestParams = request.getParams();
    assertEquals(first.getToken(), requestParams.getReconnectToken());
    EzyHandshakeController controller = new EzyHandshakeController();
    controller.handle(ctx, request);
}
Also used : EzyHandshakeParams(com.tvd12.ezyfoxserver.request.EzyHandshakeParams) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzySimpleHandshakeRequest(com.tvd12.ezyfoxserver.request.EzySimpleHandshakeRequest) EzyHandshakeController(com.tvd12.ezyfoxserver.controller.EzyHandshakeController) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test)

Example 4 with EzyHandShakeParams

use of com.tvd12.ezyfoxserver.response.EzyHandShakeParams in project ezyfox-server by youngmonkeys.

the class EzyHandShakeControllerTest method handleSocketSSLButEventNoEncryptionTest.

@Test
public void handleSocketSSLButEventNoEncryptionTest() {
    // given
    EzyHandshakeController sut = new EzyHandshakeController();
    EzyServerContext serverContext = mock(EzyServerContext.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);
    when(params.isEnableEncryption()).thenReturn(false);
    // when
    sut.handle(serverContext, request);
    // then
    Asserts.assertNull(session.getSessionKey());
}
Also used : EzyHandShakeRequest(com.tvd12.ezyfoxserver.request.EzyHandShakeRequest) EzyHandshakeParams(com.tvd12.ezyfoxserver.request.EzyHandshakeParams) EzySocketSetting(com.tvd12.ezyfoxserver.setting.EzySocketSetting) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyHandshakeController(com.tvd12.ezyfoxserver.controller.EzyHandshakeController) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) Test(org.testng.annotations.Test)

Example 5 with EzyHandShakeParams

use of com.tvd12.ezyfoxserver.response.EzyHandShakeParams 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);
}
Also used : EzyHandshakeEvent(com.tvd12.ezyfoxserver.event.EzyHandshakeEvent) KeyPair(java.security.KeyPair) EzySocketSetting(com.tvd12.ezyfoxserver.setting.EzySocketSetting) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyHandShakeRequest(com.tvd12.ezyfoxserver.request.EzyHandShakeRequest) EzyHandshakeParams(com.tvd12.ezyfoxserver.request.EzyHandshakeParams) EzyEventType(com.tvd12.ezyfoxserver.constant.EzyEventType) EzyHandshakeController(com.tvd12.ezyfoxserver.controller.EzyHandshakeController) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) Test(org.testng.annotations.Test)

Aggregations

EzySession (com.tvd12.ezyfoxserver.entity.EzySession)8 EzyHandshakeParams (com.tvd12.ezyfoxserver.request.EzyHandshakeParams)8 Test (org.testng.annotations.Test)8 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)7 EzyHandshakeController (com.tvd12.ezyfoxserver.controller.EzyHandshakeController)7 EzyHandShakeRequest (com.tvd12.ezyfoxserver.request.EzyHandShakeRequest)6 EzyServer (com.tvd12.ezyfoxserver.EzyServer)5 EzySettings (com.tvd12.ezyfoxserver.setting.EzySettings)5 EzySocketSetting (com.tvd12.ezyfoxserver.setting.EzySocketSetting)5 EzyHandshakeEvent (com.tvd12.ezyfoxserver.event.EzyHandshakeEvent)2 EzyHandShakeParams (com.tvd12.ezyfoxserver.response.EzyHandShakeParams)2 EzyHandShakeResponse (com.tvd12.ezyfoxserver.response.EzyHandShakeResponse)2 KeyPair (java.security.KeyPair)2 EzyArray (com.tvd12.ezyfox.entity.EzyArray)1 EzyEventType (com.tvd12.ezyfoxserver.constant.EzyEventType)1 EzySimpleHandshakeRequest (com.tvd12.ezyfoxserver.request.EzySimpleHandshakeRequest)1 EzyResponse (com.tvd12.ezyfoxserver.response.EzyResponse)1 BaseTest (com.tvd12.test.base.BaseTest)1