@cloud-cli/px 1.1.0 → 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 +24 -41
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -4
- package/dist/proxy-manager.d.ts +0 -1
- package/dist/proxy-manager.js +29 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,75 +1,58 @@
|
|
|
1
1
|
## PX
|
|
2
2
|
|
|
3
|
-
Reverse proxy
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
px.start({ port: 4567 });
|
|
8
|
+
// cloudy.conf.mjs
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
px.addProxy({ domain: 'example.com', target: 'localhost:1234' });
|
|
10
|
+
import proxy from '@cloud-cli/px';
|
|
16
11
|
|
|
17
|
-
|
|
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
|
-
|
|
16
|
+
#### API
|
|
25
17
|
|
|
26
|
-
|
|
27
|
-
import px from '@cloud-cli/px';
|
|
28
|
-
import { cli } from '@cloud-cli/cy';
|
|
18
|
+
**Add a certificate**
|
|
29
19
|
|
|
30
|
-
|
|
20
|
+
```
|
|
21
|
+
cy proxy.addCertificate --domain="example.com" --certificate @path/to/cert --key @path/to/key
|
|
31
22
|
```
|
|
32
23
|
|
|
33
|
-
**
|
|
34
|
-
|
|
35
|
-
| Property | Type | Default |
|
|
36
|
-
| -------- | ------ | ----------- |
|
|
37
|
-
| `host` | String | '127.0.0.1' |
|
|
38
|
-
| `port` | Number | |
|
|
24
|
+
**Remove a certificate**
|
|
39
25
|
|
|
40
|
-
|
|
26
|
+
```
|
|
27
|
+
cy proxy.removeCertificate --domain example.com
|
|
28
|
+
```
|
|
41
29
|
|
|
42
|
-
**Add a
|
|
30
|
+
**Add a proxy to a local service**
|
|
43
31
|
|
|
44
32
|
```
|
|
45
|
-
|
|
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
|
-
**
|
|
42
|
+
**List proxies**
|
|
56
43
|
|
|
57
44
|
```
|
|
58
|
-
|
|
45
|
+
cy proxy.getProxyList
|
|
59
46
|
```
|
|
60
47
|
|
|
61
|
-
**
|
|
48
|
+
**List registered domains**
|
|
62
49
|
|
|
63
50
|
```
|
|
64
|
-
|
|
65
|
-
{
|
|
66
|
-
"domain": "example.com",
|
|
67
|
-
"target": "localhost:1234"
|
|
68
|
-
}
|
|
51
|
+
cy proxy.getDomainList
|
|
69
52
|
```
|
|
70
53
|
|
|
71
|
-
**
|
|
54
|
+
**Reload all configurations**
|
|
72
55
|
|
|
73
56
|
```
|
|
74
|
-
|
|
57
|
+
cy proxy.reload
|
|
75
58
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -2,11 +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;
|
|
8
9
|
getDomainList(): string[];
|
|
9
10
|
getProxyList(): [string, Proxy][];
|
|
10
|
-
getProxyForDomain(o: DomainOption): Proxy;
|
|
11
11
|
};
|
|
12
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
|
},
|
|
@@ -21,8 +24,5 @@ export default {
|
|
|
21
24
|
},
|
|
22
25
|
getProxyList() {
|
|
23
26
|
return manager.getProxyList();
|
|
24
|
-
}
|
|
25
|
-
getProxyForDomain(o) {
|
|
26
|
-
return manager.getProxyForDomain(o);
|
|
27
|
-
},
|
|
27
|
+
}
|
|
28
28
|
};
|
package/dist/proxy-manager.d.ts
CHANGED
package/dist/proxy-manager.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync,
|
|
2
|
-
import { join } from
|
|
3
|
-
import redbird from
|
|
4
|
-
const certificatesFolder = join(process.cwd(),
|
|
5
|
-
const configurationFile = join(process.cwd(),
|
|
6
|
-
const
|
|
7
|
-
const
|
|
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
|
-
|
|
11
|
+
if (!existsSync(certificatesFolder)) {
|
|
12
|
+
mkdirSync(certificatesFolder, { recursive: true });
|
|
13
|
+
}
|
|
12
14
|
}
|
|
13
15
|
get proxy() {
|
|
14
16
|
if (!this._proxy) {
|
|
@@ -39,16 +41,13 @@ export class ProxyManager {
|
|
|
39
41
|
}
|
|
40
42
|
addCertificate({ domain, certificate, key }) {
|
|
41
43
|
const basePath = join(certificatesFolder, domain);
|
|
42
|
-
this.
|
|
43
|
-
this.writeFile(
|
|
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
|
|
48
|
-
|
|
49
|
-
|
|
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() || []);
|
|
@@ -69,23 +68,29 @@ export class ProxyManager {
|
|
|
69
68
|
getProxyList() {
|
|
70
69
|
if (!existsSync(configurationFile))
|
|
71
70
|
return [];
|
|
72
|
-
const json = readFileSync(configurationFile,
|
|
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,
|
|
80
|
-
key: join(certificatesFolder,
|
|
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
|
}
|