diff --git a/auto_block.py b/auto_block.py index 460db9c..49e38eb 100644 --- a/auto_block.py +++ b/auto_block.py @@ -63,8 +63,8 @@ def auto_block_thread(): for line in real_deny_list: if get_ip(line) and line.find('#') != 0: ip = get_ip(line) - - if ip == server_ip: + + if str(ip).find(str(server_ip)) != -1: i = 0 for line in deny_lines: @@ -81,6 +81,24 @@ def auto_block_thread(): continue + for node_ip in self.node_ip_list: + if str(ip).find(node_ip) != -1: + i = 0 + + for line in deny_lines: + if line.find(ip) != -1: + del deny_lines[i] + i = i + 1 + + deny_file = file("/etc/hosts.deny", "w+") + fcntl.flock(deny_file.fileno(),fcntl.LOCK_EX) + for line in deny_lines: + deny_file.write(line) + deny_file.write("\n") + deny_file.close() + + continue + cur = conn.cursor() cur.execute("SELECT * FROM `blockip` where `ip` = '" + str(ip) + "'") rows = cur.fetchone() diff --git a/db_transfer.py b/db_transfer.py index 3cb6997..8408508 100644 --- a/db_transfer.py +++ b/db_transfer.py @@ -38,6 +38,7 @@ class DbTransfer(object): self.is_relay = False self.relay_rule_list = {} + self.node_ip_list = [] def update_all_user(self, dt_transfer): import cymysql @@ -118,7 +119,6 @@ class DbTransfer(object): cur.execute("INSERT INTO `detect_log` (`id`, `user_id`, `list_id`, `datetime`, `node_id`) VALUES (NULL, '" + str(self.port_uid_table[port]) + "', '" + str(rule_id) + "', UNIX_TIMESTAMP(), '" + str(get_config().NODE_ID) + "')") cur.close() - deny_str = "" if platform.system() == 'Linux' and get_config().ANTISSATTACK == 1 : wrong_iplist = ServerPool.get_instance().get_servers_wrong() @@ -139,9 +139,13 @@ class DbTransfer(object): else: continue - if str(realip) == str(server_ip): + if str(realip).find(str(server_ip)) != -1: continue + for node_ip in self.node_ip_list: + if str(realip).find(node_ip) != -1: + continue + cur = conn.cursor() cur.execute("SELECT * FROM `blockip` where `ip` = '" + str(realip) + "'") rows = cur.fetchone() @@ -273,6 +277,15 @@ class DbTransfer(object): rows.append(d) cur.close() + #读取节点IP + #SELECT * FROM `ss_node` where `node_ip` != '' + self.node_ip_list = [] + cur = conn.cursor() + cur.execute("SELECT `node_ip` FROM `ss_node` where `node_ip` != ''") + for r in cur.fetchall(): + self.node_ip_list.append(str(r[0])) + cur.close() + #读取审计规则,数据包匹配部分 keys_detect = ['id','regex'] @@ -309,6 +322,8 @@ class DbTransfer(object): for id in deleted_id_list: del self.detect_text_list[id] + cur.close() + cur = conn.cursor() cur.execute("SELECT " + ','.join(keys_detect) + " FROM detect_list where `type` = 2") @@ -343,6 +358,8 @@ class DbTransfer(object): for id in deleted_id_list: del self.detect_hex_list[id] + cur.close() + #读取中转规则,如果是中转节点的话 if self.is_relay: @@ -362,7 +379,8 @@ class DbTransfer(object): d['priority'] = long(r[4]) self.relay_rule_list[d['id']] = d - cur.close() + cur.close() + conn.close() return rows