nixpkgs/nixos/modules/services/networking/frp.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
3.0 KiB
Nix
Raw Normal View History

2023-09-12 16:13:15 +00:00
{ config, lib, pkgs, ... }:
let
cfg = config.services.frp;
settingsFormat = pkgs.formats.toml { };
configFile = settingsFormat.generate "frp.toml" cfg.settings;
2023-09-12 16:13:15 +00:00
isClient = (cfg.role == "client");
isServer = (cfg.role == "server");
in
{
options = {
services.frp = {
2024-08-28 19:19:08 +00:00
enable = lib.mkEnableOption "frp";
2023-09-12 16:13:15 +00:00
2024-08-28 19:19:08 +00:00
package = lib.mkPackageOption pkgs "frp" { };
2023-09-12 16:13:15 +00:00
2024-08-28 19:19:08 +00:00
role = lib.mkOption {
type = lib.types.enum [ "server" "client" ];
2023-09-12 16:13:15 +00:00
description = ''
The frp consists of `client` and `server`. The server is usually
deployed on the machine with a public IP address, and
the client is usually deployed on the machine
where the Intranet service to be penetrated resides.
'';
};
2024-08-28 19:19:08 +00:00
settings = lib.mkOption {
2023-09-12 16:13:15 +00:00
type = settingsFormat.type;
default = { };
description = ''
Frp configuration, for configuration options
see the example of [client](https://github.com/fatedier/frp/blob/dev/conf/frpc_full_example.toml)
or [server](https://github.com/fatedier/frp/blob/dev/conf/frps_full_example.toml) on github.
2023-09-12 16:13:15 +00:00
'';
example = {
serverAddr = "x.x.x.x";
serverPort = 7000;
};
2023-09-12 16:13:15 +00:00
};
};
};
config =
let
2024-08-28 19:19:08 +00:00
serviceCapability = lib.optionals isServer [ "CAP_NET_BIND_SERVICE" ];
2023-09-12 16:13:15 +00:00
executableFile = if isClient then "frpc" else "frps";
in
2024-08-28 19:19:08 +00:00
lib.mkIf cfg.enable {
2023-09-12 16:13:15 +00:00
systemd.services = {
frp = {
2024-08-28 19:19:08 +00:00
wants = lib.optionals isClient [ "network-online.target" ];
2023-09-12 16:13:15 +00:00
after = if isClient then [ "network-online.target" ] else [ "network.target" ];
wantedBy = [ "multi-user.target" ];
description = "A fast reverse proxy frp ${cfg.role}";
serviceConfig = {
Type = "simple";
Restart = "on-failure";
RestartSec = 15;
ExecStart = "${cfg.package}/bin/${executableFile} --strict_config -c ${configFile}";
2024-08-28 19:19:08 +00:00
StateDirectoryMode = lib.optionalString isServer "0700";
2023-09-12 16:13:15 +00:00
DynamicUser = true;
# Hardening
2024-08-28 19:19:08 +00:00
UMask = lib.optionalString isServer "0007";
2023-09-12 16:13:15 +00:00
CapabilityBoundingSet = serviceCapability;
AmbientCapabilities = serviceCapability;
PrivateDevices = true;
ProtectHostname = true;
ProtectClock = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
2024-08-28 19:19:08 +00:00
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ] ++ lib.optionals isClient [ "AF_UNIX" ];
2023-09-12 16:13:15 +00:00
LockPersonality = true;
MemoryDenyWriteExecute = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
PrivateMounts = true;
SystemCallArchitectures = "native";
SystemCallFilter = [ "@system-service" ];
};
};
};
};
2024-08-28 19:19:08 +00:00
meta.maintainers = with lib.maintainers; [ zaldnoay ];
2023-09-12 16:13:15 +00:00
}