-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.js
More file actions
253 lines (220 loc) · 7.33 KB
/
Copy pathmain.js
File metadata and controls
253 lines (220 loc) · 7.33 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var import_obsidian = require("obsidian");
const DEFAULT_SETTINGS = {
enableProxy: false,
httpProxy: "",
httpsProxy: "",
socksProxy: "",
bypassRules: "<local>,127.*,10.*,172.16.*,172.17.*,172.18.*,172.19.*,172.20.*,172.21.*,172.22.*,172.23.*,172.24.*,172.25.*,172.26.*,172.27.*,172.28.*,172.29.*,172.30.*,172.31.*,192.168.*",
pluginTokens: "persist:surfing-vault-${appId}"
};
var GlobalProxyPlugin = class extends import_obsidian.Plugin {
async onload() {
await this.loadSettings();
this.addSettingTab(new GlobalProxySettingTab(this.app, this));
this.addCommand({
id: 'toggle-global-proxy',
name: 'Toggle global proxy',
callback: async () => {
this.settings.enableProxy = !this.settings.enableProxy;
await this.saveSettings();
this.settings.enableProxy ? this.enableProxy() : this.disableProxy();
this.updateStatusBar();
}
});
this.addCommand({
id: 'enable-global-proxy',
name: 'Enable global proxy',
callback: async () => {
if (!this.settings.enableProxy) {
this.settings.enableProxy = true;
await this.saveSettings();
this.enableProxy();
this.updateStatusBar();
}
}
});
this.addCommand({
id: 'disable-global-proxy',
name: 'Disable global proxy',
callback: async () => {
if (this.settings.enableProxy) {
this.settings.enableProxy = false;
await this.saveSettings();
this.disableProxy();
this.updateStatusBar();
}
}
});
this.statusBarItem = this.addStatusBarItem();
this.statusBarItem.addClass('proxy-status');
this.registerDomEvent(this.statusBarItem, 'click', async () => {
this.settings.enableProxy = !this.settings.enableProxy;
await this.saveSettings();
this.settings.enableProxy ? this.enableProxy() : this.disableProxy();
this.updateStatusBar();
});
this.updateStatusBar();
}
updateStatusBar() {
if (!this.statusBarItem) return;
if (this.settings.enableProxy) {
this.statusBarItem.setText('🌐 Proxy: ON');
this.statusBarItem.addClass('proxy-enabled');
this.statusBarItem.removeClass('proxy-disabled');
} else {
this.statusBarItem.setText('🌐 Proxy: OFF');
this.statusBarItem.addClass('proxy-disabled');
this.statusBarItem.removeClass('proxy-enabled');
}
}
async onunload() {
this.disableProxy();
if (this.statusBarItem) {
this.statusBarItem.remove();
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
this.sessionMap = {}
this.enableProxy();
}
async saveSettings() {
await this.saveData(this.settings);
}
async enableProxy() {
if (!this.settings.enableProxy) {
return;
}
let sessions = []
this.sessionMap.default = electron.remote.session.defaultSession
sessions.push(this.sessionMap.default)
if (!!this.settings.pluginTokens) {
let pluginTokens = this.settings.pluginTokens.split("\n");
for (var i = 0; i < pluginTokens.length; i++) {
if (!pluginTokens[i]) {
continue;
}
let token = pluginTokens[i].replace("${appId}", this.app.appId)
let session = await electron.remote.session.fromPartition(token)
sessions.push(session)
this.sessionMap[token] = session
}
}
let proxyRules = this.composeProxyRules(),
proxyBypassRules = proxyRules ? this.settings.bypassRules : undefined;
for (var i = 0; i < sessions.length; i++) {
await sessions[i].setProxy({ proxyRules, proxyBypassRules });
}
if (proxyRules) {
new import_obsidian.Notice('Enable proxy!');
}
this.updateStatusBar();
}
async disableProxy() {
let sessions = []
for (const key in this.sessionMap) {
sessions.push(this.sessionMap[key])
}
for (var i = 0; i < sessions.length; i++) {
await sessions[i].setProxy({});
await sessions[i].closeAllConnections();
}
new import_obsidian.Notice('Disable proxy!');
this.updateStatusBar();
}
composeProxyRules() {
if (!["socksProxy", "httpProxy", "httpsProxy"].
map((p) => !this.settings[p] || isValidFormat(this.settings[p])).reduce((res, check)=>{return res && check}, true)) {
return undefined;
}
const httpProxy= isValidFormat(this.settings.httpProxy) ? ";http=" + this.settings.httpProxy : "";
const httpsProxy= isValidFormat(this.settings.httpsProxy) ? ";https=" + this.settings.httpsProxy : "";
if (isValidFormat(this.settings.socksProxy)) {
return this.settings.socksProxy + httpProxy + httpsProxy + ",direct://"
} else if (!!httpProxy) {
return !!httpsProxy ? "http=" + this.settings.httpProxy + httpsProxy + ",direct://"
: this.settings.httpProxy + ",direct://"
} else if (!!httpsProxy) {
return this.settings.httpsProxy + ",direct://"
}
return undefined;
}
};
var GlobalProxySettingTab = class extends import_obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
new import_obsidian.Setting(containerEl)
.setName("Enable proxy")
.setDesc("Change your proxy status")
.addToggle((val) => val
.setValue(this.plugin.settings.enableProxy)
.onChange(async (value) => {
this.plugin.settings.enableProxy = value;
await this.plugin.saveSettings();
value ? this.plugin.enableProxy() : this.plugin.disableProxy();
}));
new import_obsidian.Setting(containerEl)
.setName("Socks Proxy")
.setDesc("Set up your socks proxy")
.addText((text) => text
.setPlaceholder("<scheme>://<host>:<port>")
.setValue(this.plugin.settings.socksProxy)
.onChange((value) => {
this.refreshProxy("socksProxy", value);
}));
new import_obsidian.Setting(containerEl)
.setName("Http Proxy")
.setDesc("Set up your http proxy")
.addText((text) => text
.setPlaceholder("<scheme>://<host>:<port>")
.setValue(this.plugin.settings.httpProxy)
.onChange((value) => {
this.refreshProxy("httpProxy", value);
}));
new import_obsidian.Setting(containerEl)
.setName("Https Proxy")
.setDesc("Set up your https proxy")
.addText((text) => text
.setPlaceholder("<scheme>://<host>:<port>")
.setValue(this.plugin.settings.httpsProxy)
.onChange((value) => {
this.refreshProxy("httpsProxy", value);
}));
new import_obsidian.Setting(containerEl)
.setName("Plugin Tokens")
.setDesc("For proxy specified plugins")
.addTextArea((text) => text
.setValue(this.plugin.settings.pluginTokens)
.onChange((value) => {
this.refreshProxy("pluginTokens", value);
}));
new import_obsidian.Setting(containerEl)
.setName("Blacklist")
.setDesc("Proxy blacklist")
.addTextArea((text) => text
.setPlaceholder("[URL_SCHEME://] HOSTNAME_PATTERN [:<port>]\n. HOSTNAME_SUFFIX_PATTERN [:PORT]\n[SCHEME://] IP_LITERAL [:PORT]\nIP_LITERAL / PREFIX_LENGTH_IN_BITS\n<local>")
.setValue(this.plugin.settings.bypassRules)
.onChange((value) => {
this.refreshProxy("bypassRules", value);
}));
}
async refreshProxy(key, value) {
this.plugin.settings[key] = value;
this.plugin.saveSettings();
this.plugin.enableProxy();
}
};
function isValidFormat(proxyUrl) {
if (!!proxyUrl) {
const regex = /^(\w+):\/\/([^:/]+):(\d+)$/;
const matches = proxyUrl.match(regex);
return !!matches;
}
return false;
}
module.exports = GlobalProxyPlugin;