-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.java
103 lines (96 loc) · 3.75 KB
/
server.java
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
import java.io.*;
import java.net.*;
import javax.net.*;
import javax.net.ssl.*;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
public class server implements Runnable {
private ServerSocket serverSocket = null;
private static int numConnectedClients = 0;
public server(ServerSocket ss) throws IOException {
serverSocket = ss;
newListener();
}
public void run() {
try {
SSLSocket socket=(SSLSocket)serverSocket.accept();
newListener();
SSLSession session = socket.getSession();
Certificate[] cert = session.getPeerCertificates();
String subject = ((X509Certificate) cert[0]).getSubjectX500Principal().getName();
numConnectedClients++;
System.out.println("client connected");
System.out.println("client name (cert subject DN field): " + subject);
System.out.println(numConnectedClients + " concurrent connection(s)\n");
PrintWriter out = null;
BufferedReader in = null;
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String clientMsg = null;
while ((clientMsg = in.readLine()) != null) {
String rev = new StringBuilder(clientMsg).reverse().toString();
System.out.println("received '" + clientMsg + "' from client");
System.out.print("sending '" + rev + "' to client...");
out.println(rev);
out.flush();
System.out.println("done\n");
}
in.close();
out.close();
socket.close();
numConnectedClients--;
System.out.println("client disconnected");
System.out.println(numConnectedClients + " concurrent connection(s)\n");
} catch (IOException e) {
System.out.println("Client died: " + e.getMessage());
e.printStackTrace();
return;
}
}
private void newListener() { (new Thread(this)).start(); } // calls run()
public static void main(String args[]) {
System.out.println("\nServer Started\n");
int port = -1;
if (args.length >= 1) {
port = Integer.parseInt(args[0]);
}
String type = "TLSv1.2";
try {
ServerSocketFactory ssf = getServerSocketFactory(type);
ServerSocket ss = ssf.createServerSocket(port, 0, InetAddress.getByName(null));
((SSLServerSocket)ss).setNeedClientAuth(true); // enables client authentication
new server(ss);
} catch (IOException e) {
System.out.println("Unable to start Server: " + e.getMessage());
e.printStackTrace();
}
}
private static ServerSocketFactory getServerSocketFactory(String type) {
if (type.equals("TLSv1.2")) {
SSLServerSocketFactory ssf = null;
try { // set up key manager to perform server authentication
SSLContext ctx = SSLContext.getInstance("TLSv1.2");
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance("JKS");
KeyStore ts = KeyStore.getInstance("JKS");
char[] password = "password".toCharArray();
// keystore password (storepass)
ks.load(new FileInputStream("serverkeystore"), password);
// truststore password (storepass)
ts.load(new FileInputStream("servertruststore"), password);
kmf.init(ks, password); // certificate password (keypass)
tmf.init(ts); // possible to use keystore as truststore here
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
ssf = ctx.getServerSocketFactory();
return ssf;
} catch (Exception e) {
e.printStackTrace();
}
} else {
return ServerSocketFactory.getDefault();
}
return null;
}
}