update
This commit is contained in:
15
ts/dcrouter/classes.dcr.sz.connector.ts
Normal file
15
ts/dcrouter/classes.dcr.sz.connector.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import type DcRouter from './classes.dcrouter.js';
|
||||
|
||||
export class SzDcRouterConnector {
|
||||
public qenv: plugins.qenv.Qenv;
|
||||
public dcRouterRef: DcRouter;
|
||||
constructor(dcRouterRef: DcRouter) {
|
||||
this.dcRouterRef = dcRouterRef;
|
||||
this.dcRouterRef.options.platformServiceInstance?.serviceQenv || new plugins.qenv.Qenv('./', '.nogit/');
|
||||
}
|
||||
|
||||
public async getEnvVarOnDemand(varName: string): Promise<string> {
|
||||
return this.qenv.getEnvVarOnDemand(varName) || '';
|
||||
}
|
||||
}
|
@ -1,48 +1,136 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import * as paths from '../paths.js';
|
||||
import { SzDcRouterConnector } from './classes.dcr.sz.connector.js';
|
||||
|
||||
import type { SzPlatformService } from '../platformservice.js';
|
||||
import { type IMtaConfig, MtaService } from '../mta/classes.mta.js';
|
||||
|
||||
// Types are referenced via plugins.smartproxy.*
|
||||
|
||||
export interface IDcRouterOptions {
|
||||
portProxyConfig?: plugins.smartproxy.IPortProxySettings;
|
||||
platformServiceInstance?: SzPlatformService;
|
||||
|
||||
/** SmartProxy (TCP/SNI) configuration */
|
||||
smartProxyOptions?: plugins.smartproxy.ISmartProxyOptions;
|
||||
/** Reverse proxy host configurations for HTTP(S) layer */
|
||||
reverseProxyConfigs?: plugins.smartproxy.IReverseProxyConfig[];
|
||||
/** MTA (SMTP) service configuration */
|
||||
mtaConfig?: IMtaConfig;
|
||||
/** DNS server configuration */
|
||||
dnsServerConfig?: plugins.smartdns.IDnsServerOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* DcRouter can be run on ingress and egress to and from a datacenter site.
|
||||
*/
|
||||
/**
|
||||
* Context passed to HTTP routing rules
|
||||
*/
|
||||
/**
|
||||
* Context passed to port proxy (SmartProxy) routing rules
|
||||
*/
|
||||
export interface PortProxyRuleContext {
|
||||
proxy: plugins.smartproxy.SmartProxy;
|
||||
configs: plugins.smartproxy.IPortProxySettings['domainConfigs'];
|
||||
}
|
||||
export class DcRouter {
|
||||
private options: IDcRouterOptions;
|
||||
public smartProxy: plugins.smartproxy.SmartProxy;
|
||||
public mta: MtaService;
|
||||
public dnsServer: plugins.smartdns.DnsServer;
|
||||
public szDcRouterConnector = new SzDcRouterConnector(this);
|
||||
public options: IDcRouterOptions;
|
||||
public smartProxy?: plugins.smartproxy.SmartProxy;
|
||||
public mta?: MtaService;
|
||||
public dnsServer?: plugins.smartdns.DnsServer;
|
||||
/** SMTP rule engine */
|
||||
public smtpRuleEngine?: plugins.smartrule.SmartRule<any>;
|
||||
constructor(optionsArg: IDcRouterOptions) {
|
||||
this.options = optionsArg;
|
||||
if (this.options.portProxyConfig) {
|
||||
this.smartProxy = new plugins.smartproxy.SmartProxy(this.options.portProxyConfig);
|
||||
}
|
||||
if (this.options.mtaConfig) {
|
||||
this.mta = new MtaService(null, this.options.mtaConfig);
|
||||
}
|
||||
}
|
||||
|
||||
public async start() {
|
||||
|
||||
// TCP/SNI proxy (SmartProxy)
|
||||
if (this.options.smartProxyOptions) {
|
||||
// Lets setup smartacme
|
||||
let certProvisionFunction: plugins.smartproxy.ISmartProxyOptions['certProvisionFunction'];
|
||||
if (true) {
|
||||
const smartAcmeInstance = new plugins.smartacme.SmartAcme({
|
||||
accountEmail: this.options.smartProxyOptions.acme.accountEmail,
|
||||
certManager: new plugins.smartacme.certmanagers.MongoCertManager({
|
||||
mongoDbUrl: await this.szDcRouterConnector.getEnvVarOnDemand('MONGO_DB_URL'),
|
||||
mongoDbUser: await this.szDcRouterConnector.getEnvVarOnDemand('MONGO_DB_USER'),
|
||||
mongoDbPass: await this.szDcRouterConnector.getEnvVarOnDemand('MONGO_DB_PASS'),
|
||||
mongoDbName: await this.szDcRouterConnector.getEnvVarOnDemand('MONGO_DB_NAME'),
|
||||
}),
|
||||
environment: 'production',
|
||||
accountPrivateKey: await this.szDcRouterConnector.getEnvVarOnDemand('ACME_ACCOUNT_PRIVATE_KEY'),
|
||||
challengeHandlers: [
|
||||
new plugins.smartacme.handlers.Dns01Handler(new plugins.cloudflare.CloudflareAccount('')) // TODO
|
||||
],
|
||||
|
||||
});
|
||||
certProvisionFunction = async (domainArg) => {
|
||||
const domainSupported = await smartAcmeInstance.challengeHandlers[0].checkWetherDomainIsSupported(domainArg);
|
||||
if (!domainSupported) {
|
||||
return 'http01';
|
||||
}
|
||||
return smartAcmeInstance.getCertificateForDomain(domainArg);
|
||||
};
|
||||
}
|
||||
|
||||
this.smartProxy = new plugins.smartproxy.SmartProxy(this.options.smartProxyOptions);
|
||||
// Initialize SMTP rule engine from MTA service if available
|
||||
if (this.mta) {
|
||||
this.smtpRuleEngine = this.mta.smtpRuleEngine;
|
||||
}
|
||||
}
|
||||
// MTA service
|
||||
if (this.options.mtaConfig) {
|
||||
this.mta = new MtaService(null, this.options.mtaConfig);
|
||||
}
|
||||
// DNS server
|
||||
if (this.options.dnsServerConfig) {
|
||||
this.dnsServer = new plugins.smartdns.DnsServer(this.options.dnsServerConfig);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Start SmartProxy if configured
|
||||
if (this.smartProxy) {
|
||||
await this.smartProxy.start();
|
||||
}
|
||||
// Start MTA service if configured
|
||||
if (this.mta) {
|
||||
await this.mta.start();
|
||||
}
|
||||
// Start DNS server if configured
|
||||
if (this.dnsServer) {
|
||||
await this.dnsServer.start();
|
||||
}
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
// Stop SmartProxy
|
||||
if (this.smartProxy) {
|
||||
await this.smartProxy.stop();
|
||||
}
|
||||
// Stop MTA service
|
||||
if (this.mta) {
|
||||
await this.mta.stop();
|
||||
}
|
||||
// Stop DNS server
|
||||
if (this.dnsServer) {
|
||||
await this.dnsServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an SMTP routing rule
|
||||
*/
|
||||
public addSmtpRule(
|
||||
priority: number,
|
||||
check: (email: any) => Promise<any>,
|
||||
action: (email: any) => Promise<any>
|
||||
): void {
|
||||
this.smtpRuleEngine?.createRule(priority, check, action);
|
||||
}
|
||||
}
|
||||
|
||||
|
1
ts/dcrouter/index.ts
Normal file
1
ts/dcrouter/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './classes.dcrouter.js';
|
@ -19,7 +19,7 @@ export class ApiManager {
|
||||
*/
|
||||
private registerApiEndpoints() {
|
||||
// Register the SendEmail endpoint
|
||||
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IRequest_SendEmail>(
|
||||
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IReq_SendEmail>(
|
||||
new plugins.typedrequest.TypedHandler('sendEmail', async (requestData) => {
|
||||
const mailToSend = new plugins.smartmail.Smartmail({
|
||||
body: requestData.body,
|
||||
|
@ -27,7 +27,7 @@ export class MtaConnector {
|
||||
* @param options Additional options
|
||||
*/
|
||||
public async sendEmail(
|
||||
smartmail: plugins.smartmail.Smartmail<>,
|
||||
smartmail: plugins.smartmail.Smartmail<any>, // TODO: look at type
|
||||
toAddresses: string | string[],
|
||||
options: any = {}
|
||||
): Promise<string> {
|
||||
@ -82,7 +82,7 @@ export class MtaConnector {
|
||||
* For MTA, this would handle an email already received by the SMTP server
|
||||
* @param emailData The raw email data or identifier
|
||||
*/
|
||||
public async receiveEmail(emailData: string): Promise<plugins.smartmail.Smartmail<>> {
|
||||
public async receiveEmail(emailData: string): Promise<plugins.smartmail.Smartmail<any>> {
|
||||
try {
|
||||
// In a real implementation, this would retrieve an email from the MTA storage
|
||||
// For now, we can use a simplified approach:
|
||||
@ -97,7 +97,7 @@ export class MtaConnector {
|
||||
body: parsedEmail.html || parsedEmail.text || '',
|
||||
creationObjectRef: {
|
||||
From: parsedEmail.from?.text || '',
|
||||
To: parsedEmail.to?.text || '',
|
||||
To: parsedEmail.to,
|
||||
Subject: parsedEmail.subject || ''
|
||||
}
|
||||
});
|
||||
|
@ -168,6 +168,9 @@ export class MtaService {
|
||||
|
||||
/** Whether the service is currently running */
|
||||
private running = false;
|
||||
|
||||
/** SMTP rule engine for incoming emails */
|
||||
public smtpRuleEngine: plugins.smartrule.SmartRule<Email>;
|
||||
|
||||
/**
|
||||
* Initialize the MTA service
|
||||
@ -188,6 +191,8 @@ export class MtaService {
|
||||
this.dkimVerifier = new DKIMVerifier(this);
|
||||
this.dnsManager = new DNSManager(this);
|
||||
this.apiManager = new ApiManager();
|
||||
// Initialize SMTP rule engine
|
||||
this.smtpRuleEngine = new plugins.smartrule.SmartRule<Email>();
|
||||
|
||||
// Initialize stats
|
||||
this.stats = {
|
||||
@ -408,6 +413,12 @@ export class MtaService {
|
||||
throw new Error('MTA service is not running');
|
||||
}
|
||||
|
||||
// Apply SMTP rule engine decisions
|
||||
try {
|
||||
await this.smtpRuleEngine.makeDecision(email);
|
||||
} catch (err) {
|
||||
console.error('Error executing SMTP rules:', err);
|
||||
}
|
||||
try {
|
||||
console.log(`Processing incoming email from ${email.from} to ${email.to}`);
|
||||
|
||||
|
@ -301,7 +301,7 @@ export class SMTPServer {
|
||||
this.sessions.delete(socket);
|
||||
}
|
||||
|
||||
private processEmailData(socket: plugins.net.Socket | plugins.tls.TLSSocket, data: string): void {
|
||||
private async processEmailData(socket: plugins.net.Socket | plugins.tls.TLSSocket, data: string): Promise<void> {
|
||||
const session = this.sessions.get(socket);
|
||||
if (!session) return;
|
||||
|
||||
@ -384,8 +384,12 @@ export class SMTPServer {
|
||||
mightBeSpam: email.mightBeSpam
|
||||
});
|
||||
|
||||
// Process or forward the email as needed
|
||||
// this.mtaRef.processIncomingEmail(email); // You could add this method to your MTA service
|
||||
// Process or forward the email via MTA service
|
||||
try {
|
||||
await this.mtaRef.processIncomingEmail(email);
|
||||
} catch (err) {
|
||||
console.error('Error in MTA processing of incoming email:', err);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing email:', error);
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ export {
|
||||
// @push.rocks scope
|
||||
import * as projectinfo from '@push.rocks/projectinfo';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
import * as smartacme from '@push.rocks/smartacme';
|
||||
import * as smartdata from '@push.rocks/smartdata';
|
||||
import * as smartdns from '@push.rocks/smartdns';
|
||||
import * as smartfile from '@push.rocks/smartfile';
|
||||
@ -52,12 +53,14 @@ import * as smartrequest from '@push.rocks/smartrequest';
|
||||
import * as smartrule from '@push.rocks/smartrule';
|
||||
import * as smartrx from '@push.rocks/smartrx';
|
||||
|
||||
export { projectinfo, qenv, smartdata, smartdns, smartfile, smartlog, smartmail, smartpath, smartproxy, smartpromise, smartrequest, smartrule, smartrx };
|
||||
export { projectinfo, qenv, smartacme, smartdata, smartdns, smartfile, smartlog, smartmail, smartpath, smartproxy, smartpromise, smartrequest, smartrule, smartrx };
|
||||
|
||||
// apiclient.xyz scope
|
||||
import * as cloudflare from '@apiclient.xyz/cloudflare';
|
||||
import * as letterxpress from '@apiclient.xyz/letterxpress';
|
||||
|
||||
export {
|
||||
cloudflare,
|
||||
letterxpress,
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user