Class: Relintio::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/relintio-agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Agent

Returns a new instance of Agent.



10
11
12
13
14
15
16
17
18
19
# File 'lib/relintio-agent.rb', line 10

def initialize(config = {})
  @config = {
    license_key: config[:license_key],
    api_url: config[:api_url] || "https://api.relintio.com/api",
    sync_interval: config[:sync_interval] || 60
  }
  @rules = []
  @rules_mutex = Mutex.new
  @client = Net::HTTP
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/relintio-agent.rb', line 8

def config
  @config
end

#rulesObject (readonly)

Returns the value of attribute rules.



8
9
10
# File 'lib/relintio-agent.rb', line 8

def rules
  @rules
end

Instance Method Details

#check_request(request) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/relintio-agent.rb', line 53

def check_request(request)
  @rules_mutex.synchronize do
    ip = request.ip
    user_agent = request.user_agent || ""
    path = request.path_info

    score = 0
    action = "allow"

    @rules.each do |rule|
      matched = false
      case rule['type']
      when 'ip'
        matched = match_value(ip, rule['pattern'], rule['condition'])
      when 'user_agent'
        matched = match_value(user_agent, rule['pattern'], rule['condition'])
      when 'path'
        matched = match_value(path, rule['pattern'], rule['condition'])
      end

      if matched
        score += (rule['score'] || 0)
        if rule['action'] == "block"
          action = "block"
        elsif rule['action'] == "challenge" && action != "block"
          action = "challenge"
        end
      end
    end

    if score >= 100
      action = "block"
    elsif score >= 50 && action != "block"
      action = "challenge"
    end

    { score: score, action: action }
  end
end

#send_telemetry(request, result) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/relintio-agent.rb', line 93

def send_telemetry(request, result)
  Thread.new do
    begin
      uri = URI.parse("#{@config[:api_url].chomp('/')}/telemetry/log")
      req = Net::HTTP::Post.new(uri)
      req['Authorization'] = "Bearer #{@config[:license_key]}"
      req['Content-Type'] = 'application/json'

      payload = {
        ip: request.ip,
        user_agent: request.user_agent || "",
        path: request.path_info,
        score: result[:score],
        action: result[:action],
        timestamp: Time.now.to_i
      }

      req.body = payload.to_json

      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = (uri.scheme == 'https')
      http.read_timeout = 10
      http.request(req)
    rescue
      # Fail-open
    end
  end
end

#start_syncObject



21
22
23
24
25
26
27
28
# File 'lib/relintio-agent.rb', line 21

def start_sync
  Thread.new do
    loop do
      sync_rules
      sleep @config[:sync_interval]
    end
  end
end

#sync_rulesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/relintio-agent.rb', line 30

def sync_rules
  begin
    uri = URI.parse("#{@config[:api_url].chomp('/')}/rules/sync")
    req = Net::HTTP::Get.new(uri)
    req['Authorization'] = "Bearer #{@config[:license_key]}"
    req['Content-Type'] = 'application/json'

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (uri.scheme == 'https')
    http.read_timeout = 10

    res = http.request(req)
    if res.code == '200'
      data = JSON.parse(res.body)
      @rules_mutex.synchronize do
        @rules = data['rules'] || []
      end
    end
  rescue => e
    # Fail-open: ignore errors during sync
  end
end