-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathRunning-Mode.js
87 lines (77 loc) · 2.51 KB
/
Running-Mode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*根据原作者peng-YM脚本稍作修改*/
let config = {
silence: false, // 是否静默运行,默认false
cellular: "PROXY", // 蜂窝数据下的模式,RULE代表规则模式,PROXY代表全局代理,DIRECT代表全局直连
wifi: "RULE", // wifi下默认的模式
all_direct: ["WRT32X", "WRT32X Extreme"], // 指定全局直连的wifi名字
all_proxy: [], // 指定全局代理的wifi名字
};
// load user prefs from box
const boxConfig = $persistentStore.read("surge_running_mode");
if (boxConfig) {
config = JSON.parse(boxConfig);
config.silence = JSON.parse(config.silence);
config.all_direct = JSON.parse(config.all_direct);
config.all_proxy = JSON.parse(config.all_proxy);
}
const isLoon = typeof $loon !== "undefined";
const isSurge = typeof $httpClient !== "undefined" && !isLoon;
const MODE_NAMES = {
RULE: "🚦WiFi分流模式",
PROXY: "🚀全局免流模式",
DIRECT: "🎯全局直连模式",
};
manager();
$done();
function manager() {
let ssid;
let mode;
if (isSurge) {
const v4_ip = $network.v4.primaryAddress;
// no network connection
if (!config.silence && !v4_ip) {
notify("🤖 Surge 运行模式", "❌ 当前无网络", "");
return;
}
ssid = $network.wifi.ssid;
mode = ssid ? lookupSSID(ssid) : config.cellular;
const target = {
RULE: "rule",
PROXY: "global-proxy",
DIRECT: "direct",
}[mode];
$surge.setOutboundMode(target);
} else if (isLoon) {
const conf = JSON.parse($config.getConfig());
ssid = conf.ssid;
mode = ssid ? lookupSSID(ssid) : config.cellular;
const target = {
DIRECT: 0,
RULE: 1,
PROXY: 2,
}[mode];
$config.setRunningModel(target);
}
if (!config.silence) {
notify(
` ${isSurge ? "Surge" : "Loon"} 运行模式`,
`当前网络:${ssid ? ssid : "蜂窝数据"}`,
`${isSurge ? "Surge" : "Loon"} 已切换至${MODE_NAMES[mode]}`
);
}
}
function lookupSSID(ssid) {
const map = {};
config.all_direct.map((id) => (map[id] = "DIRECT"));
config.all_proxy.map((id) => (map[id] = "PROXY"));
const matched = map[ssid];
return matched ? matched : config.wifi;
}
function notify(title, subtitle, content) {
const SUBTITLE_STORE_KEY = "running_mode_notified_subtitle";
const lastNotifiedSubtitle = $persistentStore.read(SUBTITLE_STORE_KEY);
if (!lastNotifiedSubtitle || lastNotifiedSubtitle !== subtitle) {
$persistentStore.write(subtitle.toString(), SUBTITLE_STORE_KEY);
$notification.post(title, subtitle, content);
}
}