@cloud-cli/px 4.1.7 → 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.
@@ -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
  }
@@ -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
- targets.forEach((t) => {
122
- const [domain, path = ''] = t.domain.split('/');
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,42 @@ 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
+ target: `http://localhost:${t.ports[0].host}`,
143
+ redirectToHttps: true,
144
+ cors: true,
145
+ }));
146
+ }
135
147
  px.start();
136
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
+ };
137
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.1.7",
3
+ "version": "4.2.0",
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"