Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkServer.java
41154 views
1
/*
2
* Copyright (c) 2020, 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.SocketException;
26
import java.util.ArrayList;
27
import java.util.List;
28
29
import javax.net.ssl.SNIHostName;
30
import javax.net.ssl.SNIMatcher;
31
import javax.net.ssl.SSLContext;
32
import javax.net.ssl.SSLParameters;
33
import javax.net.ssl.SSLServerSocket;
34
import javax.net.ssl.SSLServerSocketFactory;
35
import javax.net.ssl.SSLSession;
36
import javax.net.ssl.SSLSocket;
37
38
/*
39
* A JDK server based on SSLServerSocket.
40
*/
41
public class JdkServer extends AbstractServer {
42
43
protected final String response;
44
45
protected final SSLServerSocket serverSocket;
46
47
protected final SSLContext context;
48
private SSLSocket socket;
49
50
public JdkServer(Builder builder) throws Exception {
51
response = builder.getMessage();
52
53
context = Utilities.createSSLContext(builder.getCertTuple());
54
SSLServerSocketFactory serverFactory = context.getServerSocketFactory();
55
serverSocket
56
= (SSLServerSocket) serverFactory.createServerSocket(builder.getPort());
57
configServerSocket(builder);
58
}
59
60
protected void configServerSocket(Builder builder) throws SocketException {
61
serverSocket.setSoTimeout(builder.getTimeout() * 1000);
62
if (builder.getProtocols() != null) {
63
serverSocket.setEnabledProtocols(Utilities.enumsToStrs(protocol -> {
64
return JdkUtils.protocol((Protocol) protocol);
65
}, builder.getProtocols()));
66
}
67
if (builder.getCipherSuites() != null) {
68
serverSocket.setEnabledCipherSuites(
69
Utilities.enumsToStrs(builder.getCipherSuites()));
70
}
71
serverSocket.setNeedClientAuth(builder.getClientAuth());
72
SSLParameters sslParams = serverSocket.getSSLParameters();
73
if (builder.getServerNames() != null) {
74
List<SNIMatcher> matchers = new ArrayList<>();
75
for(String bufServerName : builder.getServerNames()) {
76
matchers.add(SNIHostName.createSNIMatcher(bufServerName));
77
}
78
sslParams.setSNIMatchers(matchers);
79
}
80
if (builder.getAppProtocols() != null) {
81
sslParams.setApplicationProtocols(builder.getAppProtocols());
82
for (String appProtocol : sslParams.getApplicationProtocols()) {
83
System.out.println("appProtocol: " + appProtocol);
84
}
85
}
86
serverSocket.setSSLParameters(sslParams);
87
}
88
89
public static class Builder extends AbstractServer.Builder {
90
91
@Override
92
public JdkServer build() throws Exception {
93
return new JdkServer(this);
94
}
95
}
96
97
@Override
98
public Product getProduct() {
99
return Jdk.DEFAULT;
100
}
101
102
@Override
103
public int getPort() {
104
return serverSocket.getLocalPort();
105
}
106
107
@Override
108
public void accept() throws IOException {
109
while (true) {
110
try (SSLSocket socket = (SSLSocket) serverSocket.accept()) {
111
this.socket = socket;
112
System.out.println("Server accepted connection");
113
114
String request = Utilities.readIn(socket.getInputStream());
115
System.out.printf("Server received request: [%s]%n", request);
116
117
if (response != null) {
118
Utilities.writeOut(socket.getOutputStream(), response);
119
System.out.printf("Server sent response: [%s]%n", response);
120
}
121
}
122
}
123
}
124
125
private synchronized SSLSocket getSocket() {
126
return socket;
127
}
128
129
public SSLSession getSession() {
130
return getSocket().getSession();
131
}
132
133
@Override
134
public String getNegoAppProtocol() throws SSLTestException {
135
return getSocket().getApplicationProtocol();
136
}
137
138
@Override
139
public boolean isAlive() {
140
return !serverSocket.isClosed();
141
}
142
143
@Override
144
public void close() throws IOException {
145
if (!serverSocket.isClosed()) {
146
serverSocket.close();
147
}
148
}
149
}
150
151