refactor: migrate coretraffic to current smartproxy
This commit is contained in:
@@ -6,9 +6,9 @@ import { CoreTraffic } from './coretraffic.classes.coretraffic.js';
|
||||
* Coreflow Connector
|
||||
*/
|
||||
export class CoreflowConnector {
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
public typedrouter: plugins.typedrequest.TypedRouter = new plugins.typedrequest.TypedRouter();
|
||||
public coretrafficRef: CoreTraffic;
|
||||
public typesocketClient: plugins.typedsocket.TypedSocket;
|
||||
public typesocketClient!: plugins.typedsocket.TypedSocket;
|
||||
|
||||
constructor(coretrafficRefArg: CoreTraffic) {
|
||||
this.coretrafficRef = coretrafficRefArg;
|
||||
@@ -32,9 +32,9 @@ export class CoreflowConnector {
|
||||
public async start() {
|
||||
this.typesocketClient = await plugins.typedsocket.TypedSocket.createClient(
|
||||
this.typedrouter,
|
||||
'http://coreflow:3000',
|
||||
'coretraffic'
|
||||
'http://coreflow:3000'
|
||||
);
|
||||
await this.typesocketClient.setTag('coretraffic', undefined);
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
|
||||
@@ -9,31 +9,26 @@ export interface ICoretrafficConfig {
|
||||
}
|
||||
|
||||
export class CoreTraffic {
|
||||
public projectinfo = new plugins.projectinfo.ProjectinfoNpm(paths.packageDir);
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
public projectinfo!: plugins.projectinfo.ProjectinfoNpm;
|
||||
public typedrouter: plugins.typedrequest.TypedRouter = new plugins.typedrequest.TypedRouter();
|
||||
public coreflowConnector: CoreflowConnector;
|
||||
public taskmanager: CoretrafficTaskManager;
|
||||
public networkProxy: plugins.smartproxy.NetworkProxy;
|
||||
public sslRedirect: plugins.smartproxy.SslRedirect;
|
||||
public smartProxy: plugins.smartproxy.SmartProxy;
|
||||
|
||||
constructor() {
|
||||
this.coreflowConnector = new CoreflowConnector(this);
|
||||
this.taskmanager = new CoretrafficTaskManager(this);
|
||||
this.networkProxy = new plugins.smartproxy.NetworkProxy({
|
||||
port: 8000
|
||||
this.smartProxy = new plugins.smartproxy.SmartProxy({
|
||||
routes: [],
|
||||
});
|
||||
this.sslRedirect = new plugins.smartproxy.SslRedirect(7999);
|
||||
}
|
||||
|
||||
/**
|
||||
* starts coretraffic
|
||||
*/
|
||||
public async start() {
|
||||
await this.networkProxy.start();
|
||||
await this.sslRedirect.start();
|
||||
this.networkProxy.addDefaultHeaders({
|
||||
servezone_coretraffic_version: this.projectinfo.version
|
||||
})
|
||||
this.projectinfo = await plugins.projectinfo.ProjectinfoNpm.create(paths.packageDir);
|
||||
await this.smartProxy.start();
|
||||
|
||||
await this.taskmanager.start();
|
||||
await this.coreflowConnector.start();
|
||||
@@ -47,9 +42,13 @@ export class CoreTraffic {
|
||||
console.log('stopped taskmanager');
|
||||
await this.coreflowConnector.stop();
|
||||
console.log('stopped coreflowConnector');
|
||||
await this.networkProxy.stop();
|
||||
await this.smartProxy.stop();
|
||||
console.log('stopped smartproxy!');
|
||||
await this.sslRedirect.stop();
|
||||
|
||||
}
|
||||
|
||||
public getDefaultResponseHeaders() {
|
||||
return {
|
||||
servezone_coretraffic_version: this.projectinfo?.version || 'unknown',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,13 +9,14 @@ export class CoretrafficTaskManager {
|
||||
/**
|
||||
* a task to run setup routing, runs buffered
|
||||
*/
|
||||
public setupRoutingTask: plugins.taskbuffer.Task<plugins.tsclass.network.IReverseProxyConfig[]>;
|
||||
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[]) => {
|
||||
@@ -24,11 +25,78 @@ export class CoretrafficTaskManager {
|
||||
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.networkProxy.updateProxyConfigs(reverseConfigs);
|
||||
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() {}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import * as plugins from './coretraffic.plugins.js';
|
||||
import * as paths from './coretraffic.paths.js';
|
||||
|
||||
const projectInfoNpm = new plugins.projectinfo.ProjectinfoNpm(paths.packageDir);
|
||||
|
||||
export const logger = new plugins.smartlog.Smartlog({
|
||||
logContext: {
|
||||
|
||||
+1
-2
@@ -6,11 +6,10 @@ import { logger } from './coretraffic.logging.js';
|
||||
import { CoreTraffic } from './coretraffic.classes.coretraffic.js';
|
||||
export { CoreTraffic };
|
||||
|
||||
const projectinfo = new plugins.projectinfo.ProjectInfo(paths.packageDir);
|
||||
|
||||
let coretrafficInstance: CoreTraffic;
|
||||
|
||||
export const runCli = async () => {
|
||||
const projectinfo = await plugins.projectinfo.ProjectInfo.create(paths.packageDir);
|
||||
logger.log('info', `coretraffic@v${projectinfo.npm.version}`);
|
||||
coretrafficInstance = new CoreTraffic();
|
||||
await coretrafficInstance.start();
|
||||
|
||||
Reference in New Issue
Block a user