Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java
41152 views
1
/*
2
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.net;
27
28
import java.io.IOException;
29
import java.lang.reflect.Field;
30
import java.lang.reflect.Method;
31
import java.util.HashMap;
32
import java.util.Map;
33
import java.util.Set;
34
35
/**
36
* Basic SocketImpl that relies on the internal HTTP protocol handler
37
* implementation to perform the HTTP tunneling and authentication. Once
38
* connected, all socket operations delegate to a platform SocketImpl.
39
*
40
* @since 1.8
41
*/
42
43
/*package*/ @SuppressWarnings("removal") class HttpConnectSocketImpl extends DelegatingSocketImpl {
44
45
private static final String httpURLClazzStr =
46
"sun.net.www.protocol.http.HttpURLConnection";
47
private static final String netClientClazzStr = "sun.net.NetworkClient";
48
private static final String doTunnelingStr = "doTunneling";
49
private static final Field httpField;
50
private static final Field serverSocketField;
51
private static final Method doTunneling;
52
53
private final String server;
54
private final Socket socket;
55
private InetSocketAddress external_address;
56
private HashMap<Integer, Object> optionsMap = new HashMap<>();
57
58
static {
59
try {
60
Class<?> httpClazz = Class.forName(httpURLClazzStr, true, null);
61
httpField = httpClazz.getDeclaredField("http");
62
doTunneling = httpClazz.getDeclaredMethod(doTunnelingStr);
63
Class<?> netClientClazz = Class.forName(netClientClazzStr, true, null);
64
serverSocketField = netClientClazz.getDeclaredField("serverSocket");
65
66
java.security.AccessController.doPrivileged(
67
new java.security.PrivilegedAction<>() {
68
public Void run() {
69
httpField.setAccessible(true);
70
serverSocketField.setAccessible(true);
71
return null;
72
}
73
});
74
} catch (ReflectiveOperationException x) {
75
throw new InternalError("Should not reach here", x);
76
}
77
}
78
79
HttpConnectSocketImpl(Proxy proxy, SocketImpl delegate, Socket socket) {
80
super(delegate);
81
this.socket = socket;
82
SocketAddress a = proxy.address();
83
if ( !(a instanceof InetSocketAddress ad) )
84
throw new IllegalArgumentException("Unsupported address type");
85
86
server = ad.getHostString();
87
port = ad.getPort();
88
}
89
90
@Override
91
protected void connect(String host, int port) throws IOException {
92
connect(new InetSocketAddress(host, port), 0);
93
}
94
95
@Override
96
protected void connect(InetAddress address, int port) throws IOException {
97
connect(new InetSocketAddress(address, port), 0);
98
}
99
100
@Override
101
protected void connect(SocketAddress endpoint, int timeout)
102
throws IOException
103
{
104
if (!(endpoint instanceof InetSocketAddress epoint))
105
throw new IllegalArgumentException("Unsupported address type");
106
String destHost = epoint.isUnresolved() ? epoint.getHostName()
107
: epoint.getAddress().getHostAddress();
108
final int destPort = epoint.getPort();
109
110
SecurityManager security = System.getSecurityManager();
111
if (security != null)
112
security.checkConnect(destHost, destPort);
113
114
if (destHost.contains(":"))
115
destHost = "[" + destHost + "]";
116
117
// Connect to the HTTP proxy server
118
String urlString = "http://" + destHost + ":" + destPort;
119
Socket httpSocket = privilegedDoTunnel(urlString, timeout);
120
121
// Success!
122
external_address = epoint;
123
124
// close the original socket impl and release its descriptor
125
close();
126
127
// update the Sockets impl to the impl from the http Socket
128
SocketImpl si = httpSocket.impl;
129
socket.setImpl(si);
130
131
// best effort is made to try and reset options previously set
132
Set<Map.Entry<Integer,Object>> options = optionsMap.entrySet();
133
try {
134
for(Map.Entry<Integer,Object> entry : options) {
135
si.setOption(entry.getKey(), entry.getValue());
136
}
137
} catch (IOException x) { /* gulp! */ }
138
}
139
140
141
@Override
142
protected void listen(int backlog) {
143
throw new InternalError("should not get here");
144
}
145
146
@Override
147
protected void accept(SocketImpl s) {
148
throw new InternalError("should not get here");
149
}
150
151
@Override
152
void reset() {
153
throw new InternalError("should not get here");
154
}
155
156
@Override
157
public void setOption(int opt, Object val) throws SocketException {
158
delegate.setOption(opt, val);
159
160
if (external_address != null)
161
return; // we're connected, just return
162
163
// store options so that they can be re-applied to the impl after connect
164
optionsMap.put(opt, val);
165
}
166
167
private Socket privilegedDoTunnel(final String urlString,
168
final int timeout)
169
throws IOException
170
{
171
try {
172
return java.security.AccessController.doPrivileged(
173
new java.security.PrivilegedExceptionAction<>() {
174
public Socket run() throws IOException {
175
return doTunnel(urlString, timeout);
176
}
177
});
178
} catch (java.security.PrivilegedActionException pae) {
179
throw (IOException) pae.getException();
180
}
181
}
182
183
private Socket doTunnel(String urlString, int connectTimeout)
184
throws IOException
185
{
186
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(server, port));
187
URL destURL = new URL(urlString);
188
HttpURLConnection conn = (HttpURLConnection) destURL.openConnection(proxy);
189
conn.setConnectTimeout(connectTimeout);
190
int timeout = (int) getOption(SocketOptions.SO_TIMEOUT);
191
if (timeout > 0) {
192
conn.setReadTimeout(timeout);
193
}
194
conn.connect();
195
doTunneling(conn);
196
try {
197
Object httpClient = httpField.get(conn);
198
return (Socket) serverSocketField.get(httpClient);
199
} catch (IllegalAccessException x) {
200
throw new InternalError("Should not reach here", x);
201
}
202
}
203
204
private void doTunneling(HttpURLConnection conn) throws IOException {
205
try {
206
doTunneling.invoke(conn);
207
} catch (ReflectiveOperationException x) {
208
Throwable cause = x.getCause();
209
if (cause instanceof IOException) {
210
throw (IOException) cause;
211
}
212
throw new InternalError("Should not reach here", x);
213
}
214
}
215
216
@Override
217
protected InetAddress getInetAddress() {
218
if (external_address != null)
219
return external_address.getAddress();
220
else
221
return delegate.getInetAddress();
222
}
223
224
@Override
225
protected int getPort() {
226
if (external_address != null)
227
return external_address.getPort();
228
else
229
return delegate.getPort();
230
}
231
}
232
233