@@ -3,6 +3,17 @@ import fs from "node:fs";
33import { spawnSync } from "node:child_process" ;
44import { bin } from "./constants.js" ;
55import { 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 : / t u n n e l I D = ( [ 0 - 9 a - z - ] + ) / ,
213- connectorID : / C o n n e c t o r I D : ( [ 0 - 9 a - z - ] + ) / ,
214- connect : / C o n n e c t i o n ( [ a - z 0 - 9 - ] + ) (?: .* ?) c o n n I n d e x = ( \d ) i p = ( [ 0 - 9 . ] + ) l o c a t i o n = ( [ A - Z ] + ) / ,
215- disconnect : / U n r e g i s t e r e d t u n n e l c o n n e c t i o n c o n n I n d e x = ( \d ) / ,
216- metrics : / m e t r i c s s e r v e r o n ( [ 0 - 9 . : ] + \/ m e t r i c s ) / ,
217- config : / c o n f i g = " ( .+ [ ^ \\ ] ) " / ,
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 ) {
0 commit comments