Last active
March 23, 2024 05:09
-
-
Save ImranR98/704716b7bf74851f25152231023117a3 to your computer and use it in GitHub Desktop.
RPi Cam Setup Script: Turn a webcam connected to a Raspberry Pi (or anything else) into a local IP MJPEG camera, protected with a (self-signed) HTTPS and Basic authentication.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ "$1" == 'firstTime' ]; then | |
FIRST_TIME=true | |
fi | |
header() { | |
underlinechar="-" | |
printf "%0.s"$underlinechar"" $(seq 1 "$(tput cols)") | |
echo "$1" | |
printf "%0.s"$underlinechar"" $(seq 1 "$(tput cols)") | |
} | |
header "Install/Update Go2RTC" | |
sudo curl -L "$(curl https://api.github.com/repos/AlexxIT/go2rtc/releases/latest | jq -r '.assets[].browser_download_url' | grep linux_arm64)" -o /usr/local/bin/go2rtc | |
sudo chmod +x /usr/local/bin/go2rtc | |
header "Configure Go2RTC" | |
cat <<EOF | sudo tee /var/go2rtc.yaml | |
streams: | |
video: ffmpeg:device?video=/dev/video0&input_format=mjpeg&video_size=1280x720 | |
api: | |
listen: "localhost:1984" | |
base_path: "" | |
static_dir: "" | |
origin: "" | |
rtsp: | |
listen: "" | |
srtp: | |
listen: "" | |
webrtc: | |
listen: "" | |
EOF | |
header "Enable and start Go2RTC" | |
cat <<EOF | sudo tee /etc/systemd/system/go2rtc.service | |
[Unit] | |
Description=Go2RTC | |
[Service] | |
Type=simple | |
ExecStart=/usr/local/bin/go2rtc --config /var/go2rtc.yaml | |
Restart=on-failure | |
RestartSec=5s | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
sudo systemctl daemon-reload | |
sudo systemctl enable go2rtc | |
sudo systemctl restart go2rtc | |
header "Install/Update NGINX and supporting commands" | |
sudo apt install nginx | |
sudo apt install apache2-utils | |
if [ "$FIRST_TIME" == true ]; then | |
header "Generate a user and HTTPS cert" | |
sudo htpasswd -c /etc/nginx/.htpasswd $USER | |
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/self_signed.key -out /etc/nginx/self_signed.crt | |
fi | |
header "Configure NGINX" | |
cat <<EOF | sudo tee /etc/nginx/conf.d/reverse_proxy.conf | |
server { | |
listen 80; | |
return 301 https://\$host\$request_uri; | |
} | |
server { | |
listen 443 ssl default_server; | |
ssl_certificate /etc/nginx/self_signed.crt; | |
ssl_certificate_key /etc/nginx/self_signed.key; | |
location / { | |
proxy_pass http://localhost:1984; | |
proxy_set_header Host \$host; | |
proxy_set_header X-Real-IP \$remote_addr; | |
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; | |
# Basic Authentication | |
auth_basic "Restricted Content"; | |
auth_basic_user_file /etc/nginx/.htpasswd; | |
# WebSocket support | |
proxy_http_version 1.1; | |
proxy_set_header Upgrade \$http_upgrade; | |
proxy_set_header Connection "upgrade"; | |
} | |
} | |
EOF | |
sudo nginx -t | |
header "Enable and start NGINX" | |
sudo systemctl daemon-reload | |
sudo systemctl enable nginx | |
sudo systemctl restart nginx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment