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. 79
      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,19 +64,16 @@ 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, LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP));
ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, ch.pipeline().addLast(new ProxyMessageEncoder());
LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP)); ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME, 0));
ch.pipeline().addLast(new ProxyMessageEncoder()); ch.pipeline().addLast(new ServerChannelHandler());
ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, }
IdleCheckHandler.WRITE_IDLE_TIME, 0)); });
ch.pipeline().addLast(new ServerChannelHandler());
}
});
try { try {
bootstrap.bind(ProxyConfig.getInstance().getServerBind(), ProxyConfig.getInstance().getServerPort()).get(); bootstrap.bind(ProxyConfig.getInstance().getServerBind(), ProxyConfig.getInstance().getServerPort()).get();
@ -97,27 +94,23 @@ 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, Config.getInstance().getBooleanValue("server.ssl.needsClientAuth", false)));
pipeline.addLast("ssl", createSslHandler(sslContext, ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP));
Config.getInstance().getBooleanValue("server.ssl.needsClientAuth", false))); ch.pipeline().addLast(new ProxyMessageEncoder());
ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME, 0));
LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP)); ch.pipeline().addLast(new ServerChannelHandler());
ch.pipeline().addLast(new ProxyMessageEncoder()); } catch (Throwable th) {
ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, logger.error("Severe error during pipeline creation", th);
IdleCheckHandler.WRITE_IDLE_TIME, 0)); throw th;
ch.pipeline().addLast(new ServerChannelHandler()); }
} catch (Throwable th) { }
logger.error("Severe error during pipeline creation", th); });
throw th;
}
}
});
try { try {
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
@ -131,16 +124,14 @@ 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().addFirst(new BytesMetricsHandler());
ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.USER_CHANNEL_READ_IDLE_TIME, 0, 0)); ch.pipeline().addLast(new UserChannelHandler());
ch.pipeline().addFirst(new BytesMetricsHandler()); }
ch.pipeline().addLast(new UserChannelHandler()); });
}
});
List<Integer> ports = ProxyConfig.getInstance().getUserPorts(); List<Integer> ports = ProxyConfig.getInstance().getUserPorts();
for (int port : ports) { for (int port : ports) {

Loading…
Cancel
Save