| 1 |
local argparse = require 'argparse' |
| 2 |
local redis = require 'lua_redis' |
| 3 |
local logger = require 'rspamd_logger' |
| 4 |
|
| 5 |
local parser = argparse() |
| 6 |
:name "ratelimit" |
| 7 |
:description "Manage ratelimit functional" |
| 8 |
:help_description_margin(32) |
| 9 |
:command_target('command') |
| 10 |
:require_command(true) |
| 11 |
parser:option "-c --config" |
| 12 |
:description "Path to config file" |
| 13 |
:argname("<cfg>") |
| 14 |
:default(rspamd_paths["CONFDIR"] .. "/" .. "rspamd.conf") |
| 15 |
|
| 16 |
local track_limits = parser:command 'track' |
| 17 |
:description 'Track last limits of ratelimit module' |
| 18 |
|
| 19 |
track_limits:option "-q --quantity" |
| 20 |
:description("Number of limits to track") |
| 21 |
:argname("<quantity>") |
| 22 |
:default(1) |
| 23 |
|
| 24 |
local upgrade_bucket = parser:command 'upgrade' |
| 25 |
:description 'Upgrade certain bucket or top limit bucket' |
| 26 |
upgrade_bucket:argument "prefix" |
| 27 |
:description("Prefix of bucket to operate with") |
| 28 |
:args("?") |
| 29 |
upgrade_bucket:option "-b --burst" |
| 30 |
:description("Burst to set") |
| 31 |
:argname("<burst>") |
| 32 |
:args("?") |
| 33 |
upgrade_bucket:option "-r --rate" |
| 34 |
:description("Rate to set") |
| 35 |
:argname("<rate>") |
| 36 |
:args("?") |
| 37 |
upgrade_bucket:option "-s --symbol" |
| 38 |
:description("Symbol to set") |
| 39 |
:argname("<symbol>") |
| 40 |
:args("?") |
| 41 |
upgrade_bucket:option "-B --dynamic_burst" |
| 42 |
:description("Dynamic burst to set") |
| 43 |
:argname("<dynb>") |
| 44 |
:args("?") |
| 45 |
upgrade_bucket:option "-R --dynamic_rate" |
| 46 |
:description("Dynamic rate to set") |
| 47 |
:argname("<dynr>") |
| 48 |
:args("?") |
| 49 |
|
| 50 |
local unblock_bucket = parser:command 'unblock' |
| 51 |
:description 'Unblock certain bucket or number of buckets(default: 1)' |
| 52 |
|
| 53 |
parser:mutex( |
| 54 |
unblock_bucket:argument "prefix" |
| 55 |
:description("Prefix of bucket to operate with") |
| 56 |
:args(1), |
| 57 |
unblock_bucket:argument "quantity" |
| 58 |
:description("Number of buckets to ublock") |
| 59 |
:default(1) |
| 60 |
) |
| 61 |
|
| 62 |
local redis_params |
| 63 |
local lfb_cache_prefix = 'RL_cache_prefix' |
| 64 |
local redis_attrs = { |
| 65 |
config = rspamd_config, |
| 66 |
ev_base = rspamadm_ev_base, |
| 67 |
session = rspamadm_session, |
| 68 |
log_obj = rspamd_config, |
| 69 |
resolver = rspamadm_dns_resolver, |
| 70 |
} |
| 71 |
|
| 72 |
local function track_limits_handler(args) |
| 73 |
for i = 1, args.quantity do |
| 74 |
local res, prefix = redis.request(redis_params, redis_attrs, |
| 75 |
{ 'ZRANGE', lfb_cache_prefix, -i, -i }) |
| 76 |
if not res then |
| 77 |
logger.errx('Redis request error: %s', prefix) |
| 78 |
os.exit(1) |
| 79 |
end |
| 80 |
|
| 81 |
if #prefix == 1 then |
| 82 |
local _, bucket_params = redis.request(redis_params, redis_attrs, |
| 83 |
{ 'HMGET', tostring(prefix[1]), 'l', 'b', 'r', 'dr', 'db' }) |
| 84 |
|
| 85 |
local last = tonumber(bucket_params[1]) |
| 86 |
local burst = tonumber(bucket_params[2]) |
| 87 |
local rate = tonumber(bucket_params[3]) |
| 88 |
local dynr = tonumber(bucket_params[4]) / 10000.0 |
| 89 |
local dynb = tonumber(bucket_params[5]) / 10000.0 |
| 90 |
|
| 91 |
print(string.format('prefix: %s, last: %s, burst: %s, rate: %s, dynamic_rate: %s, dynamic_burst: %s', |
| 92 |
prefix[1], last, burst, rate, dynr, dynb)) |
| 93 |
end |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
local function upgrade_bucket_handler(args) |
| 98 |
local prefix = args.prefix |
| 99 |
if prefix == nil or prefix == "" then |
| 100 |
local res = nil |
| 101 |
res, prefix = redis.request(redis_params, redis_attrs, |
| 102 |
{ 'ZRANGE', lfb_cache_prefix, '-1', '-1' }) |
| 103 |
if res ~= 1 then |
| 104 |
logger.errx('Redis request parameters are wrong') |
| 105 |
os.exit(1) |
| 106 |
end |
| 107 |
end |
| 108 |
|
| 109 |
if args.burst then |
| 110 |
local res, err = redis.request(redis_params, redis_attrs, |
| 111 |
{ 'HSET', tostring(prefix), 'b', tostring(args.burst) }) |
| 112 |
if not res then |
| 113 |
logger.errx('Incorrect arguments for redis request for burst or prefix is incorrect: %s', err) |
| 114 |
os.exit(1) |
| 115 |
end |
| 116 |
end |
| 117 |
|
| 118 |
if args.rate then |
| 119 |
local res, err = redis.request(redis_params, redis_attrs, |
| 120 |
{ 'HSET', tostring(prefix), 'r', tostring(args.rate) }) |
| 121 |
if not res then |
| 122 |
logger.errx('Incorrect arguments for redis request for rate or prefix is incorrect: %s', err) |
| 123 |
os.exit(1) |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
if args.symbol then |
| 128 |
local res, err = redis.request(redis_params, redis_attrs, |
| 129 |
{ 'HSET', tostring(prefix), 's', tostring(args.symbol) }) |
| 130 |
if not res then |
| 131 |
logger.errx('Incorrect arguments for redis request for symbol or prefix is incorrect: %s', err) |
| 132 |
os.exit(1) |
| 133 |
end |
| 134 |
end |
| 135 |
|
| 136 |
if args.dynb then |
| 137 |
local res, err = redis.request(redis_params, redis_attrs, |
| 138 |
{ 'HSET', tostring(prefix), 'db', tostring(args.dynb) }) |
| 139 |
if not res then |
| 140 |
logger.errx('Incorrect arguments for redis request for dynamic burst or prefix is incorrect: %s', err) |
| 141 |
os.exit(1) |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
if args.dynr then |
| 146 |
local res, err = redis.request(redis_params, redis_attrs, |
| 147 |
{ 'HSET', tostring(prefix), 'dr', tostring(args.dynr) }) |
| 148 |
if not res then |
| 149 |
logger.errx('Incorrect arguments for redis request for dynamic rate or prefix is incorrect: %s', err) |
| 150 |
os.exit(1) |
| 151 |
end |
| 152 |
end |
| 153 |
|
| 154 |
end |
| 155 |
|
| 156 |
local function unblock_bucket_helper(prefix) |
| 157 |
local res, err = redis.request(redis_params, redis_attrs, { 'HSET', tostring(prefix), 'b', 0 }) |
| 158 |
if not res then |
| 159 |
logger.errx('Failed to unblock bucket: %s', err) |
| 160 |
os.exit(1) |
| 161 |
end |
| 162 |
end |
| 163 |
|
| 164 |
local function unblock_buckets_handler(args) |
| 165 |
for i = 1, args.quantity do |
| 166 |
local res, prefix = redis.request(redis_params, redis_attrs, |
| 167 |
{ 'ZRANGE', lfb_cache_prefix, -i, -i }) |
| 168 |
if not res then |
| 169 |
logger.errx('Redis request parameters are wrong: %s', prefix) |
| 170 |
os.exit(1) |
| 171 |
end |
| 172 |
unblock_bucket_helper(prefix) |
| 173 |
end |
| 174 |
end |
| 175 |
|
| 176 |
local function unblock_bucket_handler(args) |
| 177 |
if (args.prefix == nil or args.prefix == "") then |
| 178 |
unblock_buckets_handler(args) |
| 179 |
end |
| 180 |
unblock_bucket_helper(args.prefix) |
| 181 |
end |
| 182 |
|
| 183 |
local command_handlers = { |
| 184 |
track = track_limits_handler, |
| 185 |
upgrade = upgrade_bucket_handler, |
| 186 |
unblock = unblock_bucket_handler |
| 187 |
} |
| 188 |
|
| 189 |
local function handler(args) |
| 190 |
local function load_config(opts) |
| 191 |
local _r, err = rspamd_config:load_ucl(opts['config']) |
| 192 |
|
| 193 |
if not _r then |
| 194 |
logger.errx('cannot parse %s: %s', opts['config'], err) |
| 195 |
os.exit(1) |
| 196 |
end |
| 197 |
|
| 198 |
_r, err = rspamd_config:parse_rcl({ 'logging', 'worker' }) |
| 199 |
if not _r then |
| 200 |
logger.errx('cannot process %s: %s', opts['config'], err) |
| 201 |
os.exit(1) |
| 202 |
end |
| 203 |
end |
| 204 |
local cmd_opts = parser:parse(args) |
| 205 |
load_config(cmd_opts) |
| 206 |
|
| 207 |
redis_params = redis.parse_redis_server('ratelimit') |
| 208 |
if not redis_params then |
| 209 |
logger.errx(rspamd_config, 'no servers are specified, cannot work with rate limits') |
| 210 |
os.exit(1) |
| 211 |
end |
| 212 |
|
| 213 |
local f = command_handlers[cmd_opts.command] |
| 214 |
if not f then |
| 215 |
parser:error(string.format("command isn't implemented: %s", |
| 216 |
cmd_opts.command)) |
| 217 |
end |
| 218 |
f(cmd_opts) |
| 219 |
end |
| 220 |
|
| 221 |
return { |
| 222 |
name = 'ratelimit', |
| 223 |
aliases = { 'ratelimit' }, |
| 224 |
handler = handler, |
| 225 |
description = parser._description |
| 226 |
} |