diff --git a/proxy-server/src/main/java/org/fengfei/lanproxy/server/ProxyChannelManager.java b/proxy-server/src/main/java/org/fengfei/lanproxy/server/ProxyChannelManager.java index f444772..dcf8615 100644 --- a/proxy-server/src/main/java/org/fengfei/lanproxy/server/ProxyChannelManager.java +++ b/proxy-server/src/main/java/org/fengfei/lanproxy/server/ProxyChannelManager.java @@ -39,11 +39,9 @@ public class ProxyChannelManager { private static final AttributeKey CHANNEL_CLIENT_KEY = AttributeKey.newInstance("channel_client_key"); - private static final AttributeKey PROXY_CHANNEL_WRITEABLE = AttributeKey - .newInstance("proxy_channel_writeable"); + private static final AttributeKey PROXY_CHANNEL_WRITEABLE = AttributeKey.newInstance("proxy_channel_writeable"); - private static final AttributeKey REAL_BACKEND_SERVER_CHANNEL_WRITEABLE = AttributeKey - .newInstance("real_backend_server_channel_writeable"); + private static final AttributeKey REAL_BACKEND_SERVER_CHANNEL_WRITEABLE = AttributeKey.newInstance("real_backend_server_channel_writeable"); private static Map portChannelMapping = new ConcurrentHashMap(); @@ -58,17 +56,8 @@ public class ProxyChannelManager { @Override public synchronized void onChanged() { Iterator> ite = proxyChannels.entrySet().iterator(); - Set proxyChannelSet = new HashSet(); while (ite.hasNext()) { Channel proxyChannel = ite.next().getValue(); - - // 因为不同的外网端口可能映射了相同的内部代理连接,相同代理连接处理一次即可 - if (proxyChannelSet.contains(proxyChannel)) { - continue; - } - - proxyChannelSet.add(proxyChannel); - String clientKey = proxyChannel.attr(CHANNEL_CLIENT_KEY).get(); // 去除已经去掉的clientKey配置 @@ -79,8 +68,7 @@ public class ProxyChannelManager { } if (proxyChannel.isActive()) { - List inetPorts = new ArrayList( - ProxyConfig.getInstance().getClientInetPorts(clientKey)); + List inetPorts = new ArrayList(ProxyConfig.getInstance().getClientInetPorts(clientKey)); Set inetPortSet = new HashSet(inetPorts); List channelInetPorts = new ArrayList(proxyChannel.attr(CHANNEL_PORT).get()); @@ -89,7 +77,7 @@ public class ProxyChannelManager { // 移除旧的连接映射关系 for (int chanelInetPort : channelInetPorts) { Channel channel = portChannelMapping.get(chanelInetPort); - if (proxyChannel == null) { + if (channel == null) { continue; } @@ -123,8 +111,7 @@ public class ProxyChannelManager { while (ite.hasNext()) { Entry entry = ite.next(); Channel proxyChannel = entry.getValue(); - logger.info("proxyChannel config, {}, {}, {} ,{}", entry.getKey(), proxyChannel, - getUserChannels(proxyChannel).size(), proxyChannel.attr(CHANNEL_PORT).get()); + logger.info("proxyChannel config, {}, {}, {} ,{}", entry.getKey(), proxyChannel, getUserChannels(proxyChannel).size(), proxyChannel.attr(CHANNEL_PORT).get()); } } @@ -170,8 +157,6 @@ public class ProxyChannelManager { // 客户端(proxy-client)相对较少,这里同步的比较重 // 保证服务器对外端口与客户端到服务器的连接关系在临界情况时调用removeChannel(Channel channel)时不出问题 synchronized (portChannelMapping) { - - // for (int port : ports) { portChannelMapping.put(port, channel); } @@ -309,10 +294,8 @@ public class ProxyChannelManager { * @param client * @param proxy */ - public static void setUserChannelReadability(Channel userChannel, Boolean realBackendServerChannelWriteability, - Boolean proxyChannelWriteability) { - logger.info("update user channel readability, {} {} {}", userChannel, realBackendServerChannelWriteability, - proxyChannelWriteability); + public static void setUserChannelReadability(Channel userChannel, Boolean realBackendServerChannelWriteability, Boolean proxyChannelWriteability) { + logger.info("update user channel readability, {} {} {}", userChannel, realBackendServerChannelWriteability, proxyChannelWriteability); synchronized (userChannel) { if (realBackendServerChannelWriteability != null) { userChannel.attr(REAL_BACKEND_SERVER_CHANNEL_WRITEABLE).set(realBackendServerChannelWriteability); @@ -322,8 +305,7 @@ public class ProxyChannelManager { userChannel.attr(PROXY_CHANNEL_WRITEABLE).set(proxyChannelWriteability); } - if (userChannel.attr(REAL_BACKEND_SERVER_CHANNEL_WRITEABLE).get() - && userChannel.attr(PROXY_CHANNEL_WRITEABLE).get()) { + if (userChannel.attr(REAL_BACKEND_SERVER_CHANNEL_WRITEABLE).get() && userChannel.attr(PROXY_CHANNEL_WRITEABLE).get()) { // 代理客户端与后端服务器连接状态均为可写时,用户连接状态为可读 userChannel.config().setOption(ChannelOption.AUTO_READ, true);