module Console1984::Ext::Socket::TcpSocket

Wraps socket methods to execute supervised when protected mode.

Constants

ComparableAddress

Public Instance Methods

write(*args) click to toggle source
Calls superclass method
# File lib/console1984/ext/socket/tcp_socket.rb, line 5
def write(*args)
  protecting do
    super
  end
end
write_nonblock(*args) click to toggle source
Calls superclass method
# File lib/console1984/ext/socket/tcp_socket.rb, line 11
def write_nonblock(*args)
  protecting do
    super
  end
end

Private Instance Methods

host_and_port_from(url) click to toggle source
# File lib/console1984/ext/socket/tcp_socket.rb, line 41
def host_and_port_from(url)
  URI(url).then do |parsed_uri|
    if parsed_uri.host
      [parsed_uri.host, parsed_uri.port]
    else
      host_and_port_from_invalid_uri(url)
    end
  end
rescue URI::InvalidURIError
  host_and_port_from_invalid_uri(url)
end
host_and_port_from_invalid_uri(url) click to toggle source
# File lib/console1984/ext/socket/tcp_socket.rb, line 53
def host_and_port_from_invalid_uri(url)
  host, _, port = url.rpartition(':')
  [host, port]
end
protected?() click to toggle source
# File lib/console1984/ext/socket/tcp_socket.rb, line 26
def protected?
  protected_addresses&.include?(ComparableAddress.new(remote_address))
end
protected_addresses() click to toggle source
# File lib/console1984/ext/socket/tcp_socket.rb, line 30
def protected_addresses
  @protected_addresses ||= protected_urls.collect do |url|
    host, port = host_and_port_from(url)
    Array(Addrinfo.getaddrinfo(host, port)).collect { |addrinfo| ComparableAddress.new(addrinfo) if addrinfo.ip_address }
  end.flatten.compact.uniq
end
protected_urls() click to toggle source
# File lib/console1984/ext/socket/tcp_socket.rb, line 37
def protected_urls
  Console1984::Shield::Modes::PROTECTED_MODE.currently_protected_urls || []
end
protecting() { || ... } click to toggle source
# File lib/console1984/ext/socket/tcp_socket.rb, line 18
def protecting
  if protected?
    raise Console1984::Errors::ProtectedConnection, remote_address.inspect
  else
    yield
  end
end