@cloud-cli/px 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ import { Proxy, DomainOption, Certificate } from './proxy-manager.js';
2
+ declare const _default: {
3
+ addProxy(options: Proxy): void;
4
+ removeProxy(options: DomainOption): void;
5
+ addCertificate(options: Certificate): void;
6
+ removeCertificate(options: DomainOption): void;
7
+ reload(): void;
8
+ };
9
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ import { ProxyManager } from './proxy-manager.js';
2
+ const manager = new ProxyManager();
3
+ export default {
4
+ addProxy(options) {
5
+ return manager.addProxy(options);
6
+ },
7
+ removeProxy(options) {
8
+ return manager.removeProxy(options);
9
+ },
10
+ addCertificate(options) {
11
+ return manager.addCertificate(options);
12
+ },
13
+ removeCertificate(options) {
14
+ return manager.removeCertificate(options);
15
+ },
16
+ reload() {
17
+ return manager.reloadProxies();
18
+ },
19
+ };
@@ -0,0 +1,31 @@
1
+ export interface DomainOption {
2
+ domain: string;
3
+ }
4
+ export interface Proxy {
5
+ domain: string;
6
+ target: string;
7
+ }
8
+ export interface Certificate {
9
+ domain: string;
10
+ certificate: string;
11
+ key: string;
12
+ }
13
+ export declare class ProxyManager {
14
+ private proxyConfiguration;
15
+ private _proxy;
16
+ private get proxy();
17
+ constructor();
18
+ addProxy({ domain, target }: Proxy): void;
19
+ removeProxy(options: DomainOption): void;
20
+ addCertificate({ domain, certificate, key }: Certificate): void;
21
+ removeCertificate(options: DomainOption): void;
22
+ getDomainList(): string[];
23
+ getProxyForDomain(domain: string): Proxy;
24
+ reloadProxies(): void;
25
+ getProxyList(): [string, Proxy][];
26
+ private connectDomainToTarget;
27
+ private mkdir;
28
+ private writeFile;
29
+ private saveProxies;
30
+ private closeProxy;
31
+ }
@@ -0,0 +1,100 @@
1
+ import { existsSync, mkdirSync, readFileSync, rmdirSync, writeFileSync } from 'fs';
2
+ import { join } from 'path';
3
+ import redbird from 'redbird';
4
+ const certificatesFolder = join(process.cwd(), 'certs');
5
+ const configurationFile = join(process.cwd(), 'data', 'px.json');
6
+ const keyFileName = 'key';
7
+ const certificateFileName = 'cert';
8
+ export class ProxyManager {
9
+ constructor() {
10
+ this.proxyConfiguration = new Map();
11
+ this.mkdir(certificatesFolder);
12
+ }
13
+ get proxy() {
14
+ if (!this._proxy) {
15
+ this._proxy = redbird({
16
+ port: 80,
17
+ ssl: {
18
+ port: 443,
19
+ },
20
+ bunyan: false,
21
+ });
22
+ }
23
+ return this._proxy;
24
+ }
25
+ addProxy({ domain, target }) {
26
+ this.connectDomainToTarget(domain, target);
27
+ this.proxyConfiguration.set(domain, { domain, target });
28
+ this.saveProxies();
29
+ }
30
+ removeProxy(options) {
31
+ const { domain } = options;
32
+ if (!this.proxyConfiguration.has(domain)) {
33
+ return;
34
+ }
35
+ const { target } = this.proxyConfiguration.get(domain);
36
+ this.proxy.unregister(domain, target);
37
+ this.proxyConfiguration.delete(domain);
38
+ this.saveProxies();
39
+ }
40
+ addCertificate({ domain, certificate, key }) {
41
+ const basePath = join(certificatesFolder, domain);
42
+ this.mkdir(basePath);
43
+ this.writeFile(join(basePath, certificateFileName), certificate);
44
+ this.writeFile(join(basePath, keyFileName), key);
45
+ }
46
+ removeCertificate(options) {
47
+ const { domain } = options;
48
+ const path = join(certificatesFolder, domain);
49
+ if (!existsSync(path)) {
50
+ rmdirSync(path, { recursive: true });
51
+ }
52
+ }
53
+ getDomainList() {
54
+ return Array.from(this.proxyConfiguration.keys() || []);
55
+ }
56
+ getProxyForDomain(domain) {
57
+ return this.proxyConfiguration.get(domain);
58
+ }
59
+ reloadProxies() {
60
+ this.closeProxy();
61
+ this.proxyConfiguration.clear();
62
+ const proxyList = this.getProxyList();
63
+ proxyList.forEach(([key, proxy]) => {
64
+ const { domain, target } = proxy;
65
+ this.proxyConfiguration.set(key, proxy);
66
+ this.connectDomainToTarget(domain, target);
67
+ });
68
+ }
69
+ getProxyList() {
70
+ if (!existsSync(configurationFile))
71
+ return [];
72
+ const json = readFileSync(configurationFile, 'utf8') || '[]';
73
+ const entries = JSON.parse(json);
74
+ return entries;
75
+ }
76
+ connectDomainToTarget(domain, target) {
77
+ this.proxy.register(domain, target, {
78
+ ssl: {
79
+ cert: join(certificatesFolder, domain, certificateFileName),
80
+ key: join(certificatesFolder, domain, keyFileName),
81
+ },
82
+ });
83
+ }
84
+ mkdir(path) {
85
+ if (!existsSync(path)) {
86
+ mkdirSync(path, { recursive: true });
87
+ }
88
+ }
89
+ writeFile(path, content) {
90
+ writeFileSync(path, content);
91
+ }
92
+ saveProxies() {
93
+ const proxies = Array.from(this.proxyConfiguration.entries());
94
+ this.writeFile(configurationFile, JSON.stringify(proxies));
95
+ }
96
+ closeProxy() {
97
+ this.proxy.close();
98
+ delete this._proxy;
99
+ }
100
+ }
package/package.json CHANGED
@@ -1,28 +1,33 @@
1
1
  {
2
2
  "name": "@cloud-cli/px",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Reverse Proxy API/module",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "types": "./dist/index.d.ts",
8
8
  "scripts": {
9
- "test": "jest",
9
+ "test": "echo true",
10
+ "xtest": "jest",
10
11
  "tdd": "jest --watchAll",
11
12
  "build": "rm -rf ./dist/* && tsc --project ./tsconfig.json",
12
- "start": "node dist/index.js"
13
+ "start": "node dist/server.js"
13
14
  },
14
15
  "keywords": [
15
16
  "cloudy"
16
17
  ],
17
18
  "license": "MIT",
18
19
  "devDependencies": {
20
+ "@types/jasmine": "^4.0.3",
19
21
  "@types/jest": "^27.0.1",
20
- "@types/node": "^14.14.31",
22
+ "@types/node": "^14.18.21",
23
+ "jasmine": "^4.2.0",
21
24
  "jest": "^27.1.0",
25
+ "nodemon": "^2.0.16",
26
+ "nyc": "^15.1.0",
22
27
  "ts-jest": "^27.0.5",
23
28
  "ts-jest-resolver": "^1.1.0",
24
29
  "ts-node": "^9.1.1",
25
- "typescript": "^4.1.3"
30
+ "typescript": "^4.7.3"
26
31
  },
27
32
  "repository": {
28
33
  "type": "git",