Class: Relintio::Agent
- Inherits:
-
Object
- Object
- Relintio::Agent
- Defined in:
- lib/relintio-agent.rb
Constant Summary collapse
- AGENT_VERSION =
'0.1.2'
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #check_request(request) ⇒ Object
-
#initialize(config = {}) ⇒ Agent
constructor
A new instance of Agent.
- #send_telemetry(request, result) ⇒ Object
- #start_sync ⇒ Object
- #stop ⇒ Object
- #sync_rules ⇒ Object
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://api.relintio.com/v1", domain: config[:domain] || '', sync_interval: [config[:sync_interval] || 10, 10].max } @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
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/relintio-agent.rb', line 10 def config @config end |
#rules ⇒ Object (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
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 115 116 117 118 119 |
# File 'lib/relintio-agent.rb', line 81 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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/relintio-agent.rb', line 121 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_sync ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/relintio-agent.rb', line 27 def start_sync return @sync_thread if @sync_thread&.alive? @sync_thread = Thread.new do failures = 0 while @running success = sync_rules failures = success ? 0 : [failures + 1, 5].min base = [@config[:sync_interval] * (2**failures), 300].min sleep [base * rand(0.8..1.2), 10].max end end @sync_thread.report_on_exception = false @sync_thread end |
#stop ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/relintio-agent.rb', line 43 def stop @running = false @sync_thread&.wakeup if @sync_thread&.alive? @telemetry_queue << nil @sync_thread&.join(2) @telemetry_thread&.join(2) end |
#sync_rules ⇒ Object
51 52 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 |
# File 'lib/relintio-agent.rb', line 51 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 return true end rescue StandardError # Fail-open: ignore errors during sync end false end |