下面列出了怎么用 io.netty.handler.codec.socksx.v5.DefaultSocks5InitialRequest 的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void channelRead0(ChannelHandlerContext ctx, Socks5Message msg) throws Exception {
if (msg instanceof DefaultSocks5InitialRequest) {
ctx.pipeline().addBefore(ctx.name(), Socks5CommandRequestDecoder.class.getName(),
new Socks5CommandRequestDecoder());
ctx.writeAndFlush(new DefaultSocks5InitialResponse(Socks5AuthMethod.NO_AUTH));
} else if (msg instanceof DefaultSocks5CommandRequest) {
final DefaultSocks5CommandRequest req = (DefaultSocks5CommandRequest) msg;
ctx.pipeline().remove(Socks5CommandRequestDecoder.class);
ctx.fireUserEventTriggered(new ProxySuccessEvent(
new InetSocketAddress(req.dstAddr(), req.dstPort()),
new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS,
Socks5AddressType.IPv4)));
} else {
throw new IllegalStateException("unexpected msg: " + msg);
}
}
@Override
protected Future<?> execute() {
List<Socks5AuthMethod> authMethods = new ArrayList<>(2);
authMethods.add(Socks5AuthMethod.NO_AUTH);
if ((username != null) || (password != null)) {
authMethods.add(Socks5AuthMethod.PASSWORD);
}
DefaultSocks5InitialRequest initialRequest = new DefaultSocks5InitialRequest(authMethods);
addFirstOrReplaceHandler(SOCKS_ENCODER_NAME, Socks5ClientEncoder.DEFAULT);
addFirstOrReplaceHandler(SOCKS_DECODER_NAME, new Socks5InitialResponseDecoder());
return writeToChannel(initialRequest);
}