tls#SecureContext TypeScript Examples

The following examples show how to use tls#SecureContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: impl.ts    From kassette with MIT License 6 votes vote down vote up
public async process(
    socket: Socket,
    hostname = getSocketConnection(socket).hostname,
  ): Promise<Socket> {
    const secureContext = await this._getSecureContext(hostname);
    const SNICallback = async (
      sni: string,
      callback: (error: Error | null, context: SecureContext) => void,
    ) => {
      const sniContext = sni === hostname ? secureContext : await this._getSecureContext(sni);
      callback(null, sniContext);
    };
    const tlsSocket = new TLSSocket(socket, {
      isServer: true,
      SNICallback,
      secureContext,
    });
    forwardSocketConnections(socket, tlsSocket);
    return tlsSocket;
  }
Example #2
Source File: connect.ts    From clearflask with Apache License 2.0 6 votes vote down vote up
sniCallback: ServerOptions['SNICallback'] = async (servername, callback) => {
  // Get cert
  const wildName = '*.' + servername
    .split('.')
    .slice(1)
    .join('.');
  var secureContext: SecureContext = secureContextCache.get(servername) || secureContextCache.get(wildName);
  if (!secureContext) {
    var certAndKey: CertGetOrCreateResponse;
    try {
      certAndKey = await ServerConnect.get()
        .dispatch()
        .certGetOrCreateConnect(
          { domain: servername },
          undefined,
          { 'x-cf-connect-token': connectConfig.connectToken });
      console.log('Found cert for servername', servername);
    } catch (response: any) {
      console.log('Cert get unknown error for servername', servername, response);
      callback(new Error('No certificate found'), null as any);
      return;
    }

    // Create secure context
    secureContext = tls.createSecureContext({
      key: certAndKey.keypair.privateKeyPem,
      cert: certAndKey.cert.cert + "\n" + certAndKey.cert.chain,
    });

    // Add to cache
    const expiresInSec = certAndKey.cert.expiresAt - new Date().getTime();
    [servername, ...certAndKey.cert.altnames].forEach(altName => secureContextCache.set(
      servername,
      secureContext,
      Math.min(3600, expiresInSec)));
  }

  callback(null, secureContext);
}
Example #3
Source File: impl.ts    From kassette with MIT License 5 votes vote down vote up
private _contexts = new Map<string, Promise<SecureContext>>();