Compare commits

...

6 Commits

30 changed files with 1808 additions and 1280 deletions

View File

@ -1,5 +1,32 @@
# Changelog
## 2025-05-04 - 2.3.1 - fix(platformservice)
Update dependency versions and refactor import paths for improved compatibility; add initial DcRouter plan documentation.
- Upgrade @git.zone/tsbuild to ^2.3.2 and @push.rocks/tapbundle to ^6.0.3.
- Upgrade @api.global/typedserver to ^3.0.74 and update related API dependencies (cloudflare, letterxpress).
- Upgrade smartdata to ^5.15.1, add smartdns (^6.2.2), upgrade smartproxy to ^10.0.2, smartrequest to ^2.1.0, smartrule to ^2.0.1, and smartrx to ^3.0.10.
- Upgrade @serve.zone/interfaces to ^5.0.4 and @tsclass/tsclass to ^9.1.0; update mailauth to ^4.8.4.
- Add packageManager field in package.json for PNPM configuration.
- Add readme.plan.md detailing the DcRouter implementation plan.
- Refactor import paths in several TS files (e.g. ts/plugins.ts, ts/mta classes) for consistency.
## 2025-03-15 - 2.3.0 - feat(platformservice)
Add AIBridge module and refactor service file paths for improved module organization
- Added new AIBridge class in ts/aibridge/classes.aibridge.ts.
- Renamed letter service file from ts/letter/letterservice.ts to ts/letter/classes.letterservice.ts and updated its index.
- Updated platformservice.ts to import letter and SMS services from new paths.
- Renamed SMS service file from ts/sms/smsservice.ts to ts/sms/classes.smsservice.ts and updated its index accordingly.
## 2025-03-15 - 2.2.1 - fix(platformservice)
Refactor module structure to update import paths and file organization
- Removed obsolete file 'ts/classes.platformservice.ts' and updated references to use 'ts/platformservice.ts'.
- Updated import paths in PlatformServiceDb, EmailService, and other modules to use new file structure.
- Renamed and moved files in the email, mta, letter, and sms directories to align with new module layout.
- Fixed references to external modules (e.g. '@serve.zone/interfaces', '@push.rocks/*', etc.) to reflect the updated paths.
## 2025-03-15 - 2.2.0 - feat(plugins)
Add smartproxy support by including the @push.rocks/smartproxy dependency and exporting it in the plugins module.

View File

@ -1,7 +1,7 @@
{
"name": "@serve.zone/platformservice",
"private": true,
"version": "2.2.0",
"version": "2.3.1",
"description": "A multifaceted platform service handling mail, SMS, letter delivery, and AI services.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
@ -16,35 +16,36 @@
"localPublish": ""
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.17",
"@git.zone/tsbuild": "^2.3.2",
"@git.zone/tsrun": "^1.2.8",
"@git.zone/tstest": "^1.0.88",
"@git.zone/tswatch": "^2.0.1",
"@push.rocks/tapbundle": "^5.0.22"
"@push.rocks/tapbundle": "^6.0.3"
},
"dependencies": {
"@api.global/typedrequest": "^3.0.19",
"@api.global/typedserver": "^3.0.27",
"@api.global/typedserver": "^3.0.74",
"@api.global/typedsocket": "^3.0.0",
"@apiclient.xyz/cloudflare": "^6.0.3",
"@apiclient.xyz/letterxpress": "^1.0.20",
"@apiclient.xyz/cloudflare": "^6.4.1",
"@apiclient.xyz/letterxpress": "^1.0.22",
"@push.rocks/projectinfo": "^5.0.1",
"@push.rocks/qenv": "^6.0.5",
"@push.rocks/smartdata": "^5.0.7",
"@push.rocks/smartdata": "^5.15.1",
"@push.rocks/smartdns": "^6.2.2",
"@push.rocks/smartfile": "^11.0.4",
"@push.rocks/smartlog": "^3.0.3",
"@push.rocks/smartmail": "^1.0.24",
"@push.rocks/smartpath": "^5.0.5",
"@push.rocks/smartpromise": "^4.0.3",
"@push.rocks/smartproxy": "^4.1.0",
"@push.rocks/smartrequest": "^2.0.21",
"@push.rocks/smartproxy": "^10.0.2",
"@push.rocks/smartrequest": "^2.1.0",
"@push.rocks/smartrule": "^2.0.1",
"@push.rocks/smartrx": "^3.0.7",
"@push.rocks/smartrx": "^3.0.10",
"@push.rocks/smartstate": "^2.0.0",
"@serve.zone/interfaces": "^4.12.1",
"@tsclass/tsclass": "^5.0.0",
"@serve.zone/interfaces": "^5.0.4",
"@tsclass/tsclass": "^9.1.0",
"@types/mailparser": "^3.4.5",
"mailauth": "^4.6.5",
"mailauth": "^4.8.4",
"mailparser": "^3.6.9",
"uuid": "^11.1.0"
},
@ -76,5 +77,6 @@
"mongodb-memory-server",
"puppeteer"
]
}
},
"packageManager": "pnpm@10.7.0+sha512.6b865ad4b62a1d9842b61d674a393903b871d9244954f652b8842c2b553c72176b278f64c463e52d40fff8aba385c235c8c9ecf5cc7de4fd78b8bb6d49633ab6"
}

2786
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

91
readme.plan.md Normal file
View File

@ -0,0 +1,91 @@
# DcRouter Implementation Plan
This document outlines the plan for developing **DcRouter**, a central routing
component for the platform service. DcRouter will provide:
- HTTP/HTTPS reverse proxy with TLS (ACME) support
- TCP/SNI-based proxying
- Mail Transfer Agent (MTA) for inbound/outbound SMTP
- Rule-based routing for web and mail traffic
- DNS-based dynamic routing
- Configuration, persistence, logging, and monitoring
## 1. Read and Summarize Dependencies
We will leverage existing modules; key points from their READMEs:
- **@push.rocks/smartproxy**: high-performance HTTP/HTTPS reverse proxy,
TLS termination, ACME HTTP-01, WebSockets, low-level port forwarding (nftables),
TCP/SNI proxy with dynamic routing and IP filtering.
- **@push.rocks/smartmail**: unified mail data structures and utilities.
- **mailparser**: parse raw SMTP messages into structured objects.
- **mailauth**: SPF/DKIM/DMARC validation and signing (DKIM).
- **@api.global/typedserver** / **typedsocket**: typed HTTP and TCP server abstractions.
- **@push.rocks/smartrule**: rule engine for dynamic decision-making on traffic.
- **@push.rocks/smartdns**: DNS resolution and caching for routing decisions.
- **@push.rocks/smartdata**: persistent key/value or document store for configs.
- **@push.rocks/smartlog** and **@push.rocks/smartrx**: logging and metrics/event tracking.
- **@push.rocks/qenv**: environment-based configuration loading.
- **ts/mta**: built-in MTA implementation (MtaService, SMTPServer, EmailSendJob, DKIMCreator/Verifier, DNSManager, ApiManager, Email class).
## 2. High-Level Architecture
DcRouter will instantiate and coordinate:
1. **HTTP(S) Proxy Layer**
- Use `NetworkProxy` and `Port80Handler` from smartproxy
- Manage virtual hosts, certificates, redirects
2. **TCP/SNI Proxy Layer (SmartProxy)**
- Use `SmartProxy` from `@push.rocks/smartproxy` to:
- Forward TCP connections by port or port ranges to backends (nftables-like port forwarding).
- Route TLS connections based on SNI to different target IPs/ports (SNI bridge).
- Integrate HTTP reverse proxy on select ports via `NetworkProxyBridge`.
- Manage ACME certificates with built-in `Port80Handler` and `CertProvisioner`.
- Apply IP filtering, connection rate limiting, timeouts, and detailed logging.
3. **SMTP/MTA Layer**
- Leverage `MtaService` from `ts/mta`, coordinating subcomponents:
- `SMTPServer` (classes.smtpserver): SMTP listener on ports 25/587 with STARTTLS/TLS
- `Email` and `EmailSendJob`: queue-based outbound sending with retries and rate limiting
- `DKIMCreator` / `DKIMVerifier`: automatic key management, signing, and verification
- `DNSManager`: generate and update DNS records for DKIM/SPF/MTA
- `ApiManager`: certificate provisioning and renewal via typedrequest/typedsocket
- Integrate inbound processing (mailparser, SPF/DKIM validation) and routing rules
4. **Rule Engine**
- Use smartrule to define routing rules for HTTP domains and mail addresses
- Support wildcard matching, priority, and dynamic updates
5. **DNS Integration**
- Use smartdns to resolve backend targets by name, with caching
6. **Persistence & Config**
- Load environment config via qenv
- Store dynamic state (rules, domains, MTA settings) in smartdata
7. **Logging & Metrics**
- Instrument all layers with smartlog and smartrx
8. **Management API / CLI**
- Expose REST/CLI commands via typedserver for: add/remove domains, rules,
view status, reload config, etc.
## 3. Feature Breakdown and Milestones
- [ ] A. Core Router scaffolding (`DcRouter` class)
- [ ] B. HTTP/HTTPS proxy integration
- [ ] C. SmartProxy Integration
- [ ] C1. Define `IPortProxySettings` with `fromPort`, `toPort`, `domainConfigs`, and global port ranges
- [ ] C2. Configure `domainConfigs` to mix port forwarding, SNI routing, and `useNetworkProxy` flags
- [ ] C3. Initialize and start `SmartProxy`, including `Port80Handler` for ACME and `CertProvisioner`
- [ ] C4. Wire SmartProxy events (e.g., certificate issuance, connection logging) into DcRouter
- [ ] C5. Support runtime updates to `domainConfigs` for dynamic routing changes
- [ ] D. MTA Integration (ts/mta)
- [ ] D1. Scaffold integration of `MtaService` into `DcRouter`
- [ ] D2. Configure and start `SMTPServer` with TLS/STARTTLS on ports 25/587
- [ ] D3. Wire inbound email handling via `processIncomingEmail` and rule engine
- [ ] D4. Implement outbound queue processing (`EmailSendJob`), retries, rate limiting
- [ ] D5. Manage DKIM keys and DNS records (`DKIMCreator`, `DNSManager`)
- [ ] D6. Provision and auto-renew certificates (`ApiManager`)
- [ ] E. Rule engine wiring for HTTP and SMTP
- [ ] F. DNS-based dynamic resolution
- [ ] G. Persistence layer for configs and state
- [ ] H. Logging/metrics instrumentation
- [ ] I. CLI and REST API endpoints
- [ ] J. Automated tests for each layer
- [ ] K. Documentation and examples in README
## 4. Next Steps
1. Review and refine this plan.
2. Begin implementation starting with the core scaffolding (A).
3. Iterate through features in milestone order.
_Last updated: 2025-05-04_

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@serve.zone/platformservice',
version: '2.2.0',
version: '2.3.1',
description: 'A multifaceted platform service handling mail, SMS, letter delivery, and AI services.'
}

View File

@ -0,0 +1,3 @@
export class AIBridge {
}

View File

@ -1,5 +1,5 @@
import * as plugins from './plugins.js';
import { SzPlatformService } from './classes.platformservice.js';
import { SzPlatformService } from './platformservice.js';

View File

@ -0,0 +1,49 @@
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import { type IMtaConfig, MtaService } from '../mta/classes.mta.js';
export interface IDcRouterOptions {
portProxyConfig?: plugins.smartproxy.IPortProxySettings;
mtaConfig?: IMtaConfig;
dnsServerConfig?: plugins.smartdns.IDnsServerOptions;
}
/**
* DcRouter can be run on ingress and egress to and from a datacenter site.
*/
export class DcRouter {
private options: IDcRouterOptions;
public smartProxy: plugins.smartproxy.SmartProxy;
public mta: MtaService;
public dnsServer: plugins.smartdns.DnsServer;
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() {
if (this.smartProxy) {
await this.smartProxy.start();
}
if (this.mta) {
await this.mta.start();
}
}
public async stop() {
if (this.smartProxy) {
await this.smartProxy.stop();
}
if (this.mta) {
await this.mta.stop();
}
}
}
export default DcRouter;

View File

@ -1,5 +1,5 @@
import * as plugins from '../plugins.js';
import { EmailService } from './email.classes.emailservice.js';
import { EmailService } from './classes.emailservice.js';
import { logger } from '../logger.js';
export class ApiManager {
@ -61,7 +61,7 @@ export class ApiManager {
);
// Add endpoint to check email status
this.typedRouter.addTypedHandler<{ emailId: string }>(
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IReq_CheckEmailStatus>(
new plugins.typedrequest.TypedHandler('checkEmailStatus', async (requestData) => {
// If MTA is enabled, use it to check status
if (this.emailRef.mtaConnector) {
@ -78,7 +78,7 @@ export class ApiManager {
);
// Add statistics endpoint
this.typedRouter.addTypedHandler<void>(
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IReq_GetEMailStats>(
new plugins.typedrequest.TypedHandler('getEmailStats', async () => {
return this.emailRef.getStats();
})

View File

@ -1,5 +1,5 @@
import * as plugins from '../plugins.js';
import { EmailService } from './email.classes.emailservice.js';
import { EmailService } from './classes.emailservice.js';
import { logger } from '../logger.js';
// Import MTA classes

View File

@ -1,10 +1,10 @@
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import { MtaConnector } from './email.classes.connector.mta.js';
import { RuleManager } from './email.classes.rulemanager.js';
import { ApiManager } from './email.classes.apimanager.js';
import { MtaConnector } from './classes.connector.mta.js';
import { RuleManager } from './classes.rulemanager.js';
import { ApiManager } from './classes.apimanager.js';
import { logger } from '../logger.js';
import type { SzPlatformService } from '../classes.platformservice.js';
import type { SzPlatformService } from '../platformservice.js';
// Import MTA service
import { MtaService, type IMtaConfig } from '../mta/index.js';

View File

@ -1,5 +1,5 @@
import * as plugins from '../plugins.js';
import { EmailService } from './email.classes.emailservice.js';
import { EmailService } from './classes.emailservice.js';
import { logger } from '../logger.js';
export class RuleManager {

View File

@ -1,4 +1,4 @@
import * as plugins from './email.plugins.js';
import * as plugins from '../plugins.js';
export class TemplateManager {
public smartmailDefault = new plugins.smartmail.Smartmail({

View File

@ -1,4 +1,4 @@
export * from './00_commitinfo_data.js';
import { SzPlatformService } from './classes.platformservice.js';
import { SzPlatformService } from './platformservice.js';
export const runCli = async () => {}

View File

@ -1,4 +1,4 @@
import type { SzPlatformService } from '../classes.platformservice.js';
import type { SzPlatformService } from '../platformservice.js';
import * as plugins from '../plugins.js';
export interface ILetterConstructorOptions {

View File

@ -0,0 +1 @@
export * from './classes.letterservice.js';

View File

@ -1,9 +1,9 @@
import * as plugins from '../plugins.js';
import { Email } from './mta.classes.email.js';
import type { IEmailOptions } from './mta.classes.email.js';
import { DeliveryStatus } from './mta.classes.emailsendjob.js';
import type { MtaService } from './mta.classes.mta.js';
import type { IDnsRecord } from './mta.classes.dnsmanager.js';
import { Email } from './classes.email.js';
import type { IEmailOptions } from './classes.email.js';
import { DeliveryStatus } from './classes.emailsendjob.js';
import type { MtaService } from './classes.mta.js';
import type { IDnsRecord } from './classes.dnsmanager.js';
/**
* Authentication options for API requests

View File

@ -1,8 +1,8 @@
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import { Email } from './mta.classes.email.js';
import type { MtaService } from './mta.classes.mta.js';
import { Email } from './classes.email.js';
import type { MtaService } from './classes.mta.js';
const readFile = plugins.util.promisify(plugins.fs.readFile);
const writeFile = plugins.util.promisify(plugins.fs.writeFile);

View File

@ -1,5 +1,5 @@
import * as plugins from '../plugins.js';
import { MtaService } from './mta.classes.mta.js';
import { MtaService } from './classes.mta.js';
class DKIMVerifier {
public mtaRef: MtaService;

View File

@ -1,6 +1,6 @@
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import type { MtaService } from './mta.classes.mta.js';
import type { MtaService } from './classes.mta.js';
/**
* Interface for DNS record information

View File

@ -1,8 +1,8 @@
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import { Email } from './mta.classes.email.js';
import { EmailSignJob } from './mta.classes.emailsignjob.js';
import type { MtaService } from './mta.classes.mta.js';
import { Email } from './classes.email.js';
import { EmailSignJob } from './classes.emailsignjob.js';
import type { MtaService } from './classes.mta.js';
// Configuration options for email sending
export interface IEmailSendOptions {

View File

@ -1,5 +1,5 @@
import * as plugins from '../plugins.js';
import type { MtaService } from './mta.classes.mta.js';
import type { MtaService } from './classes.mta.js';
interface Headers {
[key: string]: string;

View File

@ -1,14 +1,14 @@
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import { Email } from './mta.classes.email.js';
import { EmailSendJob, DeliveryStatus } from './mta.classes.emailsendjob.js';
import { DKIMCreator } from './mta.classes.dkimcreator.js';
import { DKIMVerifier } from './mta.classes.dkimverifier.js';
import { SMTPServer, type ISmtpServerOptions } from './mta.classes.smtpserver.js';
import { DNSManager } from './mta.classes.dnsmanager.js';
import { ApiManager } from './mta.classes.apimanager.js';
import type { SzPlatformService } from '../classes.platformservice.js';
import { Email } from './classes.email.js';
import { EmailSendJob, DeliveryStatus } from './classes.emailsendjob.js';
import { DKIMCreator } from './classes.dkimcreator.js';
import { DKIMVerifier } from './classes.dkimverifier.js';
import { SMTPServer, type ISmtpServerOptions } from './classes.smtpserver.js';
import { DNSManager } from './classes.dnsmanager.js';
import { ApiManager } from './classes.apimanager.js';
import type { SzPlatformService } from '../platformservice.js';
/**
* Configuration options for the MTA service

View File

@ -1,7 +1,7 @@
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import { Email } from './mta.classes.email.js';
import type { MtaService } from './mta.classes.mta.js';
import { Email } from './classes.email.js';
import type { MtaService } from './classes.mta.js';
export interface ISmtpServerOptions {
port: number;

View File

@ -1,7 +1,7 @@
export * from './mta.classes.dkimcreator.js';
export * from './mta.classes.emailsignjob.js';
export * from './mta.classes.dkimverifier.js';
export * from './mta.classes.mta.js';
export * from './mta.classes.smtpserver.js';
export * from './mta.classes.emailsendjob.js';
export * from './mta.classes.email.js';
export * from './classes.dkimcreator.js';
export * from './classes.emailsignjob.js';
export * from './classes.dkimverifier.js';
export * from './classes.mta.js';
export * from './classes.smtpserver.js';
export * from './classes.emailsendjob.js';
export * from './classes.email.js';

View File

@ -1,10 +1,10 @@
import * as plugins from './plugins.js';
import * as paths from './paths.js';
import { PlatformServiceDb } from './classes.platformservicedb.js'
import { EmailService } from './email/email.classes.emailservice.js';
import { SmsService } from './sms/smsservice.js';
import { EmailService } from './email/classes.emailservice.js';
import { SmsService } from './sms/classes.smsservice.js';
import { LetterService } from './letter/classes.letterservice.js';
import { MtaService } from './mta/mta.classes.mta.js';
import { MtaService } from './mta/classes.mta.js';
export class SzPlatformService {
public projectinfo: plugins.projectinfo.ProjectInfo;

View File

@ -41,6 +41,7 @@ export {
import * as projectinfo from '@push.rocks/projectinfo';
import * as qenv from '@push.rocks/qenv';
import * as smartdata from '@push.rocks/smartdata';
import * as smartdns from '@push.rocks/smartdns';
import * as smartfile from '@push.rocks/smartfile';
import * as smartlog from '@push.rocks/smartlog';
import * as smartmail from '@push.rocks/smartmail';
@ -51,7 +52,7 @@ 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, smartfile, smartlog, smartmail, smartpath, smartproxy, smartpromise, smartrequest, smartrule, smartrx };
export { projectinfo, qenv, smartdata, smartdns, smartfile, smartlog, smartmail, smartpath, smartproxy, smartpromise, smartrequest, smartrule, smartrx };
// apiclient.xyz scope
import * as letterxpress from '@apiclient.xyz/letterxpress';

View File

@ -1,7 +1,7 @@
import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import { logger } from '../logger.js';
import type { SzPlatformService } from '../classes.platformservice.js';
import type { SzPlatformService } from '../platformservice.js';
export interface ISmsConstructorOptions {
apiGatewayApiToken: string;

View File

@ -1 +1 @@
export * from './smsservice.js';
export * from './classes.smsservice.js';