Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/x509/URICertStore/SocksProxy.java
41153 views
1
/*
2
* Copyright (c) 2016, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.net.InetAddress;
26
import java.net.ServerSocket;
27
import java.net.Socket;
28
import java.util.Objects;
29
import java.util.function.Consumer;
30
31
import javax.net.ServerSocketFactory;
32
33
/*
34
* A simple socks proxy for traveling socket.
35
*/
36
class SocksProxy implements Runnable, AutoCloseable {
37
38
private ServerSocket server;
39
private Consumer<Socket> socketConsumer;
40
41
private SocksProxy(ServerSocket server, Consumer<Socket> socketConsumer) {
42
this.server = server;
43
this.socketConsumer = socketConsumer;
44
}
45
46
static SocksProxy startProxy(Consumer<Socket> socketConsumer)
47
throws IOException {
48
Objects.requireNonNull(socketConsumer, "socketConsumer cannot be null");
49
50
ServerSocket server
51
= ServerSocketFactory.getDefault().createServerSocket(0);
52
53
System.setProperty("socksProxyHost",
54
InetAddress.getLoopbackAddress().getHostAddress());
55
System.setProperty("socksProxyPort",
56
String.valueOf(server.getLocalPort()));
57
System.setProperty("socksProxyVersion", "5");
58
59
SocksProxy proxy = new SocksProxy(server, socketConsumer);
60
Thread proxyThread = new Thread(proxy, "Proxy");
61
proxyThread.setDaemon(true);
62
proxyThread.start();
63
64
return proxy;
65
}
66
67
@Override
68
public void run() {
69
while (!server.isClosed()) {
70
try(Socket socket = server.accept()) {
71
System.out.println("Server: accepted connection");
72
if (socketConsumer != null) {
73
socketConsumer.accept(socket);
74
}
75
} catch (IOException e) {
76
if (!server.isClosed()) {
77
throw new RuntimeException(
78
"Server: accept connection failed", e);
79
} else {
80
System.out.println("Server is closed.");
81
}
82
}
83
}
84
}
85
86
@Override
87
public void close() throws Exception {
88
if (!server.isClosed()) {
89
server.close();
90
}
91
}
92
93
int getPort() {
94
return server.getLocalPort();
95
}
96
}
97
98