diff --git a/README.md b/README.md index 2dd62b1..7389bfa 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ lanproxy -------- -lanproxy是一个将局域网个人电脑、服务器代理到公网的工具,目前仅支持tcp流量转发,可支持任何tcp上层协议(ssh访问、web服务器访问、远程桌面...)。目前市面上提供类似服务的有花生壳、TeamView、GoToMyCloud等等,但天下没有免费的午餐,要使用第三方的公网服务器就必须为第三方付费,并且这些服务器都有各种各样的限制,此外,由于数据包会流经第三方,因此对数据安全也是一大隐患。 +lanproxy是一个将局域网个人电脑、服务器代理到公网的内网穿透工具,目前仅支持tcp流量转发,可支持任何tcp上层协议(ssh访问、web服务器访问、远程桌面...)。目前市面上提供类似服务的有花生壳、TeamView、GoToMyCloud等等,但天下没有免费的午餐,要使用第三方的公网服务器就必须为第三方付费,并且这些服务器都有各种各样的限制,此外,由于数据包会流经第三方,因此对数据安全也是一大隐患。 ### 实现方案 diff --git a/proxy-client/src/main/java/org/fengfei/lanproxy/client/ClientChannelMannager.java b/proxy-client/src/main/java/org/fengfei/lanproxy/client/ClientChannelMannager.java index 183e23b..269d494 100644 --- a/proxy-client/src/main/java/org/fengfei/lanproxy/client/ClientChannelMannager.java +++ b/proxy-client/src/main/java/org/fengfei/lanproxy/client/ClientChannelMannager.java @@ -2,12 +2,15 @@ package org.fengfei.lanproxy.client; import java.util.Iterator; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelOption; import io.netty.util.AttributeKey; @@ -61,8 +64,16 @@ public class ClientChannelMannager { return realServerChannels.remove(userId); } + public static boolean isRealServerReadable(Channel realServerChannel) { + return realServerChannel.attr(CLIENT_CHANNEL_WRITEABLE).get() + && realServerChannel.attr(USER_CHANNEL_WRITEABLE).get(); + } + public static void setRealServerChannelReadability(Channel realServerChannel, Boolean client, Boolean user) { - logger.info("update real server channel readability, {} {}", client, user); + logger.debug("update real server channel readability, {} {} {}", realServerChannel, client, user); + if (realServerChannel == null) { + return; + } if (client != null) { realServerChannel.attr(CLIENT_CHANNEL_WRITEABLE).set(client); @@ -78,26 +89,25 @@ public class ClientChannelMannager { } else { realServerChannel.config().setOption(ChannelOption.AUTO_READ, false); } - } public static void notifyChannelWritabilityChanged(Channel channel) { - logger.info("channel writability changed, {}", channel.isWritable()); - - Iterator ite = realServerChannels.keySet().iterator(); - while (ite.hasNext()) { - Channel realServerChannel = realServerChannels.get(ite.next()); - setRealServerChannelReadability(realServerChannel, channel.isWritable(), null); + logger.debug("channel writability changed, {}", channel.isWritable()); + Iterator> entryIte = realServerChannels.entrySet().iterator(); + while (entryIte.hasNext()) { + setRealServerChannelReadability(entryIte.next().getValue(), channel.isWritable(), null); } } public static void clearRealServerChannels() { logger.warn("channel closed, clear real server channels"); - Iterator ite = realServerChannels.keySet().iterator(); + Iterator> ite = realServerChannels.entrySet().iterator(); while (ite.hasNext()) { - Channel realServerChannel = realServerChannels.get(ite.next()); - realServerChannel.close(); + Channel realServerChannel = ite.next().getValue(); + if (realServerChannel.isActive()) { + realServerChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); + } } realServerChannels.clear(); diff --git a/proxy-client/src/main/java/org/fengfei/lanproxy/client/handlers/ClientChannelHandler.java b/proxy-client/src/main/java/org/fengfei/lanproxy/client/handlers/ClientChannelHandler.java index 20d1575..7ffafef 100644 --- a/proxy-client/src/main/java/org/fengfei/lanproxy/client/handlers/ClientChannelHandler.java +++ b/proxy-client/src/main/java/org/fengfei/lanproxy/client/handlers/ClientChannelHandler.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -36,20 +37,20 @@ public class ClientChannelHandler extends SimpleChannelInboundHandler%n -log4j.logger.io.netty=warn \ No newline at end of file +log4j.logger.io.netty=warn +log4j.logger.org.fengfei.lanproxy.client.handlers.ClientChannelHandler=info +log4j.logger.org.fengfei.lanproxy.protocol.IdleCheckHandler=warn \ No newline at end of file diff --git a/proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/ServerChannelHandler.java b/proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/ServerChannelHandler.java index d2ec217..2fb10f0 100644 --- a/proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/ServerChannelHandler.java +++ b/proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/ServerChannelHandler.java @@ -9,6 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; @@ -76,8 +77,7 @@ public class ServerChannelHandler extends SimpleChannelInboundHandler