local function asn_check(task)
local ip = task:get_from_ip()
-
+
local function asn_dns_cb(resolver, to_resolve, results, err, key)
if results and results[1] then
local parts = asn_re:split(results[1])
end
end
end
-
+
if ip and ip:is_valid() then
local asn_provider = 'asn_provider'
if ip:get_version() == 6 then
end
local req_name = rspamd_logger.slog("%1.%2",
table.concat(ip:inversed_str_octets(), '.'), options[asn_provider])
-
+
task:get_resolver():resolve_txt(task:get_session(), task:get_mempool(),
req_name, asn_dns_cb)
end
if pool:get_variable("ipnet") then
ipnet = pool:get_variable("ipnet")
end
-
+
return asn, country, ipnet
end
else
new_total = old_total + 1
end
-
+
return old_score + score, new_total
end
if not ip or not ip:is_valid() then
return
end
-
+
local pool = task:get_mempool()
local asn, country, ipnet = ip_score_get_task_vars(task)
-
+
if not pool:has_variable('ip_score') then
return
end
local hkey = ip_score_hash_key(asn, country, ipnet, ip)
local upstream = upstreams:get_upstream_by_hash(hkey)
local addr = upstream:get_addr()
-
+
asn_score,total_asn = new_score_set(score, asn_score, total_asn)
country_score,total_country = new_score_set(score, country_score, total_country)
ipnet_score,total_ipnet = new_score_set(score, ipnet_score, total_ipnet)
local parts = asn_re:split(score)
local rep = tonumber(parts[1])
local total = tonumber(parts[2])
-
+
return rep, total
end
-
+
local function normalize_score(sc, total, mult)
if total < options['lower_bound'] then
return 0
end
-
+
-- -mult to mult
return mult * rspamd_util.tanh(2.718 * sc / total)
end
-
+
if err then
-- Key is not found or error occurred
return
country_score,total_country,
ipnet_score,total_ipnet,
ip_score, total_ip)
-
+
asn_score = normalize_score(asn_score, total_asn, options['scores']['asn'])
country_score = normalize_score(country_score, total_country,
options['scores']['country'])
ipnet_score = normalize_score(ipnet_score, total_ipnet,
options['scores']['ipnet'])
ip_score = normalize_score(ip_score, total_ip, options['scores']['ip'])
-
+
local total_score = 0.0
local description_t = {}
if options['max_score'] and (total_score*10) > options['max_score'] then total_score = options['max_score']/10 end
if options['min_score'] and (total_score*10) < options['min_score'] then total_score = options['min_score']/10 end
-
+
if total_score ~= 0 then
task:insert_result(options['symbol'], total_score, table.concat(description_t, ', '))
end
end
end
-
+
local function create_get_command(ip, asn, country, ipnet)
local cmd = 'HMGET'
-
+
local args = {options['hash']}
-
+
if asn then
table.insert(args, options['asn_prefix'] .. asn)
else
-- fake arg
table.insert(args, options['ipnet_prefix'])
end
-
+
table.insert(args, ip:to_string())
-
+
return cmd, args
end
-
+
local ip = task:get_from_ip()
if ip:is_valid() then
-- Check IP whitelist
options[k] = v
end
if options['servers'] and options['servers'] ~= '' then
- upstreams = upstream_list.create(options['servers'], default_port)
+ upstreams = upstream_list.create(rspamd_config, options['servers'], default_port)
if not upstreams then
rspamd_logger.errx(rspamd_config, 'no servers are specified')
end
return {atime,bucket}
end
end
-
- return _.iter(data):map(function(e)
- if type(e) == 'string' then
+
+ return _.iter(data):map(function(e)
+ if type(e) == 'string' then
return parse_limit_elt(e)
else
return {0, 0}
if data then
local tv = task:get_timeval()
local ntime = tv['tv_usec'] / 1000000. + tv['tv_sec']
-
+
_.each(function(elt, limit)
local bucket = elt[2]
local rate = limit[2]
local threshold = limit[1]
local atime = elt[1]
-
+
bucket = bucket - rate * (ntime - atime);
if bucket > 0 then
if bucket > threshold then
upstream:fail()
end
end
-
+
if upstream then
local cmd = generate_format_string(args, false)
-
- rspamd_redis.make_request(task, addr, rate_get_cb, cmd,
+
+ rspamd_redis.make_request(task, addr, rate_get_cb, cmd,
_.totable(_.map(function(l) return l[2] end, args)))
end
end
upstream:ok()
end
end
-
+
local function rate_set_cb(task, err, data)
if data then
local tv = task:get_timeval()
local rate = limit[1][2]
local threshold = limit[1][1]
local atime = elt[1]
-
+
if bucket > 0 then
bucket = bucket - rate * (ntime - atime) + 1;
if bucket < 0 then
table.insert(values, limit[2])
table.insert(values, lstr)
end, _.zip(parse_limits(data), _.iter(args)))
-
+
local cmd = generate_format_string(values, true)
rspamd_redis.make_request(task, addr, rate_set_key_cb, cmd, values)
elseif err then
end
if upstream then
local cmd = generate_format_string(args, false)
-
+
rspamd_redis.make_request(task, addr, rate_set_cb, cmd,
_.totable(_.map(function(l) return l[2] end, args)))
end
local rcpts_user = {}
if rcpts then
_.each(function(r) table.insert(rcpts_user, r['user']) end, rcpts)
- if _.any(function(r)
- _.any(function(w) return r == w end, whitelisted_rcpts) end,
+ if _.any(function(r)
+ _.any(function(w) return r == w end, whitelisted_rcpts) end,
rcpts_user) then
-
+
rspamd_logger.infox(task, 'skip ratelimit for whitelisted recipient')
return
end
end
end, rcpts)
end
-
+
func(task, args)
end
if not opts['servers'] then
rspamd_logger.errx(rspamd_config, 'no servers are specified')
else
- upstreams = upstream_list.create(opts['servers'], default_port)
+ upstreams = upstream_list.create(rspamd_config, opts['servers'], default_port)
if not upstreams then
rspamd_logger.errx(rspamd_config, 'no servers are specified')
else