Skip to content

Commit

Permalink
block ip
Browse files Browse the repository at this point in the history
  • Loading branch information
glzjin committed Jul 17, 2016
1 parent fa369e5 commit 862257a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
21 changes: 19 additions & 2 deletions auto_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import socket
import re
import platform
import fcntl



Expand Down Expand Up @@ -49,6 +50,7 @@ def auto_block_thread():


deny_file = open('/etc/hosts.deny')
fcntl.flock(deny_file.no(),fcntl.LOCK_EX)
deny_lines = deny_file.readlines()
deny_file.close()

Expand All @@ -69,12 +71,22 @@ def auto_block_thread():
i = i + 1

deny_file = file("/etc/hosts.deny", "w+")
fcntl.flock(deny_file.no(),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()
cur.close()

if rows != None:
continue

cur = conn.cursor()
cur.execute("INSERT INTO `blockip` (`id`, `nodeid`, `ip`, `datetime`) VALUES (NULL, '" + str(configloader.get_config().NODE_ID) + "', '" + str(ip) + "', unix_timestamp())")
cur.close()
Expand Down Expand Up @@ -109,12 +121,14 @@ def auto_block_thread():


deny_file=open('/etc/hosts.deny','a')
deny_file.write(deny_str)
fcntl.flock(deny_file.no(),fcntl.LOCK_EX)
deny_file.write(deny_str + "\n")
deny_file.close()

if configloader.get_config().ANTISSATTACK == 1 and configloader.get_config().CLOUDSAFE == 1:
deny_file=open('/etc/hosts.deny','a')
deny_file.write(deny_str_at)
fcntl.flock(deny_file.no(),fcntl.LOCK_EX)
deny_file.write(deny_str_at + "\n")
deny_file.close()


Expand All @@ -128,6 +142,7 @@ def auto_block_thread():
conn.close()

deny_file = open('/etc/hosts.deny')
fcntl.flock(deny_file.no(),fcntl.LOCK_EX)
deny_lines = deny_file.readlines()
deny_file.close()

Expand All @@ -143,8 +158,10 @@ def auto_block_thread():
i = i + 1

deny_file = file("/etc/hosts.deny", "w+")
fcntl.flock(deny_file.no(),fcntl.LOCK_EX)
for line in deny_lines:
deny_file.write(line)
deng_file.write("\n")
deny_file.close()

start_line = file_len("/etc/hosts.deny")
Expand Down
12 changes: 10 additions & 2 deletions db_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import importloader
import platform
import datetime
import fcntl


switchrule = None
Expand Down Expand Up @@ -108,6 +109,13 @@ def update_all_user(self, dt_transfer):
for ip in wrong_iplist[id]:
if str(ip) == str(server_ip):
continue
cur = conn.cursor()
cur.execute("SELECT * FROM `blockip` where `ip` = '" + str(ip) + "'")
rows = cur.fetchone()
cur.close()

if rows != None:
continue
if get_config().CLOUDSAFE == 1:
cur = conn.cursor()
cur.execute("INSERT INTO `blockip` (`id`, `nodeid`, `ip`, `datetime`) VALUES (NULL, '" + str(get_config().NODE_ID) + "', '" + str(ip) + "', unix_timestamp())")
Expand All @@ -117,8 +125,8 @@ def update_all_user(self, dt_transfer):
deny_str = deny_str + "\nALL: " + str(ip)
if get_config().ANTISSATTACK == 1 and get_config().CLOUDSAFE == 0:
deny_file=open('/etc/hosts.deny','a')
commands.getoutput(command)
deny_file.write(deny_str)
fcntl.flock(deny_file.fileno(),fcntl.LOCK_EX)
deny_file.write(deny_str + "\n")
deny_file.close()
conn.close()

Expand Down

0 comments on commit 862257a

Please sign in to comment.