Unverified Commit a266e7a8 authored by Brian May's avatar Brian May Committed by GitHub

Merge pull request #545 from skuhl/avoid-touching-etc-hosts

Only write /etc/hosts when necessary.
parents 1dbf2163 e1106a33
...@@ -47,8 +47,11 @@ def rewrite_etc_hosts(hostmap, port): ...@@ -47,8 +47,11 @@ def rewrite_etc_hosts(hostmap, port):
os.rename(tmpname, HOSTSFILE) os.rename(tmpname, HOSTSFILE)
def restore_etc_hosts(port): def restore_etc_hosts(hostmap, port):
rewrite_etc_hosts({}, port) # Only restore if we added hosts to /etc/hosts previously.
if len(hostmap) > 0:
debug2('firewall manager: undoing /etc/hosts changes.\n')
rewrite_etc_hosts({}, port)
# Isolate function that needs to be replaced for tests # Isolate function that needs to be replaced for tests
...@@ -275,8 +278,8 @@ def main(method_name, syslog): ...@@ -275,8 +278,8 @@ def main(method_name, syslog):
debug2('An error occurred, ignoring it.') debug2('An error occurred, ignoring it.')
try: try:
debug2('firewall manager: undoing /etc/hosts changes.\n') # debug2() message printed in restore_etc_hosts() function.
restore_etc_hosts(port_v6 or port_v4) restore_etc_hosts(hostmap, port_v6 or port_v4)
except BaseException: except BaseException:
try: try:
debug1("firewall manager: " debug1("firewall manager: "
......
...@@ -55,7 +55,7 @@ def test_rewrite_etc_hosts(tmpdir): ...@@ -55,7 +55,7 @@ def test_rewrite_etc_hosts(tmpdir):
assert line == "" assert line == ""
with patch('sshuttle.firewall.HOSTSFILE', new=str(new_hosts)): with patch('sshuttle.firewall.HOSTSFILE', new=str(new_hosts)):
sshuttle.firewall.restore_etc_hosts(10) sshuttle.firewall.restore_etc_hosts(hostmap, 10)
assert orig_hosts.computehash() == new_hosts.computehash() assert orig_hosts.computehash() == new_hosts.computehash()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment