You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB

  1. #!/bin/sh
  2. if [ -f "/etc/nginx/luci_nginx_ssl.conf" ] && [ -f "/etc/nginx/nginx.conf" ]; then
  3. if [ ! "$(cat '/etc/nginx/nginx.conf' | grep 'return 301 https://$host$request_uri;')" ]; then
  4. if [ -f "/etc/nginx/nginx.conf_old" ]; then
  5. rm /etc/nginx/nginx.conf
  6. else
  7. mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_old
  8. fi
  9. mv /etc/nginx/luci_nginx_ssl.conf /etc/nginx/nginx.conf
  10. core_number=$(grep -c ^processor /proc/cpuinfo)
  11. sed -i "3s/.*/worker_processes "$core_number";/" /etc/nginx/nginx.conf
  12. if [ -n "$(pgrep nginx)" ]; then
  13. /etc/init.d/nginx restart
  14. else
  15. /etc/init.d/nginx start
  16. fi
  17. else
  18. rm /etc/nginx/luci_nginx_ssl.conf
  19. fi
  20. fi
  21. if [ ! -f "/etc/nginx/nginx.key" ]; then
  22. NGINX_KEY=/etc/nginx/nginx.key
  23. NGINX_CER=/etc/nginx/nginx.cer
  24. OPENSSL_BIN=/usr/bin/openssl
  25. PX5G_BIN=/usr/sbin/px5g
  26. # Prefer px5g for certificate generation (existence evaluated last)
  27. GENKEY_CMD=""
  28. UNIQUEID=$(dd if=/dev/urandom bs=1 count=4 | hexdump -e '1/1 "%02x"')
  29. [ -x "$OPENSSL_BIN" ] && GENKEY_CMD="$OPENSSL_BIN req -x509 -nodes"
  30. [ -x "$PX5G_BIN" ] && GENKEY_CMD="$PX5G_BIN selfsigned"
  31. [ -n "$GENKEY_CMD" ] && {
  32. $GENKEY_CMD \
  33. -days 730 -newkey rsa:2048 -keyout "${NGINX_KEY}.new" -out "${NGINX_CER}.new" \
  34. -subj /C="ZZ"/ST="Somewhere"/L="Unknown"/O="OpenWrt""$UNIQUEID"/CN="OpenWrt"
  35. sync
  36. mv "${NGINX_KEY}.new" "${NGINX_KEY}"
  37. mv "${NGINX_CER}.new" "${NGINX_CER}"
  38. }
  39. fi
  40. exit 0