summary refs log tree commit diff
path: root/nixos/modules/services/networking/chisel-server.nix
blob: 134c71430cd0776079b9d788bcc8765b43dd2ec7 (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
90
91
92
93
94
95
96
97
98
99
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.chisel-server;

in {
  options = {
    services.chisel-server = {
      enable = mkEnableOption (mdDoc "Chisel Tunnel Server");
      host = mkOption {
        description = mdDoc "Address to listen on, falls back to 0.0.0.0";
        type = with types; nullOr str;
        default = null;
        example = "[::1]";
      };
      port = mkOption {
        description = mdDoc "Port to listen on, falls back to 8080";
        type = with types; nullOr port;
        default = null;
      };
      authfile = mkOption {
        description = mdDoc "Path to auth.json file";
        type = with types; nullOr path;
        default = null;
      };
      keepalive  = mkOption {
        description = mdDoc "Keepalive interval, falls back to 25s";
        type = with types; nullOr str;
        default = null;
        example = "5s";
      };
      backend = mkOption {
        description = mdDoc "HTTP server to proxy normal requests to";
        type = with types; nullOr str;
        default = null;
        example = "http://127.0.0.1:8888";
      };
      socks5 = mkOption {
        description = mdDoc "Allow clients access to internal SOCKS5 proxy";
        type = types.bool;
        default = false;
      };
      reverse = mkOption {
        description = mdDoc "Allow clients reverse port forwarding";
        type = types.bool;
        default = false;
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.chisel-server = {
      description = "Chisel Tunnel Server";
      wantedBy = [ "network-online.target" ];

      serviceConfig = {
        ExecStart = "${pkgs.chisel}/bin/chisel server " + concatStringsSep " " (
          optional (cfg.host != null) "--host ${cfg.host}"
          ++ optional (cfg.port != null) "--port ${builtins.toString cfg.port}"
          ++ optional (cfg.authfile != null) "--authfile ${cfg.authfile}"
          ++ optional (cfg.keepalive != null) "--keepalive ${cfg.keepalive}"
          ++ optional (cfg.backend != null) "--backend ${cfg.backend}"
          ++ optional cfg.socks5 "--socks5"
          ++ optional cfg.reverse "--reverse"
        );

        # Security Hardening
        # Refer to systemd.exec(5) for option descriptions.
        CapabilityBoundingSet = "";

        # implies RemoveIPC=, PrivateTmp=, NoNewPrivileges=, RestrictSUIDSGID=,
        # ProtectSystem=strict, ProtectHome=read-only
        DynamicUser = true;
        LockPersonality = true;
        PrivateDevices = true;
        PrivateUsers = true;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectProc = "invisible";
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = "~@clock @cpu-emulation @debug @mount @obsolete @reboot @swap @privileged @resources";
        UMask = "0077";
      };
    };
  };

  meta.maintainers = with maintainers; [ clerie ];
}