@@ -8,67 +8,79 @@ from sys import stderr
88from sys import stdout
99from urllib .request import urlopen
1010
11+
1112def run (command ):
1213 output = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
1314 lines , errors = output .communicate ()
1415
1516 if output .returncode != 0 :
16- print (' Error: %s' % errors .strip ())
17+ print (" Error: %s" % errors .strip ())
1718 quit (1 )
1819
19- return str (lines , 'utf-8' ), errors
20+ return str (lines , "utf-8" ), errors
21+
2022
2123def get_chain_lines (chain_name ):
22- lines , errors = run ([' iptables' , '-n' , '-L' , chain_name ])
23- lines = lines .split (' \n ' )
24+ lines , errors = run ([" iptables" , "-n" , "-L" , chain_name ])
25+ lines = lines .split (" \n " )
2426 return lines
2527
28+
2629def get_source_ips (ip_url ):
2730 response = urlopen (ip_url )
28- ip_list = str (response .read (), ' utf-8' ).split ()
31+ ip_list = str (response .read (), " utf-8" ).split ()
2932 return ip_list
3033
34+
3135def get_local_ips (lines ):
3236 result = []
3337
3438 for line in lines :
35- parts = re .split (' \s+' , line )
39+ parts = re .split (" \s+" , line )
3640 # Check if this is the correct line
37- if parts [0 ] == 'ACCEPT' and parts [1 ] in ['all' , '0' ] and parts [4 ] == '0.0.0.0/0' :
41+ if (
42+ parts [0 ] == "ACCEPT"
43+ and parts [1 ] in ["all" , "0" ]
44+ and parts [4 ] == "0.0.0.0/0"
45+ ):
3846 ip = parts [3 ]
3947 result .append (ip )
4048
4149 return result
4250
51+
4352def add_ip (ip , chain_name ):
44- stdout .write (' Adding IP %s...' % ip )
53+ stdout .write (" Adding IP %s..." % ip )
4554 stdout .flush ()
4655
47- run (['iptables' , '-A' , chain_name , '-s' , ip , '-j' , 'ACCEPT' ])
56+ run (["iptables" , "-A" , chain_name , "-s" , ip , "-j" , "ACCEPT" ])
57+
58+ stdout .write (" done\n " )
4859
49- stdout .write (' done\n ' )
5060
5161def remove_ip (ip , chain_name ):
52- stdout .write (' Removing IP %s...' % ip )
62+ stdout .write (" Removing IP %s..." % ip )
5363 stdout .flush ()
5464
55- run (['iptables' , '-D' , chain_name , '-s' , ip , '-j' , 'ACCEPT' ])
65+ run (["iptables" , "-D" , chain_name , "-s" , ip , "-j" , "ACCEPT" ])
66+
67+ stdout .write (" done\n " )
5668
57- stdout .write (' done\n ' )
5869
5970def error (msg ):
60- stderr .write (' Error: %s\n ' % msg )
71+ stderr .write (" Error: %s\n " % msg )
6172 quit (1 )
6273
74+
6375def help ():
64- stdout .write (' Usage: rifc <network-list-url> <iptables-chain-name>\n ' )
65- stdout .write (' \n ' )
76+ stdout .write (" Usage: rifc <network-list-url> <iptables-chain-name>\n " )
77+ stdout .write (" \n " )
6678 quit (2 )
6779
68- def main (argv ):
6980
81+ def main (argv ):
7082 if os .geteuid () != 0 :
71- error (' You need root access to run this program.' )
83+ error (" You need root access to run this program." )
7284
7385 try :
7486 ip_url = argv [1 ]
@@ -89,6 +101,6 @@ def main(argv):
89101 if ip not in source_ips :
90102 remove_ip (ip , chain_name )
91103
92- if __name__ == '__main__' :
93- main (sys .argv )
94104
105+ if __name__ == "__main__" :
106+ main (sys .argv )
0 commit comments