下面列出了怎么用 io.netty.handler.codec.socks.SocksInitRequestDecoder 的API类实例代码及写法,或者点击链接到github查看源代码。
public void start() {
Configuration config = Configuration.INSTANCE;
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline()
.addLast("logging", new LoggingHandler(LogLevel.DEBUG))
.addLast(new SocksInitRequestDecoder())
.addLast(new SocksMessageEncoder())
.addLast(new Socks5Handler())
.addLast(Status.TRAFFIC_HANDLER);
}
});
log.info("\tStartup {}-{}-client [{}{}]", Constants.APP_NAME, Constants.APP_VERSION, config.getMode(), config.getMode().equals("socks5") ? "" : ":" + config.getProtocol());
new Thread(() -> new UdpServer().start()).start();
ChannelFuture future = bootstrap.bind(config.getLocalHost(), config.getLocalPort()).sync();
future.addListener(future1 -> log.info("\tTCP listening at {}:{}...", config.getLocalHost(), config.getLocalPort()));
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("\tSocket bind failure ({})", e.getMessage());
} finally {
log.info("\tShutting down");
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
@Override
public void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline p = socketChannel.pipeline();
p.addLast(new SocksInitRequestDecoder());
p.addLast(socksMessageEncoder);
p.addLast(socksServerHandler);
p.addLast(trafficHandler);
}
@Override
public void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline p = socketChannel.pipeline();
p.addLast(new SocksInitRequestDecoder());
p.addLast(socksMessageEncoder);
p.addLast(socksServerHandler);
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new SocksInitRequestDecoder()).addLast(new SocksCmdRequestDecoder()).addLast(new IdleStateHandler(this.idleRead, this.idleWrite, this.idleAll)).addLast(this.channelHandlerBuilder.build());
}