2015-10-08 23:11:29 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
2016-10-20 18:54:38 +00:00
|
|
|
with import <stockholm/lib>;
|
2015-10-08 23:11:29 +00:00
|
|
|
|
|
|
|
let
|
2015-11-13 00:16:15 +00:00
|
|
|
cfg = config.krebs.go;
|
2015-10-08 23:11:29 +00:00
|
|
|
|
|
|
|
out = {
|
2015-11-13 00:16:15 +00:00
|
|
|
options.krebs.go = api;
|
2016-02-14 15:43:44 +00:00
|
|
|
config = lib.mkIf cfg.enable imp;
|
2015-10-08 23:11:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
api = {
|
|
|
|
enable = mkEnableOption "Enable go url shortener";
|
|
|
|
port = mkOption {
|
2021-01-07 23:38:34 +00:00
|
|
|
type = types.int;
|
|
|
|
default = 1337;
|
2015-10-08 23:11:29 +00:00
|
|
|
description = "on which port go should run on";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
imp = {
|
2021-04-25 15:34:23 +00:00
|
|
|
services.redis = {
|
|
|
|
enable = true;
|
|
|
|
};
|
|
|
|
|
2021-01-07 23:38:34 +00:00
|
|
|
krebs.htgen.go = {
|
|
|
|
port = cfg.port;
|
|
|
|
script = ''. ${pkgs.writeDash "go" ''
|
2021-04-25 15:34:23 +00:00
|
|
|
set -x
|
2015-10-08 23:11:29 +00:00
|
|
|
|
2021-01-07 23:38:34 +00:00
|
|
|
case "$Method $Request_URI" in
|
|
|
|
"GET /"*)
|
2021-04-25 15:34:23 +00:00
|
|
|
if item=$(${pkgs.redis}/bin/redis-cli --raw get "''${Request_URI#/}"); then
|
2021-01-07 23:38:34 +00:00
|
|
|
printf 'HTTP/1.1 302 Found\r\n'
|
|
|
|
printf 'Content-Type: text/plain\r\n'
|
|
|
|
printf 'Connection: closed\r\n'
|
2021-04-25 15:34:23 +00:00
|
|
|
printf 'Location: %s\r\n' "$item"
|
2021-01-07 23:38:34 +00:00
|
|
|
printf '\r\n'
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
"POST /")
|
2022-01-24 13:36:22 +00:00
|
|
|
uri_candidate=$(head -c "$req_content_length" \
|
2021-01-07 23:38:34 +00:00
|
|
|
| sed 's/+/ /g;s/%\(..\)/\\x\1/g;' \
|
|
|
|
| xargs -0 echo -e \
|
2022-01-24 13:36:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if $(echo "$uri_candidate" | grep -q '^uri=//.*'); then
|
|
|
|
# fix urls with missing https: in front
|
|
|
|
uri_candidate=$(echo "$uri_candidate" | sed 's,//,https://,g')
|
|
|
|
fi
|
|
|
|
|
|
|
|
uri=$(echo "$uri_candidate" | ${pkgs.urix}/bin/urix \
|
2021-01-07 23:38:34 +00:00
|
|
|
| head -1 \
|
2021-04-25 15:34:23 +00:00
|
|
|
)
|
2015-10-08 23:11:29 +00:00
|
|
|
|
2021-04-25 15:34:23 +00:00
|
|
|
sha256=$(echo "$uri" | sha256sum -b | cut -d\ -f1)
|
|
|
|
base32=$(${pkgs.nixStable}/bin/nix-hash --to-base32 --type sha256 "$sha256")
|
2021-04-30 09:29:24 +00:00
|
|
|
base32short=$(echo "$base32" | cut -c48-52)
|
2021-04-25 15:34:23 +00:00
|
|
|
${pkgs.redis}/bin/redis-cli set "$base32short" "$uri" >/dev/null
|
2015-10-08 23:11:29 +00:00
|
|
|
|
2021-04-25 15:34:23 +00:00
|
|
|
ref="http://$req_host/$base32short"
|
2015-10-08 23:11:29 +00:00
|
|
|
|
2021-01-07 23:38:34 +00:00
|
|
|
printf 'HTTP/1.1 200 OK\r\n'
|
|
|
|
printf 'Content-Type: text/plain; charset=UTF-8\r\n'
|
|
|
|
printf 'Connection: close\r\n'
|
|
|
|
printf '\r\n'
|
|
|
|
printf '%s\n' "$ref"
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
''}'';
|
2015-10-08 23:11:29 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
in out
|