wafris 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cbb8c27a7af9771ecf6c07a34fd7da75f4ebacf4fd21dba0b37551a8b2ca2cd
4
- data.tar.gz: 492a0f193e4c976a612106b5a0bb8c25aa008f170ec8111fa9504bd1cf585faf
3
+ metadata.gz: 8a2722b928a9589d9a25292b4116200e428fabfbbc7fa7d15e2eaada1a42927a
4
+ data.tar.gz: c3b941f3ed47136b92731e62df8dbb9d47c29d0817bf85e5b974bcd7b01c9ca8
5
5
  SHA512:
6
- metadata.gz: b50f3ad94a373c55285d5e800e55a483f7311a45d18c80d42464931f265edae1bc36a39f97ecf803a99065e5f12b106cc1739cd3716b3219f3a1a60f31b1a6c5
7
- data.tar.gz: d23d85b065a338c454216d37755ec6d112d4392f9415e7e13df30c4610c96ab58a15a046211332a273cee3da12bdeae2fc3c4f09722239e5cd3b414fa09d4351
6
+ metadata.gz: 8e52e73b5d385e3a81ee4f5845f50dc00cd52683b595db3e5cb4f47ca7e573d49343525b8c2d6de423f9f189752b82f47acb71240d298b509f1da9b72023313e
7
+ data.tar.gz: b8cb1ccb52d9edc06756f78b277ff4ca2c2c39c94bea68e1e913b74dd620cf8033342759b232cfa086e6cf55f683729258cbb44488eca08f4d821ee751ab1929
@@ -1,3 +1,6 @@
1
+ local version = "v0.8:"
2
+ local wafris_prefix = "w:" .. version
3
+
1
4
  local function get_time_bucket_from_timestamp(unix_time_milliseconds, minutes_flag)
2
5
  local function calculate_years_number_of_days(yr)
3
6
  return (yr % 4 == 0 and (yr % 100 ~= 0 or yr % 400 == 0)) and 366 or 365
@@ -46,10 +49,10 @@ local function get_time_bucket_from_timestamp(unix_time_milliseconds, minutes_fl
46
49
  -- local minutes, seconds = math.floor(unix_time / 60 % 60), math.floor(unix_time % 60)
47
50
  -- hours = hours > 12 and hours - 12 or hours == 0 and 12 or hours
48
51
  if minutes_flag == false then
49
- return string.format("%04d-%02d-%02d-%02d", year, month, days, hours)
52
+ return string.format("%04d%02d%02d%02d", year, month, days, hours)
50
53
  elseif minutes_flag == true then
51
54
  local minutes = math.floor(unix_time / 60 % 60)
52
- return string.format("%04d-%02d-%02d-%02d-%02d", year, month, days, hours, minutes)
55
+ return string.format("%04d%02d%02d%02d%02d", year, month, days, hours, minutes)
53
56
  end
54
57
  end
55
58
 
@@ -60,25 +63,29 @@ local function get_request_id(timestamp, ip, max_requests)
60
63
  return request_id
61
64
  end
62
65
 
63
- local function add_to_HLL_request_count(timebucket, request_id)
64
- redis.call("PFADD", "unique-requests:" .. timebucket, request_id)
66
+ local function add_to_graph_timebucket(timebucket, request_id)
67
+ local key = wafris_prefix .. "gr-ct:"
68
+ redis.call("PFADD", key .. timebucket, request_id)
69
+ -- Expire the key after 25 hours if it has no expiry
70
+ redis.call("EXPIRE", key, 90000)
65
71
  end
66
72
 
67
73
  -- For: Leaderboard of IPs with Request count as score
68
74
  local function increment_timebucket_for(type, timebucket, property)
69
- -- TODO: breaking change will to switch to client_ip: prefix
70
- type = type or "ip-"
71
- redis.call("ZINCRBY", type .. "leader-sset:" .. timebucket, 1, property)
75
+ local key = wafris_prefix .. type .. "lb:" .. timebucket
76
+ redis.call("ZINCRBY", key, 1, property)
77
+ -- Expire the key after 25 hours if it has no expiry
78
+ redis.call("EXPIRE", key, 90000)
72
79
  end
73
80
 
74
- local function increment_hourly_request_counters(unix_time_milliseconds)
81
+ local function increment_partial_hourly_request_counters(unix_time_milliseconds)
75
82
  for i = 1, 60 do
76
83
  local timebucket_in_milliseconds = unix_time_milliseconds + 60000 * (i - 1)
77
84
  local timebucket = get_time_bucket_from_timestamp(timebucket_in_milliseconds, true)
78
- local key = "w:v0:hr-ct:" .. timebucket
85
+ local key = wafris_prefix .. "hr-ct:" .. timebucket
79
86
  redis.call("INCR", key)
80
87
  -- Expire the key after 61 minutes if it has no expiry
81
- redis.call("EXPIRE", key, 3660, "NX")
88
+ redis.call("EXPIRE", key, 3660)
82
89
  end
83
90
  end
84
91
 
@@ -99,16 +106,15 @@ local request_id = get_request_id(nil, client_ip, max_requests)
99
106
  local current_timebucket = get_time_bucket_from_timestamp(unix_time_milliseconds, false)
100
107
 
101
108
  -- CARD DATA COLLECTION
102
- increment_hourly_request_counters(unix_time_milliseconds)
109
+ increment_partial_hourly_request_counters(unix_time_milliseconds)
103
110
 
104
111
  -- GRAPH DATA COLLECTION
105
- add_to_HLL_request_count(current_timebucket, request_id)
112
+ add_to_graph_timebucket(current_timebucket, request_id)
106
113
 
107
114
  -- LEADERBOARD DATA COLLECTION
108
- -- TODO: breaking change will to switch to client_ip: prefix
109
- increment_timebucket_for(nil, current_timebucket, client_ip)
110
- increment_timebucket_for("user_agent:", current_timebucket, user_agent)
111
- increment_timebucket_for("request_path:", current_timebucket, request_path)
115
+ increment_timebucket_for("ip:", current_timebucket, client_ip)
116
+ increment_timebucket_for("ua:", current_timebucket, user_agent)
117
+ increment_timebucket_for("path:", current_timebucket, request_path)
112
118
  increment_timebucket_for("host:", current_timebucket, host)
113
119
 
114
120
  redis.call("ZRANGEBYSCORE", "blocked_ranges", client_ip_to_decimal, client_ip_to_decimal, "LIMIT", 0, 1)
@@ -6,11 +6,20 @@ module Wafris
6
6
  attr_accessor :redis_pool_size
7
7
 
8
8
  def initialize
9
- @redis = Redis.new(
10
- url: ENV['REDIS_URL'],
11
- ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }
12
- )
9
+ @redis = set_redis
13
10
  @redis_pool_size = 20
11
+ set_version
12
+ end
13
+
14
+ def set_redis
15
+ if ENV['REDIS_URL']
16
+ Redis.new(
17
+ url: ENV['REDIS_URL'],
18
+ ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }
19
+ )
20
+ else
21
+ Redis.new
22
+ end
14
23
  end
15
24
 
16
25
  def connection_pool
@@ -31,6 +40,15 @@ module Wafris
31
40
  CONNECTION_ERROR
32
41
  end
33
42
 
43
+ def set_version
44
+ version_line = File.open(
45
+ file_path("wafris_core"),
46
+ &:readline
47
+ )
48
+ version = version_line.slice(/v\d.\d/)
49
+ redis.set('version', version)
50
+ end
51
+
34
52
  def core_sha
35
53
  @core_sha ||= redis.script(:load, wafris_core)
36
54
  end
@@ -43,10 +61,14 @@ module Wafris
43
61
 
44
62
  def read_lua_dist(filename)
45
63
  File.read(
46
- File.join(
47
- File.dirname(__FILE__),
48
- "../lua/dist/#{filename}.lua"
49
- )
64
+ file_path(filename)
65
+ )
66
+ end
67
+
68
+ def file_path(filename)
69
+ File.join(
70
+ File.dirname(__FILE__),
71
+ "../lua/dist/#{filename}.lua"
50
72
  )
51
73
  end
52
74
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wafris
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wafris
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micahel Buckbee
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-06-20 00:00:00.000000000 Z
12
+ date: 2023-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: connection_pool