@cloud-cli/px 4.1.6 → 4.2.0
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/dist/proxy-manager.d.ts +2 -1
- package/dist/proxy-manager.js +42 -5
- package/dist/types.d.ts +11 -0
- package/package.json +4 -3
package/dist/proxy-manager.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ProxyServer } from '@cloud-cli/proxy';
|
|
2
|
-
import type { DomainAndTarget, DomainName, Proxy, WithOptionalProps } from './types.js';
|
|
2
|
+
import type { DockerContainer, DomainAndTarget, DomainName, Proxy, WithOptionalProps } from './types.js';
|
|
3
3
|
export declare class ProxyManager {
|
|
4
4
|
server: ProxyServer;
|
|
5
5
|
addProxy(properties: WithOptionalProps<Proxy>): Promise<Proxy>;
|
|
@@ -9,4 +9,5 @@ export declare class ProxyManager {
|
|
|
9
9
|
getProxyList(filters?: Partial<Proxy>): Promise<Proxy[]>;
|
|
10
10
|
getProxyListForDomain(options: WithOptionalProps<DomainName>): Proxy[];
|
|
11
11
|
reload(): Promise<void>;
|
|
12
|
+
getRunningContainers(): Promise<DockerContainer[]>;
|
|
12
13
|
}
|
package/dist/proxy-manager.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getStorage, getConfig } from '@cloud-cli/cli';
|
|
2
2
|
import { ProxyEntry, ProxyServer, ProxySettings } from '@cloud-cli/proxy';
|
|
3
|
+
import { exec } from '@cloud-cli/exec';
|
|
3
4
|
const defaultOptions = {
|
|
4
5
|
httpPort: 80,
|
|
5
6
|
httpsPort: 443,
|
|
@@ -43,11 +44,11 @@ const readOption = value => {
|
|
|
43
44
|
};
|
|
44
45
|
function applyProperties(proxy, options) {
|
|
45
46
|
const properties = ['target', 'cors', 'redirect', 'redirectUrl', 'headers', 'authorization', 'preserveHost'];
|
|
46
|
-
|
|
47
|
+
for (const p of properties) {
|
|
47
48
|
if (p in options) {
|
|
48
49
|
proxy[p] = readOption(options[p]);
|
|
49
50
|
}
|
|
50
|
-
}
|
|
51
|
+
}
|
|
51
52
|
}
|
|
52
53
|
export class ProxyManager {
|
|
53
54
|
constructor() {
|
|
@@ -118,8 +119,8 @@ export class ProxyManager {
|
|
|
118
119
|
async reload() {
|
|
119
120
|
const targets = await getAll();
|
|
120
121
|
px.reset();
|
|
121
|
-
|
|
122
|
-
const [domain, path =
|
|
122
|
+
for (const t of targets) {
|
|
123
|
+
const [domain, path = ""] = t.domain.split("/");
|
|
123
124
|
px.add(new ProxyEntry({
|
|
124
125
|
domain: domain,
|
|
125
126
|
path: path,
|
|
@@ -129,8 +130,44 @@ export class ProxyManager {
|
|
|
129
130
|
cors: t.cors,
|
|
130
131
|
headers: t.headers,
|
|
131
132
|
authorization: t.authorization,
|
|
133
|
+
preserveHost: t.preserveHost,
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
const dockerTargets = await this.getRunningContainers();
|
|
137
|
+
for (const t of dockerTargets) {
|
|
138
|
+
if (!t.ports.length || !t.labels.host)
|
|
139
|
+
continue;
|
|
140
|
+
px.add(new ProxyEntry({
|
|
141
|
+
domain: t.labels.host,
|
|
142
|
+
target: `http://localhost:${t.ports[0].host}`,
|
|
143
|
+
redirectToHttps: true,
|
|
144
|
+
cors: true,
|
|
132
145
|
}));
|
|
133
|
-
}
|
|
146
|
+
}
|
|
134
147
|
px.start();
|
|
135
148
|
}
|
|
149
|
+
async getRunningContainers() {
|
|
150
|
+
const ps = await exec("docker", ["ps", "-aq"]);
|
|
151
|
+
const ids = ps.stdout.trim().split("\n");
|
|
152
|
+
const format = '--format="{{.Id}},{{.Name}},{{.State.Status}},{{.Image}}"';
|
|
153
|
+
const state = await exec("docker", ["inspect", format, ...ids]);
|
|
154
|
+
const json = JSON.parse(state.stdout);
|
|
155
|
+
return json.map(readDockerContainer);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function readDockerContainer(d) {
|
|
159
|
+
const labels = Object.fromEntries(Object.entries(d.Config.Labels)
|
|
160
|
+
.filter(([key]) => key.startsWith("px:"))
|
|
161
|
+
.map(([key, value]) => [key.replace("px:", ""), value]));
|
|
162
|
+
return {
|
|
163
|
+
id: d.Id,
|
|
164
|
+
image: d.Image,
|
|
165
|
+
state: d.State.Status,
|
|
166
|
+
name: d.Name,
|
|
167
|
+
labels: labels,
|
|
168
|
+
ports: Object.entries(d.HostConfig.PortBindings).map(([port, pb]) => ({
|
|
169
|
+
container: Number(port.replace(/\D+/, "")),
|
|
170
|
+
host: Number(pb[0].HostPort),
|
|
171
|
+
})),
|
|
172
|
+
};
|
|
136
173
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -19,4 +19,15 @@ export interface Proxy {
|
|
|
19
19
|
cors: boolean;
|
|
20
20
|
}
|
|
21
21
|
export type WithOptionalProps<T extends object> = T & OptionalProps;
|
|
22
|
+
export interface DockerContainer {
|
|
23
|
+
id: string;
|
|
24
|
+
image: string;
|
|
25
|
+
name: string;
|
|
26
|
+
state: string;
|
|
27
|
+
ports: Array<{
|
|
28
|
+
host: number;
|
|
29
|
+
container: number;
|
|
30
|
+
}>;
|
|
31
|
+
labels: Record<string, string>;
|
|
32
|
+
}
|
|
22
33
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-cli/px",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Reverse Proxy",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"prettier": "@cloud-cli/prettier-config",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "echo true",
|
|
11
|
-
"build": "rm -rf ./dist/* && tsc"
|
|
11
|
+
"build": "rm -rf ./dist/* && npm update && tsc"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"cloudy"
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@cloud-cli/proxy": "^1.2.0"
|
|
31
|
+
"@cloud-cli/proxy": "^1.2.0",
|
|
32
|
+
"@cloud-cli/exec": "^1.2.0"
|
|
32
33
|
},
|
|
33
34
|
"peerDependencies": {
|
|
34
35
|
"@cloud-cli/cli": "^2.0.0"
|