From 5d7f33ce6666edfdb12a8a6ebe7abf06cb96fb88 Mon Sep 17 00:00:00 2001 From: "nap.liu" Date: Fri, 30 Jun 2023 23:58:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20download=20station=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B7=BB=E5=8A=A0bt=20trackerlist=20?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=20=E5=A2=9E=E5=8A=A0=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwebstation=20docker=20=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E9=97=A8=E6=88=B7=E6=98=A0=E5=B0=84=E5=A4=B1=E8=B4=A5=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=20=E5=A2=9E=E5=8A=A0=20webstation=20=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E9=97=A8=E6=88=B7=E5=BC=80=E5=90=AFwebsocket=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auto-add-bt-trackerlist.sh | 165 ++++++++++++++ ...reload-web-station-docker-reverse-proxy.sh | 177 +++++++++++++++ enable-web-station-websocket.sh | 204 ++++++++++++++++++ 3 files changed, 546 insertions(+) create mode 100755 auto-add-bt-trackerlist.sh create mode 100755 auto-reload-web-station-docker-reverse-proxy.sh create mode 100644 enable-web-station-websocket.sh diff --git a/auto-add-bt-trackerlist.sh b/auto-add-bt-trackerlist.sh new file mode 100755 index 0000000..efca660 --- /dev/null +++ b/auto-add-bt-trackerlist.sh @@ -0,0 +1,165 @@ +#!/bin/bash + +DISABLE_COLOR=false + +BLACK="\e[30m" +RED="\e[31m" +GREEN="\e[32m" +YELLOW="\e[33m" +BLUE="\e[34m" +MAGENTA="\e[35m" +CYAN="\e[36m" +LIGHT_GRAY="\e[37m" +DARK_GRAY="\e[90m" +LIGHT_RED="\e[91m" +LIGHT_GREEN="\e[92m" +LIGHT_YELLOW="\e[93m" +LIGHT_BLUE="\e[94m" +LIGHT_MAGENTA="\e[95m" +LIGHT_CYAN="\e[96m" +WHITE="\e[97m" +END="\e[0m" + +if [ $DISABLE_COLOR == true ]; then + BLACK="" + RED="" + GREEN="" + YELLOW="" + BLUE="" + MAGENTA="" + CYAN="" + LIGHT_GRAY="" + DARK_GRAY="" + LIGHT_RED="" + LIGHT_GREEN="" + LIGHT_YELLOW="" + LIGHT_BLUE="" + LIGHT_MAGENTA="" + LIGHT_CYAN="" + WHITE="" + END="" +fi + +# 群晖DS 任务状态定义 +TASK_STATUS_WAITING=1 +TASK_STATUS_DOWNLOADING=2 +TASK_STATUS_PAUSED=3 +TASK_STATUS_FINISHING=4 +TASK_STATUS_FINISHED=5 +TASK_STATUS_HASH_CHECKING=6 +TASK_STATUS_SEEDING=7 +TASK_STATUS_FILEHOSTING_WAITING=8 +TASK_STATUS_EXTRACTING=9 +TASK_STATUS_ERROR=10 +TASK_STATUS_BROKEN_LINK=11 +TASK_STATUS_DESTINATION_NOT_EXIST=12 +TASK_STATUS_DESTINATION_DENIED=13 +TASK_STATUS_DISK_FULL=14 +TASK_STATUS_QUOTA_REACHED=15 +TASK_STATUS_TIMEOUT=16 +TASK_STATUS_EXCEED_MAX_FILE_SYSTEM_SIZE=17 +TASK_STATUS_EXCEED_MAX_DESTINATION_SIZE=18 +TASK_STATUS_EXCEED_MAX_TEMP_SIZE=19 +TASK_STATUS_ENCRYPTED_NAME_TOO_LONG=20 +TASK_STATUS_NAME_TOO_LONG=21 +TASK_STATUS_TORRENT_DUPLICATE=22 +TASK_STATUS_FILE_NOT_EXIST=23 +TASK_STATUS_REQUIRED_PREMIUM_ACCOUNT=24 +TASK_STATUS_NOT_SUPPORTED_TYPE=25 +TASK_STATUS_TRY_IT_LATER=26 +TASK_STATUS_TASK_ENCRYPTION=27 +TASK_STATUS_MISSING_PYTHON=28 +TASK_STATUS_PRIVATE_VIDEO=29 +TASK_STATUS_FTP_ENCRYPTION_NOT_SUPPORTED_TYPE=30 +TASK_STATUS_EXTRACT_FAILED=31 +TASK_STATUS_EXTRACT_FAILED_WRONG_PASSWORD=32 +TASK_STATUS_EXTRACT_FAILED_INVALID_ARCHIVE=33 +TASK_STATUS_EXTRACT_FAILED_QUOTA_REACHED=34 +TASK_STATUS_EXTRACT_FAILED_DISK_FULL=35 +TASK_STATUS_UNKNOWN=36 + +trackerlistUrl=( + "https://git.nap-liu.i234.me/github/trackerslist/raw/branch/master/trackers_all.txt" + # "https://gitea.com/XIU2/TrackersListCollection/raw/branch/master/all.txt" +) + +trackerList='[]' + +for url in ${trackerlistUrl[@]}; do + list=$(curl -f $url 2>/dev/null) + if [[ $? != 0 ]]; then + echo "Tracker list url unavailable please check url: $url" + continue + fi + list=$(echo '"'$list'"' | jq -c 'split(" ")') + if [[ $(jq 'length' <<<$list) == 0 ]]; then + echo "Tracker list url is empty please check url: $url" + continue + fi + echo "Tracker list url: $url has tracker count: $(jq 'length' <<<$list)" + trackerList=$(jq -c --argjson list $trackerList '$list + . | unique' <<<$list) +done + +trackerListLength=$(jq 'length' <<<$trackerList) + +if [[ $trackerListLength == 0 ]]; then + echo "No found tracker please check tracker list: $trackerlistUrl" + exit 1 +fi + +echo "Total load tracker url count: $trackerListLength" + +pakcageList=$(synowebapi --exec api=SYNO.Core.Package method=list version=2 additional='["status"]' 2>/dev/null | jq -c '.data.packages | map(select(.id == "DownloadStation"))') + +isRunning=$(jq -r 'map(select(.id == "DownloadStation") | .additional.status) | .[]' <<<$pakcageList) + +if [ $isRunning != "running" ]; then + echo -e "Download Station$RED not running$END exit" + exit +fi + +echo "Download Station is running go next step" + +downloadList=$(synowebapi --exec api=SYNO.DownloadStation2.Task method=list version=2 limit=-1 "status=[$TASK_STATUS_DOWNLOADING]" 2>/dev/null | jq -c '.data.task | map(select(.type == "bt"))') +downloadListCount=$(jq 'length' <<<$downloadList) + +if [[ $downloadListCount -eq 0 ]]; then + echo "Not found bt download task" + exit +fi + +echo "Download tasks count: $downloadListCount" + +for idx in $(seq 0 $((downloadListCount - 1))); do + echo "----------------Task [$idx]------------------" + task=$(jq -c ".[$idx]" <<<$downloadList) + taskId=$(jq -r .id <<<$task) + taskName=$(jq -r .title <<<$task) + taskTrackerList=$(synowebapi --exec api=SYNO.DownloadStation2.Task.BT.Tracker method=list version=2 task_id="$taskId" limit=-1 2>/dev/null | jq -c '.') + taskTrackerListMap=$(jq -c '.data.items | reduce .[] as $i ({}; .[$i.url] = 1)' 2>/dev/null <<<$taskTrackerList) + if [[ $? != 0 ]]; then + echo "Task: 【$taskName】 is busy skip task; $taskTrackerList" + continue + fi + taskTrackerListCount=$(jq '.data.total' <<<$taskTrackerList) + echo "Task:【$taskName】 has tracker count: $taskTrackerListCount" + needAddedTrackerList=$(jq -c --argjson hashMap $taskTrackerListMap 'map(select(. | in($hashMap) == false))' <<<$trackerList) + needAddedTrackerListLength=$(jq 'length' <<<$needAddedTrackerList) + + if [[ $needAddedTrackerListLength == 0 ]]; then + echo "The task does not need to add a new tracker" + continue + fi + + echo "A list of trackers that need to be added to the task: $needAddedTrackerList , count: $needAddedTrackerListLength" + + result=$(synowebapi --exec api=SYNO.DownloadStation2.Task.BT.Tracker method=add version=2 task_id=$taskId tracker=$needAddedTrackerList 2>/dev/null | jq -c '.') + + if [[ $(jq '.success' <<<$result) == true ]]; then + echo "Task: [$taskName] tracker list add success" + else + echo "Task: [$taskName] tracker list add fail: $result" + fi +done + +echo "Automatically add tracker script to complete" diff --git a/auto-reload-web-station-docker-reverse-proxy.sh b/auto-reload-web-station-docker-reverse-proxy.sh new file mode 100755 index 0000000..0246137 --- /dev/null +++ b/auto-reload-web-station-docker-reverse-proxy.sh @@ -0,0 +1,177 @@ +#!/bin/bash +DISABLE_COLOR=true + +BLACK="\e[30m" +RED="\e[31m" +GREEN="\e[32m" +YELLOW="\e[33m" +BLUE="\e[34m" +MAGENTA="\e[35m" +CYAN="\e[36m" +LIGHT_GRAY="\e[37m" +DARK_GRAY="\e[90m" +LIGHT_RED="\e[91m" +LIGHT_GREEN="\e[92m" +LIGHT_YELLOW="\e[93m" +LIGHT_BLUE="\e[94m" +LIGHT_MAGENTA="\e[95m" +LIGHT_CYAN="\e[96m" +WHITE="\e[97m" +END="\e[0m" + +if [ $DISABLE_COLOR == true ]; then + BLACK="" + RED="" + GREEN="" + YELLOW="" + BLUE="" + MAGENTA="" + CYAN="" + LIGHT_GRAY="" + DARK_GRAY="" + LIGHT_RED="" + LIGHT_GREEN="" + LIGHT_YELLOW="" + LIGHT_BLUE="" + LIGHT_MAGENTA="" + LIGHT_CYAN="" + WHITE="" + END="" +fi + +restartDockerContainer() { + stop=$(synowebapi --exec api=SYNO.Docker.Container method=stop version=1 name=$1 2>/dev/null) + status=$(jq '.success' <<<$stop) + if [ $status == false ]; then + echo -e $RED"stop container: $1 error: $stop"$END + fi + + start=$(synowebapi --exec api=SYNO.Docker.Container method=start version=1 name=$1 2>/dev/null) + status=$(jq '.success' <<<$start) + if [ $status == false ]; then + echo -e $RED"start container: $1 error: $stop"$END + fi + + echo "restart container finished: $1" +} + +checkServiceIsOffline() { + echo -e "checking container: $GREEN$1$END url: $GREEN$2$END" + result=$(curl -I $2 2>/dev/null | grep 200) + if [ ${#result} -eq 0 ]; then + echo -e "conatiner need restarted: $RED$1$END" + return 1 + else + echo -e "container is$BLUE online$END" + return 0 + fi +} + +pakcageList=$(synowebapi --exec api=SYNO.Core.Package method=list version=2 additional='["status"]' 2>/dev/null | jq -c '.data.packages | map(select(.id == "WebStation" or .id == "ContainerManager"))') + +isRunning=$(jq -r 'map(select(.id == "WebStation") | .additional.status) | .[]' <<<$pakcageList) + +if [ $isRunning != "running" ]; then + echo -e "Web Station$RED not running$END exit" + exit 0 +fi +echo "Web Station is running go next step" + +isRunning=$(jq -r 'map(select(.id == "ContainerManager") | .additional.status) | .[]' <<<$pakcageList) + +if [ $isRunning != "running" ]; then + echo -e "Container Manager$RED not running$END exit" + exit 0 +fi +echo "Container Manager is running go next step" + +webStationPortal=$(synowebapi --exec api=SYNO.WebStation.WebService.Portal method=list version=1 2>/dev/null | jq -c '.data.portals | map({host: .fqdn, http: .http_port[0], https: .https_port[0], service: .service, display_name: .shortcut.display_name})') +webStationPortalCount=$(jq 'length' <<<$webStationPortal) + +if [ $webStationPortalCount -eq 0 ]; then + echo -e $RED"not found Web Station portal"$END + exit 0 +fi + +echo -e "found Web Station portal count: $RED$webStationPortalCount"$END + +webStationService=$(synowebapi --exec api=SYNO.WebStation.WebService.Service method=list version=1 2>/dev/null | jq -c '.data.services | map(select(.category == "Docker"))') +webStationServiceCount=$(jq 'length' <<<$webStationService) + +if [ $webStationServiceCount -eq 0 ]; then + echo -e $RED"not found Web Station services exit"$END + exit 0 +fi + +echo -e "found Web Station services count: $RED$webStationServiceCount"$END + +dockerContainer=$(synowebapi --exec api=SYNO.Docker.Container method=list version=1 limit=-1 offset=0 type=all 2>/dev/null | jq -c '.data.containers | map(. | select(.status=="running" and .services[0] != null) | {name: .name, status: .status, id: .id, service: .services[0]})') +dockerContainerCount=$(jq 'length' <<<$dockerContainer) +echo -e "found docker container count: $RED$dockerContainerCount"$END + +for idx in $(seq 0 $(($webStationServiceCount - 1))); do + echo "-------------------[$((idx + 1))]---------------------" + service=$(jq -c ".[$idx]" <<<$webStationService) + serviceName=$(jq -r '.display_name' <<<$service) + serviceId=$(jq '.service' <<<$service) + serviceEnable=$(jq '.enable' <<<$service) + + portal=$(jq -c "map(select(.service == $serviceId)) | .[]" <<<$webStationPortal) + portalHost=$(jq -r '.host' <<<$portal) + + echo -e "service: $GREEN$serviceName$END enable: $BLUE$serviceEnable$END" + + if [ $serviceEnable == true ]; then + echo -e "reverse proxy: $GREEN$portalHost$END ➡️ $GREEN$serviceName$END is$BLUE running$END skip" + continue + fi + + if [ ${#portal} -ne 0 ]; then + container=$(jq -c "map(select(.service.service == $serviceId)) | .[]" <<<$dockerContainer) + + if [ ${#container} -eq 0 ]; then + echo "service: $GREEN$serviceName$END container$RED is not found skip$END" + continue + fi + + containerName=$(jq -r '.name' <<<$container) + containerStatus=$(jq -r '.status' <<<$container) + + echo -e "service container: $GREEN$containerName$END status: $GREEN$containerStatus$END" + + if [ $containerStatus != 'running' ]; then + echo -e "contianer$GREEN $containerName$END is$RED not running$END skip" + continue + fi + + httpPort=$(jq '.http' <<<$portal) + httpsPort=$(jq '.https' <<<$portal) + proxyTarget=$(jq -r '.proxy_target' <<<$service) + httpOffline=0 + httpsOffline=0 + + if [ ${#httpPort} -ne 0 ]; then + echo -e "reverse proxy found$GREEN http://$portalHost:$httpsPort $END➡️ $GREEN $proxyTarget $END" + checkServiceIsOffline $containerName "http://$portalHost:$httpPort" + httpOffline=$? + fi + + if [ ${#httpsPort} -ne 0 ]; then + echo -e "reverse proxy found$GREEN https://$portalHost:$httpsPort $END➡️ $GREEN $proxyTarget $END" + checkServiceIsOffline $containerName "https://$portalHost:$httpsPort" + httpsOffline=$? + fi + + if [[ $httpOffline -eq 1 || $httpsOffline -eq 1 ]]; then + restartDockerContainer $containerName + else + echo "service: $RED$serviceName is$BLUE online$END proccess next" + fi + + else + echo -e "service:$RED $serviceName is not found $END" + fi + +done + +echo "script finished" diff --git a/enable-web-station-websocket.sh b/enable-web-station-websocket.sh new file mode 100644 index 0000000..1f1bcbe --- /dev/null +++ b/enable-web-station-websocket.sh @@ -0,0 +1,204 @@ +#!/bin/bash + +DISABLE_COLOR=true + +BLACK="\e[30m" +RED="\e[31m" +GREEN="\e[32m" +YELLOW="\e[33m" +BLUE="\e[34m" +MAGENTA="\e[35m" +CYAN="\e[36m" +LIGHT_GRAY="\e[37m" +DARK_GRAY="\e[90m" +LIGHT_RED="\e[91m" +LIGHT_GREEN="\e[92m" +LIGHT_YELLOW="\e[93m" +LIGHT_BLUE="\e[94m" +LIGHT_MAGENTA="\e[95m" +LIGHT_CYAN="\e[96m" +WHITE="\e[97m" +END="\e[0m" + +if [ $DISABLE_COLOR == true ]; then + BLACK="" + RED="" + GREEN="" + YELLOW="" + BLUE="" + MAGENTA="" + CYAN="" + LIGHT_GRAY="" + DARK_GRAY="" + LIGHT_RED="" + LIGHT_GREEN="" + LIGHT_YELLOW="" + LIGHT_BLUE="" + LIGHT_MAGENTA="" + LIGHT_CYAN="" + WHITE="" + END="" +fi + +nginxServiceTemplate='/var/packages/WebStation/target/misc/nginx_service_template.mustache' + +pakcageList=$(synowebapi --exec api=SYNO.Core.Package method=list version=2 additional='["status"]' 2>/dev/null | jq -c '.data.packages | map(select(.id == "WebStation" or .id == "ContainerManager"))') + +isRunning=$(jq -r 'map(select(.id == "WebStation") | .additional.status) | .[]' <<<$pakcageList) + +if [ $isRunning != "running" ]; then + echo -e "Web Station$RED not running$END exit" + exit 0 +fi +echo "Web Station is running go next step" + +isRunning=$(jq -r 'map(select(.id == "ContainerManager") | .additional.status) | .[]' <<<$pakcageList) + +if [ $isRunning != "running" ]; then + echo -e "Container Manager$RED not running$END exit" + exit 0 +fi +echo "Container Manager is running go next step" + +webStationService=$(synowebapi --exec api=SYNO.WebStation.WebService.Service method=list version=1 2>/dev/null | jq -c '.data.services | map(select(.category == "Docker"))') +webStationServiceCount=$(jq 'length' <<<$webStationService) + +if [ $webStationServiceCount -eq 0 ]; then + echo -e $RED"not found Web Station services exit"$END + exit 0 +fi + +dockerContainer=$(synowebapi --exec api=SYNO.Docker.Container method=list version=1 limit=-1 offset=0 type=all 2>/dev/null | jq -c '.data.containers | map(. | select(.status=="running" and .services[0] != null) | {name: .name, status: .status, id: .id, service: .services[0]})') +dockerContainerCount=$(jq 'length' <<<$dockerContainer) +if [[ $dockerContainerCount == 0 ]]; then + echo -e $RED"not found Containers exit"$END +fi + +echo -e "found docker container count: $RED$dockerContainerCount"$END + +dispalyServices() { + echo "请输入服务序号 启用WebSocket" + jq -r 'map(.display_name) | to_entries | map((.key + 1 | tostring) + ") " + .value) | .[]' <<<$webStationService +} + +selectService() { + dispalyServices + while read index; do + if ((index - 1 < 0)); then + dispalyServices + continue + fi + service=$(jq -e -c ".[$((index - 1))]" <<<$webStationService) + if [[ $? == 0 ]]; then + break + fi + dispalyServices + done +} + +modifyNginxServiceTemplate() { + local backup="$nginxServiceTemplate.$(date +%Y%m%d%H%M).bak" + if [[ ! -f $backup ]]; then + echo "备份 nginx service template文件到: $backup" + cp $nginxServiceTemplate $backup + fi + sed -i 's/{{> @proxy_rule@ }}/{{> @proxy_rule@ }}\n\n \/usr\/local\/etc\/nginx\/conf.d\/{{service_id}}\/proxy.conf*;\n\n/' $nginxServiceTemplate + checkNginxServiceTemplate +} + +checkNginxServiceTemplate() { + if [[ $(grep '/usr/local/etc/nginx/conf.d/{{service_id}}/proxy.conf' $nginxServiceTemplate) == '' ]]; then + echo "nginx service 自定义location不存在 添加指令到模版" + modifyNginxServiceTemplate + else + echo "nginx service 自定义location已经存在" + fi +} + +modifyCustomNginxProxy() { + local proxyConfig="/usr/local/etc/nginx/conf.d/$1/proxy.conf" + local backup="/usr/local/etc/nginx/conf.d/$1/proxy.conf.$(date +%Y%m%d%H%M).bak" + if [[ -f $proxyConfig ]]; then + + if [[ $(cat $proxyConfig | grep proxy_set_header | grep Connection | grep '$connection_upgrade') == '' ]]; then + echo "自定义 proxy.conf 中没有启用websocket\n开始启用websocket" + + if [[ ! -f $backup ]]; then + cp $proxyConfig $backup + echo "备份 proxy.conf 到 $backup" + fi + + cat >>$proxyConfig <$proxyConfig </dev/null) + status=$(jq '.success' <<<$stop) + if [ $status == false ]; then + echo -e $RED"stop container: $1 error: $stop"$END + fi + + start=$(synowebapi --exec api=SYNO.Docker.Container method=start version=1 name=$1 2>/dev/null) + status=$(jq '.success' <<<$start) + if [ $status == false ]; then + echo -e $RED"start container: $1 error: $stop"$END + fi + + echo "restart container finished: $1" +} + +selectService + +serviceId=$(jq -r '.service' <<<$service) +serviceName=$(jq -r "map(select(.service.id == \"$serviceId\")) | .[] | .name" <<<$dockerContainer) + +if [[ -f $nginxServiceTemplate ]]; then + checkNginxServiceTemplate + modifyCustomNginxProxy $serviceId + if [[ $? == 0 ]]; then + echo "配置信息已修改 重启容器中" + restartDockerContainer $serviceName + fi +else + echo "nginx template 文件不存在 请检查WebStation套件是否正常" + exit 1 +fi