This commit is contained in:
chenhaodong
2023-07-05 11:06:03 +08:00
parent 8de47479c5
commit 7c55f8da61
234 changed files with 54941 additions and 18 deletions
+13
View File
@@ -0,0 +1,13 @@
package org.fengfei.lanproxy.server;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
public interface Constants {
public static final AttributeKey<Channel> NEXT_CHANNEL = AttributeKey.newInstance("nxt_channel");
public static final AttributeKey<String> USER_ID = AttributeKey.newInstance("user_id");
public static final AttributeKey<String> CLIENT_KEY = AttributeKey.newInstance("client_key");
}
+15
View File
@@ -0,0 +1,15 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.fengfei</groupId>
<artifactId>yxwlkjnwct</artifactId>
<version>0.1</version>
</parent>
<artifactId>proxy-protocol</artifactId>
<packaging>jar</packaging>
<name>proxy-protocol</name>
<url>http://maven.apache.org</url>
<dependencies>
</dependencies>
</project>
@@ -0,0 +1,13 @@
package org.fengfei.lanproxy.protocol;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
public interface Constants {
public static final AttributeKey<Channel> NEXT_CHANNEL = AttributeKey.newInstance("nxt_channel");
public static final AttributeKey<String> USER_ID = AttributeKey.newInstance("user_id");
public static final AttributeKey<String> CLIENT_KEY = AttributeKey.newInstance("client_key");
}
@@ -0,0 +1,44 @@
package org.fengfei.lanproxy.protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
/**
* check idle chanel.
*
* @author fengfei
*
*/
public class IdleCheckHandler extends IdleStateHandler {
public static final int USER_CHANNEL_READ_IDLE_TIME = 1200;
public static final int READ_IDLE_TIME = 60;
public static final int WRITE_IDLE_TIME = 40;
private static Logger logger = LoggerFactory.getLogger(IdleCheckHandler.class);
public IdleCheckHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) {
super(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds);
}
@Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
if (IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT == evt) {
logger.debug("channel write timeout {}", ctx.channel());
ProxyMessage proxyMessage = new ProxyMessage();
proxyMessage.setType(ProxyMessage.TYPE_HEARTBEAT);
ctx.channel().writeAndFlush(proxyMessage);
} else if (IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT == evt) {
logger.warn("channel read timeout {}", ctx.channel());
ctx.channel().close();
}
super.channelIdle(ctx, evt);
}
}
@@ -0,0 +1,83 @@
package org.fengfei.lanproxy.protocol;
import java.util.Arrays;
/**
* 代理客户端与代理服务器消息交换协议
*
* @author fengfei
*
*/
public class ProxyMessage {
/** 心跳消息 */
public static final byte TYPE_HEARTBEAT = 0x07;
/** 认证消息,检测clientKey是否正确 */
public static final byte C_TYPE_AUTH = 0x01;
// /** 保活确认消息 */
// public static final byte TYPE_ACK = 0x02;
/** 代理后端服务器建立连接消息 */
public static final byte TYPE_CONNECT = 0x03;
/** 代理后端服务器断开连接消息 */
public static final byte TYPE_DISCONNECT = 0x04;
/** 代理数据传输 */
public static final byte P_TYPE_TRANSFER = 0x05;
/** 用户与代理服务器以及代理客户端与真实服务器连接是否可写状态同步 */
public static final byte C_TYPE_WRITE_CONTROL = 0x06;
/** 消息类型 */
private byte type;
/** 消息流水号 */
private long serialNumber;
/** 消息命令请求信息 */
private String uri;
/** 消息传输数据 */
private byte[] data;
public void setUri(String uri) {
this.uri = uri;
}
public String getUri() {
return uri;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public byte getType() {
return type;
}
public void setType(byte type) {
this.type = type;
}
public long getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(long serialNumber) {
this.serialNumber = serialNumber;
}
@Override
public String toString() {
return "ProxyMessage [type=" + type + ", serialNumber=" + serialNumber + ", uri=" + uri + ", data=" + Arrays.toString(data) + "]";
}
}
@@ -0,0 +1,78 @@
package org.fengfei.lanproxy.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
public class ProxyMessageDecoder extends LengthFieldBasedFrameDecoder {
private static final byte HEADER_SIZE = 4;
private static final int TYPE_SIZE = 1;
private static final int SERIAL_NUMBER_SIZE = 8;
private static final int URI_LENGTH_SIZE = 1;
/**
* @param maxFrameLength
* @param lengthFieldOffset
* @param lengthFieldLength
* @param lengthAdjustment
* @param initialBytesToStrip
*/
public ProxyMessageDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment,
int initialBytesToStrip) {
super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip);
}
/**
* @param maxFrameLength
* @param lengthFieldOffset
* @param lengthFieldLength
* @param lengthAdjustment
* @param initialBytesToStrip
* @param failFast
*/
public ProxyMessageDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment,
int initialBytesToStrip, boolean failFast) {
super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
}
@Override
protected ProxyMessage decode(ChannelHandlerContext ctx, ByteBuf in2) throws Exception {
ByteBuf in = (ByteBuf) super.decode(ctx, in2);
if (in == null) {
return null;
}
if (in.readableBytes() < HEADER_SIZE) {
return null;
}
int frameLength = in.readInt();
if (in.readableBytes() < frameLength) {
return null;
}
ProxyMessage proxyMessage = new ProxyMessage();
byte type = in.readByte();
long sn = in.readLong();
proxyMessage.setSerialNumber(sn);
proxyMessage.setType(type);
byte uriLength = in.readByte();
byte[] uriBytes = new byte[uriLength];
in.readBytes(uriBytes);
proxyMessage.setUri(new String(uriBytes));
byte[] data = new byte[frameLength - TYPE_SIZE - SERIAL_NUMBER_SIZE - URI_LENGTH_SIZE - uriLength];
in.readBytes(data);
proxyMessage.setData(data);
in.release();
return proxyMessage;
}
}
@@ -0,0 +1,45 @@
package org.fengfei.lanproxy.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
public class ProxyMessageEncoder extends MessageToByteEncoder<ProxyMessage> {
private static final int TYPE_SIZE = 1;
private static final int SERIAL_NUMBER_SIZE = 8;
private static final int URI_LENGTH_SIZE = 1;
@Override
protected void encode(ChannelHandlerContext ctx, ProxyMessage msg, ByteBuf out) throws Exception {
int bodyLength = TYPE_SIZE + SERIAL_NUMBER_SIZE + URI_LENGTH_SIZE;
byte[] uriBytes = null;
if (msg.getUri() != null) {
uriBytes = msg.getUri().getBytes();
bodyLength += uriBytes.length;
}
if (msg.getData() != null) {
bodyLength += msg.getData().length;
}
// write the total packet length but without length field's length.
out.writeInt(bodyLength);
out.writeByte(msg.getType());
out.writeLong(msg.getSerialNumber());
if (uriBytes != null) {
out.writeByte((byte) uriBytes.length);
out.writeBytes(uriBytes);
} else {
out.writeByte((byte) 0x00);
}
if (msg.getData() != null) {
out.writeBytes(msg.getData());
}
}
}
@@ -0,0 +1,5 @@
#Generated by Maven
#Wed Jul 05 11:03:47 CST 2023
groupId=org.fengfei
artifactId=proxy-protocol
version=0.1
@@ -0,0 +1,5 @@
org\fengfei\lanproxy\protocol\Constants.class
org\fengfei\lanproxy\protocol\ProxyMessage.class
org\fengfei\lanproxy\protocol\IdleCheckHandler.class
org\fengfei\lanproxy\protocol\ProxyMessageEncoder.class
org\fengfei\lanproxy\protocol\ProxyMessageDecoder.class
@@ -0,0 +1,5 @@
E:\yx\lanproxy-0.1\proxy-protocol\src\main\java\org\fengfei\lanproxy\protocol\Constants.java
E:\yx\lanproxy-0.1\proxy-protocol\src\main\java\org\fengfei\lanproxy\protocol\ProxyMessageEncoder.java
E:\yx\lanproxy-0.1\proxy-protocol\src\main\java\org\fengfei\lanproxy\protocol\ProxyMessageDecoder.java
E:\yx\lanproxy-0.1\proxy-protocol\src\main\java\org\fengfei\lanproxy\protocol\ProxyMessage.java
E:\yx\lanproxy-0.1\proxy-protocol\src\main\java\org\fengfei\lanproxy\protocol\IdleCheckHandler.java