Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socks/SocksProxyVersion.java
41149 views
1
/*
2
* Copyright (c) 2011, 2019, 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
/*
25
* @test
26
* @bug 6964547 5001942 8129444
27
* @library /test/lib
28
* @run main/othervm SocksProxyVersion
29
* @summary test socksProxyVersion system property
30
*/
31
32
import java.net.InetSocketAddress;
33
import java.net.ServerSocket;
34
import java.net.Socket;
35
import java.net.SocketException;
36
import java.io.IOException;
37
import java.net.InetAddress;
38
import java.net.Proxy;
39
import jdk.test.lib.net.IPSupport;
40
41
public class SocksProxyVersion implements Runnable {
42
final ServerSocket ss;
43
volatile boolean failed;
44
volatile boolean stopped = false;
45
volatile int expected;
46
47
public static void main(String[] args) throws Exception {
48
if (InetAddress.getLocalHost().isLoopbackAddress()) {
49
System.out.println("Test cannot run. getLocalHost returns a loopback address");
50
return;
51
}
52
new SocksProxyVersion();
53
}
54
55
public SocksProxyVersion() throws Exception {
56
ss = new ServerSocket(0, 0, InetAddress.getLocalHost());
57
int port = ss.getLocalPort();
58
Thread serverThread = new Thread(this);
59
serverThread.start();
60
try (ServerSocket socket = ss) {
61
runTest(port);
62
} finally {
63
stopped = true;
64
}
65
66
serverThread.join();
67
if (failed) {
68
throw new RuntimeException("socksProxyVersion not being set correctly");
69
}
70
}
71
72
final void runTest(int port) throws Exception {
73
/*
74
* Retrieving the IP Address of the machine
75
* since "localhost" is bypassed as a non-proxy host
76
*/
77
String addr = InetAddress.getLocalHost().getHostAddress();
78
79
System.setProperty("socksProxyHost", addr);
80
System.setProperty("socksProxyPort", Integer.toString(port));
81
82
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
83
new InetSocketAddress(addr, port));
84
85
if (IPSupport.hasIPv4()) {
86
// SOCKS V4 (requires IPv4)
87
System.setProperty("socksProxyVersion", "4");
88
this.expected = 4;
89
check(new Socket(), addr, port);
90
check(new Socket(proxy), addr, port);
91
}
92
93
// SOCKS V5
94
System.setProperty("socksProxyVersion", "5");
95
this.expected = 5;
96
check(new Socket(), addr, port);
97
check(new Socket(proxy), addr, port);
98
}
99
100
private void check(Socket socket, String addr, int port)
101
throws IOException
102
{
103
try (Socket s = socket) {
104
socket.connect(new InetSocketAddress(addr, port));
105
} catch (SocketException e) {
106
// java.net.SocketException: Malformed reply from SOCKS server
107
// This exception is OK, since the "server" does not implement
108
// the socks protocol. It just verifies the version and closes.
109
}
110
}
111
112
@Override
113
public void run() {
114
int count = 0;
115
try {
116
while (!stopped) {
117
try (Socket s = ss.accept()) {
118
int version = (s.getInputStream()).read();
119
if (version != expected) {
120
System.out.printf("Iteration: %d, Got: %d, expected: %d%n",
121
count, version, expected);
122
failed = true;
123
}
124
}
125
count++;
126
}
127
} catch (IOException e) {
128
if (!ss.isClosed()) {
129
e.printStackTrace();
130
}
131
// ignore, server socket was closed
132
}
133
}
134
}
135
136