tls#TLSSocket TypeScript Examples

The following examples show how to use tls#TLSSocket. 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: ntrip.ts    From caster with GNU General Public License v3.0 6 votes vote down vote up
private static parseCredentials(req: NtripCasterRequest): AuthCredentials {
        const credentials: AuthCredentials = {};

        credentials.anonymous = true;

        // Basic authentication
        if (req.headers['authorization']?.startsWith('Basic ')) {
            credentials.anonymous = false;

            let basic = req.headers['authorization'].slice('Basic '.length);
            basic = Buffer.from(basic, 'base64').toString();
            let separator = basic.indexOf(':');
            if (separator >= 0) {
                credentials.basic = {
                    username: basic.slice(0, separator),
                    password: basic.slice(separator + 1)
                }
            }
        } else if (req.headers['authorization']?.startsWith('Bearer ')) {
            credentials.anonymous = false;

            credentials.bearer = req.headers['authorization'].slice('Bearer '.length);
        }

        // TLS client certificate
        if (req.socket instanceof TLSSocket) {
            credentials.anonymous = false;

            credentials.certificate = req.socket.getPeerCertificate().fingerprint;
        }

        return credentials;
    }
Example #3
Source File: certs.ts    From harness-cli with Apache License 2.0 6 votes vote down vote up
async function getCerts(host: string) {
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
    const options = {
        host: host,
        port: 443,
        method: 'GET',
    }
    return new Promise<string[]>(resolve => {
        const request = https.request(options, res => {
            let cert = (res.connection as TLSSocket).getPeerCertificate(true)
            const list = new Set<DetailedPeerCertificate>()
            do {
                list.add(cert)
                cert = cert.issuerCertificate
            } while (cert && typeof cert === 'object' && !list.has(cert))

            const certs = [...list].map(c => {
                const prefix = '-----BEGIN CERTIFICATE-----\n'
                const postfix = '-----END CERTIFICATE-----'
                const pemText = prefix + c.raw.toString('base64').match(/.{0,64}/g)?.join('\n') + postfix
                return pemText
            })
            process.env.NODE_TLS_REJECT_UNAUTHORIZED = ''            
            resolve(certs)
        })

        request.end()
    })
}
Example #4
Source File: http-proxy-client.d.ts    From dropify with MIT License 5 votes vote down vote up
/**
 * Establishes proxied connection to destinationPort
 */
declare function httpProxyClient(proxyUrl: string, destinationPort: number, destinationHost: string, callback: (err: Error | null, socket: TLSSocket | Socket) => void): void;
Example #5
Source File: socket.ts    From caster with GNU General Public License v3.0 5 votes vote down vote up
private async accept(socket: net.Socket) {
        socket.on('error', err => {
            this.emit('clientError', new VError({
                name: 'ClientError',
                cause: err,
                info: {
                    remote: {
                        address: socket.remoteAddress,
                        port: socket.remotePort,
                        family: socket.remoteFamily
                    }
                }
            }, "Socket server client error"));
        });

        let sourceHost = this.findMatchingSourceHost(socket);
        if (sourceHost === undefined) return socket.destroy();

        let authResponse: AuthResponse | undefined;
        if (sourceHost.authenticate) {
            const authRequest: AuthRequest = {
                type: sourceHost.type,
                mountpoint: sourceHost.mountpoint,

                host: socket.localAddress,
                source: {
                    host: socket.remoteAddress!,
                    port: socket.remotePort!,
                    family: socket.remoteFamily!
                },

                credentials: {
                    anonymous: !(socket instanceof TLSSocket),
                    certificate: socket instanceof TLSSocket ? socket.getPeerCertificate().fingerprint : undefined
                }
            };

            authResponse = await this.caster.authenticate(authRequest);
        }

        const source: SocketTransportConnectionProperties = {
            protocol: socket instanceof TLSSocket ? 'tls' : 'tcp',

            remote: {
                host: socket.remoteAddress!,
                port: socket.remotePort!,
                family: socket.remoteFamily!
            },

            toString: () => this.connectionDescription(source)
        };

        try {
            this.connect({
                type: sourceHost.type,

                source: source,

                mountpoint: sourceHost.mountpoint,

                gga: sourceHost.gga,
                str: sourceHost.str,

                stream: socket,

                auth: authResponse
            });
        } catch (err) {
            socket.destroy(err);
        }
    }