Class: Relintio::Agent

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

Constant Summary collapse

AGENT_VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Agent

Returns a new instance of Agent.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/relintio-agent.rb', line 12

def initialize(config = {})
  @config = {
    license_key: config[:license_key],
    api_url: config[:api_url] || "https://relintio.com/api",
    domain: config[:domain] || '',
    sync_interval: config[:sync_interval] || 60
  }
  @rules = []
  @rules_mutex = Mutex.new
  @running = true
  @telemetry_queue = SizedQueue.new(1_000)
  @telemetry_thread = Thread.new { telemetry_loop }
  @telemetry_thread.report_on_exception = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/relintio-agent.rb', line 10

def config
  @config
end

#rulesObject (readonly)

Returns the value of attribute rules.



10
11
12
# File 'lib/relintio-agent.rb', line 10

def rules
  @rules
end

Instance Method Details

#check_request(request) ⇒ Object



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
106
107
108
109
110
111
112
113
114
# File 'lib/relintio-agent.rb', line 76

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/relintio-agent.rb', line 116

def send_telemetry(request, result)
  return unless @running

  @telemetry_queue.push({
    license_key: @config[:license_key],
    ip: request.ip,
    user_agent: request.user_agent || '',
    path: request.path_info,
    risk_score: [[result[:score].to_i, 0].max, 100].min,
    action: result[:action].to_s.upcase,
    reason_code: 'sdk_rule',
    protocol_version: 1,
    agent_kind: 'ruby',
    agent_version: AGENT_VERSION
  }, true)
rescue ThreadError
  nil
end

#start_syncObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/relintio-agent.rb', line 27

def start_sync
  return @sync_thread if @sync_thread&.alive?

  @sync_thread = Thread.new do
    while @running
      sync_rules
      sleep @config[:sync_interval]
    end
  end
  @sync_thread.report_on_exception = false
  @sync_thread
end

#stopObject



40
41
42
43
44
45
46
# File 'lib/relintio-agent.rb', line 40

def stop
  @running = false
  @sync_thread&.wakeup if @sync_thread&.alive?
  @telemetry_queue << nil
  @sync_thread&.join(2)
  @telemetry_thread&.join(2)
end

#sync_rulesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/relintio-agent.rb', line 48

def sync_rules
  begin
    uri = URI.parse("#{@config[:api_url].chomp('/')}/agent/verify")
    req = Net::HTTP::Post.new(uri)
    req['Content-Type'] = 'application/json'
    req.body = {
      license_key: @config[:license_key],
      domain: @config[:domain],
      protocol_version: 1,
      agent_kind: 'ruby',
      agent_version: AGENT_VERSION,
      capabilities: %w[custom_rules telemetry]
    }.to_json

    res = http_client(uri).request(req)
    if res.code == '200'
      return if res.body.bytesize > 1_048_576

      data = JSON.parse(res.body)
      @rules_mutex.synchronize do
        @rules = data['rules'] || []
      end
    end
  rescue StandardError
    # Fail-open: ignore errors during sync
  end
end