mirror of
https://github.com/openstf/stf
synced 2025-10-05 02:29:26 +02:00
Add HTTP API proxy.
This commit is contained in:
parent
cadc7a17d0
commit
8bc9592a79
2 changed files with 50 additions and 4 deletions
11
lib/cli.js
11
lib/cli.js
|
@ -3,6 +3,7 @@ var os = require('os')
|
|||
|
||||
var program = require('commander')
|
||||
var Promise = require('bluebird')
|
||||
var ip = require('my-local-ip')
|
||||
|
||||
var pkg = require('../package')
|
||||
var cliutil = require('./util/cliutil')
|
||||
|
@ -39,6 +40,10 @@ program
|
|||
, 'maximum port number for worker use'
|
||||
, Number
|
||||
, 7800)
|
||||
.option('--public-ip'
|
||||
, 'public ip for global access'
|
||||
, String
|
||||
, ip())
|
||||
.action(function() {
|
||||
var serials = cliutil.allUnknownArgs(arguments)
|
||||
, options = cliutil.lastArg(arguments)
|
||||
|
@ -67,6 +72,7 @@ program
|
|||
, '--connect-sub', options.connectSub.join(',')
|
||||
, '--connect-push', options.connectPush.join(',')
|
||||
, '--ports', ports.join(',')
|
||||
, '--public-ip', options.publicIp
|
||||
])
|
||||
}
|
||||
, endpoints: {
|
||||
|
@ -91,6 +97,10 @@ program
|
|||
.option('--ports <ports>'
|
||||
, 'ports allocated to worker'
|
||||
, cliutil.list)
|
||||
.option('--public-ip <ip>'
|
||||
, 'public ip for global access'
|
||||
, String
|
||||
, ip())
|
||||
.action(function(serial, options) {
|
||||
if (!options.connectSub) {
|
||||
this.missingArgument('--connect-sub')
|
||||
|
@ -109,6 +119,7 @@ program
|
|||
serial: serial
|
||||
, provider: options.provider
|
||||
, ports: options.ports
|
||||
, publicIp: options.publicIp
|
||||
, endpoints: {
|
||||
sub: options.connectSub
|
||||
, push: options.connectPush
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
var assert = require('assert')
|
||||
var util = require('util')
|
||||
var http = require('http')
|
||||
|
||||
var Promise = require('bluebird')
|
||||
var zmq = require('zmq')
|
||||
var adbkit = require('adbkit')
|
||||
var monkey = require('adbkit-monkey')
|
||||
var request = Promise.promisifyAll(require('request'))
|
||||
var httpProxy = require('http-proxy')
|
||||
|
||||
var logger = require('../util/logger')
|
||||
var wire = require('../wire')
|
||||
|
@ -159,6 +161,8 @@ module.exports = function(options) {
|
|||
.then(function(conn) {
|
||||
conn.end()
|
||||
var ours = options.ports.pop()
|
||||
, everyones = options.ports.pop()
|
||||
, url = util.format('http://127.0.0.1:%d', ours)
|
||||
return adb.forwardAsync(
|
||||
options.serial
|
||||
, util.format('tcp:%d', ours)
|
||||
|
@ -166,10 +170,7 @@ module.exports = function(options) {
|
|||
)
|
||||
.then(function() {
|
||||
return request.getAsync({
|
||||
url: util.format(
|
||||
'http://127.0.0.1:%d/api/v1/displays/0'
|
||||
, ours
|
||||
)
|
||||
url: util.format('%s/api/v1/displays/0', url)
|
||||
, json: true
|
||||
})
|
||||
})
|
||||
|
@ -177,6 +178,40 @@ module.exports = function(options) {
|
|||
assert.ok('id' in args[1], 'Invalid response from HTTP API')
|
||||
identity.display = args[1]
|
||||
})
|
||||
.then(function() {
|
||||
var resolver = Promise.defer()
|
||||
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: url
|
||||
, ws: false
|
||||
, xfwd: false
|
||||
})
|
||||
|
||||
var proxyServer = http.createServer(proxy.web)
|
||||
.listen(everyones)
|
||||
|
||||
proxyServer.on('listening', resolver.resolve)
|
||||
proxyServer.on('error', resolver.reject)
|
||||
|
||||
return resolver.promise.finally(function() {
|
||||
proxyServer.removeListener('listening', resolver.resolve)
|
||||
proxyServer.removeListener('error', resolver.reject)
|
||||
vitals.add('device:http:proxy', proxyServer)
|
||||
})
|
||||
})
|
||||
.then(function() {
|
||||
log.info(
|
||||
'HTTP API proxy running on "http://%s:%s"'
|
||||
, options.publicIp
|
||||
, everyones
|
||||
)
|
||||
|
||||
identity.display.url = util.format(
|
||||
'http://%s:%s/api/v1/displays/0/screenshot.jpg'
|
||||
, options.publicIp
|
||||
, everyones
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue