前端代码
This commit is contained in:
23
node_modules/localtunnel/lib/HeaderHostTransformer.js
generated
vendored
Normal file
23
node_modules/localtunnel/lib/HeaderHostTransformer.js
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
const { Transform } = require('stream');
|
||||
|
||||
class HeaderHostTransformer extends Transform {
|
||||
constructor(opts = {}) {
|
||||
super(opts);
|
||||
this.host = opts.host || 'localhost';
|
||||
this.replaced = false;
|
||||
}
|
||||
|
||||
_transform(data, encoding, callback) {
|
||||
callback(
|
||||
null,
|
||||
this.replaced // after replacing the first instance of the Host header we just become a regular passthrough
|
||||
? data
|
||||
: data.toString().replace(/(\r\n[Hh]ost: )\S+/, (match, $1) => {
|
||||
this.replaced = true;
|
||||
return $1 + this.host;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HeaderHostTransformer;
|
163
node_modules/localtunnel/lib/Tunnel.js
generated
vendored
Normal file
163
node_modules/localtunnel/lib/Tunnel.js
generated
vendored
Normal file
@ -0,0 +1,163 @@
|
||||
/* eslint-disable consistent-return, no-underscore-dangle */
|
||||
|
||||
const { parse } = require('url');
|
||||
const { EventEmitter } = require('events');
|
||||
const axios = require('axios');
|
||||
const debug = require('debug')('localtunnel:client');
|
||||
|
||||
const TunnelCluster = require('./TunnelCluster');
|
||||
|
||||
module.exports = class Tunnel extends EventEmitter {
|
||||
constructor(opts = {}) {
|
||||
super(opts);
|
||||
this.opts = opts;
|
||||
this.closed = false;
|
||||
if (!this.opts.host) {
|
||||
this.opts.host = 'https://localtunnel.me';
|
||||
}
|
||||
}
|
||||
|
||||
_getInfo(body) {
|
||||
/* eslint-disable camelcase */
|
||||
const { id, ip, port, url, cached_url, max_conn_count } = body;
|
||||
const { host, port: local_port, local_host } = this.opts;
|
||||
const { local_https, local_cert, local_key, local_ca, allow_invalid_cert } = this.opts;
|
||||
return {
|
||||
name: id,
|
||||
url,
|
||||
cached_url,
|
||||
max_conn: max_conn_count || 1,
|
||||
remote_host: parse(host).hostname,
|
||||
remote_ip: ip,
|
||||
remote_port: port,
|
||||
local_port,
|
||||
local_host,
|
||||
local_https,
|
||||
local_cert,
|
||||
local_key,
|
||||
local_ca,
|
||||
allow_invalid_cert,
|
||||
};
|
||||
/* eslint-enable camelcase */
|
||||
}
|
||||
|
||||
// initialize connection
|
||||
// callback with connection info
|
||||
_init(cb) {
|
||||
const opt = this.opts;
|
||||
const getInfo = this._getInfo.bind(this);
|
||||
|
||||
const params = {
|
||||
responseType: 'json',
|
||||
};
|
||||
|
||||
const baseUri = `${opt.host}/`;
|
||||
// no subdomain at first, maybe use requested domain
|
||||
const assignedDomain = opt.subdomain;
|
||||
// where to quest
|
||||
const uri = baseUri + (assignedDomain || '?new');
|
||||
|
||||
(function getUrl() {
|
||||
axios
|
||||
.get(uri, params)
|
||||
.then(res => {
|
||||
const body = res.data;
|
||||
debug('got tunnel information', res.data);
|
||||
if (res.status !== 200) {
|
||||
const err = new Error(
|
||||
(body && body.message) || 'localtunnel server returned an error, please try again'
|
||||
);
|
||||
return cb(err);
|
||||
}
|
||||
cb(null, getInfo(body));
|
||||
})
|
||||
.catch(err => {
|
||||
debug(`tunnel server offline: ${err.message}, retry 1s`);
|
||||
return setTimeout(getUrl, 1000);
|
||||
});
|
||||
})();
|
||||
}
|
||||
|
||||
_establish(info) {
|
||||
// increase max event listeners so that localtunnel consumers don't get
|
||||
// warning messages as soon as they setup even one listener. See #71
|
||||
this.setMaxListeners(info.max_conn + (EventEmitter.defaultMaxListeners || 10));
|
||||
|
||||
this.tunnelCluster = new TunnelCluster(info);
|
||||
|
||||
// only emit the url the first time
|
||||
this.tunnelCluster.once('open', () => {
|
||||
this.emit('url', info.url);
|
||||
});
|
||||
|
||||
// re-emit socket error
|
||||
this.tunnelCluster.on('error', err => {
|
||||
debug('got socket error', err.message);
|
||||
this.emit('error', err);
|
||||
});
|
||||
|
||||
let tunnelCount = 0;
|
||||
|
||||
// track open count
|
||||
this.tunnelCluster.on('open', tunnel => {
|
||||
tunnelCount++;
|
||||
debug('tunnel open [total: %d]', tunnelCount);
|
||||
|
||||
const closeHandler = () => {
|
||||
tunnel.destroy();
|
||||
};
|
||||
|
||||
if (this.closed) {
|
||||
return closeHandler();
|
||||
}
|
||||
|
||||
this.once('close', closeHandler);
|
||||
tunnel.once('close', () => {
|
||||
this.removeListener('close', closeHandler);
|
||||
});
|
||||
});
|
||||
|
||||
// when a tunnel dies, open a new one
|
||||
this.tunnelCluster.on('dead', () => {
|
||||
tunnelCount--;
|
||||
debug('tunnel dead [total: %d]', tunnelCount);
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
this.tunnelCluster.open();
|
||||
});
|
||||
|
||||
this.tunnelCluster.on('request', req => {
|
||||
this.emit('request', req);
|
||||
});
|
||||
|
||||
// establish as many tunnels as allowed
|
||||
for (let count = 0; count < info.max_conn; ++count) {
|
||||
this.tunnelCluster.open();
|
||||
}
|
||||
}
|
||||
|
||||
open(cb) {
|
||||
this._init((err, info) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
this.clientId = info.name;
|
||||
this.url = info.url;
|
||||
|
||||
// `cached_url` is only returned by proxy servers that support resource caching.
|
||||
if (info.cached_url) {
|
||||
this.cachedUrl = info.cached_url;
|
||||
}
|
||||
|
||||
this._establish(info);
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
close() {
|
||||
this.closed = true;
|
||||
this.emit('close');
|
||||
}
|
||||
};
|
152
node_modules/localtunnel/lib/TunnelCluster.js
generated
vendored
Normal file
152
node_modules/localtunnel/lib/TunnelCluster.js
generated
vendored
Normal file
@ -0,0 +1,152 @@
|
||||
const { EventEmitter } = require('events');
|
||||
const debug = require('debug')('localtunnel:client');
|
||||
const fs = require('fs');
|
||||
const net = require('net');
|
||||
const tls = require('tls');
|
||||
|
||||
const HeaderHostTransformer = require('./HeaderHostTransformer');
|
||||
|
||||
// manages groups of tunnels
|
||||
module.exports = class TunnelCluster extends EventEmitter {
|
||||
constructor(opts = {}) {
|
||||
super(opts);
|
||||
this.opts = opts;
|
||||
}
|
||||
|
||||
open() {
|
||||
const opt = this.opts;
|
||||
|
||||
// Prefer IP if returned by the server
|
||||
const remoteHostOrIp = opt.remote_ip || opt.remote_host;
|
||||
const remotePort = opt.remote_port;
|
||||
const localHost = opt.local_host || 'localhost';
|
||||
const localPort = opt.local_port;
|
||||
const localProtocol = opt.local_https ? 'https' : 'http';
|
||||
const allowInvalidCert = opt.allow_invalid_cert;
|
||||
|
||||
debug(
|
||||
'establishing tunnel %s://%s:%s <> %s:%s',
|
||||
localProtocol,
|
||||
localHost,
|
||||
localPort,
|
||||
remoteHostOrIp,
|
||||
remotePort
|
||||
);
|
||||
|
||||
// connection to localtunnel server
|
||||
const remote = net.connect({
|
||||
host: remoteHostOrIp,
|
||||
port: remotePort,
|
||||
});
|
||||
|
||||
remote.setKeepAlive(true);
|
||||
|
||||
remote.on('error', err => {
|
||||
debug('got remote connection error', err.message);
|
||||
|
||||
// emit connection refused errors immediately, because they
|
||||
// indicate that the tunnel can't be established.
|
||||
if (err.code === 'ECONNREFUSED') {
|
||||
this.emit(
|
||||
'error',
|
||||
new Error(
|
||||
`connection refused: ${remoteHostOrIp}:${remotePort} (check your firewall settings)`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
remote.end();
|
||||
});
|
||||
|
||||
const connLocal = () => {
|
||||
if (remote.destroyed) {
|
||||
debug('remote destroyed');
|
||||
this.emit('dead');
|
||||
return;
|
||||
}
|
||||
|
||||
debug('connecting locally to %s://%s:%d', localProtocol, localHost, localPort);
|
||||
remote.pause();
|
||||
|
||||
if (allowInvalidCert) {
|
||||
debug('allowing invalid certificates');
|
||||
}
|
||||
|
||||
const getLocalCertOpts = () =>
|
||||
allowInvalidCert
|
||||
? { rejectUnauthorized: false }
|
||||
: {
|
||||
cert: fs.readFileSync(opt.local_cert),
|
||||
key: fs.readFileSync(opt.local_key),
|
||||
ca: opt.local_ca ? [fs.readFileSync(opt.local_ca)] : undefined,
|
||||
};
|
||||
|
||||
// connection to local http server
|
||||
const local = opt.local_https
|
||||
? tls.connect({ host: localHost, port: localPort, ...getLocalCertOpts() })
|
||||
: net.connect({ host: localHost, port: localPort });
|
||||
|
||||
const remoteClose = () => {
|
||||
debug('remote close');
|
||||
this.emit('dead');
|
||||
local.end();
|
||||
};
|
||||
|
||||
remote.once('close', remoteClose);
|
||||
|
||||
// TODO some languages have single threaded servers which makes opening up
|
||||
// multiple local connections impossible. We need a smarter way to scale
|
||||
// and adjust for such instances to avoid beating on the door of the server
|
||||
local.once('error', err => {
|
||||
debug('local error %s', err.message);
|
||||
local.end();
|
||||
|
||||
remote.removeListener('close', remoteClose);
|
||||
|
||||
if (err.code !== 'ECONNREFUSED') {
|
||||
return remote.end();
|
||||
}
|
||||
|
||||
// retrying connection to local server
|
||||
setTimeout(connLocal, 1000);
|
||||
});
|
||||
|
||||
local.once('connect', () => {
|
||||
debug('connected locally');
|
||||
remote.resume();
|
||||
|
||||
let stream = remote;
|
||||
|
||||
// if user requested specific local host
|
||||
// then we use host header transform to replace the host header
|
||||
if (opt.local_host) {
|
||||
debug('transform Host header to %s', opt.local_host);
|
||||
stream = remote.pipe(new HeaderHostTransformer({ host: opt.local_host }));
|
||||
}
|
||||
|
||||
stream.pipe(local).pipe(remote);
|
||||
|
||||
// when local closes, also get a new remote
|
||||
local.once('close', hadError => {
|
||||
debug('local connection closed [%s]', hadError);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
remote.on('data', data => {
|
||||
const match = data.toString().match(/^(\w+) (\S+)/);
|
||||
if (match) {
|
||||
this.emit('request', {
|
||||
method: match[1],
|
||||
path: match[2],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// tunnel is considered open when remote connects
|
||||
remote.once('connect', () => {
|
||||
this.emit('open', remote);
|
||||
connLocal();
|
||||
});
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user