@@ -11,30 +11,41 @@ public function __construct(public CommandLine $cli, public Brew $brew)
11
11
}
12
12
13
13
public function currentTunnelUrl (?string $ domain = null )
14
+ {
15
+ return $ this ->currentCloudflaredTunnels ()[$ domain ] ?? false ;
16
+ }
17
+
18
+ protected function currentCloudflaredTunnels (): array
14
19
{
15
20
$ urls = [];
21
+
22
+ // Get all cloudflared processes
16
23
$ processes = array_filter (explode ("\n" , $ this ->cli ->run ('pgrep -fl cloudflared ' )));
17
24
18
- // Every cloudflare process will start a metrics web server
19
- // where Quick Tunnel URL is mentioned under /metrics endpoint
25
+ // Every cloudflared process will start a " metrics" web server where the
26
+ // Quick Tunnel URL will be mentioned under the /metrics endpoint
20
27
foreach ($ processes as $ process ) {
21
- preg_match ('/(?<pid>\d+)\s.+--http-host-header\s(?<domain>[^\s]+).*/ ' , $ process , $ matches );
22
- if (array_key_exists ('domain ' , $ matches ) && array_key_exists ('pid ' , $ matches )) {
23
- $ local_domain = $ matches ['domain ' ];
24
- $ lsof = $ this ->cli ->run ("lsof -iTCP -P -a -p {$ matches ['pid ' ]}" );
25
- preg_match ('/TCP\s(?<server>[^\s]+:\d+)\s\(LISTEN\)/ ' , $ lsof , $ matches );
26
- if (array_key_exists ('server ' , $ matches )) {
28
+ // Get the URL shared in this process
29
+ preg_match ('/(?<pid>\d+)\s.+--http-host-header\s(?<domain>[^\s]+).*/ ' , $ process , $ pgrepMatches );
30
+
31
+ if (array_key_exists ('domain ' , $ pgrepMatches ) && array_key_exists ('pid ' , $ pgrepMatches )) {
32
+ // Get the localhost URL (localhost:port) for the metrics server
33
+ $ lsof = $ this ->cli ->run ("lsof -iTCP -P -a -p {$ pgrepMatches ['pid ' ]}" );
34
+ preg_match ('/TCP\s(?<server>[^\s]+:\d+)\s\(LISTEN\)/ ' , $ lsof , $ lsofMatches );
35
+
36
+ if (array_key_exists ('server ' , $ lsofMatches )) {
27
37
try {
28
- $ body = (new Client ())->get ("http:// {$ matches ['server ' ]}/metrics " )->getBody ();
29
- preg_match ('/userHostname="(?<url>.+)"/ ' , $ body ->getContents (), $ matches );
38
+ // Get the shared cloudflared URL from the metrics server output
39
+ $ body = (new Client ())->get ("http:// {$ lsofMatches ['server ' ]}/metrics " )->getBody ();
40
+ preg_match ('/userHostname="(?<url>.+)"/ ' , $ body ->getContents (), $ lsofMatches );
30
41
} catch (\Exception $ e ) {}
31
42
32
- $ urls [$ local_domain ] = array_key_exists ( ' url ' , $ matches ) ? $ matches ['url ' ] : false ;
43
+ $ urls [$ pgrepMatches [ ' domain ' ]] = $ lsofMatches ['url ' ] ?? false ;
33
44
}
34
45
}
35
46
}
36
47
37
- return array_key_exists ( $ domain , $ urls) ? $ urls [ $ domain ] : false ;
48
+ return $ urls ;
38
49
}
39
50
40
51
/**
0 commit comments