Add custom proxy selection (#53)

* Add custom proxy selection

* Remove request dependency

* Remove duplicate font
This commit is contained in:
Karang 2021-03-04 01:39:21 +01:00 committed by GitHub
parent 04b8ef2b68
commit edf12cea69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 64 deletions

Binary file not shown.

View file

@ -1,4 +1,5 @@
/* global THREE, prompt */ /* global THREE, prompt */
const net = require('net')
// Workaround for process.versions.node not existing in the browser // Workaround for process.versions.node not existing in the browser
process.versions.node = '14.0.0' process.versions.node = '14.0.0'
@ -31,13 +32,35 @@ async function statusRunner () {
async function main () { async function main () {
statusRunner() statusRunner()
const viewDistance = 6 const viewDistance = 6
const host = prompt('Host', '95.111.249.143') const hostprompt = prompt('Host:port', '95.111.249.143:10000')
const port = parseInt(prompt('Port', '10000')) const proxyprompt = prompt('Proxy:port (blank for default)', '')
const username = prompt('Username', 'pviewer' + (Math.floor(Math.random() * 1000))) const username = prompt('Username', 'pviewer' + (Math.floor(Math.random() * 1000)))
let password = prompt('Password (blank for offline)', '') let password = prompt('Password (blank for offline)', '')
password = password === '' ? undefined : password password = password === '' ? undefined : password
let host, port, proxy, proxyport
if (!hostprompt.includes(':')) {
host = hostprompt
port = 25565
} else {
[host, port] = hostprompt.split(':')
port = parseInt(port, 10)
}
if (!proxyprompt.includes(':')) {
proxy = proxyprompt
proxyport = undefined
} else {
[proxy, proxyport] = proxyprompt.split(':')
proxyport = parseInt(proxyport, 10)
}
console.log(`connecting to ${host} ${port} with ${username}`) console.log(`connecting to ${host} ${port} with ${username}`)
if (proxy) {
console.log(`using proxy ${proxy} ${proxyport}`)
net.setProxy({ hostname: proxy, port: proxyport })
}
status = 'Logging in' status = 'Logging in'
document.getElementById('loading-text').requestFullscreen() document.getElementById('loading-text').requestFullscreen()

View file

@ -42,8 +42,7 @@
"dependencies": { "dependencies": {
"compression": "^1.7.4", "compression": "^1.7.4",
"express": "^4.17.1", "express": "^4.17.1",
"net-browserify": "^0.2.4", "net-browserify": "PrismarineJS/net-browserify"
"request": "^2.88.2"
}, },
"devDependencies": { "devDependencies": {
"assert": "^2.0.0", "assert": "^2.0.0",

View file

@ -2,76 +2,17 @@
const express = require('express') const express = require('express')
const netApi = require('net-browserify') const netApi = require('net-browserify')
const request = require('request')
const compression = require('compression') const compression = require('compression')
const path = require('path') const path = require('path')
// Create our app // Create our app
const app = express() const app = express()
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', req.get('Origin') || '*')
res.header('Access-Control-Allow-Credentials', 'true')
res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE')
res.header('Access-Control-Expose-Headers', 'Content-Length')
res.header(
'Access-Control-Allow-Headers',
'Accept, Authorization, Content-Type, X-Requested-With, Range'
)
if (req.method === 'OPTIONS') {
return res.send(200)
} else {
return next()
}
})
app.use(compression()) app.use(compression())
app.use(netApi()) app.use(netApi({ allowOrigin: '*' }))
app.use(express.static(path.join(__dirname, './public'))) app.use(express.static(path.join(__dirname, './public')))
app.use(express.json({ limit: '100kb' })) app.use(express.json({ limit: '100kb' }))
app.all('*', function (req, res, next) {
// Set CORS headers: allow all origins, methods, and headers: you may want to lock this down in a production environment
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET, PUT, PATCH, POST, DELETE')
res.header(
'Access-Control-Allow-Headers',
req.header('access-control-request-headers')
)
if (req.method === 'OPTIONS') {
// CORS Preflight
res.send()
} else {
const targetURL = req.header('Target-URL')
if (!targetURL) {
res.status(404).send({ error: '404 Not Found' })
return
}
const newHeaders = req.headers
newHeaders.host = targetURL
.replace('https://', '')
.replace('http://', '')
.split('/')[0]
request(
{
url: targetURL + req.url,
method: req.method,
json: req.body,
headers: req.headers
},
function (error, response, body) {
if (error) {
console.error(error)
console.error('error: ' + response.statusCode)
}
// console.log(body);
}
).pipe(res)
}
})
// Start the server // Start the server
const server = app.listen(process.argv[2] === undefined ? 8080 : process.argv[2], function () { const server = app.listen(process.argv[2] === undefined ? 8080 : process.argv[2], function () {
console.log('Server listening on port ' + server.address().port) console.log('Server listening on port ' + server.address().port)