summaryrefslogtreecommitdiffstats
path: root/makefu/2configs/dcpp/hub.nix
blob: f0aac3f323215c4ad53b2133c521803bf711af57 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{ config, lib, pkgs, ... }:

# search also generates ddclient entries for all other logs

with import <stockholm/lib>;
let
  ddclientUser = "ddclient";
  sec = toString <secrets>;
  nsupdate = import "${sec}/nsupdate-hub.nix";
  stateDir = "/var/spool/ddclient";
  cfg = "${stateDir}/cfg";
  ext-if = config.makefu.server.primary-itf;
  ddclientPIDFile = "${stateDir}/ddclient.pid";

  # TODO: correct cert generation requires a `real` internet ip address

  gen-cfg = dict: ''
    ssl=yes
    cache=${stateDir}/ddclient.cache
    pid=${ddclientPIDFile}
    ${concatStringsSep "\n" (mapAttrsToList (user: pass: ''

      protocol=dyndns2
      use=web, web=http://ipv4.nsupdate.info/myip
      ssl=yes
      server=ipv4.nsupdate.info
      login=${user}
      password='${pass}'
      ${user}

    '') dict)}
  '';
  uhubDir = "/var/lib/uhub";

in {
  users.users."${ddclientUser}" = {
    uid = genid "ddclient";
    description = "ddclient daemon user";
    home = stateDir;
    isSystemUser = true;
    createHome = true;
    group = ddclientUser;
  };
  users.groups.${ddclientUser} = {};

  systemd.services = {
    ddclient-nsupdate-uhub = {
      wantedBy = [ "multi-user.target" ];
      after = [ "ip-up.target" ];
      serviceConfig = {
        Type = "forking";
        User = ddclientUser;
        PIDFile = ddclientPIDFile;
        ExecStartPre = pkgs.writeDash "init-nsupdate" ''
          cp -vf ${pkgs.writeText "ddclient-config" (gen-cfg nsupdate)} ${cfg}
          chmod 700 ${cfg}
        '';
        ExecStart = "${pkgs.ddclient}/bin/ddclient -verbose -daemon 1 -noquiet -file ${cfg}";
      };
    };
  };

  networking.firewall.extraCommands = ''
    iptables -A PREROUTING -t nat -i ${ext-if} -p tcp --dport 411 -j REDIRECT --to-port 1511
  '';
  systemd.services.uhub-home.serviceConfig = {
    PrivateTmp = true;
    DynamicUser = lib.mkForce false;
    User = "uhub";
    WorkingDirectory = uhubDir;
    PermissionsStartOnly = true;
    ExecStartPre = pkgs.writeDash "uhub-pre" ''
      cp -f ${toString <secrets/wildcard.krebsco.de.crt>} ${uhubDir}/uhub.crt
      cp -f ${toString <secrets/wildcard.krebsco.de.key>} ${uhubDir}/uhub.key
      if test -d ${uhubDir};then
        echo "Directory ${uhubDir} already exists, skipping db init"
      else
        echo "Copying sql user db"
        cp ${toString <secrets/uhub.sql>} ${uhubDir}/uhub.sql
      fi
      chown -R uhub ${uhubDir}
    '';

  };
  users.users.uhub = {
    home = uhubDir;
    createHome = true;
    isSystemUser = true;
    group = "uhub";
  };
  users.groups.uhub = {};

  services.uhub.home = {
    enable = true;
    enableTLS = true;
    settings = {
      server_port = 1511;
      server_bind_addr = "any";
      hub_name = "krebshub";
      tls_certificate = "${uhubDir}/uhub.crt";
      tls_private_key = "${uhubDir}/uhub.key";
      registered_users_only = true;
    };
    plugins = [
      {
        plugin = "${pkgs.uhub}/plugins/mod_auth_sqlite.so";
        settings.file = "${uhubDir}/uhub.sql";
      }
      {
        plugin = "${pkgs.uhub}/plugins/mod_welcome.so";
        settings.motd = toString (pkgs.writeText "motd" "shareit");
        settings.rules = toString (pkgs.writeText "rules" "1. Don't be an asshole");
      }
      {
        plugin = "${pkgs.uhub}/plugins/mod_chat_history.so";
        settings = {};
      }
    ];
  };
  networking.firewall.allowedTCPPorts = [ 411 1511 ];
}