Skip to content

Instantly share code, notes, and snippets.

@D1plo1d
Last active August 29, 2015 13:57
Show Gist options
  • Save D1plo1d/9655724 to your computer and use it in GitHub Desktop.
Save D1plo1d/9655724 to your computer and use it in GitHub Desktop.
SelfSignedHttpsAgent
tls = require "tls"
https = require "https"
http = require "http"
net = require "net"
_ = require "lodash"
# This HTTPS Agent allows us to verify a server's self-signed cert before
# accepting the connection via a "cert" event that is emitted when the cert
# is received.
module.exports = class SelfSignedHttpsAgent extends https.Agent
constructor: (options = {}) ->
super(_.merge options, rejectUnauthorized: false)
# Preventing super from overriding functions
@createConnection = SelfSignedHttpsAgent.prototype.createConnection
@getName = SelfSignedHttpsAgent.prototype.getName
createConnection: (port, host, options) ->
cleartextStream = tls.connect(port, host, options)
# console.log cleartextStream.socket
cb = _.partial @_validatePeerCertificate, cleartextStream
cleartextStream.on("secureConnect", cb)
getName: ->
"#{agentProto.getName}:tegh"
_validatePeerCertificate: (cleartextStream) ->
cert = cleartextStream.getPeerCertificate()
@emit("cert", cert)
console.log "cert!"
# console.log cert
# throw "wut"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment