Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

46
LINES

< > BotCompany Repo | #1002345 // serveHttps function

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4600L/33K).

1  
// assumes you also use serveHttp (for classes MyHTTPD and NanoHTTPD)
2  
  
3  
static MyHTTPD serveHttps_server;
4  
static int serveHttps_port = 8889;
5  
6  
static void serveHttps(int port, SSLServerSocketFactory socketFactory) ctex {
7  
  serveHttps_port = port;
8  
  serveHttps_server = new MyHTTPD(port);
9  
  serveHttps_server.makeSecure(socketFactory);
10  
  serveHttps_server.start();
11  
  print("HTTPS server started (socket factory: " + socketFactory + ", listening on port " + port + "!)");
12  
}
13  
14  
static void serveHttps(int port, File keystore, S pass) ctex {
15  
  serveHttps(port, serveHttps_makeSSLSocketFactory(keystore, pass));
16  
}
17  
18  
// uses default SSLServerSocketFactory - doesn't seem to work
19  
static void serveHttps(int port) ctex {
20  
  serveHttps_port = port;
21  
  serveHttps_server = new MyHTTPD(port);
22  
  serveHttps_server.makeSecure((SSLServerSocketFactory) SSLServerSocketFactory.getDefault());
23  
  serveHttps_server.start();
24  
  print("HTTPS server started with default socket factory (listening on port " + port + "!)");
25  
}
26  
27  
// seems to assume same password is used for keystore as well as certificate.
28  
static SSLServerSocketFactory serveHttps_makeSSLSocketFactory(File keystoreFile, S passphrase) ctex {
29  
  KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
30  
  temp InputStream keystoreStream = new FileInputStream(keystoreFile);
31  
  keystore.load(keystoreStream, passphrase.toCharArray());
32  
  TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
33  
  trustManagerFactory.init(keystore);
34  
  KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
35  
  keyManagerFactory.init(keystore, passphrase.toCharArray());
36  
  SSLContext ctx = SSLContext.getInstance("TLS");
37  
  ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
38  
  ret ctx.getServerSocketFactory();
39  
}
40  
41  
static void cleanMeUp_serveHttps() {
42  
  if (serveHttps_server != null) {
43  
    serveHttps_server.stop();
44  
    serveHttps_server = null;
45  
  }
46  
}

Author comment

Began life as a copy of #1001648

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002345
Snippet name: serveHttps function
Eternal ID of this version: #1002345/8
Text MD5: aa6e14c55c71511fcb56f1f56306aa18
Transpilation MD5: 05d2adf562ba50cd81c814f1fa460c37
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-02 13:48:53
Source code size: 2076 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 604 / 1025
Version history: 7 change(s)
Referenced in: [show references]