summary refs log tree commit diff
path: root/nixos/modules/programs/captive-browser.nix
blob: 007b0369ec10e38c3a6b22f74e31d8c12f5016c0 (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
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.programs.captive-browser;
in
{
  ###### interface

  options = {
    programs.captive-browser = {
      enable = mkEnableOption "captive browser";

      package = mkOption {
        type = types.package;
        default = pkgs.captive-browser;
        defaultText = "pkgs.captive-browser";
        description = "Which package to use for captive-browser";
      };

      interface = mkOption {
        type = types.str;
        description = "your public network interface (wlp3s0, wlan0, eth0, ...)";
      };

      # the options below are the same as in "captive-browser.toml"
      browser = mkOption {
        type = types.str;
        default = concatStringsSep " " [
          ''env XDG_CONFIG_HOME="$PREV_CONFIG_HOME"''
          ''${pkgs.chromium}/bin/chromium''
          ''--user-data-dir=''${XDG_DATA_HOME:-$HOME/.local/share}/chromium-captive''
          ''--proxy-server="socks5://$PROXY"''
          ''--host-resolver-rules="MAP * ~NOTFOUND , EXCLUDE localhost"''
          ''--no-first-run''
          ''--new-window''
          ''--incognito''
          ''-no-default-browser-check''
          ''http://cache.nixos.org/''
        ];
        description = ''
          The shell (/bin/sh) command executed once the proxy starts.
          When browser exits, the proxy exits. An extra env var PROXY is available.

          Here, we use a separate Chrome instance in Incognito mode, so that
          it can run (and be waited for) alongside the default one, and that
          it maintains no state across runs. To configure this browser open a
          normal window in it, settings will be preserved.

          @volth: chromium is to open a plain HTTP (not HTTPS nor redirect to HTTPS!) website.
                  upstream uses http://example.com but I have seen captive portals whose DNS server resolves "example.com" to 127.0.0.1
        '';
      };

      dhcp-dns = mkOption {
        type = types.str;
        description = ''
          The shell (/bin/sh) command executed to obtain the DHCP
          DNS server address. The first match of an IPv4 regex is used.
          IPv4 only, because let's be real, it's a captive portal.
        '';
      };

      socks5-addr = mkOption {
        type = types.str;
        default = "localhost:1666";
        description = "the listen address for the SOCKS5 proxy server";
      };

      bindInterface = mkOption {
        default = true;
        type = types.bool;
        description = ''
          Binds <package>captive-browser</package> to the network interface declared in
          <literal>cfg.interface</literal>. This can be used to avoid collisions
          with private subnets.
        '';
      };
    };
  };

  ###### implementation

  config = mkIf cfg.enable {

    programs.captive-browser.dhcp-dns =
      let
        iface = prefix:
          optionalString cfg.bindInterface (concatStringsSep " " (map escapeShellArg [ prefix cfg.interface ]));
      in
      mkOptionDefault (
        if config.networking.networkmanager.enable then
          "${pkgs.networkmanager}/bin/nmcli dev show ${iface ""} | ${pkgs.gnugrep}/bin/fgrep IP4.DNS"
        else if config.networking.dhcpcd.enable then
          "${pkgs.dhcpcd}/bin/dhcpcd ${iface "-U"} | ${pkgs.gnugrep}/bin/fgrep domain_name_servers"
        else if config.networking.useNetworkd then
          "${cfg.package}/bin/systemd-networkd-dns ${iface ""}"
        else
          "${config.security.wrapperDir}/udhcpc --quit --now -f ${iface "-i"} -O dns --script ${
          pkgs.writeShellScript "udhcp-script" ''
            if [ "$1" = bound ]; then
              echo "$dns"
            fi
          ''}"
      );

    security.wrappers.udhcpc = {
      capabilities = "cap_net_raw+p";
      source = "${pkgs.busybox}/bin/udhcpc";
    };

    security.wrappers.captive-browser = {
      capabilities = "cap_net_raw+p";
      source = pkgs.writeShellScript "captive-browser" ''
        export PREV_CONFIG_HOME="$XDG_CONFIG_HOME"
        export XDG_CONFIG_HOME=${pkgs.writeTextDir "captive-browser.toml" ''
                                  browser = """${cfg.browser}"""
                                  dhcp-dns = """${cfg.dhcp-dns}"""
                                  socks5-addr = """${cfg.socks5-addr}"""
                                  ${optionalString cfg.bindInterface ''
                                    bind-device = """${cfg.interface}"""
                                  ''}
                                ''}
        exec ${cfg.package}/bin/captive-browser
      '';
    };
  };
}