Skip to content

Commit a97e244

Browse files
committed
Skip /etc/hosts modification when hostname already resolves to loopback
Modern macOS and Linux systems with systemd-resolved natively resolve *.localhost to 127.0.0.1/::1 per RFC 6761, making the /etc/hosts entry redundant. This avoids an unnecessary sudo prompt on those systems.
1 parent 901e6b3 commit a97e244

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

plain-dev/plain/dev/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ def install_agent(self) -> None:
254254

255255
def modify_hosts_file(self) -> None:
256256
"""Modify the hosts file to map the custom domain to 127.0.0.1."""
257+
# Check if the hostname already resolves to loopback (e.g., *.localhost on modern OS)
258+
try:
259+
results = socket.getaddrinfo(self.hostname, None)
260+
addrs = {r[4][0] for r in results}
261+
if addrs <= {"127.0.0.1", "::1"}:
262+
return
263+
except socket.gaierror:
264+
pass # Doesn't resolve; fall through to modify hosts file
265+
257266
entry_identifier = "# Added by plain"
258267
hosts_entry = f"127.0.0.1 {self.hostname} {entry_identifier}"
259268

0 commit comments

Comments
 (0)