hackedteam/rcs-db

View on GitHub
lib/rcs-db/db_objects/collector.rb

Summary

Maintainability
C
1 day
Test Coverage
require 'mongoid'

class Collector
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String
  field :desc, type: String
  field :type, type: String
  field :address, type: String
  field :internal_address, type: String
  field :port, type: Integer
  field :instance, type: String
  field :poll, type: Boolean
  field :version, type: Integer
  field :configured, type: Boolean, default: false
  field :upgradable, type: Boolean, default: false
  field :cookie, type: String
  field :key, type: String

  # used in case of crisis
  field :good, type: Boolean, default: true

  field :next, type: Array
  field :prev, type: Array

  index({name: 1}, {background: true})
  index({address: 1}, {background: true})
  index({internal_address: 1}, {background: true})
  index({type: 1}, {background: true})

  scope :remote, where(type: 'remote')
  scope :local, where(type: 'local')

  store_in collection: 'collectors'

  after_destroy :drop_log_collection
  after_create :create_log_collection
  before_create :setup_encryption_keys

  before_save do
    raise("Unable to save #{name}. The address #{address} is blacklisted.") if (changed_attributes['address'] || new_record?) and blacklisted?
  end

  def drop_log_collection
    CappedLog.collection_class(self._id.to_s).collection.drop
  end

  def create_log_collection
    CappedLog.collection_class(self._id.to_s).create_capped_collection
  end

  def setup_encryption_keys
    return unless self.type.eql? 'remote'
    self.cookie = UUIDTools::UUID.random_create.to_s
    self.key = SecureRandom.hex(16)
  end

  def config
    # get the next hop collector
    next_hop = Collector.find(self.prev[0]) if self.prev[0]
    (next_hop and next_hop.address.length > 0) ? next_hop.address + ':80' : '-'
  end

  def self.collector_login(instance, version, ext_address, local_address)
    coll = Collector.where({type: 'local'}).any_in({instance: [instance]}).first

    # the collector does not exist, check the licence and create it
    if coll.nil?
      raise 'LICENSE_LIMIT_EXCEEDED' unless LicenseManager.instance.check :collectors

      coll = Collector.new
      coll.type = 'local'
      coll.instance = instance
      coll.name = "Collector Node on #{local_address}"
      coll.desc = "Collector Node on #{local_address}"
      coll.internal_address = local_address
      coll.address = ext_address
      coll.version = version
      coll.poll = false
      coll.next = [nil]
      coll.prev = [nil]
      coll.save
    else
      # the collector already exists, check if the external address is set, otherwise update it
      if coll.address.nil? or coll.address == ''
        coll.address = ext_address
      end
      # update the version (can change after RCS upgrade)
      coll.version = version
      coll.internal_address = local_address
      coll.save
    end
  end

  BLACKLIST = %w[
    101.99.83.12
    103.1.185.34
    106.186.118.191
    106.186.16.48
    106.186.20.84
    106.186.21.100
    106.186.22.41
    106.187.17.60
    106.187.39.208
    106.187.47.66
    106.187.48.66
    106.187.93.203
    106.187.95.195
    106.187.99.249
    108.166.112.17
    108.166.64.231
    108.171.173.171
    108.59.250.33
    109.200.22.160
    109.200.22.161
    109.200.22.162
    109.235.193.83
    109.52.0.177
    109.53.116.4
    109.53.119.199
    109.53.205.186
    109.54.1.245
    109.74.205.30
    117.18.68.13
    124.217.238.91
    124.217.245.64
    14.136.236.147
    14.136.236.155
    14.136.236.162
    14.136.236.163
    14.136.236.165
    141.105.67.79
    146.0.78.137
    146.185.23.200
    146.185.27.159
    146.185.30.109
    146.255.27.113
    149.255.111.168
    151.236.221.202
    156.54.104.153
    158.255.1.207
    164.138.28.81
    166.78.125.70
    166.78.143.145
    166.78.144.231
    166.78.242.86
    168.144.159.167
    173.230.130.68
    173.230.142.200
    173.255.201.245
    173.255.211.215
    173.255.212.72
    173.255.229.228
    173.255.234.29
    173.255.241.225
    173.255.247.246
    176.200.243.115
    176.200.244.130
    176.201.33.3
    176.201.42.124
    176.201.5.195
    176.216.47.175
    176.218.9.153
    176.55.188.147
    176.58.100.37
    176.58.102.218
    176.58.117.109
    176.58.120.129
    176.58.121.242
    176.74.178.119
    176.74.178.120
    176.74.178.203
    176.74.178.45
    176.89.253.117
    176.91.125.74
    178.170.144.28
    178.170.144.29
    178.33.45.123
    178.33.45.124
    178.33.45.125
    178.79.146.167
    178.79.159.206
    178.79.166.117
    178.79.171.87
    178.79.176.69
    184.106.178.154
    184.106.196.42
    184.106.244.36
    184.75.250.118
    185.7.35.79
    185.7.35.80
    187.145.204.164
    187.145.220.106
    187.177.168.211
    187.210.43.35
    187.210.43.46
    188.121.60.235
    189.177.50.164
    189.177.56.158
    189.177.65.13
    189.177.74.147
    189.177.98.180
    190.105.232.104
    190.105.232.105
    190.2.239.22
    190.242.96.49
    192.30.161.219
    193.17.175.94
    193.227.199.48
    195.46.247.92
    195.46.247.93
    195.70.143.173
    197.210.255.178
    198.101.232.37
    198.101.232.81
    198.101.240.125
    198.136.60.208
    198.144.178.104
    198.144.178.118
    198.55.103.29
    198.58.102.169
    198.58.104.34
    198.58.96.44
    198.61.233.103
    198.74.53.215
    199.180.131.145
    199.38.82.120
    2.193.109.253
    2.193.227.116
    2.193.229.110
    2.194.15.3
    2.228.65.226
    200.67.230.2
    200.74.245.36
    200.74.245.37
    201.122.175.101
    201.157.43.59
    201.157.43.60
    201.218.236.236
    202.129.190.103
    203.149.47.20
    203.217.178.3
    204.188.197.38
    204.188.221.198
    206.190.155.40
    206.72.195.246
    208.89.215.109
    209.140.24.194
    209.20.68.224
    209.59.205.76
    211.51.14.129
    212.117.180.108
    212.71.232.6
    212.71.252.71
    213.211.150.178
    216.118.249.92
    216.118.249.94
    216.172.132.27
    216.172.132.28
    216.172.132.29
    216.224.172.202
    216.224.174.48
    217.201.238.138
    217.29.123.184
    219.94.51.144
    219.94.51.145
    219.94.51.146
    219.94.51.147
    219.94.51.148
    219.94.51.149
    219.94.51.150
    219.94.51.151
    23.239.11.231
    23.239.16.33
    23.239.9.226
    23.92.30.182
    31.192.228.60
    37.0.121.24
    37.0.121.37
    37.0.121.99
    37.17.173.22
    37.221.166.23
    37.221.166.45
    37.242.13.10
    37.247.52.212
    41.248.248.176
    41.33.151.146
    41.33.151.147
    41.33.151.150
    41.78.109.91
    46.166.161.19
    46.166.161.20
    46.166.161.21
    46.166.162.109
    46.166.162.132
    46.166.162.134
    46.166.162.135
    46.166.162.138
    46.166.162.143
    46.166.162.145
    46.166.162.147
    46.166.162.148
    46.166.162.15
    46.166.162.150
    46.166.163.164
    46.166.163.166
    46.166.163.167
    46.166.163.168
    46.166.163.170
    46.166.163.171
    46.166.163.174
    46.166.163.175
    46.166.163.176
    46.166.163.179
    46.166.165.10
    46.166.165.100
    46.166.165.101
    46.166.167.215
    46.166.167.216
    46.166.167.217
    46.166.169.185
    46.166.169.31
    46.166.169.35
    46.166.169.36
    46.166.169.38
    46.166.169.40
    46.166.169.41
    46.166.169.43
    46.166.169.50
    46.166.169.72
    46.166.184.10
    46.166.184.100
    46.166.184.101
    46.166.184.102
    46.166.184.103
    46.166.184.104
    46.166.184.105
    46.166.185.2
    46.166.185.3
    46.166.185.4
    46.17.102.18
    46.17.97.78
    46.182.107.192
    46.183.220.222
    46.246.95.240
    46.251.239.122
    46.251.239.123
    46.251.239.124
    46.251.239.125
    46.251.239.129
    46.251.239.130
    46.251.239.131
    46.4.69.25
    5.135.193.105
    5.229.226.28
    5.96.18.86
    50.116.27.11
    50.116.32.138
    50.116.33.222
    50.116.37.7
    50.116.38.37
    50.116.45.188
    50.116.45.251
    50.56.182.189
    50.56.56.69
    50.57.153.182
    50.63.138.143
    50.63.180.92
    50.7.162.220
    50.7.251.133
    59.10.91.161
    62.109.23.153
    62.109.31.96
    62.149.88.20
    62.231.247.27
    62.244.11.51
    62.244.11.55
    62.244.11.58
    62.244.11.83
    62.244.11.87
    62.244.11.89
    62.244.11.94
    62.251.188.193
    64.251.21.33
    64.32.12.61
    64.32.12.75
    65.111.180.122
    65.111.180.124
    65.111.181.108
    65.39.236.49
    65.39.236.59
    66.175.218.92
    66.228.61.26
    66.85.184.73
    67.202.108.201
    67.202.108.204
    67.202.108.205
    67.202.108.206
    67.202.108.207
    67.202.108.208
    67.202.108.209
    67.207.153.47
    69.164.215.135
    69.164.222.128
    69.60.98.10
    69.60.98.193
    69.60.98.197
    69.60.98.202
    69.60.98.203
    69.60.98.203
    69.60.98.9
    70.39.235.234
    72.20.18.72
    74.207.224.54
    74.207.232.135
    74.208.223.2
    74.50.126.187
    74.50.126.189
    74.50.126.191
    74.50.126.193
    74.50.126.195
    74.50.126.197
    74.50.126.199
    74.50.126.201
    74.50.126.203
    74.50.126.205
    77.246.76.211
    77.246.76.212
    78.110.165.122
    78.110.165.123
    78.142.63.173
    78.142.63.174
    79.39.180.200
    80.253.180.6
    80.83.125.251
    81.16.200.196
    81.209.165.101
    81.95.224.10
    81.95.226.134
    82.104.200.51
    82.211.15.137
    82.84.66.189
    82.84.93.81
    83.103.117.82
    83.111.56.188
    83.111.56.189
    85.153.34.173
    85.153.34.187
    85.153.34.9
    87.229.65.179
    88.49.232.170
    88.49.232.172
    88.80.184.38
    88.80.197.161
    89.190.94.14
    91.109.17.189
    91.222.36.232
    91.222.36.233
    91.222.36.238
    91.222.36.240
    91.222.36.243
    91.229.76.120
    91.229.76.91
    91.82.84.90
    92.114.82.207
    92.114.82.243
    92.62.218.204
    93.62.139.39
    93.62.139.40
    93.62.139.42
    94.112.241.138
    94.199.183.167
    94.199.243.39
    94.36.180.5
    94.56.152.78
    95.141.46.236
    95.225.148.200
    95.228.202.81
    95.228.202.82
    95.228.202.99
    95.49.161.201
    95.49.161.202
    95.49.161.253
    95.49.161.74
    95.49.162.196
    95.49.162.210
    95.49.163.177
    95.49.163.25
    95.49.163.55
    95.49.163.60
    95.49.168.190
    95.49.168.245
    95.49.168.79
    95.49.168.92
    95.49.169.112
    95.49.169.139
    95.49.169.140
    95.49.169.148
    95.49.169.181
    95.49.169.216
    95.49.169.219
    95.49.169.231
    95.49.169.234
    95.49.170.123
    95.49.170.212
    95.49.170.229
    95.49.170.242
    95.49.170.7
    95.49.171.103
    95.49.171.130
    95.49.171.146
    95.49.171.175
    95.49.171.246
    95.49.171.252
    95.49.171.82
    95.49.172.203
    95.49.172.32
    95.49.172.41
    95.49.173.1
    95.49.173.193
    95.49.173.195
    95.49.173.221
    95.49.173.222
    95.49.174.149
    95.49.174.214
    95.49.174.40
    95.49.174.60
    95.49.174.68
    95.49.174.69
    95.49.174.88
    95.49.174.89
    95.49.175.104
    95.49.175.112
    95.49.175.144
    95.49.175.148
    95.49.175.162
    95.49.175.186
    95.49.175.233
    95.49.175.241
    95.49.175.48
    95.49.175.64
    95.49.175.87
    95.49.176.145
    95.49.176.160
    95.49.176.188
    95.49.176.192
    95.49.176.34
    95.49.176.70
    95.49.177.104
    95.49.177.135
    95.49.177.99
    95.49.178.1
    95.49.178.120
    95.49.178.19
    95.49.178.238
    95.49.178.25
    95.49.178.68
    95.49.178.97
    95.49.179.113
    95.49.179.158
    95.49.179.19
    95.49.179.218
    95.49.179.27
    95.49.179.64
    95.49.180.123
    95.49.180.171
    95.49.180.79
    95.49.180.98
    95.49.181.175
    95.49.181.207
    95.49.181.26
    95.49.181.54
    95.49.181.81
    95.49.182.181
    95.49.182.210
    95.49.182.218
    95.49.182.54
    95.49.183.18
    95.49.183.209
    95.49.183.231
    95.49.183.240
    95.49.183.25
    95.49.183.46
    95.49.183.92
    95.49.204.15
    95.49.204.178
    95.49.204.241
    95.49.204.31
    95.49.204.4
    95.49.205.126
    95.49.205.156
    95.49.205.188
    95.49.205.194
    95.49.205.210
    95.9.71.180
    96.126.112.42
    97.107.132.111
  ]

  def blacklist
    BLACKLIST
  end

  def blacklisted?
    self.class.blacklisted?(self.address.to_s)
  end

  def self.blacklisted?(address)
    BLACKLIST.include?(address)
  end
end