wafris 0.6.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 283b178e632194aa6ff2e0aaaa5e3eca428cacb17a6383a8aa3b57d005c5a316
4
- data.tar.gz: 9ce784b1e57629d072ccc9da4c9e8ec81916dbdcf03c3138e004777e498db4d4
3
+ metadata.gz: bc80b768ee486f172f3eb2d617fde7622bff15fca6078819e89ddbae2b112f97
4
+ data.tar.gz: 6a2528038d9ca8d278ce2a599051e74803635626ee35a67f224bc531d9fa1d7d
5
5
  SHA512:
6
- metadata.gz: 32df667c3fa70c28fb2ea9b298c05ca2542f38d2c6ecee45208e1de6df518c33e6252cce179f2d9a65759fd60093c06909b3aef65068f926405daa07722d6ee8
7
- data.tar.gz: 46722bd780d683360be5382f7b9729067cc1624e12d602caba624629ebb9b88e7e454e23e3231aef98a63087d2824fded586088cbf0d5a6ceff4203fbe0c0385
6
+ metadata.gz: a0401a246649e42b5b2f801dae3ae5b362aca9d42446c45e93f3aa8e1c32e23c63b0e065cc8ae6e5f9131be420090d80d72fa5f1d0e85e9cdb9073036657db3b
7
+ data.tar.gz: 2bf8cf0a8b1a0d13e0aef0c8bd08d869c619b3655b68a4de6a631b60f620212d171be294c9edfcb500bdfb1a856dc3e1fe578815e8d2b0ba6aa1ab9a57fb6324
@@ -1,4 +1,7 @@
1
- local function get_time_bucket_from_timestamp(unix_time_milliseconds)
1
+ local version = "v0.8:"
2
+ local wafris_prefix = "w:" .. version
3
+
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
4
7
  end
@@ -45,7 +48,12 @@ local function get_time_bucket_from_timestamp(unix_time_milliseconds)
45
48
  local hours = math.floor(unix_time / 3600 % 24)
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
- return string.format("%04d-%02d-%02d-%02d", year, month, days, hours)
51
+ if minutes_flag == false then
52
+ return string.format("%04d%02d%02d%02d", year, month, days, hours)
53
+ elseif minutes_flag == true then
54
+ local minutes = math.floor(unix_time / 60 % 60)
55
+ return string.format("%04d%02d%02d%02d%02d", year, month, days, hours, minutes)
56
+ end
49
57
  end
50
58
 
51
59
  -- For: Relationship of IP to time of Request (Stream)
@@ -55,15 +63,30 @@ local function get_request_id(timestamp, ip, max_requests)
55
63
  return request_id
56
64
  end
57
65
 
58
- local function add_to_HLL_request_count(timebucket, request_id)
59
- 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, "NX")
60
71
  end
61
72
 
62
73
  -- For: Leaderboard of IPs with Request count as score
63
74
  local function increment_timebucket_for(type, timebucket, property)
64
- -- TODO: breaking change will to switch to client_ip: prefix
65
- type = type or "ip-"
66
- 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, "NX")
79
+ end
80
+
81
+ local function increment_partial_hourly_request_counters(unix_time_milliseconds)
82
+ for i = 1, 60 do
83
+ local timebucket_in_milliseconds = unix_time_milliseconds + 60000 * (i - 1)
84
+ local timebucket = get_time_bucket_from_timestamp(timebucket_in_milliseconds, true)
85
+ local key = wafris_prefix .. "hr-ct:" .. timebucket
86
+ redis.call("INCR", key)
87
+ -- Expire the key after 61 minutes if it has no expiry
88
+ redis.call("EXPIRE", key, 3660, "NX")
89
+ end
67
90
  end
68
91
 
69
92
  -- Configuration
@@ -80,16 +103,18 @@ local host = ARGV[6]
80
103
 
81
104
  -- Initialize local variables
82
105
  local request_id = get_request_id(nil, client_ip, max_requests)
83
- local current_timebucket = get_time_bucket_from_timestamp(unix_time_milliseconds)
106
+ local current_timebucket = get_time_bucket_from_timestamp(unix_time_milliseconds, false)
107
+
108
+ -- CARD DATA COLLECTION
109
+ increment_partial_hourly_request_counters(unix_time_milliseconds)
84
110
 
85
111
  -- GRAPH DATA COLLECTION
86
- add_to_HLL_request_count(current_timebucket, request_id)
112
+ add_to_graph_timebucket(current_timebucket, request_id)
87
113
 
88
114
  -- LEADERBOARD DATA COLLECTION
89
- -- TODO: breaking change will to switch to client_ip: prefix
90
- increment_timebucket_for(nil, current_timebucket, client_ip)
91
- increment_timebucket_for("user_agent:", current_timebucket, user_agent)
92
- 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)
93
118
  increment_timebucket_for("host:", current_timebucket, host)
94
119
 
95
120
  redis.call("ZRANGEBYSCORE", "blocked_ranges", client_ip_to_decimal, client_ip_to_decimal, "LIMIT", 0, 1)
@@ -11,6 +11,7 @@ module Wafris
11
11
  ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }
12
12
  )
13
13
  @redis_pool_size = 20
14
+ set_version
14
15
  end
15
16
 
16
17
  def connection_pool
@@ -31,6 +32,15 @@ module Wafris
31
32
  CONNECTION_ERROR
32
33
  end
33
34
 
35
+ def set_version
36
+ version_line = File.open(
37
+ file_path("wafris_core"),
38
+ &:readline
39
+ )
40
+ version = version_line.slice(/v\d.\d/)
41
+ redis.set('version', version)
42
+ end
43
+
34
44
  def core_sha
35
45
  @core_sha ||= redis.script(:load, wafris_core)
36
46
  end
@@ -43,10 +53,14 @@ module Wafris
43
53
 
44
54
  def read_lua_dist(filename)
45
55
  File.read(
46
- File.join(
47
- File.dirname(__FILE__),
48
- "../lua/dist/#{filename}.lua"
49
- )
56
+ file_path(filename)
57
+ )
58
+ end
59
+
60
+ def file_path(filename)
61
+ File.join(
62
+ File.dirname(__FILE__),
63
+ "../lua/dist/#{filename}.lua"
50
64
  )
51
65
  end
52
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wafris
4
- VERSION = "0.6.0"
4
+ VERSION = "0.8.0"
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.6.0
4
+ version: 0.8.0
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