@@ -0,0 +1,108 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.programs.captive-browser;
7+in
8+{
9+ ###### interface
10+
11+ options = {
12+ programs.captive-browser = {
13+ enable = mkEnableOption "captive browser";
14+
15+ package = mkOption {
16+ type = types.package;
17+ default = pkgs.captive-browser;
18+ };
19+
20+ interface = mkOption {
21+ type = types.str;
22+ description = "your public network interface (wlp3s0, wlan0, eth0, ...)";
23+ };
24+
25+ # the options below are the same as in "captive-browser.toml"
26+ browser = mkOption {
27+ type = types.str;
28+ default = concatStringsSep " " [ ''${pkgs.chromium}/bin/chromium''
29+ ''--user-data-dir=$HOME/.chromium-captive''
30+ ''--proxy-server="socks5://$PROXY"''
31+ ''--host-resolver-rules="MAP * ~NOTFOUND , EXCLUDE localhost"''
32+ ''--no-first-run''
33+ ''--new-window''
34+ ''--incognito''
35+ ''http://cache.nixos.org/''
36+ ];
37+ description = ''
38+ the shell (/bin/sh) command executed once the proxy starts.
39+ When browser exits, the proxy exits. An extra env var PROXY is available.
40+
41+ Here, we use a separate Chrome instance in Incognito mode, so that
42+ it can run (and be waited for) alongside the default one, and that
43+ it maintains no state across runs. To configure this browser open a
44+ normal window in it, settings will be preserved.
45+
46+ @volth: chromium is to open a plain HTTP (not HTTPS nor redirect to HTTPS!) website.
47+ upstream uses http://example.com but I have seen captive portals whose DNS server resolves "example.com" to 127.0.0.1
48+ '';
49+ };
50+
51+ dhcp-dns = mkOption {
52+ type = types.str;
53+ description = ''
54+ the shell (/bin/sh) command executed to obtain the DHCP
55+ DNS server address. The first match of an IPv4 regex is used.
56+ IPv4 only, because let's be real, it's a captive portal.
57+ '';
58+ };
59+
60+ socks5-addr = mkOption {
61+ type = types.str;
62+ default = "localhost:1666";
63+ description = ''the listen address for the SOCKS5 proxy server'';
64+ };
65+ };
66+ };
67+
68+ ###### implementation
69+
70+ config = mkIf cfg.enable {
71+
72+ programs.captive-browser.dhcp-dns = mkOptionDefault (
73+ if config.networking.networkmanager.enable then
74+ "${pkgs.networkmanager}/bin/nmcli dev show ${escapeShellArg cfg.interface} | ${pkgs.gnugrep}/bin/fgrep IP4.DNS"
75+ else if config.networking.dhcpcd.enable then
76+ "${pkgs.dhcpcd}/bin/dhcpcd -U ${escapeShellArg cfg.interface} | ${pkgs.gnugrep}/bin/fgrep domain_name_servers"
77+ else if config.networking.useNetworkd then
78+ "${cfg.package}/bin/systemd-networkd-dns ${escapeShellArg cfg.interface}"
79+ else
80+ "${config.security.wrapperDir}/udhcpc --quit --now -f -i ${escapeShellArg cfg.interface} -O dns --script ${
81+ pkgs.writeScript "udhcp-script" ''
82+ #!/bin/sh
83+ if [ "$1" = bound ]; then
84+ echo "$dns"
85+ fi
86+ ''}"
87+ );
88+
89+ security.wrappers.udhcpc = {
90+ capabilities = "cap_net_raw+p";
91+ source = "${pkgs.busybox}/bin/udhcpc";
92+ };
93+
94+ security.wrappers.captive-browser = {
95+ capabilities = "cap_net_raw+p";
96+ source = pkgs.writeScript "captive-browser" ''
97+ #!${pkgs.bash}/bin/bash
98+ export XDG_CONFIG_HOME=${pkgs.writeTextDir "captive-browser.toml" ''
99+ browser = """${cfg.browser}"""
100+ dhcp-dns = """${cfg.dhcp-dns}"""
101+ socks5-addr = """${cfg.socks5-addr}"""
102+ bind-device = """${cfg.interface}"""
103+ ''}
104+ exec ${cfg.package}/bin/captive-browser
105+ '';
106+ };
107+ };
108+}