-
Notifications
You must be signed in to change notification settings - Fork 1
/
boring.sh
executable file
·282 lines (242 loc) · 6.64 KB
/
boring.sh
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/bash -ex
if [ -f /boot/boring.env ]; then
. /boot/boring.env
fi
LOCAL_GATEWAY=$(/sbin/ip route | /bin/awk '/default/ { print $3 }'| /bin/head -1)
RESET=false
if [ -f /boot/1boring.env ]; then
# if files differ, run a reset
set +e
/usr/bin/diff /boot/boring.env /boot/1boring.env
if [[ "$?" != "0" ]]; then
/usr/bin/diff /boot/boring.env /boot/1boring.env |grep "SETUP_KEY"
if [[ "$?" == "0" ]]; then
# setup key was changed, hard reset
systemctl stop netbird
rm -rf /etc/netbird/config.json ||true
rm -rf /etc/sysconfig/netbird ||true
cp /boot/1boring.env /boot/2boring.env
cat <<EOZ > /etc/hosts
127.0.0.1 localhost ${BORING_NAME}
EOZ
hostnamectl set-hostname $BORING_NAME
RESET=true
echo reseting
else
# setup key was unchanged, soft reset
# todo, soft reset stuff goes here (SSID, name, network)
RESET=false
echo soft reset
fi
fi
set -e
else
# cause it's firstboot
RESET=true
FIRSTBOOT=true
cat <<EOZ > /etc/hosts
127.0.0.1 localhost ${BORING_NAME}
EOZ
hostnamectl set-hostname $BORING_NAME
echo firstbooting
fi
set +e
HOSTAPD_RESET=false
/usr/bin/diff /boot/boring.env /boot/1boring.env |grep "WIFI_PREFERENCE"
if [[ "$?" == "0" ]]; then
HOSTAPD_RESET=true
fi
/usr/bin/diff /boot/boring.env /boot/1boring.env |grep "SSID"
if [[ "$?" == "0" ]]; then
HOSTAPD_RESET=true
fi
/usr/bin/diff /boot/boring.env /boot/1boring.env |grep "COUNTRY_CODE"
if [[ "$?" == "0" ]]; then
HOSTAPD_RESET=true
fi
/usr/bin/diff /boot/boring.env /boot/1boring.env |grep "WPA_PASSPHRASE"
if [[ "$?" == "0" ]]; then
HOSTAPD_RESET=true
fi
/usr/bin/diff /boot/boring.env /boot/1boring.env |grep "CHANNEL"
if [[ "$?" == "0" ]]; then
HOSTAPD_RESET=true
fi
set -e
# update-sidecar-connect-pi
# update journald-remote
# update telegraf.conf
if [[ "$UPDATE" == "true" ]]; then
apt-get install -y systemd-journal-remote ||true
cp /boringup/systemd-journal-gatewayd.service /lib/systemd/system/systemd-journal-gatewayd.service ||true
systemctl enable systemd-journal-gatewayd ||true
systemctl start systemd-journal-gatewayd ||true
mkdir -p /usr/local/boring ||true
cp /boringup/ip-is-in /usr/local/bin/ip-is-in
systemctl stop dnsmasq ||true
#systemctl stop dhcpcd ||true
USE_THIS_IP=$(/usr/local/bin/ip-is-in)
read A B C D <<<"${USE_THIS_IP//./ }"
cat <<EOKI > /etc/dhcpcd.conf
interface wlan0
static ip_address=${USE_THIS_IP}/24
nohook wpa_supplicant
EOKI
systemctl stop networking ||true
# dnsmasq host patch
cat <<EOZ > /etc/dnsmasq.conf
interface=wlan0
dhcp-range=${A}.${B}.${C}.2,${A}.${B}.${C}.100,255.255.255.0,24h
domain=network
address=/boring.network/${USE_THIS_IP}
addn-hosts=/etc/dnsmasq.hosts
no-resolv
server=1.1.1.1
server=1.0.0.1
server=8.8.4.4
server=8.8.8.8
EOZ
cat <<EOY > /etc/dnsmasq.hosts
${USE_THIS_IP} unconfigured.insecure.boring.surf.
44.195.86.2 boring.surf.
EOY
systemctl start networking ||true
#systemctl start dhcpcd ||true
systemctl start dnsmasq ||true
# cp telegraf.conf
cp /boringup/telegraf.conf /etc/telegraf/telegraf.conf
cd /usr/local/boring
rm -rf connect-pi.tgz ||true
wget https://s3.us-east-2.amazonaws.com/boringfiles.dank.earth/connect-pi.tgz
tar -xzvf connect-pi.tgz
cd connect-pi
if [[ "$UPDATE_UI" == "true" ]]; then
npm install -y
npm run build
#service file
cp connect-pi.service /lib/systemd/system/connect-pi.service
systemctl daemon-reload
systemctl restart connect-pi
# install nginx configure SSL for default insecure site ops
export DEBIAN_FRONTEND=true
apt install -y nginx
cp connect-pi.nginx.conf /etc/nginx/sites-enabled/default
systemctl enable nginx
fi
mkdir -p /usr/local/boring/certs
cp fullchain.pem /usr/local/boring/certs/fullchain.pem
cp privkey.pem /usr/local/boring/certs/privkey.pem
systemctl restart nginx
fi
if [[ "$HOSTAPD_RESET" == "true" || "$FIRSTBOOT" == "true" ]]; then
# wifi preference has changed, run hostapd config
if [[ "$WIFI_PREFERENCE" == "2.4Ghz" ]]; then
HW_MODE=g
cat <<EOI > /etc/hostapd/hostapd.conf
country_code=${COUNTRY_CODE}
interface=wlan0
ssid=${SSID}
hw_mode=${HW_MODE}
channel=${CHANNEL}
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=${WPA_PASSPHRASE}
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOI
systemctl restart hostapd
fi
if [[ "$WIFI_PREFERENCE" == "5Ghz" ]]; then
HW_MODE=a
CHANNEL=33
cat <<EOJ > /etc/hostapd/hostapd.conf
country_code=${COUNTRY_CODE}
interface=wlan0
ssid=${SSID}
hw_mode=${HW_MODE}
channel=${CHANNEL}
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=${WPA_PASSPHRASE}
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ieee80211d=1
ieee80211n=1
ieee80211ac=1
wmm_enabled=1
EOJ
systemctl restart hostapd
fi
fi
cp /boot/boring.env /boot/1boring.env
if [[ "$KIND" == "consumer" ]]; then
echo "setting up consumer.."
if [[ "$RESET" == "true" ]]; then
systemctl stop netbird ||true
mkdir -p /etc/sysconfig ||true
cat <<EOF > /etc/sysconfig/netbird
PROVIDER_PUBKEY=${PROVIDER_PUBKEY}
EOF
systemctl start netbird
sleep 2
netbird up --setup-key $SETUP_KEY --management-url https://boring.dank.earth:33073
sleep 5
fi
systemctl start netbird ||true
sleep 20
sysctl net.ipv4.ip_forward=1
ip route del default
ip route add default dev wt0
for i in ${PUBLIC_PEER_IP_LIST//,/ }
do
ip route add $i/32 via $LOCAL_GATEWAY dev eth0
done
iptables -t nat -A POSTROUTING -o wt0 -j MASQUERADE
# masquerade also for any direct traffic to falcon
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sleep 1
elif [[ "$KIND" == "provider" ]]; then
echo "setting up provider.."
if [[ "$RESET" == "true" ]]; then
systemctl start netbird
sleep 2
netbird up --setup-key $SETUP_KEY --management-url https://boring.dank.earth:33073
sleep 5
mypubkey=`/usr/bin/wg show all dump |/usr/bin/head -n1 |/usr/bin/cut -f3`
systemctl stop netbird
mkdir -p /etc/sysconfig ||true
cat <<EOD > /etc/sysconfig/netbird
PROVIDER_PUBKEY=${mypubkey}
EOD
fi
systemctl start netbird || true
sleep 20
sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wt0 -o eth0 -j ACCEPT
else
echo "error: invalid \$KIND specified: $KIND"
exit 1
fi
# telegraf
# do telegraf setup
# gather our pubkey
mypubkey=`/usr/bin/wg show all dump |/usr/bin/head -n1 |/usr/bin/cut -f3`
# telegraf needs perms
setcap CAP_NET_ADMIN+epi /usr/bin/telegraf
systemctl stop telegraf ||true
# setup telegraf id
cat <<EOT > /etc/default/telegraf
INFLUX_TOKEN=QqNqJPMtU3vQk5s-NOOtLU9kQbXZ106181ux7AR6wGOnA7pPVIWtWhvLXT3ai06L_FMcUj2fM1bfsHG_fUFIpw==
BORING_ID=${BORING_ID}
BORING_NAME=${BORING_NAME}
MYPUBKEY=${mypubkey}
EOT
systemctl start telegraf ||true