164 lines
5.2 KiB
Awk
164 lines
5.2 KiB
Awk
# =============================================================================
|
|
# parse-sites.awk
|
|
# -----------------------------------------------------------------------------
|
|
# Liest config/sites.yaml (fester, dokumentierter Formatstil - siehe Kopf der
|
|
# sites.yaml) und gibt eine einfache, tab-getrennte Zwischenform aus, die
|
|
# generate.sh mit reinem Bash weiterverarbeitet:
|
|
#
|
|
# GLOBAL<TAB>acme_email
|
|
# DOMAIN<TAB>name<TAB>backend<TAB>backend_scheme<TAB>backend_tls_insecure<TAB>tls_mode<TAB>allowed_ips(komma-getrennt)
|
|
# TCP<TAB>name<TAB>listen_port<TAB>backend<TAB>allowed_ips(komma-getrennt)
|
|
#
|
|
# Bewusst ohne gawk-spezifische Features (kein 3-arg match(), kein gensub())
|
|
# geschrieben, damit es auch mit mawk oder busybox awk (Alpine) läuft.
|
|
# =============================================================================
|
|
|
|
function trim(s) {
|
|
sub(/^[ \t\r]+/, "", s)
|
|
sub(/[ \t\r]+$/, "", s)
|
|
return s
|
|
}
|
|
|
|
function stripq(s) {
|
|
gsub(/"/, "", s)
|
|
return s
|
|
}
|
|
|
|
function flush_domain() {
|
|
if (in_domain) {
|
|
ips = ""
|
|
for (i = 1; i <= n_ips; i++) { ips = ips (i > 1 ? "," : "") ips_arr[i] }
|
|
printf "DOMAIN\t%s\t%s\t%s\t%s\t%s\t%s\n", d_name, d_backend, d_scheme, d_tls_insecure, d_tls_mode, ips
|
|
in_domain = 0
|
|
}
|
|
}
|
|
|
|
function flush_tcp() {
|
|
if (in_tcp) {
|
|
ips = ""
|
|
for (i = 1; i <= n_ips; i++) { ips = ips (i > 1 ? "," : "") ips_arr[i] }
|
|
printf "TCP\t%s\t%s\t%s\t%s\n", t_name, t_port, t_backend, ips
|
|
in_tcp = 0
|
|
}
|
|
}
|
|
|
|
BEGIN {
|
|
section = ""
|
|
in_domain = 0
|
|
in_tcp = 0
|
|
in_allowed_ips = 0
|
|
acme_email = ""
|
|
}
|
|
|
|
{
|
|
line = $0
|
|
sub(/\r$/, "", line)
|
|
|
|
# volle Kommentarzeilen und Leerzeilen überspringen
|
|
if (line ~ /^[ \t]*#/ || line ~ /^[ \t]*$/) next
|
|
|
|
# Einrückung ermitteln (Anzahl führender Leerzeichen)
|
|
stripped = line
|
|
gsub(/^ */, "", stripped)
|
|
indent = length(line) - length(stripped)
|
|
content = stripped
|
|
|
|
# Top-Level-Abschnitt
|
|
if (indent == 0) {
|
|
flush_domain()
|
|
flush_tcp()
|
|
if (content ~ /^global:/) { section = "global" }
|
|
else if (content ~ /^domains:/) { section = "domains" }
|
|
else if (content ~ /^tcp_services:/) { section = "tcp_services" }
|
|
else { section = "" }
|
|
next
|
|
}
|
|
|
|
if (section == "global" && indent == 2) {
|
|
if (content ~ /^acme_email:/) {
|
|
val = content
|
|
sub(/^acme_email:[ \t]*/, "", val)
|
|
acme_email = stripq(trim(val))
|
|
}
|
|
next
|
|
}
|
|
|
|
if (section == "domains") {
|
|
if (indent == 2 && content ~ /^- name:/) {
|
|
flush_domain()
|
|
in_domain = 1
|
|
in_allowed_ips = 0
|
|
n_ips = 0
|
|
d_backend = ""
|
|
d_scheme = ""
|
|
d_tls_insecure = "false"
|
|
d_tls_mode = "auto"
|
|
val = content
|
|
sub(/^- name:[ \t]*/, "", val)
|
|
d_name = stripq(trim(val))
|
|
next
|
|
}
|
|
if (in_domain && indent == 4) {
|
|
if (content ~ /^backend:/) {
|
|
val = content; sub(/^backend:[ \t]*/, "", val); d_backend = stripq(trim(val))
|
|
in_allowed_ips = 0; next
|
|
}
|
|
if (content ~ /^backend_scheme:/) {
|
|
val = content; sub(/^backend_scheme:[ \t]*/, "", val); d_scheme = stripq(trim(val))
|
|
in_allowed_ips = 0; next
|
|
}
|
|
if (content ~ /^backend_tls_insecure:/) {
|
|
val = content; sub(/^backend_tls_insecure:[ \t]*/, "", val); d_tls_insecure = stripq(trim(val))
|
|
in_allowed_ips = 0; next
|
|
}
|
|
if (content ~ /^tls_mode:/) {
|
|
val = content; sub(/^tls_mode:[ \t]*/, "", val); d_tls_mode = stripq(trim(val))
|
|
in_allowed_ips = 0; next
|
|
}
|
|
if (content ~ /^allowed_ips:/) { in_allowed_ips = 1; next }
|
|
}
|
|
if (in_domain && in_allowed_ips && indent == 6 && content ~ /^- /) {
|
|
val = content; sub(/^- [ \t]*/, "", val)
|
|
n_ips++; ips_arr[n_ips] = stripq(trim(val))
|
|
next
|
|
}
|
|
}
|
|
|
|
if (section == "tcp_services") {
|
|
if (indent == 2 && content ~ /^- name:/) {
|
|
flush_tcp()
|
|
in_tcp = 1
|
|
in_allowed_ips = 0
|
|
n_ips = 0
|
|
t_port = ""
|
|
t_backend = ""
|
|
val = content
|
|
sub(/^- name:[ \t]*/, "", val)
|
|
t_name = stripq(trim(val))
|
|
next
|
|
}
|
|
if (in_tcp && indent == 4) {
|
|
if (content ~ /^listen_port:/) {
|
|
val = content; sub(/^listen_port:[ \t]*/, "", val); t_port = stripq(trim(val))
|
|
in_allowed_ips = 0; next
|
|
}
|
|
if (content ~ /^backend:/) {
|
|
val = content; sub(/^backend:[ \t]*/, "", val); t_backend = stripq(trim(val))
|
|
in_allowed_ips = 0; next
|
|
}
|
|
if (content ~ /^allowed_ips:/) { in_allowed_ips = 1; next }
|
|
}
|
|
if (in_tcp && in_allowed_ips && indent == 6 && content ~ /^- /) {
|
|
val = content; sub(/^- [ \t]*/, "", val)
|
|
n_ips++; ips_arr[n_ips] = stripq(trim(val))
|
|
next
|
|
}
|
|
}
|
|
}
|
|
|
|
END {
|
|
flush_domain()
|
|
flush_tcp()
|
|
printf "GLOBAL\t%s\n", acme_email
|
|
}
|