Skip to content

Commit

Permalink
Join the same network to enable communication from extension -> Nextjs
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson committed Jan 28, 2024
1 parent 41ed213 commit bb6ddc4
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/server-actions/devices.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"use server";

import { spawn } from "child_process";
import { exec, spawn } from "child_process";
import { promisify } from "util";
import { Device } from "@/models";

let lastHostPort = 3000;
const killers: Record<number, Function> = {};

const execAsync = promisify(exec);

export async function createDevice({
hostname,
isSecure,
Expand All @@ -22,21 +25,30 @@ export async function createDevice({
// make sure a valid URL is provided
new URL(url);

const { stdout: networksStdout } = await execAsync(
'docker inspect $(hostname) -f "{{json .NetworkSettings.Networks }}"'
);
const networks = JSON.parse(networksStdout) as Record<string, unknown>;
const network = Object.keys(networks)[0];

return new Promise((resolve) => {
const dockerImage = "poc-device:chrome"; // must matches the image name in `./scripts/dev.sh`
const containerPort = 5900;
const hostPort = ++lastHostPort;

const args = [
`run`,
`--env`,
"run",
"--env",
`SCREEN_RESOLUTION=${width}x${height}x24`,
`--name`,
"--name",
`poc-${hostPort}`,
`--publish`,
"--network",
network,
"--publish",
`${hostPort}:${containerPort}`,
`--rm`,
"--rm",
dockerImage,
`google-chrome`,
"google-chrome",
`--app=${url}`,
];
const docker = spawn("docker", args);
Expand Down

0 comments on commit bb6ddc4

Please sign in to comment.