Created
November 14, 2022 18:42
-
-
Save ServOKio/842021a2b9997e407abd1fab8f3535af to your computer and use it in GitHub Desktop.
proxy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const | |
express = require('express'), | |
axios = require('axios'), | |
sizeOf = require('image-size'), | |
os = require("os"); | |
var app = express(); | |
function formatBytes(bytes, decimals = 2) { | |
if (!+bytes) return '0 Bytes' | |
const k = 1024 | |
const dm = decimals < 0 ? 0 : decimals | |
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | |
const i = Math.floor(Math.log(bytes) / Math.log(k)) | |
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}` | |
} | |
app.get('/', function(req, res) { | |
const type = req.query.type; | |
if (!type) { | |
res.status(400).json({ | |
statscode: 2, | |
message: "type is not specified" | |
}); | |
return; | |
} | |
if (type === 'get_info') { | |
const url = req.query.url; | |
const parsed = new URL(url); | |
const config = { | |
method: 'get', | |
url: parsed.href, | |
headers: { | |
'cache-control': 'max-age=0', | |
'upgrade-insecure-requests': '1', | |
'user-agent': 'Mozilla/5.0 (compatible; ServOKioBot/1.0; + https://servokio.ru/bots)', | |
'sec-fetch-user': '?1', | |
'host': parsed.host, | |
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', | |
'accept-encoding': 'gzip', | |
'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7' | |
}, | |
responseType: 'arraybuffer' | |
} | |
axios(config).then(response => { | |
if (response.status === 200) { | |
const | |
d = response.data, | |
headers = response.headers; | |
let pre = { | |
statscode: 1, | |
message: 'ok', | |
contentType: headers['content-type'], | |
httpCode: response.status, | |
contentLength: d.length, | |
reatableContentLenght: formatBytes(d.length), | |
url: response.request.res.responseUrl | |
} | |
if (['image/png', 'image/gif', 'image/jpeg', 'image/webp'].includes(headers['content-type'])) { | |
const s = sizeOf(Buffer.from(d, 'utf8')); | |
pre.width = s.width; | |
pre.height = s.height; | |
} | |
res.status(200).json(pre); | |
} else { | |
res.status(409).json({ | |
statscode: 1, | |
message: 'Conflict', | |
httpCode: response.status, | |
contentType: headers['content-type'], | |
}); | |
} | |
}).catch(error => { | |
if (error.response) { | |
res.status(200).json({ | |
statscode: 1, | |
message: 'ok', | |
contentType: headers['content-type'], | |
httpCode: error.response.status, | |
contentLength: error.response.data.length, | |
reatableContentLenght: formatBytes(error.response.data.length), | |
}); | |
} else if (error.request) { | |
res.status(200).json({ | |
statscode: 1, | |
message: 'Server rejected the request' | |
}); | |
} else { | |
console.log(error) | |
res.status(200).json({ | |
statscode: 1, | |
message: 'Unknown error' | |
}); | |
} | |
}); | |
} else if (['get_content', 'get_image', 'get_video', 'get_mp3'].includes(type)) { | |
const url = req.query.url; | |
const parsed = new URL(url); | |
const u18chan = req.query.u18chan != null; | |
const config = { | |
method: 'get', | |
url: parsed.href, | |
headers: { | |
'cache-control': 'max-age=0', | |
'upgrade-insecure-requests': '1', | |
'user-agent': 'Mozilla/5.0 (compatible; ServOKioBot/1.0; + https://servokio.ru/bots)', | |
'sec-fetch-user': '?1', | |
'host': parsed.host, | |
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', | |
'accept-encoding': 'gzip', | |
'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7' | |
}, | |
responseType: 'arraybuffer' | |
} | |
axios(config).then(response => { | |
if (response.status === 200) { | |
const | |
d = response.data, | |
headers = response.headers; | |
res.status(200).type(headers['content-type']).send(d) | |
} else { | |
res.status(409).json({ | |
statscode: 1, | |
message: 'Conflict', | |
httpCode: response.status | |
}); | |
} | |
}).catch(error => { | |
if (error.response) { | |
res.status(200).json({ | |
statscode: 1, | |
message: 'ok', | |
contentType: headers['content-type'], | |
httpCode: error.response.status, | |
contentLength: error.response.data.length, | |
reatableContentLenght: formatBytes(error.response.data.length), | |
}); | |
} else if (error.request) { | |
res.status(200).json({ | |
statscode: 1, | |
message: 'Server rejected the request' | |
}); | |
} else { | |
console.log(error) | |
res.status(200).json({ | |
statscode: 1, | |
message: 'Unknown error' | |
}); | |
} | |
}); | |
} else if (type === 'get_server_info') { | |
const startUsage = process.cpuUsage(); | |
const now = Date.now(); | |
while (Date.now() - now < 500); | |
res.status(200).json({ | |
statscode: 1, | |
message: 'ok', | |
server: { | |
disk_free_space: -1, | |
disk_total_space: -1, | |
ram_free_space: os.freemem(), | |
ram_total_space: os.totalmem(), | |
cpu_usage: process.cpuUsage(startUsage).user | |
} | |
}); | |
} else { | |
res.status(200).json({ | |
statscode: 1, | |
message: 'ok' | |
}); | |
} | |
}); | |
app.listen(3000, _ => { | |
console.log('ok') | |
}); | |
process.on('uncaughtException', err => { | |
console.error(err); | |
console.warn(`Node NOT Exiting...`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment