How to Use Surge to Switch Proxy Modes Based on Wi-Fi (Step-by-Step Guide)

Shows how to trigger Surge scripts when Wi-Fi changes so proxy mode flips automatically between DIRECT and RULE.

Feb 6, 2021 · 1 min read · 164 Words · -Views -Comments · Digital Nomad
Network Tools 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

If your home router already handles global proxying, there’s no need to enable another proxy on your Mac—it just adds latency. But once you leave home and join office or café Wi-Fi, you do need it. Surge event scripts make this easy to automate.

Docs: https://manual.nssurge.com/scripting/common.html

Configuration

Add this to your config:

[Script]
script1 = type=event,event-name=network-changed,script-path=wifi-changed.js

wifi-changed.js:

const WIFI_DONT_NEED_PROXYS = ['xiaomi_Alan_5G_1'];
const CURRENT_WIFI_SSID_KEY = 'current_wifi_ssid';

if (wifiChanged()) {
  const mode = WIFI_DONT_NEED_PROXYS.includes($network.wifi.ssid)
    ? 'direct'
    : 'rule';
  $surge.setOutboundMode(mode);
  $notification.post(
    'Surge',
    `Wi-Fi changed to ${$network.wifi.ssid || 'cellular'}`,
    `use ${mode} mode`
  );
}

function wifiChanged() {
  const currentWifiSSid = $persistentStore.read(CURRENT_WIFI_SSID_KEY);
  const changed = currentWifiSSid !== $network.wifi.ssid;
  if (changed) $persistentStore.write($network.wifi.ssid, CURRENT_WIFI_SSID_KEY);
  return changed;
}

$done();

Whenever the Wi-Fi SSID changes, Surge switches outbound mode and posts a notification.

https://static.1991421.cn/2021/2021-02-06-121252.jpeg

The network-changed event fires for any network transition (e.g., toggling VPN). We use $persistentStore to detect real Wi-Fi changes.

Final Thoughts

Out of the box Surge can handle this sort of quality-of-life automation—one more reason it’s my go-to network tool.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover