Skip to content

Commit

Permalink
feat: update service regex
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Apr 25, 2023
1 parent 10f395b commit 8513d43
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
10 changes: 10 additions & 0 deletions src/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const conn_regex = /connection[= ]([a-z0-9-]+)/i;
export const ip_regex = /ip=([0-9.]+)/;
export const location_regex = /location=([A-Z]+)/;
export const index_regex = /connIndex=(\d)/;

export const disconnect_regex = /Unregistered tunnel connection connIndex=(\d)/i;
export const tunnelID_regex = /tunnelID=([0-9a-z-]+)/i;
export const connectorID_regex = /Connector ID: ([0-9a-z-]+)/i;
export const metrics_regex = /metrics server on ([0-9.:]+\/metrics)/;
export const config_regex = /config="(.+[^\\])"/;
56 changes: 32 additions & 24 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import fs from "node:fs";
import { spawnSync } from "node:child_process";
import { bin } from "./constants.js";
import { Connection } from "./types.js";
import {
config_regex,
conn_regex,
connectorID_regex,
disconnect_regex,
index_regex,
ip_regex,
location_regex,
metrics_regex,
tunnelID_regex,
} from "./regex.js";

/**
* Cloudflared launchd identifier.
Expand Down Expand Up @@ -208,15 +219,6 @@ export function current(): {

const log = is_systemd() ? journal() : err();

const regex = {
tunnelID: /tunnelID=([0-9a-z-]+)/,
connectorID: /Connector ID: ([0-9a-z-]+)/,
connect: /Connection ([a-z0-9-]+) (?:.*?)connIndex=(\d) ip=([0-9.]+) location=([A-Z]+)/,
disconnect: /Unregistered tunnel connection connIndex=(\d)/,
metrics: /metrics server on ([0-9.:]+\/metrics)/,
config: /config="(.+[^\\])"/,
};

let tunnelID = "";
let connectorID = "";
const connections: Connection[] = [];
Expand All @@ -228,24 +230,30 @@ export function current(): {

for (const line of log.split("\n")) {
try {
if (line.match(regex.tunnelID)) {
tunnelID = line.match(regex.tunnelID)?.[1] ?? "";
} else if (line.match(regex.connectorID)) {
connectorID = line.match(regex.connectorID)?.[1] ?? "";
} else if (line.match(regex.connect)) {
const [, id, idx, ip, location] = line.match(regex.connect) ?? [];
if (id && idx && ip && location) {
connections[parseInt(idx)] = { id, ip, location };
}
} else if (line.match(regex.disconnect)) {
const [, idx] = line.match(regex.disconnect) ?? [];
if (line.match(tunnelID_regex)) {
tunnelID = line.match(tunnelID_regex)?.[1] ?? "";
} else if (line.match(connectorID_regex)) {
connectorID = line.match(connectorID_regex)?.[1] ?? "";
} else if (
line.match(conn_regex) &&
line.match(location_regex) &&
line.match(ip_regex) &&
line.match(index_regex)
) {
const [, id] = line.match(conn_regex) ?? [];
const [, location] = line.match(location_regex) ?? [];
const [, ip] = line.match(ip_regex) ?? [];
const [, idx] = line.match(index_regex) ?? [];
connections[parseInt(idx)] = { id, ip, location };
} else if (line.match(disconnect_regex)) {
const [, idx] = line.match(disconnect_regex) ?? [];
if (parseInt(idx) in connections) {
connections[parseInt(idx)] = { id: "", ip: "", location: "" };
}
} else if (line.match(regex.metrics)) {
metrics = line.match(regex.metrics)?.[1] ?? "";
} else if (line.match(regex.config)) {
config = JSON.parse(line.match(regex.config)?.[1].replace(/\\/g, "") ?? "{}");
} else if (line.match(metrics_regex)) {
metrics = line.match(metrics_regex)?.[1] ?? "";
} else if (line.match(config_regex)) {
config = JSON.parse(line.match(config_regex)?.[1].replace(/\\/g, "") ?? "{}");
}
} catch (err) {
if (process.env.VERBOSE) {
Expand Down
5 changes: 1 addition & 4 deletions src/tunnel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { spawn, ChildProcess } from "node:child_process";
import { bin } from "./constants.js";
import { Connection } from "./types.js";
import { conn_regex, ip_regex, location_regex, index_regex } from "./regex.js";

/**
* Create a tunnel.
Expand Down Expand Up @@ -43,10 +44,6 @@ export function tunnel(options: Record<string, string | number | null> = {}): {
let url_rejector: (reason: unknown) => void = () => undefined;
const url = new Promise<string>((...pair) => ([url_resolver, url_rejector] = pair));

const conn_regex = /connection[= ]([a-z0-9-]+)/i;
const ip_regex = /ip=([0-9.]+)/;
const location_regex = /location=([A-Z]+)/;
const index_regex = /connIndex=(\d)/;
const connection_resolvers: ((value: Connection | PromiseLike<Connection>) => void)[] = [];
const connection_rejectors: ((reason: unknown) => void)[] = [];
const connections: Promise<Connection>[] = [];
Expand Down

0 comments on commit 8513d43

Please sign in to comment.