vuls: init at 0.27.0
[NixPkgs.git] / nixos / modules / services / networking / frp.nix
blob56af543b845badc92afec29fd06760985f875edf
1 { config, lib, pkgs, ... }:
2 let
3   cfg = config.services.frp;
4   settingsFormat = pkgs.formats.toml { };
5   configFile = settingsFormat.generate "frp.toml" cfg.settings;
6   isClient = (cfg.role == "client");
7   isServer = (cfg.role == "server");
8 in
10   options = {
11     services.frp = {
12       enable = lib.mkEnableOption "frp";
14       package = lib.mkPackageOption pkgs "frp" { };
16       role = lib.mkOption {
17         type = lib.types.enum [ "server" "client" ];
18         description = ''
19           The frp consists of `client` and `server`. The server is usually
20           deployed on the machine with a public IP address, and
21           the client is usually deployed on the machine
22           where the Intranet service to be penetrated resides.
23         '';
24       };
26       settings = lib.mkOption {
27         type = settingsFormat.type;
28         default = { };
29         description = ''
30           Frp configuration, for configuration options
31           see the example of [client](https://github.com/fatedier/frp/blob/dev/conf/frpc_full_example.toml)
32           or [server](https://github.com/fatedier/frp/blob/dev/conf/frps_full_example.toml) on github.
33         '';
34         example = {
35             serverAddr = "x.x.x.x";
36             serverPort = 7000;
37           };
38       };
39     };
40   };
42   config =
43     let
44       serviceCapability = lib.optionals isServer [ "CAP_NET_BIND_SERVICE" ];
45       executableFile = if isClient then "frpc" else "frps";
46     in
47     lib.mkIf cfg.enable {
48       systemd.services = {
49         frp = {
50           wants = lib.optionals isClient [ "network-online.target" ];
51           after = if isClient then [ "network-online.target" ] else [ "network.target" ];
52           wantedBy = [ "multi-user.target" ];
53           description = "A fast reverse proxy frp ${cfg.role}";
54           serviceConfig = {
55             Type = "simple";
56             Restart = "on-failure";
57             RestartSec = 15;
58             ExecStart = "${cfg.package}/bin/${executableFile} --strict_config -c ${configFile}";
59             StateDirectoryMode = lib.optionalString isServer "0700";
60             DynamicUser = true;
61             # Hardening
62             UMask = lib.optionalString isServer "0007";
63             CapabilityBoundingSet = serviceCapability;
64             AmbientCapabilities = serviceCapability;
65             PrivateDevices = true;
66             ProtectHostname = true;
67             ProtectClock = true;
68             ProtectKernelTunables = true;
69             ProtectKernelModules = true;
70             ProtectKernelLogs = true;
71             ProtectControlGroups = true;
72             RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ] ++ lib.optionals isClient [ "AF_UNIX" ];
73             LockPersonality = true;
74             MemoryDenyWriteExecute = true;
75             RestrictRealtime = true;
76             RestrictSUIDSGID = true;
77             PrivateMounts = true;
78             SystemCallArchitectures = "native";
79             SystemCallFilter = [ "@system-service" ];
80           };
81         };
82       };
83     };
85   meta.maintainers = with lib.maintainers; [ zaldnoay ];