ably 1.1.4.rc → 1.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/check.yml +41 -0
- data/CHANGELOG.md +85 -0
- data/COPYRIGHT +1 -0
- data/LICENSE +173 -10
- data/MAINTAINERS.md +1 -0
- data/README.md +24 -18
- data/SPEC.md +1020 -922
- data/ably.gemspec +13 -8
- data/lib/ably.rb +1 -0
- data/lib/ably/agent.rb +3 -0
- data/lib/ably/auth.rb +12 -2
- data/lib/ably/exceptions.rb +6 -0
- data/lib/ably/models/connection_details.rb +2 -0
- data/lib/ably/models/message.rb +14 -0
- data/lib/ably/models/presence_message.rb +14 -0
- data/lib/ably/models/protocol_message.rb +8 -0
- data/lib/ably/modules/ably.rb +11 -1
- data/lib/ably/realtime/channel.rb +7 -11
- data/lib/ably/realtime/channel/channel_manager.rb +3 -3
- data/lib/ably/realtime/channel/channel_properties.rb +24 -0
- data/lib/ably/realtime/channel/publisher.rb +5 -0
- data/lib/ably/realtime/client.rb +9 -0
- data/lib/ably/realtime/client/incoming_message_dispatcher.rb +14 -6
- data/lib/ably/realtime/connection.rb +9 -5
- data/lib/ably/realtime/connection/websocket_transport.rb +67 -1
- data/lib/ably/realtime/presence.rb +0 -14
- data/lib/ably/rest/channel.rb +10 -3
- data/lib/ably/rest/client.rb +22 -21
- data/lib/ably/version.rb +1 -13
- data/spec/acceptance/realtime/auth_spec.rb +1 -1
- data/spec/acceptance/realtime/channel_history_spec.rb +25 -0
- data/spec/acceptance/realtime/channel_spec.rb +24 -0
- data/spec/acceptance/realtime/client_spec.rb +72 -16
- data/spec/acceptance/realtime/connection_failures_spec.rb +29 -12
- data/spec/acceptance/realtime/connection_spec.rb +31 -33
- data/spec/acceptance/realtime/presence_history_spec.rb +3 -59
- data/spec/acceptance/realtime/presence_spec.rb +66 -157
- data/spec/acceptance/realtime/push_admin_spec.rb +3 -19
- data/spec/acceptance/rest/auth_spec.rb +6 -75
- data/spec/acceptance/rest/base_spec.rb +8 -4
- data/spec/acceptance/rest/channel_spec.rb +13 -0
- data/spec/acceptance/rest/client_spec.rb +144 -45
- data/spec/acceptance/rest/push_admin_spec.rb +3 -19
- data/spec/shared/client_initializer_behaviour.rb +131 -8
- data/spec/shared/model_behaviour.rb +1 -1
- data/spec/spec_helper.rb +12 -2
- data/spec/support/serialization_helper.rb +21 -0
- data/spec/unit/models/message_spec.rb +59 -0
- data/spec/unit/models/presence_message_spec.rb +49 -0
- data/spec/unit/models/protocol_message_spec.rb +48 -0
- data/spec/unit/realtime/channel_spec.rb +1 -1
- data/spec/unit/realtime/client_spec.rb +19 -6
- data/spec/unit/realtime/incoming_message_dispatcher_spec.rb +38 -0
- data/spec/unit/rest/channel_spec.rb +10 -0
- data/spec/unit/rest/client_spec.rb +20 -0
- metadata +52 -32
- data/.travis.yml +0 -18
data/ably.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_runtime_dependency 'eventmachine', '~> 1.2.6'
|
22
22
|
spec.add_runtime_dependency 'em-http-request', '~> 1.1'
|
23
|
-
spec.add_runtime_dependency 'statesman', '~>
|
23
|
+
spec.add_runtime_dependency 'statesman', '~> 8.0'
|
24
24
|
spec.add_runtime_dependency 'faraday', '~> 1.0'
|
25
25
|
spec.add_runtime_dependency 'typhoeus', '~> 1.4'
|
26
26
|
|
@@ -35,23 +35,28 @@ Gem::Specification.new do |spec|
|
|
35
35
|
|
36
36
|
spec.add_development_dependency 'rake', '~> 13.0'
|
37
37
|
spec.add_development_dependency 'redcarpet', '~> 3.3'
|
38
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.3.0' # version lock, see config.around(:example, :event_machine) in event_machine_helper.rb
|
39
39
|
spec.add_development_dependency 'rspec-retry', '~> 0.6'
|
40
40
|
spec.add_development_dependency 'yard', '~> 0.9'
|
41
41
|
spec.add_development_dependency 'rspec-instafail', '~> 1.0'
|
42
|
-
spec.add_development_dependency 'bundler', '
|
42
|
+
spec.add_development_dependency 'bundler', '>= 1.3.0'
|
43
43
|
|
44
44
|
if RUBY_VERSION.match(/^1\./)
|
45
45
|
spec.add_development_dependency 'public_suffix', '~> 1.4.6' # Later versions do not support Ruby 1.9
|
46
46
|
spec.add_development_dependency 'webmock', '2.2'
|
47
47
|
spec.add_development_dependency 'parallel_tests', '~> 2.9.0'
|
48
48
|
else
|
49
|
-
spec.add_development_dependency 'webmock', '~>
|
50
|
-
spec.add_development_dependency '
|
51
|
-
spec.add_development_dependency '
|
49
|
+
spec.add_development_dependency 'webmock', '~> 3.11'
|
50
|
+
spec.add_development_dependency 'simplecov', '~> 0.21.2'
|
51
|
+
spec.add_development_dependency 'simplecov-lcov', '~> 0.8.0'
|
52
|
+
spec.add_development_dependency 'parallel_tests', '~> 3.7'
|
52
53
|
if !RUBY_VERSION.match(/^2\.[0123]/)
|
53
|
-
spec.add_development_dependency 'pry'
|
54
|
-
spec.add_development_dependency 'pry-byebug'
|
54
|
+
spec.add_development_dependency 'pry', '~> 0.14.1'
|
55
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.8.0'
|
55
56
|
end
|
56
57
|
end
|
58
|
+
|
59
|
+
if RUBY_VERSION.match(/^3\./)
|
60
|
+
spec.add_development_dependency 'webrick', '~> 1.7.0'
|
61
|
+
end
|
57
62
|
end
|
data/lib/ably.rb
CHANGED
data/lib/ably/agent.rb
ADDED
data/lib/ably/auth.rb
CHANGED
@@ -103,7 +103,6 @@ module Ably
|
|
103
103
|
end
|
104
104
|
|
105
105
|
if has_client_id? && !token_creatable_externally? && !token_option
|
106
|
-
raise ArgumentError, 'client_id cannot be provided without a complete API key or means to authenticate. An API key is needed to automatically authenticate with Ably and obtain a token' unless api_key_present?
|
107
106
|
@client_id = ensure_utf_8(:client_id, client_id) if client_id
|
108
107
|
end
|
109
108
|
|
@@ -377,7 +376,7 @@ module Ably
|
|
377
376
|
# True when Token Auth is being used to authenticate with Ably
|
378
377
|
def using_token_auth?
|
379
378
|
return options[:use_token_auth] if options.has_key?(:use_token_auth)
|
380
|
-
!!(token_option || current_token_details ||
|
379
|
+
!!(token_option || current_token_details || token_creatable_externally?)
|
381
380
|
end
|
382
381
|
|
383
382
|
def client_id
|
@@ -408,6 +407,17 @@ module Ably
|
|
408
407
|
end
|
409
408
|
end
|
410
409
|
|
410
|
+
# Extra headers that may be used during authentication
|
411
|
+
#
|
412
|
+
# @return [Hash] headers
|
413
|
+
def extra_auth_headers
|
414
|
+
if client_id && using_basic_auth?
|
415
|
+
{ 'X-Ably-ClientId' => Base64.urlsafe_encode64(client_id) }
|
416
|
+
else
|
417
|
+
{}
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
411
421
|
# Auth params used in URI endpoint for Realtime connections
|
412
422
|
# Will reauthorize implicitly if required and capable
|
413
423
|
#
|
data/lib/ably/exceptions.rb
CHANGED
@@ -52,6 +52,12 @@ module Ably
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
# Maximum frame size exceeded TO3l9
|
56
|
+
class MaxFrameSizeExceeded < BaseAblyException; end
|
57
|
+
|
58
|
+
# Maximum message size exceeded TO3l8
|
59
|
+
class MaxMessageSizeExceeded < BaseAblyException; end
|
60
|
+
|
55
61
|
# An invalid request was received by Ably
|
56
62
|
class InvalidRequest < BaseAblyException; end
|
57
63
|
|
data/lib/ably/models/message.rb
CHANGED
@@ -105,6 +105,20 @@ module Ably::Models
|
|
105
105
|
end.to_json
|
106
106
|
end
|
107
107
|
|
108
|
+
# The size is the sum over name, data, clientId, and extras in bytes (TO3l8a)
|
109
|
+
#
|
110
|
+
def size
|
111
|
+
%w(name data client_id extras).map do |attr|
|
112
|
+
if (value = attributes[attr.to_sym]).is_a?(String)
|
113
|
+
value.bytesize
|
114
|
+
elsif value.nil?
|
115
|
+
0
|
116
|
+
else
|
117
|
+
value.to_json.bytesize
|
118
|
+
end
|
119
|
+
end.sum
|
120
|
+
end
|
121
|
+
|
108
122
|
# Assign this message to a ProtocolMessage before delivery to the Ably system
|
109
123
|
# @api private
|
110
124
|
def assign_to_protocol_message(protocol_message)
|
@@ -125,6 +125,20 @@ module Ably::Models
|
|
125
125
|
end.to_json
|
126
126
|
end
|
127
127
|
|
128
|
+
# The size is the sum over data and clientId in bytes (TO3l8a)
|
129
|
+
#
|
130
|
+
def size
|
131
|
+
%w(data client_id).map do |attr|
|
132
|
+
if (value = attributes[attr.to_sym]).is_a?(String)
|
133
|
+
value.bytesize
|
134
|
+
elsif value.nil?
|
135
|
+
0
|
136
|
+
else
|
137
|
+
value.to_json.bytesize
|
138
|
+
end
|
139
|
+
end.sum
|
140
|
+
end
|
141
|
+
|
128
142
|
# Assign this presence message to a ProtocolMessage before delivery to the Ably system
|
129
143
|
# @api private
|
130
144
|
def assign_to_protocol_message(protocol_message)
|
@@ -185,6 +185,14 @@ module Ably::Models
|
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
188
|
+
def message_size
|
189
|
+
presence.map(&:size).sum + messages.map(&:size).sum
|
190
|
+
end
|
191
|
+
|
192
|
+
def has_correct_message_size?
|
193
|
+
message_size <= connection_details.max_message_size
|
194
|
+
end
|
195
|
+
|
188
196
|
def flags
|
189
197
|
Integer(attributes[:flags])
|
190
198
|
rescue TypeError
|
data/lib/ably/modules/ably.rb
CHANGED
@@ -6,8 +6,18 @@
|
|
6
6
|
module Ably
|
7
7
|
# Fallback hosts to use when a connection to rest/realtime.ably.io is not possible due to
|
8
8
|
# network failures either at the client, between the client and Ably, within an Ably data center, or at the IO domain registrar
|
9
|
+
# see https://docs.ably.io/client-lib-development-guide/features/#RSC15a
|
9
10
|
#
|
10
|
-
|
11
|
+
FALLBACK_DOMAIN = 'ably-realtime.com'.freeze
|
12
|
+
FALLBACK_IDS = %w(a b c d e).freeze
|
13
|
+
|
14
|
+
# Default production fallbacks a.ably-realtime.com ... e.ably-realtime.com
|
15
|
+
FALLBACK_HOSTS = FALLBACK_IDS.map { |host| "#{host}.#{FALLBACK_DOMAIN}".freeze }.freeze
|
16
|
+
|
17
|
+
# Custom environment default fallbacks {ENV}-a-fallback.ably-realtime.com ... {ENV}-a-fallback.ably-realtime.com
|
18
|
+
CUSTOM_ENVIRONMENT_FALLBACKS_SUFFIXES = FALLBACK_IDS.map do |host|
|
19
|
+
"-#{host}-fallback.#{FALLBACK_DOMAIN}".freeze
|
20
|
+
end.freeze
|
11
21
|
|
12
22
|
INTERNET_CHECK = {
|
13
23
|
url: '//internet-up.ably-realtime.com/is-the-internet-up.txt',
|
@@ -79,6 +79,10 @@ module Ably
|
|
79
79
|
# @return [Hash]
|
80
80
|
attr_reader :options
|
81
81
|
|
82
|
+
# Properties of a channel and its state
|
83
|
+
# @return [{Ably::Realtime::Channel::ChannelProperties}]
|
84
|
+
attr_reader :properties
|
85
|
+
|
82
86
|
# When a channel failure occurs this attribute contains the Ably Exception
|
83
87
|
# @return [Ably::Models::ErrorInfo,Ably::Exceptions::BaseAblyException]
|
84
88
|
attr_reader :error_reason
|
@@ -88,11 +92,6 @@ module Ably
|
|
88
92
|
# @api private
|
89
93
|
attr_reader :manager
|
90
94
|
|
91
|
-
# Serial number assigned to this channel when it was attached
|
92
|
-
# @return [Integer]
|
93
|
-
# @api private
|
94
|
-
attr_reader :attached_serial
|
95
|
-
|
96
95
|
# Initialize a new Channel object
|
97
96
|
#
|
98
97
|
# @param client [Ably::Rest::Client]
|
@@ -112,6 +111,7 @@ module Ably
|
|
112
111
|
@state = STATE(state_machine.current_state)
|
113
112
|
@manager = ChannelManager.new(self, client.connection)
|
114
113
|
@push = PushChannel.new(self)
|
114
|
+
@properties = ChannelProperties.new(self)
|
115
115
|
|
116
116
|
setup_event_handlers
|
117
117
|
setup_presence
|
@@ -292,7 +292,7 @@ module Ably
|
|
292
292
|
error = Ably::Exceptions::InvalidRequest.new('option :until_attach is invalid as the channel is not attached' )
|
293
293
|
return Ably::Util::SafeDeferrable.new_and_fail_immediately(logger, error)
|
294
294
|
end
|
295
|
-
options[:from_serial] =
|
295
|
+
options[:from_serial] = properties.attach_serial
|
296
296
|
end
|
297
297
|
|
298
298
|
async_wrap(callback) do
|
@@ -319,11 +319,6 @@ module Ably
|
|
319
319
|
@error_reason = nil
|
320
320
|
end
|
321
321
|
|
322
|
-
# @api private
|
323
|
-
def set_attached_serial(serial)
|
324
|
-
@attached_serial = serial
|
325
|
-
end
|
326
|
-
|
327
322
|
# @api private
|
328
323
|
def update_options(channel_options)
|
329
324
|
@options = channel_options.clone.freeze
|
@@ -372,3 +367,4 @@ end
|
|
372
367
|
require 'ably/realtime/channel/channel_manager'
|
373
368
|
require 'ably/realtime/channel/channel_state_machine'
|
374
369
|
require 'ably/realtime/channel/push_channel'
|
370
|
+
require 'ably/realtime/channel/channel_properties'
|
@@ -37,7 +37,7 @@ module Ably::Realtime
|
|
37
37
|
# library, such as returning to attached whne detach has failed
|
38
38
|
if attached_protocol_message
|
39
39
|
update_presence_sync_state_following_attached attached_protocol_message
|
40
|
-
channel.
|
40
|
+
channel.properties.set_attach_serial(attached_protocol_message.channel_serial)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -63,6 +63,8 @@ module Ably::Realtime
|
|
63
63
|
log_channel_error protocol_message.error
|
64
64
|
end
|
65
65
|
|
66
|
+
channel.properties.set_attach_serial(protocol_message.channel_serial)
|
67
|
+
|
66
68
|
if protocol_message.has_channel_resumed_flag?
|
67
69
|
logger.debug { "ChannelManager: Additional resumed ATTACHED message received for #{channel.state} channel '#{channel.name}'" }
|
68
70
|
else
|
@@ -75,8 +77,6 @@ module Ably::Realtime
|
|
75
77
|
)
|
76
78
|
update_presence_sync_state_following_attached protocol_message
|
77
79
|
end
|
78
|
-
|
79
|
-
channel.set_attached_serial protocol_message.channel_serial
|
80
80
|
end
|
81
81
|
|
82
82
|
# Handle DETACED messages, see #RTL13 for server-initated detaches
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Ably::Realtime
|
2
|
+
class Channel
|
3
|
+
# Represents properties of a channel and its state
|
4
|
+
class ChannelProperties
|
5
|
+
# {Ably::Realtime::Channel} this object associated with
|
6
|
+
# @return [Ably::Realtime::Channel]
|
7
|
+
attr_reader :channel
|
8
|
+
|
9
|
+
# Contains the last channelSerial received in an ATTACHED ProtocolMesage for the channel, see RTL15a
|
10
|
+
#
|
11
|
+
# @return [String]
|
12
|
+
attr_reader :attach_serial
|
13
|
+
|
14
|
+
def initialize(channel)
|
15
|
+
@channel = channel
|
16
|
+
end
|
17
|
+
|
18
|
+
# @api private
|
19
|
+
def set_attach_serial(attach_serial)
|
20
|
+
@attach_serial = attach_serial
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -22,6 +22,11 @@ module Ably::Realtime
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
if messages.sum(&:size) > Ably::Realtime::Connection::MAX_MESSAGE_SIZE
|
26
|
+
error = Ably::Exceptions::MaxMessageSizeExceeded.new("Message size exceeded #{Ably::Realtime::Connection::MAX_MESSAGE_SIZE} bytes.")
|
27
|
+
return Ably::Util::SafeDeferrable.new_and_fail_immediately(logger, error)
|
28
|
+
end
|
29
|
+
|
25
30
|
connection.send_protocol_message(
|
26
31
|
action: Ably::Models::ProtocolMessage::ACTION.Message.to_i,
|
27
32
|
channel: channel_name,
|
data/lib/ably/realtime/client.rb
CHANGED
@@ -65,11 +65,16 @@ module Ably
|
|
65
65
|
# @return [String,Nil]
|
66
66
|
attr_reader :recover
|
67
67
|
|
68
|
+
# Additional parameters to be sent in the querystring when initiating a realtime connection
|
69
|
+
# @return [Hash]
|
70
|
+
attr_reader :transport_params
|
71
|
+
|
68
72
|
def_delegators :auth, :client_id, :auth_options
|
69
73
|
def_delegators :@rest_client, :encoders
|
70
74
|
def_delegators :@rest_client, :use_tls?, :protocol, :protocol_binary?
|
71
75
|
def_delegators :@rest_client, :environment, :custom_host, :custom_port, :custom_tls_port
|
72
76
|
def_delegators :@rest_client, :log_level
|
77
|
+
def_delegators :@rest_client, :options
|
73
78
|
|
74
79
|
# Creates a {Ably::Realtime::Client Realtime Client} and configures the {Ably::Auth} object for the connection.
|
75
80
|
#
|
@@ -82,6 +87,7 @@ module Ably
|
|
82
87
|
# @option options [Boolean] :echo_messages If false, prevents messages originating from this connection being echoed back on the same connection
|
83
88
|
# @option options [String] :recover When a recover option is specified a connection inherits the state of a previous connection that may have existed under a different instance of the Realtime library, please refer to the API documentation for further information on connection state recovery
|
84
89
|
# @option options [Boolean] :auto_connect By default as soon as the client library is instantiated it will connect to Ably. You can optionally set this to false and explicitly connect.
|
90
|
+
# @option options [Hash] :transport_params Additional parameters to be sent in the querystring when initiating a realtime connection. Keys are Strings, values are Stringifiable(a value must respond to #to_s)
|
85
91
|
#
|
86
92
|
# @option options [Integer] :channel_retry_timeout (15 seconds). When a channel becomes SUSPENDED, after this delay in seconds, the channel will automatically attempt to reattach if the connection is CONNECTED
|
87
93
|
# @option options [Integer] :disconnected_retry_timeout (15 seconds). When the connection enters the DISCONNECTED state, after this delay in seconds, if the state is still DISCONNECTED, the client library will attempt to reconnect automatically
|
@@ -109,6 +115,9 @@ module Ably
|
|
109
115
|
end
|
110
116
|
end
|
111
117
|
|
118
|
+
@transport_params = options.delete(:transport_params).to_h.each_with_object({}) do |(key, value), acc|
|
119
|
+
acc[key.to_s] = value.to_s
|
120
|
+
end
|
112
121
|
@rest_client = Ably::Rest::Client.new(options.merge(realtime_client: self))
|
113
122
|
@echo_messages = rest_client.options.fetch(:echo_messages, true) == false ? false : true
|
114
123
|
@queue_messages = rest_client.options.fetch(:queue_messages, true) == false ? false : true
|
@@ -121,15 +121,23 @@ module Ably::Realtime
|
|
121
121
|
presence.manager.sync_process_messages protocol_message.channel_serial, protocol_message.presence
|
122
122
|
|
123
123
|
when ACTION.Presence
|
124
|
-
|
125
|
-
|
126
|
-
presence.
|
124
|
+
if protocol_message.has_correct_message_size?
|
125
|
+
presence = get_channel(protocol_message.channel).presence
|
126
|
+
protocol_message.presence.each do |presence_message|
|
127
|
+
presence.__incoming_msgbus__.publish :presence, presence_message
|
128
|
+
end
|
129
|
+
else
|
130
|
+
logger.fatal Ably::Exceptions::ProtocolError.new("Not published. Channel message limit exceeded #{protocol_message.message_size} bytes", 400, Ably::Exceptions::Codes::UNABLE_TO_RECOVER_CHANNEL_MESSAGE_LIMIT_EXCEEDED).message
|
127
131
|
end
|
128
132
|
|
129
133
|
when ACTION.Message
|
130
|
-
|
131
|
-
|
132
|
-
|
134
|
+
if protocol_message.has_correct_message_size?
|
135
|
+
channel = get_channel(protocol_message.channel)
|
136
|
+
protocol_message.messages.each do |message|
|
137
|
+
channel.__incoming_msgbus__.publish :message, message
|
138
|
+
end
|
139
|
+
else
|
140
|
+
logger.fatal Ably::Exceptions::ProtocolError.new("Not published. Channel message limit exceeded #{protocol_message.message_size} bytes", 400, Ably::Exceptions::Codes::UNABLE_TO_RECOVER_CHANNEL_MESSAGE_LIMIT_EXCEEDED).message
|
133
141
|
end
|
134
142
|
|
135
143
|
when ACTION.Auth
|
@@ -82,6 +82,9 @@ module Ably
|
|
82
82
|
# Max number of messages to bundle in a single ProtocolMessage
|
83
83
|
MAX_PROTOCOL_MESSAGE_BATCH_SIZE = 50
|
84
84
|
|
85
|
+
# Max message size
|
86
|
+
MAX_MESSAGE_SIZE = 65536 # See spec TO3l8
|
87
|
+
|
85
88
|
# A unique public identifier for this connection, used to identify this member in presence events and messages
|
86
89
|
# @return [String]
|
87
90
|
attr_reader :id
|
@@ -292,7 +295,7 @@ module Ably
|
|
292
295
|
def internet_up?
|
293
296
|
url = "http#{'s' if client.use_tls?}:#{Ably::INTERNET_CHECK.fetch(:url)}"
|
294
297
|
EventMachine::DefaultDeferrable.new.tap do |deferrable|
|
295
|
-
EventMachine::HttpRequest.new(url).get.tap do |http|
|
298
|
+
EventMachine::HttpRequest.new(url, tls: { verify_peer: true }).get.tap do |http|
|
296
299
|
http.errback do
|
297
300
|
yield false if block_given?
|
298
301
|
deferrable.fail Ably::Exceptions::ConnectionFailed.new("Unable to connect to #{url}", nil, Ably::Exceptions::Codes::CONNECTION_FAILED)
|
@@ -431,10 +434,10 @@ module Ably
|
|
431
434
|
client.auth.auth_params.tap do |auth_deferrable|
|
432
435
|
auth_deferrable.callback do |auth_params|
|
433
436
|
url_params = auth_params.merge(
|
434
|
-
format
|
435
|
-
echo
|
436
|
-
v
|
437
|
-
|
437
|
+
'format' => client.protocol,
|
438
|
+
'echo' => client.echo_messages,
|
439
|
+
'v' => Ably::PROTOCOL_VERSION,
|
440
|
+
'agent' => client.rest_client.agent
|
438
441
|
)
|
439
442
|
|
440
443
|
# Use native websocket heartbeats if possible, but allow Ably protocol heartbeats
|
@@ -445,6 +448,7 @@ module Ably
|
|
445
448
|
end
|
446
449
|
|
447
450
|
url_params['clientId'] = client.auth.client_id if client.auth.has_client_id?
|
451
|
+
url_params.merge!(client.transport_params)
|
448
452
|
|
449
453
|
if connection_resumable?
|
450
454
|
url_params.merge! resume: key, connection_serial: serial
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
|
1
3
|
module Ably::Realtime
|
2
4
|
class Connection
|
3
5
|
# EventMachine WebSocket transport
|
@@ -16,10 +18,13 @@ module Ably::Realtime
|
|
16
18
|
)
|
17
19
|
include Ably::Modules::StateEmitter
|
18
20
|
|
21
|
+
attr_reader :host
|
22
|
+
|
19
23
|
def initialize(connection, url)
|
20
24
|
@connection = connection
|
21
25
|
@state = STATE.Initialized
|
22
26
|
@url = url
|
27
|
+
@host = URI.parse(url).hostname
|
23
28
|
|
24
29
|
setup_event_handlers
|
25
30
|
end
|
@@ -49,7 +54,7 @@ module Ably::Realtime
|
|
49
54
|
# Required {http://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine/Connection EventMachine::Connection} interface
|
50
55
|
def connection_completed
|
51
56
|
change_state STATE.Connected
|
52
|
-
start_tls if client.use_tls?
|
57
|
+
start_tls(tls_opts) if client.use_tls?
|
53
58
|
driver.start
|
54
59
|
end
|
55
60
|
|
@@ -77,6 +82,51 @@ module Ably::Realtime
|
|
77
82
|
send_data(data)
|
78
83
|
end
|
79
84
|
|
85
|
+
# TLS verification support, original implementation by Mislav Marohnić:
|
86
|
+
#
|
87
|
+
# https://github.com/lostisland/faraday/commit/63cf47c95b573539f047c729bd9ad67560bc83ff
|
88
|
+
def ssl_verify_peer(cert_string)
|
89
|
+
cert = nil
|
90
|
+
begin
|
91
|
+
cert = OpenSSL::X509::Certificate.new(cert_string)
|
92
|
+
rescue OpenSSL::X509::CertificateError => e
|
93
|
+
disconnect_with_reason "Websocket host '#{host}' returned an invalid TLS certificate: #{e.message}"
|
94
|
+
return false
|
95
|
+
end
|
96
|
+
|
97
|
+
@last_seen_cert = cert
|
98
|
+
|
99
|
+
if certificate_store.verify(@last_seen_cert)
|
100
|
+
begin
|
101
|
+
certificate_store.add_cert(@last_seen_cert)
|
102
|
+
rescue OpenSSL::X509::StoreError => e
|
103
|
+
unless e.message == 'cert already in hash table'
|
104
|
+
disconnect_with_reason "Websocket host '#{host}' returned an invalid TLS certificate: #{e.message}"
|
105
|
+
return false
|
106
|
+
end
|
107
|
+
end
|
108
|
+
true
|
109
|
+
else
|
110
|
+
disconnect_with_reason "Websocket host '#{host}' returned an invalid TLS certificate"
|
111
|
+
false
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def ssl_handshake_completed
|
116
|
+
unless OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
|
117
|
+
disconnect_with_reason "Websocket host '#{host}' returned an invalid TLS certificate"
|
118
|
+
false
|
119
|
+
else
|
120
|
+
true
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def certificate_store
|
125
|
+
@certificate_store ||= OpenSSL::X509::Store.new.tap do |store|
|
126
|
+
store.set_default_paths
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
80
130
|
# True if socket connection is ready to be released
|
81
131
|
# i.e. it is not currently connecting or connected
|
82
132
|
def ready_for_release?
|
@@ -106,6 +156,12 @@ module Ably::Realtime
|
|
106
156
|
@connection
|
107
157
|
end
|
108
158
|
|
159
|
+
def disconnect_with_reason(reason)
|
160
|
+
client.logger.error { "WebsocketTransport: Disconnecting due to error: #{reason}" }
|
161
|
+
@reason_closed = reason
|
162
|
+
disconnect
|
163
|
+
end
|
164
|
+
|
109
165
|
def reason_closed
|
110
166
|
@reason_closed
|
111
167
|
end
|
@@ -214,6 +270,16 @@ module Ably::Realtime
|
|
214
270
|
end
|
215
271
|
)
|
216
272
|
end
|
273
|
+
|
274
|
+
# TLS options to pass to EventMachine::Connection#start_tls
|
275
|
+
#
|
276
|
+
# See https://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine/Connection#start_tls-instance_method
|
277
|
+
def tls_opts
|
278
|
+
{
|
279
|
+
sni_hostname: host,
|
280
|
+
verify_peer: true,
|
281
|
+
}
|
282
|
+
end
|
217
283
|
end
|
218
284
|
end
|
219
285
|
end
|