Class: CableX::Cable::Connection

Inherits:
ActionCable::Connection::Base
  • Object
show all
Includes:
RateLimit::BlockCheck, RateLimit::CheckRateLimit, RateLimit::RateLimitRedis
Defined in:
lib/cable_x/cable/connection.rb

Overview

Connection class to serve entry point

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RateLimit::BlockCheck

#block_device, #check_block, #check_block_unit, #disconnect_client

Methods included from RateLimit::CheckRateLimit

#check_rate_limit, #check_unit, #do_rate_limit

Methods included from RateLimit::RateLimitRedis

#block_device_key, #device_key, #redis_exists, #redis_get, #redis_incr, #redis_set

Class Attribute Details

.rate_limitObject

Returns the value of attribute rate_limit.



17
18
19
# File 'lib/cable_x/cable/connection.rb', line 17

def rate_limit
  @rate_limit
end

.redis_configObject

Returns the value of attribute redis_config.



17
18
19
# File 'lib/cable_x/cable/connection.rb', line 17

def redis_config
  @redis_config
end

Instance Attribute Details

#device_idObject

Returns the value of attribute device_id.



20
21
22
# File 'lib/cable_x/cable/connection.rb', line 20

def device_id
  @device_id
end

#rate_limitObject

Returns the value of attribute rate_limit.



20
21
22
# File 'lib/cable_x/cable/connection.rb', line 20

def rate_limit
  @rate_limit
end

#redisObject

Returns the value of attribute redis.



20
21
22
# File 'lib/cable_x/cable/connection.rb', line 20

def redis
  @redis
end

#redis_configObject

Returns the value of attribute redis_config.



20
21
22
# File 'lib/cable_x/cable/connection.rb', line 20

def redis_config
  @redis_config
end

Instance Method Details

#check_message(message) ⇒ Object



52
53
54
# File 'lib/cable_x/cable/connection.rb', line 52

def check_message(message)
  JSON.parse(message) rescue false
end

#close(reason, reconnect = false) ⇒ Object



40
41
42
43
44
45
# File 'lib/cable_x/cable/connection.rb', line 40

def close(reason, reconnect = false)
  transmit(type: ActionCable::INTERNAL[:message_types][:disconnect],
           reason: reason,
           reconnect: reconnect)
  websocket.close
end

#connectObject



23
24
25
26
27
28
# File 'lib/cable_x/cable/connection.rb', line 23

def connect
  self.device_id = Digest::SHA1.hexdigest(request_str)
  setup_rate_limit
rescue StandardError => _e
  reject_unauthorized_connection
end

#malformed_message_responseObject



47
48
49
50
# File 'lib/cable_x/cable/connection.rb', line 47

def malformed_message_response
  transmit(type: 'error',
           message: 'malformed_message')
end

#receive(message) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/cable_x/cable/connection.rb', line 30

def receive(message)
  if (blocked_unit = check_block) || (blocked_unit = do_rate_limit)
    disconnect_client(blocked_unit)
  elsif check_message(message)
    super(message)
  else
    malformed_message_response
  end
end

#request_strObject



66
67
68
69
70
71
72
# File 'lib/cable_x/cable/connection.rb', line 66

def request_str
  headers_to_get = %w[SERVER_PROTOCOL SERVER_SOFTWARE REQUEST_METHOD REQUEST_PATH HTTP_USER_AGENT\
                      HTTP_CONNECTION HTTP_ORIGIN REMOTE_ADDR ORIGINAL_FULLPATH ORIGINAL_SCRIPT_NAME\
                      action_dispatch.authorized_host]
  headers = request.headers.to_h
  headers_to_get.map { |key| headers[key] }.join('-').gsub(' ', '')
end

#setup_rate_limitObject



56
57
58
59
60
61
62
63
64
# File 'lib/cable_x/cable/connection.rb', line 56

def setup_rate_limit
  self.rate_limit = self.class.rate_limit
  self.redis_config = self.class.redis_config
  if rate_limit
    self.redis = Redis.new(url: redis_config[:url])
    raise StandardError, 'Rate limiting: Slow down!. Try after some time' if check_block
  end
  true
end