Module: Paraxial::Checker

Defined in:
lib/paraxial/checker.rb

Class Method Summary collapse

Class Method Details

.ban_ip_msg(ip, length, msg) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/paraxial/checker.rb', line 133

def self.ban_ip_msg(ip, length, msg)
  if whitelist_ip?(ip)
    :on_whitelist
  elsif blacklist_ip?(ip)
    :already_banned
  else
    local_ban(ip)

    uri = URI.parse(Paraxial::Helpers.get_ruby_ban_url)
    body =
      {
        bad_ip: ip,
        ban_length: length,
        msg: msg,
        api_key: Paraxial::Helpers.get_api_key
      }
    r = Net::HTTP.post(uri, body.to_json, @headers)
    if r.code == '200'
      :ok
    else
      :error
    end
  end
end

.blacklist_ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
197
198
199
200
# File 'lib/paraxial/checker.rb', line 194

def self.blacklist_ip?(ip)
  if ip.include?('.')
    !@bans['v4'].search_best(ip).nil? # v4 on allow list
  else
    !@bans['v6'].search_best(ip).nil? # v6 on allow list
  end
end

.create_patricia(list, type) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/paraxial/checker.rb', line 115

def self.create_patricia(list, type)
  if type == 'v4'
    p = Patricia.new
    list.each do |ip|
      p.add(ip)
    end
    p
  elsif type == 'v6'
    p = Patricia.new(:AF_INET6)
    list.each do |ip|
      p.add(ip)
    end
    p
  else
    raise 'Wrong type in Paraxial::Checker.create_patricia'
  end
end

.flush_bufferObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/paraxial/checker.rb', line 25

def self.flush_buffer
  @mutex.synchronize do
    requests = []
    until @buffer.empty?
      requests << @buffer.pop(true) rescue nil
    end

    send_async_request(requests) unless requests.empty?
  end
end

.get_abrObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/paraxial/checker.rb', line 56

def self.get_abr
  uri = URI.parse(Paraxial::Helpers.get_abr_url)

  body = { api_key: Paraxial::Helpers.get_api_key }
  begin
    r = Net::HTTP.post(uri, body.to_json, @headers)
    if r.code == '200'
      put_abr(JSON.parse(r.body))
    else
      'ab_failed'
    end
  rescue StandardError => e
    puts '[Paraxial] HTTP connection to backend failed, check configuration'
    'ab_failed'
  end
end

.honeypot_ban(ip, length) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/paraxial/checker.rb', line 158

def self.honeypot_ban(ip, length)
  local_ban(ip)

  uri = URI.parse(Paraxial::Helpers.get_honeypot_url)

  body = { api_key: Paraxial::Helpers.get_api_key, bad_ip: ip, ban_length: length }
  r = Net::HTTP.post(uri, body.to_json, @headers)
  if r.code == '200'
    :ok
  else
    :error
  end
end

.local_ban(ip) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/paraxial/checker.rb', line 172

def self.local_ban(ip)
  if ip.include?('.')
    # IPv4
    current_t = @bans['v4']
    current_t.add(ip)
    @bans['v4'] = current_t
  else
    # IPv6
    current_t = @bans['v6']
    current_t.add(ip)
    @bans['v6'] = current_t
  end
end

.put(allow_ban) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/paraxial/checker.rb', line 107

def self.put(allow_ban)
  allows = allow_ban['allows']
  bans = allow_ban['bans']
  @allows = { 'v4' => create_patricia(allows['v4'], 'v4'), 'v6' => create_patricia(allows['v6'], 'v6') }
  @bans = { 'v4' => create_patricia(bans['v4'], 'v4'), 'v6' => create_patricia(bans['v6'], 'v6') }
  :ok
end

.put_abr(abr) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/paraxial/checker.rb', line 73

def self.put_abr(abr)
  # Expected input: a hash
  # {"allows"=>[{"address"=>[96, 56, 162, 210], "netmask"=>32}],
  # "bans"=>
  # [{"address"=>[8193, 3512, 34211, 0, 0, 35374, 880, 29492], "netmask"=>128},
  # {"address"=>[111, 56, 162, 210], "netmask"=>32}],
  # "rules"=>[]}
  ipv4_a = []
  ipv4_b = []
  ipv6_a = []
  ipv6_b = []
  abr.each do |key, value|
    next if key == 'rules' # skip rules for now

    value.each do |ip|
      address = ip['address']
      if address.length == 4
        if key == 'allows'
          ipv4_a << address.join('.')
        elsif key == 'bans'
          ipv4_b << address.join('.')
        end
      elsif key == 'allows'
        ipv6_a << address.map { |n| n.to_s(16).rjust(4, '0') }.join(':')
      elsif key == 'bans'
        ipv6_b << address.map { |n| n.to_s(16).rjust(4, '0') }.join(':')
      end
    end
  end

  ab = { 'allows' => { 'v4' => ipv4_a, 'v6' => ipv6_a }, 'bans' => { 'v4' => ipv4_b, 'v6' => ipv6_b } }
  Paraxial::Checker.put(ab)
end

.req_to_buff(req_hash) ⇒ Object



21
22
23
# File 'lib/paraxial/checker.rb', line 21

def self.req_to_buff(req_hash)
  @buffer << req_hash
end

.send_async_request(requests) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/paraxial/checker.rb', line 36

def self.send_async_request(requests)
  body =
    {
      http_requests: requests,
      private_api_key: Paraxial::Helpers.get_api_key
    }

  # Do not send HTTP events when free tier is set to true
  ft = Paraxial::FreeTier.is_free_tier
  if ft
    puts "[Paraxial] HTTP ingest not supported on free tier"
  else
    Thread.new do
      uri = URI.parse(Paraxial::Helpers.get_ingest_url)
      Net::HTTP.post(uri, body.to_json, @headers)
    end
  end
end

.whitelist_ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
189
190
191
192
# File 'lib/paraxial/checker.rb', line 186

def self.whitelist_ip?(ip)
  if ip.include?('.')
    !@allows['v4'].search_best(ip).nil? # v4 on allow list
  else
    !@allows['v6'].search_best(ip).nil? # v6 on allow list
  end
end