Skip to content

Commit

Permalink
fix a stupid error
Browse files Browse the repository at this point in the history
  • Loading branch information
glzjin committed Aug 15, 2016
1 parent d647041 commit e4231da
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
20 changes: 10 additions & 10 deletions auto_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@


def get_ip(text):
if match_ipv4_address(text) != None:
return match_ipv4_address(text)
if common.match_ipv4_address(text) != None:
return common.match_ipv4_address(text)
else:
if match_ipv6_address(text) != None:
return match_ipv6_address(text)
if common.match_ipv6_address(text) != None:
return common.match_ipv6_address(text)
return None

def file_len(fname):
Expand Down Expand Up @@ -116,8 +116,8 @@ def auto_block_thread():

if str(node) == str(configloader.get_config().NODE_ID):
if configloader.get_config().ANTISSATTACK == 1 and configloader.get_config().CLOUDSAFE == 1 and ip not in denyed_ip_list:
if is_ip(ip) != False:
if is_ip(ip) == socket.AF_INET:
if common.is_ip(ip) != False:
if common.is_ip(ip) == socket.AF_INET:
os.system('route add -host %s gw 127.0.0.1' % str(ip))
deny_str = deny_str + "\nALL: " + str(ip)
else:
Expand All @@ -126,8 +126,8 @@ def auto_block_thread():

logging.info("Remote Block ip:" + str(ip))
else:
if is_ip(ip) != False:
if is_ip(ip) == socket.AF_INET:
if common.is_ip(ip) != False:
if common.is_ip(ip) == socket.AF_INET:
os.system('route add -host %s gw 127.0.0.1' % str(ip))
deny_str = deny_str + "\nALL: " + str(ip)
else:
Expand Down Expand Up @@ -169,8 +169,8 @@ def auto_block_thread():
ip = str(row[1])
if line.find(ip) != -1:
del deny_lines[i]
if is_ip(ip) != False:
if is_ip(ip) == socket.AF_INET:
if common.is_ip(ip) != False:
if common.is_ip(ip) == socket.AF_INET:
os.system('route del -host %s gw 127.0.0.1' % str(ip))
else:
os.system('ip -6 route del ::1/128 via %s/128' % str(ip))
Expand Down
22 changes: 11 additions & 11 deletions db_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ def update_all_user(self, dt_transfer):
for ip in wrong_iplist[id]:
realip = ""
is_ipv6 = False
if is_ip(ip) != False:
if(is_ip(ip) == socket.AF_INET):
if common.is_ip(ip) != False:
if(common.is_ip(ip) == socket.AF_INET):
realip = ip
else:
if match_ipv4_address(ip) != None:
realip = match_ipv4_address(ip)
if common.match_ipv4_address(ip) != None:
realip = common.match_ipv4_address(ip)
else:
is_ipv6 = True
realip = ip
Expand All @@ -135,13 +135,13 @@ def update_all_user(self, dt_transfer):
cur = conn.cursor()
cur.execute("INSERT INTO `blockip` (`id`, `nodeid`, `ip`, `datetime`) VALUES (NULL, '" + str(get_config().NODE_ID) + "', '" + str(realip) + "', unix_timestamp())")
cur.close()
if get_config().CLOUDSAFE == 0:
if is_ipv6 == False:
os.system('route add -host %s gw 127.0.0.1' % str(realip))
deny_str = deny_str + "\nALL: " + str(realip)
else:
os.system('ip -6 route add ::1/128 via %s/128' % str(realip))
deny_str = deny_str + "\nALL: [" + str(realip) +"]/128"
else:
if is_ipv6 == False:
os.system('route add -host %s gw 127.0.0.1' % str(realip))
deny_str = deny_str + "\nALL: " + str(realip)
else:
os.system('ip -6 route add ::1/128 via %s/128' % str(realip))
deny_str = deny_str + "\nALL: [" + str(realip) +"]/128"
if get_config().CLOUDSAFE == 0:
deny_file=open('/etc/hosts.deny','a')
fcntl.flock(deny_file.fileno(),fcntl.LOCK_EX)
Expand Down
14 changes: 7 additions & 7 deletions server_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,25 +288,25 @@ def get_server_wrong(self, port):
if port in self.tcp_servers_pool:
templist = self.tcp_servers_pool[port].wrong_iplist.copy()
for ip in templist:
if ip not in ret and templist[ip] < time.time()-300:
if ip not in ret and templist[ip] < time.time() - 60:
ret.append(ip)
self.tcp_servers_pool[port].wrong_iplist_clean()
if port in self.udp_servers_pool and templist[ip] < time.time()-300:
if port in self.udp_servers_pool:
templist = self.udp_servers_pool[port].wrong_iplist.copy()
for ip in templist:
if ip not in ret:
if ip not in ret and templist[ip] < time.time() - 60:
ret.append(ip)
self.udp_servers_pool[port].wrong_iplist_clean()
if port in self.tcp_ipv6_servers_pool and templist[ip] < time.time()-300:
if port in self.tcp_ipv6_servers_pool:
templist = self.tcp_ipv6_servers_pool[port].wrong_iplist.copy()
for ip in templist:
if ip not in ret:
if ip not in ret and templist[ip] < time.time() - 60:
ret.append(ip)
self.tcp_ipv6_servers_pool[port].wrong_iplist_clean()
if port in self.udp_ipv6_servers_pool and templist[ip] < time.time()-300:
if port in self.udp_ipv6_servers_pool:
templist = self.udp_ipv6_servers_pool[port].wrong_iplist.copy()
for ip in templist:
if ip not in ret:
if ip not in ret and templist[ip] < time.time() - 60:
ret.append(ip)
self.udp_ipv6_servers_pool[port].wrong_iplist_clean()
return ret
Expand Down
27 changes: 11 additions & 16 deletions shadowsocks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,17 @@ def is_ip(address):
pass
return False

def match_ipv4_address(address):
pat = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
test = pat.match(hostIP)
if test:
return test.group(1)
else:
return None

def match_ipv6_address(address):
pat = re.compile("(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}")
test = pat.match(hostIP)
if test:
return test.group(1)
else:
return None

def match_ipv4_address(text):
reip = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
for ip in reip.findall(text):
return ip
return None

def match_ipv6_address(text):
reip = re.compile(r'(?<![:.\w])(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}(?![:.\w])')
for ip in reip.findall(text):
return ip
return None

def patch_socket():
if not hasattr(socket, 'inet_pton'):
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks/tcprelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ def wrong_iplist_clean(self):

temp_new_list = {}
for key in self.wrong_iplist:
if self.wrong_iplist[key] > time.time()-300:
if self.wrong_iplist[key] > time.time() - 60:
temp_new_list[key] = self.wrong_iplist[key]

self.wrong_iplist = temp_new_list.copy()
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks/udprelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ def wrong_iplist_clean(self):

temp_new_list = {}
for key in self.wrong_iplist:
if self.wrong_iplist[key] > time.time()-300:
if self.wrong_iplist[key] > time.time() - 60:
temp_new_list[key] = self.wrong_iplist[key]

self.wrong_iplist = temp_new_list.copy()
Expand Down

0 comments on commit e4231da

Please sign in to comment.