|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2024, by Samuel Williams. |
| 5 | + |
| 6 | +require 'io/endpoint' |
| 7 | +require 'io/endpoint/host_endpoint' |
| 8 | +require 'io/endpoint/ssl_endpoint' |
| 9 | + |
| 10 | +require_relative 'protocol/resp2' |
| 11 | +require_relative 'protocol/authenticated' |
| 12 | +require_relative 'protocol/selected' |
| 13 | + |
| 14 | +module Async |
| 15 | + module Redis |
| 16 | + def self.local_endpoint(**options) |
| 17 | + Endpoint.local(**options) |
| 18 | + end |
| 19 | + |
| 20 | + # Represents a way to connect to a remote Redis server. |
| 21 | + class Endpoint < ::IO::Endpoint::Generic |
| 22 | + LOCALHOST = URI.parse("redis://localhost").freeze |
| 23 | + |
| 24 | + def self.local(**options) |
| 25 | + self.new(LOCALHOST, **options) |
| 26 | + end |
| 27 | + |
| 28 | + SCHEMES = { |
| 29 | + 'redis' => URI::Generic, |
| 30 | + 'rediss' => URI::Generic, |
| 31 | + } |
| 32 | + |
| 33 | + def self.parse(string, endpoint = nil, **options) |
| 34 | + url = URI.parse(string).normalize |
| 35 | + |
| 36 | + return self.new(url, endpoint, **options) |
| 37 | + end |
| 38 | + |
| 39 | + # Construct an endpoint with a specified scheme, hostname, optional path, and options. |
| 40 | + # |
| 41 | + # @parameter scheme [String] The scheme to use, e.g. "redis" or "rediss". |
| 42 | + # @parameter hostname [String] The hostname to connect to (or bind to). |
| 43 | + # @parameter *options [Hash] Additional options, passed to {#initialize}. |
| 44 | + def self.for(scheme, hostname, credentials: nil, port: nil, database: nil, **options) |
| 45 | + uri_klass = SCHEMES.fetch(scheme.downcase) do |
| 46 | + raise ArgumentError, "Unsupported scheme: #{scheme.inspect}" |
| 47 | + end |
| 48 | + |
| 49 | + if database |
| 50 | + path = "/#{database}" |
| 51 | + end |
| 52 | + |
| 53 | + self.new( |
| 54 | + uri_klass.new(scheme, credentials&.join(":"), hostname, port, nil, path, nil, nil, nil).normalize, |
| 55 | + **options |
| 56 | + ) |
| 57 | + end |
| 58 | + |
| 59 | + # Coerce the given object into an endpoint. |
| 60 | + # @parameter url [String | Endpoint] The URL or endpoint to convert. |
| 61 | + def self.[](object) |
| 62 | + if object.is_a?(self) |
| 63 | + return object |
| 64 | + else |
| 65 | + self.parse(object.to_s) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + # @option scheme [String] the scheme to use, overrides the URL scheme. |
| 70 | + # @option hostname [String] the hostname to connect to (or bind to), overrides the URL hostname (used for SNI). |
| 71 | + # @option port [Integer] the port to bind to, overrides the URL port. |
| 72 | + # @option ssl_context [OpenSSL::SSL::SSLContext] the context to use for TLS. |
| 73 | + # @option alpn_protocols [Array<String>] the alpn protocols to negotiate. |
| 74 | + def initialize(url, endpoint = nil, **options) |
| 75 | + super(**options) |
| 76 | + |
| 77 | + raise ArgumentError, "URL must be absolute (include scheme, host): #{url}" unless url.absolute? |
| 78 | + |
| 79 | + @url = url |
| 80 | + |
| 81 | + if endpoint |
| 82 | + @endpoint = self.build_endpoint(endpoint) |
| 83 | + else |
| 84 | + @endpoint = nil |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def to_url |
| 89 | + url = @url.dup |
| 90 | + |
| 91 | + unless default_port? |
| 92 | + url.port = self.port |
| 93 | + end |
| 94 | + |
| 95 | + return url |
| 96 | + end |
| 97 | + |
| 98 | + def to_s |
| 99 | + "\#<#{self.class} #{self.to_url} #{@options}>" |
| 100 | + end |
| 101 | + |
| 102 | + def inspect |
| 103 | + "\#<#{self.class} #{self.to_url} #{@options.inspect}>" |
| 104 | + end |
| 105 | + |
| 106 | + attr :url |
| 107 | + |
| 108 | + def address |
| 109 | + endpoint.address |
| 110 | + end |
| 111 | + |
| 112 | + def secure? |
| 113 | + ['rediss'].include?(self.scheme) |
| 114 | + end |
| 115 | + |
| 116 | + def protocol |
| 117 | + protocol = @options.fetch(:protocol, Protocol::RESP2) |
| 118 | + |
| 119 | + if database = self.database |
| 120 | + protocol = Protocol::Selected.new(database, protocol) |
| 121 | + end |
| 122 | + |
| 123 | + if credentials = self.credentials |
| 124 | + protocol = Protocol::Authenticated.new(credentials, protocol) |
| 125 | + end |
| 126 | + |
| 127 | + return protocol |
| 128 | + end |
| 129 | + |
| 130 | + def default_port |
| 131 | + 6379 |
| 132 | + end |
| 133 | + |
| 134 | + def default_port? |
| 135 | + port == default_port |
| 136 | + end |
| 137 | + |
| 138 | + def port |
| 139 | + @options[:port] || @url.port || default_port |
| 140 | + end |
| 141 | + |
| 142 | + # The hostname is the server we are connecting to: |
| 143 | + def hostname |
| 144 | + @options[:hostname] || @url.hostname |
| 145 | + end |
| 146 | + |
| 147 | + def scheme |
| 148 | + @options[:scheme] || @url.scheme |
| 149 | + end |
| 150 | + |
| 151 | + def database |
| 152 | + @options[:database] || @url.path[1..-1].to_i |
| 153 | + end |
| 154 | + |
| 155 | + def credentials |
| 156 | + @options[:credentials] || @url.userinfo&.split(":") |
| 157 | + end |
| 158 | + |
| 159 | + def localhost? |
| 160 | + @url.hostname =~ /^(.*?\.)?localhost\.?$/ |
| 161 | + end |
| 162 | + |
| 163 | + # We don't try to validate peer certificates when talking to localhost because they would always be self-signed. |
| 164 | + def ssl_verify_mode |
| 165 | + if self.localhost? |
| 166 | + OpenSSL::SSL::VERIFY_NONE |
| 167 | + else |
| 168 | + OpenSSL::SSL::VERIFY_PEER |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + def ssl_context |
| 173 | + @options[:ssl_context] || OpenSSL::SSL::SSLContext.new.tap do |context| |
| 174 | + context.set_params( |
| 175 | + verify_mode: self.ssl_verify_mode |
| 176 | + ) |
| 177 | + end |
| 178 | + end |
| 179 | + |
| 180 | + def build_endpoint(endpoint = nil) |
| 181 | + endpoint ||= tcp_endpoint |
| 182 | + |
| 183 | + if secure? |
| 184 | + # Wrap it in SSL: |
| 185 | + return ::IO::Endpoint::SSLEndpoint.new(endpoint, |
| 186 | + ssl_context: self.ssl_context, |
| 187 | + hostname: @url.hostname, |
| 188 | + timeout: self.timeout, |
| 189 | + ) |
| 190 | + end |
| 191 | + |
| 192 | + return endpoint |
| 193 | + end |
| 194 | + |
| 195 | + def endpoint |
| 196 | + @endpoint ||= build_endpoint |
| 197 | + end |
| 198 | + |
| 199 | + def endpoint=(endpoint) |
| 200 | + @endpoint = build_endpoint(endpoint) |
| 201 | + end |
| 202 | + |
| 203 | + def bind(*arguments, &block) |
| 204 | + endpoint.bind(*arguments, &block) |
| 205 | + end |
| 206 | + |
| 207 | + def connect(&block) |
| 208 | + endpoint.connect(&block) |
| 209 | + end |
| 210 | + |
| 211 | + def each |
| 212 | + return to_enum unless block_given? |
| 213 | + |
| 214 | + self.tcp_endpoint.each do |endpoint| |
| 215 | + yield self.class.new(@url, endpoint, **@options) |
| 216 | + end |
| 217 | + end |
| 218 | + |
| 219 | + def key |
| 220 | + [@url, @options] |
| 221 | + end |
| 222 | + |
| 223 | + def eql? other |
| 224 | + self.key.eql? other.key |
| 225 | + end |
| 226 | + |
| 227 | + def hash |
| 228 | + self.key.hash |
| 229 | + end |
| 230 | + |
| 231 | + protected |
| 232 | + |
| 233 | + def tcp_options |
| 234 | + options = @options.dup |
| 235 | + |
| 236 | + options.delete(:scheme) |
| 237 | + options.delete(:port) |
| 238 | + options.delete(:hostname) |
| 239 | + options.delete(:ssl_context) |
| 240 | + options.delete(:protocol) |
| 241 | + |
| 242 | + return options |
| 243 | + end |
| 244 | + |
| 245 | + def tcp_endpoint |
| 246 | + ::IO::Endpoint.tcp(self.hostname, port, **tcp_options) |
| 247 | + end |
| 248 | + end |
| 249 | + end |
| 250 | +end |
0 commit comments