From 4afec792c4e7b4f9596443d83f7d43c86ceeb20d Mon Sep 17 00:00:00 2001 From: throwaway Date: Fri, 22 Mar 2024 00:34:50 -0700 Subject: [PATCH 1/2] update php and improve docker docs --- Dockerfile | 4 +- docker/docker-entrypoint.sh | 15 ++-- docs/docker.md | 151 +++++++++++++++++++++++++++++------- 3 files changed, 137 insertions(+), 33 deletions(-) diff --git a/Dockerfile b/Dockerfile index 62638c9..bb0e98b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM alpine:latest WORKDIR /var/www/html/4get RUN apk update && apk upgrade -RUN apk add php apache2-ssl php82-fileinfo php82-openssl php82-iconv php82-common php82-dom php82-curl curl php82-pecl-apcu php82-apache2 imagemagick php82-pecl-imagick php-mbstring imagemagick-webp imagemagick-jpeg +RUN apk add php apache2-ssl php83-fileinfo php83-openssl php83-iconv php83-common php83-dom php83-curl curl php83-pecl-apcu php83-apache2 imagemagick php83-pecl-imagick php-mbstring imagemagick-webp imagemagick-jpeg COPY ./docker/apache/ /etc/apache2/ COPY . . @@ -12,4 +12,6 @@ RUN chmod 777 /var/www/html/4get/icons EXPOSE 80 EXPOSE 443 +ENV FOURGET_PROTO=http + CMD ["./docker/docker-entrypoint.sh"] diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 0a41ddd..ca0c4bc 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -1,14 +1,19 @@ #!/bin/sh set -e -if [ ! -f /etc/4get/certs/fullchain.pem ] || [ ! -f /etc/4get/certs/privkey.pem ]; then - echo "Using http configuration" - cp /etc/apache2/http.conf /etc/apache2/httpd.conf -else + +# remove quotes from variable if present +FOURGET_PROTO="${FOURGET_PROTO%\"}" +FOURGET_PROTO="${FOURGET_PROTO#\"}" + +if [ "$FOURGET_PROTO" = "https" ] || [ -f /etc/4get/certs/fullchain.pem ] || [ -f /etc/4get/certs/privkey.pem ]; then echo "Using https configuration" cp /etc/apache2/https.conf /etc/apache2/httpd.conf +else + echo "Using http configuration" + cp /etc/apache2/http.conf /etc/apache2/httpd.conf fi -php82 ./docker/gen_config.php +php ./docker/gen_config.php echo "4get is running" diff --git a/docs/docker.md b/docs/docker.md index 2aabd9f..2507bcf 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -1,33 +1,65 @@ -# Install guide for Docker +#### Install guide for Docker + +When using docker container any environment variables prefixed with `FOURGET_` will be added to the generated config located at `/var/www/html/4get/data/config.php` + +#### Special environment variables + +| Name | value | Example | +| - | - | - | +| FOURGET_PROTO | "http" or "https" | "https" | +| FOURGET_INSTANCES | comma separated string of urls | "https://4get.ca,https://domain.tld" | + +#### Important directories + +| Mountpoint | Description | +| - | - | +| /etc/4get/certs | SSL certificate directory | +| /var/www/html/4get/banner | Custom Banners directory | +| /var/www/html/4get/data/captcha | Captcha dataset | + + +the certificate directory `/etc/4get/certs` expects files named `fullchain.pem` and `privkey.pem` + +The captcha dataset should have a subdirectory for each category. In each category, images should be named from 1.png to X.png, and be 100x100 in size. + +example directory structure: ``` -docker run -d -p 80:80 -e FOURGET_SERVER_NAME="4get.ca" luuul/4get:latest +captcha/ + birds/ + 1.png + 2.png + 3.png + anime/ + 1.png + 2.png +``` + +For more information on configuration view [data/config.php](../data/config.php) + +#### Usage + +You can start 4get with + +``` +docker run -d -p 80:80 -e FOURGET_SERVER_NAME="4get.ca" -e FOURGET_PROTO="http" luuul/4get:latest ``` ...Or with SSL: -``` -docker run -d -p 443:443 -v /etc/letsencrypt/live/domain.tld:/etc/4get/certs -e FOURGET_SERVER_NAME="4get.ca" luuul/4get:latest -``` - -if the certificate files are not mounted to /etc/4get/certs the service listens to port 80 - -the certificate directory expects files named `fullchain.pem` and `privkey.pem` - -# Install using Docker Compose - -copy `docker-compose.yaml` - -to serve custom banners create a directory named `banners` for example with images and mount to `/var/www/html/4get/banner` - -to serve captcha images create a directory named `captchas` for example containing subfolders with images and mount to `/var/www/html/4get/data/captcha` - -any environment variables prefixed with `FOURGET_` will be added to the generated config - -the entrypoint will automatically set the `CAPTCHA_DATASET` value for you based on directory names and number of files in each - -to set `INSTANCES` pass a comma separated string of urls (FOURGET_INSTANCES = "https://4get.ca,https://domain.tld") ``` +docker run -d -p 443:443 -e FOURGET_SERVER_NAME="4get.ca" -e FOURGET_PROTO="https" -v /etc/letsencrypt/live/domain.tld:/etc/4get/certs luuul/4get:latest +``` + + +#### With Docker Compose + +Replace relevant values and start with `docker compose up -d` + +##### HTTP + +``` +# docker-compose.yaml version: "3.7" services: @@ -35,16 +67,81 @@ services: image: luuul/4get:latest restart: always environment: + - FOURGET_VERSION=6 + - FOURGET_PROTO=http + - FOURGET_SERVER_NAME=4get.ca + + ports: + - "80:80" +``` + +##### HTTPS + +``` +# docker-compose.yaml +version: "3.7" + +services: + fourget: + image: luuul/4get:latest + restart: always + environment: + - FOURGET_VERSION=6 + - FOURGET_PROTO=https - FOURGET_SERVER_NAME=4get.ca ports: - "80:80" - "443:443" - + volumes: - /etc/letsencrypt/live/domain.tld:/etc/4get/certs - - ./banners:/var/www/html/4get/banner - - ./captchas:/var/www/html/4get/data/captcha ``` -Replace relevant values and start with `docker compose up -d` +##### Captcha Enabled + +Set `FOURGET_BOT_PROTECTION=1` and mount a directory containing captcha files to `/var/www/html/4get/data/captcha` + + +``` +# docker-compose.yaml +version: "3.7" + +services: + fourget: + image: luuul/4get:latest + restart: always + environment: + - FOURGET_VERSION=6 + - FOURGET_PROTO=http + - FOURGET_SERVER_NAME=4get.ca + - FOURGET_BOT_PROTECTION=1 + + ports: + - "80:80" + + volumes: + - ./captcha:/var/www/html/4get/data/captcha +``` + +##### Custom Banners + +``` +# docker-compose.yaml +version: "3.7" + +services: + fourget: + image: luuul/4get:latest + restart: always + environment: + - FOURGET_VERSION=6 + - FOURGET_PROTO=http + - FOURGET_SERVER_NAME=4get.ca + + ports: + - "80:80" + + volumes: + - ./banners:/var/www/html/4get/banner +``` From e31b9494af16bf833d14b6256ca055fa94b6f77b Mon Sep 17 00:00:00 2001 From: throwaway Date: Fri, 19 Apr 2024 13:47:02 -0700 Subject: [PATCH 2/2] pass array as comma separated env --- docker/docker-entrypoint.sh | 4 ++++ docker/gen_config.php | 31 +++++++++++++++++++------------ docs/docker.md | 15 ++++++++++----- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index ca0c4bc..bdb706a 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -5,6 +5,10 @@ set -e FOURGET_PROTO="${FOURGET_PROTO%\"}" FOURGET_PROTO="${FOURGET_PROTO#\"}" +# make lowercase +FOURGET_PROTO=`echo $FOURGET_PROTO | awk '{print tolower($0)}'` + + if [ "$FOURGET_PROTO" = "https" ] || [ -f /etc/4get/certs/fullchain.pem ] || [ -f /etc/4get/certs/privkey.pem ]; then echo "Using https configuration" cp /etc/apache2/https.conf /etc/apache2/httpd.conf diff --git a/docker/gen_config.php b/docker/gen_config.php index cb12e3a..cc43916 100644 --- a/docker/gen_config.php +++ b/docker/gen_config.php @@ -3,19 +3,21 @@ include "/var/www/html/4get/data/config.php"; $refl = new ReflectionClass('config'); -$config = ($refl->getConstants()); +$from_config = ($refl->getConstants()); +$from_env = array(); $env = getenv(); $fourget_env = array_filter($env, function($v, $k) { return str_starts_with($k, "FOURGET"); }, ARRAY_FILTER_USE_BOTH); - foreach($fourget_env as $key => $val) { $target_key = preg_replace('/^FOURGET_/', '', $key); - $config[$target_key] = trim($val, '\'"'); + $from_env[$target_key] = trim($val, '\'"'); }; +$merged_config = array_merge($from_config, $from_env); + function type_to_string($n) { $type = gettype($n); if ($type === "NULL") { @@ -52,26 +54,31 @@ function detect_captcha_dirs() { } -$special_keys = ["CAPTCHA_DATASET", "INSTANCES"]; +$special_keys = ["PROTO", "CAPTCHA_DATASET"]; $output = " $val){ +foreach(($merged_config) as $key => $val){ if(!in_array($key, $special_keys)) { -$output = $output . "\tconst " . $key . " = " . type_to_string($val) . ";\n"; -continue; + + // conversion between arrays and comma separated env value. + // If original type of field is array and there is a type mismatch such as when a comma separted string is passed, + // then split on comma if string and not numeric + if(gettype($from_config[$key]) != gettype($val) && !is_numeric($val)) { + $data = gettype($val) === "string" ? explode(",", $val) : $val; + $output = $output . "\tconst " . $key . " = " . type_to_string($data) . ";\n"; + } else { + $output = $output . "\tconst " . $key . " = " . type_to_string($val) . ";\n"; + } + + continue; } if($key === "CAPTCHA_DATASET") { $output = $output . "\tconst " . $key . " = " . type_to_string(detect_captcha_dirs()) . ";\n"; } - if($key === "INSTANCES") { - $instances_list = gettype($val) === "string" ? explode(",", $val) : $val; - $output = $output . "\tconst " . $key . " = " . type_to_string($instances_list) . ";\n"; - } - } $output = $output . "}\n"; diff --git a/docs/docker.md b/docs/docker.md index 2507bcf..e56b5ca 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -2,12 +2,17 @@ When using docker container any environment variables prefixed with `FOURGET_` will be added to the generated config located at `/var/www/html/4get/data/config.php` +When lists of data is expected in [data/config.php](../data/config.php), such as `INSTANCES`, you can pass in a comma separated string via environment variable. + +Example: +`FOURGET_INSTANCES="https://4get.ca,https://domain.tld"` + #### Special environment variables | Name | value | Example | | - | - | - | | FOURGET_PROTO | "http" or "https" | "https" | -| FOURGET_INSTANCES | comma separated string of urls | "https://4get.ca,https://domain.tld" | + #### Important directories @@ -65,7 +70,7 @@ version: "3.7" services: fourget: image: luuul/4get:latest - restart: always + restart: unless-stopped environment: - FOURGET_VERSION=6 - FOURGET_PROTO=http @@ -84,7 +89,7 @@ version: "3.7" services: fourget: image: luuul/4get:latest - restart: always + restart: unless-stopped environment: - FOURGET_VERSION=6 - FOURGET_PROTO=https @@ -110,7 +115,7 @@ version: "3.7" services: fourget: image: luuul/4get:latest - restart: always + restart: unless-stopped environment: - FOURGET_VERSION=6 - FOURGET_PROTO=http @@ -133,7 +138,7 @@ version: "3.7" services: fourget: image: luuul/4get:latest - restart: always + restart: unless-stopped environment: - FOURGET_VERSION=6 - FOURGET_PROTO=http