master
fengfei 7 years ago
parent 6b93b96baa
commit 303f11948b
  1. 2
      proxy-client/src/main/java/org/fengfei/lanproxy/client/ProxyClientContainer.java
  2. 5
      proxy-protocol/src/main/java/org/fengfei/lanproxy/protocol/IdleCheckHandler.java
  3. 25
      proxy-server/src/main/java/org/fengfei/lanproxy/server/ProxyServerContainer.java

@ -84,7 +84,7 @@ public class ProxyClientContainer implements Container, ChannelStatusListener {
} }
ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP)); ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP));
ch.pipeline().addLast(new ProxyMessageEncoder()); ch.pipeline().addLast(new ProxyMessageEncoder());
ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME, 0)); ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME - 10, 0));
ch.pipeline().addLast(new ClientChannelHandler(realServerBootstrap, bootstrap, ProxyClientContainer.this)); ch.pipeline().addLast(new ClientChannelHandler(realServerBootstrap, bootstrap, ProxyClientContainer.this));
} }
}); });

@ -30,11 +30,6 @@ public class IdleCheckHandler extends IdleStateHandler {
@Override @Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception { protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
// 正在使用中的连接不处理
if (ctx.channel().attr(Constants.NEXT_CHANNEL) != null) {
return;
}
if (IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT == evt) { if (IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT == evt) {
logger.debug("channel write timeout {}", ctx.channel()); logger.debug("channel write timeout {}", ctx.channel());
ProxyMessage proxyMessage = new ProxyMessage(); ProxyMessage proxyMessage = new ProxyMessage();

@ -64,16 +64,13 @@ public class ProxyServerContainer implements Container, ConfigChangedListener {
@Override @Override
public void start() { public void start() {
ServerBootstrap bootstrap = new ServerBootstrap(); ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class) bootstrap.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP));
LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP));
ch.pipeline().addLast(new ProxyMessageEncoder()); ch.pipeline().addLast(new ProxyMessageEncoder());
ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME, 0));
IdleCheckHandler.WRITE_IDLE_TIME, 0));
ch.pipeline().addLast(new ServerChannelHandler()); ch.pipeline().addLast(new ServerChannelHandler());
} }
}); });
@ -97,20 +94,16 @@ public class ProxyServerContainer implements Container, ConfigChangedListener {
private void initializeSSLTCPTransport(String host, int port, final SSLContext sslContext) { private void initializeSSLTCPTransport(String host, int port, final SSLContext sslContext) {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
b.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class) b.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
try { try {
pipeline.addLast("ssl", createSslHandler(sslContext, pipeline.addLast("ssl", createSslHandler(sslContext, Config.getInstance().getBooleanValue("server.ssl.needsClientAuth", false)));
Config.getInstance().getBooleanValue("server.ssl.needsClientAuth", false))); ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP));
ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET,
LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP));
ch.pipeline().addLast(new ProxyMessageEncoder()); ch.pipeline().addLast(new ProxyMessageEncoder());
ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME, 0));
IdleCheckHandler.WRITE_IDLE_TIME, 0));
ch.pipeline().addLast(new ServerChannelHandler()); ch.pipeline().addLast(new ServerChannelHandler());
} catch (Throwable th) { } catch (Throwable th) {
logger.error("Severe error during pipeline creation", th); logger.error("Severe error during pipeline creation", th);
@ -131,12 +124,10 @@ public class ProxyServerContainer implements Container, ConfigChangedListener {
private void startUserPort() { private void startUserPort() {
ServerBootstrap bootstrap = new ServerBootstrap(); ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class) bootstrap.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.USER_CHANNEL_READ_IDLE_TIME, 0, 0));
ch.pipeline().addFirst(new BytesMetricsHandler()); ch.pipeline().addFirst(new BytesMetricsHandler());
ch.pipeline().addLast(new UserChannelHandler()); ch.pipeline().addLast(new UserChannelHandler());
} }

Loading…
Cancel
Save