Browse Source

Fix plugins

tags/1.1.0
Vsevolod Stakhov 8 years ago
parent
commit
c959fab15e
3 changed files with 40 additions and 40 deletions
  1. 1
    1
      src/plugins/lua/dmarc.lua
  2. 22
    22
      src/plugins/lua/ip_score.lua
  3. 17
    17
      src/plugins/lua/ratelimit.lua

+ 1
- 1
src/plugins/lua/dmarc.lua View File

@@ -290,7 +290,7 @@ end
if not opts['servers'] then
rspamd_logger.errx(rspamd_config, 'no servers are specified for dmarc stats')
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, 'cannot parse servers parameter')
end

+ 22
- 22
src/plugins/lua/ip_score.lua View File

@@ -69,7 +69,7 @@ local asn_re = rspamd_regexp.create_cached("[\\|\\s]")

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])
@@ -85,7 +85,7 @@ local function asn_check(task)
end
end
end
if ip and ip:is_valid() then
local asn_provider = 'asn_provider'
if ip:get_version() == 6 then
@@ -93,7 +93,7 @@ local function asn_check(task)
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
@@ -124,7 +124,7 @@ local function ip_score_get_task_vars(task)
if pool:get_variable("ipnet") then
ipnet = pool:get_variable("ipnet")
end
return asn, country, ipnet
end

@@ -137,7 +137,7 @@ local ip_score_set = function(task)
else
new_total = old_total + 1
end
return old_score + score, new_total
end

@@ -152,10 +152,10 @@ local ip_score_set = function(task)
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
@@ -180,7 +180,7 @@ local ip_score_set = function(task)
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)
@@ -203,19 +203,19 @@ local ip_score_check = function(task)
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
@@ -242,14 +242,14 @@ local ip_score_check = function(task)
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 = {}

@@ -272,18 +272,18 @@ local ip_score_check = function(task)

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
@@ -302,12 +302,12 @@ local ip_score_check = function(task)
-- 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
@@ -344,7 +344,7 @@ local configure_ip_score_module = function()
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

+ 17
- 17
src/plugins/lua/ratelimit.lua View File

@@ -72,9 +72,9 @@ local function parse_limits(data)
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}
@@ -111,13 +111,13 @@ local function check_limits(task, args)
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
@@ -130,11 +130,11 @@ local function check_limits(task, args)
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
@@ -153,7 +153,7 @@ local function set_limits(task, args)
upstream:ok()
end
end
local function rate_set_cb(task, err, data)
if data then
local tv = task:get_timeval()
@@ -164,7 +164,7 @@ local function set_limits(task, args)
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
@@ -177,7 +177,7 @@ local function set_limits(task, args)
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
@@ -187,7 +187,7 @@ local function set_limits(task, args)
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
@@ -230,10 +230,10 @@ local function rate_test_set(task, func)
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
@@ -269,7 +269,7 @@ local function rate_test_set(task, func)
end
end, rcpts)
end
func(task, args)
end

@@ -378,7 +378,7 @@ if opts then
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

Loading…
Cancel
Save