2013-10-21 13:49:51 -05:00
|
|
|
class Admin::ScreenedIpAddressesController < Admin::AdminController
|
|
|
|
|
2013-10-22 15:30:30 -05:00
|
|
|
before_filter :fetch_screened_ip_address, only: [:update, :destroy]
|
|
|
|
|
2013-10-21 13:49:51 -05:00
|
|
|
def index
|
2014-11-21 14:11:59 -06:00
|
|
|
screened_ip_addresses = ScreenedIpAddress.limit(200).order('match_count desc').to_a
|
2013-10-22 15:30:30 -05:00
|
|
|
render_serialized(screened_ip_addresses, ScreenedIpAddressSerializer)
|
|
|
|
end
|
|
|
|
|
2013-10-24 16:18:10 -05:00
|
|
|
def create
|
|
|
|
screened_ip_address = ScreenedIpAddress.new(allowed_params)
|
|
|
|
if screened_ip_address.save
|
|
|
|
render_serialized(screened_ip_address, ScreenedIpAddressSerializer)
|
|
|
|
else
|
|
|
|
render_json_error(screened_ip_address)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-22 15:30:30 -05:00
|
|
|
def update
|
|
|
|
if @screened_ip_address.update_attributes(allowed_params)
|
|
|
|
render json: success_json
|
|
|
|
else
|
|
|
|
render_json_error(@screened_ip_address)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@screened_ip_address.destroy
|
|
|
|
render json: success_json
|
2013-10-21 13:49:51 -05:00
|
|
|
end
|
|
|
|
|
2014-11-27 12:29:30 -06:00
|
|
|
def star_subnets_query
|
|
|
|
@star_subnets_query ||= <<-SQL
|
|
|
|
SELECT network(inet(host(ip_address) || '/24')) AS ip_range
|
2014-11-24 10:25:48 -06:00
|
|
|
FROM screened_ip_addresses
|
2014-11-27 12:29:30 -06:00
|
|
|
WHERE action_type = #{ScreenedIpAddress.actions[:block]}
|
2014-11-24 10:25:48 -06:00
|
|
|
AND family(ip_address) = 4
|
|
|
|
AND masklen(ip_address) = 32
|
|
|
|
GROUP BY ip_range
|
|
|
|
HAVING COUNT(*) >= :min_count
|
|
|
|
SQL
|
2014-11-27 12:29:30 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def star_star_subnets_query
|
|
|
|
@star_star_subnets_query ||= <<-SQL
|
|
|
|
WITH weighted_subnets AS (
|
|
|
|
SELECT network(inet(host(ip_address) || '/16')) AS ip_range,
|
|
|
|
CASE masklen(ip_address)
|
|
|
|
WHEN 32 THEN 1
|
|
|
|
WHEN 24 THEN :roll_up_weight
|
|
|
|
ELSE 0
|
|
|
|
END AS weight
|
|
|
|
FROM screened_ip_addresses
|
|
|
|
WHERE action_type = #{ScreenedIpAddress.actions[:block]}
|
|
|
|
AND family(ip_address) = 4
|
|
|
|
)
|
|
|
|
SELECT ip_range
|
|
|
|
FROM weighted_subnets
|
|
|
|
GROUP BY ip_range
|
|
|
|
HAVING SUM(weight) >= :min_count
|
|
|
|
SQL
|
|
|
|
end
|
2014-11-24 10:25:48 -06:00
|
|
|
|
2014-11-27 12:29:30 -06:00
|
|
|
def star_subnets
|
|
|
|
min_count = SiteSetting.min_ban_entries_for_roll_up
|
|
|
|
ScreenedIpAddress.exec_sql(star_subnets_query, min_count: min_count).values.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
def star_star_subnets
|
|
|
|
weight = SiteSetting.min_ban_entries_for_roll_up
|
|
|
|
ScreenedIpAddress.exec_sql(star_star_subnets_query, min_count: 10, roll_up_weight: weight).values.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
def roll_up
|
|
|
|
# 1 - retrieve all subnets that needs roll up
|
|
|
|
subnets = [star_subnets, star_star_subnets].flatten
|
2014-11-24 10:25:48 -06:00
|
|
|
|
2014-11-24 12:48:54 -06:00
|
|
|
# 2 - log the call
|
|
|
|
StaffActionLogger.new(current_user).log_roll_up(subnets) unless subnets.blank?
|
|
|
|
|
2014-11-24 10:25:48 -06:00
|
|
|
subnets.each do |subnet|
|
2014-11-24 12:48:54 -06:00
|
|
|
# 3 - create subnet if not already exists
|
2014-11-24 10:25:48 -06:00
|
|
|
ScreenedIpAddress.new(ip_address: subnet).save unless ScreenedIpAddress.where(ip_address: subnet).first
|
|
|
|
|
2014-11-24 12:48:54 -06:00
|
|
|
# 4 - update stats
|
2014-11-24 10:25:48 -06:00
|
|
|
sql = <<-SQL
|
|
|
|
UPDATE screened_ip_addresses
|
|
|
|
SET match_count = sum_match_count,
|
|
|
|
created_at = min_created_at,
|
|
|
|
last_match_at = max_last_match_at
|
|
|
|
FROM (
|
|
|
|
SELECT SUM(match_count) AS sum_match_count,
|
|
|
|
MIN(created_at) AS min_created_at,
|
|
|
|
MAX(last_match_at) AS max_last_match_at
|
|
|
|
FROM screened_ip_addresses
|
2014-11-27 12:29:30 -06:00
|
|
|
WHERE action_type = #{ScreenedIpAddress.actions[:block]}
|
2014-11-24 10:25:48 -06:00
|
|
|
AND family(ip_address) = 4
|
|
|
|
AND ip_address << :ip_address
|
|
|
|
) s
|
|
|
|
WHERE ip_address = :ip_address
|
|
|
|
SQL
|
|
|
|
|
2014-11-27 12:29:30 -06:00
|
|
|
ScreenedIpAddress.exec_sql(sql, ip_address: subnet)
|
2014-11-24 10:25:48 -06:00
|
|
|
|
2014-11-24 12:48:54 -06:00
|
|
|
# 5 - remove old matches
|
2014-11-24 10:25:48 -06:00
|
|
|
ScreenedIpAddress.where(action_type: ScreenedIpAddress.actions[:block])
|
|
|
|
.where("family(ip_address) = 4")
|
|
|
|
.where("ip_address << ?", subnet)
|
|
|
|
.delete_all
|
|
|
|
end
|
|
|
|
|
2014-11-27 12:29:30 -06:00
|
|
|
render json: success_json.merge!({ subnets: subnets })
|
2014-11-24 10:25:48 -06:00
|
|
|
end
|
|
|
|
|
2013-10-22 15:30:30 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def allowed_params
|
|
|
|
params.require(:ip_address)
|
|
|
|
params.permit(:ip_address, :action_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_screened_ip_address
|
|
|
|
@screened_ip_address = ScreenedIpAddress.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2013-10-21 13:49:51 -05:00
|
|
|
end
|