Skip to content

Commit cebbac3

Browse files
committed
feat: 初步完成基于vertx实现的HTTP反向代理
1 parent 175c596 commit cebbac3

File tree

6 files changed

+417
-460
lines changed

6 files changed

+417
-460
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>top.meethigher</groupId>
88
<artifactId>tcp-reverse-proxy</artifactId>
9-
<version>1.0</version>
9+
<version>1.0.0</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
Lines changed: 108 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,87 @@
11
package top.meethigher.proxy.http;
22

3-
import java.net.URL;
3+
import java.io.Serializable;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
46

57
/**
68
* 服务信息
79
*
810
* @author chenchuancheng
911
* @since 2024/07/17 23:21
1012
*/
11-
public class ProxyRoute {
12-
13-
private boolean enable;
13+
public class ProxyRoute implements Serializable {
1414

1515
private String name;
16+
1617
/**
1718
* /* represents all interfaces below proxy /, no distinction between /* and /**
1819
*/
1920
private String sourceUrl;
2021

2122
private String targetUrl;
2223

23-
private boolean forwardIp;
24-
25-
private boolean preserveCookies;
26-
27-
private boolean preserveHost;
28-
29-
private boolean followRedirects;
24+
private boolean forwardIp = false;
3025

31-
private LOG log;
26+
private boolean preserveCookies = true;
3227

33-
private CORSControl corsControl;
28+
private boolean preserveHost = false;
3429

30+
private boolean followRedirects = true;
3531

36-
/**
37-
* 跨域控制
38-
*
39-
* @author chenchuancheng
40-
* @since 2024/06/29 11:59
41-
*/
42-
public static class CORSControl {
43-
/**
44-
* true表示所有的代理请求的跨域都由自己管理
45-
* false表示所有的代理请求的跨域由被代理方控制
46-
*/
47-
private boolean enable;
48-
49-
/**
50-
* 当enable=true时,该参数才会生效
51-
* 如果该参数为true表示所有经过代理的服务都允许跨域
52-
* 如果该参数为true表示所有经过代理的服务均不允许跨域
53-
*/
54-
private boolean allowCORS;
32+
private boolean httpKeepAlive = true;
5533

56-
public boolean isEnable() {
57-
return enable;
58-
}
34+
private LOG log = new LOG();
5935

60-
public void setEnable(boolean enable) {
61-
this.enable = enable;
62-
}
36+
private CORSControl corsControl = new CORSControl();
6337

64-
public boolean isAllowCORS() {
65-
return allowCORS;
66-
}
6738

68-
public void setAllowCORS(boolean allowCORS) {
69-
this.allowCORS = allowCORS;
70-
}
39+
public String getSourceUrl() {
40+
return sourceUrl;
7141
}
7242

73-
public static class LOG {
74-
private boolean enable;
75-
/**
76-
* Configure the agent’s log format. The options are remoteAddr、remotePort、userAgent、method、source、target
77-
*/
78-
private String logFormat;
79-
80-
public boolean isEnable() {
81-
return enable;
82-
}
43+
public void setSourceUrl(String sourceUrl) {
44+
this.sourceUrl = sourceUrl;
45+
}
8346

84-
public void setEnable(boolean enable) {
85-
this.enable = enable;
86-
}
47+
public String getTargetUrl() {
48+
return targetUrl;
49+
}
8750

88-
public String getLogFormat() {
89-
return logFormat;
90-
}
51+
public void setTargetUrl(String targetUrl) {
52+
this.targetUrl = targetUrl;
53+
}
9154

92-
public void setLogFormat(String logFormat) {
93-
this.logFormat = logFormat;
94-
}
55+
public LOG getLog() {
56+
return log;
9557
}
9658

59+
public boolean isHttpKeepAlive() {
60+
return httpKeepAlive;
61+
}
9762

98-
public String getName() {
99-
return name;
63+
public void setHttpKeepAlive(boolean httpKeepAlive) {
64+
this.httpKeepAlive = httpKeepAlive;
10065
}
10166

102-
public void setName(String name) {
103-
this.name = name;
67+
public void setLog(LOG log) {
68+
this.log = log;
10469
}
10570

106-
public String getSourceUrl() {
107-
return sourceUrl;
71+
public CORSControl getCorsControl() {
72+
return corsControl;
10873
}
10974

110-
public void setSourceUrl(String sourceUrl) {
111-
this.sourceUrl = sourceUrl;
75+
public void setCorsControl(CORSControl corsControl) {
76+
this.corsControl = corsControl;
11277
}
11378

114-
public String getTargetUrl() {
115-
return targetUrl;
79+
public String getName() {
80+
return name;
11681
}
11782

118-
public void setTargetUrl(String targetUrl) {
119-
this.targetUrl = targetUrl;
83+
public void setName(String name) {
84+
this.name = name;
12085
}
12186

12287
public boolean isForwardIp() {
@@ -151,58 +116,82 @@ public void setFollowRedirects(boolean followRedirects) {
151116
this.followRedirects = followRedirects;
152117
}
153118

154-
public LOG getLog() {
155-
return log;
119+
public Map<String, String> toMap() {
120+
Map<String, String> map = new LinkedHashMap<>();
121+
map.put("name", getName());
122+
map.put("sourceUrl", getSourceUrl());
123+
map.put("targetUrl", getTargetUrl());
124+
map.put("forwardIp", String.valueOf(isForwardIp()));
125+
map.put("preserveHost", String.valueOf(isPreserveHost()));
126+
map.put("preserveCookies", String.valueOf(isPreserveCookies()));
127+
map.put("followRedirects", String.valueOf(isFollowRedirects()));
128+
map.put("httpKeepAlive", String.valueOf(isHttpKeepAlive()));
129+
map.put("log.enable", String.valueOf(getLog().isEnable()));
130+
map.put("log.logFormat", String.valueOf(getLog().getLogFormat()));
131+
map.put("corsControl.enable", String.valueOf(getCorsControl().isEnable()));
132+
map.put("corsControl.allowCors", String.valueOf(getCorsControl().isAllowCORS()));
133+
return map;
156134
}
157135

158-
public void setLog(LOG log) {
159-
this.log = log;
160-
}
161136

162-
public boolean isEnable() {
163-
return enable;
164-
}
137+
/**
138+
* 跨域控制
139+
*
140+
* @author chenchuancheng
141+
* @since 2024/06/29 11:59
142+
*/
143+
public static class CORSControl {
144+
/**
145+
* true表示所有的代理请求的跨域都由自己管理
146+
* false表示所有的代理请求的跨域由被代理方控制
147+
*/
148+
private boolean enable = false;
165149

166-
public void setEnable(boolean enable) {
167-
this.enable = enable;
168-
}
150+
/**
151+
* 当enable=true时,该参数才会生效
152+
* 如果该参数为true表示所有经过代理的服务都允许跨域
153+
* 如果该参数为true表示所有经过代理的服务均不允许跨域
154+
*/
155+
private boolean allowCORS;
169156

170-
public CORSControl getCorsControl() {
171-
return corsControl;
172-
}
157+
public boolean isEnable() {
158+
return enable;
159+
}
173160

174-
public void setCorsControl(CORSControl corsControl) {
175-
this.corsControl = corsControl;
176-
}
161+
public void setEnable(boolean enable) {
162+
this.enable = enable;
163+
}
177164

178-
public String[] formatTargetUrl() throws IllegalArgumentException {
179-
try {
180-
URL url = new URL(targetUrl);
181-
String protocol = url.getProtocol();
182-
if (protocol == null || protocol.isEmpty()) {
183-
throw new IllegalArgumentException("Invalid URL: Protocol is missing");
184-
}
185-
String host = url.getHost();
186-
if (host == null || host.isEmpty()) {
187-
throw new IllegalArgumentException("Invalid URL: Host is missing");
188-
}
189-
int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort();
190-
if (port == -1) { // 如果协议未提供默认端口,则认为无效
191-
throw new IllegalArgumentException("Invalid URL: Port is missing");
192-
}
193-
String requestURI = url.getPath();
194-
if (requestURI == null || requestURI.isEmpty()) {
195-
requestURI = "/";
196-
}
197-
return new String[]{
198-
protocol,
199-
host,
200-
String.valueOf(port),
201-
requestURI
202-
};
203-
} catch (Exception e) {
204-
throw new IllegalArgumentException("Invalid URL format: " + targetUrl, e);
165+
public boolean isAllowCORS() {
166+
return allowCORS;
167+
}
168+
169+
public void setAllowCORS(boolean allowCORS) {
170+
this.allowCORS = allowCORS;
205171
}
206172
}
207173

174+
public static class LOG {
175+
private boolean enable = true;
176+
/**
177+
* Configure the agent’s log format. The options are remoteAddr、remotePort、userAgent、method、source、target
178+
*/
179+
private String logFormat = "{name} -- {method} -- {userAgent} -- {remoteAddr}:{remotePort} -- {source} --> {target} -- {statusCode} consumed {consumedMills} ms";
180+
181+
public boolean isEnable() {
182+
return enable;
183+
}
184+
185+
public void setEnable(boolean enable) {
186+
this.enable = enable;
187+
}
188+
189+
public String getLogFormat() {
190+
return logFormat;
191+
}
192+
193+
public void setLogFormat(String logFormat) {
194+
this.logFormat = logFormat;
195+
}
196+
}
208197
}

0 commit comments

Comments
 (0)