Created
May 6, 2024 12:34
-
-
Save aapzu/5dfb16d3a9d8fcdaca0a32ff070a293b to your computer and use it in GitHub Desktop.
Mac domain aliases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/.config/fish/functions/add-domain-alias.fish | |
function add-domain-alias -d "Create a domain alias for local development" | |
# Usage: add-domain-alias myapp 8000 | |
set domain $argv[1] | |
set port $argv[2] | |
if test -z "$domain" -o -z "$port" | |
echo "Usage: add-domain-alias <domain> <port>" | |
return 1 | |
end | |
set existing (cat /etc/hosts | grep $domain | cut -d' ' -f1) | |
if test -n "$existing" | |
set localip $existing | |
else | |
set localip "127.0.0.1" | |
echo "$localip $domain" | sudo tee -a /etc/hosts | |
end | |
begin | |
sudo ifconfig lo0 alias $localip | |
echo "rdr pass on lo0 inet proto tcp from any to $localip port 80 -> $localip port $port" | sudo pfctl -ef - | |
end | |
echo "Done. Run apps at $localip port $port and view them at http://$domain/." | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/.config/fish/functions/remove-domain-alias.fish | |
function remove-domain-alias -d "Remove a domain alias for local development" | |
# Usage: remove-domain-alias myapp | |
set domain $argv[1] | |
if test -z "$domain" | |
echo "Usage: remove-domain-alias <domain>" | |
return 1 | |
end | |
# Retrieve the local IP used for the domain | |
set localip (cat /etc/hosts | grep $domain | cut -d' ' -f1) | |
if test -n "$localip" | |
# Remove the IP alias | |
sudo ifconfig lo0 -alias $localip | |
# Remove the rdr rules associated with this IP | |
sudo pfctl -a com.apple/250.rdr -F all | |
# Remove the domain from /etc/hosts | |
sudo sed -i '' "/$localip $domain/d" /etc/hosts | |
echo "Alias for $domain removed successfully." | |
else | |
echo "No alias found for $domain." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment