@cloud-cli/px 4.1.7 → 4.2.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/dist/proxy-manager.d.ts +2 -1
- package/dist/proxy-manager.js +40 -3
- package/dist/types.d.ts +11 -0
- package/package.json +3 -2
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,
|
|
@@ -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,
|
|
@@ -131,7 +132,43 @@ export class ProxyManager {
|
|
|
131
132
|
authorization: t.authorization,
|
|
132
133
|
preserveHost: t.preserveHost,
|
|
133
134
|
}));
|
|
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
|
+
path: t.labels.path || '',
|
|
143
|
+
target: `http://localhost:${t.ports[0].host}`,
|
|
144
|
+
redirectToHttps: true,
|
|
145
|
+
cors: true,
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
135
148
|
px.start();
|
|
136
149
|
}
|
|
150
|
+
async getRunningContainers() {
|
|
151
|
+
const ps = await exec("docker", ["ps", "-aq"]);
|
|
152
|
+
const ids = ps.stdout.trim().split("\n");
|
|
153
|
+
const format = '--format="{{.Id}},{{.Name}},{{.State.Status}},{{.Image}}"';
|
|
154
|
+
const state = await exec("docker", ["inspect", format, ...ids]);
|
|
155
|
+
const json = JSON.parse(state.stdout);
|
|
156
|
+
return json.map(readDockerContainer);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function readDockerContainer(d) {
|
|
160
|
+
const labels = Object.fromEntries(Object.entries(d.Config.Labels)
|
|
161
|
+
.filter(([key]) => key.startsWith("px:"))
|
|
162
|
+
.map(([key, value]) => [key.replace("px:", ""), value]));
|
|
163
|
+
return {
|
|
164
|
+
id: d.Id,
|
|
165
|
+
image: d.Image,
|
|
166
|
+
state: d.State.Status,
|
|
167
|
+
name: d.Name,
|
|
168
|
+
labels: labels,
|
|
169
|
+
ports: Object.entries(d.HostConfig.PortBindings).map(([port, pb]) => ({
|
|
170
|
+
container: Number(port.replace(/\D+/, "")),
|
|
171
|
+
host: Number(pb[0].HostPort),
|
|
172
|
+
})),
|
|
173
|
+
};
|
|
137
174
|
}
|
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.1
|
|
3
|
+
"version": "4.2.1",
|
|
4
4
|
"description": "Reverse Proxy",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -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"
|