summaryrefslogtreecommitdiff
path: root/os/srv/fail2ban.nix
blob: 9b51ceb8506e896171adc8d0c53fd83d57f556f8 (plain)
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
{ config, lib, ... }:

let
  cfg = config.os.srv.fail2ban;
in
{
  options.os.srv.fail2ban = {
    enable = lib.mkEnableOption "the NGINX reverse proxy service";
    nginxJails.enable = lib.mkEnableOption "enables Nginx basic-auth and botsearch jails" // {
      default = true;
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
      {
        assertion = config.networking.firewall.enable || config.networking.nftables.enable;
        message = "Fail2ban requires the NixOS firewall or nftables to be enabled to block IPs.";
      }
      {
        assertion = cfg.nginxJails.enable -> config.os.srv.nginx.enable;
        message = "Fail2ban Nginx jails require your custom Nginx service to be enabled.";
      }
    ];

    services.fail2ban = {
      enable = true;

      bantime = "24h";
      # findtime = "10m";
      maxretry = 5;

      banaction = "nftables-multiport";

      ignoreIP = [ "10.0.0.0/16" ];

      jails = lib.mkMerge [
        {
          sshd = {
            enabled = true;
            settings = {
              maxretry = 3;
            };
          };
        }

        (lib.mkIf cfg.nginxJails.enable {
          nginx-http-auth = {
            enabled = true;
            settings = {
              port = "http,https";
              filter = "nginx-http-auth";
              maxretry = 5;
            };
          };

          nginx-botsearch = {
            enabled = true;
            settings = {
              port = "http,https";
              filter = "nginx-botsearch";
              maxretry = 3;
            };
          };
        })
      ];
    };
  };
}