forked from jaffamd/sonostreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure-streambox.sh
355 lines (318 loc) · 10.4 KB
/
configure-streambox.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/sh
# This script configures the SonoConnect Streambox environment on the Raspberry Pi platform
# Usage: sudo /boot/configure-streambox.sh [you will be prompted to enter the client WiFi info (the existing network) and your desired access point info (the network you are creating)]
# Licence: GPLv3
# Author: Elias Jaffa (@jaffa_md)
# Special thanks to: https://albeec13.github.io/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ and Darko Lukic <[email protected]>
MAC_ADDRESS="$(cat /sys/class/net/wlan0/address)"
echo -n "Enter the existing WiFi network name (SSID) you wish to connect to > "
read CLIENT_SSID
echo -n "Enter the password of the existing WiFi network > "
read CLIENT_PASSPHRASE
echo -n "Enter the name you wish to use for the new access point > "
read AP_SSID
echo -n "Enter the password you wish to use for the $AP_SSID access point > "
read AP_PASSPHRASE
echo "Beginning configuration........."
# Populate `/etc/udev/rules.d/70-persistent-net.rules`
sudo bash -c 'cat > /etc/udev/rules.d/70-persistent-net.rules' << EOF
SUBSYSTEM=="ieee80211", ACTION=="add|change", ATTR{macaddress}=="${MAC_ADDRESS}", KERNEL=="phy0", \
RUN+="/sbin/iw phy phy0 interface add ap0 type __ap", \
RUN+="/bin/ip link set ap0 address ${MAC_ADDRESS}"
EOF
# Increase GPU memory for better encoding performance
sudo echo "gpu_mem=512" >> /boot/config.txt
# Install dependencies
sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y install dnsmasq hostapd
# Populate `/etc/dnsmasq.conf`
sudo bash -c 'cat > /etc/dnsmasq.conf' << EOF
interface=lo,ap0
no-dhcp-interface=lo,wlan0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.10.50,192.168.10.150,12h
EOF
# Populate `/etc/hostapd/hostapd.conf`
sudo bash -c 'cat > /etc/hostapd/hostapd.conf' << EOF
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
interface=ap0
driver=nl80211
ssid=${AP_SSID}
hw_mode=g
channel=11
wmm_enabled=0
macaddr_acl=0
auth_algs=1
wpa=2
wpa_passphrase=${AP_PASSPHRASE}
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP
EOF
# Populate `/etc/default/hostapd`
sudo bash -c 'cat > /etc/default/hostapd' << EOF
DAEMON_CONF="/etc/hostapd/hostapd.conf"
EOF
# Populate `/etc/wpa_supplicant/wpa_supplicant.conf`
sudo bash -c 'cat > /etc/wpa_supplicant/wpa_supplicant.conf' << EOF
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="${CLIENT_SSID}"
psk="${CLIENT_PASSPHRASE}"
id_str="AP1"
}
EOF
# Populate `/etc/network/interfaces`
sudo bash -c 'cat > /etc/network/interfaces' << EOF
source-directory /etc/network/interfaces.d
auto lo
auto ap0
auto wlan0
iface lo inet loopback
allow-hotplug ap0
iface ap0 inet static
address 192.168.10.1
netmask 255.255.255.0
hostapd /etc/hostapd/hostapd.conf
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface AP1 inet dhcp
EOF
# Populate `/bin/start_wifi.sh`
sudo bash -c 'cat > /bin/start_wifi.sh' << EOF
#!/bin/bash
echo "Starting wifi......."
sleep 15
sudo ifdown --force wlan0
sudo ifdown --force ap0
sleep 15
sudo ifdown --force wlan0
sudo ifdown --force ap0
sudo ifup ap0
sudo ifup wlan0
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 ! -d 192.168.10.0/24 -j MASQUERADE
sudo systemctl restart dnsmasq
echo "Wifi script finished running"
EOF
sudo chmod +x /bin/start_wifi.sh
# Modify '/etc/rc.local' to run the start_wifi script on reboot
sudo bash -c 'cat > /etc/rc.local' << EOF
#!/bin/sh -e
/bin/start_wifi.sh &
exit 0
EOF
# Add the local domain name to the hosts file
sudo echo "192.168.10.1 streambox.com" >> /etc/hosts
# Give sudo access to www-data for reboot and shutdown functions
sudo usermod -aG sudo www-data
sudo echo "www-data ALL=NOPASSWD: /sbin/reboot, /sbin/shutdown" >> /etc/sudoers
sudo update-rc.d dhcpcd disable
echo "Wifi configuration is finished!"
echo "Installing additional dependencies.........."
sudo apt-get install ffmpeg -y
sudo apt-get install apache2 -y
sudo apt-get install php libapache2-mod-php -y
# Remove index.html and create a new one
sudo rm /var/www/html/index.html
# Download jQuery to allow for "offline" use
cd /var/www/html
wget http://code.jquery.com/jquery-3.3.1.min.js
cd ~/
sudo bash -c 'cat > /var/www/html/index.html' << EOF
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="./jquery-3.3.1.min.js"></script>
<script type="text/javascript">
\$(document).ready(function(){
\$('#startbutton').click(function(){
var a = new XMLHttpRequest();
a.open("GET","startstream.php");
a.onreadystatechange=function(){
if(a.readyState==4){
if(a.status == 200){
}
else alert("HTTP ERROR");
}
}
a.send();
});
\$('#stopbutton').click(function(){
var a = new XMLHttpRequest();
a.open("GET","stopstream.php");
a.onreadystatechange=function(){
if(a.readyState==4){
if(a.status == 200){
}
else alert("HTTP ERROR");
}
}
a.send();
});
\$('#rebootbutton').click(function(){
var a = new XMLHttpRequest();
a.open("GET","reboot.php");
a.onreadystatechange=function(){
if(a.readyState==4){
if(a.status == 200){
}
else alert("HTTP ERROR");
}
}
a.send();
});
\$('#shutdownbutton').click(function(){
var a = new XMLHttpRequest();
a.open("GET","shutdown.php");
a.onreadystatechange=function(){
if(a.readyState==4){
if(a.status == 200){
}
else alert("HTTP ERROR");
}
}
a.send();
});
\$('#newwifibutton').click(function(){
window.location.href = "addwifi.php";
});
});
</script>
<title>POCUS-Pi</title>
<style>
button {
margin: auto;
width: 50%;
max-width: 400px;
font-size: 24px;
text-align: center;
padding: 20px;
margin-top: 40px;
box-shadow: 6px 6px 3px grey;
display: block;
}
</style>
</head>
<body>
<button id="startbutton">Start Livestream</button>
<button id="stopbutton">Stop Livestream</button>
<button id="newwifibutton">Add WiFi Network</button>
<button id="rebootbutton">Reboot System</button>
<button id="shutdownbutton">Shutdown System</button>
</body>
</html>
EOF
# Create PHP script files
sudo bash -c 'cat > /var/www/html/addwifi.php' << EOF
<?php
\$ssid = \$psk = "";
if (\$_SERVER["REQUEST_METHOD"] == "POST") {
if (empty(\$_POST["ssid"])) {
\$nameErr = "Network name is required";
} else {
\$ssid = test_input(\$_POST["ssid"]);
}
if (empty(\$_POST["psk"])) {
\$nameErr = "Password is required";
} else {
\$psk = test_input(\$_POST["psk"]);
}
if ((\$ssid != "") && (\$psk != "")) {
\$file = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "r") or die("Unable to open wpa_supplicant.conf");
\$x=1;
\$lastAP = "";
while (!feof(\$file)) {
\$line = fgets(\$file);
if (strpos(\$line, "id_str") !== false) {
\$lastAP = "AP" . \$x;
\$x++;
}
}
fclose(\$file);
\$file = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "a") or die("Unable to open wpa_supplicant.conf");
\$strtowrite = PHP_EOL . 'network={' . PHP_EOL . ' ssid="' . \$ssid . '"' . PHP_EOL . ' psk="' . \$psk . '"' . PHP_EOL . ' id_str="AP' . \$x . '"' . PHP_EOL . '}';
fwrite(\$file, \$strtowrite) or die("Unable to write file!");
fclose(\$file);
\$file = fopen("/etc/network/interfaces", "a") or die('Unable to open network/interfaces');
\$strtowritenetwork = PHP_EOL . 'iface AP' . \$x . ' inet dhcp';
fwrite(\$file, \$strtowritenetwork) or die("Unable to write to network interfaces file");
fclose(\$file);
header("Location: index.html");
}
}
function test_input(\$data) {
\$data = trim(\$data);
\$data = stripslashes(\$data);
\$data = htmlspecialchars(\$data);
return \$data;
}
?>
<html>
<head>
<meta charset="utf-8">
<title>Add WiFi Network Details</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<form action="<?php echo htmlspecialchars(\$_SERVER["PHP_SELF"]);?>" method="post">
WiFi Network Name: <input type="text" name="ssid"><span class="error">* <?php echo \$nameErr;?></span><br><br>
WiFi Network Password: <input type="text" name="psk"><span class="error">* <?php echo \$nameErr;?></span><br><br>
<input type="submit">
</form>
</body>
</html>
EOF
sudo bash -c 'cat > /var/www/html/startstream.php' << EOF
<?php
exec('./livestream.py');
?>
EOF
sudo bash -c 'cat > /var/www/html/stopstream.php' << EOF
<?php
exec("killall ffmpeg");
?>
EOF
sudo bash -c 'cat > /var/www/html/shutdown.php' << EOF
<?php
exec("sudo shutdown now");
?>
EOF
sudo bash -c 'cat > /var/www/html/reboot.php' << EOF
<?php
exec("sudo reboot now");
?>
EOF
sudo bash -c 'cat > /var/www/html/livestream.py' << EOF
#!/usr/bin/python
import os
# Set input type: 0 for composite, 1 for S-video
os.system('v4l2-ctl -d /dev/video0 -i 0')
os.system('ffmpeg -y -re -f video4linux2 -standard NTSC -i /dev/video0 -c:v h264_omx -an -f flv rtmp://YOUR-IP-HERE/live/livestream')
EOF
# Change file permissions for important files
sudo chown -R pi: /var/www/html
sudo chown www-data: /var/www/html/index.html
sudo chmod 644 /var/www/html/index.html
sudo chown pi: /var/www/html/livestream.py
sudo chmod 755 /var/www/html/livestream.py
sudo chown www-data: /var/www/html/*.php
sudo chmod 644 /var/www/html/*.php
sudo chmod ugo+w /etc/wpa_supplicant/wpa_supplicant.conf
sudo chmod ugo+w /etc/network/interfaces
# Give the webpage access to the video device
sudo usermod -a -G video www-data
# Give the user access to the video device
sudo chmod ugo+rw /dev/video0
echo "System configuration complete. Please reboot your Pi now....."