*/
LUA_FUNCTION_DEF (ip, copy);
+/**
+ * @method ip:get_port()
+ * Returns associated port for this IP address
+ * @return {number} port number or nil
+ */
+LUA_FUNCTION_DEF (ip, get_port);
+
static const struct luaL_reg iplib_m[] = {
LUA_INTERFACE_DEF (ip, to_string),
LUA_INTERFACE_DEF (ip, to_table),
LUA_INTERFACE_DEF (ip, str_octets),
LUA_INTERFACE_DEF (ip, inversed_str_octets),
LUA_INTERFACE_DEF (ip, get_version),
+ LUA_INTERFACE_DEF (ip, get_port),
LUA_INTERFACE_DEF (ip, is_valid),
LUA_INTERFACE_DEF (ip, apply_mask),
LUA_INTERFACE_DEF (ip, copy),
return 1;
}
+static gint
+lua_ip_get_port (lua_State *L)
+{
+ struct rspamd_lua_ip *ip = lua_check_ip (L, 1);
+
+ if (ip != NULL && ip->is_valid) {
+ lua_pushnumber (L, rspamd_inet_address_get_port (&ip->addr));
+ }
+ else {
+ lua_pushnil (L);
+ }
+
+ return 1;
+}
+
static gint
lua_ip_from_string (lua_State *L)
{
--- Check specific limit inside redis
local function check_specific_limit (task, limit, key)
- local upstream = upstreams:get_upstream_by_hash(key, task:get_date())
+ local upstream = upstreams:get_upstream_by_hash(key)
+ local addr = upstream:get_addr()
--- Called when value was set on server
local function rate_set_key_cb(task, err, data)
if err then
bucket = bucket - limit[2] * (ntime - atime);
if bucket > 0 then
local lstr = string.format('%.7f:%.7f', ntime, bucket)
- rspamd_redis.make_request(task, upstream:get_ip_string(), upstream:get_port(), rate_set_key_cb,
+ rspamd_redis.make_request(task, addr:to_string(), addr:get_port(), rate_set_key_cb,
'SET %b %b', key, lstr)
if bucket > limit[1] then
task:set_pre_result('soft reject', 'Ratelimit exceeded: ' .. key)
end
else
- rspamd_redis.make_request(task, upstream:get_ip_string(), upstream:get_port(), rate_set_key_cb,
+ rspamd_redis.make_request(task, addr:to_string(), addr:get_port(), rate_set_key_cb,
'DEL %b', key)
end
end
end
end
if upstream then
- rspamd_redis.make_request(task, upstream:get_ip_string(), upstream:get_port(), rate_get_cb, 'GET %b', key)
+ rspamd_redis.make_request(task, addr:to_string(), addr:get_port(), rate_get_cb, 'GET %b', key)
end
end
--- Set specific limit inside redis
local function set_specific_limit (task, limit, key)
- local upstream = upstreams:get_upstream_by_hash(key, task:get_date())
+ local upstream = upstreams:get_upstream_by_hash(key)
+ local addr = upstream:get_addr()
--- Called when value was set on server
local function rate_set_key_cb(task, err, data)
if err then
--- Add new entry
local tv = task:get_timeval()
local atime = tv['tv_usec'] / 1000000. + tv['tv_sec']
- local lstr = string.format('%.7f:1', atime)
- rspamd_redis.make_request(task, upstream:get_ip_string(), upstream:get_port(), rate_set_key_cb,
+ local lstr = string.format('%.7f:1', atime)
+ rspamd_redis.make_request(task, addr:to_string(), addr:get_port(), rate_set_key_cb,
'SET %b %b', key, lstr)
elseif data then
local atime, bucket = parse_limit_data(data)
-- Leak messages
bucket = bucket - limit[2] * (ntime - atime) + 1;
local lstr = string.format('%.7f:%.7f', ntime, bucket)
- rspamd_redis.make_request(task, upstream:get_ip_string(), upstream:get_port(), rate_set_key_cb,
+ rspamd_redis.make_request(task, addr:to_string(), addr:get_port(), rate_set_key_cb,
'SET %b %b', key, lstr)
elseif err then
rspamd_logger.info('got error while setting limit: ' .. err)
end
end
if upstream then
- rspamd_redis.make_request(task, upstream:get_ip_string(), upstream:get_port(), rate_set_cb, 'GET %b', key)
+ rspamd_redis.make_request(task, addr:to_string(), addr:get_port(), rate_set_cb, 'GET %b', key)
end
end