Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IPv6 address #133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/faye/websocket/client.rb
@@ -1,4 +1,5 @@
require 'forwardable'
require 'ipaddr'

module Faye
class WebSocket
Expand All @@ -7,8 +8,9 @@ class Client
extend Forwardable
include API

DEFAULT_PORTS = { 'http' => 80, 'https' => 443, 'ws' => 80, 'wss' => 443 }
SECURE_PROTOCOLS = ['https', 'wss']
DEFAULT_PORTS = { 'http' => 80, 'https' => 443, 'ws' => 80, 'wss' => 443 }.freeze
SECURE_PROTOCOLS = ['https', 'wss'].freeze
IPV6_HOST_REGEX = /\[.*\]/.freeze

def_delegators :@driver, :headers, :status

Expand All @@ -24,7 +26,7 @@ def initialize(url, protocols = nil, options = {})

configure_proxy(proxy)

EventMachine.connect(@endpoint.host, port, Connection) do |conn|
EventMachine.connect(host(@endpoint), port, Connection) do |conn|
conn.parent = self
end
rescue => error
Expand All @@ -33,6 +35,14 @@ def initialize(url, protocols = nil, options = {})

private

def host(uri)
if IPV6_HOST_REGEX.match?(uri.host) && IPAddr.new(uri.host).ipv6?
uri.host.gsub(/\[|\]/, '')
else
uri.host
end
end

def configure_proxy(proxy)
return unless proxy[:origin]

Expand All @@ -53,7 +63,7 @@ def configure_proxy(proxy)
def start_tls(uri, options)
return unless SECURE_PROTOCOLS.include?(uri.scheme)

tls_options = { :sni_hostname => uri.host, :verify_peer => true }.merge(options)
tls_options = { :sni_hostname => host(uri), :verify_peer => true }.merge(options)
@ssl_verifier = SslVerifier.new(uri.host, tls_options)
@stream.start_tls(tls_options)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/faye/websocket/client_spec.rb
Expand Up @@ -125,10 +125,12 @@ def wait(seconds, &callback)
let(:protocols) { ["foo", "echo"] }

let(:localhost) { "localhost" }
let(:ipv6_address) { '[::1]' }
let(:port) { 4180 }
let(:plain_text_url) { "ws://#{ localhost }:#{ port }/" }
let(:wrong_url) { "ws://#{ localhost }:9999/" }
let(:secure_url) { "wss://#{ localhost }:#{ port }/" }
let(:ipv6_url) { "ws://#{ipv6_address}:#{port}/" }

let :tls_options do
{ :root_cert_file => File.expand_path('../../../server.crt', __FILE__) }
Expand Down Expand Up @@ -246,4 +248,10 @@ def wait(seconds, &callback)

it_should_behave_like "socket server"
end

describe "with IPv6 address" do
let(:socket_url) { ipv6_url }

it_should_behave_like "socket server"
end
end