[fix] 代码格式化

This commit is contained in:
wujiawei
2025-06-16 13:48:35 +08:00
parent 6b16ff7065
commit 1699130f14
422 changed files with 4511 additions and 6278 deletions

View File

@@ -42,6 +42,7 @@ public abstract class AbstractNettyHttpProxyActuator implements NettyHttpProxyAc
public void doSend(Channel proxyChannel, FullHttpRequest fullHttpRequest) {
proxyChannel.writeAndFlush(FullHttpRequestUtils.toByteBuf(fullHttpRequest));
}
/**
* 发送消息
*

View File

@@ -9,8 +9,6 @@ import io.netty.handler.codec.http.FullHttpRequest;
public interface NettyHttpProxyActuator {
/**
* 发送消息
*

View File

@@ -2,11 +2,14 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.advanced;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpClientProxyClientTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
@@ -24,8 +27,8 @@ public class HttpClientProxyClientProxyTypeAdvanced extends AbstractHttpClientPr
/**
* 处理当前数据
*
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {

View File

@@ -2,11 +2,14 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.advanced;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.AbstractHttpLocalProxyTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
@@ -24,8 +27,8 @@ public class HttpProtocolHandleChannelLocalProxyTypeAdvanced extends AbstractHtt
/**
* 处理当前数据
*
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {

View File

@@ -28,8 +28,8 @@ public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
/**
* 处理当前数据
*
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
* @param nettyChannelContext 当前通道
* @param nettyProxyMsg 通道数据
*/
@Override
protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {

View File

@@ -19,9 +19,9 @@ public class NettyHttpProxySocketApplicationListener implements SocketApplicatio
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
private ChannelFuture channelFuture;
private final NettyHttpProxyFilter nettyHttpProxyFilter;// 通道业务处理
private final ProtocolProxyProperties protocolProxyProperties;
private ChannelFuture channelFuture;
public NettyHttpProxySocketApplicationListener(NettyHttpProxyFilter nettyHttpProxyFilter, ProtocolProxyProperties protocolProxyProperties) {
this.nettyHttpProxyFilter = nettyHttpProxyFilter;
@@ -58,7 +58,7 @@ public class NettyHttpProxySocketApplicationListener implements SocketApplicatio
channelFuture.addListener((ChannelFutureListener) channelFuture -> {
// 服务器已启动
log.info("Http 协议代理 服务器启动成功 【{}】",httpProtocolProxyPort);
log.info("Http 协议代理 服务器启动成功 【{}】", httpProtocolProxyPort);
});
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {

View File

@@ -18,8 +18,8 @@ public class NettySocketProxySocketApplicationListener implements SocketApplicat
private final EventLoopGroup bossGroup = new NioEventLoopGroup();
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
private final NettyTcpProxyFilter nettyTcpProxyFilter;// 通道业务处理
private ChannelFuture channelFuture;
private final ProtocolProxyProperties protocolProxyProperties;
private ChannelFuture channelFuture;
public NettySocketProxySocketApplicationListener(NettyTcpProxyFilter nettyTcpProxyFilter, ProtocolProxyProperties protocolProxyProperties) {
this.nettyTcpProxyFilter = nettyTcpProxyFilter;

View File

@@ -55,6 +55,5 @@ public class NettyTcpProxyFilter extends DebugChannelInitializer<SocketChannel>
pipeline.addLast(new NettySocks5CommandRequestHandler(channelTypeAdapter));
}
}

View File

@@ -2,7 +2,10 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
@@ -12,11 +15,17 @@ import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeK
public class NettyHttpProxyBackendHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
/**
* Is called for each message of type {@link I}.
*
* @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
* belongs to
* @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
* belongs to
* @param nettyByteBuf the message to handle
* @throws Exception is thrown if an error occurred
*/
@@ -24,7 +33,7 @@ public class NettyHttpProxyBackendHandler extends SimpleChannelInboundHandler<Ne
protected void channelRead0(ChannelHandlerContext ctx, NettyByteBuf nettyByteBuf) throws Exception {
Channel channel = ctx.channel();
byte[] bytes = nettyByteBuf.getData();
log.debug("bytes.length:{}",bytes.length);
log.debug("bytes.length:{}", bytes.length);
log.debug("接收客户端真实服务数据:{}", new String(bytes));
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
// 将二进制数组转换成 ByteBuf 然后进行发送
@@ -46,10 +55,4 @@ public class NettyHttpProxyBackendHandler extends SimpleChannelInboundHandler<Ne
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}

View File

@@ -7,8 +7,8 @@ import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import lombok.extern.slf4j.Slf4j;
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.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute;
@@ -34,11 +34,16 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
private final ChannelTypeAdapter channelTypeAdapter;
public NettyHttpProxyHandler(ChannelTypeAdapter channelTypeAdapter) {
this.channelTypeAdapter = channelTypeAdapter;
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
/**
* Calls {@link ChannelHandlerContext#fireChannelActive()} to forward
* to the next {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
@@ -54,7 +59,7 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
System.out.println("http 这是一个TCP通道");
} else if (channel instanceof NioDatagramChannel) {
System.out.println("http 这是一个UDP通道");
} else {
} else {
System.out.println("http 未知类型的通道");
}
String visitorId = UUID.randomUUID().toString();
@@ -64,8 +69,7 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
if (msg instanceof FullHttpRequest request) {
URI uri = new URI(request.uri());
String originHost = uri.getHost();
int originPort = uri.getPort();
@@ -86,28 +90,28 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
proxyMsg.setVisitorId(visitorId);
proxyMsg.setData(bytes);
if(ObjectUtils.isEmpty(route)){
if (ObjectUtils.isEmpty(route)) {
// 本地代理
proxyMsg.setType(ProxyMessageType.HTTP_LOCAL_PROXY);
proxyMsg.setTargetIp(originHost);
proxyMsg.setTargetPort(originPort);
}else {
} else {
// 远程代理
String targetIp = route.getTargetIp();
String targetPort = NormalUsedString.ASTERISK.equals(route.getTargetPort())?String.valueOf(originPort):route.getTargetPort();
String targetPort = NormalUsedString.ASTERISK.equals(route.getTargetPort()) ? String.valueOf(originPort) : route.getTargetPort();
proxyMsg.setTargetIp(targetIp);
proxyMsg.setTargetPort(Integer.valueOf(targetPort));
if(RouteType.LOCAL.equals(route.getRouteType())){
if (RouteType.LOCAL.equals(route.getRouteType())) {
proxyMsg.setType(ProxyMessageType.HTTP_LOCAL_PROXY);
}else if (RouteType.CLIENT_PROXY_CLIENT.equals(route.getRouteType())){
ClientProxyRoute clientProxyRoute= (ClientProxyRoute) route;
} else if (RouteType.CLIENT_PROXY_CLIENT.equals(route.getRouteType())) {
ClientProxyRoute clientProxyRoute = (ClientProxyRoute) route;
String clientId = clientProxyRoute.getTargetClientId();
proxyMsg.setClientId(clientId);
proxyMsg.setType(ProxyMessageType.HTTP_CLIENT_PROXY_CLIENT_);
}else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){
} else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())) {
proxyMsg.setType(ProxyMessageType.HTTP_CLIENT_PROXY_SERVER_);
}else if (RouteType.SERVER_PROXY_CLIENT.equals(route.getRouteType())){
ClientProxyRoute clientProxyRoute= (ClientProxyRoute) route;
} else if (RouteType.SERVER_PROXY_CLIENT.equals(route.getRouteType())) {
ClientProxyRoute clientProxyRoute = (ClientProxyRoute) route;
String clientId = clientProxyRoute.getTargetClientId();
proxyMsg.setClientId(clientId);
proxyMsg.setType(ProxyMessageType.HTTP_SERVER_PROXY_CLIENT_);
@@ -127,7 +131,7 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
if (nextChannel != null) {
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
// close
NettyProxyMsg closeMes=new NettyProxyMsg();
NettyProxyMsg closeMes = new NettyProxyMsg();
closeMes.setVisitorId(visitorId);
closeMes.setType(ProxyMessageType.HTTP_REPORT_CLIENT_PROXY_SERVER_TRANSFER_CLOSE_);
nextChannel.writeAndFlush(closeMes);
@@ -140,10 +144,4 @@ public class NettyHttpProxyHandler extends ChannelInboundHandlerAdapter {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}

View File

@@ -37,10 +37,10 @@ public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandle
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
if (channel instanceof NioSocketChannel) {
log.info("socks 这是一个TCP通道");
log.info("socks 这是一个TCP通道");
} else if (channel instanceof NioDatagramChannel) {
log.info("socks 这是一个UDP通道");
} else {
} else {
log.info("socks 未知类型的通道");
}
String visitorId = UUID.randomUUID().toString();
@@ -62,29 +62,28 @@ public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandle
ProxyRoute route = RouteContext.getRoute(originHost, String.valueOf(originPort));
if(ObjectUtils.isEmpty(route)){
if (ObjectUtils.isEmpty(route)) {
// 未查询到路由信息、本地代理
proxyMsg.setType(ProxyMessageType.SOCKS_LOCAL_PROXY);
proxyMsg.setTargetIp(originHost);
proxyMsg.setTargetPort(originPort);
}else {
} else {
// 其他路由代理
String targetIp = route.getTargetIp();
String targetPort = NormalUsedString.ASTERISK.equals(route.getTargetPort())?String.valueOf(originPort):route.getTargetPort();
String targetPort = NormalUsedString.ASTERISK.equals(route.getTargetPort()) ? String.valueOf(originPort) : route.getTargetPort();
proxyMsg.setTargetIp(targetIp);
proxyMsg.setTargetPort(Integer.parseInt(targetPort));
if(RouteType.LOCAL.equals(route.getRouteType())){
if (RouteType.LOCAL.equals(route.getRouteType())) {
proxyMsg.setType(ProxyMessageType.SOCKS_LOCAL_PROXY);
}else if (RouteType.CLIENT_PROXY_CLIENT.equals(route.getRouteType())){
ClientProxyRoute clientProxyRoute= (ClientProxyRoute) route;
} else if (RouteType.CLIENT_PROXY_CLIENT.equals(route.getRouteType())) {
ClientProxyRoute clientProxyRoute = (ClientProxyRoute) route;
String clientId = clientProxyRoute.getTargetClientId();
proxyMsg.setClientId(clientId);
proxyMsg.setType(ProxyMessageType.SOCKS_CLIENT_PROXY_CLIENT_);
}else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())){
} else if (RouteType.CLIENT_PROXY_SEVER.equals(route.getRouteType())) {
proxyMsg.setType(ProxyMessageType.SOCKS_CLIENT_PROXY_SERVER_);
}else if (RouteType.SERVER_PROXY_CLIENT.equals(route.getRouteType())){
ClientProxyRoute clientProxyRoute= (ClientProxyRoute) route;
} else if (RouteType.SERVER_PROXY_CLIENT.equals(route.getRouteType())) {
ClientProxyRoute clientProxyRoute = (ClientProxyRoute) route;
String clientId = clientProxyRoute.getTargetClientId();
proxyMsg.setClientId(clientId);
proxyMsg.setType(ProxyMessageType.SOCKS_SERVER_PROXY_CLIENT_);
@@ -93,7 +92,7 @@ public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandle
// 绑定当前通道地址类型
ChannelAttributeKeyUtils.buildSocks5AddressType(ctx.channel(), socks5AddressType.byteValue());
NettySocketChannelContext nettyChannelContext=new NettySocketChannelContext();
NettySocketChannelContext nettyChannelContext = new NettySocketChannelContext();
nettyChannelContext.setChannelHandlerContext(ctx);
nettyChannelContext.setSocks5AddressType(request.dstAddrType());
channelTypeAdapter.handler(nettyChannelContext, proxyMsg);

View File

@@ -24,16 +24,14 @@ public class ProtocolProxyProperties {
private SocketProtocolProxy socketProtocolProxy = new SocketProtocolProxy();
@Data
public static class HttpProtocolProxy {
private Integer port=8001;
private Integer port = 8001;
}
@Data
public static class SocketProtocolProxy {
private Integer port=9001;
private Integer port = 9001;
}
}

View File

@@ -7,16 +7,14 @@ import lombok.Data;
* 代理路由信息
*/
@Data
public class ClientProxyRoute implements ProxyRoute{
public class ClientProxyRoute implements ProxyRoute {
/**
*
* 目标客户端ID
*/
private String targetClientId;
/**
*
* 虚拟IP
*/
private String virtualIp;
@@ -27,7 +25,6 @@ public class ClientProxyRoute implements ProxyRoute{
/**
*
* 路由IP
*/
private String targetIp;

View File

@@ -6,7 +6,6 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.route;
public interface ProxyRoute {
String getVirtualIp();

View File

@@ -10,9 +10,7 @@ import lombok.Data;
public class ServerProxyRoute implements ProxyRoute {
/**
*
* 虚拟IP
*/
private String virtualIp;
@@ -22,7 +20,6 @@ public class ServerProxyRoute implements ProxyRoute {
private String virtualPort;
/**
*
* 路由IP
*/
private String targetIp;

View File

@@ -12,10 +12,11 @@ public class FullHttpRequestUtils {
/**
* 将 FullHttpRequest 转换成 ByteBuf 发送下一个通道
*
* @param request FullHttpRequest
* @return ByteBuf
*/
public static ByteBuf toByteBuf(FullHttpRequest request) {
public static ByteBuf toByteBuf(FullHttpRequest request) {
// TODO CompositeByteBuf
ByteBuf body = PooledByteBufAllocator.DEFAULT.buffer();
// request-line

View File

@@ -19,23 +19,27 @@ public class DnsServer {
this.port = port;
}
public static void main(String[] args) throws Exception {
new DnsServer(53).run();
}
public void run() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
ch.pipeline().addLast(
new DatagramDnsQueryDecoder(),
new DatagramDnsResponseEncoder(),
new DnsServerHandler()
);
}
});
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
ch.pipeline().addLast(
new DatagramDnsQueryDecoder(),
new DatagramDnsResponseEncoder(),
new DnsServerHandler()
);
}
});
// 绑定端口
ChannelFuture f = b.bind(port).sync();
@@ -45,8 +49,4 @@ public class DnsServer {
group.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new DnsServer(53).run();
}
}

View File

@@ -16,7 +16,7 @@ import java.util.Map;
public class DnsServerHandler extends SimpleChannelInboundHandler<DatagramDnsQuery> {
private Map<String, String> domainIpMapping = new HashMap<>();
private final Map<String, String> domainIpMapping = new HashMap<>();
{

View File

@@ -8,7 +8,9 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
@@ -24,18 +26,18 @@ public class HttpProxyServer {
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new HttpClientCodec(),
new HttpObjectAggregator(1048576),
new HttpProxyServerHandler()
);
}
});
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new HttpClientCodec(),
new HttpObjectAggregator(1048576),
new HttpProxyServerHandler()
);
}
});
ChannelFuture f = b.bind(PORT).sync();
System.out.println("Proxy server started and listening on port " + PORT);
@@ -50,10 +52,15 @@ public class HttpProxyServer {
private Channel outboundChannel;
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
if (msg instanceof FullHttpRequest request) {
URI uri = new URI(request.uri());
String host = uri.getHost();
int port = uri.getPort();
@@ -63,17 +70,17 @@ public class HttpProxyServer {
Bootstrap b = new Bootstrap();
b.group(ctx.channel().eventLoop())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new HttpClientCodec(),
new HttpObjectAggregator(1048576),
new ProxyBackendHandler(ctx.channel())
);
}
});
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new HttpClientCodec(),
new HttpObjectAggregator(1048576),
new ProxyBackendHandler(ctx.channel())
);
}
});
ChannelFuture f = b.connect(host, port);
outboundChannel = f.channel();
@@ -104,12 +111,6 @@ public class HttpProxyServer {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
private static class ProxyBackendHandler extends ChannelInboundHandlerAdapter {
@@ -120,6 +121,12 @@ public class HttpProxyServer {
this.inboundChannel = inboundChannel;
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 将目标服务器的响应转发给客户端
@@ -136,11 +143,5 @@ public class HttpProxyServer {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
}

View File

@@ -10,7 +10,9 @@ import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
@@ -90,10 +92,15 @@ public class MultiProtocolProxyServer {
private static class HttpProxyServerHandler extends ChannelInboundHandlerAdapter {
private Channel outboundChannel;
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
if (msg instanceof FullHttpRequest request) {
URI uri = new URI(request.uri());
String host = uri.getHost();
int port = uri.getPort();
@@ -142,12 +149,6 @@ public class MultiProtocolProxyServer {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
private static class HttpProxyBackendHandler extends ChannelInboundHandlerAdapter {
@@ -157,6 +158,12 @@ public class MultiProtocolProxyServer {
this.inboundChannel = inboundChannel;
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
inboundChannel.writeAndFlush(msg);
@@ -172,16 +179,16 @@ public class MultiProtocolProxyServer {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
}
private static class TcpProxyServerHandler extends ChannelInboundHandlerAdapter {
private Channel outboundChannel;
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
private static class TcpProxyServerHandler extends ChannelInboundHandlerAdapter {
private Channel outboundChannel;
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
@@ -229,12 +236,6 @@ public class MultiProtocolProxyServer {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
private static class TcpProxyBackendHandler extends ChannelInboundHandlerAdapter {
@@ -244,6 +245,12 @@ public class MultiProtocolProxyServer {
this.inboundChannel = inboundChannel;
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
inboundChannel.writeAndFlush(msg);
@@ -259,16 +266,10 @@ public class MultiProtocolProxyServer {
cause.printStackTrace();
closeOnFlush(ctx.channel());
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
private static class UdpProxyServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
private InetSocketAddress targetAddress = new InetSocketAddress("example.com", 80);
private final InetSocketAddress targetAddress = new InetSocketAddress("example.com", 80);
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {

View File

@@ -1,7 +1,6 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.socket5;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
@@ -10,9 +9,6 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* @author kdyzm
* @date 2021-04-23

View File

@@ -18,7 +18,6 @@ public class Socks5PasswordAuthRequestInboundHandler extends SimpleChannelInboun
ctx.writeAndFlush(passwordAuthResponse);
ctx.pipeline().remove(this);
ctx.pipeline().remove(Socks5PasswordAuthRequestDecoder.class);
return;
// 认证失败
// Socks5PasswordAuthResponse passwordAuthResponse = new DefaultSocks5PasswordAuthResponse(Socks5PasswordAuthStatus.FAILURE);

View File

@@ -10,7 +10,6 @@ import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5PasswordAuthRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5ServerEncoder;
import lombok.extern.slf4j.Slf4j;
@@ -22,6 +21,10 @@ public class Socks5ProxyServer {
this.port = port;
}
public static void main(String[] args) throws Exception {
new Socks5ProxyServer(1080).run();
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
@@ -64,8 +67,4 @@ public class Socks5ProxyServer {
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new Socks5ProxyServer(1080).run();
}
}

View File

@@ -5,8 +5,6 @@ import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.concurrent.atomic.AtomicReference;
public class Socks5ServerConnectHandler extends SimpleChannelInboundHandler<Object> {
private final Channel relayChannel;
@@ -28,8 +26,8 @@ public class Socks5ServerConnectHandler extends SimpleChannelInboundHandler<Obje
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (relayChannel.isActive()) {
relayChannel.writeAndFlush(new io.netty.handler.codec.socksx.v5.DefaultSocks5CommandResponse(
io.netty.handler.codec.socksx.v5.Socks5CommandStatus.FAILURE,
io.netty.handler.codec.socksx.v5.Socks5AddressType.IPv4))
io.netty.handler.codec.socksx.v5.Socks5CommandStatus.FAILURE,
io.netty.handler.codec.socksx.v5.Socks5AddressType.IPv4))
.addListener(ChannelFutureListener.CLOSE);
}
}