1
1
package top .meethigher .proxy .http ;
2
2
3
- import java .net .URL ;
3
+ import java .io .Serializable ;
4
+ import java .util .LinkedHashMap ;
5
+ import java .util .Map ;
4
6
5
7
/**
6
8
* 服务信息
7
9
*
8
10
* @author chenchuancheng
9
11
* @since 2024/07/17 23:21
10
12
*/
11
- public class ProxyRoute {
12
-
13
- private boolean enable ;
13
+ public class ProxyRoute implements Serializable {
14
14
15
15
private String name ;
16
+
16
17
/**
17
18
* /* represents all interfaces below proxy /, no distinction between /* and /**
18
19
*/
19
20
private String sourceUrl ;
20
21
21
22
private String targetUrl ;
22
23
23
- private boolean forwardIp ;
24
-
25
- private boolean preserveCookies ;
26
-
27
- private boolean preserveHost ;
28
-
29
- private boolean followRedirects ;
24
+ private boolean forwardIp = false ;
30
25
31
- private LOG log ;
26
+ private boolean preserveCookies = true ;
32
27
33
- private CORSControl corsControl ;
28
+ private boolean preserveHost = false ;
34
29
30
+ private boolean followRedirects = true ;
35
31
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 ;
55
33
56
- public boolean isEnable () {
57
- return enable ;
58
- }
34
+ private LOG log = new LOG ();
59
35
60
- public void setEnable (boolean enable ) {
61
- this .enable = enable ;
62
- }
36
+ private CORSControl corsControl = new CORSControl ();
63
37
64
- public boolean isAllowCORS () {
65
- return allowCORS ;
66
- }
67
38
68
- public void setAllowCORS (boolean allowCORS ) {
69
- this .allowCORS = allowCORS ;
70
- }
39
+ public String getSourceUrl () {
40
+ return sourceUrl ;
71
41
}
72
42
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
+ }
83
46
84
- public void setEnable ( boolean enable ) {
85
- this . enable = enable ;
86
- }
47
+ public String getTargetUrl ( ) {
48
+ return targetUrl ;
49
+ }
87
50
88
- public String getLogFormat ( ) {
89
- return logFormat ;
90
- }
51
+ public void setTargetUrl ( String targetUrl ) {
52
+ this . targetUrl = targetUrl ;
53
+ }
91
54
92
- public void setLogFormat (String logFormat ) {
93
- this .logFormat = logFormat ;
94
- }
55
+ public LOG getLog () {
56
+ return log ;
95
57
}
96
58
59
+ public boolean isHttpKeepAlive () {
60
+ return httpKeepAlive ;
61
+ }
97
62
98
- public String getName ( ) {
99
- return name ;
63
+ public void setHttpKeepAlive ( boolean httpKeepAlive ) {
64
+ this . httpKeepAlive = httpKeepAlive ;
100
65
}
101
66
102
- public void setName ( String name ) {
103
- this .name = name ;
67
+ public void setLog ( LOG log ) {
68
+ this .log = log ;
104
69
}
105
70
106
- public String getSourceUrl () {
107
- return sourceUrl ;
71
+ public CORSControl getCorsControl () {
72
+ return corsControl ;
108
73
}
109
74
110
- public void setSourceUrl ( String sourceUrl ) {
111
- this .sourceUrl = sourceUrl ;
75
+ public void setCorsControl ( CORSControl corsControl ) {
76
+ this .corsControl = corsControl ;
112
77
}
113
78
114
- public String getTargetUrl () {
115
- return targetUrl ;
79
+ public String getName () {
80
+ return name ;
116
81
}
117
82
118
- public void setTargetUrl (String targetUrl ) {
119
- this .targetUrl = targetUrl ;
83
+ public void setName (String name ) {
84
+ this .name = name ;
120
85
}
121
86
122
87
public boolean isForwardIp () {
@@ -151,58 +116,82 @@ public void setFollowRedirects(boolean followRedirects) {
151
116
this .followRedirects = followRedirects ;
152
117
}
153
118
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 ;
156
134
}
157
135
158
- public void setLog (LOG log ) {
159
- this .log = log ;
160
- }
161
136
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 ;
165
149
166
- public void setEnable (boolean enable ) {
167
- this .enable = enable ;
168
- }
150
+ /**
151
+ * 当enable=true时,该参数才会生效
152
+ * 如果该参数为true表示所有经过代理的服务都允许跨域
153
+ * 如果该参数为true表示所有经过代理的服务均不允许跨域
154
+ */
155
+ private boolean allowCORS ;
169
156
170
- public CORSControl getCorsControl () {
171
- return corsControl ;
172
- }
157
+ public boolean isEnable () {
158
+ return enable ;
159
+ }
173
160
174
- public void setCorsControl ( CORSControl corsControl ) {
175
- this .corsControl = corsControl ;
176
- }
161
+ public void setEnable ( boolean enable ) {
162
+ this .enable = enable ;
163
+ }
177
164
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 ;
205
171
}
206
172
}
207
173
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
+ }
208
197
}
0 commit comments