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.

46 lines
1.3 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 -q '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. fi
  18. fi
  19. if [ ! -f "/etc/nginx/nginx.key" ]; then
  20. NGINX_KEY=/etc/nginx/nginx.key
  21. NGINX_CER=/etc/nginx/nginx.cer
  22. OPENSSL_BIN=/usr/bin/openssl
  23. PX5G_BIN=/usr/sbin/px5g
  24. # Prefer px5g for certificate generation (existence evaluated last)
  25. GENKEY_CMD=""
  26. UNIQUEID=$(dd if=/dev/urandom bs=1 count=4 | hexdump -e '1/1 "%02x"')
  27. [ -x "$OPENSSL_BIN" ] && GENKEY_CMD="$OPENSSL_BIN req -x509 -nodes"
  28. [ -x "$PX5G_BIN" ] && GENKEY_CMD="$PX5G_BIN selfsigned"
  29. [ -n "$GENKEY_CMD" ] && {
  30. $GENKEY_CMD \
  31. -days 730 -newkey rsa:2048 -keyout "${NGINX_KEY}.new" -out "${NGINX_CER}.new" \
  32. -subj /C="ZZ"/ST="Somewhere"/L="Unknown"/O="OpenWrt""$UNIQUEID"/CN="OpenWrt"
  33. sync
  34. mv "${NGINX_KEY}.new" "${NGINX_KEY}"
  35. mv "${NGINX_CER}.new" "${NGINX_CER}"
  36. }
  37. fi
  38. exit 0