下面列出了 io.netty.handler.codec.socksx.v5.Socks5ServerEncoder #io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void configure(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
switch (testMode) {
case INTERMEDIARY:
p.addLast(DECODER, new Socks5InitialRequestDecoder());
p.addLast(ENCODER, Socks5ServerEncoder.DEFAULT);
p.addLast(new Socks5IntermediaryHandler());
break;
case TERMINAL:
p.addLast(DECODER, new Socks5InitialRequestDecoder());
p.addLast(ENCODER, Socks5ServerEncoder.DEFAULT);
p.addLast(new Socks5TerminalHandler());
break;
case UNRESPONSIVE:
p.addLast(UnresponsiveHandler.INSTANCE);
break;
}
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
final int readerIndex = in.readerIndex();
if (in.writerIndex() == readerIndex) {
return;
}
ChannelPipeline p = ctx.pipeline();
final byte versionVal = in.getByte(readerIndex);
SocksVersion version = SocksVersion.valueOf(versionVal);
switch (version) {
case SOCKS4a:
logKnownVersion(ctx, version);
p.addAfter(ctx.name(), null, Socks4ServerEncoder.INSTANCE);
p.addAfter(ctx.name(), null, new Socks4ServerDecoder());
break;
case SOCKS5:
logKnownVersion(ctx, version);
p.addAfter(ctx.name(), null, socks5encoder);
p.addAfter(ctx.name(), null, new Socks5InitialRequestDecoder());
break;
default:
logUnknownVersion(ctx, versionVal);
in.skipBytes(in.readableBytes());
ctx.close();
return;
}
p.remove(this);
}
@Override
public void handlePipeline(ChannelPipeline pipeline) {
pipeline.addLast("socks5-server-encoder", Socks5ServerEncoder.DEFAULT);
pipeline.addLast("socks5-initial-decoder", new Socks5InitialRequestDecoder());
pipeline.addLast(new Socks5ProxyHandler(messageListener, sslContextManager, proxyHandlerSupplier));
}
public void start() throws Exception {
if(proxyFlowLog == null) {
proxyFlowLog = new ProxyFlowLog4j();
}
if(passwordAuth == null) {
passwordAuth = new PropertiesPasswordAuth();
}
EventLoopGroup boss = new NioEventLoopGroup(2);
EventLoopGroup worker = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//流量统计
ch.pipeline().addLast(
ProxyChannelTrafficShapingHandler.PROXY_TRAFFIC,
new ProxyChannelTrafficShapingHandler(3000, proxyFlowLog, channelListener)
);
//channel超时处理
ch.pipeline().addLast(new IdleStateHandler(3, 30, 0));
ch.pipeline().addLast(new ProxyIdleHandler());
//netty日志
if(logging) {
ch.pipeline().addLast(new LoggingHandler());
}
//Socks5MessagByteBuf
ch.pipeline().addLast(Socks5ServerEncoder.DEFAULT);
//sock5 init
ch.pipeline().addLast(new Socks5InitialRequestDecoder());
//sock5 init
ch.pipeline().addLast(new Socks5InitialRequestHandler(ProxyServer.this));
if(isAuth()) {
//socks auth
ch.pipeline().addLast(new Socks5PasswordAuthRequestDecoder());
//socks auth
ch.pipeline().addLast(new Socks5PasswordAuthRequestHandler(getPasswordAuth()));
}
//socks connection
ch.pipeline().addLast(new Socks5CommandRequestDecoder());
//Socks connection
ch.pipeline().addLast(new Socks5CommandRequestHandler(ProxyServer.this.getBossGroup()));
}
});
ChannelFuture future = bootstrap.bind(port).sync();
logger.debug("bind port : " + port);
future.channel().closeFuture().sync();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}