Files
coretraffic/ts/coretraffic.classes.taskmanager.ts
T

104 lines
3.1 KiB
TypeScript

import * as plugins from './coretraffic.plugins.js';
import { CoreTraffic } from './coretraffic.classes.coretraffic.js';
import { logger } from './coretraffic.logging.js';
export class CoretrafficTaskManager {
public coretrafficRef: CoreTraffic;
public taskmanager: plugins.taskbuffer.TaskManager;
/**
* a task to run setup routing, runs buffered
*/
public setupRoutingTask: plugins.taskbuffer.Task;
constructor(coretrafficRefArg: CoreTraffic) {
this.coretrafficRef = coretrafficRefArg;
this.taskmanager = new plugins.taskbuffer.TaskManager();
this.setupRoutingTask = new plugins.taskbuffer.Task({
name: 'setupRouting',
buffered: true,
bufferMax: 2,
taskFunction: async (reverseConfigs: plugins.tsclass.network.IReverseProxyConfig[]) => {
console.log('this is what got to the task:');
console.log(reverseConfigs);
logger.log('info', `routing setup task triggered`);
logger.log('info', `Found ${reverseConfigs.length} host reverse configs!`);
logger.log('info', `trying to deploy host candidates now`);
await this.coretrafficRef.smartProxy.updateRoutes(this.createRoutesFromReverseConfigs(reverseConfigs));
},
});
}
private createRoutesFromReverseConfigs(
reverseConfigs: plugins.tsclass.network.IReverseProxyConfig[]
): plugins.smartproxy.IRouteConfig[] {
const responseHeaders = this.coretrafficRef.getDefaultResponseHeaders();
const routes: plugins.smartproxy.IRouteConfig[] = [
{
name: 'http-to-https-redirect',
match: {
ports: 7999,
protocol: 'http',
},
action: {
type: 'socket-handler',
socketHandler: plugins.smartproxy.SocketHandlers.httpRedirect('https://{domain}{path}', 301),
},
headers: {
response: responseHeaders,
},
},
];
for (const reverseConfig of reverseConfigs) {
routes.push({
name: `https-${reverseConfig.hostName}`,
match: {
ports: 8000,
domains: reverseConfig.hostName,
protocol: 'http',
},
action: {
type: 'forward',
targets: reverseConfig.destinationIps.flatMap((destinationIp) =>
reverseConfig.destinationPorts.map((destinationPort) => ({
host: destinationIp,
port: destinationPort,
}))
),
tls: {
mode: 'terminate',
certificate: {
key: reverseConfig.privateKey,
cert: reverseConfig.publicKey,
},
},
},
headers: {
response: responseHeaders,
},
security: reverseConfig.authentication
? {
basicAuth: {
enabled: true,
users: [
{
username: reverseConfig.authentication.user,
password: reverseConfig.authentication.pass,
},
],
},
}
: undefined,
});
}
return routes;
}
public async start() {}
public async stop() {}
}