cable_room 0.4.3 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aec4300c2501be5b03552fb3ee2aa096dfeeae41203f85ad1d9715084f209e2e
4
- data.tar.gz: 13b6d7062eb049b8235948d80fc121defffd571054cbaf64403ca5adda0c77b4
3
+ metadata.gz: 57e1433d4d4cc345377d621c6c1ce693280625909eeab0da36a2d1c43dca13de
4
+ data.tar.gz: d7f1de1afa7a9d527f6682bc1c109f6ee4dd9979399c369bc0bf4a1903f0b4f5
5
5
  SHA512:
6
- metadata.gz: 58db3c0ae21e9844c810c124241911dcf4414ca068b90f72f7fb9bb734ba3a301f59ae6d73efc787d01144b023fcb6b69aa0ffc842f889a5306349dbab1b8b76
7
- data.tar.gz: b2fd75650ced68cbb4e071e77e9d81e2f2fa61502d8164eda4f9a0bfaa5360471f4ed2ae0a4840be40aa6a4cd9c8ef53759b3198021fa7eb105c7740c22e1294
6
+ metadata.gz: 723a2bca5fccb0d7254f0e255b5e1221e73293365af63a56fe17453eb37aa90f8be3ddc20c248c75df3472a89d0802a85b641db971f99446ef692bfb593a25b0
7
+ data.tar.gz: f3c985570d1d8c9361351850da19b4a0371e14268354acb79f3f5fb1bf50246360c7c20a960d7622a53646a0e71d090798587056194079cb62ecf1f47885ea65
@@ -105,6 +105,8 @@ module CableRoom
105
105
  @mutex.synchronize do
106
106
  return if @current_state == :dead || @current_state == :shutting_down
107
107
 
108
+ logger.info "Initiating shutdown: #{reason}"
109
+
108
110
  # Stop streams immediately to prevent further messages from being added
109
111
  stop_all_streams
110
112
 
@@ -75,8 +75,6 @@ module CableRoom
75
75
  end
76
76
  end
77
77
 
78
- protected
79
-
80
78
  def each_room_channel(&blk)
81
79
  @room_channels.dup.each(&blk)
82
80
  end
@@ -14,9 +14,19 @@ module CableRoom
14
14
  runner do
15
15
  end
16
16
 
17
- initializer "cable_room.shutdown" do
18
- Rails.application.reloader.before_class_unload do
19
- CableRoom::ChannelTracker.instance.shutdown_rooms!
17
+ initializer "cable_room.hook_action_cable_restart" do
18
+ module ActionCableServerExtensions
19
+ def restart
20
+ CableRoom::ChannelTracker.instance.each_room_channel do |chan|
21
+ chan.unsubscribe_from_channel
22
+ end
23
+ # CableRoom::ChannelTracker.instance.shutdown_rooms!
24
+ super
25
+ end
26
+ end
27
+
28
+ ActiveSupport.on_load(:action_cable) do
29
+ ActionCable::Server::Base.prepend(ActionCableServerExtensions)
20
30
  end
21
31
  end
22
32
 
@@ -25,6 +25,9 @@ module CableRoom
25
25
  vchannel.subscribe_to_channel
26
26
 
27
27
  true
28
+ rescue => e
29
+ CableRoom.lock_manager.unlock(lock_info) if lock_info
30
+ raise e
28
31
  end
29
32
 
30
33
  def room_port_key(room_key, port = nil)
@@ -28,11 +28,20 @@ module CableRoom
28
28
  end
29
29
  end
30
30
  end
31
+
32
+ # Prevent certain message types being forwarded from clients
33
+ def system_message_types(*types)
34
+ ([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse_each do |target|
35
+ target._system_message_types = target._system_message_types | Set.new(types.map(&:to_sym))
36
+ end
37
+ end
31
38
  end
32
39
 
33
40
  included do
34
41
  define_callbacks :receive_message
35
42
 
43
+ class_attribute :_system_message_types, instance_writer: false, default: Set.new
44
+
36
45
  set_callback(:receive_message, :before) do
37
46
  logger.debug "Received message: #{message.inspect}"
38
47
  end
@@ -36,8 +36,16 @@ module CableRoom
36
36
  end
37
37
  end
38
38
 
39
+ system_message_types(:port_connected, :port_disconnected, :port_ping)
40
+
39
41
  require_relative 'port_policies'
40
42
  include PortPolicies
43
+
44
+ PortPolicies::TagPolicy.define_tag_alias :connect, [:port_connected, :port_ping, :port_disconnected]
45
+
46
+ inbound_tag_policy(priority: -10) do
47
+ allow :*, :connect
48
+ end
41
49
  end
42
50
 
43
51
  def initialize(...)
@@ -36,10 +36,6 @@ module CableRoom
36
36
  next true if %i[port_connected port_disconnected].include?(msg_type)
37
37
  policy_allows?(msg_type)
38
38
  end
39
-
40
- inbound_tag_policy(priority: -10) do
41
- allow :*, :connect
42
- end
43
39
  end
44
40
 
45
41
  protected
@@ -58,8 +54,6 @@ module CableRoom
58
54
  end
59
55
  end
60
56
 
61
- define_tag_alias :connect, [:port_connected, :port_ping, :port_disconnected]
62
-
63
57
  def initialize()
64
58
  @policy_rules = []
65
59
  @fallback_rule = { action: :allow, priority: -100, tag: :*, methods: :* }
@@ -25,11 +25,13 @@ module CableRoom
25
25
  end
26
26
  end
27
27
 
28
+ system_message_types(:user_joined, :user_left)
29
+
30
+ PortPolicies::TagPolicy.define_tag_alias :join, [:connect, :user_joined, :user_left]
31
+
28
32
  inbound_tag_policy(priority: -10) do
29
33
  allow :*, :join
30
34
  end
31
-
32
- PortPolicies::TagPolicy.define_tag_alias :join, [:connect, :user_joined, :user_left]
33
35
  end
34
36
 
35
37
  def initialize(...)
@@ -120,7 +120,7 @@ module CableRoom
120
120
  def ping!
121
121
  return if left?
122
122
 
123
- self << { type: 'port_ping' }
123
+ port_transmit(room_class::ROOM_IN_CHANNEL, { type: 'port_ping' }, secure_context: true)
124
124
  maybe_provision_room
125
125
  end
126
126
 
@@ -129,7 +129,7 @@ module CableRoom
129
129
 
130
130
  close_streamed_ports!
131
131
  @cable_channel._room_memberships.delete(self)
132
- self << { type: 'port_disconnected' }
132
+ port_transmit(room_class::ROOM_IN_CHANNEL, { type: 'port_disconnected' }, secure_context: true)
133
133
  @has_left = true
134
134
  @on_closed&.call(self)
135
135
  end
@@ -138,9 +138,18 @@ module CableRoom
138
138
 
139
139
  protected
140
140
 
141
- def port_transmit(port, data)
141
+ def port_transmit(port, data, secure_context: false)
142
142
  data[:mtok] = @token
143
- super
143
+
144
+ unless secure_context
145
+ t = (data[:type] || data['type']).to_sym
146
+ if room_class._system_message_types.include?(t)
147
+ logger.warn "Dropping attempt to send system message type: #{t.inspect}"
148
+ return
149
+ end
150
+ end
151
+
152
+ super(port, data)
144
153
  end
145
154
 
146
155
  def room_port_key(port)
@@ -172,7 +181,7 @@ module CableRoom
172
181
  tags: @tags,
173
182
  }
174
183
  msg[:extra] = ::ActiveJob::Arguments.serialize([@extra]) if @extra
175
- self << msg
184
+ port_transmit(room_class::ROOM_IN_CHANNEL, msg, secure_context: true)
176
185
  end
177
186
 
178
187
  def maybe_provision_room
@@ -1,3 +1,3 @@
1
1
  module CableRoom
2
- VERSION = "0.4.3".freeze
2
+ VERSION = "0.4.5".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cable_room
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan Knapp