替换部分过期neety方法

master
星期八 5 years ago
parent ac67c48357
commit 7e3b699aae
  1. 2
      proxy-client/src/main/java/org/fengfei/lanproxy/client/ClientChannelMannager.java
  2. 2
      proxy-client/src/main/java/org/fengfei/lanproxy/client/ProxyClientContainer.java
  3. 2
      proxy-client/src/main/java/org/fengfei/lanproxy/client/handlers/ClientChannelHandler.java
  4. 6
      proxy-common/pom.xml
  5. 6
      proxy-common/src/main/java/org/fengfei/lanproxy/common/container/Container.java
  6. 1
      proxy-server/src/main/java/org/fengfei/lanproxy/server/ProxyServerContainer.java
  7. 1
      proxy-server/src/main/java/org/fengfei/lanproxy/server/SslContextCreator.java
  8. 8
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/ProxyConfig.java
  9. 2
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/ApiRoute.java
  10. 2
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/HttpRequestHandler.java
  11. 4
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/MimeType.java
  12. 9
      proxy-server/src/main/java/org/fengfei/lanproxy/server/config/web/routes/RouteConfig.java
  13. 6
      proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/ServerChannelHandler.java
  14. 6
      proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/UserChannelHandler.java

@ -70,7 +70,7 @@ public class ClientChannelMannager {
proxyChanel.close(); proxyChanel.close();
} else { } else {
proxyChanel.config().setOption(ChannelOption.AUTO_READ, true); proxyChanel.config().setOption(ChannelOption.AUTO_READ, true);
proxyChanel.attr(Constants.NEXT_CHANNEL).remove(); proxyChanel.attr(Constants.NEXT_CHANNEL).set(null);
proxyChannelPool.offer(proxyChanel); proxyChannelPool.offer(proxyChanel);
logger.debug("return ProxyChanel to the pool, channel is {}, pool size is {} ", proxyChanel, proxyChannelPool.size()); logger.debug("return ProxyChanel to the pool, channel is {}, pool size is {} ", proxyChanel, proxyChannelPool.size());
} }

@ -68,6 +68,8 @@ public class ProxyClientContainer implements Container, ChannelStatusListener {
} }
}); });
bootstrap = new Bootstrap(); bootstrap = new Bootstrap();
bootstrap.group(workerGroup); bootstrap.group(workerGroup);
bootstrap.channel(NioSocketChannel.class); bootstrap.channel(NioSocketChannel.class);

@ -72,7 +72,7 @@ public class ClientChannelHandler extends SimpleChannelInboundHandler<ProxyMessa
Channel realServerChannel = ctx.channel().attr(Constants.NEXT_CHANNEL).get(); Channel realServerChannel = ctx.channel().attr(Constants.NEXT_CHANNEL).get();
logger.debug("handleDisconnectMessage, {}", realServerChannel); logger.debug("handleDisconnectMessage, {}", realServerChannel);
if (realServerChannel != null) { if (realServerChannel != null) {
ctx.channel().attr(Constants.NEXT_CHANNEL).remove(); ctx.channel().attr(Constants.NEXT_CHANNEL).set(null);
ClientChannelMannager.returnProxyChanel(ctx.channel()); ClientChannelMannager.returnProxyChanel(ctx.channel());
realServerChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); realServerChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
} }

@ -16,5 +16,11 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.7</version> <version>2.7</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -2,7 +2,13 @@ package org.fengfei.lanproxy.common.container;
public interface Container { public interface Container {
/**
* 启动容器
*/
void start(); void start();
/**
* 停止容器
*/
void stop(); void stop();
} }

@ -92,6 +92,7 @@ public class ProxyServerContainer implements Container, ConfigChangedListener {
} }
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
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).childHandler(new ChannelInitializer<SocketChannel>() { b.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

@ -22,6 +22,7 @@ import org.fengfei.lanproxy.common.Config;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
public class SslContextCreator { public class SslContextCreator {
private static Logger logger = LoggerFactory.getLogger(SslContextCreator.class); private static Logger logger = LoggerFactory.getLogger(SslContextCreator.class);

@ -183,8 +183,8 @@ public class ProxyConfig implements Serializable {
clients = new ArrayList<Client>(); clients = new ArrayList<Client>();
} }
Map<String, List<Integer>> clientInetPortMapping = new HashMap<String, List<Integer>>(); Map<String, List<Integer>> clientInetPortMapping = new HashMap<String, List<Integer>>(clients.size());
Map<Integer, String> inetPortLanInfoMapping = new HashMap<Integer, String>(); Map<Integer, String> inetPortLanInfoMapping = new HashMap<Integer, String>(clients.size());
// 构造端口映射关系 // 构造端口映射关系
for (Client client : clients) { for (Client client : clients) {
@ -406,7 +406,9 @@ public class ProxyConfig implements Serializable {
* *
*/ */
public static interface ConfigChangedListener { public static interface ConfigChangedListener {
/**
* 配置更新回调
*/
void onChanged(); void onChanged();
} }
} }

@ -71,7 +71,7 @@ public class ApiRoute {
middleware.preRequest(request); middleware.preRequest(request);
} }
URI uri = new URI(request.getUri()); URI uri = new URI(request.uri());
RequestHandler handler = routes.get(uri.getPath()); RequestHandler handler = routes.get(uri.getPath());
ResponseInfo responseInfo = null; ResponseInfo responseInfo = null;
if (handler != null) { if (handler != null) {

@ -104,7 +104,7 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequ
HttpResponse response = new DefaultHttpResponse(request.protocolVersion(), status); HttpResponse response = new DefaultHttpResponse(request.protocolVersion(), status);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, mimeType); response.headers().set(HttpHeaderNames.CONTENT_TYPE, mimeType);
boolean keepAlive = HttpHeaders.isKeepAlive(request); boolean keepAlive = HttpUtil.isKeepAlive(request);
if (keepAlive) { if (keepAlive) {
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, length); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, length);
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

@ -7,7 +7,7 @@ import java.util.regex.Pattern;
public class MimeType { public class MimeType {
final static Pattern pattern = Pattern.compile("\\S*[?]\\S*"); final static Pattern PATTERN = Pattern.compile("\\S*[?]\\S*");
private static Map<String, String> h = new HashMap<String, String>(); private static Map<String, String> h = new HashMap<String, String>();
static { static {
@ -211,7 +211,7 @@ public class MimeType {
public static String parseSuffix(String url) { public static String parseSuffix(String url) {
try { try {
Matcher matcher = pattern.matcher(url); Matcher matcher = PATTERN.matcher(url);
String[] spUrl = url.toString().split("/"); String[] spUrl = url.toString().split("/");
int len = spUrl.length; int len = spUrl.length;
String endUrl = spUrl[len - 1]; String endUrl = spUrl[len - 1];

@ -5,6 +5,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import io.netty.handler.codec.http.HttpHeaderNames;
import org.fengfei.lanproxy.common.JsonUtil; import org.fengfei.lanproxy.common.JsonUtil;
import org.fengfei.lanproxy.server.ProxyChannelManager; import org.fengfei.lanproxy.server.ProxyChannelManager;
import org.fengfei.lanproxy.server.config.ProxyConfig; import org.fengfei.lanproxy.server.config.ProxyConfig;
@ -50,7 +51,7 @@ public class RouteConfig {
@Override @Override
public void preRequest(FullHttpRequest request) { public void preRequest(FullHttpRequest request) {
String cookieHeader = request.headers().get(HttpHeaders.Names.COOKIE); String cookieHeader = request.headers().get(HttpHeaderNames.COOKIE);
boolean authenticated = false; boolean authenticated = false;
if (cookieHeader != null) { if (cookieHeader != null) {
String[] cookies = cookieHeader.split(";"); String[] cookies = cookieHeader.split(";");
@ -64,7 +65,7 @@ public class RouteConfig {
} }
} }
String auth = request.headers().get(HttpHeaders.Names.AUTHORIZATION); String auth = request.headers().get(HttpHeaderNames.AUTHORIZATION);
if (!authenticated && auth != null) { if (!authenticated && auth != null) {
String[] authArr = auth.split(" "); String[] authArr = auth.split(" ");
if (authArr.length == 2 && authArr[0].equals(ProxyConfig.getInstance().getConfigAdminUsername()) && authArr[1].equals(ProxyConfig.getInstance().getConfigAdminPassword())) { if (authArr.length == 2 && authArr[0].equals(ProxyConfig.getInstance().getConfigAdminUsername()) && authArr[1].equals(ProxyConfig.getInstance().getConfigAdminPassword())) {
@ -72,11 +73,11 @@ public class RouteConfig {
} }
} }
if (!LOGIN_URL.equals(request.getUri()) && !authenticated) { if (!LOGIN_URL.equals(request.uri()) && !authenticated) {
throw new ContextException(ResponseInfo.CODE_UNAUTHORIZED); throw new ContextException(ResponseInfo.CODE_UNAUTHORIZED);
} }
logger.info("handle request for api {}", request.getUri()); logger.info("handle request for api {}", request.uri());
} }
}); });

@ -83,9 +83,9 @@ public class ServerChannelHandler extends SimpleChannelInboundHandler<ProxyMessa
if (userChannel != null) { if (userChannel != null) {
// 数据发送完成后再关闭连接,解决http1.0数据传输问题 // 数据发送完成后再关闭连接,解决http1.0数据传输问题
userChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); userChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
ctx.channel().attr(Constants.NEXT_CHANNEL).remove(); ctx.channel().attr(Constants.NEXT_CHANNEL).set(null);
ctx.channel().attr(Constants.CLIENT_KEY).remove(); ctx.channel().attr(Constants.CLIENT_KEY).set(null);
ctx.channel().attr(Constants.USER_ID).remove(); ctx.channel().attr(Constants.USER_ID).set(null);
} }
} }

@ -94,9 +94,9 @@ public class UserChannelHandler extends SimpleChannelInboundHandler<ByteBuf> {
ProxyChannelManager.removeUserChannelFromCmdChannel(cmdChannel, userId); ProxyChannelManager.removeUserChannelFromCmdChannel(cmdChannel, userId);
Channel proxyChannel = userChannel.attr(Constants.NEXT_CHANNEL).get(); Channel proxyChannel = userChannel.attr(Constants.NEXT_CHANNEL).get();
if (proxyChannel != null && proxyChannel.isActive()) { if (proxyChannel != null && proxyChannel.isActive()) {
proxyChannel.attr(Constants.NEXT_CHANNEL).remove(); proxyChannel.attr(Constants.NEXT_CHANNEL).set(null);
proxyChannel.attr(Constants.CLIENT_KEY).remove(); proxyChannel.attr(Constants.CLIENT_KEY).set(null);
proxyChannel.attr(Constants.USER_ID).remove(); proxyChannel.attr(Constants.USER_ID).set(null);
proxyChannel.config().setOption(ChannelOption.AUTO_READ, true); proxyChannel.config().setOption(ChannelOption.AUTO_READ, true);
// 通知客户端,用户连接已经断开 // 通知客户端,用户连接已经断开

Loading…
Cancel
Save