@Test public void messageReceivedOpenedInvalid() throws Exception { when(session.getAttribute(Socks5AcceptorHandler.STATE_KEY)).thenReturn(ProxyState.OPENED); IoBuffer buffer = IoBuffer.wrap(new byte[] {1,2,3,4}); handler.messageReceived(session, buffer); verify(session).close(false); }
public void messageReceived(final IoSession session, Object message) throws Exception { IoBuffer buffer = (IoBuffer) message; ProxyState state = getState(session); try { if (state == ProxyState.OPENED) { OpeningPacket packet = new OpeningPacket(buffer); if (packet.isAuthSupported()) { setState(session, ProxyState.INITIATED); session.write(packet.createResponse()); log.info("SOCKS5 session initiated"); } else { session.write(packet.createResponse()); session.close(false); log.info("SOCKS5 session closed"); } } else if (state == ProxyState.INITIATED) { InitiatingPacket packet = new InitiatingPacket(buffer); session.write(packet.createResponse()); setState(session, ProxyState.CONNECTED); String hash = packet.getAddress(); Socks5Pair pair = connections.register(hash, session); session.setAttribute(PAIR_KEY, pair); } else { Socks5Pair pair = getPair(session); if (pair != null && pair.isActivated()) { pair.getOther(session).write(message); } else { pair.close(); } } } catch (IllegalArgumentException e) { session.close(false); log.info("SOCKS5 session closed"); } }
Socks5AcceptorHandler extends IoHandlerAdapter { public void messageReceived(final IoSession session, Object message) throws Exception { IoBuffer buffer = (IoBuffer) message; ProxyState state = getState(session); try { if (state == ProxyState.OPENED) { OpeningPacket packet = new OpeningPacket(buffer); if (packet.isAuthSupported()) { setState(session, ProxyState.INITIATED); session.write(packet.createResponse()); log.info("SOCKS5 session initiated"); } else { session.write(packet.createResponse()); session.close(false); log.info("SOCKS5 session closed"); } } else if (state == ProxyState.INITIATED) { InitiatingPacket packet = new InitiatingPacket(buffer); session.write(packet.createResponse()); setState(session, ProxyState.CONNECTED); String hash = packet.getAddress(); Socks5Pair pair = connections.register(hash, session); session.setAttribute(PAIR_KEY, pair); } else { Socks5Pair pair = getPair(session); if (pair != null && pair.isActivated()) { pair.getOther(session).write(message); } else { pair.close(); } } } catch (IllegalArgumentException e) { session.close(false); log.info("SOCKS5 session closed"); } } }
Socks5AcceptorHandler extends IoHandlerAdapter { public void messageReceived(final IoSession session, Object message) throws Exception { IoBuffer buffer = (IoBuffer) message; ProxyState state = getState(session); try { if (state == ProxyState.OPENED) { OpeningPacket packet = new OpeningPacket(buffer); if (packet.isAuthSupported()) { setState(session, ProxyState.INITIATED); session.write(packet.createResponse()); log.info("SOCKS5 session initiated"); } else { session.write(packet.createResponse()); session.close(false); log.info("SOCKS5 session closed"); } } else if (state == ProxyState.INITIATED) { InitiatingPacket packet = new InitiatingPacket(buffer); session.write(packet.createResponse()); setState(session, ProxyState.CONNECTED); String hash = packet.getAddress(); Socks5Pair pair = connections.register(hash, session); session.setAttribute(PAIR_KEY, pair); } else { Socks5Pair pair = getPair(session); if (pair != null && pair.isActivated()) { pair.getOther(session).write(message); } else { pair.close(); } } } catch (IllegalArgumentException e) { session.close(false); log.info("SOCKS5 session closed"); } } Socks5AcceptorHandler(Socks5ConnectionsRegistry connections); }
Socks5AcceptorHandler extends IoHandlerAdapter { public void messageReceived(final IoSession session, Object message) throws Exception { IoBuffer buffer = (IoBuffer) message; ProxyState state = getState(session); try { if (state == ProxyState.OPENED) { OpeningPacket packet = new OpeningPacket(buffer); if (packet.isAuthSupported()) { setState(session, ProxyState.INITIATED); session.write(packet.createResponse()); log.info("SOCKS5 session initiated"); } else { session.write(packet.createResponse()); session.close(false); log.info("SOCKS5 session closed"); } } else if (state == ProxyState.INITIATED) { InitiatingPacket packet = new InitiatingPacket(buffer); session.write(packet.createResponse()); setState(session, ProxyState.CONNECTED); String hash = packet.getAddress(); Socks5Pair pair = connections.register(hash, session); session.setAttribute(PAIR_KEY, pair); } else { Socks5Pair pair = getPair(session); if (pair != null && pair.isActivated()) { pair.getOther(session).write(message); } else { pair.close(); } } } catch (IllegalArgumentException e) { session.close(false); log.info("SOCKS5 session closed"); } } Socks5AcceptorHandler(Socks5ConnectionsRegistry connections); void sessionOpened(IoSession session); void sessionIdle(IoSession session, IdleStatus status); void messageReceived(final IoSession session, Object message); void sessionClosed(IoSession session); void exceptionCaught(IoSession session, Throwable cause); }
Socks5AcceptorHandler extends IoHandlerAdapter { public void messageReceived(final IoSession session, Object message) throws Exception { IoBuffer buffer = (IoBuffer) message; ProxyState state = getState(session); try { if (state == ProxyState.OPENED) { OpeningPacket packet = new OpeningPacket(buffer); if (packet.isAuthSupported()) { setState(session, ProxyState.INITIATED); session.write(packet.createResponse()); log.info("SOCKS5 session initiated"); } else { session.write(packet.createResponse()); session.close(false); log.info("SOCKS5 session closed"); } } else if (state == ProxyState.INITIATED) { InitiatingPacket packet = new InitiatingPacket(buffer); session.write(packet.createResponse()); setState(session, ProxyState.CONNECTED); String hash = packet.getAddress(); Socks5Pair pair = connections.register(hash, session); session.setAttribute(PAIR_KEY, pair); } else { Socks5Pair pair = getPair(session); if (pair != null && pair.isActivated()) { pair.getOther(session).write(message); } else { pair.close(); } } } catch (IllegalArgumentException e) { session.close(false); log.info("SOCKS5 session closed"); } } Socks5AcceptorHandler(Socks5ConnectionsRegistry connections); void sessionOpened(IoSession session); void sessionIdle(IoSession session, IdleStatus status); void messageReceived(final IoSession session, Object message); void sessionClosed(IoSession session); void exceptionCaught(IoSession session, Throwable cause); static final AttributeKey STATE_KEY; static final AttributeKey PAIR_KEY; }