@cloud-cli/px 1.0.1 → 1.1.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.
package/README.md CHANGED
@@ -1,75 +1,58 @@
1
1
  ## PX
2
2
 
3
- Reverse proxy with Nginx
3
+ Reverse proxy to map local services to http(s) subdomains
4
4
 
5
5
  #### Usage
6
6
 
7
- As a module:
8
-
9
7
  ```ts
10
- import px from '@cloud-cli/px';
11
-
12
- px.start({ port: 4567 });
8
+ // cloudy.conf.mjs
13
9
 
14
- px.removeProxy({ domain: 'example.com' });
15
- px.addProxy({ domain: 'example.com', target: 'localhost:1234' });
10
+ import proxy from '@cloud-cli/px';
16
11
 
17
- px.removeCertificate({ domain: 'example.com' });
18
- px.addCertificate({ domain: 'example.com', certificate: '...', key: '...' });
12
+ export default { proxy };
19
13
 
20
- console.log(px.getDomainList());
21
- console.log(px.getProxyForDomain('example.com'));
22
14
  ```
23
15
 
24
- With Cloudy CLI:
16
+ #### API
25
17
 
26
- ```ts
27
- import px from '@cloud-cli/px';
28
- import { cli } from '@cloud-cli/cy';
18
+ **Add a certificate**
29
19
 
30
- cli.add('px', px);
20
+ ```
21
+ cy proxy.addCertificate --domain="example.com" --certificate @path/to/cert --key @path/to/key
31
22
  ```
32
23
 
33
- **start() options**
34
-
35
- | Property | Type | Default |
36
- | -------- | ------ | ----------- |
37
- | `host` | String | '127.0.0.1' |
38
- | `port` | Number | |
24
+ **Remove a certificate**
39
25
 
40
- #### HTTP API
26
+ ```
27
+ cy proxy.removeCertificate --domain example.com
28
+ ```
41
29
 
42
- **Add a certificate**
30
+ **Add a proxy to a local service**
43
31
 
44
32
  ```
45
- POST /certificates
33
+ cy proxy.addProxy --domain "foo.example.com" --target "localhost:1234"
34
+ ```
46
35
 
47
- {
48
- "domain": "example.com",
49
- "certificate": "...",
50
- "key": "...",
51
- }
36
+ **Remove a proxy**
52
37
 
38
+ ```
39
+ cy proxy.removeProxy --domain "foo.example.com"
53
40
  ```
54
41
 
55
- **Add a certificate**
42
+ **List proxies**
56
43
 
57
44
  ```
58
- DELETE /certificates/example.com
45
+ cy proxy.getProxyList
59
46
  ```
60
47
 
61
- **Add a service**
48
+ **List registered domains**
62
49
 
63
50
  ```
64
- POST /services
65
- {
66
- "domain": "example.com",
67
- "target": "localhost:1234"
68
- }
51
+ cy proxy.getDomainList
69
52
  ```
70
53
 
71
- **Remove a service**
54
+ **Reload all configurations**
72
55
 
73
56
  ```
74
- DELETE /services/example.com
57
+ cy proxy.reload
75
58
  ```
package/dist/index.d.ts CHANGED
@@ -2,8 +2,11 @@ import { Proxy, DomainOption, Certificate } from './proxy-manager.js';
2
2
  declare const _default: {
3
3
  addProxy(options: Proxy): void;
4
4
  removeProxy(options: DomainOption): void;
5
+ getProxy(options: DomainOption): Proxy;
5
6
  addCertificate(options: Certificate): void;
6
7
  removeCertificate(options: DomainOption): void;
7
8
  reload(): void;
9
+ getDomainList(): string[];
10
+ getProxyList(): [string, Proxy][];
8
11
  };
9
12
  export default _default;
package/dist/index.js CHANGED
@@ -7,6 +7,9 @@ export default {
7
7
  removeProxy(options) {
8
8
  return manager.removeProxy(options);
9
9
  },
10
+ getProxy(options) {
11
+ return manager.getProxyForDomain(options);
12
+ },
10
13
  addCertificate(options) {
11
14
  return manager.addCertificate(options);
12
15
  },
@@ -16,4 +19,10 @@ export default {
16
19
  reload() {
17
20
  return manager.reloadProxies();
18
21
  },
22
+ getDomainList() {
23
+ return manager.getDomainList();
24
+ },
25
+ getProxyList() {
26
+ return manager.getProxyList();
27
+ }
19
28
  };
@@ -20,11 +20,10 @@ export declare class ProxyManager {
20
20
  addCertificate({ domain, certificate, key }: Certificate): void;
21
21
  removeCertificate(options: DomainOption): void;
22
22
  getDomainList(): string[];
23
- getProxyForDomain(domain: string): Proxy;
23
+ getProxyForDomain(options: DomainOption): Proxy;
24
24
  reloadProxies(): void;
25
25
  getProxyList(): [string, Proxy][];
26
26
  private connectDomainToTarget;
27
- private mkdir;
28
27
  private writeFile;
29
28
  private saveProxies;
30
29
  private closeProxy;
@@ -1,14 +1,16 @@
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';
1
+ import { existsSync, mkdirSync, readFileSync, rmSync, 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 keyExtension = ".key";
7
+ const certificateExtension = ".cert";
8
8
  export class ProxyManager {
9
9
  constructor() {
10
10
  this.proxyConfiguration = new Map();
11
- this.mkdir(certificatesFolder);
11
+ if (!existsSync(certificatesFolder)) {
12
+ mkdirSync(certificatesFolder, { recursive: true });
13
+ }
12
14
  }
13
15
  get proxy() {
14
16
  if (!this._proxy) {
@@ -39,22 +41,19 @@ export class ProxyManager {
39
41
  }
40
42
  addCertificate({ domain, certificate, key }) {
41
43
  const basePath = join(certificatesFolder, domain);
42
- this.mkdir(basePath);
43
- this.writeFile(join(basePath, certificateFileName), certificate);
44
- this.writeFile(join(basePath, keyFileName), key);
44
+ this.writeFile(basePath + certificateExtension, certificate);
45
+ this.writeFile(basePath + keyExtension, key);
45
46
  }
46
47
  removeCertificate(options) {
47
- const { domain } = options;
48
- const path = join(certificatesFolder, domain);
49
- if (!existsSync(path)) {
50
- rmdirSync(path, { recursive: true });
51
- }
48
+ const basePath = join(certificatesFolder, options.domain);
49
+ rmSync(basePath + certificateExtension, { force: true });
50
+ rmSync(basePath + keyExtension, { force: true });
52
51
  }
53
52
  getDomainList() {
54
53
  return Array.from(this.proxyConfiguration.keys() || []);
55
54
  }
56
- getProxyForDomain(domain) {
57
- return this.proxyConfiguration.get(domain);
55
+ getProxyForDomain(options) {
56
+ return this.proxyConfiguration.get(options.domain);
58
57
  }
59
58
  reloadProxies() {
60
59
  this.closeProxy();
@@ -69,23 +68,29 @@ export class ProxyManager {
69
68
  getProxyList() {
70
69
  if (!existsSync(configurationFile))
71
70
  return [];
72
- const json = readFileSync(configurationFile, 'utf8') || '[]';
71
+ const json = readFileSync(configurationFile, "utf8") || "[]";
73
72
  const entries = JSON.parse(json);
74
73
  return entries;
75
74
  }
76
75
  connectDomainToTarget(domain, target) {
76
+ const parts = domain.split(".");
77
+ let rootDomain = "";
78
+ while (parts.length) {
79
+ rootDomain = parts.join(".");
80
+ if (existsSync(join(certificatesFolder, rootDomain) + keyExtension))
81
+ break;
82
+ parts.shift();
83
+ }
84
+ if (!rootDomain) {
85
+ throw new Error("Certificate not found for " + domain);
86
+ }
77
87
  this.proxy.register(domain, target, {
78
88
  ssl: {
79
- cert: join(certificatesFolder, domain, certificateFileName),
80
- key: join(certificatesFolder, domain, keyFileName),
89
+ cert: join(certificatesFolder, rootDomain) + certificateExtension,
90
+ key: join(certificatesFolder, rootDomain) + keyExtension,
81
91
  },
82
92
  });
83
93
  }
84
- mkdir(path) {
85
- if (!existsSync(path)) {
86
- mkdirSync(path, { recursive: true });
87
- }
88
- }
89
94
  writeFile(path, content) {
90
95
  writeFileSync(path, content);
91
96
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/px",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "Reverse Proxy API/module",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",