【fix】修改 * 端口问题

This commit is contained in:
wujiawei 2025-05-08 09:09:09 +08:00
parent ad7fa61067
commit 68f4c92366
3 changed files with 21 additions and 20 deletions

View File

@ -6,7 +6,6 @@ import io.netty.channel.*;
import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
@ -17,6 +16,7 @@ import org.framework.lazy.cloud.network.heartbeat.protocol.route.ProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext; import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType; import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType;
import org.framework.lazy.cloud.network.heartbeat.protocol.utils.FullHttpRequestUtils; import org.framework.lazy.cloud.network.heartbeat.protocol.utils.FullHttpRequestUtils;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.core.utils.ObjectUtils; import org.wu.framework.core.utils.ObjectUtils;
import java.net.URI; import java.net.URI;
@ -67,10 +67,10 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
if (msg instanceof FullHttpRequest) { if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg; FullHttpRequest request = (FullHttpRequest) msg;
URI uri = new URI(request.uri()); URI uri = new URI(request.uri());
String host = uri.getHost(); String originHost = uri.getHost();
int port = uri.getPort(); int originPort = uri.getPort();
if (port == -1) { if (originPort == -1) {
port = 80; originPort = 80;
} }
ByteBuf byteBuf = FullHttpRequestUtils.toByteBuf(request); ByteBuf byteBuf = FullHttpRequestUtils.toByteBuf(request);
@ -82,7 +82,7 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
NettyProxyMsg proxyMsg = new NettyProxyMsg(); NettyProxyMsg proxyMsg = new NettyProxyMsg();
ProxyRoute route = RouteContext.getRoute(host, String.valueOf(port)); ProxyRoute route = RouteContext.getRoute(originHost, String.valueOf(originPort));
proxyMsg.setVisitorId(visitorId); proxyMsg.setVisitorId(visitorId);
proxyMsg.setData(bytes); proxyMsg.setData(bytes);
@ -92,12 +92,12 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
if(ObjectUtils.isEmpty(route)){ if(ObjectUtils.isEmpty(route)){
// 本地代理 // 本地代理
proxyMsg.setType(ProxyMessageType.HTTP_LOCAL_PROXY); proxyMsg.setType(ProxyMessageType.HTTP_LOCAL_PROXY);
proxyMsg.setClientTargetIp(host); proxyMsg.setClientTargetIp(originHost);
proxyMsg.setClientTargetPort(port); proxyMsg.setClientTargetPort(originPort);
}else { }else {
// 远程代理 // 远程代理
String targetIp = route.getTargetIp(); String targetIp = route.getTargetIp();
String targetPort = route.getTargetPort(); String targetPort = NormalUsedString.ASTERISK.equals(route.getTargetPort())?String.valueOf(originPort):route.getTargetPort();
proxyMsg.setClientTargetIp(targetIp); proxyMsg.setClientTargetIp(targetIp);
proxyMsg.setClientTargetPort(Integer.valueOf(targetPort)); proxyMsg.setClientTargetPort(Integer.valueOf(targetPort));
if(RouteType.LOCAL.equals(route.getRouteType())){ if(RouteType.LOCAL.equals(route.getRouteType())){

View File

@ -19,6 +19,7 @@ import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRout
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ProxyRoute; import org.framework.lazy.cloud.network.heartbeat.protocol.route.ProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext; import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType; import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.core.utils.ObjectUtils; import org.wu.framework.core.utils.ObjectUtils;
import java.util.UUID; import java.util.UUID;
@ -51,26 +52,26 @@ public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandle
protected void channelRead0(ChannelHandlerContext ctx, Socks5CommandRequest request) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, Socks5CommandRequest request) throws Exception {
if (request.type() == Socks5CommandType.CONNECT) { if (request.type() == Socks5CommandType.CONNECT) {
String host = request.dstAddr(); String originHost = request.dstAddr();
int port = request.dstPort(); int originPort = request.dstPort();
Socks5AddressType socks5AddressType = request.dstAddrType(); Socks5AddressType socks5AddressType = request.dstAddrType();
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel()); String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
NettyProxyMsg proxyMsg = new NettyProxyMsg(); NettyProxyMsg proxyMsg = new NettyProxyMsg();
proxyMsg.setVisitorId(visitorId); proxyMsg.setVisitorId(visitorId);
ProxyRoute route = RouteContext.getRoute(host, String.valueOf(port)); ProxyRoute route = RouteContext.getRoute(originHost, String.valueOf(originPort));
if(ObjectUtils.isEmpty(route)){ if(ObjectUtils.isEmpty(route)){
// 未查询到路由信息本地代理 // 未查询到路由信息本地代理
proxyMsg.setType(ProxyMessageType.SOCKS_LOCAL_PROXY); proxyMsg.setType(ProxyMessageType.SOCKS_LOCAL_PROXY);
proxyMsg.setClientTargetIp(host); proxyMsg.setClientTargetIp(originHost);
proxyMsg.setClientTargetPort(port); proxyMsg.setClientTargetPort(originPort);
}else { }else {
// 其他路由代理 // 其他路由代理
String targetIp = route.getTargetIp(); String targetIp = route.getTargetIp();
String targetPort = route.getTargetPort(); String targetPort = NormalUsedString.ASTERISK.equals(route.getTargetPort())?String.valueOf(originPort):route.getTargetPort();
proxyMsg.setClientTargetIp(targetIp); proxyMsg.setClientTargetIp(targetIp);
proxyMsg.setClientTargetPort(Integer.parseInt(targetPort)); proxyMsg.setClientTargetPort(Integer.parseInt(targetPort));
if(RouteType.LOCAL.equals(route.getRouteType())){ if(RouteType.LOCAL.equals(route.getRouteType())){

View File

@ -6,17 +6,17 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.route;
public interface ProxyRoute { public interface ProxyRoute {
public String getVirtualIp(); String getVirtualIp();
public RouteType getRouteType(); RouteType getRouteType();
public String getVirtualPort(); String getVirtualPort();
public String getTargetIp(); String getTargetIp();
public String getTargetPort(); String getTargetPort();
} }