windows xp firewall bug fix

This commit is contained in:
Barak Hoffer 2015-10-12 14:57:04 +03:00
parent cc32b369b4
commit 5b647f97af
1 changed files with 16 additions and 15 deletions

View File

@ -15,16 +15,20 @@ class FirewallApp(object):
def listen_allowed(self, **kwargs): def listen_allowed(self, **kwargs):
return True return True
def __exit__(self): def __enter__(self):
self.close() return self
def __exit__(self, type, value, traceback):
self.close()
def close(self): def close(self):
return return
def _run_netsh_cmd(command, args): def _run_netsh_cmd(command, args):
cmd = subprocess.Popen("netsh %s %s" % (command, " ".join(['%s="%s"'%(key,value) for key,value in args.items()])), stdout=subprocess.PIPE) cmd = subprocess.Popen("netsh %s %s" % (command, " ".join(['%s="%s"'%(key,value) for key,value in args.items() if value])), stdout=subprocess.PIPE)
return cmd.stdout.read().strip().lower().endswith('ok.') return cmd.stdout.read().strip().lower().endswith('ok.')
class WinAdvFirewall(FirewallApp): class WinAdvFirewall(FirewallApp):
def __init__(self): def __init__(self):
self._rules = {} self._rules = {}
@ -67,7 +71,7 @@ class WinAdvFirewall(FirewallApp):
del self._rules[name] del self._rules[name]
return True return True
else: else:
return False return False
except: except:
return None return None
@ -117,7 +121,8 @@ class WinFirewall(FirewallApp):
netsh_args.update(kwargs) netsh_args.update(kwargs)
try: try:
if _run_netsh_cmd('firewall add', netsh_args): if _run_netsh_cmd('firewall add %s' % rule, netsh_args):
netsh_args['rule'] = rule
self._rules[name] = netsh_args self._rules[name] = netsh_args
return True return True
else: else:
@ -125,13 +130,11 @@ class WinFirewall(FirewallApp):
except: except:
return None return None
def remove_firewall_rule(self, rule='allowedprogram', name="Firewall", **kwargs): def remove_firewall_rule(self, rule='allowedprogram', name="Firewall", mode="ENABLE", program=sys.executable, **kwargs):
netsh_args = {'name': name, netsh_args = {'program' : program}
'mode' : mode,
'program' : program}
netsh_args.update(kwargs) netsh_args.update(kwargs)
try: try:
if _run_netsh_cmd('firewall delete', netsh_args): if _run_netsh_cmd('firewall delete %s' % rule, netsh_args):
if self._rules.has_key(name): if self._rules.has_key(name):
del self._rules[name] del self._rules[name]
return True return True
@ -146,16 +149,14 @@ class WinFirewall(FirewallApp):
for rule in self._rules.values(): for rule in self._rules.values():
if rule.get('program') == sys.executable and \ if rule.get('program') == sys.executable and \
'allowedprogram' == rule.get('rule') and \ 'ENABLE' == rule.get('mode'):
'ENABLE' == rule.get('mode') and \
4 == len(rule.keys()):
return True return True
return False return False
def close(self): def close(self):
try: try:
for rule in self._rules.keys(): for rule in self._rules.values():
_run_netsh_cmd('firewall delete', {'name' : rule}) self.remove_firewall_rule(**rule)
except: except:
pass pass