-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.js
268 lines (245 loc) · 7.7 KB
/
main.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const {Plugin, PluginSettingTab, Setting } = require("obsidian");
let server = 'fastgit'
let proMap = {
fastgit:{
down:"https://download.fastgit.org/"
,raw:"https://raw.fastgit.org/"
,home:"https://hub.fastgit.org/"
}
,mtr:{
down:"https://download.fastgit.org/"
,raw:"https://raw-gh.gcdn.mirr.one/"
,home:"https://api.mtr.pub/"
}
,ghproxy:{
down:"https://mirror.ghproxy.com/https://github.com/"
,raw:"https://mirror.ghproxy.com/https://github.com/"
,home:"https://mirror.ghproxy.com/https://github.com/"
}
,gitclone:{
down:"https://download.fastgit.org/"
,raw:"https://raw.fastgit.org/"
,home:"https://gitclone.com/github.com/"
}
,mirr:{
down:"https://gh.gcdn.mirr.one/"
,raw:"https://raw-gh.gcdn.mirr.one/"
,home:"https://gh.gcdn.mirr.one/"
}
}
let include = [
{
match: (url) => url.indexOf("/releases/download/") >= 0
, to: (url) => url.replace("https://github.com/", proMap[server].down)
}
, {
match: (url) => url.startsWith("https://raw.githubusercontent.com/") >= 0
, to: (url) => url.replace("https://raw.githubusercontent.com/", proMap[server].raw)
}
, {
match: (url) => url.startsWith("https://github.com/") >= 0
, to: (url) => url.replace("https://github.com/", proMap[server].home)
}
]
// 匹配URL
function matchUrl(e) {
console.log("开始访问:" + JSON.stringify(e))
for (var key in include) {
let item = include[key]
console.log(key)
console.log(item)
if (e && e.url && item.match(e.url)) {
e.url = item.to(e.url)
console.log("要访问的地址:" + e.url)
if (!e.headers) {
e.headers = {}
}
e.headers["content-type"] = "application/x-www-form-urlencoded";
e.headers["Access-Control-Allow-Origin"] = "*"
return true;
}
}
return false;
}
// 代理访问
function proxy(e) {
return new Promise((function (t, n) {
e.success = t;
e.error = function (e, t) {
return n(t)
}
debugger
if (app.isMobile) {
forMobile(e);
return;
}
forPC(e)
}))
}
/**
*
* https://github.com/denolehov/obsidian-git/issues/57
* https://capacitorjs.com/blog/bypassing-cors-with-the-http-plugin
* https://github.com/capacitor-community/http
*
* @param {*} e
*/
async function forMobile(e) {
try {
const http = require('@capacitor-community/http')
const options = {url: e.url};
new window.Notice("发送请求:" + e.url, 10000)
new window.Notice(JSON.stringify(http.get) + "123", 10000)
// http.get(options);
const resp = await http.get(options).then((resp) => {
new window.Notice("请求成功:", 10000)
new window.Notice("请求成功:" + resp.status, 10000)
e.success(resp.data)
}).catch((error) => {
new window.Notice("出错了:" + JSON.stringify(error), 10000)
})
new window.Notice("请求成功12", 10000)
e.success(resp.data)
} catch (e) {
e.error(e)
new window.Notice("加载@capacitor-community/http出错", 10000)
}
}
function forPC(e) {
try {
const https = require('https')
https.get(e.url, function (res) {
new window.Notice("https.get成功", 10000)
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
new window.Notice("https.get处理数据成功", 10000)
e.success(rawData)
} catch (e) {
new window.Notice("https.get处理数据失败", 10000)
e.error(e)
}
});
}).on('error', function (e) {
new window.Notice("https.get失败", 10000)
e.error(e)
})
} catch (e) {
new window.Notice("导入http出错", 10000)
new window.Notice(JSON.parse(e), 10000)
}
}
function apProxy() {
var ap;
this.regedit = function() {
ap = window.ajaxPromise;
window.ajaxPromise = function (e) {
if (!matchUrl(e)) {
return ap(e);
}
new window.Notice("正在通过 ProxyGithub 来代理访问社区插件!")
return proxy(e)
}
}
this.unRegedit = function() {
window.ajaxPromise = ap;
}
}
//window.Capacitor.registerPlugin("App").request
function apCapacitor() {
var ap;
this.regedit = function() {
ap = window.Capacitor.registerPlugin("App").request;
console.log(ap)
window.Capacitor.registerPlugin("App").request = function (e){
matchUrl(e);
new window.Notice("正在通过 ProxyGithub 来代理访问社区插件!")
ap(e);
// if (matchUrl(e)) {
// return ap(e);
// }
}
console.log("apc注册成功")
}
this.unRegedit = function() {
window.window.Capacitor.registerPlugin("App").request = ap;
}
}
function apElectron() {
var ap;
this.regedit = function() {
ap = window.require("electron").ipcRenderer.send;
debugger
console.log(ap)
window.require("electron").ipcRenderer.send = function (a,b,e,...rest){
debugger
matchUrl(e);
new window.Notice("正在通过 ProxyGithub 来代理访问社区插件!")
ap(a,b,e, ...rest);
// if (matchUrl(e)) {
// return ap(e);
// }
}
console.log("apc注册成功")
}
this.unRegedit = function() {
window.require("electron").ipcRenderer.send = ap;
}
}
class ProxyGithubSettingTab extends PluginSettingTab {
constructor(app, plugin) {
console.log("加载了tab~~~~~~~~~~~~~~~~~~~~~~")
super(app, plugin)
this.plugin = plugin
}
async display() {
this.containerEl.empty()
new Setting(this.containerEl)
.setName('代理服务器')
.setDesc(`通过选择不同的服务器来切换代理,可以解决某些情况下,某个服务器无法访问的情况。当前代理服务器:${this.plugin.settings.server}`)
// .setValue(this.plugin.settings.server) // <-- Add me!
.addDropdown(dropDown => {
dropDown.addOption('mirr', '请选择');
dropDown.addOption('fastgit', 'fastgit');
dropDown.addOption('mtr', 'mtr');
dropDown.addOption('ghproxy', 'ghproxy');
dropDown.addOption('gitclone', 'gitclone');
dropDown.addOption('mirr', 'mirr');
dropDown.onChange(async (value) => {
this.plugin.settings.server=value
// this.plugin.settings.server = value;
await this.plugin.saveSettings();
});
});
}
}
let app = new apProxy();
let apc = new apCapacitor();
let ape = new apElectron();
module.exports = class ProxyGithub extends Plugin {
onload() {
new window.Notice("添加 ProxyGithub 代理访问社区插件!");
this.addSettingTab(new ProxyGithubSettingTab(this.app, this));
ape.regedit();
apc.regedit();
app.regedit();
this.settings = {server:'mirr'}
}
async loadSettings() {
this.settings = Object.assign({}, {server:'mirr'}, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
server = this.settings.server;
debugger
}
onunload() {
ape.unRegedit()
apc.unRegedit()
app.unRegedit()
}
}