From 5cbb9aeb7249c51e7a6f4075695e0ca1ec9e0296 Mon Sep 17 00:00:00 2001 From: lassulus Date: Wed, 18 May 2022 16:57:57 +0200 Subject: l: move radio to subfolder --- lass/2configs/radio/news.nix | 120 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 lass/2configs/radio/news.nix (limited to 'lass/2configs/radio/news.nix') diff --git a/lass/2configs/radio/news.nix b/lass/2configs/radio/news.nix new file mode 100644 index 000000000..4f88197cb --- /dev/null +++ b/lass/2configs/radio/news.nix @@ -0,0 +1,120 @@ +{ config, lib, pkgs, ... }: +let + weather_report = pkgs.writers.writeDashBin "weather_report" '' + set -efu + export PATH="${lib.makeBinPath [ + pkgs.iproute2 + pkgs.coreutils + pkgs.jq + pkgs.curl + pkgs.gnugrep + pkgs.gnused + ]}" + ss -Hno state established 'sport = :8000' | + grep '^tcp' | sed 's/.*\[.*\].*\[\(::ffff:\)\{0,1\}\(.*\)\].*/\2/' | + sed '/127.0.0.1/d;/:/d' | + while read -r ip; do + curl -sSL "https://wttr.in/@$ip?format=j1" + done | jq -rs 'unique_by(.nearest_area[0].areaName[0].value) | + map((.nearest_area[0] | + "Weather report for \(.areaName[0].value), \(.country[0].value).") + + (.current_condition[0] | + " Currently it is \(.weatherDesc[0].value) outside with a temperature of \(.temp_C) degrees." + ) + ) | unique | .[]' + ' + ''; + send_to_radio = pkgs.writers.writeDashBin "send_to_radio" '' + ${pkgs.vorbisTools}/bin/oggenc - | + ${pkgs.libshout}/bin/shout --format ogg --host localhost --port 1338 --mount /live + ''; + + gc_news = pkgs.writers.writeDashBin "gc_news" '' + set -xefu + ${pkgs.coreutils}/bin/cat $HOME/news | ${pkgs.jq}/bin/jq -cs 'map(select((.to|fromdateiso8601) > now)) | .[]' > $HOME/bla-news.tmp + ${pkgs.coreutils}/bin/mv $HOME/bla-news.tmp $HOME/news + ''; + + get_current_news = pkgs.writers.writeDashBin "get_current_news" '' + set -xefu + ${pkgs.coreutils}/bin/cat $HOME/news | ${pkgs.jq}/bin/jq -rs 'map(select(((.to | fromdateiso8601) > now) and (.from|fromdateiso8601) < now) | .text) | .[]' + ''; + + newsshow = pkgs.writers.writeDashBin "newsshow" /* sh */ '' + cat << EOF + hello crabpeople! + $(${pkgs.ddate}/bin/ddate +'Today is %{%A, the %e of %B%}, %Y. %N%nCelebrate %H') + It is $(date --utc +%H) o clock UTC. + todays news: + $(get_current_news) + $(gc_news) + $(weather_report) + EOF + ''; +in +{ + systemd.services.newsshow = { + path = [ + newsshow + send_to_radio + gc_news + get_current_news + weather_report + pkgs.curl + pkgs.retry + ]; + script = '' + set -efu + retry -t 5 -d 10 -- newsshow | + retry -t 5 -d 10 -- curl -fSsG http://tts.r/api/tts --data-urlencode 'text@-' | + retry -t 5 -d 10 -- send_to_radio + ''; + startAt = "*:00:00"; + serviceConfig = { + User = "radio-news"; + }; + }; + + krebs.iptables.tables.filter.INPUT.rules = [ + { predicate = "-p tcp --dport 7999"; target = "ACCEPT"; } + ]; + + krebs.htgen.news = { + port = 7999; + user = { + name = "radio-news"; + }; + script = ''. ${pkgs.writers.writeDash "htgen-news" '' + set -xefu + case "''${Method:-GET} $Request_URI" in + "GET /") + printf 'HTTP/1.1 200 OK\r\n' + printf 'Access-Control-Allow-Origin: *\r\n'; + printf 'Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n'; + printf 'Connection: close\r\n' + printf '\r\n' + cat "$HOME"/news | jq -sc . + exit + ;; + "POST /") + payload=$(head -c "$req_content_length") + echo "$payload" | jq 'has("from") and has("to") and has("text")' >&2 + echo "$payload" | jq -c '{ from: (.from | fromdate | todate), to: (.to | fromdate | todate), text: .text }' >> "$HOME"/news + printf 'HTTP/1.1 200 OK\r\n' + printf 'Access-Control-Allow-Origin: *\r\n'; + printf 'Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n'; + printf 'Connection: close\r\n' + printf '\r\n' + exit + ;; + esac + ''}''; + }; + + ## debug + # environment.systemPackages = [ + # weather_report + # send_to_radio + # newsshow + # ]; +} -- cgit v1.2.3 From 5befc5a6f260e2283863212b39749d5ed05afb63 Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 19 May 2022 19:16:09 +0200 Subject: l radio news: use openweather api --- lass/2configs/radio/news.nix | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'lass/2configs/radio/news.nix') diff --git a/lass/2configs/radio/news.nix b/lass/2configs/radio/news.nix index 4f88197cb..6d73f4bb7 100644 --- a/lass/2configs/radio/news.nix +++ b/lass/2configs/radio/news.nix @@ -1,29 +1,27 @@ { config, lib, pkgs, ... }: let + weather_for_ips = pkgs.writers.writePython3Bin "weather_for_ips" { + libraries = [ pkgs.python3Packages.geoip2 ]; + } ./weather_for_ips.py; + weather_report = pkgs.writers.writeDashBin "weather_report" '' set -efu export PATH="${lib.makeBinPath [ pkgs.iproute2 pkgs.coreutils - pkgs.jq pkgs.curl pkgs.gnugrep pkgs.gnused ]}" + curl -z /tmp/GeoLite2-City.mmdb -o /tmp/GeoLite2-City.mmdb http://c.r/GeoLite2-City.mmdb + MAXMIND_GEOIP_DB="/tmp/GeoLite2-City.mmdb"; export MAXMIND_GEOIP_DB + OPENWEATHER_API_KEY=$(cat "$CREDENTIALS_DIRECTORY/openweather_api"); export OPENWEATHER_API_KEY ss -Hno state established 'sport = :8000' | grep '^tcp' | sed 's/.*\[.*\].*\[\(::ffff:\)\{0,1\}\(.*\)\].*/\2/' | - sed '/127.0.0.1/d;/:/d' | - while read -r ip; do - curl -sSL "https://wttr.in/@$ip?format=j1" - done | jq -rs 'unique_by(.nearest_area[0].areaName[0].value) | - map((.nearest_area[0] | - "Weather report for \(.areaName[0].value), \(.country[0].value).") - + (.current_condition[0] | - " Currently it is \(.weatherDesc[0].value) outside with a temperature of \(.temp_C) degrees." - ) - ) | unique | .[]' - ' + sed '/127.0.0.1/d;/::1/d' | + ${weather_for_ips}/bin/weather_for_ips ''; + send_to_radio = pkgs.writers.writeDashBin "send_to_radio" '' ${pkgs.vorbisTools}/bin/oggenc - | ${pkgs.libshout}/bin/shout --format ogg --host localhost --port 1338 --mount /live @@ -72,6 +70,9 @@ in startAt = "*:00:00"; serviceConfig = { User = "radio-news"; + LoadCredential = [ + "openweather_api:${toString }/openweather_api_key" + ]; }; }; -- cgit v1.2.3 From 3b46460fe9f54c1ca06af31cfe715c0cb51ce1c3 Mon Sep 17 00:00:00 2001 From: lassulus Date: Sun, 22 May 2022 20:51:14 +0200 Subject: l radio: use jc instead of sed/grep --- lass/2configs/radio/news.nix | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'lass/2configs/radio/news.nix') diff --git a/lass/2configs/radio/news.nix b/lass/2configs/radio/news.nix index 6d73f4bb7..a3b39a9ea 100644 --- a/lass/2configs/radio/news.nix +++ b/lass/2configs/radio/news.nix @@ -7,18 +7,22 @@ let weather_report = pkgs.writers.writeDashBin "weather_report" '' set -efu export PATH="${lib.makeBinPath [ - pkgs.iproute2 pkgs.coreutils pkgs.curl - pkgs.gnugrep - pkgs.gnused + pkgs.iproute2 + pkgs.jc + pkgs.jq ]}" curl -z /tmp/GeoLite2-City.mmdb -o /tmp/GeoLite2-City.mmdb http://c.r/GeoLite2-City.mmdb MAXMIND_GEOIP_DB="/tmp/GeoLite2-City.mmdb"; export MAXMIND_GEOIP_DB OPENWEATHER_API_KEY=$(cat "$CREDENTIALS_DIRECTORY/openweather_api"); export OPENWEATHER_API_KEY - ss -Hno state established 'sport = :8000' | - grep '^tcp' | sed 's/.*\[.*\].*\[\(::ffff:\)\{0,1\}\(.*\)\].*/\2/' | - sed '/127.0.0.1/d;/::1/d' | + ss -no 'sport = :8000' | + jc --ss | jq -r '.[] | + select( + .local_address != "[::ffff:127.0.0.1]" + and .local_address != "[::1]" + ) | .peer_address | gsub("[\\[\\]]"; "") + ' | ${weather_for_ips}/bin/weather_for_ips ''; -- cgit v1.2.3 From 1c02415b6e95c0fbb920fd2bfb5f4b975f1c7fbe Mon Sep 17 00:00:00 2001 From: lassulus Date: Wed, 25 May 2022 14:59:08 +0200 Subject: l radio: add radio-news.r alias --- lass/2configs/radio/news.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lass/2configs/radio/news.nix') diff --git a/lass/2configs/radio/news.nix b/lass/2configs/radio/news.nix index a3b39a9ea..5c6c5d33a 100644 --- a/lass/2configs/radio/news.nix +++ b/lass/2configs/radio/news.nix @@ -84,6 +84,16 @@ in { predicate = "-p tcp --dport 7999"; target = "ACCEPT"; } ]; + services.nginx.virtualHosts."radio-news.r" = { + locations."/" = { + proxyPass = "http://localhost:7999"; + proxyWebsockets = true; + extraConfig = '' + add_header 'Access-Control-Allow-Origin' '*'; + add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; + ''; + }; + }; krebs.htgen.news = { port = 7999; user = { -- cgit v1.2.3 From d9b09371ecd31e35bd8bbd1d9e929237e52132b1 Mon Sep 17 00:00:00 2001 From: lassulus Date: Wed, 25 May 2022 17:42:56 +0200 Subject: l radio news: remove duplicate CORS headers --- lass/2configs/radio/news.nix | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lass/2configs/radio/news.nix') diff --git a/lass/2configs/radio/news.nix b/lass/2configs/radio/news.nix index 5c6c5d33a..80e5f9cc8 100644 --- a/lass/2configs/radio/news.nix +++ b/lass/2configs/radio/news.nix @@ -104,8 +104,6 @@ in case "''${Method:-GET} $Request_URI" in "GET /") printf 'HTTP/1.1 200 OK\r\n' - printf 'Access-Control-Allow-Origin: *\r\n'; - printf 'Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n'; printf 'Connection: close\r\n' printf '\r\n' cat "$HOME"/news | jq -sc . @@ -116,8 +114,6 @@ in echo "$payload" | jq 'has("from") and has("to") and has("text")' >&2 echo "$payload" | jq -c '{ from: (.from | fromdate | todate), to: (.to | fromdate | todate), text: .text }' >> "$HOME"/news printf 'HTTP/1.1 200 OK\r\n' - printf 'Access-Control-Allow-Origin: *\r\n'; - printf 'Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n'; printf 'Connection: close\r\n' printf '\r\n' exit -- cgit v1.2.3 From e92a58fe0f4edbf047ff4dd89866ae0991d37350 Mon Sep 17 00:00:00 2001 From: lassulus Date: Wed, 25 May 2022 17:44:56 +0200 Subject: l radio news: don't expose htgen directly --- lass/2configs/radio/news.nix | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lass/2configs/radio/news.nix') diff --git a/lass/2configs/radio/news.nix b/lass/2configs/radio/news.nix index 80e5f9cc8..27b124093 100644 --- a/lass/2configs/radio/news.nix +++ b/lass/2configs/radio/news.nix @@ -80,10 +80,6 @@ in }; }; - krebs.iptables.tables.filter.INPUT.rules = [ - { predicate = "-p tcp --dport 7999"; target = "ACCEPT"; } - ]; - services.nginx.virtualHosts."radio-news.r" = { locations."/" = { proxyPass = "http://localhost:7999"; -- cgit v1.2.3