Skip to content

Commit 01cdcb1

Browse files
committed
feat: 按照JavaDoc规范调整代码
1 parent a07c1d6 commit 01cdcb1

File tree

7 files changed

+92
-6
lines changed

7 files changed

+92
-6
lines changed

src/main/java/top/meethigher/proxy/http/ReverseHttpProxy.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ protected static String generateName() {
254254
/**
255255
* 更新路由内数据。
256256
* 该内容以路由为单位。
257+
*
258+
* @param route 路由对象
259+
* @param key 元数据键
260+
* @param value 元数据值
257261
*/
258262
protected void setRouteMetadata(Route route, String key, Object value) {
259263
route.putMetadata(key, value == null ? "" : value);
@@ -268,6 +272,10 @@ protected Object getRouteMetadata(Route route, String key) {
268272
/**
269273
* 更新请求上下文内数据。
270274
* 该内容以请求为单位。对于请求来说,是线程安全的。
275+
*
276+
* @param ctx 路由上下文
277+
* @param key 数据键
278+
* @param value 数据值
271279
*/
272280
protected void setContextData(RoutingContext ctx, String key, Object value) {
273281
ctx.put(key, value == null ? "" : value);
@@ -348,7 +356,12 @@ public ReverseHttpProxy addRoute(ProxyRoute proxyRoute, Integer order) {
348356
}
349357

350358
/**
351-
* order越小,优先级越高
359+
* 添加路由
360+
*
361+
* @param proxyRoute 路由信息
362+
* @param order order越小,优先级越高
363+
* @param printLog true表示打印日志
364+
* @return 实例本身
352365
*/
353366
public ReverseHttpProxy addRoute(
354367
ProxyRoute proxyRoute,
@@ -406,6 +419,9 @@ public List<Route> getRoutes() {
406419
/**
407420
* 将标头转为小写后,判断是否是逐跳标头
408421
* 时间复杂度为 O(1)
422+
*
423+
* @param headerName 标头名称
424+
* @return 是否是逐跳标头
409425
*/
410426
protected boolean isHopByHopHeader(String headerName) {
411427
return headerName != null && HOP_BY_HOP_HEADERS_SET.contains(headerName.toLowerCase());
@@ -414,6 +430,10 @@ protected boolean isHopByHopHeader(String headerName) {
414430

415431
/**
416432
* 复制请求头。复制的过程中忽略逐跳标头
433+
*
434+
* @param ctx 路由上下文
435+
* @param realReq 真实请求
436+
* @param proxyReq 代理请求
417437
*/
418438
protected void copyRequestHeaders(RoutingContext ctx, HttpServerRequest realReq, HttpClientRequest proxyReq) {
419439
proxyReq.headers().clear();
@@ -451,6 +471,11 @@ protected void copyRequestHeaders(RoutingContext ctx, HttpServerRequest realReq,
451471

452472
/**
453473
* 复制响应头。复制的过程中忽略逐跳标头
474+
*
475+
* @param ctx 路由上下文
476+
* @param realReq 真实请求
477+
* @param realResp 真实响应
478+
* @param proxyResp 代理响应
454479
*/
455480
protected void copyResponseHeaders(RoutingContext ctx, HttpServerRequest realReq, HttpServerResponse realResp, HttpClientResponse proxyResp) {
456481
realResp.headers().clear();
@@ -510,6 +535,11 @@ else if ("Location".equalsIgnoreCase(headerName)) {
510535

511536
/**
512537
* 重写Location
538+
*
539+
* @param ctx 路由上下文
540+
* @param url 原始URL
541+
* @param location 重定向位置
542+
* @return 重写后的Location
513543
*/
514544
protected String rewriteLocation(RoutingContext ctx, String url, String location) {
515545
// 若重定向的地址,在反向代理的范围内,则进行重写
@@ -525,6 +555,12 @@ protected String rewriteLocation(RoutingContext ctx, String url, String location
525555

526556
/**
527557
* 发起请求Handler
558+
*
559+
* @param ctx 路由上下文
560+
* @param serverReq 服务端请求
561+
* @param serverResp 服务端响应
562+
* @param proxyUrl 代理URL
563+
* @return 异步处理Handler
528564
*/
529565
protected Handler<AsyncResult<HttpClientResponse>> sendRequestHandler(RoutingContext ctx, HttpServerRequest serverReq, HttpServerResponse serverResp, String proxyUrl) {
530566
return ar -> {
@@ -560,6 +596,12 @@ protected Handler<AsyncResult<HttpClientResponse>> sendRequestHandler(RoutingCon
560596

561597
/**
562598
* 建立连接Handler
599+
*
600+
* @param ctx 路由上下文
601+
* @param serverReq 服务端请求
602+
* @param serverResp 服务端响应
603+
* @param proxyUrl 代理URL
604+
* @return 异步处理Handler
563605
*/
564606
protected Handler<AsyncResult<HttpClientRequest>> connectHandler(RoutingContext ctx, HttpServerRequest serverReq, HttpServerResponse serverResp, String proxyUrl) {
565607
return ar -> {
@@ -611,6 +653,9 @@ protected void badGateway(RoutingContext ctx, HttpServerResponse serverResp) {
611653

612654
/**
613655
* 路由处理Handler
656+
*
657+
* @param httpClient HTTP客户端
658+
* @return 路由处理Handler
614659
*/
615660
protected Handler<RoutingContext> routingContextHandler(HttpClient httpClient) {
616661
return ctx -> {
@@ -685,6 +730,11 @@ protected Handler<RoutingContext> routingContextHandler(HttpClient httpClient) {
685730
/**
686731
* 获取代理后的完整proxyUrl,不区分代理目标路径是否以/结尾。
687732
* 处理逻辑为删除掉匹配的路径,并将剩下的内容追加到代理目标路径后面。
733+
*
734+
* @param ctx 路由上下文
735+
* @param serverReq 服务端请求
736+
* @param serverResp 服务端响应
737+
* @return 代理后的完整URL
688738
*/
689739
protected String getProxyUrl(RoutingContext ctx, HttpServerRequest serverReq, HttpServerResponse serverResp) {
690740
String targetUrl = getContextData(ctx, P_TARGET_URL).toString();

src/main/java/top/meethigher/proxy/http/UrlParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ public class UrlParser {
77

88
/**
99
* 在压测时,性能比String.replace略优
10+
* @param text 需要替换的文本
11+
* @param search 要搜索的字符串
12+
* @param replacement 替换的字符串
13+
* @return 替换后的文本
1014
*/
1115
public static String fastReplace(String text, String search, String replacement) {
1216
if (text == null || search == null || replacement == null || search.isEmpty()) {
@@ -25,6 +29,9 @@ public static String fastReplace(String text, String search, String replacement)
2529

2630
/**
2731
* 拼接两个uri。不要求uri以"/"结尾
32+
* @param uri1 第一个URI字符串
33+
* @param uri2 第二个URI字符串
34+
* @return 拼接后的URI字符串
2835
*/
2936
public static String joinURI(String uri1, String uri2) {
3037
if (uri1.endsWith("/") && uri2.startsWith("/")) {

src/main/java/top/meethigher/proxy/tcp/tunnel/ReverseTcpProxyTunnelServer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public DataProxyServer(Vertx vertx, String name,
206206
* <p>
207207
* 通过标识码判定是用户连接还是数据连接,通过唯一编号判定用户连接和数据连接的对应关系
208208
*
209-
* @param socket 连接
209+
* @param socket 建立的网络连接Socket,需要通过前缀字节判断其类型(用户连接或数据连接)
210210
*/
211211
protected void handleConnect(NetSocket socket) {
212212
socket.pause();
@@ -240,8 +240,8 @@ protected void handleConnect(NetSocket socket) {
240240
/**
241241
* 数据连接的处理逻辑
242242
*
243-
* @param socket 连接
244-
* @param buf 数据
243+
* @param socket 数据连接的Socket
244+
* @param buf 接收到的数据缓冲区,包含4字节标识码和4字节会话ID
245245
* @param timerId 延时判定数据连接的定时器id,-1表示不存在定时器
246246
*/
247247
protected void handleDataConnection(NetSocket socket, Buffer buf, long timerId) {
@@ -264,8 +264,8 @@ protected void handleDataConnection(NetSocket socket, Buffer buf, long timerId)
264264
/**
265265
* 用户连接的处理逻辑
266266
*
267-
* @param socket 连接
268-
* @param buf 数据
267+
* @param socket 用户连接的Socket
268+
* @param buf 接收到的数据缓冲区,可能为null表示没有接收到数据
269269
* @param timerId 延时判定数据连接的定时器id,-1表示不存在定时器
270270
*/
271271
protected void handleUserConnection(NetSocket socket, Buffer buf, long timerId) {

src/main/java/top/meethigher/proxy/tcp/tunnel/Tunnel.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ protected Tunnel(Vertx vertx) {
4545

4646
/**
4747
* 监听socket连接成功触发的动作
48+
*
49+
* @param tunnelHandler 连接成功后处理逻辑
4850
*/
4951
public void onConnected(TunnelHandler tunnelHandler) {
5052
tunnelHandlers.put(null, tunnelHandler);
5153
}
5254

5355
/**
5456
* 监听 {@code TunnelMessageType} 事件
57+
*
58+
* @param type 消息类型
59+
* @param tunnelHandler 处理逻辑
5560
*/
5661
public void on(TunnelMessageType type, TunnelHandler tunnelHandler) {
5762
tunnelHandlers.put(type, tunnelHandler);

src/main/java/top/meethigher/proxy/tcp/tunnel/codec/TunnelMessageCodec.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
public class TunnelMessageCodec {
1616

1717

18+
/**
19+
* 将消息类型和消息体编码为Buffer
20+
*
21+
* @param type 消息类型,2字节
22+
* @param body 消息体字节数组
23+
* @return 编码后的Buffer,格式为:4字节消息长度 + 2字节消息类型 + 变长消息体
24+
*/
1825
public static Buffer encode(short type, byte[] body) {
1926
int totalLength = 4 + 2 + body.length;
2027
Buffer buffer = Buffer.buffer();
@@ -24,13 +31,22 @@ public static Buffer encode(short type, byte[] body) {
2431
return buffer;
2532
}
2633

34+
/**
35+
* 将Buffer解码为DecodedMessage对象
36+
*
37+
* @param buffer 待解码的Buffer,格式为:4字节消息长度 + 2字节消息类型 + 变长消息体
38+
* @return 解码后的DecodedMessage对象,包含总长度、消息类型和消息体
39+
*/
2740
public static DecodedMessage decode(Buffer buffer) {
2841
int totalLength = buffer.getInt(0);
2942
short type = buffer.getShort(4);
3043
byte[] body = buffer.getBytes(6, buffer.length());
3144
return new DecodedMessage(totalLength, type, body);
3245
}
3346

47+
/**
48+
* 解码后的消息对象,包含总长度、消息类型和消息体
49+
*/
3450
public static class DecodedMessage {
3551
public final int totalLength;
3652
public final short type;

src/main/java/top/meethigher/proxy/tcp/tunnel/handler/AbstractTunnelHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public void handle(Vertx vertx, NetSocket netSocket, Buffer buffer) {
3434

3535
/**
3636
* 执行逻辑,并返回执行结果。true表示成功
37+
*
38+
* @param vertx 用于执行异步操作的Vertx实例
39+
* @param netSocket 网络连接Socket
40+
* @param type 消息类型
41+
* @param bodyBytes 消息体字节数组
42+
* @return 处理结果,true表示成功,false表示失败
3743
*/
3844
protected abstract boolean doHandle(Vertx vertx, NetSocket netSocket, TunnelMessageType type, byte[] bodyBytes);
3945
}

src/main/java/top/meethigher/proxy/tcp/tunnel/utils/IdGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class IdGenerator {
1515
/**
1616
* 获取一个 4 字节的唯一编号
1717
* 超过 Integer.MAX_VALUE 后从 1 重新开始(跳过负数)
18+
*
19+
* @return 编号
1820
*/
1921
public static int nextId() {
2022
int id = counter.getAndIncrement();

0 commit comments

Comments
 (0)