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') %} + +{% endif %} \ No newline at end of file diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig index 35c5174..0ec0338 100644 --- a/templates/security/login.html.twig +++ b/templates/security/login.html.twig @@ -16,7 +16,7 @@ {{ include('flashes.html.twig') }} {% if error %} @@ -37,8 +37,16 @@ - + + {% if app.request.server.get('DEMO_MODE') %} + {# Demo mode credentials #} +
+

{{ 'demomode.credentials.header' | trans }}

+

{{ 'demomode.credentials.username' | trans }}: {{ app.request.server.get('DEMO_USER') }}

+

{{ 'demomode.credentials.password' | trans }}: {{ app.request.server.get('DEMO_PASS') }}

+
+ {% endif %} diff --git a/translations/messages.en-l33t.yaml b/translations/messages.en-l33t.yaml index 3dd8349..5252adf 100644 --- a/translations/messages.en-l33t.yaml +++ b/translations/messages.en-l33t.yaml @@ -1,5 +1,11 @@ title: "W3bcr0n m4n4g3m3nt" header: "W3bcr0n m4n4g3m3nt" +demomode: + flashnotice: "d1z 4ppL1c4t10n b 1n d3m0 m0d3. Ch4ng3z 1z p3rs1st3d 1n t3h d4t4b4s3, but j0bz 1z n0t b31n' 3x3cut3d" + credentials: + header: "L0g1n 4 d3m0 m0d3" + username: "Us3rn4m3" + password: "P4ssw0rd" menu: overview: "0v3rv13w" add: "4dd @ n3w cr0nj0b" diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index 5cf2124..52e7ac9 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -1,5 +1,11 @@ title: "Webcron management" header: "Webcron management" +demomode: + flashnotice: "This application is in demo mode. Changes are persisted in the database, but jobs are not being executed" + credentials: + header: "Login for demo mode" + username: "Username" + password: "Password" menu: overview: "Overview" add: "Add a new cronjob" diff --git a/translations/messages.nl.yaml b/translations/messages.nl.yaml index bd19b8a..9ee9b52 100644 --- a/translations/messages.nl.yaml +++ b/translations/messages.nl.yaml @@ -1,5 +1,12 @@ title: "Webcron management" header: "Webcron management" +demomode: + flashnotice: "Deze applicatie is in demo modus. Alle functies zijn actief, maar taken worden niet uitgevoerd" + credentials: + header: "Aanmeldgegevens voor demo modus:" + username: "Gebruikersnaam" + password: "Wachtwoord" + menu: overview: "Overzicht" add: "Taak toevoegen"