下面列出了怎么用 io.netty.handler.codec.socksx.v5.Socks5ServerEncoder 的API类实例代码及写法,或者点击链接到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;
}
}
/**
* Creates a new instance with the specified {@link Socks5ServerEncoder}.
* This constructor is useful when a user wants to use an alternative {@link Socks5AddressEncoder}.
*/
public SocksPortUnificationServerHandler(Socks5ServerEncoder socks5encoder) {
if (socks5encoder == null) {
throw new NullPointerException("socks5encoder");
}
this.socks5encoder = socks5encoder;
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
LOGGER.info("{} : handlerAdded", connectionInfo.toString(true));
Socks5ServerEncoder socks5ServerEncoder = new Socks5ServerEncoder(Socks5AddressEncoder.DEFAULT);
SocksPortUnificationServerHandler socksPortUnificationServerHandler = new SocksPortUnificationServerHandler(socks5ServerEncoder);
ctx.pipeline().addBefore(ctx.name(), null, socksPortUnificationServerHandler);
}
/**
* Creates a new instance with the default configuration.
*/
public SocksPortUnificationServerHandler() {
this(Socks5ServerEncoder.DEFAULT);
}
@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();
}
}