From 8b68ea90574e16a3290fecd5d9cf8f7a3474d586 Mon Sep 17 00:00:00 2001 From: Joonas Trussmann Date: Sat, 10 Sep 2022 03:46:56 +0300 Subject: [PATCH] Removed proxy command, it depended on old adbkit. --- margerine.js | 13 ------ src/proxy.js | 115 --------------------------------------------------- 2 files changed, 128 deletions(-) delete mode 100644 src/proxy.js diff --git a/margerine.js b/margerine.js index 3b581ee..ba711b2 100644 --- a/margerine.js +++ b/margerine.js @@ -4,9 +4,6 @@ const { SerialPort } = require('serialport') const yargs = require('yargs') const chalk = require('chalk') -const proxyListen = require("./src/proxy").listen - - /* we use Sentry to help debug in case of errors in the obfuscated build and basic analytics */ const Sentry = require("@sentry/node"); const Tracing = require("@sentry/tracing"); @@ -78,16 +75,6 @@ const argv = yargs }) }) -}) -.command('proxy [port]', 'start the built in http -> https proxy', (yargs) => { - return yargs - .positional('port', { - describe: 'port to start on' - }) - }, (argv) => { - const port = argv.port ? argv.port : constants.defaultProxyPort - return proxyListen(port) - }) .command('shell [port]', 'execute a command on rooted device, once per reboot', (yargs) => { return yargs diff --git a/src/proxy.js b/src/proxy.js deleted file mode 100644 index 87db91a..0000000 --- a/src/proxy.js +++ /dev/null @@ -1,115 +0,0 @@ - const http = require('http'); - const https = require('https') - var url = require('url') - - const Adb = require('@devicefarmer/adbkit') - - - var verbosity = 0 - - const server = http.createServer(function(request, response) { - var url = request.url - const options = { - followAllRedirects: true, - method: request.method, - headers: request.headers, - rejectUnauthorized: false - } - var protocol = http - if(request.url.startsWith("http://") && !request.url.startsWith("http://repo.localdev.me")) { - url = "https://"+request.url.substring("http://".length) - protocol = https - } - if(request.url.startsWith("http://repo.fpv.wtf/http%3a//")) { - url = "https://"+request.url.substring("http://repo.fpv.wtf/http%3a//".length) - protocol = https - } - //sure, this could've been one less if with a regex - //but would it really have been better? - if(request.url.startsWith("http://repo.fpv.wtf/https%3a//")) { - url = "https://"+request.url.substring("http://repo.fpv.wtf/https%3a//".length) - protocol = https - } - - if(verbosity) - console.log("proxy serving request to "+url, options) - - const proxy_request = protocol.request(url, options, (res) => { - if(verbosity) - console.log("got response "+res.statusCode) - - res.on('data', function(chunk) { - //console.log("got response data") - response.write(chunk, 'binary'); - }); - res.on('end', function() { - if(verbosity) - console.log("response ended") - response.end(); - }); - res.on('error', error => { - console.error(error) - }) - if(res.statusCode === 301 || res.statusCode === 302) { - if(res.headers.location.startsWith("https://")) { - res.headers.location = "http://"+res.headers.location.substring("https://".length) - } - } - if(verbosity) - console.log("responding with", res.statusCode, res.headers) - response.writeHead(res.statusCode, res.headers); - - }); - - proxy_request.on('error', error => { - console.error(error) - }) - - request.on('data', function(chunk) { - proxy_request.write(chunk, 'binary'); - }) - request.on('end', function() { - if(verbosity) - console.log("request end") - proxy_request.end(); - }) -}) - -module.exports.listen = (port) => { - server.on('error', (e) => { - if (e.code === 'EADDRINUSE') { - console.log('proxy port in use, presuming other proxy process running'); - } - else { - throw e - } - }); - server.listen(port, '0.0.0.0', () => { - console.log("proxy listening on port: "+port) - var client = Adb.createClient({host:"127.0.0.1"}) - client.trackDevices() - .then(function(tracker) { - tracker.on('add', function(device) { - console.log('Device %s was plugged in', device.id) - client.reverse(device.id, "tcp:8089", "tcp:"+port).then(()=> { - console.log("reverse port forward suceeded") - }) - }) - - tracker.on('remove', function(device) { - console.log('Device %s was unplugged', device.id) - }) - tracker.on('end', function() { - console.log('Tracking stopped') - }) - }) - .catch(function(err) { - console.error('Something went wrong:', err.stack) - }) - }) - -} - -module.exports.setVerbosity = (_verbosity) => { - verbosity = _verbosity -} \ No newline at end of file