
我一直在嘗試在網路發生變化時重新載入 pf 規則。這是我的 plist 文件。 /Library/LaunchAgents/com.wwk.networkchange.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.wwk.networkchange</string>
<key>ProgramArguments</key>
<array>
<string>sudo /sbin/pfctl -f /Users/wwk/pf.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WatchPaths</key>
<array>
<string>/private/var/run/resolv.conf</string>
</array>
</dict>
</plist>
我可以看到當網路介面更改時 /private/var/run/resolv.conf 發生更改,但 pf 規則根本沒有重新載入。僅供參考,/Users/wwk/pf.conf 在那裡,我已經透過 /etc/sudoers 檔案啟用了 /sbin/pfctl 在沒有密碼提示的情況下運行。先致謝!
答案1
其實這是因為我的新守護程式與Apple預設的pfctl守護程式衝突。因此,我必須更新預設的 pfctl 守護程式 plist 以監視網路變更時更改的路徑。將以下內容新增至/System/Library/LaunchDaemons/com.apple.pfctl.plist
<WatchPaths>
<array>
<string>/private/var/run/resolv.conf</string>
<string>/etc/pf.conf</string>
<string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
</array>
順便說一句,我們必須$ csrutil disable
在 macOS 復原模式下啟用系統檔案的變更才能對上述 plist 檔案進行變更。 (應該$ csrutil enable
在系統檔案進行必要的變更後執行)