use of org.mockserver.netty.proxy.socks.Socks5ProxyHandler in project mockserver by mock-server.
the class HttpProxyUnificationInitializerSOCKSErrorTest method shouldHandleErrorsDuringSOCKSConnection.
@Test
public void shouldHandleErrorsDuringSOCKSConnection() throws DecoderException {
// given - embedded channel
short localPort = 1234;
final LifeCycle lifeCycle = mock(LifeCycle.class);
when(lifeCycle.getScheduler()).thenReturn(mock(Scheduler.class));
EmbeddedChannel embeddedChannel = new EmbeddedChannel(new MockServerUnificationInitializer(configuration(), lifeCycle, new HttpState(configuration(), new MockServerLogger(), mock(Scheduler.class)), mock(HttpActionHandler.class), null));
// and - no SOCKS handlers
assertThat(embeddedChannel.pipeline().get(Socks5ProxyHandler.class), is(nullValue()));
assertThat(embeddedChannel.pipeline().get(SocksMessageEncoder.class), is(nullValue()));
assertThat(embeddedChannel.pipeline().get(SocksInitRequestDecoder.class), is(nullValue()));
// when - SOCKS INIT message
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(new byte[] { // SOCKS5
(byte) 0x05, // 1 authentication method
(byte) 0x02, // NO_AUTH
(byte) 0x00, // AUTH_PASSWORD
(byte) 0x02 }));
// then - INIT response
assertThat(ByteBufUtil.hexDump((ByteBuf) embeddedChannel.readOutbound()), is(Hex.encodeHexString(new byte[] { // SOCKS5
(byte) 0x05, // NO_AUTH
(byte) 0x00 })));
// and then - should add SOCKS handlers first
if (MockServerLogger.isEnabled(TRACE)) {
assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("LoggingHandler#0", "Socks5CommandRequestDecoder#0", "Socks5ServerEncoder#0", "Socks5ProxyHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
} else {
assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("Socks5CommandRequestDecoder#0", "Socks5ServerEncoder#0", "Socks5ProxyHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
}
// and when - SOCKS CONNECT command
String portInHex = Strings.padStart(BigInteger.valueOf(localPort).toString(16), 4, '0');
byte[] ipAddressInBytes = NetUtil.createByteArrayFromIpAddressString("127.0.0.1");
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(Bytes.concat(new byte[] { // SOCKS5
(byte) 0x05, // command type CONNECT
(byte) 0x01, // reserved (must be 0x00)
(byte) 0x00, // address type IPv4
(byte) 0x01 }, // ip address
ipAddressInBytes, // port
Hex.decodeHex(portInHex))));
// then - CONNECT response
byte[] domainInBytes = "127.0.0.1".getBytes(CharsetUtil.US_ASCII);
String dominLegnthAndBytes = Strings.padStart(BigInteger.valueOf(domainInBytes.length).toString(16), 2, '0') + new BigInteger(domainInBytes).toString(16);
assertThat(ByteBufUtil.hexDump((ByteBuf) embeddedChannel.readOutbound()), is(Hex.encodeHexString(new byte[] { // SOCKS5
(byte) 0x05, // general failure (caused by connection failure)
(byte) 0x01, // reserved (must be 0x00)
(byte) 0x00, // address type domain
(byte) 0x03 }) + // ip address
dominLegnthAndBytes + // port
portInHex));
// then - channel is closed after error
assertThat(embeddedChannel.isOpen(), is(false));
}
use of org.mockserver.netty.proxy.socks.Socks5ProxyHandler in project mockserver by mock-server.
the class DirectProxyUnificationHandlerTest method shouldSwitchToSOCKS.
@Test
public void shouldSwitchToSOCKS() {
// given - embedded channel
EmbeddedChannel embeddedChannel = new EmbeddedChannel(new MockServerUnificationInitializer(configuration(), mock(LifeCycle.class), new HttpState(configuration(), new MockServerLogger(), mock(Scheduler.class)), mock(HttpActionHandler.class), null));
// and - no SOCKS handlers
assertThat(embeddedChannel.pipeline().get(Socks5ProxyHandler.class), is(nullValue()));
assertThat(embeddedChannel.pipeline().get(SocksMessageEncoder.class), is(nullValue()));
assertThat(embeddedChannel.pipeline().get(SocksInitRequestDecoder.class), is(nullValue()));
// when - SOCKS INIT message
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(new byte[] { // SOCKS5
(byte) 0x05, // 1 authentication method
(byte) 0x02, // NO_AUTH
(byte) 0x00, // AUTH_PASSWORD
(byte) 0x02 }));
// then - INIT response
assertThat(ByteBufUtil.hexDump((ByteBuf) embeddedChannel.readOutbound()), is(Hex.encodeHexString(new byte[] { // SOCKS5
(byte) 0x05, // NO_AUTH
(byte) 0x00 })));
// and then - should add SOCKS handlers first
if (MockServerLogger.isEnabled(TRACE)) {
assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("LoggingHandler#0", "Socks5CommandRequestDecoder#0", "Socks5ServerEncoder#0", "Socks5ProxyHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
} else {
assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("Socks5CommandRequestDecoder#0", "Socks5ServerEncoder#0", "Socks5ProxyHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
}
}
use of org.mockserver.netty.proxy.socks.Socks5ProxyHandler in project mockserver by mock-server.
the class HttpProxyUnificationInitializerTest method shouldSwitchToSOCKS.
@Test
public void shouldSwitchToSOCKS() {
// given - embedded channel
EmbeddedChannel embeddedChannel = new EmbeddedChannel(new MockServerUnificationInitializer(configuration(), mock(LifeCycle.class), new HttpState(configuration(), new MockServerLogger(), mock(Scheduler.class)), mock(HttpActionHandler.class), null));
// and - no SOCKS handlers
assertThat(embeddedChannel.pipeline().get(Socks5ProxyHandler.class), is(nullValue()));
assertThat(embeddedChannel.pipeline().get(SocksMessageEncoder.class), is(nullValue()));
assertThat(embeddedChannel.pipeline().get(SocksInitRequestDecoder.class), is(nullValue()));
// when - SOCKS INIT message
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(new byte[] { // SOCKS5
(byte) 0x05, // 1 authentication method
(byte) 0x02, // NO_AUTH
(byte) 0x00, // AUTH_PASSWORD
(byte) 0x02 }));
// then - INIT response
assertThat(ByteBufUtil.hexDump((ByteBuf) embeddedChannel.readOutbound()), is(Hex.encodeHexString(new byte[] { // SOCKS5
(byte) 0x05, // NO_AUTH
(byte) 0x00 })));
// and then - should add SOCKS handlers first
assertThat(String.valueOf(embeddedChannel.pipeline().names()), embeddedChannel.pipeline().names(), contains("Socks5CommandRequestDecoder#0", "Socks5ServerEncoder#0", "Socks5ProxyHandler#0", "PortUnificationHandler#0", "DefaultChannelPipeline$TailContext#0"));
}
Aggregations