ProtoUtil.java
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.github.monkeywie.proxyee.util;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpRequest;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;
public class ProtoUtil {
/*
代理服务器需要处理两种握手类型,一种是非CONNECT的http报文代理,另外一种是CONNECT的TCP报文原始转发
示例:
GET http://www.google.com/ HTTP/1.1
CONNECT www.google.com:443 HTTP/1.1
CONNECT echo.websocket.org:443 HTTP/1.1
CONNECT echo.websocket.org:80 HTTP/1.1
当客户端请求协议为TLS(https、wss)、WebSocket(ws)的时候,都会发起CONNECT请求进行原始转发,
所以在握手的时候是无法区分客户端原始请求是否为TLS。
*/
public static RequestProto getRequestProto(HttpRequest httpRequest) {
RequestProto requestProto = new RequestProto();
String uri = httpRequest.uri().toLowerCase();
if (!uri.startsWith("http://")) {
uri = "http://" + uri;
}
URL url;
try {
url = new URL(uri);
} catch (MalformedURLException e) {
return null;
}
requestProto.setHost(url.getHost());
requestProto.setPort(url.getPort() != -1 ? url.getPort() : url.getDefaultPort());
requestProto.setProxy(httpRequest.headers().contains(HttpHeaderNames.PROXY_CONNECTION));
return requestProto;
}
public static class RequestProto implements Serializable {
private static final long serialVersionUID = -6471051659605127698L;
/**
* 请求是否来源于http代理,用于区分是通过代理服务访问的还是直接通过http访问的代理服务器
*/
private boolean proxy;
private String host;
private int port;
private boolean ssl;
public RequestProto() {
}
public RequestProto(String host, int port, boolean ssl) {
this.host = host;
this.port = port;
this.ssl = ssl;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public boolean getSsl() {
return ssl;
}
public void setSsl(boolean ssl) {
this.ssl = ssl;
}
public boolean getProxy() {
return proxy;
}
public void setProxy(boolean proxy) {
this.proxy = proxy;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RequestProto that = (RequestProto) o;
return port == that.port &&
ssl == that.ssl &&
host.equals(that.host);
}
@Override
public int hashCode() {
return Objects.hash(host, port, ssl);
}
public RequestProto copy() {
RequestProto requestProto = new RequestProto();
requestProto.setProxy(proxy);
requestProto.setHost(host);
requestProto.setPort(port);
requestProto.setSsl(ssl);
return requestProto;
}
}
}