1+ /* Copyright rememberjava.com. Licensed under GPL 3. See http://rememberjava.com/license */
2+ package com .shinemo .mpush .alloc ;
3+
4+ import com .sun .net .httpserver .HttpServer ;
5+ import com .sun .net .httpserver .HttpsConfigurator ;
6+ import com .sun .net .httpserver .HttpsServer ;
7+
8+ import javax .net .ssl .KeyManagerFactory ;
9+ import javax .net .ssl .SSLContext ;
10+ import javax .net .ssl .TrustManagerFactory ;
11+ import java .io .File ;
12+ import java .io .FileInputStream ;
13+ import java .lang .ProcessBuilder .Redirect ;
14+ import java .net .InetSocketAddress ;
15+ import java .security .KeyStore ;
16+
17+ /**
18+ * A HTTPS server using a self-signed TLS 1.2 key and certificate generated by
19+ * the Java keytool command.
20+ * <p>
21+ */
22+ @ SuppressWarnings ("restriction" )
23+ public class HttpServerCreator {
24+
25+ private static final File KEYSTORE_FILE = new File (System .getProperty ("java.io.tmpdir" ), "mpush.jks" );
26+
27+ private static final String KEYSTORE_PASSWORD = "mpush_2017" ;
28+
29+ private static final String KEY_PASSWORD = "mpush_2017" ;
30+
31+ /**
32+ * Generates a new self-signed certificate in /tmp/test.jks, if it does not
33+ * already exist.
34+ */
35+ private static void generateCertificate () throws Exception {
36+ if (KEYSTORE_FILE .exists ()) return ;
37+
38+ System .setProperty ("javax.net.debug" , "all" );
39+
40+ File keytool = new File (System .getProperty ("java.home" ), "bin/keytool" );
41+
42+ String [] genkeyCmd = new String []{
43+ keytool .toString (),
44+ "-genkey" ,
45+ "-keyalg" , "RSA" ,
46+ "-alias" , "mpush.com" ,
47+ "-validity" , "365" ,
48+ "-keysize" , "2048" ,
49+ "-dname" , "cn=mpush.com,ou=mpush,o=OHUN .Inc,c=CN" ,
50+ "-keystore" , KEYSTORE_FILE .getAbsolutePath (),
51+ "-storepass" , KEYSTORE_PASSWORD ,
52+ "-keypass" , KEY_PASSWORD };
53+
54+ System .out .println (String .join (" " , genkeyCmd ));
55+
56+ ProcessBuilder processBuilder = new ProcessBuilder (genkeyCmd );
57+ processBuilder .redirectErrorStream (true );
58+ processBuilder .redirectOutput (Redirect .INHERIT );
59+ processBuilder .redirectError (Redirect .INHERIT );
60+ Process exec = processBuilder .start ();
61+ exec .waitFor ();
62+
63+ System .out .println ("Exit value: " + exec .exitValue ());
64+
65+ }
66+
67+ private static HttpsServer createHttpsServer (int port ) throws Exception {
68+ generateCertificate ();
69+ HttpsServer httpsServer = HttpsServer .create (new InetSocketAddress (port ), 0 );
70+ SSLContext sslContext = getSslContext ();
71+ httpsServer .setHttpsConfigurator (new HttpsConfigurator (sslContext ));
72+ return httpsServer ;
73+ }
74+
75+ private static HttpServer createHttpServer (int port ) throws Exception {
76+ HttpServer httpServer = HttpServer .create (new InetSocketAddress (port ), 0 );
77+ return httpServer ;
78+ }
79+
80+ private static SSLContext getSslContext () throws Exception {
81+ KeyStore ks = KeyStore .getInstance ("JKS" );
82+ ks .load (new FileInputStream (KEYSTORE_FILE ), KEYSTORE_PASSWORD .toCharArray ());
83+
84+ KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
85+ kmf .init (ks , KEY_PASSWORD .toCharArray ());
86+
87+ TrustManagerFactory tmf = TrustManagerFactory .getInstance ("SunX509" );
88+ tmf .init (ks );
89+
90+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
91+ sslContext .init (kmf .getKeyManagers (), tmf .getTrustManagers (), null );
92+
93+ return sslContext ;
94+ }
95+
96+ public static HttpServer createServer (int port , boolean https ) {
97+ try {
98+ if (https ) {
99+ return HttpServerCreator .createHttpsServer (port );
100+ } else {
101+ return HttpServerCreator .createHttpServer (port );
102+ }
103+ } catch (Exception e ) {
104+ throw new RuntimeException (e );
105+ }
106+ }
107+ }
0 commit comments