Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Created January 21, 2018 21:46
Show Gist options
  • Save kylekyle/361986fd9f58efdd476a9c619992a905 to your computer and use it in GitHub Desktop.
Save kylekyle/361986fd9f58efdd476a9c619992a905 to your computer and use it in GitHub Desktop.
How to attach information to a TCPSocket for the OpenSSL context later on
require 'socket'
class TCPSocket
class << self
def new host, *args, &block
socket = super host, *args, &block
# check whitelist, set indicator
socket.instance_variable_set :@mypki, true # or false
# return socket
socket
end
alias :open :new
end
end
require 'openssl'
class OpenSSL::SSL::SSLSocket
class << self
def new io, ctx = nil
if io.is_a? TCPSocket
# check indicator, and swap context appropriately
puts io.instance_variable_get(:@mypki)
end
super io, ctx
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment