smartproxy/ts/smartproxy.portproxy.ts

33 lines
806 B
TypeScript
Raw Permalink Normal View History

2019-08-22 13:09:48 +00:00
import * as plugins from './smartproxy.plugins';
import { expose } from '@pushrocks/smartspawn';
import * as net from 'net';
2020-02-07 12:43:37 +00:00
let netServer: plugins.net.Server;
2019-08-22 13:09:48 +00:00
const portProxyCalls = {
2020-02-07 12:43:37 +00:00
start: async (portArg = 8000) => {
netServer = net
2020-02-07 13:04:11 +00:00
.createServer(from => {
const to = net.createConnection({
host: 'localhost',
port: 8001
});
from.pipe(to);
to.pipe(from);
})
.listen(portArg);
console.log(`PortProxy -> OK: Now listening on port ${portArg}`);
2020-02-07 12:43:37 +00:00
},
2019-08-22 13:09:48 +00:00
stop: async () => {
const done = plugins.smartpromise.defer();
2020-02-07 12:43:37 +00:00
netServer.close(() => {
2019-08-22 13:09:48 +00:00
done.resolve();
});
await done.promise;
}
};
export type TPortProxyCalls = typeof portProxyCalls;
expose(portProxyCalls);
2019-08-22 14:14:50 +00:00
console.log('PortProxy Initialized');