andyzhshg / syno-acme

通过acme协议更新群晖HTTPS泛域名证书的自动脚本
MIT License
745 stars 265 forks source link

依旧使用letsencrypt作为加密证书提供商,并自动获取最新版acm.sh #78

Open wanghan0501 opened 2 years ago

wanghan0501 commented 2 years ago
  1. 依旧使用letsencrypt作为加密证书提供商
  2. 自动获取最新版acm.sh
  3. 适配群辉6.2X
iihong commented 2 years ago

学习了,以下是我的一点优化

1、在 config 配置文件中加入

# The cert server has zerossl and letsencrypt
export CERT_SERVER=letsencrypt

# ZeroSSL registered email
export ACCOUNT_EMAIL="xxx@xxx.com"

通过设置 CERT_SERVER 为 zerossl 或 letsencrypt 来决定证书服务商 设置为 zerossl 时:必须设置 ACCOUNT_EMAIL,并以 ZeroSSL 提供证书服务更新 设置为 letsencrypt 时:以 Let's Encrypt 提供证书服务更新,如果出现code:60错误,无法建立SSL连接,请升级群辉内置CA机构根证书

2、在 cert-up.sh 文件中修改 installAcme () {} 方法,并增加 versionLt () {} 方法 解决重复下载安装的问题,并增加自动效验acme.sh新版,有新版则自动升级acme.sh

versionLt () { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }
installAcme () {
  ALLOW_INSTALL=false
  ACME_SH_FILE=${ACME_BIN_PATH}/acme.sh
  ACME_SH_NEW_VERSION=$(wget -qO- -t1 -T2 "https://api.github.com/repos/acmesh-official/acme.sh/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
  ACME_SH_ADDRESS=https://mirror.ghproxy.com/https://github.com/acmesh-official/acme.sh/archive/${ACME_SH_NEW_VERSION}.tar.gz
  if [ -z "${ACME_SH_NEW_VERSION}" ]; then
    echo 'unable to get new version number'
    return 0
  fi
  if [ ! -f "${ACME_SH_FILE}" ]; then
    ALLOW_INSTALL=true
    echo 'acme not installed, start install'
  else
    ACME_SH_VERSION=$(cat ${ACME_SH_FILE} | grep "VER=*" | head -n 1 | awk -F "=" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
    if versionLt ${ACME_SH_VERSION} ${ACME_SH_NEW_VERSION}; then
      ALLOW_INSTALL=true
      echo 'acme has a new version, start updating'
    else
      echo 'skip acme installation'
    fi
  fi
  if [ ${ALLOW_INSTALL} == true ]; then
    echo 'in progress...'
    mkdir -p ${TEMP_PATH}
    cd ${TEMP_PATH}
    echo 'begin downloading acme.sh tool...'
    # ACME_SH_ADDRESS=`curl -L https://cdn.jsdelivr.net/gh/andyzhshg/syno-acme@master/acme.sh.address`
    SRC_TAR_NAME=acme.sh.tar.gz
    curl -L -o ${SRC_TAR_NAME} ${ACME_SH_ADDRESS}
    SRC_NAME=`tar -tzf ${SRC_TAR_NAME} | head -1 | cut -f1 -d"/"`
    tar zxvf ${SRC_TAR_NAME}
    echo 'begin installing acme.sh tool...'
    cd ${SRC_NAME}
    ./acme.sh --install --nocron --home ${ACME_BIN_PATH}
    echo 'done installAcme'
    rm -rf ${TEMP_PATH}
  fi
  return 0
}

3、在 cert-up.sh 文件中修改 generateCrt () {} 方法 增加 ZeroSSL 账户自动注册代码,在 ... acme.sh --force --log --issue 后面增加 --server ${CERT_SERVER}

generateCrt () {
  echo 'begin generateCrt'
  cd ${BASE_ROOT}
  source ./config
  # add register zerossl account
  if [ ${CERT_SERVER} == 'zerossl' ]; then
    echo 'register zerossl account'
    ${ACME_BIN_PATH}/acme.sh  --register-account  -m ${ACCOUNT_EMAIL} --server zerossl
  fi
  echo 'begin updating default cert by acme.sh tool'
  source ${ACME_BIN_PATH}/acme.sh.env
  # ${ACME_BIN_PATH}/acme.sh --force --log --issue --dns ${DNS} --dnssleep ${DNS_SLEEP} -d "${DOMAIN}" -d "*.${DOMAIN}"
  ${ACME_BIN_PATH}/acme.sh --force --log --issue --server ${CERT_SERVER} --dns ${DNS} --dnssleep ${DNS_SLEEP} -d "${DOMAIN}" -d "*.${DOMAIN}"
  ${ACME_BIN_PATH}/acme.sh --force --installcert -d ${DOMAIN} -d *.${DOMAIN} \
    --certpath ${CRT_PATH}/cert.pem \
    --key-file ${CRT_PATH}/privkey.pem \
    --fullchain-file ${CRT_PATH}/fullchain.pem

  if [ -s "${CRT_PATH}/cert.pem" ]; then
    echo 'done generateCrt'
    return 0
  else
    echo '[ERR] fail to generateCrt'
    echo "begin revert"
    revertCrt
    exit 1;
  fi
}

https://github.com/andyzhshg/syno-acme/issues/77#issuecomment-984407513

wanghan0501 commented 2 years ago

没毛病!

dongfengpo commented 2 years ago

作者好像不更新了,能不能fork一份把pr整合一下呢

wanghan0501 commented 2 years ago

提交pr本身就要fork一份,你可以直接用我的脚本

yosheng commented 2 years ago

测试 6.1.7 也可以透过这个脚本更新 感谢大佬