aboutsummaryrefslogtreecommitdiff
path: root/net/nextdns/files/nextdns.init
blob: 789f7d96864f7cb2b02e5aef37f5b4c9d3e63f70 (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
70
71
72
73
74
75
76
#!/bin/sh /etc/rc.common

# shellcheck disable=SC2034 disable=SC2154

USE_PROCD=1

# starts after network starts
START=21
# stops before networking stops
STOP=89

PROG=/usr/sbin/nextdns

add_dnsmasq_opt() {
        mkdir -p /tmp/dnsmasq.d
        echo "$1" >> /tmp/dnsmasq.d/nextdns.conf
}

dnsmasq_reload() {
        # Reload dnsmasq is already running.
        if /etc/init.d/dnsmasq running; then
                /etc/init.d/dnsmasq reload
        fi
}

handle_host_config() {
    host_config_args="$host_config_args -config=$1"
}

start_service() {
        config_load nextdns
        config_get_bool enabled main enabled "1"
        rm -f /tmp/dnsmasq.d/nextdns.conf
        if [ "$enabled" = "1" ]; then
                config_get config main config ""
                config_list_foreach main host_config handle_host_config
                config_get listen main listen "127.0.0.1:5342"
                config_get_bool report_client_info main report_client_info "1"
                config_get_bool hardened_privacy main hardened_privacy "0"
                config_get_bool log_queries main log_queries "0"

                # Add a custom configuration for dnsmasq.
                server=$(echo "$listen" | sed -e 's/:/#/')
                add_dnsmasq_opt "server=$server"
                add_dnsmasq_opt "no-resolv"
                if [ "$report_client_info" = "1" ]; then
                        add_dnsmasq_opt "add-mac"
                        add_dnsmasq_opt "add-subnet=32,128"
                fi

                procd_open_instance
                # shellcheck disable=SC2086
                procd_set_param command "$PROG" run \
                        -listen="$listen" \
                        $host_config_args \
                        -config="$config" \
                        -report-client-info="$report_client_info" \
                        -hardened-privacy="$hardened_privacy" \
                        -log-queries="$log_queries"
                procd_set_param stdout 1
                procd_set_param stderr 1
                procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
                procd_close_instance
        fi

        dnsmasq_reload
}

stop_service() {
        rm -f /tmp/dnsmasq.d/nextdns.conf
        dnsmasq_reload
}

service_triggers() {
        procd_add_reload_trigger "nextdns"
}