From c93f37ddc38591e9f3d5b72655c9c29db7650474 Mon Sep 17 00:00:00 2001 From: mzz <2017@duck.com> Date: Sun, 16 Apr 2023 18:46:34 +0800 Subject: [PATCH] Update run-on-macos.md Add auto set route and DNS --- docs/getting-started/run-on-macos.md | 73 +++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/docs/getting-started/run-on-macos.md b/docs/getting-started/run-on-macos.md index 511e88a..613d563 100644 --- a/docs/getting-started/run-on-macos.md +++ b/docs/getting-started/run-on-macos.md @@ -23,7 +23,9 @@ curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C ho ## Lima -This chapter intruduces how to use [lima](https://github.com/lima-vm/lima) virtual machine to run dae, and proxy whole macOS host network. +### Setup + +This section intruduces how to use [lima](https://github.com/lima-vm/lima) virtual machine to run dae, and proxy whole macOS host network. First, we should install `lima` and `socket_vmnet`. @@ -178,7 +180,7 @@ Set default route of macOS to dae VM. > **Note** > You may need to execute this command every time you connect to network. > -> Refer to [run a script after a interface comes up](https://apple.stackexchange.com/questions/32354/how-do-you-run-a-script-after-a-network-interface-comes-up) if you want to auto execute it. +> Refer to [Auto set route and DNS](#auto-set-route-and-dns) if you want to auto execute it. ```shell # Get IP of dae VM. @@ -195,3 +197,70 @@ Verify that we were successful. # Verify. curl -v ipinfo.io ``` + +### Auto set route and DNS + +Write a script to execute. + +```shell +# The script to execute. +mkdir -p /Users/Shared/bin +cat << 'EOF' > /Users/Shared/bin/dae-network-update.sh +#!/bin/sh +set -e +export PATH=$PATH:/opt/local/bin/ +dae_ip=$(limactl shell dae ip --json addr | limactl shell dae jq -cr '.[] | select( .ifname == "lima0" ).addr_info | .[] | select( .family == "inet" ).local') +current_gateway=$(route get default|grep gateway|rev|cut -d' ' -f1|rev) +[ "$current_gateway" != "$dae_ip" ] && (sudo route delete default; sudo route add default $dae_ip) +networksetup -setdnsservers Wi-Fi $dae_ip +exit 0 +EOF + +# Give executable permission. +chmod +x /Users/Shared/bin/dae-network-update.sh +``` + +Give no-password permission for route. +```shell +if [ $(id -u) -eq "0" ]; then echo 'Do not use root!!'; else echo "$(whoami) ALL=(ALL) NOPASSWD: $(which route)" | sudo tee /etc/sudoers.d/"$(whoami)"-route; fi +``` + +Write a plist service file. + +```shell +cat << 'EOF' > ~/Library/LaunchAgents/org.v2raya.dae.networkchanging.plist + + + + + Label + org.v2raya.dae.networkchanging + + LowPriorityIO + + + ProgramArguments + + /Users/Shared/bin/dae-network-update.sh + + + WatchPaths + + /etc/resolv.conf + /Library/Preferences/SystemConfiguration/NetworkInterfaces.plist + /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist + + + RunAtLoad + + + +EOF +``` + +Load the plist service. + +```shell +launchctl load ~/Library/LaunchAgents/org.v2raya.dae.networkchanging.plist +```