Spaces:
Sleeping
Sleeping
Upload server.js
Browse files
server.js
CHANGED
|
@@ -24,6 +24,12 @@ function normalizeUrl(u) {
|
|
| 24 |
if (!u) return null
|
| 25 |
let s = String(u).trim()
|
| 26 |
if (!s) return null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
if (!/^https?:\/\//i.test(s)) {
|
| 28 |
s = 'https://' + s
|
| 29 |
}
|
|
@@ -115,7 +121,7 @@ function jsWrapper(serverBase, baseUrl, transpiledCode, needRegenerator) {
|
|
| 115 |
'function origin(u){try{var a=document.createElement("a");a.href=u;return (a.protocol? a.protocol:"") + "//" + (a.host||"");}catch(e){return "";}}' +
|
| 116 |
'var allow=window.__es5ProxyAllow||{};window.__es5ProxyAllow=allow;' +
|
| 117 |
'allow[origin(__es5_base)]=1;' +
|
| 118 |
-
'function proxyJs(u){var a=abs(u);var o=origin(a);if(__es5_active)allow[o]=1;allow[o]=1;if(allow[o]&&__es5_server)return __es5_server + "/p?url=" + encodeURIComponent(a);return a;}' +
|
| 119 |
'function proxyCss(u){var a=abs(u);var o=origin(a);if(__es5_active)allow[o]=1;if(allow[o]&&__es5_server)return __es5_server + "/s?url=" + encodeURIComponent(a);return a;}' +
|
| 120 |
'function mapArr(items, fn){if(!items||!items.length)return items;for(var i=0;i<items.length;i++){items[i]=fn(items[i]);}return items;}' +
|
| 121 |
'try{' +
|
|
@@ -130,7 +136,7 @@ function jsWrapper(serverBase, baseUrl, transpiledCode, needRegenerator) {
|
|
| 130 |
'var origInsertBefore=Element.prototype.insertBefore;' +
|
| 131 |
'function patchNode(node){' +
|
| 132 |
'if(node&&node.tagName==="SCRIPT"&&node.src){' +
|
| 133 |
-
'var a=abs(node.src);var o2=origin(a);if(__es5_active)allow[o2]=1;allow[o2]=1;if(allow[o2]&&__es5_server&&node.src.indexOf(__es5_server)===-1){node.src=__es5_server+"/p?url="+encodeURIComponent(a);}' +
|
| 134 |
'}' +
|
| 135 |
'}' +
|
| 136 |
'Element.prototype.appendChild=function(node){' +
|
|
@@ -231,7 +237,13 @@ app.get('/s', async (req, res) => {
|
|
| 231 |
})
|
| 232 |
|
| 233 |
app.get('/p', async (req, res) => {
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
if (!source) return res.status(400).type('text/plain').send('Bad url')
|
| 236 |
|
| 237 |
try {
|
|
|
|
| 24 |
if (!u) return null
|
| 25 |
let s = String(u).trim()
|
| 26 |
if (!s) return null
|
| 27 |
+
|
| 28 |
+
// Если URL уже закодирован, раскодируем его один раз перед проверкой
|
| 29 |
+
if (s.indexOf('%3A') !== -1 || s.indexOf('%3a') !== -1) {
|
| 30 |
+
try { s = decodeURIComponent(s) } catch(e) {}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
if (!/^https?:\/\//i.test(s)) {
|
| 34 |
s = 'https://' + s
|
| 35 |
}
|
|
|
|
| 121 |
'function origin(u){try{var a=document.createElement("a");a.href=u;return (a.protocol? a.protocol:"") + "//" + (a.host||"");}catch(e){return "";}}' +
|
| 122 |
'var allow=window.__es5ProxyAllow||{};window.__es5ProxyAllow=allow;' +
|
| 123 |
'allow[origin(__es5_base)]=1;' +
|
| 124 |
+
'function proxyJs(u){var a=abs(u);var o=origin(a);if(__es5_active)allow[o]=1;allow[o]=1;if(allow[o]&&__es5_server){try{var testUrl=new URL(a);if(testUrl.protocol==="http:"||testUrl.protocol==="https:")return __es5_server + "/p?url=" + encodeURIComponent(a);}catch(e){}}return a;}' +
|
| 125 |
'function proxyCss(u){var a=abs(u);var o=origin(a);if(__es5_active)allow[o]=1;if(allow[o]&&__es5_server)return __es5_server + "/s?url=" + encodeURIComponent(a);return a;}' +
|
| 126 |
'function mapArr(items, fn){if(!items||!items.length)return items;for(var i=0;i<items.length;i++){items[i]=fn(items[i]);}return items;}' +
|
| 127 |
'try{' +
|
|
|
|
| 136 |
'var origInsertBefore=Element.prototype.insertBefore;' +
|
| 137 |
'function patchNode(node){' +
|
| 138 |
'if(node&&node.tagName==="SCRIPT"&&node.src){' +
|
| 139 |
+
'var a=abs(node.src);var o2=origin(a);if(__es5_active)allow[o2]=1;allow[o2]=1;if(allow[o2]&&__es5_server&&node.src.indexOf(__es5_server)===-1){try{var testUrl=new URL(a);if(testUrl.protocol==="http:"||testUrl.protocol==="https:"){node.src=__es5_server+"/p?url="+encodeURIComponent(a);}}catch(e){}}' +
|
| 140 |
'}' +
|
| 141 |
'}' +
|
| 142 |
'Element.prototype.appendChild=function(node){' +
|
|
|
|
| 237 |
})
|
| 238 |
|
| 239 |
app.get('/p', async (req, res) => {
|
| 240 |
+
let rawUrl = req.query.url;
|
| 241 |
+
// Попытка раскодировать URL, если он пришел дважды закодированным
|
| 242 |
+
if(rawUrl && (rawUrl.indexOf('%3A') !== -1 || rawUrl.indexOf('%3a') !== -1)){
|
| 243 |
+
try { rawUrl = decodeURIComponent(rawUrl); } catch(e){}
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
const source = normalizeUrl(rawUrl)
|
| 247 |
if (!source) return res.status(400).type('text/plain').send('Bad url')
|
| 248 |
|
| 249 |
try {
|