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

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

157 lines
4.3 KiB
Nix
Raw Normal View History

2015-08-19 07:37:20 +00:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.softether;
package = cfg.package.override { inherit (cfg) dataDir; };
2018-02-22 15:37:59 +00:00
2015-08-19 07:37:20 +00:00
in
{
###### interface
options = {
services.softether = {
enable = mkEnableOption "SoftEther VPN services";
package = mkPackageOption pkgs "softether" { };
2017-09-25 19:03:16 +00:00
2015-08-19 07:37:20 +00:00
vpnserver.enable = mkEnableOption "SoftEther VPN Server";
vpnbridge.enable = mkEnableOption "SoftEther VPN Bridge";
vpnclient = {
enable = mkEnableOption "SoftEther VPN Client";
up = mkOption {
type = types.lines;
default = "";
description = ''
Shell commands executed when the Virtual Network Adapter(s) is/are starting.
'';
};
down = mkOption {
type = types.lines;
default = "";
description = ''
Shell commands executed when the Virtual Network Adapter(s) is/are shutting down.
'';
};
};
dataDir = mkOption {
type = types.path;
2018-02-22 15:37:59 +00:00
default = "/var/lib/softether";
2015-08-19 07:37:20 +00:00
description = ''
Data directory for SoftEther VPN.
'';
};
};
};
###### implementation
config = mkIf cfg.enable (
mkMerge [{
2018-02-22 15:37:59 +00:00
environment.systemPackages = [ package ];
2019-08-13 21:52:01 +00:00
systemd.services.softether-init = {
description = "SoftEther VPN services initial task";
2017-09-25 19:03:16 +00:00
wantedBy = [ "network.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = false;
};
script = ''
2015-08-19 07:37:20 +00:00
for d in vpnserver vpnbridge vpnclient vpncmd; do
if ! test -e ${cfg.dataDir}/$d; then
${pkgs.coreutils}/bin/mkdir -m0700 -p ${cfg.dataDir}/$d
2018-02-22 15:37:59 +00:00
install -m0600 ${package}${cfg.dataDir}/$d/hamcore.se2 ${cfg.dataDir}/$d/hamcore.se2
2015-08-19 07:37:20 +00:00
fi
done
rm -rf ${cfg.dataDir}/vpncmd/vpncmd
2018-02-22 15:37:59 +00:00
ln -s ${package}${cfg.dataDir}/vpncmd/vpncmd ${cfg.dataDir}/vpncmd/vpncmd
2015-08-19 07:37:20 +00:00
'';
};
}
(mkIf cfg.vpnserver.enable {
2015-08-19 07:37:20 +00:00
systemd.services.vpnserver = {
description = "SoftEther VPN Server";
2017-09-25 19:03:16 +00:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 07:37:20 +00:00
serviceConfig = {
Type = "forking";
2018-02-22 15:37:59 +00:00
ExecStart = "${package}/bin/vpnserver start";
ExecStop = "${package}/bin/vpnserver stop";
2015-08-19 07:37:20 +00:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnserver/vpnserver
2018-02-22 15:37:59 +00:00
ln -s ${package}${cfg.dataDir}/vpnserver/vpnserver ${cfg.dataDir}/vpnserver/vpnserver
2015-08-19 07:37:20 +00:00
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnserver/vpnserver
'';
};
})
(mkIf cfg.vpnbridge.enable {
2015-08-19 07:37:20 +00:00
systemd.services.vpnbridge = {
description = "SoftEther VPN Bridge";
2017-09-25 19:03:16 +00:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 07:37:20 +00:00
serviceConfig = {
Type = "forking";
2018-02-22 15:37:59 +00:00
ExecStart = "${package}/bin/vpnbridge start";
ExecStop = "${package}/bin/vpnbridge stop";
2015-08-19 07:37:20 +00:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnbridge/vpnbridge
2018-02-22 15:37:59 +00:00
ln -s ${package}${cfg.dataDir}/vpnbridge/vpnbridge ${cfg.dataDir}/vpnbridge/vpnbridge
2015-08-19 07:37:20 +00:00
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnbridge/vpnbridge
'';
};
})
(mkIf cfg.vpnclient.enable {
2015-08-19 07:37:20 +00:00
systemd.services.vpnclient = {
description = "SoftEther VPN Client";
2017-09-25 19:03:16 +00:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 07:37:20 +00:00
serviceConfig = {
Type = "forking";
2018-02-22 15:37:59 +00:00
ExecStart = "${package}/bin/vpnclient start";
ExecStop = "${package}/bin/vpnclient stop";
2015-08-19 07:37:20 +00:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnclient/vpnclient
2018-02-22 15:37:59 +00:00
ln -s ${package}${cfg.dataDir}/vpnclient/vpnclient ${cfg.dataDir}/vpnclient/vpnclient
2015-08-19 07:37:20 +00:00
'';
postStart = ''
sleep 1
${cfg.vpnclient.up}
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnclient/vpnclient
sleep 1
${cfg.vpnclient.down}
'';
};
boot.kernelModules = [ "tun" ];
})
]);
}