summary refs log tree commit diff
path: root/nixos/modules/services/networking/sslh.nix
blob: c87fe914df8bc1688a251f28e91de88ec62f8469 (plain) (blame)
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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.sslh;
  configFile = pkgs.writeText "sslh.conf" ''
    verbose: ${if cfg.verbose then "true" else "false"};
    foreground: true;
    inetd: false;
    numeric: false;
    transparent: false;
    timeout: "${toString cfg.timeout}";
    user: "nobody";
    pidfile: "${cfg.pidfile}";

    listen:
    (
      { host: "${cfg.host}"; port: "${toString cfg.port}"; }
    );

    ${cfg.appendConfig}
  '';
  defaultAppendConfig = ''
    protocols:
    (
      { name: "ssh"; service: "ssh"; host: "localhost"; port: "22"; probe: "builtin"; },
      { name: "openvpn"; host: "localhost"; port: "1194"; probe: "builtin"; },
      { name: "xmpp"; host: "localhost"; port: "5222"; probe: "builtin"; },
      { name: "http"; host: "localhost"; port: "80"; probe: "builtin"; },
      { name: "ssl"; host: "localhost"; port: "443"; probe: "builtin"; },
      { name: "anyprot"; host: "localhost"; port: "443"; probe: "builtin"; }
    );
  '';
in
{
  options = {
    services.sslh = {
      enable = mkEnableOption "sslh";

      verbose = mkOption {
        type = types.bool;
        default = false;
        description = "Verbose logs.";
      };

      timeout = mkOption {
        type = types.int;
        default = 2;
        description = "Timeout in seconds.";
      };

      pidfile = mkOption {
        type = types.path;
        default = "/run/sslh.pid";
        description = "PID file path for sslh daemon.";
      };

      host = mkOption {
        type = types.str;
        default = config.networking.hostName;
        description = "Listening hostname.";
      };

      port = mkOption {
        type = types.int;
        default = 443;
        description = "Listening port.";
      };

      appendConfig = mkOption {
        type = types.str;
        default = defaultAppendConfig;
        description = "Verbatim configuration file.";
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.sslh = {
      description = "Applicative Protocol Multiplexer (e.g. share SSH and HTTPS on the same port)";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig.ExecStart = "${pkgs.sslh}/bin/sslh -F${configFile}";
      serviceConfig.KillMode = "process";
      serviceConfig.PIDFile = "${cfg.pidfile}";
    };
  };
}