2024-05-15 10:10:41 +02:00
|
|
|
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
|
|
|
|
|
*/
|
2026-04-28 12:02:58 +00:00
|
|
|
public setupRoutingTask: plugins.taskbuffer.Task;
|
2024-05-15 10:10:41 +02:00
|
|
|
|
|
|
|
|
constructor(coretrafficRefArg: CoreTraffic) {
|
|
|
|
|
this.coretrafficRef = coretrafficRefArg;
|
|
|
|
|
this.taskmanager = new plugins.taskbuffer.TaskManager();
|
|
|
|
|
|
|
|
|
|
this.setupRoutingTask = new plugins.taskbuffer.Task({
|
2026-04-28 12:02:58 +00:00
|
|
|
name: 'setupRouting',
|
2024-05-15 10:10:41 +02:00
|
|
|
buffered: true,
|
|
|
|
|
bufferMax: 2,
|
2024-12-29 22:51:34 +01:00
|
|
|
taskFunction: async (reverseConfigs: plugins.tsclass.network.IReverseProxyConfig[]) => {
|
2024-05-15 10:10:41 +02:00
|
|
|
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`);
|
2026-04-28 12:02:58 +00:00
|
|
|
await this.coretrafficRef.smartProxy.updateRoutes(this.createRoutesFromReverseConfigs(reverseConfigs));
|
2024-05-15 10:10:41 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 12:02:58 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-15 10:10:41 +02:00
|
|
|
public async start() {}
|
|
|
|
|
|
|
|
|
|
public async stop() {}
|
|
|
|
|
}
|