[mod] proxy nginx config is now generated based on domains.env

- domains and servers in domains.env are mapped 1:1
 - moved domains.env to ./files/domains.env
 - renamed kweb to web for a more generic approach
This commit is contained in:
2020-05-27 15:58:21 -04:00
parent 3d778ff698
commit f3d17e931a
8 changed files with 130 additions and 46 deletions

View File

@@ -9,9 +9,11 @@ server_retries=10
log_file="" # leave empty for stdout
# variables
config_file="$temp_path/nginx.conf"
domain_file="$temp_path/domains.txt"
certs_updated="$cert_root_path/.updated"
certs_daemon_pid=-1
server_configs=""
#functions
function log()
@@ -41,6 +43,24 @@ function log()
esac
}
# this is a helper function as dash does not support arrays
function get_element()
{
element="$1"
seperator="$2"; shift; shift
array=$@
old_IFS=$IFS
IFS="$seperator"
set -- $array
while [ "$element" -gt 0 ]; do
shift
element=$(( element - 1 ))
done
result=$1
IFS=$old_IFS
echo $result
}
function sleep_wait()
{
# waiting on sleep to be able to handle SIGTERM
@@ -85,13 +105,15 @@ function read_file()
local func=$1
local file=$2
local result=0
local count=0
while read -r line || [[ -n "$line" ]]; do
count=$(( count+1 ))
# skip empty lines if READ_EMPTY_LINES is not set
[ -z ${READ_EMPTY_LINES+x} ] && [ "${#line}" -lt 1 ] && continue
log read_file "LINE: $line"
log read_file "LINE[$count]: $line"
log read_file "FUNC START [$func]"
if $func $line; then
if $func $count $line; then
log read_file "FUNC END [$func]"
else
result=1
@@ -102,9 +124,77 @@ function read_file()
return $result
}
function rng()
{
rng_n=$(od -An -N2 -i < /dev/urandom)
echo $rng_n
}
function gen_server_config()
{
local server=$1; shift
local root_domain=$1; shift
local domain_list="$@"
local domain_count=0
local domains="$root_domain"
# no arrays in dash
local subdomain=$(get_element $domain_count " " $domain_list)
while [ ${#subdomain} -gt 0 ]; do
domains="$domains ${subdomain}.$root_domain"
domain_count=$(( domain_count + 1 ))
subdomain=$(get_element $domain_count " " $domain_list)
done
local rng_number=$(rng)
local server_var="
server {
listen 8443 ssl;
# prevent redirecting to 8080 port
# (e.g. when trailing slash is missed)
port_in_redirect off;
server_name $domains;
ssl_certificate /service/ssl/$root_domain/fullchain.pem;
ssl_certificate_key /service/ssl/$root_domain/privkey.pem;
location / {
# allow for upstream to be offline by setting
# docker dns resolver and a variable for the server
resolver 127.0.0.11 valid=30s;
set \$web_$rng_number $server;
proxy_pass http://\$web_$rng_number:8080;
proxy_redirect off;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
}
}
"
echo "$server_var"
}
function server_config()
{
local server_number=$1; shift
local server_name=$(get_element $(( server_number-1 )) " " $SERVER)
local server_domain=$@
if [ ${#server_name} -eq 0 ]; then
log server_config "no name for server name for domains [$server_domain]"
return 1
fi
log server_config "creating config for [$server_name] with domains [$server_domain]"
server_configs="$server_configs$(gen_server_config $server_name $server_domain)"
}
function cert_exists()
{
local domain=$1
local domain=$2
local cert_path="$cert_root_path/$domain"
[ -f "$cert_path/fullchain.pem" ]
}
@@ -143,6 +233,13 @@ function nginx_handler()
# start the daemon (it forks itself)
certs_daemon
# generate server config
read_file server_config "$domain_file"
tmp_file="$temp_path/tmp.conf"
echo -e "$server_configs" > $tmp_file
awk 'NR==FNR { a[n++]=$0; next } /#<SERVER>/ { for (i=0;i<n;++i) print a[i]; next }1' \
$tmp_file /etc/nginx/nginx.conf > "$config_file"
# wait for certs to exist before starting nginx
# just have nginx crash after n retries (default 10)
while ! read_file cert_exists "$domain_file" && [ "$retries" -lt "$server_retries" ]; do
@@ -153,7 +250,7 @@ function nginx_handler()
# starting nginx in foreground
log nginx_handler "Starting nginx in foreground"
exec nginx -g "daemon off;"
exec nginx -c "$config_file" -g "daemon off;"
}
function main()