aboutsummaryrefslogtreecommitdiffstats
path: root/lualib/rspamadm/ratelimit.lua
blob: 1dd42472faed939b00b67ce854d127495e30c67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
75
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
local argparse = require 'argparse'
local redis = require 'lua_redis'
local logger = require 'rspamd_logger'

local parser = argparse()
    :name "ratelimit"
    :description "Manage ratelimit functional"
    :help_description_margin(32)
    :command_target('command')
    :require_command(true)
parser:option "-c --config"
      :description "Path to config file"
      :argname("<cfg>")
      :default(rspamd_paths["CONFDIR"] .. "/" .. "rspamd.conf")

local track_limits = parser:command 'track'
                           :description 'Track last limits of ratelimit module'

track_limits:option "-q --quantity"
            :description("Number of limits to track")
            :argname("<quantity>")
            :default(1)

local upgrade_bucket = parser:command 'upgrade'
                             :description 'Upgrade certain bucket or top limit bucket'
upgrade_bucket:argument "prefix"
              :description("Prefix of bucket to operate with")
              :args("?")
upgrade_bucket:option "-b --burst"
              :description("Burst to set")
              :argname("<burst>")
              :args("?")
upgrade_bucket:option "-r --rate"
              :description("Rate to set")
              :argname("<rate>")
              :args("?")
upgrade_bucket:option "-s --symbol"
              :description("Symbol to set")
              :argname("<symbol>")
              :args("?")
upgrade_bucket:option "-B --dynamic_burst"
              :description("Dynamic burst to set")
              :argname("<dynb>")
              :args("?")
upgrade_bucket:option "-R --dynamic_rate"
              :description("Dynamic rate to set")
              :argname("<dynr>")
              :args("?")

local unblock_bucket = parser:command 'unblock'
                             :description 'Unblock certain bucket or number of buckets(default: 1)'

parser:mutex(
    unblock_bucket:argument "prefix"
                  :description("Prefix of bucket to operate with")
                  :args(1),
    unblock_bucket:argument "quantity"
                  :description("Number of buckets to ublock")
                  :default(1)
)

local redis_params
local lfb_cache_prefix = 'RL_cache_prefix'
local redis_attrs = {
  config = rspamd_config,
  ev_base = rspamadm_ev_base,
  session = rspamadm_session,
  log_obj = rspamd_config,
  resolver = rspamadm_dns_resolver,
}

local function track_limits_handler(args)
  for i = 1, args.quantity do
    local res, prefix = redis.request(redis_params, redis_attrs,
        { 'ZRANGE', lfb_cache_prefix, -i, -i })
    if not res then
      logger.errx('Redis request error: %s', prefix)
      os.exit(1)
    end

    if #prefix == 1 then
      local _, bucket_params = redis.request(redis_params, redis_attrs,
          { 'HMGET', tostring(prefix[1]), 'l', 'b', 'r', 'dr', 'db' })

      local last = tonumber(bucket_params[1])
      local burst = tonumber(bucket_params[2])
      local rate = tonumber(bucket_params[3])
      local dynr = tonumber(bucket_params[4]) / 10000.0
      local dynb = tonumber(bucket_params[5]) / 10000.0

      print(string.format('prefix: %s, last: %s, burst: %s, rate: %s, dynamic_rate: %s, dynamic_burst: %s',
          prefix[1], last, burst, rate, dynr, dynb))
    end
  end
end

local function upgrade_bucket_handler(args)
  local prefix = args.prefix
  if prefix == nil or prefix == "" then
    local res = nil
    res, prefix = redis.request(redis_params, redis_attrs,
        { 'ZRANGE', lfb_cache_prefix, '-1', '-1' })
    if res ~= 1 then
      logger.errx('Redis request parameters are wrong')
      os.exit(1)
    end
  end

  if args.burst then
    local res, err = redis.request(redis_params, redis_attrs,
        { 'HSET', tostring(prefix), 'b', tostring(args.burst) })
    if not res then
      logger.errx('Incorrect arguments for redis request for burst or prefix is incorrect: %s', err)
      os.exit(1)
    end
  end

  if args.rate then
    local res, err = redis.request(redis_params, redis_attrs,
        { 'HSET', tostring(prefix), 'r', tostring(args.rate) })
    if not res then
      logger.errx('Incorrect arguments for redis request for rate or prefix is incorrect: %s', err)
      os.exit(1)
    end
  end

  if args.symbol then
    local res, err = redis.request(redis_params, redis_attrs,
        { 'HSET', tostring(prefix), 's', tostring(args.symbol) })
    if not res then
      logger.errx('Incorrect arguments for redis request for symbol or prefix is incorrect: %s', err)
      os.exit(1)
    end
  end

  if args.dynb then
    local res, err = redis.request(redis_params, redis_attrs,
        { 'HSET', tostring(prefix), 'db', tostring(args.dynb) })
    if not res then
      logger.errx('Incorrect arguments for redis request for dynamic burst or prefix is incorrect: %s', err)
      os.exit(1)
    end
  end

  if args.dynr then
    local res, err = redis.request(redis_params, redis_attrs,
        { 'HSET', tostring(prefix), 'dr', tostring(args.dynr) })
    if not res then
      logger.errx('Incorrect arguments for redis request for dynamic rate or prefix is incorrect: %s', err)
      os.exit(1)
    end
  end

end

local function unblock_bucket_helper(prefix)
  local res, err = redis.request(redis_params, redis_attrs, { 'HSET', tostring(prefix), 'b', 0 })
  if not res then
    logger.errx('Failed to unblock bucket: %s', err)
    os.exit(1)
  end
end

local function unblock_buckets_handler(args)
  for i = 1, args.quantity do
    local res, prefix = redis.request(redis_params, redis_attrs,
        { 'ZRANGE', lfb_cache_prefix, -i, -i })
    if not res then
      logger.errx('Redis request parameters are wrong: %s', prefix)
      os.exit(1)
    end
    unblock_bucket_helper(prefix)
  end
end

local function unblock_bucket_handler(args)
  if (args.prefix == nil or args.prefix == "") then
    unblock_buckets_handler(args)
  end
  unblock_bucket_helper(args.prefix)
end

local command_handlers = {
  track = track_limits_handler,
  upgrade = upgrade_bucket_handler,
  unblock = unblock_bucket_handler
}

local function handler(args)
  local function load_config(opts)
    local _r, err = rspamd_config:load_ucl(opts['config'])

    if not _r then
      logger.errx('cannot parse %s: %s', opts['config'], err)
      os.exit(1)
    end

    _r, err = rspamd_config:parse_rcl({ 'logging', 'worker' })
    if not _r then
      logger.errx('cannot process %s: %s', opts['config'], err)
      os.exit(1)
    end
  end
  local cmd_opts = parser:parse(args)
  load_config(cmd_opts)

  redis_params = redis.parse_redis_server('ratelimit')
  if not redis_params then
    logger.errx(rspamd_config, 'no servers are specified, cannot work with rate limits')
    os.exit(1)
  end

  local f = command_handlers[cmd_opts.command]
  if not f then
    parser:error(string.format("command isn't implemented: %s",
        cmd_opts.command))
  end
  f(cmd_opts)
end

return {
  name = 'ratelimit',
  aliases = { 'ratelimit' },
  handler = handler,
  description = parser._description
}