summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/hidden-ssh.nix
blob: acbe717d9bf403842292bafca3a26e4d8665ae4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{ config, lib, pkgs, ... }:

with import <stockholm/lib>;
let
  cfg = config.krebs.hidden-ssh;

  out = {
    options.krebs.hidden-ssh = api;
    config = lib.mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "hidden SSH announce";
    channel = mkOption {
      type = types.str;
      default = "#krebs-announce";
    };
    server = mkOption {
      type = types.str;
      default = "irc.hackint.org";
    };
    port = mkOption {
      type = types.int;
      default = 6697;
    };
    tls = mkOption {
      type = types.bool;
      default = true;
    };
    message = mkOption {
      type = types.str;
      default = "SSH Hidden Service at ";
    };
  };

  imp = let
    torDirectory = "/var/lib/tor"; # from tor.nix
    hiddenServiceDir = torDirectory + "/onion/hidden-ssh";
  in {
    services.tor = {
      enable = true;
      relay.onionServices.hidden-ssh = {
        version = 3;
        map = [{
          port = 22;
          target.port = 22;
        }];
      };
      client.enable = true;
    };
    systemd.services.hidden-ssh-announce = {
      description = "irc announce hidden ssh";
      after = [ "tor.service" "network-online.target" ];
      wants = [ "tor.service" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        # ${pkgs.tor}/bin/torify
        ExecStart = pkgs.writeDash "irc-announce-ssh" ''
          set -efu
          until test -e ${hiddenServiceDir}/hostname; do
            echo "still waiting for ${hiddenServiceDir}/hostname"
            sleep 1
          done
          ${pkgs.untilport}/bin/untilport ${escapeShellArg cfg.server} ${toString cfg.port}

          ${pkgs.irc-announce}/bin/irc-announce \
            ${escapeShellArg cfg.server} \
            ${toString cfg.port} \
            "${config.krebs.build.host.name}-ssh" \
            ${escapeShellArg cfg.channel} \
            ${escapeShellArg cfg.tls} \
            "${cfg.message}$(cat ${hiddenServiceDir}/hostname)"
        '';
        PrivateTmp = "true";
        User = "tor";
        Type = "oneshot";
      };
    };
  };
in
out