diff --git a/.env.sample b/.env.sample index 5d6e040..0e12b15 100644 --- a/.env.sample +++ b/.env.sample @@ -15,6 +15,20 @@ DATABASE_URL="mysql://root:letmein@localhost:3306/webcron?charset=UTF8" ## Sqlite sample DATABASE_URL="sqlite:///storage/database.sqlite" +################# +### DEMO-MODE ### +################# + +## Demo mode is used for showcasing the application. Demo mode will create a warning on every page and won't run your cronjobs +## As this will also display credentials on the login page, you should avoid enabling this on production environmnents +## Why would you want to do that anyway? +DEMO_MODE=false + +## The username and password of the demo mode cannot be read from the database. Therefore we are putting these in plain-text here. +## They should be available anyway +DEMO_USER=example@example.com +DEMO_PASS=password + ################## ### ENCRYPTION ### ################## @@ -27,10 +41,8 @@ SECRET=ImNotThatSecretSoPleaseChangeMe0123456789 ## Please see https://www.php.net/openssl-get-cipher-methods for possible values ENCRYPTION_METHOD="AES-256-CBC" -## Hashing methods algorithms to save passwords. -## By saving a hash instead of plain-text password your password is kept safe as hashing is considered to be irreversable -## Additionally when saving a secret variable, a hash of the secret value is calculated as well to ensure -## the encrypted data is not tampered +## Encryption can only be secure if you are sure it is not tampered. +## The value below is the hashing algorithm that is used to verify the encrypted date HASHING_METHOD="sha256" ################### diff --git a/src/Repository/JobRepository.php b/src/Repository/JobRepository.php index 7e3515a..bc439f0 100644 --- a/src/Repository/JobRepository.php +++ b/src/Repository/JobRepository.php @@ -13,6 +13,8 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use phpseclib3\Crypt\PublicKeyLoader; use phpseclib3\Net\SSH2; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\HttpKernelInterface; /** * @@ -185,6 +187,14 @@ class JobRepository extends EntityRepository */ private function runHttpJob(Job &$job): array { + if(isset($_ENV['DEMO_MODE']) && $_ENV['DEMO_MODE']) { + $exitcodes = [...array_fill(0,120, $job->getData('http-status')[0]), ...array_keys(Response::$statusTexts)]; + $return['exitcode'] = $exitcodes[random_int(0, 181)]; + $return['failed'] = !in_array($return['exitcode'], $job->getData('http-status')); + $return['output'] = 'Demo mode!'; + + return $return; + } $client = new Client(); $url = $job->getData('url'); @@ -218,6 +228,14 @@ class JobRepository extends EntityRepository */ private function runCommandJob(Job &$job): array { + if(isset($_ENV['DEMO_MODE']) && $_ENV['DEMO_MODE']) { + $exitcodes = [...array_fill(0,400, $job->getData('response')), ...range(0, 255)]; + $return['exitcode'] = $exitcodes[random_int(0, 655)]; + $return['failed'] = !in_array($return['exitcode'], $job->getData('response')); + $return['output'] = 'Demo mode!'; + + return $return; + } $command = $job->getData('command'); if(!empty($job->getData('vars'))) { foreach ($job->getData('vars') as $key => $var) { @@ -303,6 +321,13 @@ class JobRepository extends EntityRepository { $em = $this->getEntityManager(); if($job->getRunning() == 1) { + if(isset($_ENV['DEMO_MODE']) && $_ENV['DEMO_MODE']) { + $job->setRunning(time() + $job->getData('reboot-delay-secs') + ($job->getData('reboot-duration') * 60)); + $em->persist($job); + $em->flush(); + + return ['status' => 'deferred']; + } $this->setTempVar($job, 'starttime', $starttime); $this->setTempVar($job, 'manual', $manual); $rebootcommand = $job->getData('reboot-command'); @@ -337,6 +362,16 @@ class JobRepository extends EntityRepository if($job->getRunning() > time()) { return ['status' => 'deferred']; } + if(isset($_ENV['DEMO_MODE']) && $_ENV['DEMO_MODE']) { + $exitcodes = [...array_fill(0,400, $job->getData('getservices-response')), ...range(0, 255)]; + $return['exitcode'] = $exitcodes[random_int(0, 655)]; + $return['failed'] = !in_array($return['exitcode'], $job->getData('getservices-response')); + $return['output'] = 'Demo mode!'; + $job->setRunning(1); + $em->persist($job); + $em->flush(); + return $return; + } $starttime = (float)$this->getTempVar($job, 'starttime'); $this->deleteTempVar($job, 'starttime'); $manual = $this->getTempVar($job, 'manual'); diff --git a/templates/flashes.html.twig b/templates/flashes.html.twig index a357ef7..dab3f78 100644 --- a/templates/flashes.html.twig +++ b/templates/flashes.html.twig @@ -7,3 +7,10 @@ {% endfor %} {% endfor %} +{% if app.request.server.get('DEMO_MODE') %} +
{{ 'demomode.credentials.header' | trans }}
+{{ 'demomode.credentials.username' | trans }}: {{ app.request.server.get('DEMO_USER') }}
+{{ 'demomode.credentials.password' | trans }}: {{ app.request.server.get('DEMO_PASS') }}
+