Skip to content

Instantly share code, notes, and snippets.

@snowfag
Created July 24, 2020 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowfag/ef1bfd7ef53eccad00cdadd028e77524 to your computer and use it in GitHub Desktop.
Save snowfag/ef1bfd7ef53eccad00cdadd028e77524 to your computer and use it in GitHub Desktop.
tee /usr/local/bin/cloudflare-update >/dev/null << EOF
#!/usr/bin/env bash
curl -s "https://www.cloudflare.com/ips-v{4,6}" | tee >(awk '{print "set_real_ip_from " \$0 ";"} END {print "real_ip_header CF-Connecting-IP;\nproxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\nproxy_set_header X-Real-IP \$http_cf_connecting_ip;"}' >/etc/nginx/snippets/cf-realip.conf) >(awk '{print "allow " \$0 ";"} END {print "deny all;"}' >/etc/nginx/snippets/cf-restrict.conf) >/dev/null 2>&1
EOF
chown root:root /usr/local/bin/cloudflare-update
chmod 775 /usr/local/bin/cloudflare-update
tee /etc/systemd/system/cf-updater.service >/dev/null << EOF
[Unit]
Description=updates nginx's snippets using cloudflare ips
[Service]
Type=oneshot
User=root
WorkingDirectory=/etc/nginx/snippets
ExecStart=/usr/local/bin/cloudflare-update
EOF
tee /etc/systemd/system/cf-updater.timer >/dev/null << EOF
[Unit]
Description=Run cf-updater every week.
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
EOF
systemctl enable cf-updater.timer
systemctl start cf-updater.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment