mirror of
https://github.com/StevenBlack/hosts.git
synced 2026-07-01 02:36:52 +00:00
fix windows sudo and replace
This commit is contained in:
@@ -42,6 +42,8 @@ except ImportError:
|
|||||||
# Syntactic sugar for "sudo" command in UNIX / Linux
|
# Syntactic sugar for "sudo" command in UNIX / Linux
|
||||||
if platform.system() == "OpenBSD":
|
if platform.system() == "OpenBSD":
|
||||||
SUDO = ["/usr/bin/doas"]
|
SUDO = ["/usr/bin/doas"]
|
||||||
|
elif platform.system() == "Windows":
|
||||||
|
SUDO = ["Start-Process", "powershell", "-Verb", "runAs"]
|
||||||
else:
|
else:
|
||||||
SUDO = ["/usr/bin/env", "sudo"]
|
SUDO = ["/usr/bin/env", "sudo"]
|
||||||
|
|
||||||
@@ -1279,42 +1281,35 @@ def move_hosts_file_into_place(final_file):
|
|||||||
""" # noqa: W605
|
""" # noqa: W605
|
||||||
|
|
||||||
filename = os.path.abspath(final_file.name)
|
filename = os.path.abspath(final_file.name)
|
||||||
|
if platform.system() == "Windows":
|
||||||
if sys.platform == "linux":
|
target_file = Path(os.getenv("SystemRoot")) / "system32" / "drivers" / "etc" / "hosts"
|
||||||
|
else:
|
||||||
target_file = "/etc/hosts"
|
target_file = "/etc/hosts"
|
||||||
|
|
||||||
if os.getenv("IN_CONTAINER"):
|
if os.getenv("IN_CONTAINER"):
|
||||||
# It's not allowed to remove/replace a mounted /etc/hosts, so we replace the content.
|
# It's not allowed to remove/replace a mounted /etc/hosts, so we replace the content.
|
||||||
# This requires running the container user as root, as is the default.
|
# This requires running the container user as root, as is the default.
|
||||||
print(f"Running in container, so we will replace the content of {target_file}.")
|
print(f"Running in container, so we will replace the content of {target_file}.")
|
||||||
try:
|
|
||||||
with open(target_file, "w") as target_stream:
|
|
||||||
with open(filename, "r") as source_stream:
|
|
||||||
target_stream.write(source_stream.read())
|
|
||||||
return True
|
|
||||||
except Exception:
|
|
||||||
print_failure(f"Replacing content of {target_file} failed.")
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
print(
|
|
||||||
f"Replacing {target_file} requires root privileges. You might need to enter your password."
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
subprocess.run(SUDO + ["cp", filename, target_file], check=True)
|
|
||||||
return True
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
print_failure(f"Replacing {target_file} failed.")
|
|
||||||
return False
|
|
||||||
elif sys.platform == "win32":
|
|
||||||
target_file = Path(os.getenv("SystemRoot")) / "system32" / "drivers" / "etc" / "hosts"
|
|
||||||
try:
|
try:
|
||||||
with open(target_file, "w") as target_stream:
|
with open(target_file, "w") as target_stream:
|
||||||
with open(filename, "r") as source_stream:
|
with open(filename, "r") as source_stream:
|
||||||
target_stream.write(source_stream.read())
|
target_stream.write(source_stream.read())
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
print_failure(f"Replacing content of {target_file} failed.")
|
print_failure(f"Replacing content of {target_file} failed.")
|
||||||
return False
|
return False
|
||||||
|
elif platform.system() == "Linux" or platform.system() == "Windows":
|
||||||
|
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"Replacing {target_file} requires root privileges. You might need to enter your password."
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
subprocess.run(SUDO + ["cp", filename, target_file], check=True)
|
||||||
|
return True
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
print_failure(f"Replacing {target_file} failed.")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def flush_dns_cache():
|
def flush_dns_cache():
|
||||||
|
|||||||
Reference in New Issue
Block a user