1
+ #! /usr/bin/env bash
2
+
3
+ WORKDIR=" /opt/valet-linux"
4
+ DNSFILE=" ${WORKDIR} /dns-servers"
5
+ DNSHEAD=" ${WORKDIR} /custom-nameservers"
6
+ LOGFILE=" ${WORKDIR} /watch.log"
7
+ VRESOLV=" ${WORKDIR} /resolv.conf"
8
+ RESOLV=" /etc/resolv.conf"
9
+
10
+ function unique() {
11
+ # Function to remove duplicated lines (even when they are not contiguous)
12
+ # cat -n puts line numbers
13
+ # sort -uk2 sort and remove duplicates (ignoring line numbers)
14
+ # sort -nk1 sort by line number
15
+ # cut -f2- remove line numbers
16
+ cat -n | sort -uk2 | sort -nk1 | cut -f2-
17
+ }
18
+
19
+ function symlinkResolv() {
20
+ if [[ $( readlink -f " $RESOLV " ) != " $VRESOLV " ]]; then
21
+ if [[ ! -f " ${RESOLV} .bak" ]]; then
22
+ mv " $RESOLV " " ${RESOLV} .bak"
23
+ fi
24
+
25
+ ln -sf " $VRESOLV " " ${RESOLV} .tmp"
26
+ mv " ${RESOLV} .tmp" " $RESOLV "
27
+ fi
28
+ }
29
+
30
+ function getDirs() {
31
+ DIRS=()
32
+ local TARRAY=()
33
+
34
+ readarray -t TARRAY <<< " $(find /run -path '/run/user' -prune -o ! -readable -prune -o -name 'resolv.conf' -print)"
35
+
36
+ # Find nameserver files in the /run/NetworkManager folder (as they do not have a standard name)
37
+ if [[ ! -f " /run/NetworkManager/resolv.conf" && -d " /run/NetworkManager" ]]; then
38
+ TARRAY=(${TARRAY[@]} ' /run/NetworkManager/' )
39
+ fi
40
+
41
+ # Find nameserver files in the /run/resolvconf/interface folder (as they do not have a standard name)
42
+ if [[ -d " /run/resolvconf/interface" ]]; then
43
+ TARRAY=(${TARRAY[@]} ' /run/resolvconf/interface/' )
44
+ fi
45
+
46
+ for ENTRY in " ${TARRAY[@]} " ; do
47
+ local TMP=${ENTRY%/* }
48
+ DIRS=(${DIRS[@]} " $TMP " )
49
+ done
50
+ }
51
+
52
+ function updateNameservers() {
53
+ # Read all of the nameserver files at once, filter lines starting with 'nameserver'
54
+ # and exclude the ones containing 127.0.0.1 (localhost)
55
+
56
+ getFiles
57
+
58
+ echo " ${FILES[@]} " | tee " ${WORKDIR} /resolvfiles.log" & > /dev/null
59
+
60
+ cat " $DNSHEAD " | unique | tee " $DNSFILE " & > /dev/null
61
+ cat " ${FILES[@]} " | grep -i ' ^nameserver' | grep -v ' 127.0.0.1' | unique | tee -a " $DNSFILE " & > /dev/null
62
+
63
+ symlinkResolv
64
+
65
+ cat " ${FILES[@]} " | grep -v ' ^nameserver' | grep -v ' ^#' | unique | tee " $VRESOLV " & > /dev/null
66
+ echo ' nameserver 127.0.0.1' >> " $VRESOLV "
67
+
68
+ # Add "search" and "domain" directives to /etc/resolv.conf
69
+ # chattr -i "$RESOLV" && \
70
+ # cat "${FILES[@]}" | grep -v '^nameserver' | grep -v '^#' | unique | tee "$VRESOLV" &>/dev/null && \
71
+ # echo 'nameserver 127.0.0.1' >> "$VRESOLV" && \
72
+ # chattr +i "$RESOLV"
73
+ }
74
+
75
+ function getFiles() {
76
+ FILES=()
77
+ local TARRAY=()
78
+
79
+ for DIR in " ${DIRS[@]} " ; do
80
+ readarray -t TARRAY <<< " $(find ${DIR} -path ! -readable -prune -o -name 'resolv.conf' -print)"
81
+
82
+ # Find nameserver files in the /run/resolvconf/interface folder (as they do not have a standard name)
83
+ if [[ " $DIR " = " /run/resolvconf/interface" ]]; then
84
+ readarray -t TARRAY <<< " $(find ${DIR} ! -readable -prune -o -type f -print)"
85
+ fi
86
+
87
+ FILES=(${FILES[@]} ${TARRAY[@]} )
88
+ done
89
+ }
90
+
91
+ function watchDirs() {
92
+ local WATCHERS=(${DIRS[@]} " $DNSHEAD " )
93
+
94
+ # Log which directories are being watched
95
+ echo " Watching the following directories:" >> " $LOGFILE "
96
+
97
+ for DIR in " ${DIRS[@]} " ; do
98
+ echo " - ${1} " >> " $LOGFILE "
99
+ done
100
+
101
+ # Watch directories for changes in files
102
+ inotifywait -q -m -e modify -e create -e delete --format " %w%f" " ${WATCHERS[@]} " | while read change; do
103
+ updateNameservers
104
+ done &
105
+
106
+ pgrep -f ' inotifywait -q -m -e modify' | tee " ${WORKDIR} /watch.pid" & > /dev/null
107
+ }
108
+
109
+ function main() {
110
+ # Create dns file in case it does not exists
111
+ touch " $DNSHEAD "
112
+ touch " $DNSFILE "
113
+
114
+ # Clear log file
115
+ if [[ -f " $LOGFILE " ]]; then
116
+ rm " $LOGFILE "
117
+ fi
118
+
119
+ touch " $LOGFILE "
120
+
121
+ getDirs
122
+ updateNameservers
123
+ watchDirs
124
+ }
125
+
126
+ # ###############################################################################
127
+
128
+ function start {
129
+ if [[ $( pgrep -f ' inotifywait -q -m -e modify' ) ]]; then
130
+ echo -e " Valet DNS Watcher is already running..."
131
+ else
132
+ echo -e " Starting Valet DNS Watcher..."
133
+ main
134
+ echo -e " Valet DNS Watcher started succesfully."
135
+ fi
136
+ }
137
+
138
+ function stop {
139
+ echo -e " Stopping Valet DNS Watcher...\n"
140
+
141
+ pkill -f " inotifywait -q -m -e modify"
142
+
143
+ rm " $LOGFILE " && touch " $LOGFILE "
144
+
145
+ if [[ ! $( pgrep -f ' inotifywait -q -m -e modify' ) ]]; then
146
+ echo -e " \nValet DNS Watcher stopped succesfully."
147
+ fi
148
+ }
149
+
150
+ function restart {
151
+ echo -e " Stopping Valet DNS Watcher..."
152
+
153
+ if [[ $( pgrep -f ' inotifywait -q -m -e modify' ) ]]; then
154
+ pkill -f " inotifywait -q -m -e modify"
155
+ fi
156
+
157
+ echo -e " Starting Valet DNS Watcher..."
158
+
159
+ main
160
+
161
+ if [[ $( pgrep -f ' inotifywait -q -m -e modify' ) ]]; then
162
+ echo -e " Valet DNS Watcher restarted succesfully."
163
+ fi
164
+ }
165
+
166
+ function status {
167
+ echo -e " Watching for changes in the following directories:\n"
168
+ cat ' /opt/valet-linux/watch.log'
169
+ }
170
+
171
+ case " $1 " in
172
+ start)
173
+ start
174
+ ;;
175
+ stop)
176
+ stop
177
+ ;;
178
+ restart)
179
+ restart
180
+ ;;
181
+ status)
182
+ status
183
+ ;;
184
+ * )
185
+ echo " Usage: $0 {start|stop|restart|status}"
186
+ esac
187
+
188
+ exit 0
0 commit comments