Created
January 21, 2018 21:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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