summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorspag <spag@golwen.net>2013-02-20 08:40:15 +0100
committerspag <spag@golwen.net>2013-02-20 08:40:15 +0100
commitaa54ca3ea7b726d6e9c411ed41952ac8b483484c (patch)
treec662969c77a0465fd8fdfdc3da47fba9bddee439 /app/models
parentcb0d2acf3cd0c6a8e07ef564ac906fe623673978 (diff)
write firewall blacklist method
Diffstat (limited to 'app/models')
-rw-r--r--app/models/intruder.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/app/models/intruder.rb b/app/models/intruder.rb
index 249fffc..97e3773 100644
--- a/app/models/intruder.rb
+++ b/app/models/intruder.rb
@@ -31,6 +31,41 @@ class Intruder < ActiveRecord::Base
end
end
+ def self.write_firewall_blacklist
+ firewall_blacklist_file = GsParameter.get('blacklist_file', 'perimeter', 'general')
+ entry_template = GsParameter.get('blacklist_file_entry', 'perimeter', 'general')
+ comment_template = GsParameter.get('blacklist_file_comment', 'perimeter', 'general')
+ File.open(firewall_blacklist_file, 'w') do |file|
+ Intruder.where(:list_type => 'blacklist').where('bans > 0').all.each do |entry|
+ if ! comment_template.blank?
+ file.write(self.expand_variables(comment_template, entry.to_hash) + "\n")
+ end
+ file.write(self.expand_variables(entry_template, entry.to_hash) + "\n")
+ end
+ end
+ end
+
+ def self.expand_variables(line, variables)
+ return line.gsub(/\{([a-z_]+)\}/) do |m|
+ variables[$1.to_sym]
+ end
+ end
+
+ def to_hash
+ return {
+ :key => self.key,
+ :points => self.points,
+ :bans => self.bans,
+ :received_port => self.contact_port,
+ :received_ip => self.contact_ip,
+ :contact_count => self.contact_count,
+ :user_agent => self.user_agent,
+ :to_user => self.to_user,
+ :comment => self.comment,
+ :date => DateTime.now.strftime('%Y-%m-%d %X')
+ }
+ end
+
private
def set_key_if_empty
if self.key.blank?