Skip to content

Commit 8513d43

Browse files
committed
feat: update service regex
1 parent 10f395b commit 8513d43

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

src/regex.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const conn_regex = /connection[= ]([a-z0-9-]+)/i;
2+
export const ip_regex = /ip=([0-9.]+)/;
3+
export const location_regex = /location=([A-Z]+)/;
4+
export const index_regex = /connIndex=(\d)/;
5+
6+
export const disconnect_regex = /Unregistered tunnel connection connIndex=(\d)/i;
7+
export const tunnelID_regex = /tunnelID=([0-9a-z-]+)/i;
8+
export const connectorID_regex = /Connector ID: ([0-9a-z-]+)/i;
9+
export const metrics_regex = /metrics server on ([0-9.:]+\/metrics)/;
10+
export const config_regex = /config="(.+[^\\])"/;

src/service.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ import fs from "node:fs";
33
import { spawnSync } from "node:child_process";
44
import { bin } from "./constants.js";
55
import { Connection } from "./types.js";
6+
import {
7+
config_regex,
8+
conn_regex,
9+
connectorID_regex,
10+
disconnect_regex,
11+
index_regex,
12+
ip_regex,
13+
location_regex,
14+
metrics_regex,
15+
tunnelID_regex,
16+
} from "./regex.js";
617

718
/**
819
* Cloudflared launchd identifier.
@@ -208,15 +219,6 @@ export function current(): {
208219

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

211-
const regex = {
212-
tunnelID: /tunnelID=([0-9a-z-]+)/,
213-
connectorID: /Connector ID: ([0-9a-z-]+)/,
214-
connect: /Connection ([a-z0-9-]+) (?:.*?)connIndex=(\d) ip=([0-9.]+) location=([A-Z]+)/,
215-
disconnect: /Unregistered tunnel connection connIndex=(\d)/,
216-
metrics: /metrics server on ([0-9.:]+\/metrics)/,
217-
config: /config="(.+[^\\])"/,
218-
};
219-
220222
let tunnelID = "";
221223
let connectorID = "";
222224
const connections: Connection[] = [];
@@ -228,24 +230,30 @@ export function current(): {
228230

229231
for (const line of log.split("\n")) {
230232
try {
231-
if (line.match(regex.tunnelID)) {
232-
tunnelID = line.match(regex.tunnelID)?.[1] ?? "";
233-
} else if (line.match(regex.connectorID)) {
234-
connectorID = line.match(regex.connectorID)?.[1] ?? "";
235-
} else if (line.match(regex.connect)) {
236-
const [, id, idx, ip, location] = line.match(regex.connect) ?? [];
237-
if (id && idx && ip && location) {
238-
connections[parseInt(idx)] = { id, ip, location };
239-
}
240-
} else if (line.match(regex.disconnect)) {
241-
const [, idx] = line.match(regex.disconnect) ?? [];
233+
if (line.match(tunnelID_regex)) {
234+
tunnelID = line.match(tunnelID_regex)?.[1] ?? "";
235+
} else if (line.match(connectorID_regex)) {
236+
connectorID = line.match(connectorID_regex)?.[1] ?? "";
237+
} else if (
238+
line.match(conn_regex) &&
239+
line.match(location_regex) &&
240+
line.match(ip_regex) &&
241+
line.match(index_regex)
242+
) {
243+
const [, id] = line.match(conn_regex) ?? [];
244+
const [, location] = line.match(location_regex) ?? [];
245+
const [, ip] = line.match(ip_regex) ?? [];
246+
const [, idx] = line.match(index_regex) ?? [];
247+
connections[parseInt(idx)] = { id, ip, location };
248+
} else if (line.match(disconnect_regex)) {
249+
const [, idx] = line.match(disconnect_regex) ?? [];
242250
if (parseInt(idx) in connections) {
243251
connections[parseInt(idx)] = { id: "", ip: "", location: "" };
244252
}
245-
} else if (line.match(regex.metrics)) {
246-
metrics = line.match(regex.metrics)?.[1] ?? "";
247-
} else if (line.match(regex.config)) {
248-
config = JSON.parse(line.match(regex.config)?.[1].replace(/\\/g, "") ?? "{}");
253+
} else if (line.match(metrics_regex)) {
254+
metrics = line.match(metrics_regex)?.[1] ?? "";
255+
} else if (line.match(config_regex)) {
256+
config = JSON.parse(line.match(config_regex)?.[1].replace(/\\/g, "") ?? "{}");
249257
}
250258
} catch (err) {
251259
if (process.env.VERBOSE) {

src/tunnel.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { spawn, ChildProcess } from "node:child_process";
22
import { bin } from "./constants.js";
33
import { Connection } from "./types.js";
4+
import { conn_regex, ip_regex, location_regex, index_regex } from "./regex.js";
45

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

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

0 commit comments

Comments
 (0)