From affdd5bee9a0c16166c506779b4906a47e23346e Mon Sep 17 00:00:00 2001 From: Sascha Nitsch Date: Thu, 29 Aug 2024 20:08:51 +0200 Subject: [PATCH] automatic session management --- index.php | 2 +- scriptedbrowser/control.php | 15 ++++++++++----- scriptedbrowser/main.php | 19 ++++++++++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/index.php b/index.php index 1529262..427683c 100644 --- a/index.php +++ b/index.php @@ -16,5 +16,5 @@ if ($_SERVER['argc'] < 2) { } $serverHost = '127.0.0.1'; $serverPort = 4444; -$main = new ScriptedBrowser\Main($serverHost, $serverPort, 6, $_SERVER['argc'] > 2 ? $_SERVER['argv'][2] : null); +$main = new ScriptedBrowser\Main($serverHost, $serverPort, 6); $main->run($_SERVER['argv'][1]); diff --git a/scriptedbrowser/control.php b/scriptedbrowser/control.php index 1ae48e6..98e68b8 100644 --- a/scriptedbrowser/control.php +++ b/scriptedbrowser/control.php @@ -121,7 +121,6 @@ class Control curl_close($ch); $respJson = json_decode($resp, true); if (!is_array($respJson)) { - echo "resp: '$resp'\n"; return []; } if (array_key_exists('value', $respJson)) { @@ -171,7 +170,8 @@ class Control */ public function getURL() : string { - return $this->execute('GET', 'url'); + $url = $this->execute('GET', 'url'); + return (!is_array($url)) ? $url : ''; } /** @@ -222,16 +222,21 @@ class Control $openSession = true; if ($session !== null) { - // todo validate session - $openSession = false; $this->session = $session; + // get current URL to see if the session is valid + $url = $this->getURL(); + if ($url !== '') { + $openSession = false; + } else { + $this->session = ''; + } } if ($openSession) { $sessionData = $this->execute('POST', "session", '{"capabilities" : {"alwaysMatch": {"moz:firefoxOptions":{"prefs":{"browser.display.os-zoom-behavior": 0,"layout.css.prefers-color-scheme.content-override": 1}}}, "firstMatch": [{"browserName": "firefox"}]}}'); $this->session = $sessionData['sessionId']; - echo 'Session: '. $this->session. "\n"; } + return $this->session; } /** diff --git a/scriptedbrowser/main.php b/scriptedbrowser/main.php index b918919..976058a 100644 --- a/scriptedbrowser/main.php +++ b/scriptedbrowser/main.php @@ -105,9 +105,8 @@ class Main * @param string $serverHost server host name * @param int $serverPort server port * @param int $display VNC display id - * @param string $session existing session id from geckodriver */ - public function __construct($serverHost, $serverPort, $display = 6, $session = null) + public function __construct($serverHost, $serverPort, $display = 6) { require 'commands/base.php'; $this->display = $display; @@ -117,7 +116,21 @@ class Main // open xdotool $this->control->openInput(); // open/connect to browser - $this->control->openGeckoDriver($session); + $sessionFile = @fopen('.session', 'r'); + $session = null; + if ($sessionFile !== false) { + $session = trim(fgets($sessionFile)); + fclose($sessionFile); + if ($session == '') { + $session = null; + } + } + $newSession = $this->control->openGeckoDriver($session); + if ($newSession !== $session) { + $sessionFile = fopen('.session', 'w'); + fwrite($sessionFile, $newSession."\n"); + fclose($sessionFile); + } $this->control->mousemove(959, 499); $windowpos = $this->control->fullscreen(); $this->control->mousemove(960, 500);