summary refs log tree commit diff
path: root/nixos/modules/services/networking/dae.nix
blob: 3c7f386d2d482673af1f6096388701e0204fe6bc (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{ config, lib, pkgs, ... }:

let
  cfg = config.services.dae;
  assets = cfg.assets;
  genAssetsDrv = paths: pkgs.symlinkJoin {
    name = "dae-assets";
    inherit paths;
  };
in
{
  meta.maintainers = with lib.maintainers; [ pokon548 oluceps ];

  options = {
    services.dae = with lib;{
      enable = mkEnableOption
        (mdDoc "A Linux high-performance transparent proxy solution based on eBPF");

      package = mkPackageOptionMD pkgs "dae" { };


      assets = mkOption {
        type = with types;(listOf path);
        default = with pkgs; [ v2ray-geoip v2ray-domain-list-community ];
        defaultText = literalExpression "with pkgs; [ v2ray-geoip v2ray-domain-list-community ]";
        description = mdDoc ''
          Assets required to run dae.
        '';
      };

      assetsPath = mkOption {
        type = types.str;
        default = "${genAssetsDrv assets}/share/v2ray";
        defaultText = literalExpression ''
          (symlinkJoin {
              name = "dae-assets";
              paths = assets;
          })/share/v2ray
        '';
        description = mdDoc ''
          The path which contains geolocation database.
          This option will override `assets`.
        '';
      };

      openFirewall = mkOption {
        type = with types; submodule {
          options = {
            enable = mkEnableOption "enable";
            port = mkOption {
              type = types.port;
              description = ''
                Port to be opened. Consist with field `tproxy_port` in config file.
              '';
            };
          };
        };
        default = {
          enable = true;
          port = 12345;
        };
        defaultText = literalExpression ''
          {
            enable = true;
            port = 12345;
          }
        '';
        description = mdDoc ''
          Open the firewall port.
        '';
      };

      configFile = mkOption {
        type = with types; (nullOr path);
        default = null;
        example = "/path/to/your/config.dae";
        description = mdDoc ''
          The path of dae config file, end with `.dae`.
        '';
      };

      config = mkOption {
        type = with types; (nullOr str);
        default = null;
        description = mdDoc ''
          WARNING: This option will expose store your config unencrypted world-readable in the nix store.
          Config text for dae.

          See <https://github.com/daeuniverse/dae/blob/main/example.dae>.
        '';
      };

      disableTxChecksumIpGeneric =
        mkEnableOption (mdDoc "See <https://github.com/daeuniverse/dae/issues/43>");

    };
  };

  config = lib.mkIf cfg.enable

    {
      environment.systemPackages = [ cfg.package ];
      systemd.packages = [ cfg.package ];

      networking = lib.mkIf cfg.openFirewall.enable {
        firewall =
          let portToOpen = cfg.openFirewall.port;
          in
          {
            allowedTCPPorts = [ portToOpen ];
            allowedUDPPorts = [ portToOpen ];
          };
      };

      systemd.services.dae =
        let
          daeBin = lib.getExe cfg.package;

          configPath =
            if cfg.configFile != null
            then cfg.configFile else pkgs.writeText "config.dae" cfg.config;

          TxChecksumIpGenericWorkaround = with lib;
            (getExe pkgs.writeShellApplication {
              name = "disable-tx-checksum-ip-generic";
              text = with pkgs; ''
                iface=$(${iproute2}/bin/ip route | ${lib.getExe gawk} '/default/ {print $5}')
                ${lib.getExe ethtool} -K "$iface" tx-checksum-ip-generic off
              '';
            });
        in
        {
          wantedBy = [ "multi-user.target" ];
          serviceConfig = {
            LoadCredential = [ "config.dae:${configPath}" ];
            ExecStartPre = [ "" "${daeBin} validate -c \${CREDENTIALS_DIRECTORY}/config.dae" ]
              ++ (with lib; optional cfg.disableTxChecksumIpGeneric TxChecksumIpGenericWorkaround);
            ExecStart = [ "" "${daeBin} run --disable-timestamp -c \${CREDENTIALS_DIRECTORY}/config.dae" ];
            Environment = "DAE_LOCATION_ASSET=${cfg.assetsPath}";
          };
        };

      assertions = [
        {
          assertion = lib.pathExists (toString (genAssetsDrv cfg.assets) + "/share/v2ray");
          message = ''
            Packages in `assets` has no preset paths included.
            Please set `assetsPath` instead.
          '';
        }

        {
          assertion = !((config.services.dae.config != null)
            && (config.services.dae.configFile != null));
          message = ''
            Option `config` and `configFile` could not be set
            at the same time.
          '';
        }

        {
          assertion = !((config.services.dae.config == null)
            && (config.services.dae.configFile == null));
          message = ''
            Either `config` or `configFile` should be set.
          '';
        }
      ];
    };
}