smartproxy/ts/smartproxy.classes.smartproxy.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-08-20 16:42:52 +00:00
import * as plugins from './smartproxy.plugins';
import * as interfaces from './interfaces';
import { SmartproxyRouter } from './smartproxy.classes.router';
export class SmartProxy {
public expressInstance: plugins.express.Express;
2019-08-20 20:26:44 +00:00
public httpsServer: plugins.https.Server | plugins.http.Server;
2019-08-20 16:42:52 +00:00
public router = new SmartproxyRouter();
public hostCandidates: interfaces.IHostConfig[] = [];
public addHostCandidate(hostCandidate: interfaces.IHostConfig) {
// TODO search for old hostCandidates with that target
this.hostCandidates.push(hostCandidate);
2019-08-20 16:43:15 +00:00
}
2019-08-20 16:42:52 +00:00
/**
* starts the proxyInstance
*/
public async start() {
this.expressInstance = plugins.express();
2019-08-20 20:26:44 +00:00
this.httpsServer = plugins.http.createServer(this.expressInstance);
2019-08-20 17:02:13 +00:00
for (const hostCandidate of this.hostCandidates) {
2019-08-20 20:26:44 +00:00
/* this.httpsServer.addContext(hostCandidate.hostName, {
2019-08-20 17:02:13 +00:00
cert: hostCandidate.publicKey,
key: hostCandidate.privateKey
2019-08-20 20:26:44 +00:00
}); */
2019-08-20 17:02:13 +00:00
}
// proxy middleware options
const proxyOptions: plugins.httpProxyMiddleware.Config = {
2019-08-20 20:26:44 +00:00
target: 'https://nullresolve.lossless.one',
2019-08-20 17:02:13 +00:00
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
router: (req: plugins.express.Request) => {
return this.router.routeReq(req);
}
};
this.expressInstance.use(plugins.httpProxyMiddleware(proxyOptions));
2019-08-20 20:26:44 +00:00
this.httpsServer.listen(3000);
2019-08-20 17:02:13 +00:00
2019-08-20 16:42:52 +00:00
}
public async update() {
await this.start();
}
}