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