Skip to content

Commit

Permalink
feat: expose server through secured tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Jul 24, 2022
1 parent 81bb50c commit 7ae8818
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -23,6 +23,7 @@
"author": "JacobLinCool <jacoblincool@gmail.com> (https://github.com/JacobLinCool)",
"license": "MIT",
"dependencies": {
"cloudflared": "^0.1.1",
"commander": "^9.3.0",
"js-yaml": "^4.1.0",
"pidusage": "^3.0.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 33 additions & 2 deletions src/commands/run/index.ts
Expand Up @@ -2,6 +2,7 @@ import path from "node:path";
import fs from "node:fs";
import yaml from "js-yaml";
import { OptionValues, Command } from "commander";
import { tunnel } from "cloudflared";
import { Config } from "../../types";
import { server } from "../../server";
import { compile } from "../../compile";
Expand Down Expand Up @@ -46,7 +47,33 @@ export default async function run(opts: OptionValues, cmd: Command) {

console.log({ dirs });

const result = await server(config);
const port = opts.port || 52022;

const result = server(config, { port });

let server_tunnel: ReturnType<typeof tunnel> | undefined;
if (opts.expose) {
server_tunnel = tunnel({ url: `http://localhost:${port}` });
const url = await server_tunnel.url;
console.log(`Establishing Secure Tunnel ...`);
const connections = await Promise.all(server_tunnel.connections);
console.log(`Secure Tunnel Established.`);
console.log(` URL: ${url}`);
console.log(` Connections:`);
for (const connection of connections) {
console.log(
` [${connection.location}] ${connection.ip.padEnd(15)} - ${connection.id}`,
);
}
console.log("---");
console.log(
`\x1b[95mFor spectators around the world: https://game.ncaic.cc/?url=${url.replace(
"https",
"wss",
)}\x1b[m`,
);
console.log("---");
}

const [team_a, team_b] = Object.keys(config.teams);
const output = await Template(opts.output, { team_a, team_b, time });
Expand All @@ -55,11 +82,15 @@ export default async function run(opts: OptionValues, cmd: Command) {
fs.mkdirSync(path.dirname(output), { recursive: true });
}

fs.writeFileSync(output, JSON.stringify(result));
fs.writeFileSync(output, JSON.stringify(await result));

for (const dir of dirs) {
fs.rmSync(dir, { recursive: true });
}

if (server_tunnel) {
server_tunnel.stop();
}

process.exit(0);
}
2 changes: 2 additions & 0 deletions src/index.ts
Expand Up @@ -50,6 +50,8 @@ program
.command("run")
.description("run a competition")
.option("-c, --config <path>", "path to the config file", "config.yml")
.option("-p, --port <port>", "port to run the server on", parseInt, 52022)
.option("-e, --expose [token]", "expose a wss tunnel", "")
.option(
"-o, --output <path>",
"path to the output file",
Expand Down

0 comments on commit 7ae8818

Please sign in to comment.