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).

// assumes you also use serveHttp (for classes MyHTTPD and NanoHTTPD)
  
static MyHTTPD serveHttps_server;
static int serveHttps_port = 8889;

static void serveHttps(int port, SSLServerSocketFactory socketFactory) ctex {
  serveHttps_port = port;
  serveHttps_server = new MyHTTPD(port);
  serveHttps_server.makeSecure(socketFactory);
  serveHttps_server.start();
  print("HTTPS server started (socket factory: " + socketFactory + ", listening on port " + port + "!)");
}

static void serveHttps(int port, File keystore, S pass) ctex {
  serveHttps(port, serveHttps_makeSSLSocketFactory(keystore, pass));
}

// uses default SSLServerSocketFactory - doesn't seem to work
static void serveHttps(int port) ctex {
  serveHttps_port = port;
  serveHttps_server = new MyHTTPD(port);
  serveHttps_server.makeSecure((SSLServerSocketFactory) SSLServerSocketFactory.getDefault());
  serveHttps_server.start();
  print("HTTPS server started with default socket factory (listening on port " + port + "!)");
}

// seems to assume same password is used for keystore as well as certificate.
static SSLServerSocketFactory serveHttps_makeSSLSocketFactory(File keystoreFile, S passphrase) ctex {
  KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
  temp InputStream keystoreStream = new FileInputStream(keystoreFile);
  keystore.load(keystoreStream, passphrase.toCharArray());
  TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  trustManagerFactory.init(keystore);
  KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  keyManagerFactory.init(keystore, passphrase.toCharArray());
  SSLContext ctx = SSLContext.getInstance("TLS");
  ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
  ret ctx.getServerSocketFactory();
}

static void cleanMeUp_serveHttps() {
  if (serveHttps_server != null) {
    serveHttps_server.stop();
    serveHttps_server = null;
  }
}

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: 679 / 1112
Version history: 7 change(s)
Referenced in: #1002427 - Accellerating 629 (SPIKE)
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#3000382 - Answer for ferdie (>> t = 1, f = 0)