Compare commits

..
Author SHA1 Message Date
whyour f4337008eb 修复文件校验 2024-07-07 21:26:11 +08:00
whyour ca5cb88eca 测试 cli 2024-07-07 20:17:36 +08:00
177 changed files with 11571 additions and 19943 deletions
+11 -8
View File
@@ -1,11 +1,14 @@
GRPC_PORT=5500 UPDATE_PORT=5300
BACK_PORT=5700 PUBLIC_PORT=5400
CRON_PORT=5500
BACK_PORT=5600
PORT=5700
LOG_LEVEL='info' LOG_LEVEL='debug'
JWT_SECRET= SECRET='whyour'
JWT_EXPIRES_IN=
QINIU_AK= QINIU_AK=''
QINIU_SK= QINIU_SK=''
QINIU_SCOPE= QINIU_SCOPE=''
TEMP=''
-4
View File
@@ -1,4 +0,0 @@
---
name: Bug Fixer
description: Fix this issue following our error handling pattern.
---
@@ -7,50 +7,45 @@ on:
branches: branches:
- "master" - "master"
- "develop" - "develop"
- "test-cli"
tags: tags:
- "v*" - "v*"
schedule:
- cron: "00 20 * * *"
workflow_dispatch: workflow_dispatch:
jobs: jobs:
code_gitlab: to_gitlab:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v6 - uses: actions/checkout@v4
with: with:
fetch-depth: 0 fetch-depth: 0
- uses: Yikun/hub-mirror-action@master - uses: pixta-dev/repository-mirroring-action@v1
with: with:
src: github/whyour target_repo_url: git@gitlab.com:whyour/qinglong.git
dst: gitlab/whyour ssh_private_key: ${{ secrets.GITLAB_SSH_PK }}
dst_key: ${{ secrets.GITLAB_SSH_PK }}
dst_token: ${{ secrets.GITLAB_TOKEN }}
static_list: "qinglong"
force_update: true
code_gitee: to_gitee:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v6 - uses: actions/checkout@v4
with: with:
fetch-depth: 0 fetch-depth: 0
- uses: Yikun/hub-mirror-action@master - uses: pixta-dev/repository-mirroring-action@v1
with: with:
src: github/whyour target_repo_url: git@gitee.com:whyour/qinglong.git
dst: gitee/whyour ssh_private_key: ${{ secrets.GITLAB_SSH_PK }}
dst_key: ${{ secrets.GITLAB_SSH_PK }}
dst_token: ${{ secrets.GITEE_TOKEN }}
static_list: "qinglong"
force_update: true
build-static: build-static:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v6 - uses: actions/checkout@v4
- uses: pnpm/action-setup@v4 - uses: pnpm/action-setup@v3
with: with:
version: "8.3.1" version: "8.3.1"
- uses: actions/setup-node@v6 - uses: actions/setup-node@v4
with: with:
cache: "pnpm" cache: "pnpm"
@@ -77,66 +72,39 @@ jobs:
git commit --allow-empty -m "copy static at $(date +'%Y-%m-%d %H:%M:%S')" git commit --allow-empty -m "copy static at $(date +'%Y-%m-%d %H:%M:%S')"
git push --force --quiet "https://${{ secrets.API_TOKEN }}@${GITHUB_REPO}.git" ${GITHUB_BRANCH}:${GITHUB_BRANCH} git push --force --quiet "https://${{ secrets.API_TOKEN }}@${GITHUB_REPO}.git" ${GITHUB_BRANCH}:${GITHUB_BRANCH}
static_gitlab: mkdir -p ~/.ssh
needs: build-static echo "${PRIVATE_KEY}" > ~/.ssh/id_rsa
runs-on: ubuntu-latest chmod 600 ~/.ssh/id_rsa
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- uses: Yikun/hub-mirror-action@master
with:
src: github/whyour
dst: gitlab/whyour
dst_key: ${{ secrets.GITLAB_SSH_PK }}
dst_token: ${{ secrets.GITLAB_TOKEN }}
static_list: "qinglong-static"
force_update: true
static_gitee: export GIT_SSH_COMMAND="ssh -v -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no -l git"
needs: build-static git remote add gitee "${REPO_GITEE}"
runs-on: ubuntu-latest git remote add gitlab "${REPO_GITLAB}"
steps: git gc
- uses: actions/checkout@v6 git push --force --quiet gitee ${GITHUB_BRANCH}:${GITHUB_BRANCH}
with: git push --force --quiet gitlab ${GITHUB_BRANCH}:${GITHUB_BRANCH}
fetch-depth: 0
- uses: Yikun/hub-mirror-action@master
with:
src: github/whyour
dst: gitee/whyour
dst_key: ${{ secrets.GITLAB_SSH_PK }}
dst_token: ${{ secrets.GITEE_TOKEN }}
static_list: "qinglong-static"
force_update: true
build: build:
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
needs: build-static needs: build-static
runs-on: ubuntu-22.04 # 由于 ubuntu-latest 中使用 linux6.x 内核 linux/s390x npm 无法使用
runs-on: ubuntu-20.04
# runs-on: self-hosted
permissions: permissions:
packages: write packages: write
contents: read contents: read
steps: steps:
- uses: actions/checkout@v6 - uses: actions/checkout@v4
- uses: pnpm/action-setup@v4 - uses: pnpm/action-setup@v3
with: with:
version: "8.3.1" version: "8.3.1"
- uses: actions/setup-node@v6 - uses: actions/setup-node@v4
with: with:
cache: "pnpm" cache: "pnpm"
- name: Read version from version.yaml
id: version
run: |
VERSION=$(grep '^version:' version.yaml | awk '{print $2}')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Version: $VERSION"
- name: Setup timezone - name: Setup timezone
uses: szenius/set-timezone@v2.0 uses: szenius/set-timezone@v1.2
with: with:
timezoneLinux: Asia/Shanghai timezoneLinux: Asia/Shanghai
@@ -160,13 +128,19 @@ jobs:
images: | images: |
${{ github.repository }} ${{ github.repository }}
ghcr.io/${{ github.repository }} ghcr.io/${{ github.repository }}
# generate Docker tags based on the following events/attributes
# nightly, master, pr-2, 1.2.3, 1.2, 1
flavor: | flavor: |
latest=false latest=false
tags: | tags: |
type=ref,event=branch,enable=${{ github.ref == format('refs/heads/{0}', 'develop') }} type=schedule,pattern=nightly
type=edge
type=ref,event=pr
type=ref,event=branch,enable=${{ github.ref != format('refs/heads/{0}', 'master') }}
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }} type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
type=raw,value=${{ steps.version.outputs.version }},enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
type=semver,pattern={{version}} type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- name: Set up QEMU - name: Set up QEMU
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v3
@@ -176,15 +150,15 @@ jobs:
- name: Build and push - name: Build and push
id: docker_build id: docker_build
uses: docker/build-push-action@v6 uses: docker/build-push-action@v5
with: with:
build-args: | build-args: |
MAINTAINER=${{ github.repository_owner }} MAINTAINER=${{ github.repository_owner }}
QL_BRANCH=${{ github.ref_name }} QL_BRANCH=${{ github.ref_name }}
SOURCE_COMMIT=${{ github.sha }} SOURCE_COMMIT=${{ github.sha }}
network: host network: host
# linux/s390x npm 暂不可用 platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x,linux/386
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/386 # platforms: linux/amd64,linux/arm64,linux/ppc64le,linux/s390x,linux/386
context: . context: .
file: ./docker/Dockerfile file: ./docker/Dockerfile
push: true push: true
@@ -201,30 +175,23 @@ jobs:
if: ${{ github.ref_name == 'master' }} if: ${{ github.ref_name == 'master' }}
needs: build-static needs: build-static
runs-on: ubuntu-22.04 runs-on: ubuntu-20.04
permissions: permissions:
packages: write packages: write
contents: read contents: read
steps: steps:
- uses: actions/checkout@v6 - uses: actions/checkout@v4
- uses: pnpm/action-setup@v4 - uses: pnpm/action-setup@v3
with: with:
version: "8.3.1" version: "8.3.1"
- uses: actions/setup-node@v6 - uses: actions/setup-node@v4
with: with:
cache: "pnpm" cache: "pnpm"
- name: Read version from version.yaml
id: version
run: |
VERSION=$(grep '^version:' version.yaml | awk '{print $2}')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Version: $VERSION"
- name: Setup timezone - name: Setup timezone
uses: szenius/set-timezone@v2.0 uses: szenius/set-timezone@v1.2
with: with:
timezoneLinux: Asia/Shanghai timezoneLinux: Asia/Shanghai
@@ -249,21 +216,18 @@ jobs:
- name: Build and push python3.10 - name: Build and push python3.10
id: docker_build_310 id: docker_build_310
uses: docker/build-push-action@v6 uses: docker/build-push-action@v5
with: with:
build-args: | build-args: |
MAINTAINER=${{ github.repository_owner }} MAINTAINER=${{ github.repository_owner }}
QL_BRANCH=${{ github.ref_name }} QL_BRANCH=${{ github.ref_name }}
SOURCE_COMMIT=${{ github.sha }} SOURCE_COMMIT=${{ github.sha }}
network: host network: host
# linux/s390x npm 暂不可用 platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x,linux/386
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/386
context: . context: .
file: ./docker/310.Dockerfile file: ./docker/310.Dockerfile
push: true push: true
tags: | tags: whyour/qinglong:python3.10
whyour/qinglong:python3.10
whyour/qinglong:${{ steps.version.outputs.version }}-python3.10
cache-from: type=registry,ref=whyour/qinglong:cache-python3.10 cache-from: type=registry,ref=whyour/qinglong:cache-python3.10
cache-to: type=registry,ref=whyour/qinglong:cache-python3.10,mode=max cache-to: type=registry,ref=whyour/qinglong:cache-python3.10,mode=max
-5
View File
@@ -23,8 +23,3 @@
.history .history
.version.ts .version.ts
/.tmp /.tmp
__pycache__
/shell/preload/env.*
/shell/preload/notify.*
/shell/preload/*-notify.json
/shell/preload/__ql_notify__.*
+12 -11
View File
@@ -2,22 +2,23 @@
**/*.svg **/*.svg
**/*.ejs **/*.ejs
**/*.html **/*.html
/.umi package.json
/.umi-production .umi
/.umi-test .umi-production
/.history .umi-test
/.tmp .history
/node_modules .tmp
node_modules
npm-debug.log* npm-debug.log*
yarn-error.log yarn-error.log
yarn.lock yarn.lock
package-lock.json package-lock.json
/static static
/data data
DS_Store DS_Store
/src/.umi src/.umi
/src/.umi-production src/.umi-production
/src/.umi-test src/.umi-test
.env.local .env.local
.env .env
version.ts version.ts
+20 -3
View File
@@ -16,8 +16,18 @@ export default defineConfig({
favicons: [`https://qn.whyour.cn/favicon.svg`], favicons: [`https://qn.whyour.cn/favicon.svg`],
publicPath: process.env.NODE_ENV === 'production' ? './' : '/', publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
proxy: { proxy: {
[`${baseUrl}api/update`]: {
target: 'http://127.0.0.1:5300/',
changeOrigin: true,
pathRewrite: { [`^${baseUrl}api/update`]: '/api' },
},
[`${baseUrl}api/public`]: {
target: 'http://127.0.0.1:5400/',
changeOrigin: true,
pathRewrite: { [`^${baseUrl}api/public`]: '/api' },
},
[`${baseUrl}api`]: { [`${baseUrl}api`]: {
target: 'http://127.0.0.1:5700/', target: 'http://127.0.0.1:5600/',
changeOrigin: true, changeOrigin: true,
ws: true, ws: true,
pathRewrite: { [`^${baseUrl}api`]: '/api' }, pathRewrite: { [`^${baseUrl}api`]: '/api' },
@@ -33,12 +43,19 @@ export default defineConfig({
}), }),
); );
}) as any, }) as any,
headScripts: [`./api/env.js`], externals: {
react: 'window.React',
'react-dom': 'window.ReactDOM',
},
headScripts: [
`./api/env.js`,
'https://gw.alipayobjects.com/os/lib/react/18.2.0/umd/react.production.min.js',
'https://gw.alipayobjects.com/os/lib/react-dom/18.2.0/umd/react-dom.production.min.js',
],
copy: [ copy: [
{ {
from: 'node_modules/monaco-editor/min/vs', from: 'node_modules/monaco-editor/min/vs',
to: 'static/dist/monaco-editor/min/vs', to: 'static/dist/monaco-editor/min/vs',
}, },
], ],
npmClient: 'pnpm',
}); });
+121 -8
View File
@@ -18,9 +18,9 @@ Timed task management platform supporting Python3, JavaScript, Shell, Typescript
[docker-image-size-image]: https://img.shields.io/docker/image-size/whyour/qinglong?style=flat [docker-image-size-image]: https://img.shields.io/docker/image-size/whyour/qinglong?style=flat
[docker-image-size-url]: https://hub.docker.com/r/whyour/qinglong [docker-image-size-url]: https://hub.docker.com/r/whyour/qinglong
[Demo](http://demo.qinglong.online:4433/) / [Issues](https://github.com/whyour/qinglong/issues) / [Telegram Channel](https://t.me/jiao_long) / [Buy Me a Coffee](https://www.buymeacoffee.com/qinglong) [Demo](http://demo.dlww.cc:4433/) / [Issues](https://github.com/whyour/qinglong/issues) / [Telegram Channel](https://t.me/jiao_long) / [Buy Me a Coffee](https://www.buymeacoffee.com/qinglong)
[演示](http://demo.qinglong.online:4433/) / [反馈](https://github.com/whyour/qinglong/issues) / [Telegram 频道](https://t.me/jiao_long) / [打赏开发者](https://user-images.githubusercontent.com/22700758/244744295-29cd0cd1-c8bb-4ea1-adf6-29bd390ad4dd.jpg) [演示](http://demo.dlww.cc:4433/) / [反馈](https://github.com/whyour/qinglong/issues) / [Telegram 频道](https://t.me/jiao_long) / [打赏开发者](https://user-images.githubusercontent.com/22700758/244744295-29cd0cd1-c8bb-4ea1-adf6-29bd390ad4dd.jpg)
</div> </div>
![cover](https://user-images.githubusercontent.com/22700758/244847235-8dc1ca21-e03f-4606-9458-0541fab60413.png) ![cover](https://user-images.githubusercontent.com/22700758/244847235-8dc1ca21-e03f-4606-9458-0541fab60413.png)
@@ -41,8 +41,6 @@ Timed task management platform supporting Python3, JavaScript, Shell, Typescript
The `latest` image is built on `alpine` and the `debian` image is built on `debian-slim`. If you need to use a dependency that is not supported by `alpine`, it is recommended that you use the `debian` image. The `latest` image is built on `alpine` and the `debian` image is built on `debian-slim`. If you need to use a dependency that is not supported by `alpine`, it is recommended that you use the `debian` image.
**⚠️ Important**: If you need to run Docker as a **non-root user**, please use the `debian` image. Alpine's `crond` requires root privileges.
```bash ```bash
docker pull whyour/qinglong:latest docker pull whyour/qinglong:latest
docker pull whyour/qinglong:debian docker pull whyour/qinglong:debian
@@ -58,15 +56,130 @@ npm i @whyour/qinglong
## Deployment ## Deployment
[View Documentation](https://qinglong.online/guide/getting-started/installation-guide) ### Docker (Recommended)
## Built-in API ```bash
# curl -sSL get.docker.com | sh
docker run -dit \
-v $PWD/ql/data:/ql/data \
# The 5700 after the colon is the default port, if QlPort is set, it needs to be the same as QlPort.
-p 5700:5700 \
# Deployment paths are not required, e.g. /test.
-e QlBaseUrl="/" \
# Deployment port is not required, when using host mode, you can set the port after service startup, default 5700
-e QlPort="5700" \
--name qinglong \
--hostname qinglong \
--restart unless-stopped \
whyour/qinglong:latest
```
[View Documentation](https://qinglong.online/guide/user-guide/built-in-api) ### Docker-compose (Recommended)
```bash
# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
mkdir qinglong
wget https://raw.githubusercontent.com/whyour/qinglong/master/docker/docker-compose.yml
# start
docker-compose up -d
# stop
docker-compose down
```
### Podman (Recommended)
```bash
# https://podman.io/getting-started/installation
podman run -dit \
--network bridge \
-v $PWD/ql/data:/ql/data \
# The 5700 after the colon is the default port, if QlPort is set, it needs to be the same as QlPort.
-p 5700:5700 \
# Deployment paths are not required, e.g. /test.
-e QlBaseUrl="/" \
# Deployment port is not required, when using host mode, you can set the port after service startup, default 5700
-e QlPort="5700" \
--name qinglong \
--hostname qinglong \
docker.io/whyour/qinglong:latest
```
### Npm
It is recommended to use a pure system installation to avoid losing the original system data, you need to install node/npm/python3/pip3/pnpm yourself
```bash
# Debian/Ubuntu
curl -sL https://deb.nodesource.com/setup_20.x | sudo -E bash -
```
```bash
npm install -g node-pre-gyp pnpm@8.3.1
npm install -g @whyour/qinglong
qinglong
# Add the environment variables QL_DIR and QL_DATA_DIR when prompted
export QL_DIR=""
export QL_DATA_DIR=""
# Run again
qinglong
```
## Built-in commands ## Built-in commands
[View Documentation](https://qinglong.online/guide/user-guide/basic-explanation) - task
```bash
# Execute in sequence, if a random delay is set, it will be randomly delayed by a certain number of seconds
task <file_path>
# Execute in sequence, regardless of whether a random delay is set, all run immediately,
# and the foreground will output the day, while recorded in the log file
task <file_path> now
# Concurrent execution, regardless of whether a random delay is set, are run immediately,
# the foreground does not generate the day, directly recorded in the log file, and can be specified account execution
task <file_path> conc <env_name> <account_number>(Optional)
# Specify the account to execute and run immediately regardless of whether a random delay is set
task <file_path> desi <env_name> <account_number>
# Set task timeout
task -m <max_time> <file_path>
# Use -- to split, -- followed by a parameter that is passed to the script, as in the following example, the script receives the parameter -u whyour -p password
task <file_path> -- -u whyour -p password
```
- ql
```bash
# Update and restart Green Dragon
ql update
# Run custom scripts extra.sh
ql extra
# Adding a single script file
ql raw <file_url>
# Add a specific script for a single repository
ql repo <repo_url> <whitelist> <blacklist> <dependence> <branch>
# Delete old logs
ql rmlog <days>
# Start bot
ql bot
# Detecting the Green Dragon environment and repairing it
ql check
# Reset the number of login errors
ql resetlet
# Disable two-step login
ql resettfa
```
| **Parameter** | **Description** |
|---|---|
| file_url | Script address |
| repo_url | Repository address |
| whitelist | The whitelist when pulling the repository, i.e., the string contained in the path of the script to be pulled |
| blacklist | Blacklisting when pulling repositories, i.e. strings that are not included in the path of the script to be pulled |
| dependence | Pulling the dependencies needed for the repository will be copied directly from the repository to the repository directory under scripts, regardless of the blacklist |
| extensions | Pull the branch of the repository |
| branch | Number of days of logs to be kept |
| days | File path for task execution |
| file_path | The name of the environment variable that needs to be concurrent or specified at the time of task execution |
## Development ## Development
+119 -8
View File
@@ -20,9 +20,9 @@ Timed task management platform supporting Python3, JavaScript, Shell, Typescript
[docker-image-size-image]: https://img.shields.io/docker/image-size/whyour/qinglong?style=flat [docker-image-size-image]: https://img.shields.io/docker/image-size/whyour/qinglong?style=flat
[docker-image-size-url]: https://hub.docker.com/r/whyour/qinglong [docker-image-size-url]: https://hub.docker.com/r/whyour/qinglong
[Demo](http://demo.qinglong.online:4433/) / [Issues](https://github.com/whyour/qinglong/issues) / [Telegram Channel](https://t.me/jiao_long) / [Buy Me a Coffee](https://www.buymeacoffee.com/qinglong) [Demo](http://demo.dlww.cc:4433/) / [Issues](https://github.com/whyour/qinglong/issues) / [Telegram Channel](https://t.me/jiao_long) / [Buy Me a Coffee](https://www.buymeacoffee.com/qinglong)
[演示](http://demo.qinglong.online:4433/) / [反馈](https://github.com/whyour/qinglong/issues) / [Telegram 频道](https://t.me/jiao_long) / [打赏开发者](https://user-images.githubusercontent.com/22700758/244744295-29cd0cd1-c8bb-4ea1-adf6-29bd390ad4dd.jpg) [演示](http://demo.dlww.cc:4433/) / [反馈](https://github.com/whyour/qinglong/issues) / [Telegram 频道](https://t.me/jiao_long) / [打赏开发者](https://user-images.githubusercontent.com/22700758/244744295-29cd0cd1-c8bb-4ea1-adf6-29bd390ad4dd.jpg)
</div> </div>
![cover](https://user-images.githubusercontent.com/22700758/244847235-8dc1ca21-e03f-4606-9458-0541fab60413.png) ![cover](https://user-images.githubusercontent.com/22700758/244847235-8dc1ca21-e03f-4606-9458-0541fab60413.png)
@@ -43,8 +43,6 @@ Timed task management platform supporting Python3, JavaScript, Shell, Typescript
`latest` 镜像是基于 `alpine` 构建,`debian` 镜像是基于 `debian-slim` 构建。如果需要使用 `alpine` 不支持的依赖,建议使用 `debian` 镜像 `latest` 镜像是基于 `alpine` 构建,`debian` 镜像是基于 `debian-slim` 构建。如果需要使用 `alpine` 不支持的依赖,建议使用 `debian` 镜像
**⚠️ 重要提示**: 如果您需要以**非 root 用户**运行 Docker,请使用 `debian` 镜像。Alpine 的 `crond` 需要 root 权限。
```bash ```bash
docker pull whyour/qinglong:latest docker pull whyour/qinglong:latest
docker pull whyour/qinglong:debian docker pull whyour/qinglong:debian
@@ -60,15 +58,128 @@ npm i @whyour/qinglong
## 部署 ## 部署
[查看文档](https://qinglong.online/guide/getting-started/installation-guide) ### docker (推荐)
## 内置 API ```bash
# curl -sSL get.docker.com | sh
docker run -dit \
-v $PWD/ql/data:/ql/data \
# 冒号后面的 5700 为默认端口,如果设置了 QlPort, 需要跟 QlPort 保持一致
-p 5700:5700 \
# 部署路径非必须,比如 /test
-e QlBaseUrl="/" \
# 部署端口非必须,当使用 host 模式时,可以设置服务启动后的端口,默认 5700
-e QlPort="5700" \
--name qinglong \
--hostname qinglong \
--restart unless-stopped \
whyour/qinglong:latest
```
[查看文档](https://qinglong.online/guide/user-guide/built-in-api) ### docker-compose (推荐)
```bash
# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
mkdir qinglong
wget https://raw.githubusercontent.com/whyour/qinglong/master/docker/docker-compose.yml
# 启动
docker-compose up -d
# 停止
docker-compose down
```
### podman (推荐)
```bash
# https://podman.io/getting-started/installation
podman run -dit \
--network bridge \
-v $PWD/ql/data:/ql/data \
# 冒号后面的 5700 为默认端口,如果设置了 QlPort, 需要跟 QlPort 保持一致
-p 5700:5700 \
# 部署路径非必须,比如 /test
-e QlBaseUrl="/" \
# 部署端口非必须,当使用 host 模式时,可以设置服务启动后的端口,默认 5700
-e QlPort="5700" \
--name qinglong \
--hostname qinglong \
docker.io/whyour/qinglong:latest
```
### npm
建议使用纯净系统安装,避免系统原有数据丢失,需要自己安装 node/npm/python3/pip3/pnpm
```bash
# Debian/Ubuntu
curl -sL https://deb.nodesource.com/setup_20.x | sudo -E bash -
```
```bash
npm install -g node-pre-gyp pnpm@8.3.1
npm install -g @whyour/qinglong
qinglong
# 根据提示增加环境变量 QL_DIR 和 QL_DATA_DIR
export QL_DIR=""
export QL_DATA_DIR=""
# 再次执行
qinglong
```
## 内置命令 ## 内置命令
[查看文档](https://qinglong.online/guide/user-guide/basic-explanation) - task
```bash
# 依次执行,如果设置了随机延迟,将随机延迟一定秒数
task <file_path>
# 依次执行,无论是否设置了随机延迟,均立即运行,前台会输出日,同时记录在日志文件中
task <file_path> now
# 并发执行,无论是否设置了随机延迟,均立即运行,前台不产生日,直接记录在日志文件中,且可指定账号执行
task <file_path> conc <env_name> <account_number>(可选的)
# 指定账号执行,无论是否设置了随机延迟,均立即运行
task <file_path> desi <env_name> <account_number>
# 设置任务超时时间
task -m <max_time> <file_path>
# 使用 -- 分割,-- 后面的参数会传给脚本,下面的例子,脚本就可接收到参数 -u whyour -p password
task <file_path> -- -u whyour -p password
```
- ql
```bash
# 更新并重启青龙
ql update
# 运行自定义脚本extra.sh
ql extra
# 添加单个脚本文件
ql raw <file_url>
# 添加单个仓库的指定脚本
ql repo <repo_url> <whitelist> <blacklist> <dependence> <branch> <extensions>
# 删除旧日志
ql rmlog <days>
# 启动tg-bot
ql bot
# 检测青龙环境并修复
ql check
# 重置登录错误次数
ql resetlet
# 禁用两步登录
ql resettfa
```
| **参数** | **说明** |
|------------|---------------------------------------------------------------------------------------------|
| file_url | 脚本地址 |
| repo_url | 仓库地址 |
| whitelist | 拉取仓库时的白名单,即就是需要拉取的脚本的路径包含的字符串,多个竖线分割 |
| blacklist | 拉取仓库时的黑名单,即就是需要拉取的脚本的路径不包含的字符串,多个竖线分割 |
| dependence | 拉取仓库需要的依赖文件,会直接从仓库拷贝到scripts下的仓库目录,不受黑名单影响,多个竖线分割 |
| extensions | 拉取仓库的文件后缀,多个竖线分割 |
| branch | 拉取仓库的分支 |
| days | 需要保留的日志的天数 |
| file_path | 任务执行时的文件路径 |
## 开发 ## 开发
-5
View File
@@ -1,5 +0,0 @@
## Reporting a Vulnerability
To report a vulnerability, please open a private vulnerability report at <https://github.com/whyour/qinglong/security>.
While the discovery of new vulnerabilities is rare, we also recommend always using the latest versions of Qinglong to ensure your application remains as secure as possible.
+2 -2
View File
@@ -1,3 +1,4 @@
import { getFileContentByName, getLastModifyFilePath } from '../config/util';
import { Router, Request, Response, NextFunction } from 'express'; import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi'; import { Container } from 'typedi';
import { Logger } from 'winston'; import { Logger } from 'winston';
@@ -7,7 +8,6 @@ import { celebrate, Joi } from 'celebrate';
import { join } from 'path'; import { join } from 'path';
import { SAMPLE_FILES } from '../config/const'; import { SAMPLE_FILES } from '../config/const';
import ConfigService from '../services/config'; import ConfigService from '../services/config';
import { writeFileWithLock } from '../shared/utils';
const route = Router(); const route = Router();
export default (app: Router) => { export default (app: Router) => {
@@ -78,7 +78,7 @@ export default (app: Router) => {
if (name.startsWith('data/scripts/')) { if (name.startsWith('data/scripts/')) {
path = join(config.rootPath, name); path = join(config.rootPath, name);
} }
await writeFileWithLock(path, content); await fs.writeFile(path, content);
res.send({ code: 200, message: '保存成功' }); res.send({ code: 200, message: '保存成功' });
} catch (e) { } catch (e) {
return next(e); return next(e);
+31 -5
View File
@@ -4,8 +4,7 @@ import { Logger } from 'winston';
import CronService from '../services/cron'; import CronService from '../services/cron';
import CronViewService from '../services/cronView'; import CronViewService from '../services/cronView';
import { celebrate, Joi } from 'celebrate'; import { celebrate, Joi } from 'celebrate';
import { commonCronSchema } from '../validation/schedule'; import cron_parser from 'cron-parser';
const route = Router(); const route = Router();
export default (app: Router) => { export default (app: Router) => {
@@ -171,14 +170,27 @@ export default (app: Router) => {
route.post( route.post(
'/', '/',
celebrate({ celebrate({
body: Joi.object(commonCronSchema), body: Joi.object({
command: Joi.string().required(),
schedule: Joi.string().required(),
name: Joi.string().optional(),
labels: Joi.array().optional(),
sub_id: Joi.number().optional().allow(null),
extra_schedules: Joi.array().optional().allow(null),
task_before: Joi.string().optional().allow('').allow(null),
task_after: Joi.string().optional().allow('').allow(null),
}),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
if (cron_parser.parseExpression(req.body.schedule).hasNext()) {
const cronService = Container.get(CronService); const cronService = Container.get(CronService);
const data = await cronService.create(req.body); const data = await cronService.create(req.body);
return res.send({ code: 200, data }); return res.send({ code: 200, data });
} else {
return res.send({ code: 400, message: 'param schedule error' });
}
} catch (e) { } catch (e) {
return next(e); return next(e);
} }
@@ -319,16 +331,30 @@ export default (app: Router) => {
'/', '/',
celebrate({ celebrate({
body: Joi.object({ body: Joi.object({
...commonCronSchema, labels: Joi.array().optional().allow(null),
command: Joi.string().required(),
schedule: Joi.string().required(),
name: Joi.string().optional().allow(null),
sub_id: Joi.number().optional().allow(null),
extra_schedules: Joi.array().optional().allow(null),
task_before: Joi.string().optional().allow('').allow(null),
task_after: Joi.string().optional().allow('').allow(null),
id: Joi.number().required(), id: Joi.number().required(),
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
if (
!req.body.schedule ||
cron_parser.parseExpression(req.body.schedule).hasNext()
) {
const cronService = Container.get(CronService); const cronService = Container.get(CronService);
const data = await cronService.update(req.body); const data = await cronService.update(req.body);
return res.send({ code: 200, data }); return res.send({ code: 200, data });
} else {
return res.send({ code: 400, message: 'param schedule error' });
}
} catch (e) { } catch (e) {
return next(e); return next(e);
} }
@@ -392,7 +418,7 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const cronService = Container.get(CronService); const cronService = Container.get(CronService);
const data = await cronService.importCrontab(); const data = await cronService.import_crontab();
return res.send({ code: 200, data }); return res.send({ code: 200, data });
} catch (e) { } catch (e) {
return next(e); return next(e);
+8 -13
View File
@@ -8,17 +8,7 @@ const route = Router();
export default (app: Router) => { export default (app: Router) => {
app.use('/dependencies', route); app.use('/dependencies', route);
route.get( route.get('/', async (req: Request, res: Response, next: NextFunction) => {
'/',
celebrate({
query:
Joi.object({
searchValue: Joi.string().optional().allow(''),
type: Joi.string().optional().allow(''),
status: Joi.string().optional().allow(''),
}).unknown(true),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const dependenceService = Container.get(DependenceService); const dependenceService = Container.get(DependenceService);
@@ -28,8 +18,7 @@ export default (app: Router) => {
logger.error('🔥 error: %o', e); logger.error('🔥 error: %o', e);
return next(e); return next(e);
} }
}, });
);
route.post( route.post(
'/', '/',
@@ -43,6 +32,7 @@ export default (app: Router) => {
), ),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
const dependenceService = Container.get(DependenceService); const dependenceService = Container.get(DependenceService);
const data = await dependenceService.create(req.body); const data = await dependenceService.create(req.body);
@@ -64,6 +54,7 @@ export default (app: Router) => {
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
const dependenceService = Container.get(DependenceService); const dependenceService = Container.get(DependenceService);
const data = await dependenceService.update(req.body); const data = await dependenceService.update(req.body);
@@ -80,6 +71,7 @@ export default (app: Router) => {
body: Joi.array().items(Joi.number().required()), body: Joi.array().items(Joi.number().required()),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
const dependenceService = Container.get(DependenceService); const dependenceService = Container.get(DependenceService);
const data = await dependenceService.remove(req.body); const data = await dependenceService.remove(req.body);
@@ -96,6 +88,7 @@ export default (app: Router) => {
body: Joi.array().items(Joi.number().required()), body: Joi.array().items(Joi.number().required()),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
const dependenceService = Container.get(DependenceService); const dependenceService = Container.get(DependenceService);
const data = await dependenceService.remove(req.body, true); const data = await dependenceService.remove(req.body, true);
@@ -114,6 +107,7 @@ export default (app: Router) => {
}), }),
}), }),
async (req: Request<{ id: number }>, res: Response, next: NextFunction) => { async (req: Request<{ id: number }>, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
const dependenceService = Container.get(DependenceService); const dependenceService = Container.get(DependenceService);
const data = await dependenceService.getDb({ id: req.params.id }); const data = await dependenceService.getDb({ id: req.params.id });
@@ -130,6 +124,7 @@ export default (app: Router) => {
body: Joi.array().items(Joi.number().required()), body: Joi.array().items(Joi.number().required()),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
const dependenceService = Container.get(DependenceService); const dependenceService = Container.get(DependenceService);
const data = await dependenceService.reInstall(req.body); const data = await dependenceService.reInstall(req.body);
+7 -41
View File
@@ -1,12 +1,12 @@
import { Joi, celebrate } from 'celebrate'; import { Router, Request, Response, NextFunction } from 'express';
import { NextFunction, Request, Response, Router } from 'express';
import fs from 'fs';
import multer from 'multer';
import { Container } from 'typedi'; import { Container } from 'typedi';
import { Logger } from 'winston';
import config from '../config';
import { safeJSONParse } from '../config/util';
import EnvService from '../services/env'; import EnvService from '../services/env';
import { Logger } from 'winston';
import { celebrate, Joi } from 'celebrate';
import multer from 'multer';
import config from '../config';
import fs from 'fs';
import { safeJSONParse } from '../config/util';
const route = Router(); const route = Router();
const storage = multer.diskStorage({ const storage = multer.diskStorage({
@@ -196,40 +196,6 @@ export default (app: Router) => {
}, },
); );
route.put(
'/pin',
celebrate({
body: Joi.array().items(Joi.number().required()),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const envService = Container.get(EnvService);
const data = await envService.pin(req.body);
return res.send({ code: 200, data });
} catch (e) {
return next(e);
}
},
);
route.put(
'/unpin',
celebrate({
body: Joi.array().items(Joi.number().required()),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const envService = Container.get(EnvService);
const data = await envService.unPin(req.body);
return res.send({ code: 200, data });
} catch (e) {
return next(e);
}
},
);
route.post( route.post(
'/upload', '/upload',
upload.single('env'), upload.single('env'),
-27
View File
@@ -1,27 +0,0 @@
import { Router } from 'express';
import Logger from '../loaders/logger';
import { HealthService } from '../services/health';
import Container from 'typedi';
const route = Router();
export default (app: Router) => {
app.use('/', route);
route.get('/health', async (req, res) => {
try {
const healthService = Container.get(HealthService);
const health = await healthService.check();
res.status(200).send({
code: 200,
data: health,
});
} catch (err: any) {
Logger.error('Health check failed:', err);
res.status(500).send({
code: 500,
message: 'Health check failed',
error: err.message,
});
}
});
};
-4
View File
@@ -9,8 +9,6 @@ import open from './open';
import dependence from './dependence'; import dependence from './dependence';
import system from './system'; import system from './system';
import subscription from './subscription'; import subscription from './subscription';
import update from './update';
import health from './health';
export default () => { export default () => {
const app = Router(); const app = Router();
@@ -24,8 +22,6 @@ export default () => {
dependence(app); dependence(app);
system(app); system(app);
subscription(app); subscription(app);
update(app);
health(app);
return app; return app;
}; };
+26 -75
View File
@@ -1,15 +1,11 @@
import { celebrate, Joi } from 'celebrate'; import { Router, Request, Response, NextFunction } from 'express';
import { NextFunction, Request, Response, Router } from 'express';
import { Container } from 'typedi'; import { Container } from 'typedi';
import { Logger } from 'winston'; import { Logger } from 'winston';
import * as fs from 'fs';
import config from '../config'; import config from '../config';
import { import { getFileContentByName, readDirs, rmPath } from '../config/util';
getFileContentByName, import { join } from 'path';
readDirs, import { celebrate, Joi } from 'celebrate';
removeAnsi,
rmPath,
} from '../config/util';
import LogService from '../services/log';
const route = Router(); const route = Router();
const blacklist = ['.tmp']; const blacklist = ['.tmp'];
@@ -34,19 +30,16 @@ export default (app: Router) => {
'/detail', '/detail',
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
try { try {
const logService = Container.get(LogService); if (blacklist.includes(req.path)) {
const finalPath = logService.checkFilePath( return res.send({ code: 403, message: '暂无权限' });
(req.query.path as string) || '',
(req.query.file as string) || '',
);
if (!finalPath || blacklist.includes(req.query.path as string)) {
return res.send({
code: 403,
message: '暂无权限',
});
} }
const content = await getFileContentByName(finalPath); const filePath = join(
res.send({ code: 200, data: removeAnsi(content) }); config.logPath,
(req.query.path || '') as string,
req.query.file as string,
);
const content = await getFileContentByName(filePath);
res.send({ code: 200, data: content });
} catch (e) { } catch (e) {
return next(e); return next(e);
} }
@@ -57,18 +50,15 @@ export default (app: Router) => {
'/:file', '/:file',
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
try { try {
const logService = Container.get(LogService); if (blacklist.includes(req.path)) {
const finalPath = logService.checkFilePath( return res.send({ code: 403, message: '暂无权限' });
(req.query.path as string) || '',
(req.params.file as string) || '',
);
if (!finalPath || blacklist.includes(req.query.path as string)) {
return res.send({
code: 403,
message: '暂无权限',
});
} }
const content = await getFileContentByName(finalPath); const filePath = join(
config.logPath,
(req.query.path || '') as string,
req.params.file,
);
const content = await getFileContentByName(filePath);
res.send({ code: 200, data: content }); res.send({ code: 200, data: content });
} catch (e) { } catch (e) {
return next(e); return next(e);
@@ -87,56 +77,17 @@ export default (app: Router) => {
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
try { try {
let { filename, path } = req.body as { let { filename, path, type } = req.body as {
filename: string; filename: string;
path: string; path: string;
type: string;
}; };
const logService = Container.get(LogService); const filePath = join(config.logPath, path, filename);
const finalPath = logService.checkFilePath(path, filename); await rmPath(filePath);
if (!finalPath || blacklist.includes(path)) {
return res.send({
code: 403,
message: '暂无权限',
});
}
await rmPath(finalPath);
res.send({ code: 200 }); res.send({ code: 200 });
} catch (e) { } catch (e) {
return next(e); return next(e);
} }
}, },
); );
route.post(
'/download',
celebrate({
body: Joi.object({
filename: Joi.string().required(),
path: Joi.string().allow(''),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
let { filename, path } = req.body as {
filename: string;
path: string;
};
const logService = Container.get(LogService);
const filePath = logService.checkFilePath(path, filename);
if (!filePath) {
return res.send({
code: 403,
message: '暂无权限',
});
}
return res.download(filePath, filename, (err) => {
if (err) {
return next(err);
}
});
} catch (e) {
return next(e);
}
},
);
}; };
+40 -93
View File
@@ -1,4 +1,11 @@
import { fileExist, readDirs, readDir, rmPath, IFile } from '../config/util'; import {
fileExist,
getFileContentByName,
readDirs,
getLastModifyFilePath,
readDir,
rmPath,
} from '../config/util';
import { Router, Request, Response, NextFunction } from 'express'; import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi'; import { Container } from 'typedi';
import { Logger } from 'winston'; import { Logger } from 'winston';
@@ -8,7 +15,6 @@ import { celebrate, Joi } from 'celebrate';
import path, { join, parse } from 'path'; import path, { join, parse } from 'path';
import ScriptService from '../services/script'; import ScriptService from '../services/script';
import multer from 'multer'; import multer from 'multer';
import { writeFileWithLock } from '../shared/utils';
const route = Router(); const route = Router();
const storage = multer.diskStorage({ const storage = multer.diskStorage({
@@ -24,17 +30,10 @@ const upload = multer({ storage: storage });
export default (app: Router) => { export default (app: Router) => {
app.use('/scripts', route); app.use('/scripts', route);
route.get( route.get('/', async (req: Request, res: Response, next: NextFunction) => {
'/',
celebrate({
query: Joi.object({
path: Joi.string().optional().allow(''),
}).unknown(true),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
let result: IFile[] = []; let result = [];
const blacklist = [ const blacklist = [
'node_modules', 'node_modules',
'.git', '.git',
@@ -44,11 +43,11 @@ export default (app: Router) => {
'package-lock.json', 'package-lock.json',
]; ];
if (req.query.path) { if (req.query.path) {
result = await readDir( const targetPath = path.join(
req.query.path as string,
config.scriptPath, config.scriptPath,
blacklist, req.query.path as string,
); );
result = await readDir(targetPath, config.scriptPath, blacklist);
} else { } else {
result = await readDirs( result = await readDirs(
config.scriptPath, config.scriptPath,
@@ -75,17 +74,11 @@ export default (app: Router) => {
route.get( route.get(
'/detail', '/detail',
celebrate({
query: Joi.object({
path: Joi.string().optional().allow(''),
file: Joi.string().required(),
}).unknown(true),
}),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
try { try {
const scriptService = Container.get(ScriptService); const scriptService = Container.get(ScriptService);
const content = await scriptService.getFile( const content = await scriptService.getFile(
req.query?.path as string || '', req.query.path as string,
req.query.file as string, req.query.file as string,
); );
res.send({ code: 200, data: content }); res.send({ code: 200, data: content });
@@ -97,19 +90,11 @@ export default (app: Router) => {
route.get( route.get(
'/:file', '/:file',
celebrate({
params: Joi.object({
file: Joi.string().required(),
}),
query: Joi.object({
path: Joi.string().optional().allow(''),
}).unknown(true),
}),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
try { try {
const scriptService = Container.get(ScriptService); const scriptService = Container.get(ScriptService);
const content = await scriptService.getFile( const content = await scriptService.getFile(
req.query?.path as string || '', req.query.path as string,
req.params.file, req.params.file,
); );
res.send({ code: 200, data: content }); res.send({ code: 200, data: content });
@@ -122,17 +107,8 @@ export default (app: Router) => {
route.post( route.post(
'/', '/',
upload.single('file'), upload.single('file'),
celebrate({
body: Joi.object({
filename: Joi.string().required(),
path: Joi.string().optional().allow(''),
content: Joi.string().optional().allow(''),
originFilename: Joi.string().optional().allow(''),
directory: Joi.string().optional().allow(''),
file: Joi.string().optional().allow(''),
}).unknown(true),
}),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
let { filename, path, content, originFilename, directory } = let { filename, path, content, originFilename, directory } =
req.body as { req.body as {
@@ -154,8 +130,8 @@ export default (app: Router) => {
} }
if (config.writePathList.every((x) => !path.startsWith(x))) { if (config.writePathList.every((x) => !path.startsWith(x))) {
return res.send({ return res.send({
code: 403, code: 430,
message: '暂无权限', message: '文件路径禁止访问',
}); });
} }
@@ -176,7 +152,6 @@ export default (app: Router) => {
path, path,
`${originFilename.replace(/\//g, '')}`, `${originFilename.replace(/\//g, '')}`,
); );
await fs.mkdir(path, { recursive: true });
const filePath = join(path, `${filename.replace(/\//g, '')}`); const filePath = join(path, `${filename.replace(/\//g, '')}`);
const fileExists = await fileExist(filePath); const fileExists = await fileExist(filePath);
if (fileExists) { if (fileExists) {
@@ -188,7 +163,7 @@ export default (app: Router) => {
await rmPath(originFilePath); await rmPath(originFilePath);
} }
} }
await writeFileWithLock(filePath, content); await fs.writeFile(filePath, content);
return res.send({ code: 200 }); return res.send({ code: 200 });
} catch (e) { } catch (e) {
return next(e); return next(e);
@@ -206,21 +181,15 @@ export default (app: Router) => {
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
let { filename, content, path } = req.body as { let { filename, content, path } = req.body as {
filename: string; filename: string;
content: string; content: string;
path: string; path: string;
}; };
const scriptService = Container.get(ScriptService); const filePath = join(config.scriptPath, path, filename);
const filePath = scriptService.checkFilePath(path, filename); await fs.writeFile(filePath, content);
if (!filePath) {
return res.send({
code: 403,
message: '暂无权限',
});
}
await writeFileWithLock(filePath, content);
return res.send({ code: 200 }); return res.send({ code: 200 });
} catch (e) { } catch (e) {
return next(e); return next(e);
@@ -233,27 +202,19 @@ export default (app: Router) => {
celebrate({ celebrate({
body: Joi.object({ body: Joi.object({
filename: Joi.string().required(), filename: Joi.string().required(),
path: Joi.string().optional().allow(''), path: Joi.string().allow(''),
type: Joi.string().optional(), type: Joi.string().optional(),
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
let { filename, path } = req.body as { let { filename, path, type } = req.body as {
filename: string; filename: string;
path: string; path: string;
type: string;
}; };
if (!path) { const filePath = join(config.scriptPath, path, filename);
path = '';
}
const scriptService = Container.get(ScriptService);
const filePath = scriptService.checkFilePath(path, filename);
if (!filePath) {
return res.send({
code: 403,
message: '暂无权限',
});
}
await rmPath(filePath); await rmPath(filePath);
res.send({ code: 200 }); res.send({ code: 200 });
} catch (e) { } catch (e) {
@@ -267,30 +228,24 @@ export default (app: Router) => {
celebrate({ celebrate({
body: Joi.object({ body: Joi.object({
filename: Joi.string().required(), filename: Joi.string().required(),
path: Joi.string().optional().allow(''),
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
let { filename, path } = req.body as { let { filename } = req.body as {
filename: string; filename: string;
path: string;
}; };
if (!path) { const filePath = join(config.scriptPath, filename);
path = ''; // const stats = fs.statSync(filePath);
} // res.set({
const scriptService = Container.get(ScriptService); // 'Content-Type': 'application/octet-stream', //告诉浏览器这是一个二进制文件
const filePath = scriptService.checkFilePath(path, filename); // 'Content-Disposition': 'attachment; filename=' + filename, //告诉浏览器这是一个需要下载的文件
if (!filePath) { // 'Content-Length': stats.size //文件大小
return res.send({ // });
code: 403, // fs.createReadStream(filePath).pipe(res);
message: '暂无权限',
});
}
return res.download(filePath, filename, (err) => { return res.download(filePath, filename, (err) => {
if (err) {
return next(err); return next(err);
}
}); });
} catch (e) { } catch (e) {
return next(e); return next(e);
@@ -311,12 +266,9 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
let { filename, content, path } = req.body; let { filename, content, path } = req.body;
if (!path) {
path = '';
}
const { name, ext } = parse(filename); const { name, ext } = parse(filename);
const filePath = join(config.scriptPath, path, `${name}.swap${ext}`); const filePath = join(config.scriptPath, path, `${name}.swap${ext}`);
await writeFileWithLock(filePath, content || ''); await fs.writeFile(filePath, content || '', { encoding: 'utf8' });
const scriptService = Container.get(ScriptService); const scriptService = Container.get(ScriptService);
const result = await scriptService.runScript(filePath); const result = await scriptService.runScript(filePath);
@@ -339,9 +291,6 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
try { try {
let { filename, path, pid } = req.body; let { filename, path, pid } = req.body;
if (!path) {
path = '';
}
const { name, ext } = parse(filename); const { name, ext } = parse(filename);
const filePath = join(config.scriptPath, path, `${name}.swap${ext}`); const filePath = join(config.scriptPath, path, `${name}.swap${ext}`);
const logPath = join(config.logPath, path, `${name}.swap`); const logPath = join(config.logPath, path, `${name}.swap`);
@@ -369,14 +318,12 @@ export default (app: Router) => {
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
try { try {
let { filename, path, newFilename } = req.body as { let { filename, path, type, newFilename } = req.body as {
filename: string; filename: string;
path: string; path: string;
type: string;
newFilename: string; newFilename: string;
}; };
if (!path) {
path = '';
}
const filePath = join(config.scriptPath, path, filename); const filePath = join(config.scriptPath, path, filename);
const newPath = join(config.scriptPath, path, newFilename); const newPath = join(config.scriptPath, path, newFilename);
await fs.rename(filePath, newPath); await fs.rename(filePath, newPath);
+5 -6
View File
@@ -3,7 +3,7 @@ import { Container } from 'typedi';
import { Logger } from 'winston'; import { Logger } from 'winston';
import SubscriptionService from '../services/subscription'; import SubscriptionService from '../services/subscription';
import { celebrate, Joi } from 'celebrate'; import { celebrate, Joi } from 'celebrate';
import CronExpressionParser from 'cron-parser'; import cron_parser from 'cron-parser';
const route = Router(); const route = Router();
export default (app: Router) => { export default (app: Router) => {
@@ -15,7 +15,6 @@ export default (app: Router) => {
const subscriptionService = Container.get(SubscriptionService); const subscriptionService = Container.get(SubscriptionService);
const data = await subscriptionService.list( const data = await subscriptionService.list(
req.query.searchValue as string, req.query.searchValue as string,
req.query.ids as string,
); );
return res.send({ code: 200, data }); return res.send({ code: 200, data });
} catch (e) { } catch (e) {
@@ -60,7 +59,7 @@ export default (app: Router) => {
try { try {
if ( if (
!req.body.schedule || !req.body.schedule ||
CronExpressionParser.parse(req.body.schedule).hasNext() cron_parser.parseExpression(req.body.schedule).hasNext()
) { ) {
const subscriptionService = Container.get(SubscriptionService); const subscriptionService = Container.get(SubscriptionService);
const data = await subscriptionService.create(req.body); const data = await subscriptionService.create(req.body);
@@ -193,7 +192,7 @@ export default (app: Router) => {
if ( if (
!req.body.schedule || !req.body.schedule ||
typeof req.body.schedule === 'object' || typeof req.body.schedule === 'object' ||
CronExpressionParser.parse(req.body.schedule).hasNext() cron_parser.parseExpression(req.body.schedule).hasNext()
) { ) {
const subscriptionService = Container.get(SubscriptionService); const subscriptionService = Container.get(SubscriptionService);
const data = await subscriptionService.update(req.body); const data = await subscriptionService.update(req.body);
@@ -213,8 +212,8 @@ export default (app: Router) => {
body: Joi.array().items(Joi.number().required()), body: Joi.array().items(Joi.number().required()),
query: Joi.object({ query: Joi.object({
force: Joi.boolean().optional(), force: Joi.boolean().optional(),
t: Joi.number(), t: Joi.number()
}), })
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
+9 -122
View File
@@ -14,7 +14,6 @@ import {
} from '../config/util'; } from '../config/util';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import multer from 'multer'; import multer from 'multer';
import { logStreamManager } from '../shared/logStreamManager';
const route = Router(); const route = Router();
const storage = multer.diskStorage({ const storage = multer.diskStorage({
@@ -34,7 +33,7 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const userService = Container.get(UserService); const userService = Container.get(UserService);
const authInfo = await userService.getAuthInfo(); const authInfo = await userService.getUserInfo();
const { version, changeLog, changeLogLink, publishTime } = const { version, changeLog, changeLogLink, publishTime } =
await parseVersion(config.versionFile); await parseVersion(config.versionFile);
@@ -274,22 +273,19 @@ export default (app: Router) => {
{ {
onStart: async (cp, startTime) => { onStart: async (cp, startTime) => {
res.setHeader('QL-Task-Pid', `${cp.pid}`); res.setHeader('QL-Task-Pid', `${cp.pid}`);
res.setHeader('QL-Task-Log', `${logPath}`);
}, },
onEnd: async (cp, endTime, diff) => { onEnd: async (cp, endTime, diff) => {
// Close the stream after task completion
await logStreamManager.closeStream(await handleLogPath(logPath));
res.end(); res.end();
}, },
onError: async (message: string) => { onError: async (message: string) => {
res.write(message); res.write(`\n${message}`);
const absolutePath = await handleLogPath(logPath); const absolutePath = await handleLogPath(logPath);
await logStreamManager.write(absolutePath, message); await fs.appendFile(absolutePath, `\n${message}`);
}, },
onLog: async (message: string) => { onLog: async (message: string) => {
res.write(message); res.write(`\n${message}`);
const absolutePath = await handleLogPath(logPath); const absolutePath = await handleLogPath(logPath);
await logStreamManager.write(absolutePath, message); await fs.appendFile(absolutePath, `\n${message}`);
}, },
}, },
); );
@@ -320,15 +316,10 @@ export default (app: Router) => {
route.put( route.put(
'/data/export', '/data/export',
celebrate({
body: Joi.object({
type: Joi.array().items(Joi.string()).optional(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
try { try {
const systemService = Container.get(SystemService); const systemService = Container.get(SystemService);
await systemService.exportData(res, req.body.type); await systemService.exportData(res);
} catch (e) { } catch (e) {
return next(e); return next(e);
} }
@@ -349,116 +340,12 @@ export default (app: Router) => {
}, },
); );
route.get( route.get('/log', async (req: Request, res: Response, next: NextFunction) => {
'/log',
celebrate({
query: {
startTime: Joi.string().allow('').optional(),
endTime: Joi.string().allow('').optional(),
t: Joi.string().optional(),
},
}),
async (req: Request, res: Response, next: NextFunction) => {
try { try {
const systemService = Container.get(SystemService); const systemService = Container.get(SystemService);
await systemService.getSystemLog( await systemService.getSystemLog(res);
res,
req.query as {
startTime?: string;
endTime?: string;
},
);
} catch (e) { } catch (e) {
return next(e); return next(e);
} }
}, });
);
route.delete(
'/log',
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
await systemService.deleteSystemLog();
res.send({ code: 200 });
} catch (e) {
return next(e);
}
},
);
route.put(
'/auth/reset',
celebrate({
body: Joi.object({
retries: Joi.number().optional(),
twoFactorActivated: Joi.boolean().optional(),
password: Joi.string().optional(),
username: Joi.string().optional(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const userService = Container.get(UserService);
await userService.resetAuthInfo(req.body);
res.send({ code: 200, message: '更新成功' });
} catch (e) {
return next(e);
}
},
);
route.put(
'/config/timezone',
celebrate({
body: Joi.object({
timezone: Joi.string().allow('').allow(null),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.updateTimezone(req.body);
res.send(result);
} catch (e) {
return next(e);
}
},
);
route.put(
'/config/global-ssh-key',
celebrate({
body: Joi.object({
globalSshKey: Joi.string().allow('').allow(null),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.updateGlobalSshKey(req.body);
res.send(result);
} catch (e) {
return next(e);
}
},
);
route.put(
'/config/dependence-clean',
celebrate({
body: Joi.object({
type: Joi.string().allow(''),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.cleanDependence(req.body.type);
res.send(result);
} catch (e) {
return next(e);
}
},
);
}; };
-51
View File
@@ -1,51 +0,0 @@
import { NextFunction, Request, Response, Router } from 'express';
import Container from 'typedi';
import Logger from '../loaders/logger';
import SystemService from '../services/system';
const route = Router();
export default (app: Router) => {
app.use('/update', route);
route.put(
'/reload',
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.reloadSystem();
res.send(result);
} catch (e) {
Logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.put(
'/system',
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.reloadSystem('system');
res.send(result);
} catch (e) {
Logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.put(
'/data',
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.reloadSystem('data');
res.send(result);
} catch (e) {
Logger.error('🔥 error: %o', e);
return next(e);
}
},
);
};
+4 -5
View File
@@ -8,7 +8,6 @@ import path from 'path';
import { v4 as uuidV4 } from 'uuid'; import { v4 as uuidV4 } from 'uuid';
import rateLimit from 'express-rate-limit'; import rateLimit from 'express-rate-limit';
import config from '../config'; import config from '../config';
import { isDemoEnv, getToken } from '../config/util';
const route = Router(); const route = Router();
const storage = multer.diskStorage({ const storage = multer.diskStorage({
@@ -56,8 +55,7 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const userService = Container.get(UserService); const userService = Container.get(UserService);
const token = getToken(req); await userService.logout(req.platform);
await userService.logout(req.platform, token);
res.send({ code: 200 }); res.send({ code: 200 });
} catch (e) { } catch (e) {
return next(e); return next(e);
@@ -74,8 +72,9 @@ export default (app: Router) => {
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
if (isDemoEnv()) { if (process.env.DeployEnv === 'demo') {
return res.send({ code: 450, message: '未知错误' }); return res.send({ code: 450, message: '未知错误' });
} }
const userService = Container.get(UserService); const userService = Container.get(UserService);
@@ -91,7 +90,7 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const userService = Container.get(UserService); const userService = Container.get(UserService);
const authInfo = await userService.getAuthInfo(); const authInfo = await userService.getUserInfo();
res.send({ res.send({
code: 200, code: 200,
data: { data: {
+21 -295
View File
@@ -1,306 +1,32 @@
import 'reflect-metadata'; import 'reflect-metadata'; // We need this in order to use @Decorators
import cluster, { type Worker } from 'cluster';
import compression from 'compression';
import cors from 'cors';
import express from 'express';
import helmet from 'helmet';
import { Container } from 'typedi';
import config from './config'; import config from './config';
import express from 'express';
import Logger from './loaders/logger'; import Logger from './loaders/logger';
import { monitoringMiddleware } from './middlewares/monitoring';
import { type GrpcServerService } from './services/grpc';
import { type HttpServerService } from './services/http';
interface WorkerMetadata { async function startServer() {
id: number; const app = express();
pid: number;
serviceType: string;
startTime: Date;
}
class Application { await require('./loaders/db').default();
private app: express.Application;
private httpServerService?: HttpServerService;
private grpcServerService?: GrpcServerService;
private isShuttingDown = false;
private workerMetadataMap = new Map<number, WorkerMetadata>();
private httpWorker?: Worker;
constructor() { await require('./loaders/initFile').default();
this.app = express();
// 创建一个全局中间件,删除查询参数中的t
this.app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
if (req.query.t) {
delete req.query.t;
}
next();
});
}
async start() { await require('./loaders/sentry').default({ expressApp: app });
try {
if (cluster.isPrimary) {
await this.initializeDatabase();
}
if (cluster.isPrimary) {
this.startMasterProcess();
} else {
await this.startWorkerProcess();
}
} catch (error) {
Logger.error('Failed to start application:', error);
process.exit(1);
}
}
private startMasterProcess() { await require('./loaders/app').default({ expressApp: app });
// Fork gRPC worker first and wait for it to be ready
const grpcWorker = this.forkWorker('grpc');
// Wait for gRPC worker to signal it's ready before starting HTTP worker
this.waitForWorkerReady(grpcWorker, 30000)
.then(() => {
Logger.info('✌️ gRPC worker is ready, starting HTTP worker');
this.httpWorker = this.forkWorker('http');
})
.catch((error) => {
Logger.error('✌️ Failed to wait for gRPC worker:', error);
process.exit(1);
});
cluster.on('exit', (worker, code, signal) => {
const metadata = this.workerMetadataMap.get(worker.id);
if (metadata) {
if (!this.isShuttingDown) {
Logger.error(
`✌️ ${metadata.serviceType} worker ${worker.process.pid} died (${signal || code
}). Restarting...`,
);
// If gRPC worker died, restart it and wait for it to be ready
if (metadata.serviceType === 'grpc') {
const newGrpcWorker = this.forkWorker('grpc');
this.waitForWorkerReady(newGrpcWorker, 30000)
.then(() => {
Logger.info('✌️ gRPC worker restarted and ready');
// Re-register cron jobs by notifying the HTTP worker
if (this.httpWorker) {
try {
this.httpWorker.send('reregister-crons');
Logger.info('✌️ Sent reregister-crons message to HTTP worker');
} catch (error) {
Logger.error('✌️ Failed to send reregister-crons message:', error);
}
}
})
.catch((error) => {
Logger.error('✌️ Failed to restart gRPC worker:', error);
process.exit(1);
});
} else {
// For HTTP worker, just restart it
const newWorker = this.forkWorker(metadata.serviceType);
this.httpWorker = newWorker;
Logger.info(`✌️ Restarted ${metadata.serviceType} worker (PID: ${newWorker.process.pid})`);
}
}
this.workerMetadataMap.delete(worker.id);
}
});
this.setupMasterShutdown();
}
private waitForWorkerReady(worker: Worker, timeoutMs: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
const messageHandler = (msg: any) => {
if (msg === 'ready') {
worker.removeListener('message', messageHandler);
clearTimeout(timeoutId);
resolve();
}
};
worker.on('message', messageHandler);
// Timeout after specified milliseconds
const timeoutId = setTimeout(() => {
worker.removeListener('message', messageHandler);
reject(new Error(`Worker failed to start within ${timeoutMs / 1000} seconds`));
}, timeoutMs);
});
}
private forkWorker(serviceType: string): Worker {
const worker = cluster.fork({ SERVICE_TYPE: serviceType });
this.workerMetadataMap.set(worker.id, {
id: worker.id,
pid: worker.process.pid!,
serviceType,
startTime: new Date(),
});
return worker;
}
private async initializeDatabase() {
const dbLoader = await import('./loaders/db');
await dbLoader.default();
}
private setupMiddlewares() {
this.app.use(helmet({
contentSecurityPolicy: false,
}));
this.app.use(cors(config.cors));
this.app.use(compression());
this.app.use(monitoringMiddleware);
}
private setupMasterShutdown() {
const shutdown = async () => {
if (this.isShuttingDown) return;
this.isShuttingDown = true;
const workers = Object.values(cluster.workers || {});
const workerPromises: Promise<void>[] = [];
workers.forEach((worker) => {
if (worker) {
const exitPromise = new Promise<void>((resolve) => {
worker.once('exit', () => {
Logger.info(`✌️ Worker ${worker.process.pid} exited`);
resolve();
});
try {
worker.send('shutdown');
} catch (error) {
Logger.warn(
`✌️ Failed to send shutdown to worker ${worker.process.pid}:`,
error,
);
}
});
workerPromises.push(exitPromise);
}
});
try {
await Promise.race([
Promise.all(workerPromises),
new Promise<void>((resolve) => {
setTimeout(() => {
Logger.warn('✌️ Worker shutdown timeout reached');
resolve();
}, 10000);
}),
]);
process.exit(0);
} catch (error) {
Logger.error('✌️ Error during worker shutdown:', error);
process.exit(1);
}
};
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
}
private async startWorkerProcess() {
const serviceType = process.env.SERVICE_TYPE;
if (!serviceType || !['http', 'grpc'].includes(serviceType)) {
Logger.error('✌️ Invalid SERVICE_TYPE:', serviceType);
process.exit(1);
}
Logger.info(`✌️ ${serviceType} worker started (PID: ${process.pid})`);
try {
if (serviceType === 'http') {
await this.startHttpService();
} else {
await this.startGrpcService();
}
const server = app
.listen(config.port, '0.0.0.0', () => {
Logger.debug(`✌️ 后端服务启动成功!`);
console.debug(`✌️ 后端服务启动成功!`);
process.send?.('ready'); process.send?.('ready');
} catch (error) { })
Logger.error(`✌️ ${serviceType} worker failed:`, error); .on('error', (err) => {
process.exit(1); Logger.error(err);
} console.error(err);
}
private async startHttpService() {
this.setupMiddlewares();
const { HttpServerService } = await import('./services/http');
this.httpServerService = Container.get(HttpServerService);
const appLoader = await import('./loaders/app');
await appLoader.default({ app: this.app });
const server = await this.httpServerService.initialize(
this.app,
config.port,
);
const serverLoader = await import('./loaders/server');
await (serverLoader.default as any)({ server });
this.setupWorkerShutdown('http');
}
private async startGrpcService() {
const { GrpcServerService } = await import('./services/grpc');
this.grpcServerService = Container.get(GrpcServerService);
await this.grpcServerService.initialize();
this.setupWorkerShutdown('grpc');
}
private setupWorkerShutdown(serviceType: string) {
process.on('message', async (msg) => {
if (msg === 'shutdown') {
this.gracefulShutdown(serviceType);
} else if (msg === 'reregister-crons' && serviceType === 'http') {
// Re-register cron jobs when gRPC worker restarts
try {
Logger.info('✌️ Received reregister-crons message, re-registering cron jobs...');
const CronService = (await import('./services/cron')).default;
const cronService = Container.get(CronService);
await cronService.autosave_crontab();
Logger.info('✌️ Cron jobs re-registered successfully');
} catch (error) {
Logger.error('✌️ Failed to re-register cron jobs:', error);
}
}
});
const shutdown = () => this.gracefulShutdown(serviceType);
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
}
private async gracefulShutdown(serviceType: string) {
if (this.isShuttingDown) return;
this.isShuttingDown = true;
try {
if (serviceType === 'http') {
await this.httpServerService?.shutdown();
} else {
await this.grpcServerService?.shutdown();
}
process.exit(0);
} catch (error) {
Logger.error(`✌️ [${serviceType}] Error during shutdown:`, error);
process.exit(1);
}
}
}
const app = new Application();
app.start().catch((error) => {
Logger.error('🙅‍♀️ Application failed to start:', error);
process.exit(1); process.exit(1);
}); });
await require('./loaders/server').default({ server });
}
startServer();
-26
View File
@@ -23,29 +23,3 @@ export const SAMPLE_FILES = [
target: 'data/scripts/notify.py', target: 'data/scripts/notify.py',
}, },
]; ];
export const PYTHON_INSTALL_DIR = process.env.PYTHON_HOME;
export const NotificationModeStringMap = {
0: 'gotify',
1: 'goCqHttpBot',
2: 'serverChan',
3: 'pushDeer',
4: 'bark',
5: 'chat',
6: 'telegramBot',
7: 'dingtalkBot',
8: 'weWorkBot',
9: 'weWorkApp',
10: 'aibotk',
11: 'iGot',
12: 'pushPlus',
13: 'wePlusBot',
14: 'email',
15: 'pushMe',
16: 'feishu',
17: 'webhook',
18: 'chronocat',
19: 'ntfy',
20: 'wxPusherBot',
} as const;
-68
View File
@@ -1,68 +0,0 @@
import { request as undiciRequest, Dispatcher } from 'undici';
type RequestBaseOptions = {
dispatcher?: Dispatcher;
json?: Record<string, any>;
form?: string;
headers?: Record<string, string>;
} & Omit<Dispatcher.RequestOptions<null>, 'origin' | 'path' | 'method'>;
type RequestOptionsWithOptions = RequestBaseOptions &
Partial<Pick<Dispatcher.RequestOptions, 'method'>>;
type ResponseTypeMap = {
json: Record<string, any>;
text: string;
};
type ResponseTypeKey = keyof ResponseTypeMap;
async function request(
url: string,
options?: RequestOptionsWithOptions,
): Promise<Dispatcher.ResponseData<null>> {
const { json, form, body, headers = {}, ...rest } = options || {};
const finalHeaders = { ...headers } as Record<string, string>;
let finalBody = body;
if (json) {
finalHeaders['content-type'] = 'application/json';
finalBody = JSON.stringify(json);
} else if (form) {
finalBody = form;
delete finalHeaders['content-type'];
}
const res = await undiciRequest(url, {
method: 'POST',
headers: finalHeaders,
body: finalBody,
...rest,
});
return res;
}
async function post<T extends ResponseTypeKey = 'json'>(
url: string,
options?: RequestBaseOptions & { responseType?: T },
): Promise<ResponseTypeMap[T]> {
const resp = await request(url, { ...options, method: 'POST' });
const rawText = await resp.body.text();
if (options?.responseType === 'text') {
return rawText as ResponseTypeMap[T];
}
try {
return JSON.parse(rawText) as ResponseTypeMap[T];
} catch {
return rawText as ResponseTypeMap[T];
}
}
export const httpClient = {
post,
request,
};
+16 -80
View File
@@ -2,60 +2,12 @@ import dotenv from 'dotenv';
import path from 'path'; import path from 'path';
import { createRandomString } from './share'; import { createRandomString } from './share';
dotenv.config({
path: path.join(__dirname, '../../.env'),
});
interface Config {
port: number;
grpcPort: number;
nodeEnv: string;
isDevelopment: boolean;
isProduction: boolean;
jwt: {
secret: string;
expiresIn?: string;
};
cors: {
origin: string[];
methods: string[];
};
logs: {
level: string;
};
api: {
prefix: string;
};
}
const config: Config = {
port: parseInt(process.env.BACK_PORT || '5700', 10),
grpcPort: parseInt(process.env.GRPC_PORT || '5500', 10),
nodeEnv: process.env.NODE_ENV || 'development',
isDevelopment: process.env.NODE_ENV === 'development',
isProduction: process.env.NODE_ENV === 'production',
logs: {
level: process.env.LOG_LEVEL || 'silly',
},
api: {
prefix: '/api',
},
jwt: {
secret: process.env.JWT_SECRET || 'whyour-secret',
expiresIn: process.env.JWT_EXPIRES_IN,
},
cors: {
origin: process.env.CORS_ORIGIN
? process.env.CORS_ORIGIN.split(',')
: ['*'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
},
};
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; process.env.NODE_ENV = process.env.NODE_ENV || 'development';
if (!process.env.QL_DIR) { if (!process.env.QL_DIR) {
// 声明QL_DIR环境变量
let qlHomePath = path.join(__dirname, '../../'); let qlHomePath = path.join(__dirname, '../../');
// 生产环境
if (qlHomePath.endsWith('/static/')) { if (qlHomePath.endsWith('/static/')) {
qlHomePath = path.join(qlHomePath, '../'); qlHomePath = path.join(qlHomePath, '../');
} }
@@ -64,19 +16,6 @@ if (!process.env.QL_DIR) {
const lastVersionFile = `https://qn.whyour.cn/version.yaml`; const lastVersionFile = `https://qn.whyour.cn/version.yaml`;
// Get and normalize QlBaseUrl
let baseUrl = process.env.QlBaseUrl || '';
if (baseUrl) {
// Ensure it starts with /
if (!baseUrl.startsWith('/')) {
baseUrl = `/${baseUrl}`;
}
// Remove trailing slash for consistency in route definitions
if (baseUrl.endsWith('/')) {
baseUrl = baseUrl.slice(0, -1);
}
}
const rootPath = process.env.QL_DIR as string; const rootPath = process.env.QL_DIR as string;
const envFound = dotenv.config({ path: path.join(rootPath, '.env') }); const envFound = dotenv.config({ path: path.join(rootPath, '.env') });
@@ -87,25 +26,20 @@ if (process.env.QL_DATA_DIR) {
} }
const shellPath = path.join(rootPath, 'shell/'); const shellPath = path.join(rootPath, 'shell/');
const preloadPath = path.join(shellPath, 'preload/');
const tmpPath = path.join(rootPath, '.tmp/'); const tmpPath = path.join(rootPath, '.tmp/');
const samplePath = path.join(rootPath, 'sample/'); const samplePath = path.join(rootPath, 'sample/');
const configPath = path.join(dataPath, 'config/'); const configPath = path.join(dataPath, 'config/');
const scriptPath = path.join(dataPath, 'scripts/'); const scriptPath = path.join(dataPath, 'scripts/');
const repoPath = path.join(dataPath, 'repo/');
const bakPath = path.join(dataPath, 'bak/'); const bakPath = path.join(dataPath, 'bak/');
const logPath = path.join(dataPath, 'log/'); const logPath = path.join(dataPath, 'log/');
const dbPath = path.join(dataPath, 'db/'); const dbPath = path.join(dataPath, 'db/');
const uploadPath = path.join(dataPath, 'upload/'); const uploadPath = path.join(dataPath, 'upload/');
const sshdPath = path.join(dataPath, 'ssh.d/'); const sshdPath = path.join(dataPath, 'ssh.d/');
const systemLogPath = path.join(dataPath, 'syslog/'); const systemLogPath = path.join(dataPath, 'syslog/');
const dependenceCachePath = path.join(dataPath, 'dep_cache/');
const envFile = path.join(preloadPath, 'env.sh'); const envFile = path.join(configPath, 'env.sh');
const jsEnvFile = path.join(preloadPath, 'env.js'); const jsEnvFile = path.join(configPath, 'env.js');
const pyEnvFile = path.join(preloadPath, 'env.py'); const pyEnvFile = path.join(configPath, 'env.py');
const jsNotifyFile = path.join(preloadPath, '__ql_notify__.js');
const pyNotifyFile = path.join(preloadPath, '__ql_notify__.py');
const confFile = path.join(configPath, 'config.sh'); const confFile = path.join(configPath, 'config.sh');
const crontabFile = path.join(configPath, 'crontab.list'); const crontabFile = path.join(configPath, 'crontab.list');
const authConfigFile = path.join(configPath, 'auth.json'); const authConfigFile = path.join(configPath, 'auth.json');
@@ -127,9 +61,17 @@ if (envFound.error) {
} }
export default { export default {
...config, port: parseInt(process.env.BACK_PORT as string, 10),
jwt: config.jwt, cronPort: parseInt(process.env.CRON_PORT as string, 10),
baseUrl, publicPort: parseInt(process.env.PUBLIC_PORT as string, 10),
updatePort: parseInt(process.env.UPDATE_PORT as string, 10),
secret: process.env.SECRET || createRandomString(16, 32),
logs: {
level: process.env.LOG_LEVEL || 'silly',
},
api: {
prefix: '/api',
},
rootPath, rootPath,
tmpPath, tmpPath,
dataPath, dataPath,
@@ -149,13 +91,10 @@ export default {
envFile, envFile,
jsEnvFile, jsEnvFile,
pyEnvFile, pyEnvFile,
jsNotifyFile,
pyNotifyFile,
dbPath, dbPath,
uploadPath, uploadPath,
configPath, configPath,
scriptPath, scriptPath,
repoPath,
samplePath, samplePath,
blackFileList: [ blackFileList: [
'auth.json', 'auth.json',
@@ -172,7 +111,6 @@ export default {
bakPath, bakPath,
apiWhiteList: [ apiWhiteList: [
'/api/user/login', '/api/user/login',
'/api/health',
'/open/auth/token', '/open/auth/token',
'/api/user/two-factor/login', '/api/user/two-factor/login',
'/api/system', '/api/system',
@@ -189,6 +127,4 @@ export default {
sqliteFile, sqliteFile,
sshdPath, sshdPath,
systemLogPath, systemLogPath,
dependenceCachePath,
maxTokensPerPlatform: 10, // Maximum number of concurrent sessions per platform
}; };
+3 -3
View File
@@ -5,13 +5,13 @@ let pickedEnv: Record<string, string>;
function getPickedEnv() { function getPickedEnv() {
if (pickedEnv) return pickedEnv; if (pickedEnv) return pickedEnv;
const picked = pick(process.env, ['QlBaseUrl', 'DeployEnv', 'QL_DIR']); const picked = pick(process.env, ['QlBaseUrl', 'DeployEnv']);
if (picked.QlBaseUrl) { if (picked.QlBaseUrl) {
if (!picked.QlBaseUrl.startsWith('/')) { if (!picked.QlBaseUrl.startsWith('/')) {
picked.QlBaseUrl = `/${picked.QlBaseUrl}`; picked.QlBaseUrl = `/${picked.QlBaseUrl}`
} }
if (!picked.QlBaseUrl.endsWith('/')) { if (!picked.QlBaseUrl.endsWith('/')) {
picked.QlBaseUrl = `${picked.QlBaseUrl}/`; picked.QlBaseUrl = `${picked.QlBaseUrl}/`
} }
} }
pickedEnv = picked as Record<string, string>; pickedEnv = picked as Record<string, string>;
+106 -171
View File
@@ -1,15 +1,15 @@
import * as fs from 'fs/promises'; import * as fs from 'fs/promises';
import * as path from 'path'; import * as path from 'path';
import got from 'got';
import iconv from 'iconv-lite';
import { exec } from 'child_process'; import { exec } from 'child_process';
import psTreeFun from 'ps-tree'; import FormData from 'form-data';
import psTreeFun from 'pstree.remy';
import { promisify } from 'util'; import { promisify } from 'util';
import { load } from 'js-yaml'; import { load } from 'js-yaml';
import config from './index'; import config from './index';
import { PYTHON_INSTALL_DIR, TASK_COMMAND } from './const'; import { TASK_COMMAND } from './const';
import Logger from '../loaders/logger'; import Logger from '../loaders/logger';
import { writeFileWithLock } from '../shared/utils';
import { DependenceTypes } from '../data/dependence';
import { FormData } from 'undici';
export * from './share'; export * from './share';
@@ -21,10 +21,6 @@ export async function getFileContentByName(fileName: string) {
return ''; return '';
} }
export function removeAnsi(text: string) {
return text.replace(/\x1b\[\d+m/g, '');
}
export async function getLastModifyFilePath(dir: string) { export async function getLastModifyFilePath(dir: string) {
let filePath = ''; let filePath = '';
@@ -56,6 +52,78 @@ export function getToken(req: any) {
return ''; return '';
} }
export async function getNetIp(req: any) {
const ipArray = [
...new Set([
...(req.headers['x-real-ip'] || '').split(','),
...(req.headers['x-forwarded-for'] || '').split(','),
req.ip,
...req.ips,
req.socket.remoteAddress,
]),
].filter(Boolean);
let ip = ipArray[0];
if (ipArray.length > 1) {
for (let i = 0; i < ipArray.length; i++) {
const ipNumArray = ipArray[i].split('.');
const tmp = ipNumArray[0] + '.' + ipNumArray[1];
if (
tmp === '192.168' ||
(ipNumArray[0] === '172' &&
ipNumArray[1] >= 16 &&
ipNumArray[1] <= 32) ||
tmp === '10.7' ||
tmp === '127.0'
) {
continue;
}
ip = ipArray[i];
break;
}
}
ip = ip.substr(ip.lastIndexOf(':') + 1, ip.length);
if (ip.includes('127.0') || ip.includes('192.168') || ip.includes('10.7')) {
ip = '';
}
if (!ip) {
return { address: `获取失败`, ip };
}
try {
const csdnApi = got
.get(`https://searchplugin.csdn.net/api/v1/ip/get?ip=${ip}`, {
timeout: 10000,
retry: 0,
})
.text();
const pconlineApi = got
.get(`https://whois.pconline.com.cn/ipJson.jsp?ip=${ip}&json=true`, {
timeout: 10000,
retry: 0,
})
.buffer();
const [csdnBody, pconlineBody] = await await Promise.all<any>([
csdnApi,
pconlineApi,
]);
const csdnRes = JSON.parse(csdnBody);
const pconlineRes = JSON.parse(iconv.decode(pconlineBody, 'GBK'));
let address = '';
if (csdnBody && csdnRes.code == 200) {
address = csdnRes.data.address;
} else if (pconlineRes && pconlineRes.addr) {
address = pconlineRes.addr;
}
return { address, ip };
} catch (error) {
return { address: `获取失败`, ip };
}
}
export function getPlatform(userAgent: string): 'mobile' | 'desktop' { export function getPlatform(userAgent: string): 'mobile' | 'desktop' {
const ua = userAgent.toLowerCase(); const ua = userAgent.toLowerCase();
const testUa = (regexp: RegExp) => regexp.test(ua); const testUa = (regexp: RegExp) => regexp.test(ua);
@@ -77,19 +145,12 @@ export function getPlatform(userAgent: string): 'mobile' | 'desktop' {
system = 'android'; // android系统 system = 'android'; // android系统
} else if (testUa(/ios|iphone|ipad|ipod|iwatch/g)) { } else if (testUa(/ios|iphone|ipad|ipod|iwatch/g)) {
system = 'ios'; // ios系统 system = 'ios'; // ios系统
} else if (testUa(/openharmony/g)) {
system = 'openharmony'; // openharmony系统
} }
let platform = 'desktop'; let platform = 'desktop';
if (system === 'windows' || system === 'macos' || system === 'linux') { if (system === 'windows' || system === 'macos' || system === 'linux') {
platform = 'desktop'; platform = 'desktop';
} else if ( } else if (system === 'android' || system === 'ios' || testUa(/mobile/g)) {
system === 'android' ||
system === 'ios' ||
system === 'openharmony' ||
testUa(/mobile/g)
) {
platform = 'mobile'; platform = 'mobile';
} }
@@ -107,7 +168,7 @@ export async function fileExist(file: any) {
export async function createFile(file: string, data: string = '') { export async function createFile(file: string, data: string = '') {
await fs.mkdir(path.dirname(file), { recursive: true }); await fs.mkdir(path.dirname(file), { recursive: true });
await writeFileWithLock(file, data); await fs.writeFile(file, data);
} }
export async function handleLogPath( export async function handleLogPath(
@@ -164,19 +225,19 @@ enum FileType {
'file', 'file',
} }
export interface IFile { interface IFile {
title: string; title: string;
key: string; key: string;
type: 'directory' | 'file'; type: 'directory' | 'file';
parent: string; parent: string;
createTime: number; mtime: number;
size?: number; size?: number;
children?: IFile[]; children?: IFile[];
} }
export function dirSort(a: IFile, b: IFile): number { export function dirSort(a: IFile, b: IFile): number {
if (a.type === 'file' && b.type === 'file') { if (a.type === 'file' && b.type === 'file') {
return b.createTime - a.createTime; return b.mtime - a.mtime;
} else if (a.type === 'directory' && b.type === 'directory') { } else if (a.type === 'directory' && b.type === 'directory') {
return a.title.localeCompare(b.title); return a.title.localeCompare(b.title);
} else { } else {
@@ -210,7 +271,7 @@ export async function readDirs(
key, key,
type: 'directory', type: 'directory',
parent: relativePath, parent: relativePath,
createTime: stats.birthtime.getTime(), mtime: stats.mtime.getTime(),
children: children.sort(sort), children: children.sort(sort),
}); });
} else { } else {
@@ -220,7 +281,7 @@ export async function readDirs(
key, key,
parent: relativePath, parent: relativePath,
size: stats.size, size: stats.size,
createTime: stats.birthtime.getTime(), mtime: stats.mtime.getTime(),
}); });
} }
} }
@@ -232,51 +293,23 @@ export async function readDir(
dir: string, dir: string,
baseDir: string = '', baseDir: string = '',
blacklist: string[] = [], blacklist: string[] = [],
): Promise<IFile[]> { ) {
const absoluteDir = path.join(baseDir, dir); const relativePath = path.relative(baseDir, dir);
const relativePath = path.relative(baseDir, absoluteDir); const files = await fs.readdir(dir);
const result: any = files
try { .filter((x) => !blacklist.includes(x))
const files = await fs.readdir(absoluteDir); .map(async (file: string) => {
const result: IFile[] = []; const subPath = path.join(dir, file);
for (const file of files) {
const subPath = path.join(absoluteDir, file);
const stats = await fs.lstat(subPath); const stats = await fs.lstat(subPath);
const key = path.join(relativePath, file); const key = path.join(relativePath, file);
return {
if (blacklist.includes(file) || stats.isSymbolicLink()) {
continue;
}
if (stats.isDirectory()) {
result.push({
title: file, title: file,
type: 'directory', type: stats.isDirectory() ? 'directory' : 'file',
key, key,
parent: relativePath, parent: relativePath,
createTime: stats.birthtime.getTime(), };
children: [],
}); });
} else {
result.push({
title: file,
type: 'file',
key,
parent: relativePath,
size: stats.size,
createTime: stats.birthtime.getTime(),
});
}
}
return result; return result;
} catch (error: any) {
if (error.code === 'ENOENT') {
return [];
}
throw error;
}
} }
export async function promiseExec(command: string): Promise<string> { export async function promiseExec(command: string): Promise<string> {
@@ -307,9 +340,9 @@ export function parseHeaders(headers: string) {
if (!headers) return {}; if (!headers) return {};
const parsed: any = {}; const parsed: any = {};
let key: string; let key;
let val: string; let val;
let i: number; let i;
headers && headers &&
headers.split('\n').forEach(function parser(line) { headers.split('\n').forEach(function parser(line) {
@@ -388,11 +421,11 @@ export function parseBody(
export function psTree(pid: number): Promise<number[]> { export function psTree(pid: number): Promise<number[]> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
psTreeFun(pid, (err: any, children) => { psTreeFun(pid, (err: any, pids: number[]) => {
if (err) { if (err) {
reject(err); reject(err);
} }
resolve(children.map((x) => Number(x.PID)).filter((x) => !isNaN(x))); resolve(pids.filter((x) => !isNaN(x)));
}); });
}); });
} }
@@ -417,27 +450,6 @@ export async function getPid(cmd: string) {
return pid ? Number(pid) : undefined; return pid ? Number(pid) : undefined;
} }
export async function getAllPids(cmd: string): Promise<number[]> {
const taskCommand = `ps -eo pid,command | grep "${cmd}" | grep -v grep | awk '{print $1}'`;
const pidsStr = await promiseExec(taskCommand);
if (!pidsStr) return [];
return pidsStr
.split('\n')
.map((p) => Number(p.trim()))
.filter((p) => !isNaN(p) && p > 0);
}
export async function killAllTasks(cmd: string): Promise<void> {
const pids = await getAllPids(cmd);
for (const pid of pids) {
try {
await killTask(pid);
} catch (error) {
// Ignore errors if process already terminated
}
}
}
interface IVersion { interface IVersion {
version: string; version: string;
changeLogLink: string; changeLogLink: string;
@@ -449,7 +461,7 @@ export async function parseVersion(path: string): Promise<IVersion> {
return load(await fs.readFile(path, 'utf8')) as IVersion; return load(await fs.readFile(path, 'utf8')) as IVersion;
} }
export function parseContentVersion(content: string): IVersion { export async function parseContentVersion(content: string): Promise<IVersion> {
return load(content) as IVersion; return load(content) as IVersion;
} }
@@ -457,18 +469,13 @@ export async function getUniqPath(
command: string, command: string,
id: string, id: string,
): Promise<string> { ): Promise<string> {
let suffix = '';
if (/^\d+$/.test(id)) { if (/^\d+$/.test(id)) {
suffix = `_${id}`; id = `_${id}`;
} } else {
id = '';
let items = command.split(/ +/);
const maxTimeCommandIndex = items.findIndex((x) => x === '-m');
if (maxTimeCommandIndex !== -1) {
items = items.slice(maxTimeCommandIndex + 2);
} }
const items = command.split(/ +/);
let str = items[0]; let str = items[0];
if (items[0] === TASK_COMMAND) { if (items[0] === TASK_COMMAND) {
str = items[1]; str = items[1];
@@ -492,7 +499,7 @@ export async function getUniqPath(
str = `${tempStr}_${str.slice(slashIndex + 1)}`; str = `${tempStr}_${str.slice(slashIndex + 1)}`;
} }
return `${str}${suffix}`; return `${str}${id}`;
} }
export function safeJSONParse(value?: string) { export function safeJSONParse(value?: string) {
@@ -503,7 +510,7 @@ export function safeJSONParse(value?: string) {
try { try {
return JSON.parse(value); return JSON.parse(value);
} catch (error) { } catch (error) {
Logger.error('[safeJSONParse失败]', error); Logger.error('[JSON.parse失败]', error);
return {}; return {};
} }
} }
@@ -518,75 +525,3 @@ export async function rmPath(path: string) {
Logger.error('[rmPath失败]', error); Logger.error('[rmPath失败]', error);
} }
} }
export async function setSystemTimezone(timezone: string): Promise<boolean> {
try {
if (!(await fileExist(`/usr/share/zoneinfo/${timezone}`))) {
throw new Error('Invalid timezone');
}
await promiseExec(`ln -sf /usr/share/zoneinfo/${timezone} /etc/localtime`);
await promiseExec(`echo "${timezone}" > /etc/timezone`);
return true;
} catch (error) {
Logger.error('[setSystemTimezone失败]', error);
return false;
}
}
export function getGetCommand(type: DependenceTypes, name: string): string {
const baseCommands = {
[DependenceTypes.nodejs]: `pnpm ls -g | grep "${name}" | head -1`,
[DependenceTypes.python3]: `
python3 -c "exec('''
name='${name}'
try:
from importlib.metadata import version
print(version(name))
except:
import importlib.util as u
import importlib.metadata as m
spec=u.find_spec(name)
print(name if spec else '')
''')"`,
[DependenceTypes.linux]: `apk info -es ${name}`,
};
return baseCommands[type];
}
export function getInstallCommand(type: DependenceTypes, name: string): string {
const baseCommands = {
[DependenceTypes.nodejs]: 'pnpm add -g',
[DependenceTypes.python3]:
'pip3 install --disable-pip-version-check --root-user-action=ignore',
[DependenceTypes.linux]: 'apk add --no-check-certificate',
};
let command = baseCommands[type];
if (type === DependenceTypes.python3 && PYTHON_INSTALL_DIR) {
command = `${command} --prefix=${PYTHON_INSTALL_DIR}`;
}
return `${command} ${name.trim()}`;
}
export function getUninstallCommand(
type: DependenceTypes,
name: string,
): string {
const baseCommands = {
[DependenceTypes.nodejs]: 'pnpm remove -g',
[DependenceTypes.python3]:
'pip3 uninstall --disable-pip-version-check --root-user-action=ignore -y',
[DependenceTypes.linux]: 'apk del',
};
return `${baseCommands[type]} ${name.trim()}`;
}
export function isDemoEnv() {
return process.env.DeployEnv === 'demo';
}
-6
View File
@@ -21,8 +21,6 @@ export class Crontab {
extra_schedules?: Array<{ schedule: string }>; extra_schedules?: Array<{ schedule: string }>;
task_before?: string; task_before?: string;
task_after?: string; task_after?: string;
log_name?: string;
allow_multiple_instances?: 1 | 0;
constructor(options: Crontab) { constructor(options: Crontab) {
this.name = options.name; this.name = options.name;
@@ -47,8 +45,6 @@ export class Crontab {
this.extra_schedules = options.extra_schedules; this.extra_schedules = options.extra_schedules;
this.task_before = options.task_before; this.task_before = options.task_before;
this.task_after = options.task_after; this.task_after = options.task_after;
this.log_name = options.log_name;
this.allow_multiple_instances = options.allow_multiple_instances || 0;
} }
} }
@@ -88,6 +84,4 @@ export const CrontabModel = sequelize.define<CronInstance>('Crontab', {
extra_schedules: DataTypes.JSON, extra_schedules: DataTypes.JSON,
task_before: DataTypes.STRING, task_before: DataTypes.STRING,
task_after: DataTypes.STRING, task_after: DataTypes.STRING,
log_name: DataTypes.STRING,
allow_multiple_instances: DataTypes.NUMBER,
}); });
+18
View File
@@ -41,12 +41,30 @@ export enum DependenceTypes {
'linux', 'linux',
} }
export enum InstallDependenceCommandTypes {
'pnpm add -g',
'pip3 install --disable-pip-version-check --root-user-action=ignore',
'apk add --no-check-certificate',
}
export enum GetDependenceCommandTypes {
'pnpm ls -g ',
'pip3 show --disable-pip-version-check',
'apk info -es',
}
export enum versionDependenceCommandTypes { export enum versionDependenceCommandTypes {
'@', '@',
'==', '==',
'=', '=',
} }
export enum unInstallDependenceCommandTypes {
'pnpm remove -g',
'pip3 uninstall --disable-pip-version-check --root-user-action=ignore -y',
'apk del',
}
export interface DependenceInstance export interface DependenceInstance
extends Model<Dependence, Dependence>, extends Model<Dependence, Dependence>,
Dependence {} Dependence {}
+2 -5
View File
@@ -1,5 +1,5 @@
import { DataTypes, Model } from 'sequelize';
import { sequelize } from '.'; import { sequelize } from '.';
import { DataTypes, Model, ModelDefined } from 'sequelize';
export class Env { export class Env {
value?: string; value?: string;
@@ -9,7 +9,6 @@ export class Env {
position?: number; position?: number;
name?: string; name?: string;
remarks?: string; remarks?: string;
isPinned?: 1 | 0;
constructor(options: Env) { constructor(options: Env) {
this.value = options.value; this.value = options.value;
@@ -21,8 +20,7 @@ export class Env {
this.timestamp = new Date().toString(); this.timestamp = new Date().toString();
this.position = options.position; this.position = options.position;
this.name = options.name; this.name = options.name;
this.remarks = options.remarks || ''; this.remarks = options.remarks;
this.isPinned = options.isPinned || 0;
} }
} }
@@ -44,5 +42,4 @@ export const EnvModel = sequelize.define<EnvInstance>('Env', {
position: DataTypes.NUMBER, position: DataTypes.NUMBER,
name: { type: DataTypes.STRING, unique: 'compositeIndex' }, name: { type: DataTypes.STRING, unique: 'compositeIndex' },
remarks: DataTypes.STRING, remarks: DataTypes.STRING,
isPinned: DataTypes.NUMBER,
}); });
+9 -32
View File
@@ -1,3 +1,5 @@
import { IncomingHttpHeaders } from 'http';
export enum NotificationMode { export enum NotificationMode {
'gotify' = 'gotify', 'gotify' = 'gotify',
'goCqHttpBot' = 'goCqHttpBot', 'goCqHttpBot' = 'goCqHttpBot',
@@ -18,8 +20,6 @@ export enum NotificationMode {
'feishu' = 'feishu', 'feishu' = 'feishu',
'webhook' = 'webhook', 'webhook' = 'webhook',
'chronocat' = 'Chronocat', 'chronocat' = 'Chronocat',
'ntfy' = 'ntfy',
'wxPusherBot' = 'wxPusherBot',
} }
abstract class NotificationBaseInfo { abstract class NotificationBaseInfo {
@@ -47,8 +47,9 @@ export class PushDeerNotification extends NotificationBaseInfo {
public pushDeerUrl = ''; public pushDeerUrl = '';
} }
export class synologyChatNotification extends NotificationBaseInfo { export class ChatNotification extends NotificationBaseInfo {
public synologyChatUrl = ''; public chatUrl = '';
public chatToken = '';
} }
export class BarkNotification extends NotificationBaseInfo { export class BarkNotification extends NotificationBaseInfo {
@@ -58,7 +59,7 @@ export class BarkNotification extends NotificationBaseInfo {
public barkGroup = 'qinglong'; public barkGroup = 'qinglong';
public barkLevel = 'active'; public barkLevel = 'active';
public barkUrl = ''; public barkUrl = '';
public barkArchive = ''; public barkArchive=""
} }
export class TelegramBotNotification extends NotificationBaseInfo { export class TelegramBotNotification extends NotificationBaseInfo {
@@ -98,11 +99,6 @@ export class IGotNotification extends NotificationBaseInfo {
export class PushPlusNotification extends NotificationBaseInfo { export class PushPlusNotification extends NotificationBaseInfo {
public pushPlusToken = ''; public pushPlusToken = '';
public pushPlusUser = ''; public pushPlusUser = '';
public pushPlusTemplate = '';
public pushplusChannel = '';
public pushplusWebhook = '';
public pushplusCallbackUrl = '';
public pushplusTo = '';
} }
export class WePlusBotNotification extends NotificationBaseInfo { export class WePlusBotNotification extends NotificationBaseInfo {
@@ -115,7 +111,6 @@ export class EmailNotification extends NotificationBaseInfo {
public emailService: string = ''; public emailService: string = '';
public emailUser: string = ''; public emailUser: string = '';
public emailPass: string = ''; public emailPass: string = '';
public emailTo: string = '';
} }
export class PushMeNotification extends NotificationBaseInfo { export class PushMeNotification extends NotificationBaseInfo {
@@ -142,23 +137,6 @@ export class WebhookNotification extends NotificationBaseInfo {
export class LarkNotification extends NotificationBaseInfo { export class LarkNotification extends NotificationBaseInfo {
public larkKey = ''; public larkKey = '';
public larkSecret = '';
}
export class NtfyNotification extends NotificationBaseInfo {
public ntfyUrl = '';
public ntfyTopic = '';
public ntfyPriority = '';
public ntfyToken = '';
public ntfyUsername = '';
public ntfyPassword = '';
public ntfyActions = '';
}
export class WxPusherBotNotification extends NotificationBaseInfo {
public wxPusherBotAppToken = '';
public wxPusherBotTopicIds = '';
public wxPusherBotUids = '';
} }
export interface NotificationInfo export interface NotificationInfo
@@ -166,7 +144,7 @@ export interface NotificationInfo
GotifyNotification, GotifyNotification,
ServerChanNotification, ServerChanNotification,
PushDeerNotification, PushDeerNotification,
synologyChatNotification, ChatNotification,
BarkNotification, BarkNotification,
TelegramBotNotification, TelegramBotNotification,
DingtalkBotNotification, DingtalkBotNotification,
@@ -180,6 +158,5 @@ export interface NotificationInfo
PushMeNotification, PushMeNotification,
WebhookNotification, WebhookNotification,
ChronocatNotification, ChronocatNotification,
LarkNotification, LarkNotification {}
NtfyNotification,
WxPusherBotNotification {}
+1 -1
View File
@@ -24,7 +24,7 @@ export interface AppToken {
expiration: number; expiration: number;
} }
export type AppScope = 'envs' | 'crons' | 'configs' | 'scripts' | 'logs' | 'system'; export type AppScope = 'envs' | 'crons' | 'configs' | 'scripts' | 'logs';
export interface AppInstance extends Model<App, App>, App {} export interface AppInstance extends Model<App, App>, App {}
export const AppModel = sequelize.define<AppInstance>('App', { export const AppModel = sequelize.define<AppInstance>('App', {
+2 -37
View File
@@ -27,7 +27,6 @@ export enum AuthDataType {
'notification' = 'notification', 'notification' = 'notification',
'removeLogFrequency' = 'removeLogFrequency', 'removeLogFrequency' = 'removeLogFrequency',
'systemConfig' = 'systemConfig', 'systemConfig' = 'systemConfig',
'authConfig' = 'authConfig',
} }
export interface SystemConfigInfo { export interface SystemConfigInfo {
@@ -37,8 +36,6 @@ export interface SystemConfigInfo {
nodeMirror?: string; nodeMirror?: string;
pythonMirror?: string; pythonMirror?: string;
linuxMirror?: string; linuxMirror?: string;
timezone?: string;
globalSshKey?: string;
} }
export interface LoginLogInfo { export interface LoginLogInfo {
@@ -49,43 +46,11 @@ export interface LoginLogInfo {
status?: LoginStatus; status?: LoginStatus;
} }
export interface TokenInfo {
value: string;
timestamp: number;
ip: string;
address: string;
platform: string;
/**
* Token expiration time in seconds since Unix epoch.
* If undefined, the token uses JWT's built-in expiration.
*/
expiration?: number;
}
export interface AuthInfo {
username: string;
password: string;
retries: number;
lastlogon: number;
lastip: string;
lastaddr: string;
platform: string;
isTwoFactorChecking: boolean;
token: string;
tokens: Record<string, string | TokenInfo[]>;
twoFactorActivated: boolean;
twoFactorSecret: string;
avatar: string;
}
export type SystemModelInfo = SystemConfigInfo & export type SystemModelInfo = SystemConfigInfo &
Partial<NotificationInfo> & Partial<NotificationInfo> &
LoginLogInfo & LoginLogInfo;
Partial<AuthInfo>;
export interface SystemInstance export interface SystemInstance extends Model<SystemInfo, SystemInfo>, SystemInfo { }
extends Model<SystemInfo, SystemInfo>,
SystemInfo {}
export const SystemModel = sequelize.define<SystemInstance>('Auth', { export const SystemModel = sequelize.define<SystemInstance>('Auth', {
ip: DataTypes.STRING, ip: DataTypes.STRING,
type: DataTypes.STRING, type: DataTypes.STRING,
+7
View File
@@ -0,0 +1,7 @@
declare namespace Express {
interface Request {
platform: 'desktop' | 'mobile';
}
}
declare module 'pstree.remy';
-13
View File
@@ -1,13 +0,0 @@
export enum ScheduleType {
BOOT = '@boot',
ONCE = '@once',
}
export type ScheduleValidator = (schedule?: string) => boolean;
export type CronSchedulerPayload = {
name: string;
id: string;
schedule: string;
command: string;
extra_schedules: Array<{ schedule: string }>;
};
+10 -9
View File
@@ -5,24 +5,25 @@ import initData from './initData';
import { Application } from 'express'; import { Application } from 'express';
import linkDeps from './deps'; import linkDeps from './deps';
import initTask from './initTask'; import initTask from './initTask';
import initFile from './initFile';
export default async ({ app }: { app: Application }) => { export default async ({ expressApp }: { expressApp: Application }) => {
depInjectorLoader(); depInjectorLoader();
Logger.info('✌️ Dependency loaded'); Logger.info('✌️ Dependency loaded');
console.log('✌️ Dependency loaded');
await linkDeps();
Logger.info('✌️ Link deps loaded');
initFile();
Logger.info('✌️ Init file loaded');
await initData(); await initData();
Logger.info('✌️ Init data loaded'); Logger.info('✌️ Init data loaded');
console.log('✌️ Init data loaded');
await linkDeps();
Logger.info('✌️ Link deps loaded');
console.log('✌️ Link deps loaded');
initTask(); initTask();
Logger.info('✌️ Init task loaded'); Logger.info('✌️ Init task loaded');
console.log('✌️ Init task loaded');
expressLoader({ app }); expressLoader({ app: expressApp });
Logger.info('✌️ Express loaded'); Logger.info('✌️ Express loaded');
console.log('✌️ Express loaded');
}; };
-8
View File
@@ -1,8 +0,0 @@
import Container from 'typedi';
import CronService from '../services/cron';
export default async () => {
const cronService = Container.get(CronService);
await cronService.bootTask();
};
+110 -29
View File
@@ -1,11 +1,15 @@
import Logger from './logger'; import Logger from './logger';
import path from 'path';
import DataStore from 'nedb';
import { EnvModel } from '../data/env'; import { EnvModel } from '../data/env';
import { CrontabModel } from '../data/cron'; import { CrontabModel } from '../data/cron';
import { DependenceModel } from '../data/dependence'; import { DependenceModel } from '../data/dependence';
import { AppModel } from '../data/open'; import { AppModel } from '../data/open';
import { SystemModel } from '../data/system'; import { SystemModel } from '../data/system';
import { fileExist } from '../config/util';
import { SubscriptionModel } from '../data/subscription'; import { SubscriptionModel } from '../data/subscription';
import { CrontabViewModel } from '../data/cronView'; import { CrontabViewModel } from '../data/cronView';
import config from '../config';
import { sequelize } from '../data'; import { sequelize } from '../data';
export default async () => { export default async () => {
@@ -19,41 +23,118 @@ export default async () => {
await CrontabViewModel.sync(); await CrontabViewModel.sync();
// 初始化新增字段 // 初始化新增字段
const migrations = [
{
table: 'CrontabViews',
column: 'filterRelation',
type: 'VARCHAR(255)',
},
{ table: 'Subscriptions', column: 'proxy', type: 'VARCHAR(255)' },
{ table: 'CrontabViews', column: 'type', type: 'NUMBER' },
{ table: 'Subscriptions', column: 'autoAddCron', type: 'NUMBER' },
{ table: 'Subscriptions', column: 'autoDelCron', type: 'NUMBER' },
{ table: 'Crontabs', column: 'sub_id', type: 'NUMBER' },
{ table: 'Crontabs', column: 'extra_schedules', type: 'JSON' },
{ table: 'Crontabs', column: 'task_before', type: 'TEXT' },
{ table: 'Crontabs', column: 'task_after', type: 'TEXT' },
{ table: 'Crontabs', column: 'log_name', type: 'VARCHAR(255)' },
{
table: 'Crontabs',
column: 'allow_multiple_instances',
type: 'NUMBER',
},
{ table: 'Envs', column: 'isPinned', type: 'NUMBER' },
];
for (const migration of migrations) {
try { try {
await sequelize.query( await sequelize.query(
`alter table ${migration.table} add column ${migration.column} ${migration.type}`, 'alter table CrontabViews add column filterRelation VARCHAR(255)',
); );
} catch (error) { } catch (error) {}
// Column already exists or other error, continue try {
} await sequelize.query(
'alter table Subscriptions add column proxy VARCHAR(255)',
);
} catch (error) {}
try {
await sequelize.query('alter table CrontabViews add column type NUMBER');
} catch (error) {}
try {
await sequelize.query(
'alter table Subscriptions add column autoAddCron NUMBER',
);
} catch (error) {}
try {
await sequelize.query(
'alter table Subscriptions add column autoDelCron NUMBER',
);
} catch (error) {}
try {
await sequelize.query('alter table Crontabs add column sub_id NUMBER');
} catch (error) { }
try {
await sequelize.query('alter table Crontabs add column extra_schedules JSON');
} catch (error) { }
try {
await sequelize.query('alter table Crontabs add column task_before TEXT');
} catch (error) { }
try {
await sequelize.query('alter table Crontabs add column task_after TEXT');
} catch (error) { }
// 2.10-2.11 升级
const cronDbFile = path.join(config.rootPath, 'db/crontab.db');
const envDbFile = path.join(config.rootPath, 'db/env.db');
const appDbFile = path.join(config.rootPath, 'db/app.db');
const authDbFile = path.join(config.rootPath, 'db/auth.db');
const dependenceDbFile = path.join(config.rootPath, 'db/dependence.db');
const crondbExist = await fileExist(cronDbFile);
const dependenceDbExist = await fileExist(dependenceDbFile);
const envDbExist = await fileExist(envDbFile);
const appDbExist = await fileExist(appDbFile);
const authDbExist = await fileExist(authDbFile);
const cronCount = await CrontabModel.count();
const dependenceCount = await DependenceModel.count();
const envCount = await EnvModel.count();
const appCount = await AppModel.count();
const authCount = await SystemModel.count();
if (crondbExist && cronCount === 0) {
const cronDb = new DataStore({
filename: cronDbFile,
autoload: true,
});
cronDb.persistence.compactDatafile();
cronDb.find({}).exec(async (err, docs) => {
await CrontabModel.bulkCreate(docs, { ignoreDuplicates: true });
});
} }
if (dependenceDbExist && dependenceCount === 0) {
const dependenceDb = new DataStore({
filename: dependenceDbFile,
autoload: true,
});
dependenceDb.persistence.compactDatafile();
dependenceDb.find({}).exec(async (err, docs) => {
await DependenceModel.bulkCreate(docs, { ignoreDuplicates: true });
});
}
if (envDbExist && envCount === 0) {
const envDb = new DataStore({
filename: envDbFile,
autoload: true,
});
envDb.persistence.compactDatafile();
envDb.find({}).exec(async (err, docs) => {
await EnvModel.bulkCreate(docs, { ignoreDuplicates: true });
});
}
if (appDbExist && appCount === 0) {
const appDb = new DataStore({
filename: appDbFile,
autoload: true,
});
appDb.persistence.compactDatafile();
appDb.find({}).exec(async (err, docs) => {
await AppModel.bulkCreate(docs, { ignoreDuplicates: true });
});
}
if (authDbExist && authCount === 0) {
const authDb = new DataStore({
filename: authDbFile,
autoload: true,
});
authDb.persistence.compactDatafile();
authDb.find({}).exec(async (err, docs) => {
await SystemModel.bulkCreate(docs, { ignoreDuplicates: true });
});
}
console.log('✌️ DB loaded');
Logger.info('✌️ DB loaded'); Logger.info('✌️ DB loaded');
} catch (error) { } catch (error) {
Logger.error('✌️ DB load failed', error); console.error('✌️ DB load failed');
Logger.error(error);
} }
}; };
+21 -30
View File
@@ -1,9 +1,8 @@
import path from 'path'; import path from 'path';
import fs from 'fs/promises'; import fs from 'fs/promises';
import os from 'os';
import chokidar from 'chokidar'; import chokidar from 'chokidar';
import config from '../config/index'; import config from '../config/index';
import Logger from './logger'; import { fileExist, promiseExec, rmPath } from '../config/util';
async function linkToNodeModule(src: string, dst?: string) { async function linkToNodeModule(src: string, dst?: string) {
const target = path.join(config.rootPath, 'node_modules', dst || src); const target = path.join(config.rootPath, 'node_modules', dst || src);
@@ -18,45 +17,37 @@ async function linkToNodeModule(src: string, dst?: string) {
} }
async function linkCommand() { async function linkCommand() {
const homeDir = os.homedir(); const commandPath = await promiseExec('which node');
let userBinDir = path.join(homeDir, 'bin'); const commandDir = path.dirname(commandPath);
const oldLinkShell = [
try {
await fs.mkdir(userBinDir, { recursive: true });
await linkCommandToDir(userBinDir);
} catch (error) {
Logger.error('Linking command failed:', error);
}
}
async function linkCommandToDir(commandDir: string) {
const linkShell = [
{ {
src: 'update.sh', src: 'update.sh',
dest: 'ql', dest: 'ql',
tmp: 'ql_tmp', tmp: 'ql_tmp',
}, },
{
src: 'task.sh',
dest: 'task',
tmp: 'task_tmp',
},
]; ];
// const newLinkShell = [
// {
// src: 'task.mjs',
// dest: 'task',
// tmp: 'task_tmp',
// },
// ];
for (const link of linkShell) { for (const link of oldLinkShell) {
const source = path.join(config.rootPath, 'shell', link.src); const source = path.join(config.rootPath, 'shell', link.src);
const target = path.join(commandDir, link.dest); const target = path.join(commandDir, link.dest);
const tmpTarget = path.join(commandDir, link.tmp); const tmpTarget = path.join(commandDir, link.tmp);
try {
const stats = await fs.lstat(tmpTarget);
if (stats) {
await fs.unlink(tmpTarget);
}
} catch (error) { }
await fs.symlink(source, tmpTarget); await fs.symlink(source, tmpTarget);
await fs.rename(tmpTarget, target); await fs.rename(tmpTarget, target);
} }
// for (const link of newLinkShell) {
// const source = path.join(config.rootPath, 'cli', link.src);
// const target = path.join(commandDir, link.dest);
// const tmpTarget = path.join(commandDir, link.tmp);
// await fs.symlink(source, tmpTarget);
// await fs.rename(tmpTarget, target);
// }
} }
export default async (src: string = 'deps') => { export default async (src: string = 'deps') => {
@@ -70,6 +61,6 @@ export default async (src: string = 'deps') => {
}); });
watcher watcher
.on('add', () => linkToNodeModule(src)) .on('add', (path) => linkToNodeModule(src))
.on('change', () => linkToNodeModule(src)); .on('change', (path) => linkToNodeModule(src));
}; };
+38 -43
View File
@@ -3,40 +3,45 @@ import bodyParser from 'body-parser';
import cors from 'cors'; import cors from 'cors';
import routes from '../api'; import routes from '../api';
import config from '../config'; import config from '../config';
import { UnauthorizedError, expressjwt } from 'express-jwt'; import jwt, { UnauthorizedError } from 'express-jwt';
import { getPlatform, getToken } from '../config/util'; import fs from 'fs/promises';
import { getPlatform, getToken, safeJSONParse } from '../config/util';
import Container from 'typedi';
import OpenService from '../services/open';
import rewrite from 'express-urlrewrite'; import rewrite from 'express-urlrewrite';
import UserService from '../services/user';
import * as Sentry from '@sentry/node';
import { EnvModel } from '../data/env';
import { errors } from 'celebrate'; import { errors } from 'celebrate';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { serveEnv } from '../config/serverEnv'; import { serveEnv } from '../config/serverEnv';
import { IKeyvStore, shareStore } from '../shared/store'; import Logger from './logger';
import { isValidToken } from '../shared/auth';
import path from 'path';
export default ({ app }: { app: Application }) => { export default ({ app }: { app: Application }) => {
app.set('trust proxy', 'loopback'); app.set('trust proxy', 'loopback');
app.use(cors()); app.use(cors());
// Rewrite URLs to strip baseUrl prefix if configured
// This allows the rest of the app to work without baseUrl awareness
if (config.baseUrl) {
app.use(rewrite(`${config.baseUrl}/*`, '/$1'));
}
app.get(`${config.api.prefix}/env.js`, serveEnv); app.get(`${config.api.prefix}/env.js`, serveEnv);
app.use(`${config.api.prefix}/static`, express.static(config.uploadPath)); app.use(`${config.api.prefix}/static`, express.static(config.uploadPath));
app.use(
'/api/public',
createProxyMiddleware({
target: `http://0.0.0.0:${config.publicPort}/api`,
changeOrigin: true,
pathRewrite: { '/api/public': '' },
logProvider: () => Logger,
}),
);
app.use(bodyParser.json({ limit: '50mb' })); app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })); app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
const frontendPath = path.join(config.rootPath, 'static/dist');
app.use(express.static(frontendPath));
app.use( app.use(
expressjwt({ jwt({
secret: config.jwt.secret, secret: config.secret,
algorithms: ['HS384'], algorithms: ['HS384'],
}).unless({ }).unless({
path: [...config.apiWhiteList, /^\/(?!api\/).*/], path: [...config.apiWhiteList, /^\/open\//],
}), }),
); );
@@ -50,17 +55,11 @@ export default ({ app }: { app: Application }) => {
return next(); return next();
}); });
app.use(async (req: Request, res, next) => { app.use(async (req, res, next) => {
if (!['/open/', '/api/'].some((x) => req.path.startsWith(x))) {
return next();
}
const headerToken = getToken(req); const headerToken = getToken(req);
if (req.path.startsWith('/open/')) { if (req.path.startsWith('/open/')) {
const apps = await shareStore.getApps(); const openService = Container.get(OpenService);
const doc = apps?.filter((x) => const doc = await openService.findTokenByValue(headerToken);
x.tokens?.find((y) => y.value === headerToken),
)?.[0];
if (doc && doc.tokens && doc.tokens.length > 0) { if (doc && doc.tokens && doc.tokens.length > 0) {
const currentToken = doc.tokens.find((x) => x.value === headerToken); const currentToken = doc.tokens.find((x) => x.value === headerToken);
const keyMatch = req.path.match(/\/open\/([a-z]+)\/*/); const keyMatch = req.path.match(/\/open\/([a-z]+)\/*/);
@@ -84,10 +83,13 @@ export default ({ app }: { app: Application }) => {
return next(); return next();
} }
const authInfo = await shareStore.getAuthInfo(); const data = await fs.readFile(config.authConfigFile, 'utf8');
if (isValidToken(authInfo, headerToken, req.platform)) { if (data && headerToken) {
const { token = '', tokens = {} } = safeJSONParse(data);
if (headerToken === token || tokens[req.platform] === headerToken) {
return next(); return next();
} }
}
const errorCode = headerToken ? 'invalid_token' : 'credentials_required'; const errorCode = headerToken ? 'invalid_token' : 'credentials_required';
const errorMessage = headerToken const errorMessage = headerToken
@@ -101,8 +103,8 @@ export default ({ app }: { app: Application }) => {
if (!['/api/user/init', '/api/user/notification/init'].includes(req.path)) { if (!['/api/user/init', '/api/user/notification/init'].includes(req.path)) {
return next(); return next();
} }
const authInfo = const userService = Container.get(UserService);
(await shareStore.getAuthInfo()) || ({} as IKeyvStore['authInfo']); const authInfo = await userService.getUserInfo();
let isInitialized = true; let isInitialized = true;
if ( if (
@@ -123,19 +125,10 @@ export default ({ app }: { app: Application }) => {
app.use(rewrite('/open/*', '/api/$1')); app.use(rewrite('/open/*', '/api/$1'));
app.use(config.api.prefix, routes()); app.use(config.api.prefix, routes());
app.get('*', (req, res, next) => { app.use((req, res, next) => {
// Don't serve index.html for API routes
if (req.path.startsWith('/api/')) {
return next();
}
const indexPath = path.join(frontendPath, 'index.html');
res.sendFile(indexPath, (err) => {
if (err) {
const err: any = new Error('Not Found'); const err: any = new Error('Not Found');
err['status'] = 404; err['status'] = 404;
next(err); next(err);
}
});
}); });
app.use(errors()); app.use(errors());
@@ -169,8 +162,8 @@ export default ({ app }: { app: Application }) => {
.status(500) .status(500)
.send({ .send({
code: 400, code: 400,
message: `${err.message}`, message: `${err.name} ${err.message}`,
errors: err.errors, validation: err.errors,
}) })
.end(); .end();
} }
@@ -185,6 +178,8 @@ export default ({ app }: { app: Application }) => {
res: Response, res: Response,
next: NextFunction, next: NextFunction,
) => { ) => {
Sentry.captureException(err);
res.status(err.status || 500); res.status(err.status || 500);
res.json({ res.json({
code: err.status || 500, code: err.status || 500,
+10 -68
View File
@@ -11,90 +11,39 @@ import { CrontabViewModel, CronViewType } from '../data/cronView';
import { initPosition } from '../data/env'; import { initPosition } from '../data/env';
import { AuthDataType, SystemModel } from '../data/system'; import { AuthDataType, SystemModel } from '../data/system';
import SystemService from '../services/system'; import SystemService from '../services/system';
import UserService from '../services/user';
import { writeFile, readFile } from 'fs/promises';
import { createRandomString, fileExist, safeJSONParse } from '../config/util';
import OpenService from '../services/open';
import { shareStore } from '../shared/store';
import Logger from './logger';
import { AppModel } from '../data/open';
export default async () => { export default async () => {
const cronService = Container.get(CronService); const cronService = Container.get(CronService);
const envService = Container.get(EnvService); const envService = Container.get(EnvService);
const dependenceService = Container.get(DependenceService); const dependenceService = Container.get(DependenceService);
const systemService = Container.get(SystemService); const systemService = Container.get(SystemService);
const userService = Container.get(UserService);
const openService = Container.get(OpenService);
// 初始化增加系统配置 // 初始化增加系统配置
let systemApp = ( await SystemModel.upsert({ type: AuthDataType.systemConfig });
await AppModel.findOne({ await SystemModel.upsert({ type: AuthDataType.notification });
where: { name: 'system' },
})
)?.get({ plain: true });
if (!systemApp) {
systemApp = await AppModel.create({
name: 'system',
scopes: ['crons', 'system'],
client_id: createRandomString(12, 12),
client_secret: createRandomString(24, 24),
});
}
const [systemConfig] = await SystemModel.findOrCreate({
where: { type: AuthDataType.systemConfig },
});
await SystemModel.findOrCreate({
where: { type: AuthDataType.notification },
});
const [authConfig] = await SystemModel.findOrCreate({
where: { type: AuthDataType.authConfig },
});
if (!authConfig?.info) {
let authInfo = {
username: 'admin',
password: 'admin',
};
try {
const authFileExist = await fileExist(config.authConfigFile);
if (authFileExist) {
const content = await readFile(config.authConfigFile, 'utf8');
authInfo = safeJSONParse(content);
}
} catch (error) {
Logger.warn('Failed to read auth config file, using default credentials');
}
await SystemModel.upsert({
id: authConfig?.id,
info: authInfo,
type: AuthDataType.authConfig,
});
}
const installDependencies = async () => { const installDependencies = () => {
const docs = await DependenceModel.findAll({ // 初始化时安装所有处于安装中,安装成功,安装失败的依赖
DependenceModel.findAll({
where: {}, where: {},
order: [ order: [
['type', 'DESC'], ['type', 'DESC'],
['createdAt', 'DESC'], ['createdAt', 'DESC'],
], ],
raw: true, raw: true,
}); }).then(async (docs) => {
await DependenceModel.update( await DependenceModel.update(
{ status: DependenceStatus.queued, log: [] }, { status: DependenceStatus.queued, log: [] },
{ where: { id: docs.map((x) => x.id!) } }, { where: { id: docs.map((x) => x.id!) } },
); );
setTimeout(() => {
setTimeout(async () => { dependenceService.installDependenceOneByOne(docs);
await dependenceService.installDependenceOneByOne(docs);
const bootAfterLoader = await import('./bootAfter');
bootAfterLoader.default();
}, 5000); }, 5000);
});
}; };
// 初始化更新 linux/python/nodejs 镜像源配置 // 初始化更新 linux/python/nodejs 镜像源配置
const systemConfig = await systemService.getSystemConfig();
if (systemConfig.info?.pythonMirror) { if (systemConfig.info?.pythonMirror) {
systemService.updatePythonMirror({ systemService.updatePythonMirror({
pythonMirror: systemConfig.info?.pythonMirror, pythonMirror: systemConfig.info?.pythonMirror,
@@ -213,11 +162,4 @@ export default async () => {
// 初始化保存一次ck和定时任务数据 // 初始化保存一次ck和定时任务数据
await cronService.autosave_crontab(); await cronService.autosave_crontab();
await envService.set_envs(); await envService.set_envs();
const authInfo = await userService.getAuthInfo();
const apps = await openService.findApps();
await shareStore.updateAuthInfo(authInfo);
if (apps?.length) {
await shareStore.updateApps(apps);
}
}; };
+79 -83
View File
@@ -3,7 +3,6 @@ import path from 'path';
import os from 'os'; import os from 'os';
import Logger from './logger'; import Logger from './logger';
import { fileExist } from '../config/util'; import { fileExist } from '../config/util';
import { writeFileWithLock } from '../shared/utils';
const rootPath = process.env.QL_DIR as string; const rootPath = process.env.QL_DIR as string;
let dataPath = path.join(rootPath, 'data/'); let dataPath = path.join(rootPath, 'data/');
@@ -12,7 +11,6 @@ if (process.env.QL_DATA_DIR) {
dataPath = process.env.QL_DATA_DIR.replace(/\/$/g, ''); dataPath = process.env.QL_DATA_DIR.replace(/\/$/g, '');
} }
const preloadPath = path.join(rootPath, 'shell/preload/');
const configPath = path.join(dataPath, 'config/'); const configPath = path.join(dataPath, 'config/');
const scriptPath = path.join(dataPath, 'scripts/'); const scriptPath = path.join(dataPath, 'scripts/');
const logPath = path.join(dataPath, 'log/'); const logPath = path.join(dataPath, 'log/');
@@ -21,107 +19,105 @@ const bakPath = path.join(dataPath, 'bak/');
const samplePath = path.join(rootPath, 'sample/'); const samplePath = path.join(rootPath, 'sample/');
const tmpPath = path.join(logPath, '.tmp/'); const tmpPath = path.join(logPath, '.tmp/');
const confFile = path.join(configPath, 'config.sh'); const confFile = path.join(configPath, 'config.sh');
const authConfigFile = path.join(configPath, 'auth.json');
const sampleConfigFile = path.join(samplePath, 'config.sample.sh'); const sampleConfigFile = path.join(samplePath, 'config.sample.sh');
const sampleAuthFile = path.join(samplePath, 'auth.sample.json');
const sampleTaskShellFile = path.join(samplePath, 'task.sample.sh'); const sampleTaskShellFile = path.join(samplePath, 'task.sample.sh');
const sampleNotifyJsFile = path.join(samplePath, 'notify.js'); const sampleNotifyJsFile = path.join(samplePath, 'notify.js');
const sampleNotifyPyFile = path.join(samplePath, 'notify.py'); const sampleNotifyPyFile = path.join(samplePath, 'notify.py');
const scriptNotifyJsFile = path.join(scriptPath, 'sendNotify.js'); const scriptNotifyJsFile = path.join(scriptPath, 'sendNotify.js');
const scriptNotifyPyFile = path.join(scriptPath, 'notify.py'); const scriptNotifyPyFile = path.join(scriptPath, 'notify.py');
const jsNotifyFile = path.join(preloadPath, '__ql_notify__.js');
const pyNotifyFile = path.join(preloadPath, '__ql_notify__.py');
const TaskBeforeFile = path.join(configPath, 'task_before.sh'); const TaskBeforeFile = path.join(configPath, 'task_before.sh');
const TaskBeforeJsFile = path.join(configPath, 'task_before.js');
const TaskBeforePyFile = path.join(configPath, 'task_before.py');
const TaskAfterFile = path.join(configPath, 'task_after.sh'); const TaskAfterFile = path.join(configPath, 'task_after.sh');
const homedir = os.homedir(); const homedir = os.homedir();
const sshPath = path.resolve(homedir, '.ssh'); const sshPath = path.resolve(homedir, '.ssh');
const sshdPath = path.join(dataPath, 'ssh.d'); const sshdPath = path.join(dataPath, 'ssh.d');
const systemLogPath = path.join(dataPath, 'syslog'); const systemLogPath = path.join(dataPath, 'syslog');
const directories = [
configPath,
scriptPath,
preloadPath,
logPath,
tmpPath,
uploadPath,
sshPath,
bakPath,
sshdPath,
systemLogPath,
];
const files = [
{
target: confFile,
source: sampleConfigFile,
checkExistence: true,
},
{
target: jsNotifyFile,
source: sampleNotifyJsFile,
checkExistence: false,
},
{
target: pyNotifyFile,
source: sampleNotifyPyFile,
checkExistence: false,
},
{
target: scriptNotifyJsFile,
source: sampleNotifyJsFile,
checkExistence: true,
},
{
target: scriptNotifyPyFile,
source: sampleNotifyPyFile,
checkExistence: true,
},
{
target: TaskBeforeFile,
source: sampleTaskShellFile,
checkExistence: true,
},
{
target: TaskBeforeJsFile,
content:
'// The JavaScript code that executes before the JavaScript task execution will execute.',
checkExistence: true,
},
{
target: TaskBeforePyFile,
content:
'# The Python code that executes before the Python task execution will execute.',
checkExistence: true,
},
{
target: TaskAfterFile,
source: sampleTaskShellFile,
checkExistence: true,
},
];
export default async () => { export default async () => {
for (const dirPath of directories) { const authFileExist = await fileExist(authConfigFile);
if (!(await fileExist(dirPath))) { const confFileExist = await fileExist(confFile);
await fs.mkdir(dirPath); const scriptDirExist = await fileExist(scriptPath);
} const logDirExist = await fileExist(logPath);
const configDirExist = await fileExist(configPath);
const uploadDirExist = await fileExist(uploadPath);
const sshDirExist = await fileExist(sshPath);
const bakDirExist = await fileExist(bakPath);
const sshdDirExist = await fileExist(sshdPath);
const systemLogDirExist = await fileExist(systemLogPath);
const tmpDirExist = await fileExist(tmpPath);
const scriptNotifyJsFileExist = await fileExist(scriptNotifyJsFile);
const scriptNotifyPyFileExist = await fileExist(scriptNotifyPyFile);
const TaskBeforeFileExist = await fileExist(TaskBeforeFile);
const TaskAfterFileExist = await fileExist(TaskAfterFile);
if (!configDirExist) {
await fs.mkdir(configPath);
} }
for (const item of files) { if (!scriptDirExist) {
const exists = await fileExist(item.target); await fs.mkdir(scriptPath);
if (!item.checkExistence || !exists) { }
if (!item.content && !item.source) {
throw new Error( if (!logDirExist) {
`Neither content nor source specified for ${item.target}`, await fs.mkdir(logPath);
}
if (!tmpDirExist) {
await fs.mkdir(tmpPath);
}
if (!uploadDirExist) {
await fs.mkdir(uploadPath);
}
if (!sshDirExist) {
await fs.mkdir(sshPath);
}
if (!bakDirExist) {
await fs.mkdir(bakPath);
}
if (!sshdDirExist) {
await fs.mkdir(sshdPath);
}
if (!systemLogDirExist) {
await fs.mkdir(systemLogPath);
}
// 初始化文件
if (!authFileExist) {
await fs.writeFile(authConfigFile, await fs.readFile(sampleAuthFile));
}
if (!confFileExist) {
await fs.writeFile(confFile, await fs.readFile(sampleConfigFile));
}
if (!scriptNotifyJsFileExist) {
await fs.writeFile(
scriptNotifyJsFile,
await fs.readFile(sampleNotifyJsFile),
); );
} }
const content =
item.content || if (!scriptNotifyPyFileExist) {
(await fs.readFile(item.source!, { encoding: 'utf-8' })); await fs.writeFile(
await writeFileWithLock(item.target, content); scriptNotifyPyFile,
await fs.readFile(sampleNotifyPyFile),
);
} }
if (!TaskBeforeFileExist) {
await fs.writeFile(TaskBeforeFile, await fs.readFile(sampleTaskShellFile));
}
if (!TaskAfterFileExist) {
await fs.writeFile(TaskAfterFile, await fs.readFile(sampleTaskShellFile));
} }
Logger.info('✌️ Init file down'); Logger.info('✌️ Init file down');
console.log('✌️ Init file down');
}; };
+8 -31
View File
@@ -2,7 +2,6 @@ import { Container } from 'typedi';
import SystemService from '../services/system'; import SystemService from '../services/system';
import ScheduleService, { ScheduleTaskType } from '../services/schedule'; import ScheduleService, { ScheduleTaskType } from '../services/schedule';
import SubscriptionService from '../services/subscription'; import SubscriptionService from '../services/subscription';
import SshKeyService from '../services/sshKey';
import config from '../config'; import config from '../config';
import { fileExist } from '../config/util'; import { fileExist } from '../config/util';
import { join } from 'path'; import { join } from 'path';
@@ -11,13 +10,9 @@ export default async () => {
const systemService = Container.get(SystemService); const systemService = Container.get(SystemService);
const scheduleService = Container.get(ScheduleService); const scheduleService = Container.get(ScheduleService);
const subscriptionService = Container.get(SubscriptionService); const subscriptionService = Container.get(SubscriptionService);
const sshKeyService = Container.get(SshKeyService);
// 生成内置token // 生成内置token
let tokenCommand = `ts-node-transpile-only ${join( let tokenCommand = `tsx ${join(config.rootPath, 'back/token.ts')}`;
config.rootPath,
'back/token.ts',
)}`;
const tokenFile = join(config.rootPath, 'static/build/token.js'); const tokenFile = join(config.rootPath, 'static/build/token.js');
if (await fileExist(tokenFile)) { if (await fileExist(tokenFile)) {
@@ -27,48 +22,30 @@ export default async () => {
id: NaN, id: NaN,
name: '生成token', name: '生成token',
command: tokenCommand, command: tokenCommand,
runOrigin: 'system',
} as ScheduleTaskType; } as ScheduleTaskType;
await scheduleService.cancelIntervalTask(cron); await scheduleService.cancelIntervalTask(cron);
scheduleService.createIntervalTask( scheduleService.createIntervalTask(cron, {
cron,
{
days: 28, days: 28,
}, });
true,
);
// 运行删除日志任务 // 运行删除日志任务
const data = await systemService.getSystemConfig(); const data = await systemService.getSystemConfig();
if (data && data.info) { if (data && data.info && data.info.logRemoveFrequency) {
if (data.info.logRemoveFrequency) {
const rmlogCron = { const rmlogCron = {
id: data.id as number, id: data.id as number,
name: '删除日志', name: '删除日志',
command: `ql rmlog ${data.info.logRemoveFrequency}`, command: `ql rmlog ${data.info.logRemoveFrequency}`,
runOrigin: 'system' as const,
}; };
await scheduleService.cancelIntervalTask(rmlogCron); await scheduleService.cancelIntervalTask(rmlogCron);
scheduleService.createIntervalTask( scheduleService.createIntervalTask(rmlogCron, {
rmlogCron,
{
days: data.info.logRemoveFrequency, days: data.info.logRemoveFrequency,
}, });
true,
);
}
systemService.updateTimezone(data.info);
// Apply global SSH key if configured
if (data.info.globalSshKey) {
await sshKeyService.addGlobalSSHKey(data.info.globalSshKey, 'global');
}
} }
// 运行所有订阅
await subscriptionService.setSshConfig(); await subscriptionService.setSshConfig();
const subs = await subscriptionService.list(); const subs = await subscriptionService.list();
for (const sub of subs) { for (const sub of subs) {
subscriptionService.handleTask(sub.get({ plain: true }), !sub.is_disabled); subscriptionService.handleTask(sub, !sub.is_disabled, !sub.is_disabled);
} }
}; };
+25 -50
View File
@@ -4,60 +4,35 @@ import config from '../config';
import path from 'path'; import path from 'path';
const levelMap: Record<string, string> = { const levelMap: Record<string, string> = {
info: '', // info图标 info: '🔵',
warn: '⚠️', // 警告图标 warn: '🟡',
error: '', // 错误图标 error: '🔴',
debug: '🐛', // debug调试图标 debug: '🔶'
}
const customFormat = winston.format.combine(
winston.format.splat(),
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
winston.format.align(),
winston.format.printf((i) => `[${levelMap[i.level]}${i.level}] [${[i.timestamp]}]: ${i.message}`),
);
const defaultOptions = {
format: customFormat,
datePattern: "YYYY-MM-DD",
maxSize: "20m",
maxFiles: "7d",
}; };
const baseFormat = [
winston.format.splat(),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.align(),
];
const consoleFormat = winston.format.combine(
winston.format.colorize({ level: true }),
...baseFormat,
winston.format.printf((info) => {
return `[${info.level} ${info.timestamp}]:${info.message}`;
}),
);
const plainFormat = winston.format.combine(
winston.format.uncolorize(),
...baseFormat,
winston.format.printf((info) => {
return `[${levelMap[info.level] || ''}${info.level} ${info.timestamp}]:${
info.message
}`;
}),
);
const consoleTransport = new winston.transports.Console({
format: consoleFormat,
level: 'debug',
});
const fileTransport = new winston.transports.DailyRotateFile({
filename: path.join(config.systemLogPath, '%DATE%.log'),
datePattern: 'YYYY-MM-DD',
maxSize: '20m',
maxFiles: '7d',
format: plainFormat,
level: config.logs.level || 'info',
});
const LoggerInstance = winston.createLogger({ const LoggerInstance = winston.createLogger({
level: 'debug', level: config.logs.level,
levels: winston.config.npm.levels, levels: winston.config.npm.levels,
transports: [consoleTransport, fileTransport], transports: [
exceptionHandlers: [consoleTransport, fileTransport], new winston.transports.DailyRotateFile({
rejectionHandlers: [consoleTransport, fileTransport], filename: path.join(config.systemLogPath, '%DATE%.log'),
}); ...defaultOptions,
})
LoggerInstance.on('error', (error) => { ],
console.error('Logger error:', error);
}); });
export default LoggerInstance; export default LoggerInstance;
+32
View File
@@ -0,0 +1,32 @@
import { Application } from 'express';
import * as Sentry from '@sentry/node';
import Logger from './logger';
import config from '../config';
import fs from 'fs';
import { parseVersion } from '../config/util';
export default async ({ expressApp }: { expressApp: Application }) => {
const { version } = await parseVersion(config.versionFile);
Sentry.init({
ignoreErrors: [
/SequelizeUniqueConstraintError/i,
/Validation error/i,
/UnauthorizedError/i,
/celebrate request validation failed/i,
],
dsn: 'https://8b5c84cfef3e22541bc84de0ed00497b@o1098464.ingest.sentry.io/6122819',
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Sentry.Integrations.Express({ app: expressApp }),
],
tracesSampleRate: 0.8,
release: version,
});
expressApp.use(Sentry.Handlers.requestHandler());
expressApp.use(Sentry.Handlers.tracingHandler());
Logger.info('✌️ Sentry loaded');
console.log('✌️ Sentry loaded');
};
+12 -8
View File
@@ -2,13 +2,12 @@ import sockJs from 'sockjs';
import { Server } from 'http'; import { Server } from 'http';
import { Container } from 'typedi'; import { Container } from 'typedi';
import SockService from '../services/sock'; import SockService from '../services/sock';
import { getPlatform } from '../config/util'; import config from '../config/index';
import { shareStore } from '../shared/store'; import fs from 'fs/promises';
import { isValidToken } from '../shared/auth'; import { getPlatform, safeJSONParse } from '../config/util';
import config from '../config';
export default async ({ server }: { server: Server }) => { export default async ({ server }: { server: Server }) => {
const echo = sockJs.createServer({ prefix: `${config.baseUrl}/api/ws`, log: () => { } }); const echo = sockJs.createServer({ prefix: '/api/ws', log: () => {} });
const sockService = Container.get(SockService); const sockService = Container.get(SockService);
echo.on('connection', async (conn) => { echo.on('connection', async (conn) => {
@@ -16,11 +15,13 @@ export default async ({ server }: { server: Server }) => {
conn.close('404'); conn.close('404');
} }
const authInfo = await shareStore.getAuthInfo(); const data = await fs.readFile(config.authConfigFile, 'utf8');
const platform = getPlatform(conn.headers['user-agent'] || '') || 'desktop'; const platform = getPlatform(conn.headers['user-agent'] || '') || 'desktop';
const headerToken = conn.url.replace(`${conn.pathname}?token=`, ''); const headerToken = conn.url.replace(`${conn.pathname}?token=`, '');
if (data) {
if (isValidToken(authInfo, headerToken, platform)) { const { token = '', tokens = {} } = safeJSONParse(data);
if (headerToken === token || tokens[platform] === headerToken) {
conn.write(JSON.stringify({ type: 'ping', message: 'hanhh' }));
sockService.addClient(conn); sockService.addClient(conn);
conn.on('data', (message) => { conn.on('data', (message) => {
@@ -32,6 +33,9 @@ export default async ({ server }: { server: Server }) => {
}); });
return; return;
} else {
conn.write(JSON.stringify({ type: 'ping', message: 'whyour' }));
}
} }
conn.close('404'); conn.close('404');
+106
View File
@@ -0,0 +1,106 @@
import bodyParser from 'body-parser';
import { errors } from 'celebrate';
import cors from 'cors';
import { Application, NextFunction, Request, Response } from 'express';
import jwt from 'express-jwt';
import Container from 'typedi';
import config from '../config';
import SystemService from '../services/system';
import Logger from './logger';
export default ({ app }: { app: Application }) => {
app.set('trust proxy', 'loopback');
app.use(cors());
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(
jwt({
secret: config.secret,
algorithms: ['HS384'],
}),
);
app.put(
'/api/reload',
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.reloadSystem();
res.send(result);
} catch (e) {
Logger.error('🔥 error: %o', e);
return next(e);
}
},
);
app.put(
'/api/system',
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.reloadSystem('system');
res.send(result);
} catch (e) {
Logger.error('🔥 error: %o', e);
return next(e);
}
},
);
app.put(
'/api/data',
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.reloadSystem('data');
res.send(result);
} catch (e) {
Logger.error('🔥 error: %o', e);
return next(e);
}
},
);
app.use((req, res, next) => {
const err: any = new Error('Not Found');
err['status'] = 404;
next(err);
});
app.use(errors());
app.use(
(
err: Error & { status: number },
req: Request,
res: Response,
next: NextFunction,
) => {
if (err.name === 'UnauthorizedError') {
return res
.status(err.status)
.send({ code: 401, message: err.message })
.end();
}
return next(err);
},
);
app.use(
(
err: Error & { status: number },
req: Request,
res: Response,
next: NextFunction,
) => {
res.status(err.status || 500);
res.json({
code: err.status || 500,
message: err.message,
});
},
);
};
-80
View File
@@ -1,80 +0,0 @@
import { Request, Response, NextFunction } from 'express';
import Logger from '../loaders/logger';
import { performance } from 'perf_hooks';
import { metricsService } from '../services/metrics';
interface RequestMetrics {
method: string;
path: string;
duration: number;
statusCode: number;
timestamp: number;
platform?: string;
}
const requestMetrics: RequestMetrics[] = [];
export const monitoringMiddleware = (
req: Request,
res: Response,
next: NextFunction,
) => {
const start = performance.now();
const originalEnd = res.end;
res.end = function (chunk?: any, encoding?: any, cb?: any) {
const duration = performance.now() - start;
const metric: RequestMetrics = {
method: req.method,
path: req.path,
duration,
statusCode: res.statusCode,
timestamp: Date.now(),
platform: req.platform,
};
requestMetrics.push(metric);
metricsService.record('http_request', duration, {
method: req.method,
path: req.path,
statusCode: res.statusCode.toString(),
...(req.platform && { platform: req.platform }),
});
if (requestMetrics.length > 1000) {
requestMetrics.shift();
}
if (duration > 1000) {
Logger.warn(
`Slow request detected: ${req.method} ${
req.path
} took ${duration.toFixed(2)}ms`,
);
}
return originalEnd.call(this, chunk, encoding, cb);
};
next();
};
export const getMetrics = () => {
return {
totalRequests: requestMetrics.length,
averageDuration:
requestMetrics.reduce((acc, curr) => acc + curr.duration, 0) /
requestMetrics.length,
requestsByMethod: requestMetrics.reduce((acc, curr) => {
acc[curr.method] = (acc[curr.method] || 0) + 1;
return acc;
}, {} as Record<string, number>),
requestsByPlatform: requestMetrics.reduce((acc, curr) => {
if (curr.platform) {
acc[curr.platform] = (acc[curr.platform] || 0) + 1;
}
return acc;
}, {} as Record<string, number>),
recentRequests: requestMetrics.slice(-10),
};
};
-275
View File
@@ -1,275 +0,0 @@
syntax = "proto3";
package com.ql.api;
message EnvItem {
optional int32 id = 1;
optional string name = 2;
optional string value = 3;
optional string remarks = 4;
optional int32 status = 5;
optional int64 position = 6;
}
message GetEnvsRequest { string searchValue = 1; }
message CreateEnvRequest { repeated EnvItem envs = 1; }
message UpdateEnvRequest { EnvItem env = 1; }
message DeleteEnvsRequest { repeated int32 ids = 1; }
message MoveEnvRequest {
int32 id = 1;
int32 fromIndex = 2;
int32 toIndex = 3;
}
message DisableEnvsRequest { repeated int32 ids = 1; }
message EnableEnvsRequest { repeated int32 ids = 1; }
message UpdateEnvNamesRequest {
repeated int32 ids = 1;
string name = 2;
}
message GetEnvByIdRequest { int32 id = 1; }
message EnvsResponse {
int32 code = 1;
repeated EnvItem data = 2;
optional string message = 3;
}
message EnvResponse {
int32 code = 1;
EnvItem data = 2;
optional string message = 3;
}
message Response {
int32 code = 1;
optional string message = 2;
}
message ExtraScheduleItem { string schedule = 1; }
message CronItem {
optional int32 id = 1;
optional string command = 2;
optional string schedule = 3;
optional string name = 4;
repeated string labels = 5;
optional int32 sub_id = 6;
repeated ExtraScheduleItem extra_schedules = 7;
optional string task_before = 8;
optional string task_after = 9;
optional int32 status = 10;
optional string log_path = 11;
optional int32 pid = 12;
optional int64 last_running_time = 13;
optional int64 last_execution_time = 14;
}
message CreateCronRequest {
string command = 1;
string schedule = 2;
optional string name = 3;
repeated string labels = 4;
optional int32 sub_id = 5;
repeated ExtraScheduleItem extra_schedules = 6;
optional string task_before = 7;
optional string task_after = 8;
}
message UpdateCronRequest {
int32 id = 1;
optional string command = 2;
optional string schedule = 3;
optional string name = 4;
repeated string labels = 5;
optional int32 sub_id = 6;
repeated ExtraScheduleItem extra_schedules = 7;
optional string task_before = 8;
optional string task_after = 9;
}
message DeleteCronsRequest { repeated int32 ids = 1; }
message GetCronsRequest {
optional string searchValue = 1;
}
message GetCronByIdRequest { int32 id = 1; }
message EnableCronsRequest { repeated int32 ids = 1; }
message DisableCronsRequest { repeated int32 ids = 1; }
message RunCronsRequest { repeated int32 ids = 1; }
message CronsResponse {
int32 code = 1;
repeated CronItem data = 2;
optional string message = 3;
}
message CronResponse {
int32 code = 1;
CronItem data = 2;
optional string message = 3;
}
message CronDetailRequest { string log_path = 1; }
message CronDetailResponse {
int32 code = 1;
CronItem data = 2;
optional string message = 3;
}
enum NotificationMode {
gotify = 0;
goCqHttpBot = 1;
serverChan = 2;
pushDeer = 3;
bark = 4;
chat = 5;
telegramBot = 6;
dingtalkBot = 7;
weWorkBot = 8;
weWorkApp = 9;
aibotk = 10;
iGot = 11;
pushPlus = 12;
wePlusBot = 13;
email = 14;
pushMe = 15;
feishu = 16;
webhook = 17;
chronocat = 18;
ntfy = 19;
wxPusherBot = 20;
}
message NotificationInfo {
NotificationMode type = 1;
optional string gotifyUrl = 2;
optional string gotifyToken = 3;
optional int32 gotifyPriority = 4;
optional string goCqHttpBotUrl = 5;
optional string goCqHttpBotToken = 6;
optional string goCqHttpBotQq = 7;
optional string serverChanKey = 8;
optional string pushDeerKey = 9;
optional string pushDeerUrl = 10;
optional string synologyChatUrl = 11;
optional string barkPush = 12;
optional string barkIcon = 13;
optional string barkSound = 14;
optional string barkGroup = 15;
optional string barkLevel = 16;
optional string barkUrl = 17;
optional string barkArchive = 18;
optional string telegramBotToken = 19;
optional string telegramBotUserId = 20;
optional string telegramBotProxyHost = 21;
optional string telegramBotProxyPort = 22;
optional string telegramBotProxyAuth = 23;
optional string telegramBotApiHost = 24;
optional string dingtalkBotToken = 25;
optional string dingtalkBotSecret = 26;
optional string weWorkBotKey = 27;
optional string weWorkOrigin = 28;
optional string weWorkAppKey = 29;
optional string aibotkKey = 30;
optional string aibotkType = 31;
optional string aibotkName = 32;
optional string iGotPushKey = 33;
optional string pushPlusToken = 34;
optional string pushPlusUser = 35;
optional string pushPlusTemplate = 36;
optional string pushplusChannel = 37;
optional string pushplusWebhook = 38;
optional string pushplusCallbackUrl = 39;
optional string pushplusTo = 40;
optional string wePlusBotToken = 41;
optional string wePlusBotReceiver = 42;
optional string wePlusBotVersion = 43;
optional string emailService = 44;
optional string emailUser = 45;
optional string emailPass = 46;
optional string emailTo = 47;
optional string pushMeKey = 48;
optional string pushMeUrl = 49;
optional string chronocatURL = 50;
optional string chronocatQQ = 51;
optional string chronocatToken = 52;
optional string webhookHeaders = 53;
optional string webhookBody = 54;
optional string webhookUrl = 55;
optional string webhookMethod = 56;
optional string webhookContentType = 57;
optional string larkKey = 58;
optional string larkSecret = 69;
optional string ntfyUrl = 59;
optional string ntfyTopic = 60;
optional string ntfyPriority = 61;
optional string ntfyToken = 62;
optional string ntfyUsername = 63;
optional string ntfyPassword = 64;
optional string ntfyActions = 65;
optional string wxPusherBotAppToken = 66;
optional string wxPusherBotTopicIds = 67;
optional string wxPusherBotUids = 68;
}
message SystemNotifyRequest {
string title = 1;
string content = 2;
optional NotificationInfo notificationInfo = 3;
}
service Api {
rpc GetEnvs(GetEnvsRequest) returns (EnvsResponse) {}
rpc CreateEnv(CreateEnvRequest) returns (EnvsResponse) {}
rpc UpdateEnv(UpdateEnvRequest) returns (EnvResponse) {}
rpc DeleteEnvs(DeleteEnvsRequest) returns (Response) {}
rpc MoveEnv(MoveEnvRequest) returns (EnvResponse) {}
rpc DisableEnvs(DisableEnvsRequest) returns (Response) {}
rpc EnableEnvs(EnableEnvsRequest) returns (Response) {}
rpc UpdateEnvNames(UpdateEnvNamesRequest) returns (Response) {}
rpc GetEnvById(GetEnvByIdRequest) returns (EnvResponse) {}
rpc SystemNotify(SystemNotifyRequest) returns (Response) {}
rpc GetCronDetail(CronDetailRequest) returns (CronDetailResponse) {}
rpc CreateCron(CreateCronRequest) returns (CronResponse) {}
rpc UpdateCron(UpdateCronRequest) returns (CronResponse) {}
rpc DeleteCrons(DeleteCronsRequest) returns (Response) {}
rpc GetCrons(GetCronsRequest) returns (CronsResponse) {}
rpc GetCronById(GetCronByIdRequest) returns (CronResponse) {}
rpc EnableCrons(EnableCronsRequest) returns (Response) {}
rpc DisableCrons(DisableCronsRequest) returns (Response) {}
rpc RunCrons(RunCronsRequest) returns (Response) {}
}
-4740
View File
File diff suppressed because it is too large Load Diff
+91 -114
View File
@@ -1,23 +1,17 @@
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// versions:
// protoc-gen-ts_proto v2.6.1
// protoc v3.21.12
// source: back/protos/cron.proto
/* eslint-disable */ /* eslint-disable */
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
import { import {
type CallOptions, CallOptions,
ChannelCredentials, ChannelCredentials,
Client, Client,
type ClientOptions, ClientOptions,
type ClientUnaryCall, ClientUnaryCall,
type handleUnaryCall, handleUnaryCall,
makeGenericClientConstructor, makeGenericClientConstructor,
Metadata, Metadata,
type ServiceError, ServiceError,
type UntypedServiceImplementation, UntypedServiceImplementation,
} from "@grpc/grpc-js"; } from "@grpc/grpc-js";
import _m0 from "protobufjs/minimal";
export const protobufPackage = "com.ql.cron"; export const protobufPackage = "com.ql.cron";
@@ -29,7 +23,7 @@ export interface ICron {
id: string; id: string;
schedule: string; schedule: string;
command: string; command: string;
extra_schedules: ISchedule[]; extraSchedules: ISchedule[];
name: string; name: string;
} }
@@ -51,22 +45,22 @@ function createBaseISchedule(): ISchedule {
return { schedule: "" }; return { schedule: "" };
} }
export const ISchedule: MessageFns<ISchedule> = { export const ISchedule = {
encode(message: ISchedule, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { encode(message: ISchedule, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.schedule !== "") { if (message.schedule !== "") {
writer.uint32(10).string(message.schedule); writer.uint32(10).string(message.schedule);
} }
return writer; return writer;
}, },
decode(input: BinaryReader | Uint8Array, length?: number): ISchedule { decode(input: _m0.Reader | Uint8Array, length?: number): ISchedule {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input); const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length; let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseISchedule(); const message = createBaseISchedule();
while (reader.pos < end) { while (reader.pos < end) {
const tag = reader.uint32(); const tag = reader.uint32();
switch (tag >>> 3) { switch (tag >>> 3) {
case 1: { case 1:
if (tag !== 10) { if (tag !== 10) {
break; break;
} }
@@ -74,30 +68,28 @@ export const ISchedule: MessageFns<ISchedule> = {
message.schedule = reader.string(); message.schedule = reader.string();
continue; continue;
} }
}
if ((tag & 7) === 4 || tag === 0) { if ((tag & 7) === 4 || tag === 0) {
break; break;
} }
reader.skip(tag & 7); reader.skipType(tag & 7);
} }
return message; return message;
}, },
fromJSON(object: any): ISchedule { fromJSON(object: any): ISchedule {
return { schedule: isSet(object.schedule) ? globalThis.String(object.schedule) : "" }; return { schedule: isSet(object.schedule) ? String(object.schedule) : "" };
}, },
toJSON(message: ISchedule): unknown { toJSON(message: ISchedule): unknown {
const obj: any = {}; const obj: any = {};
if (message.schedule !== "") { message.schedule !== undefined && (obj.schedule = message.schedule);
obj.schedule = message.schedule;
}
return obj; return obj;
}, },
create<I extends Exact<DeepPartial<ISchedule>, I>>(base?: I): ISchedule { create<I extends Exact<DeepPartial<ISchedule>, I>>(base?: I): ISchedule {
return ISchedule.fromPartial(base ?? ({} as any)); return ISchedule.fromPartial(base ?? {});
}, },
fromPartial<I extends Exact<DeepPartial<ISchedule>, I>>(object: I): ISchedule { fromPartial<I extends Exact<DeepPartial<ISchedule>, I>>(object: I): ISchedule {
const message = createBaseISchedule(); const message = createBaseISchedule();
message.schedule = object.schedule ?? ""; message.schedule = object.schedule ?? "";
@@ -106,11 +98,11 @@ export const ISchedule: MessageFns<ISchedule> = {
}; };
function createBaseICron(): ICron { function createBaseICron(): ICron {
return { id: "", schedule: "", command: "", extra_schedules: [], name: "" }; return { id: "", schedule: "", command: "", extraSchedules: [], name: "" };
} }
export const ICron: MessageFns<ICron> = { export const ICron = {
encode(message: ICron, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { encode(message: ICron, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.id !== "") { if (message.id !== "") {
writer.uint32(10).string(message.id); writer.uint32(10).string(message.id);
} }
@@ -120,8 +112,8 @@ export const ICron: MessageFns<ICron> = {
if (message.command !== "") { if (message.command !== "") {
writer.uint32(26).string(message.command); writer.uint32(26).string(message.command);
} }
for (const v of message.extra_schedules) { for (const v of message.extraSchedules) {
ISchedule.encode(v!, writer.uint32(34).fork()).join(); ISchedule.encode(v!, writer.uint32(34).fork()).ldelim();
} }
if (message.name !== "") { if (message.name !== "") {
writer.uint32(42).string(message.name); writer.uint32(42).string(message.name);
@@ -129,46 +121,42 @@ export const ICron: MessageFns<ICron> = {
return writer; return writer;
}, },
decode(input: BinaryReader | Uint8Array, length?: number): ICron { decode(input: _m0.Reader | Uint8Array, length?: number): ICron {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input); const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length; let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseICron(); const message = createBaseICron();
while (reader.pos < end) { while (reader.pos < end) {
const tag = reader.uint32(); const tag = reader.uint32();
switch (tag >>> 3) { switch (tag >>> 3) {
case 1: { case 1:
if (tag !== 10) { if (tag !== 10) {
break; break;
} }
message.id = reader.string(); message.id = reader.string();
continue; continue;
} case 2:
case 2: {
if (tag !== 18) { if (tag !== 18) {
break; break;
} }
message.schedule = reader.string(); message.schedule = reader.string();
continue; continue;
} case 3:
case 3: {
if (tag !== 26) { if (tag !== 26) {
break; break;
} }
message.command = reader.string(); message.command = reader.string();
continue; continue;
} case 4:
case 4: {
if (tag !== 34) { if (tag !== 34) {
break; break;
} }
message.extra_schedules.push(ISchedule.decode(reader, reader.uint32())); message.extraSchedules.push(ISchedule.decode(reader, reader.uint32()));
continue; continue;
} case 5:
case 5: {
if (tag !== 42) { if (tag !== 42) {
break; break;
} }
@@ -176,56 +164,50 @@ export const ICron: MessageFns<ICron> = {
message.name = reader.string(); message.name = reader.string();
continue; continue;
} }
}
if ((tag & 7) === 4 || tag === 0) { if ((tag & 7) === 4 || tag === 0) {
break; break;
} }
reader.skip(tag & 7); reader.skipType(tag & 7);
} }
return message; return message;
}, },
fromJSON(object: any): ICron { fromJSON(object: any): ICron {
return { return {
id: isSet(object.id) ? globalThis.String(object.id) : "", id: isSet(object.id) ? String(object.id) : "",
schedule: isSet(object.schedule) ? globalThis.String(object.schedule) : "", schedule: isSet(object.schedule) ? String(object.schedule) : "",
command: isSet(object.command) ? globalThis.String(object.command) : "", command: isSet(object.command) ? String(object.command) : "",
extra_schedules: globalThis.Array.isArray(object?.extra_schedules) extraSchedules: Array.isArray(object?.extraSchedules)
? object.extra_schedules.map((e: any) => ISchedule.fromJSON(e)) ? object.extraSchedules.map((e: any) => ISchedule.fromJSON(e))
: [], : [],
name: isSet(object.name) ? globalThis.String(object.name) : "", name: isSet(object.name) ? String(object.name) : "",
}; };
}, },
toJSON(message: ICron): unknown { toJSON(message: ICron): unknown {
const obj: any = {}; const obj: any = {};
if (message.id !== "") { message.id !== undefined && (obj.id = message.id);
obj.id = message.id; message.schedule !== undefined && (obj.schedule = message.schedule);
} message.command !== undefined && (obj.command = message.command);
if (message.schedule !== "") { if (message.extraSchedules) {
obj.schedule = message.schedule; obj.extraSchedules = message.extraSchedules.map((e) => e ? ISchedule.toJSON(e) : undefined);
} } else {
if (message.command !== "") { obj.extraSchedules = [];
obj.command = message.command;
}
if (message.extra_schedules?.length) {
obj.extra_schedules = message.extra_schedules.map((e) => ISchedule.toJSON(e));
}
if (message.name !== "") {
obj.name = message.name;
} }
message.name !== undefined && (obj.name = message.name);
return obj; return obj;
}, },
create<I extends Exact<DeepPartial<ICron>, I>>(base?: I): ICron { create<I extends Exact<DeepPartial<ICron>, I>>(base?: I): ICron {
return ICron.fromPartial(base ?? ({} as any)); return ICron.fromPartial(base ?? {});
}, },
fromPartial<I extends Exact<DeepPartial<ICron>, I>>(object: I): ICron { fromPartial<I extends Exact<DeepPartial<ICron>, I>>(object: I): ICron {
const message = createBaseICron(); const message = createBaseICron();
message.id = object.id ?? ""; message.id = object.id ?? "";
message.schedule = object.schedule ?? ""; message.schedule = object.schedule ?? "";
message.command = object.command ?? ""; message.command = object.command ?? "";
message.extra_schedules = object.extra_schedules?.map((e) => ISchedule.fromPartial(e)) || []; message.extraSchedules = object.extraSchedules?.map((e) => ISchedule.fromPartial(e)) || [];
message.name = object.name ?? ""; message.name = object.name ?? "";
return message; return message;
}, },
@@ -235,22 +217,22 @@ function createBaseAddCronRequest(): AddCronRequest {
return { crons: [] }; return { crons: [] };
} }
export const AddCronRequest: MessageFns<AddCronRequest> = { export const AddCronRequest = {
encode(message: AddCronRequest, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { encode(message: AddCronRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
for (const v of message.crons) { for (const v of message.crons) {
ICron.encode(v!, writer.uint32(10).fork()).join(); ICron.encode(v!, writer.uint32(10).fork()).ldelim();
} }
return writer; return writer;
}, },
decode(input: BinaryReader | Uint8Array, length?: number): AddCronRequest { decode(input: _m0.Reader | Uint8Array, length?: number): AddCronRequest {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input); const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length; let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseAddCronRequest(); const message = createBaseAddCronRequest();
while (reader.pos < end) { while (reader.pos < end) {
const tag = reader.uint32(); const tag = reader.uint32();
switch (tag >>> 3) { switch (tag >>> 3) {
case 1: { case 1:
if (tag !== 10) { if (tag !== 10) {
break; break;
} }
@@ -258,30 +240,32 @@ export const AddCronRequest: MessageFns<AddCronRequest> = {
message.crons.push(ICron.decode(reader, reader.uint32())); message.crons.push(ICron.decode(reader, reader.uint32()));
continue; continue;
} }
}
if ((tag & 7) === 4 || tag === 0) { if ((tag & 7) === 4 || tag === 0) {
break; break;
} }
reader.skip(tag & 7); reader.skipType(tag & 7);
} }
return message; return message;
}, },
fromJSON(object: any): AddCronRequest { fromJSON(object: any): AddCronRequest {
return { crons: globalThis.Array.isArray(object?.crons) ? object.crons.map((e: any) => ICron.fromJSON(e)) : [] }; return { crons: Array.isArray(object?.crons) ? object.crons.map((e: any) => ICron.fromJSON(e)) : [] };
}, },
toJSON(message: AddCronRequest): unknown { toJSON(message: AddCronRequest): unknown {
const obj: any = {}; const obj: any = {};
if (message.crons?.length) { if (message.crons) {
obj.crons = message.crons.map((e) => ICron.toJSON(e)); obj.crons = message.crons.map((e) => e ? ICron.toJSON(e) : undefined);
} else {
obj.crons = [];
} }
return obj; return obj;
}, },
create<I extends Exact<DeepPartial<AddCronRequest>, I>>(base?: I): AddCronRequest { create<I extends Exact<DeepPartial<AddCronRequest>, I>>(base?: I): AddCronRequest {
return AddCronRequest.fromPartial(base ?? ({} as any)); return AddCronRequest.fromPartial(base ?? {});
}, },
fromPartial<I extends Exact<DeepPartial<AddCronRequest>, I>>(object: I): AddCronRequest { fromPartial<I extends Exact<DeepPartial<AddCronRequest>, I>>(object: I): AddCronRequest {
const message = createBaseAddCronRequest(); const message = createBaseAddCronRequest();
message.crons = object.crons?.map((e) => ICron.fromPartial(e)) || []; message.crons = object.crons?.map((e) => ICron.fromPartial(e)) || [];
@@ -293,13 +277,13 @@ function createBaseAddCronResponse(): AddCronResponse {
return {}; return {};
} }
export const AddCronResponse: MessageFns<AddCronResponse> = { export const AddCronResponse = {
encode(_: AddCronResponse, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { encode(_: AddCronResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
return writer; return writer;
}, },
decode(input: BinaryReader | Uint8Array, length?: number): AddCronResponse { decode(input: _m0.Reader | Uint8Array, length?: number): AddCronResponse {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input); const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length; let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseAddCronResponse(); const message = createBaseAddCronResponse();
while (reader.pos < end) { while (reader.pos < end) {
@@ -309,7 +293,7 @@ export const AddCronResponse: MessageFns<AddCronResponse> = {
if ((tag & 7) === 4 || tag === 0) { if ((tag & 7) === 4 || tag === 0) {
break; break;
} }
reader.skip(tag & 7); reader.skipType(tag & 7);
} }
return message; return message;
}, },
@@ -324,8 +308,9 @@ export const AddCronResponse: MessageFns<AddCronResponse> = {
}, },
create<I extends Exact<DeepPartial<AddCronResponse>, I>>(base?: I): AddCronResponse { create<I extends Exact<DeepPartial<AddCronResponse>, I>>(base?: I): AddCronResponse {
return AddCronResponse.fromPartial(base ?? ({} as any)); return AddCronResponse.fromPartial(base ?? {});
}, },
fromPartial<I extends Exact<DeepPartial<AddCronResponse>, I>>(_: I): AddCronResponse { fromPartial<I extends Exact<DeepPartial<AddCronResponse>, I>>(_: I): AddCronResponse {
const message = createBaseAddCronResponse(); const message = createBaseAddCronResponse();
return message; return message;
@@ -336,22 +321,22 @@ function createBaseDeleteCronRequest(): DeleteCronRequest {
return { ids: [] }; return { ids: [] };
} }
export const DeleteCronRequest: MessageFns<DeleteCronRequest> = { export const DeleteCronRequest = {
encode(message: DeleteCronRequest, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { encode(message: DeleteCronRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
for (const v of message.ids) { for (const v of message.ids) {
writer.uint32(10).string(v!); writer.uint32(10).string(v!);
} }
return writer; return writer;
}, },
decode(input: BinaryReader | Uint8Array, length?: number): DeleteCronRequest { decode(input: _m0.Reader | Uint8Array, length?: number): DeleteCronRequest {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input); const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length; let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseDeleteCronRequest(); const message = createBaseDeleteCronRequest();
while (reader.pos < end) { while (reader.pos < end) {
const tag = reader.uint32(); const tag = reader.uint32();
switch (tag >>> 3) { switch (tag >>> 3) {
case 1: { case 1:
if (tag !== 10) { if (tag !== 10) {
break; break;
} }
@@ -359,30 +344,32 @@ export const DeleteCronRequest: MessageFns<DeleteCronRequest> = {
message.ids.push(reader.string()); message.ids.push(reader.string());
continue; continue;
} }
}
if ((tag & 7) === 4 || tag === 0) { if ((tag & 7) === 4 || tag === 0) {
break; break;
} }
reader.skip(tag & 7); reader.skipType(tag & 7);
} }
return message; return message;
}, },
fromJSON(object: any): DeleteCronRequest { fromJSON(object: any): DeleteCronRequest {
return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => globalThis.String(e)) : [] }; return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
}, },
toJSON(message: DeleteCronRequest): unknown { toJSON(message: DeleteCronRequest): unknown {
const obj: any = {}; const obj: any = {};
if (message.ids?.length) { if (message.ids) {
obj.ids = message.ids; obj.ids = message.ids.map((e) => e);
} else {
obj.ids = [];
} }
return obj; return obj;
}, },
create<I extends Exact<DeepPartial<DeleteCronRequest>, I>>(base?: I): DeleteCronRequest { create<I extends Exact<DeepPartial<DeleteCronRequest>, I>>(base?: I): DeleteCronRequest {
return DeleteCronRequest.fromPartial(base ?? ({} as any)); return DeleteCronRequest.fromPartial(base ?? {});
}, },
fromPartial<I extends Exact<DeepPartial<DeleteCronRequest>, I>>(object: I): DeleteCronRequest { fromPartial<I extends Exact<DeepPartial<DeleteCronRequest>, I>>(object: I): DeleteCronRequest {
const message = createBaseDeleteCronRequest(); const message = createBaseDeleteCronRequest();
message.ids = object.ids?.map((e) => e) || []; message.ids = object.ids?.map((e) => e) || [];
@@ -394,13 +381,13 @@ function createBaseDeleteCronResponse(): DeleteCronResponse {
return {}; return {};
} }
export const DeleteCronResponse: MessageFns<DeleteCronResponse> = { export const DeleteCronResponse = {
encode(_: DeleteCronResponse, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { encode(_: DeleteCronResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
return writer; return writer;
}, },
decode(input: BinaryReader | Uint8Array, length?: number): DeleteCronResponse { decode(input: _m0.Reader | Uint8Array, length?: number): DeleteCronResponse {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input); const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length; let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseDeleteCronResponse(); const message = createBaseDeleteCronResponse();
while (reader.pos < end) { while (reader.pos < end) {
@@ -410,7 +397,7 @@ export const DeleteCronResponse: MessageFns<DeleteCronResponse> = {
if ((tag & 7) === 4 || tag === 0) { if ((tag & 7) === 4 || tag === 0) {
break; break;
} }
reader.skip(tag & 7); reader.skipType(tag & 7);
} }
return message; return message;
}, },
@@ -425,8 +412,9 @@ export const DeleteCronResponse: MessageFns<DeleteCronResponse> = {
}, },
create<I extends Exact<DeepPartial<DeleteCronResponse>, I>>(base?: I): DeleteCronResponse { create<I extends Exact<DeepPartial<DeleteCronResponse>, I>>(base?: I): DeleteCronResponse {
return DeleteCronResponse.fromPartial(base ?? ({} as any)); return DeleteCronResponse.fromPartial(base ?? {});
}, },
fromPartial<I extends Exact<DeepPartial<DeleteCronResponse>, I>>(_: I): DeleteCronResponse { fromPartial<I extends Exact<DeepPartial<DeleteCronResponse>, I>>(_: I): DeleteCronResponse {
const message = createBaseDeleteCronResponse(); const message = createBaseDeleteCronResponse();
return message; return message;
@@ -496,14 +484,12 @@ export interface CronClient extends Client {
export const CronClient = makeGenericClientConstructor(CronService, "com.ql.cron.Cron") as unknown as { export const CronClient = makeGenericClientConstructor(CronService, "com.ql.cron.Cron") as unknown as {
new (address: string, credentials: ChannelCredentials, options?: Partial<ClientOptions>): CronClient; new (address: string, credentials: ChannelCredentials, options?: Partial<ClientOptions>): CronClient;
service: typeof CronService; service: typeof CronService;
serviceName: string;
}; };
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
export type DeepPartial<T> = T extends Builtin ? T export type DeepPartial<T> = T extends Builtin ? T
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> } : T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>; : Partial<T>;
@@ -514,12 +500,3 @@ export type Exact<P, I extends P> = P extends Builtin ? P
function isSet(value: any): boolean { function isSet(value: any): boolean {
return value !== null && value !== undefined; return value !== null && value !== undefined;
} }
export interface MessageFns<T> {
encode(message: T, writer?: BinaryWriter): BinaryWriter;
decode(input: BinaryReader | Uint8Array, length?: number): T;
fromJSON(object: any): T;
toJSON(message: T): unknown;
create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
}
+29 -50
View File
@@ -1,25 +1,19 @@
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// versions:
// protoc-gen-ts_proto v2.6.1
// protoc v3.21.12
// source: back/protos/health.proto
/* eslint-disable */ /* eslint-disable */
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
import { import {
type CallOptions, CallOptions,
ChannelCredentials, ChannelCredentials,
Client, Client,
type ClientOptions, ClientOptions,
type ClientReadableStream, ClientReadableStream,
type ClientUnaryCall, ClientUnaryCall,
type handleServerStreamingCall, handleServerStreamingCall,
type handleUnaryCall, handleUnaryCall,
makeGenericClientConstructor, makeGenericClientConstructor,
Metadata, Metadata,
type ServiceError, ServiceError,
type UntypedServiceImplementation, UntypedServiceImplementation,
} from "@grpc/grpc-js"; } from "@grpc/grpc-js";
import _m0 from "protobufjs/minimal";
export const protobufPackage = "com.ql.health"; export const protobufPackage = "com.ql.health";
@@ -80,22 +74,22 @@ function createBaseHealthCheckRequest(): HealthCheckRequest {
return { service: "" }; return { service: "" };
} }
export const HealthCheckRequest: MessageFns<HealthCheckRequest> = { export const HealthCheckRequest = {
encode(message: HealthCheckRequest, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { encode(message: HealthCheckRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.service !== "") { if (message.service !== "") {
writer.uint32(10).string(message.service); writer.uint32(10).string(message.service);
} }
return writer; return writer;
}, },
decode(input: BinaryReader | Uint8Array, length?: number): HealthCheckRequest { decode(input: _m0.Reader | Uint8Array, length?: number): HealthCheckRequest {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input); const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length; let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseHealthCheckRequest(); const message = createBaseHealthCheckRequest();
while (reader.pos < end) { while (reader.pos < end) {
const tag = reader.uint32(); const tag = reader.uint32();
switch (tag >>> 3) { switch (tag >>> 3) {
case 1: { case 1:
if (tag !== 10) { if (tag !== 10) {
break; break;
} }
@@ -103,30 +97,28 @@ export const HealthCheckRequest: MessageFns<HealthCheckRequest> = {
message.service = reader.string(); message.service = reader.string();
continue; continue;
} }
}
if ((tag & 7) === 4 || tag === 0) { if ((tag & 7) === 4 || tag === 0) {
break; break;
} }
reader.skip(tag & 7); reader.skipType(tag & 7);
} }
return message; return message;
}, },
fromJSON(object: any): HealthCheckRequest { fromJSON(object: any): HealthCheckRequest {
return { service: isSet(object.service) ? globalThis.String(object.service) : "" }; return { service: isSet(object.service) ? String(object.service) : "" };
}, },
toJSON(message: HealthCheckRequest): unknown { toJSON(message: HealthCheckRequest): unknown {
const obj: any = {}; const obj: any = {};
if (message.service !== "") { message.service !== undefined && (obj.service = message.service);
obj.service = message.service;
}
return obj; return obj;
}, },
create<I extends Exact<DeepPartial<HealthCheckRequest>, I>>(base?: I): HealthCheckRequest { create<I extends Exact<DeepPartial<HealthCheckRequest>, I>>(base?: I): HealthCheckRequest {
return HealthCheckRequest.fromPartial(base ?? ({} as any)); return HealthCheckRequest.fromPartial(base ?? {});
}, },
fromPartial<I extends Exact<DeepPartial<HealthCheckRequest>, I>>(object: I): HealthCheckRequest { fromPartial<I extends Exact<DeepPartial<HealthCheckRequest>, I>>(object: I): HealthCheckRequest {
const message = createBaseHealthCheckRequest(); const message = createBaseHealthCheckRequest();
message.service = object.service ?? ""; message.service = object.service ?? "";
@@ -138,22 +130,22 @@ function createBaseHealthCheckResponse(): HealthCheckResponse {
return { status: 0 }; return { status: 0 };
} }
export const HealthCheckResponse: MessageFns<HealthCheckResponse> = { export const HealthCheckResponse = {
encode(message: HealthCheckResponse, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { encode(message: HealthCheckResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.status !== 0) { if (message.status !== 0) {
writer.uint32(8).int32(message.status); writer.uint32(8).int32(message.status);
} }
return writer; return writer;
}, },
decode(input: BinaryReader | Uint8Array, length?: number): HealthCheckResponse { decode(input: _m0.Reader | Uint8Array, length?: number): HealthCheckResponse {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input); const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length; let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseHealthCheckResponse(); const message = createBaseHealthCheckResponse();
while (reader.pos < end) { while (reader.pos < end) {
const tag = reader.uint32(); const tag = reader.uint32();
switch (tag >>> 3) { switch (tag >>> 3) {
case 1: { case 1:
if (tag !== 8) { if (tag !== 8) {
break; break;
} }
@@ -161,11 +153,10 @@ export const HealthCheckResponse: MessageFns<HealthCheckResponse> = {
message.status = reader.int32() as any; message.status = reader.int32() as any;
continue; continue;
} }
}
if ((tag & 7) === 4 || tag === 0) { if ((tag & 7) === 4 || tag === 0) {
break; break;
} }
reader.skip(tag & 7); reader.skipType(tag & 7);
} }
return message; return message;
}, },
@@ -176,15 +167,14 @@ export const HealthCheckResponse: MessageFns<HealthCheckResponse> = {
toJSON(message: HealthCheckResponse): unknown { toJSON(message: HealthCheckResponse): unknown {
const obj: any = {}; const obj: any = {};
if (message.status !== 0) { message.status !== undefined && (obj.status = healthCheckResponse_ServingStatusToJSON(message.status));
obj.status = healthCheckResponse_ServingStatusToJSON(message.status);
}
return obj; return obj;
}, },
create<I extends Exact<DeepPartial<HealthCheckResponse>, I>>(base?: I): HealthCheckResponse { create<I extends Exact<DeepPartial<HealthCheckResponse>, I>>(base?: I): HealthCheckResponse {
return HealthCheckResponse.fromPartial(base ?? ({} as any)); return HealthCheckResponse.fromPartial(base ?? {});
}, },
fromPartial<I extends Exact<DeepPartial<HealthCheckResponse>, I>>(object: I): HealthCheckResponse { fromPartial<I extends Exact<DeepPartial<HealthCheckResponse>, I>>(object: I): HealthCheckResponse {
const message = createBaseHealthCheckResponse(); const message = createBaseHealthCheckResponse();
message.status = object.status ?? 0; message.status = object.status ?? 0;
@@ -246,14 +236,12 @@ export interface HealthClient extends Client {
export const HealthClient = makeGenericClientConstructor(HealthService, "com.ql.health.Health") as unknown as { export const HealthClient = makeGenericClientConstructor(HealthService, "com.ql.health.Health") as unknown as {
new (address: string, credentials: ChannelCredentials, options?: Partial<ClientOptions>): HealthClient; new (address: string, credentials: ChannelCredentials, options?: Partial<ClientOptions>): HealthClient;
service: typeof HealthService; service: typeof HealthService;
serviceName: string;
}; };
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
export type DeepPartial<T> = T extends Builtin ? T export type DeepPartial<T> = T extends Builtin ? T
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> } : T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>; : Partial<T>;
@@ -264,12 +252,3 @@ export type Exact<P, I extends P> = P extends Builtin ? P
function isSet(value: any): boolean { function isSet(value: any): boolean {
return value !== null && value !== undefined; return value !== null && value !== undefined;
} }
export interface MessageFns<T> {
encode(message: T, writer?: BinaryWriter): BinaryWriter;
decode(input: BinaryReader | Uint8Array, length?: number): T;
fromJSON(object: any): T;
toJSON(message: T): unknown;
create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
}
+36
View File
@@ -0,0 +1,36 @@
import express from 'express';
import Logger from './loaders/logger';
import config from './config';
import { HealthClient } from './protos/health';
import { credentials } from '@grpc/grpc-js';
const app = express();
const client = new HealthClient(
`0.0.0.0:${config.cronPort}`,
credentials.createInsecure(),
{ 'grpc.enable_http_proxy': 0 },
);
app.get('/api/health', (req, res) => {
client.check({ service: 'cron' }, (err, response) => {
if (err) {
return res.status(200).send({ code: 500, error: err });
}
return res.status(200).send({ code: 200, data: response });
});
});
app
.listen(config.publicPort, '0.0.0.0', async () => {
await require('./loaders/sentry').default({ expressApp: app });
await require('./loaders/db').default();
Logger.debug(`✌️ 公共服务启动成功!`);
console.debug(`✌️ 公共服务启动成功!`);
process.send?.('ready');
})
.on('error', (err) => {
Logger.error(err);
console.error(err);
process.exit(1);
});
+10 -10
View File
@@ -10,41 +10,41 @@ const addCron = (
callback: sendUnaryData<AddCronResponse>, callback: sendUnaryData<AddCronResponse>,
) => { ) => {
for (const item of call.request.crons) { for (const item of call.request.crons) {
const { id, schedule, command, extra_schedules, name } = item; const { id, schedule, command, extraSchedules, name } = item;
if (scheduleStacks.has(id)) { if (scheduleStacks.has(id)) {
scheduleStacks.get(id)?.forEach((x) => x.cancel()); scheduleStacks.get(id)?.forEach((x) => x.cancel());
} }
Logger.info( Logger.info(
'[schedule][创建定时任务] 任务ID: %s, 名称: %s, cron: %s, 执行命令: %s', '[schedule][创建定时任务], 任务ID: %s, 名称: %s, cron: %s, 执行命令: %s',
id, id,
name, name,
schedule, schedule,
command, command,
); );
if (extra_schedules?.length) { if (extraSchedules?.length) {
extra_schedules.forEach((x) => { extraSchedules.forEach(x => {
Logger.info( Logger.info(
'[schedule][创建定时任务] 任务ID: %s, 名称: %s, cron: %s, 执行命令: %s', '[schedule][创建定时任务], 任务ID: %s, 名称: %s, cron: %s, 执行命令: %s',
id, id,
name, name,
x.schedule, x.schedule,
command, command,
); );
}); })
} }
scheduleStacks.set(id, [ scheduleStacks.set(id, [
nodeSchedule.scheduleJob(id, schedule, async () => { nodeSchedule.scheduleJob(id, schedule, async () => {
Logger.info(`[schedule][准备运行任务] 命令: ${command}`); Logger.info(`[schedule][准备运行任务] 命令: ${command}`);
runCron(command, item); runCron(command, { name, schedule, extraSchedules });
}), }),
...(extra_schedules?.length ...(extraSchedules?.length
? extra_schedules.map((x) => ? extraSchedules.map((x) =>
nodeSchedule.scheduleJob(id, x.schedule, async () => { nodeSchedule.scheduleJob(id, x.schedule, async () => {
Logger.info(`[schedule][准备运行任务] 命令: ${command}`); Logger.info(`[schedule][准备运行任务] 命令: ${command}`);
runCron(command, item); runCron(command, { name, schedule, extraSchedules });
}), }),
) )
: []), : []),
-444
View File
@@ -1,444 +0,0 @@
import 'reflect-metadata';
import { Container } from 'typedi';
import EnvService from '../services/env';
import { sendUnaryData, ServerUnaryCall } from '@grpc/grpc-js';
import {
CreateEnvRequest,
CronItem,
DeleteEnvsRequest,
DisableEnvsRequest,
EnableEnvsRequest,
EnvItem,
EnvResponse,
EnvsResponse,
GetEnvByIdRequest,
GetEnvsRequest,
MoveEnvRequest,
Response,
SystemNotifyRequest,
UpdateEnvNamesRequest,
UpdateEnvRequest,
} from '../protos/api';
import LoggerInstance from '../loaders/logger';
import pick from 'lodash/pick';
import SystemService from '../services/system';
import CronService from '../services/cron';
import {
CronDetailRequest,
CronDetailResponse,
CreateCronRequest,
UpdateCronRequest,
DeleteCronsRequest,
CronResponse,
GetCronsRequest,
CronsResponse,
GetCronByIdRequest,
EnableCronsRequest,
DisableCronsRequest,
RunCronsRequest,
} from '../protos/api';
import { NotificationInfo } from '../data/notify';
Container.set('logger', LoggerInstance);
export const getEnvs = async (
call: ServerUnaryCall<GetEnvsRequest, EnvsResponse>,
callback: sendUnaryData<EnvsResponse>,
) => {
try {
const envService = Container.get(EnvService);
const data = await envService.envs(call.request.searchValue);
callback(null, {
code: 200,
data: data.map((x) => ({ ...x, remarks: x.remarks || '' })),
});
} catch (e: any) {
callback(null, {
code: 500,
data: [],
message: e.message,
});
}
};
export const createEnv = async (
call: ServerUnaryCall<CreateEnvRequest, EnvsResponse>,
callback: sendUnaryData<EnvsResponse>,
) => {
try {
const envService = Container.get(EnvService);
const data = await envService.create(call.request.envs);
callback(null, { code: 200, data });
} catch (e: any) {
callback(e);
}
};
export const updateEnv = async (
call: ServerUnaryCall<UpdateEnvRequest, EnvResponse>,
callback: sendUnaryData<EnvResponse>,
) => {
try {
if (!call.request.env?.id) {
return callback(null, {
code: 400,
data: undefined,
message: 'id parameter is required',
});
}
const envService = Container.get(EnvService);
const data = await envService.update(
pick(call.request.env, ['id', 'name', 'value', 'remarks']) as EnvItem,
);
callback(null, { code: 200, data });
} catch (e: any) {
callback(e);
}
};
export const deleteEnvs = async (
call: ServerUnaryCall<DeleteEnvsRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
if (!call.request.ids || call.request.ids.length === 0) {
return callback(null, {
code: 400,
message: 'ids parameter is required',
});
}
const envService = Container.get(EnvService);
await envService.remove(call.request.ids);
callback(null, { code: 200 });
} catch (e: any) {
callback(e);
}
};
export const moveEnv = async (
call: ServerUnaryCall<MoveEnvRequest, EnvResponse>,
callback: sendUnaryData<EnvResponse>,
) => {
try {
if (!call.request.id) {
return callback(null, {
code: 400,
data: undefined,
message: 'id parameter is required',
});
}
const envService = Container.get(EnvService);
const data = await envService.move(call.request.id, {
fromIndex: call.request.fromIndex,
toIndex: call.request.toIndex,
});
callback(null, { code: 200, data });
} catch (e: any) {
callback(e);
}
};
export const disableEnvs = async (
call: ServerUnaryCall<DisableEnvsRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
if (!call.request.ids || call.request.ids.length === 0) {
return callback(null, {
code: 400,
message: 'ids parameter is required',
});
}
const envService = Container.get(EnvService);
await envService.disabled(call.request.ids);
callback(null, { code: 200 });
} catch (e: any) {
callback(e);
}
};
export const enableEnvs = async (
call: ServerUnaryCall<EnableEnvsRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
if (!call.request.ids || call.request.ids.length === 0) {
return callback(null, {
code: 400,
message: 'ids parameter is required',
});
}
const envService = Container.get(EnvService);
await envService.enabled(call.request.ids);
callback(null, { code: 200 });
} catch (e: any) {
callback(e);
}
};
export const updateEnvNames = async (
call: ServerUnaryCall<UpdateEnvNamesRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
if (!call.request.ids || call.request.ids.length === 0) {
return callback(null, {
code: 400,
message: 'ids parameter is required',
});
}
const envService = Container.get(EnvService);
await envService.updateNames({
ids: call.request.ids,
name: call.request.name,
});
callback(null, { code: 200 });
} catch (e: any) {
callback(e);
}
};
export const getEnvById = async (
call: ServerUnaryCall<GetEnvByIdRequest, EnvResponse>,
callback: sendUnaryData<EnvResponse>,
) => {
try {
if (!call.request.id) {
return callback(null, {
code: 400,
data: undefined,
message: 'id parameter is required',
});
}
const envService = Container.get(EnvService);
const data = await envService.getDb({ id: call.request.id });
callback(null, {
code: 200,
data: { ...data, remarks: data.remarks || '' },
});
} catch (e: any) {
callback(e);
}
};
export const systemNotify = async (
call: ServerUnaryCall<SystemNotifyRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
const systemService = Container.get(SystemService);
const data = await systemService.notify({
title: call.request.title,
content: call.request.content,
notificationInfo: call.request.notificationInfo as unknown as NotificationInfo,
});
callback(null, data);
} catch (e: any) {
callback(e);
}
};
const normalizeCronData = (data: CronItem | null): CronItem | undefined => {
if (!data) return undefined;
return {
...data,
sub_id: data.sub_id ?? undefined,
extra_schedules: data.extra_schedules ?? undefined,
pid: data.pid ?? undefined,
task_before: data.task_before ?? undefined,
task_after: data.task_after ?? undefined,
};
};
export const getCronDetail = async (
call: ServerUnaryCall<CronDetailRequest, CronDetailResponse>,
callback: sendUnaryData<CronDetailResponse>,
) => {
try {
if (!call.request.log_path) {
return callback(null, {
code: 400,
data: undefined,
message: 'log_path is required',
});
}
const cronService = Container.get(CronService);
const data = (await cronService.find({
log_path: call.request.log_path,
})) as CronItem;
callback(null, { code: 200, data: normalizeCronData(data) });
} catch (e: any) {
callback(e);
}
};
export const createCron = async (
call: ServerUnaryCall<CreateCronRequest, CronResponse>,
callback: sendUnaryData<CronResponse>,
) => {
try {
const cronService = Container.get(CronService);
const data = (await cronService.create(call.request)) as CronItem;
callback(null, { code: 200, data: normalizeCronData(data) });
} catch (e: any) {
callback(e);
}
};
export const updateCron = async (
call: ServerUnaryCall<UpdateCronRequest, CronResponse>,
callback: sendUnaryData<CronResponse>,
) => {
try {
const cronService = Container.get(CronService);
const { id, ...fields } = call.request;
const updateRequest = {
id,
...Object.entries(fields).reduce((acc: any, [key, value]) => {
if (value !== undefined) {
acc[key] = value;
}
return acc;
}, {}),
} as UpdateCronRequest;
const data = (await cronService.update(updateRequest)) as CronItem;
callback(null, { code: 200, data: normalizeCronData(data) });
} catch (e: any) {
callback(e);
}
};
export const deleteCrons = async (
call: ServerUnaryCall<DeleteCronsRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
const cronService = Container.get(CronService);
await cronService.remove(call.request.ids);
callback(null, { code: 200 });
} catch (e: any) {
callback(e);
}
};
export const getCrons = async (
call: ServerUnaryCall<GetCronsRequest, CronsResponse>,
callback: sendUnaryData<CronsResponse>,
) => {
try {
const cronService = Container.get(CronService);
const result = await cronService.crontabs({
searchValue: call.request.searchValue || '',
page: '0',
size: '0',
sorter: '',
filters: '',
queryString: '',
});
const data = result.data.map((x) => normalizeCronData(x as CronItem));
callback(null, {
code: 200,
data: data.filter((x): x is CronItem => x !== undefined),
});
} catch (e: any) {
callback(null, {
code: 500,
data: [],
message: e.message,
});
}
};
export const getCronById = async (
call: ServerUnaryCall<GetCronByIdRequest, CronResponse>,
callback: sendUnaryData<CronResponse>,
) => {
try {
if (!call.request.id) {
return callback(null, {
code: 400,
data: undefined,
message: 'id parameter is required',
});
}
const cronService = Container.get(CronService);
const data = (await cronService.getDb({ id: call.request.id })) as CronItem;
callback(null, { code: 200, data: normalizeCronData(data) });
} catch (e: any) {
callback(null, {
code: 404,
data: undefined,
message: e.message,
});
}
};
export const enableCrons = async (
call: ServerUnaryCall<EnableCronsRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
if (!call.request.ids || call.request.ids.length === 0) {
return callback(null, {
code: 400,
message: 'ids parameter is required',
});
}
const cronService = Container.get(CronService);
await cronService.enabled(call.request.ids);
callback(null, { code: 200 });
} catch (e: any) {
callback(e);
}
};
export const disableCrons = async (
call: ServerUnaryCall<DisableCronsRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
if (!call.request.ids || call.request.ids.length === 0) {
return callback(null, {
code: 400,
message: 'ids parameter is required',
});
}
const cronService = Container.get(CronService);
await cronService.disabled(call.request.ids);
callback(null, { code: 200 });
} catch (e: any) {
callback(e);
}
};
export const runCrons = async (
call: ServerUnaryCall<RunCronsRequest, Response>,
callback: sendUnaryData<Response>,
) => {
try {
if (!call.request.ids || call.request.ids.length === 0) {
return callback(null, {
code: 400,
message: 'ids parameter is required',
});
}
const cronService = Container.get(CronService);
await cronService.run(call.request.ids);
callback(null, { code: 200 });
} catch (e: any) {
callback(e);
}
};
+1 -1
View File
@@ -10,7 +10,7 @@ import config from '../config';
class Client { class Client {
private client = new CronClient( private client = new CronClient(
`0.0.0.0:${config.grpcPort}`, `0.0.0.0:${config.cronPort}`,
credentials.createInsecure(), credentials.createInsecure(),
{ 'grpc.enable_http_proxy': 0 }, { 'grpc.enable_http_proxy': 0 },
); );
+1 -1
View File
@@ -10,7 +10,7 @@ const delCron = (
for (const id of call.request.ids) { for (const id of call.request.ids) {
if (scheduleStacks.has(id)) { if (scheduleStacks.has(id)) {
Logger.info( Logger.info(
'[schedule][取消定时任务] 任务ID: %s', '[schedule][取消定时任务], 任务ID: %s',
id, id,
); );
scheduleStacks.get(id)?.forEach(x => x.cancel()); scheduleStacks.get(id)?.forEach(x => x.cancel());
+6 -3
View File
@@ -17,11 +17,14 @@ const check = async (
return callback(null, { status: 1 }); return callback(null, { status: 1 });
} }
const qinglongErrLog = await promiseExec( const panelErrLog = await promiseExec(
`tail -n 300 ~/.pm2/logs/qinglong-error.log`, `tail -n 300 ~/.pm2/logs/panel-error.log`,
);
const scheduleErrLog = await promiseExec(
`tail -n 300 ~/.pm2/logs/schedule-error.log`,
); );
return callback( return callback(
new Error(`${qinglongErrLog || ''}\n${res}`.trim()), new Error(`${scheduleErrLog || ''}\n${panelErrLog || ''}\n${res}`.trim()),
); );
default: default:
+25
View File
@@ -0,0 +1,25 @@
import { Server, ServerCredentials } from '@grpc/grpc-js';
import { CronService } from '../protos/cron';
import { addCron } from './addCron';
import { delCron } from './delCron';
import { HealthService } from '../protos/health';
import { check } from './health';
import config from '../config';
import Logger from '../loaders/logger';
const server = new Server({ 'grpc.enable_http_proxy': 0 });
server.addService(HealthService, { check });
server.addService(CronService, { addCron, delCron });
server.bindAsync(
`0.0.0.0:${config.cronPort}`,
ServerCredentials.createInsecure(),
(err, port) => {
if (err) {
throw err;
}
server.start();
Logger.debug(`✌️ 定时服务启动成功!`);
console.debug(`✌️ 定时服务启动成功!`);
process.send?.('ready');
},
);
+5 -17
View File
@@ -3,7 +3,7 @@ import path, { join } from 'path';
import config from '../config'; import config from '../config';
import { getFileContentByName } from '../config/util'; import { getFileContentByName } from '../config/util';
import { Response } from 'express'; import { Response } from 'express';
import { request } from 'undici'; import got from 'got';
@Service() @Service()
export default class ConfigService { export default class ConfigService {
@@ -11,26 +11,14 @@ export default class ConfigService {
public async getFile(filePath: string, res: Response) { public async getFile(filePath: string, res: Response) {
let content = ''; let content = '';
const avaliablePath = [config.rootPath, config.configPath].map((x) => if (config.blackFileList.includes(filePath) || !filePath) {
path.resolve(x, filePath), res.send({ code: 403, message: '文件无法访问' });
);
if (
config.blackFileList.includes(filePath) ||
avaliablePath.every(
(x) =>
!x.startsWith(config.scriptPath) && !x.startsWith(config.configPath),
) ||
!filePath
) {
return res.send({ code: 403, message: '文件无法访问' });
} }
if (filePath.startsWith('sample/')) { if (filePath.startsWith('sample/')) {
const res = await request( const res = await got.get(
`https://gitlab.com/whyour/qinglong/-/raw/master/${filePath}`, `https://gitlab.com/whyour/qinglong/-/raw/master/${filePath}`,
); );
content = await res.body.text(); content = res.body;
} else if (filePath.startsWith('data/scripts/')) { } else if (filePath.startsWith('data/scripts/')) {
content = await getFileContentByName(join(config.rootPath, filePath)); content = await getFileContentByName(join(config.rootPath, filePath));
} else { } else {
+56 -193
View File
@@ -4,17 +4,15 @@ import config from '../config';
import { Crontab, CrontabModel, CrontabStatus } from '../data/cron'; import { Crontab, CrontabModel, CrontabStatus } from '../data/cron';
import { exec, execSync } from 'child_process'; import { exec, execSync } from 'child_process';
import fs from 'fs/promises'; import fs from 'fs/promises';
import CronExpressionParser from 'cron-parser'; import cron_parser from 'cron-parser';
import { import {
getFileContentByName, getFileContentByName,
fileExist, fileExist,
killTask, killTask,
killAllTasks,
getUniqPath, getUniqPath,
safeJSONParse, safeJSONParse,
isDemoEnv,
} from '../config/util'; } from '../config/util';
import { Op, where, col as colFn, FindOptions, fn, Order } from 'sequelize'; import { Op, where, col as colFn, FindOptions, fn } from 'sequelize';
import path from 'path'; import path from 'path';
import { TASK_PREFIX, QL_PREFIX } from '../config/const'; import { TASK_PREFIX, QL_PREFIX } from '../config/const';
import cronClient from '../schedule/client'; import cronClient from '../schedule/client';
@@ -23,9 +21,6 @@ import { spawn } from 'cross-spawn';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import pickBy from 'lodash/pickBy'; import pickBy from 'lodash/pickBy';
import omit from 'lodash/omit'; import omit from 'lodash/omit';
import { writeFileWithLock } from '../shared/utils';
import { ScheduleType } from '../interface/schedule';
import { logStreamManager } from '../shared/logStreamManager';
@Service() @Service()
export default class CronService { export default class CronService {
@@ -39,60 +34,22 @@ export default class CronService {
return false; return false;
} }
private isOnceSchedule(schedule?: string) {
return schedule?.startsWith(ScheduleType.ONCE);
}
private isBootSchedule(schedule?: string) {
return schedule?.startsWith(ScheduleType.BOOT);
}
private isSpecialSchedule(schedule?: string) {
return this.isOnceSchedule(schedule) || this.isBootSchedule(schedule);
}
private async getLogName(cron: Crontab) {
const { log_name, command, id } = cron;
if (log_name === '/dev/null') {
return log_name;
}
let uniqPath = await getUniqPath(command, `${id}`);
if (log_name) {
const normalizedLogName = log_name.startsWith('/')
? log_name
: path.join(config.logPath, log_name);
if (normalizedLogName.startsWith(config.logPath)) {
uniqPath = log_name;
}
}
const logDirPath = path.resolve(config.logPath, `${uniqPath}`);
await fs.mkdir(logDirPath, { recursive: true });
return uniqPath;
}
public async create(payload: Crontab): Promise<Crontab> { public async create(payload: Crontab): Promise<Crontab> {
const tab = new Crontab(payload); const tab = new Crontab(payload);
tab.saved = false; tab.saved = false;
tab.log_name = await this.getLogName(tab);
const doc = await this.insert(tab); const doc = await this.insert(tab);
if (this.isNodeCron(doc)) {
if (isDemoEnv()) {
return doc;
}
if (this.isNodeCron(doc) && !this.isSpecialSchedule(doc.schedule)) {
await cronClient.addCron([ await cronClient.addCron([
{ {
name: doc.name || '', name: doc.name || '',
id: String(doc.id), id: String(doc.id),
schedule: doc.schedule!, schedule: doc.schedule!,
command: this.makeCommand(doc), command: this.makeCommand(doc),
extra_schedules: doc.extra_schedules || [], extraSchedules: doc.extra_schedules || [],
}, },
]); ]);
} }
await this.set_crontab();
await this.setCrontab();
return doc; return doc;
} }
@@ -100,34 +57,29 @@ export default class CronService {
return await CrontabModel.create(payload, { returning: true }); return await CrontabModel.create(payload, { returning: true });
} }
public async update(payload: Partial<Crontab>): Promise<Crontab> { public async update(payload: Crontab): Promise<Crontab> {
const doc = await this.getDb({ id: payload.id }); const doc = await this.getDb({ id: payload.id });
const tab = new Crontab({ ...doc, ...payload }); const tab = new Crontab({ ...doc, ...payload });
tab.saved = false; tab.saved = false;
tab.log_name = await this.getLogName(tab);
const newDoc = await this.updateDb(tab); const newDoc = await this.updateDb(tab);
if (doc.isDisabled === 1) {
if (doc.isDisabled === 1 || isDemoEnv()) {
return newDoc; return newDoc;
} }
if (this.isNodeCron(doc)) { if (this.isNodeCron(doc)) {
await cronClient.delCron([String(doc.id)]); await cronClient.delCron([String(doc.id)]);
} }
if (this.isNodeCron(newDoc)) {
if (this.isNodeCron(newDoc) && !this.isSpecialSchedule(newDoc.schedule)) {
await cronClient.addCron([ await cronClient.addCron([
{ {
name: doc.name || '', name: doc.name || '',
id: String(newDoc.id), id: String(newDoc.id),
schedule: newDoc.schedule!, schedule: newDoc.schedule!,
command: this.makeCommand(newDoc), command: this.makeCommand(newDoc),
extra_schedules: newDoc.extra_schedules || [], extraSchedules: newDoc.extra_schedules || [],
}, },
]); ]);
} }
await this.set_crontab();
await this.setCrontab();
return newDoc; return newDoc;
} }
@@ -182,7 +134,7 @@ export default class CronService {
public async remove(ids: number[]) { public async remove(ids: number[]) {
await CrontabModel.destroy({ where: { id: ids } }); await CrontabModel.destroy({ where: { id: ids } });
await cronClient.delCron(ids.map(String)); await cronClient.delCron(ids.map(String));
await this.setCrontab(); await this.set_crontab();
} }
public async pin(ids: number[]) { public async pin(ids: number[]) {
@@ -226,8 +178,8 @@ export default class CronService {
for (const col of viewQuery.filters) { for (const col of viewQuery.filters) {
const { property, value, operation } = col; const { property, value, operation } = col;
let q: any = {}; let q: any = {};
let operate2: any = null; let operate2 = null;
let operate: any = null; let operate = null;
switch (operation) { switch (operation) {
case 'Reg': case 'Reg':
operate = Op.like; operate = Op.like;
@@ -238,39 +190,22 @@ export default class CronService {
operate2 = Op.and; operate2 = Op.and;
break; break;
case 'In': case 'In':
if (
property === 'status' &&
!value.includes(CrontabStatus.disabled)
) {
q[Op.and] = [
{ [property]: Array.isArray(value) ? value : [value] },
{ isDisabled: 0 },
];
} else {
q[Op.or] = [ q[Op.or] = [
{ {
[property]: Array.isArray(value) ? value : [value], [property]: Array.isArray(value) ? value : [value],
}, },
property === 'status' && value.includes(CrontabStatus.disabled) property === 'status' && value.includes(2)
? { isDisabled: 1 } ? { isDisabled: 1 }
: {}, : {},
]; ];
}
break; break;
case 'Nin': case 'Nin':
q[Op.and] = [ q[Op.and] = [
{
[Op.or]: [
{ {
[property]: { [property]: {
[Op.notIn]: Array.isArray(value) ? value : [value], [Op.notIn]: Array.isArray(value) ? value : [value],
}, },
}, },
{
[property]: { [Op.is]: null },
},
],
},
property === 'status' && value.includes(2) property === 'status' && value.includes(2)
? { isDisabled: { [Op.ne]: 1 } } ? { isDisabled: { [Op.ne]: 1 } }
: {}, : {},
@@ -384,10 +319,10 @@ export default class CronService {
log_path, log_path,
}: { }: {
log_path: string; log_path: string;
}): Promise<Crontab | undefined> { }): Promise<Crontab | null> {
try { try {
const result = await CrontabModel.findOne({ where: { log_path } }); const result = await CrontabModel.findOne({ where: { log_path } });
return result?.get({ plain: true }); return result;
} catch (error) { } catch (error) {
throw error; throw error;
} }
@@ -427,9 +362,9 @@ export default class CronService {
order.unshift([field, type]); order.unshift([field, type]);
} }
} }
let condition: FindOptions<Crontab> = { let condition: any = {
where: query, where: query,
order: order as Order, order: order,
}; };
if (page && size) { if (page && size) {
condition.offset = (page - 1) * size; condition.offset = (page - 1) * size;
@@ -438,7 +373,7 @@ export default class CronService {
try { try {
const result = await CrontabModel.findAll(condition); const result = await CrontabModel.findAll(condition);
const count = await CrontabModel.count({ where: query }); const count = await CrontabModel.count({ where: query });
return { data: result.map((x) => x.get({ plain: true })), total: count }; return { data: result, total: count };
} catch (error) { } catch (error) {
throw error; throw error;
} }
@@ -465,17 +400,12 @@ export default class CronService {
public async stop(ids: number[]) { public async stop(ids: number[]) {
const docs = await CrontabModel.findAll({ where: { id: ids } }); const docs = await CrontabModel.findAll({ where: { id: ids } });
for (const doc of docs) { for (const doc of docs) {
// Kill all running instances of this task if (doc.pid) {
try { try {
const command = this.makeCommand(doc); await killTask(doc.pid);
await killAllTasks(command);
this.logger.info(
`[panel][停止所有运行中的任务实例] 任务ID: ${doc.id}, 命令: ${command}`,
);
} catch (error) { } catch (error) {
this.logger.error( this.logger.error(error);
`[panel][停止任务失败] 任务ID: ${doc.id}, 错误: ${error}`, }
);
} }
} }
@@ -493,7 +423,7 @@ export default class CronService {
name: cron.name, name: cron.name,
command: cron.command, command: cron.command,
schedule: cron.schedule, schedule: cron.schedule,
extra_schedules: cron.extra_schedules, extraSchedules: cron.extra_schedules,
}; };
if (cron.status !== CrontabStatus.queued) { if (cron.status !== CrontabStatus.queued) {
resolve(params); resolve(params);
@@ -501,18 +431,16 @@ export default class CronService {
} }
this.logger.info( this.logger.info(
`[panel][开始执行任务] 参数: ${JSON.stringify(params)}`, `[panel][开始执行任务] 参数 ${JSON.stringify(params)}`,
); );
let { id, command, log_name } = cron; let { id, command, log_path } = cron;
const uniqPath = await getUniqPath(command, `${id}`);
const uniqPath =
log_name === '/dev/null' || !log_name
? await getUniqPath(command, `${id}`)
: log_name;
const logTime = dayjs().format('YYYY-MM-DD-HH-mm-ss-SSS'); const logTime = dayjs().format('YYYY-MM-DD-HH-mm-ss-SSS');
const logDirPath = path.resolve(config.logPath, `${uniqPath}`); const logDirPath = path.resolve(config.logPath, `${uniqPath}`);
if (log_path?.split('/')?.every((x) => x !== uniqPath)) {
await fs.mkdir(logDirPath, { recursive: true }); await fs.mkdir(logDirPath, { recursive: true });
}
const logPath = `${uniqPath}/${logTime}.log`; const logPath = `${uniqPath}/${logTime}.log`;
const absolutePath = path.resolve(config.logPath, `${logPath}`); const absolutePath = path.resolve(config.logPath, `${logPath}`);
const cp = spawn( const cp = spawn(
@@ -528,33 +456,16 @@ export default class CronService {
{ where: { id } }, { where: { id } },
); );
cp.stdout.on('data', async (data) => { cp.stdout.on('data', async (data) => {
await logStreamManager.write(absolutePath, data.toString()); await fs.appendFile(absolutePath, data.toString());
}); });
cp.stderr.on('data', async (data) => { cp.stderr.on('data', async (data) => {
this.logger.info( await fs.appendFile(absolutePath, data.toString());
'[panel][执行任务失败] 命令: %s, 错误信息: %j',
command,
data.toString(),
);
await logStreamManager.write(absolutePath, data.toString());
}); });
cp.on('error', async (err) => { cp.on('error', async (err) => {
this.logger.error( await fs.appendFile(absolutePath, JSON.stringify(err));
'[panel][创建任务失败] 命令: %s, 错误信息: %j',
command,
err,
);
await logStreamManager.write(absolutePath, JSON.stringify(err));
}); });
cp.on('exit', async (code) => { cp.on('exit', async (code) => {
this.logger.info(
'[panel][执行任务结束] 参数: %s, 退出码: %j',
JSON.stringify(params),
code,
);
// Close the stream after task completion
await logStreamManager.closeStream(absolutePath);
await CrontabModel.update( await CrontabModel.update(
{ status: CrontabStatus.idle, pid: undefined }, { status: CrontabStatus.idle, pid: undefined },
{ where: { id } }, { where: { id } },
@@ -568,27 +479,23 @@ export default class CronService {
public async disabled(ids: number[]) { public async disabled(ids: number[]) {
await CrontabModel.update({ isDisabled: 1 }, { where: { id: ids } }); await CrontabModel.update({ isDisabled: 1 }, { where: { id: ids } });
await cronClient.delCron(ids.map(String)); await cronClient.delCron(ids.map(String));
await this.setCrontab(); await this.set_crontab();
} }
public async enabled(ids: number[]) { public async enabled(ids: number[]) {
await CrontabModel.update({ isDisabled: 0 }, { where: { id: ids } }); await CrontabModel.update({ isDisabled: 0 }, { where: { id: ids } });
const docs = await CrontabModel.findAll({ where: { id: ids } }); const docs = await CrontabModel.findAll({ where: { id: ids } });
const sixCron = docs const sixCron = docs
.filter((x) => this.isNodeCron(x) && !this.isSpecialSchedule(x.schedule)) .filter((x) => this.isNodeCron(x))
.map((doc) => ({ .map((doc) => ({
name: doc.name || '', name: doc.name || '',
id: String(doc.id), id: String(doc.id),
schedule: doc.schedule!, schedule: doc.schedule!,
command: this.makeCommand(doc), command: this.makeCommand(doc),
extra_schedules: doc.extra_schedules || [], extraSchedules: doc.extra_schedules || [],
})); }));
if (isDemoEnv()) {
return;
}
await cronClient.addCron(sixCron); await cronClient.addCron(sixCron);
await this.setCrontab(); await this.set_crontab();
} }
public async log(id: number) { public async log(id: number) {
@@ -596,18 +503,13 @@ export default class CronService {
if (!doc) { if (!doc) {
return ''; return '';
} }
if (doc.log_name === '/dev/null') {
return '日志设置为忽略';
}
const absolutePath = path.resolve(config.logPath, `${doc.log_path}`); const absolutePath = path.resolve(config.logPath, `${doc.log_path}`);
const logFileExist = doc.log_path && (await fileExist(absolutePath)); const logFileExist = doc.log_path && (await fileExist(absolutePath));
if (logFileExist) { if (logFileExist) {
return await getFileContentByName(`${absolutePath}`); return await getFileContentByName(`${absolutePath}`);
} else { } else {
return typeof doc.status === 'number' && return '任务未运行';
[CrontabStatus.queued, CrontabStatus.running].includes(doc.status)
? '运行中...'
: '日志不存在...';
} }
} }
@@ -627,7 +529,7 @@ export default class CronService {
files.map(async (x) => ({ files.map(async (x) => ({
filename: x, filename: x,
directory: relativeDir.replace(config.logPath, ''), directory: relativeDir.replace(config.logPath, ''),
time: (await fs.lstat(`${dir}/${x}`)).birthtimeMs, time: (await fs.lstat(`${dir}/${x}`)).mtime.getTime(),
})), })),
) )
).sort((a, b) => b.time - a.time); ).sort((a, b) => b.time - a.time);
@@ -641,21 +543,17 @@ export default class CronService {
if (!command.startsWith(TASK_PREFIX) && !command.startsWith(QL_PREFIX)) { if (!command.startsWith(TASK_PREFIX) && !command.startsWith(QL_PREFIX)) {
command = `${TASK_PREFIX}${tab.command}`; command = `${TASK_PREFIX}${tab.command}`;
} }
let commandVariable = `real_time=${Boolean(realTime)} no_tee=true ID=${tab.id} `; let commandVariable = `real_time=${Boolean(realTime)} no_tee=true ID=${
// Only include log_name if it has a truthy value to avoid passing null/undefined to shell tab.id
if (tab.log_name) { } `;
commandVariable += `log_name=${tab.log_name} `;
}
if (tab.task_before) { if (tab.task_before) {
commandVariable += `task_before='${tab.task_before commandVariable += `task_before='${tab.task_before
.replace(/'/g, "'\\''") .replace(/'/g, "'\\''")
.replace(/;? *\n/g, ';')
.trim()}' `; .trim()}' `;
} }
if (tab.task_after) { if (tab.task_after) {
commandVariable += `task_after='${tab.task_after commandVariable += `task_after='${tab.task_after
.replace(/'/g, "'\\''") .replace(/'/g, "'\\''")
.replace(/;? *\n/g, ';')
.trim()}' `; .trim()}' `;
} }
@@ -663,14 +561,15 @@ export default class CronService {
return crontab_job_string; return crontab_job_string;
} }
private async setCrontab(data?: { data: Crontab[]; total: number }) { private async set_crontab(data?: { data: Crontab[]; total: number }) {
const tabs = data ?? (await this.crontabs()); const tabs = data ?? (await this.crontabs());
var crontab_string = ''; var crontab_string = '';
tabs.data.forEach((tab) => { tabs.data.forEach((tab) => {
const _schedule = tab.schedule && tab.schedule.split(/ +/);
if ( if (
tab.isDisabled === 1 || tab.isDisabled === 1 ||
this.isNodeCron(tab) || _schedule!.length !== 5 ||
this.isSpecialSchedule(tab.schedule) tab.extra_schedules?.length
) { ) {
crontab_string += '# '; crontab_string += '# ';
crontab_string += tab.schedule; crontab_string += tab.schedule;
@@ -685,25 +584,14 @@ export default class CronService {
} }
}); });
await writeFileWithLock(config.crontabFile, crontab_string); await fs.writeFile(config.crontabFile, crontab_string);
try {
execSync(`crontab ${config.crontabFile}`); execSync(`crontab ${config.crontabFile}`);
} catch (error: any) {
const errorMsg = error.message || String(error);
this.logger.error('[crontab] Failed to update system crontab:', errorMsg);
}
await CrontabModel.update({ saved: true }, { where: {} }); await CrontabModel.update({ saved: true }, { where: {} });
} }
public importCrontab() { public import_crontab() {
exec('crontab -l', (error, stdout) => { exec('crontab -l', (error, stdout, stderr) => {
if (error) {
const errorMsg = error.message || String(error);
this.logger.error('[crontab] Failed to read system crontab:', errorMsg);
}
const lines = stdout.split('\n'); const lines = stdout.split('\n');
const namePrefix = new Date().getTime(); const namePrefix = new Date().getTime();
@@ -717,7 +605,7 @@ export default class CronService {
if ( if (
command && command &&
schedule && schedule &&
CronExpressionParser.parse(schedule).hasNext() cron_parser.parseExpression(schedule).hasNext()
) { ) {
const name = namePrefix + '_' + index; const name = namePrefix + '_' + index;
@@ -738,42 +626,17 @@ export default class CronService {
public async autosave_crontab() { public async autosave_crontab() {
const tabs = await this.crontabs(); const tabs = await this.crontabs();
const regularCrons = tabs.data this.set_crontab(tabs);
.filter(
(x) => const sixCron = tabs.data
x.isDisabled !== 1 && .filter((x) => this.isNodeCron(x) && x.isDisabled !== 1)
this.isNodeCron(x) &&
!this.isSpecialSchedule(x.schedule),
)
.map((doc) => ({ .map((doc) => ({
name: doc.name || '', name: doc.name || '',
id: String(doc.id), id: String(doc.id),
schedule: doc.schedule!, schedule: doc.schedule!,
command: this.makeCommand(doc), command: this.makeCommand(doc),
extra_schedules: doc.extra_schedules || [], extraSchedules: doc.extra_schedules || [],
})); }));
await cronClient.addCron(sixCron);
if (isDemoEnv()) {
await writeFileWithLock(config.crontabFile, '');
return;
}
await cronClient.addCron(regularCrons);
this.setCrontab(tabs);
}
public async bootTask() {
const tabs = await this.crontabs();
const bootTasks = tabs.data.filter(
(x) => !x.isDisabled && this.isBootSchedule(x.schedule),
);
if (bootTasks.length > 0) {
await CrontabModel.update(
{ status: CrontabStatus.queued },
{ where: { id: bootTasks.map((t) => t.id!) } },
);
for (const task of bootTasks) {
await this.runSingle(task.id!);
}
}
} }
} }
+42 -35
View File
@@ -3,9 +3,12 @@ import winston from 'winston';
import config from '../config'; import config from '../config';
import { import {
Dependence, Dependence,
InstallDependenceCommandTypes,
DependenceStatus, DependenceStatus,
DependenceTypes, DependenceTypes,
unInstallDependenceCommandTypes,
DependenceModel, DependenceModel,
GetDependenceCommandTypes,
versionDependenceCommandTypes, versionDependenceCommandTypes,
} from '../data/dependence'; } from '../data/dependence';
import { spawn } from 'cross-spawn'; import { spawn } from 'cross-spawn';
@@ -16,9 +19,6 @@ import {
getPid, getPid,
killTask, killTask,
promiseExecSuccess, promiseExecSuccess,
getInstallCommand,
getUninstallCommand,
getGetCommand,
} from '../config/util'; } from '../config/util';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import taskLimit from '../shared/pLimit'; import taskLimit from '../shared/pLimit';
@@ -67,9 +67,6 @@ export default class DependenceService {
public async remove(ids: number[], force = false): Promise<Dependence[]> { public async remove(ids: number[], force = false): Promise<Dependence[]> {
const docs = await DependenceModel.findAll({ where: { id: ids } }); const docs = await DependenceModel.findAll({ where: { id: ids } });
for (const doc of docs) {
taskLimit.removeQueuedDependency(doc);
}
const unInstalledDeps = docs.filter( const unInstalledDeps = docs.filter(
(x) => x.status !== DependenceStatus.installed, (x) => x.status !== DependenceStatus.installed,
); );
@@ -98,32 +95,34 @@ export default class DependenceService {
searchValue, searchValue,
type, type,
status, status,
}: { }: { searchValue: string; type: string; status: string },
searchValue: string;
type: keyof typeof DependenceTypes;
status: string;
},
sort: any = [], sort: any = [],
query: any = {}, query: any = {},
): Promise<Dependence[]> { ): Promise<Dependence[]> {
let condition = query; let condition = {
if (type && DependenceTypes[type] !== undefined) { ...query,
condition.type = DependenceTypes[type]; type: DependenceTypes[type as any],
} };
if (status) { if (status) {
condition.status = status.split(',').map(Number); condition.status = status.split(',').map(Number);
} }
if (searchValue) { if (searchValue) {
const encodeText = encodeURI(searchValue); const encodeText = encodeURI(searchValue);
condition.name = { const reg = {
[Op.or]: [ [Op.or]: [
{ [Op.like]: `%${searchValue}%` }, { [Op.like]: `%${searchValue}%` },
{ [Op.like]: `%${encodeText}%` }, { [Op.like]: `%${encodeText}%` },
], ],
}; };
condition = {
...condition,
name: reg,
};
} }
try { try {
return await this.find(condition, sort); const result = await this.find(condition, sort);
return result as any;
} catch (error) { } catch (error) {
throw error; throw error;
} }
@@ -133,12 +132,10 @@ export default class DependenceService {
docs: Dependence[], docs: Dependence[],
isInstall: boolean = true, isInstall: boolean = true,
force: boolean = false, force: boolean = false,
): Promise<void> { ) {
docs.forEach((dep) => { docs.forEach((dep) => {
this.installOrUninstallDependency(dep, isInstall, force); this.installOrUninstallDependency(dep, isInstall, force);
}); });
return taskLimit.waitDependencyQueueDone();
} }
public async reInstall(ids: number[]): Promise<Dependence[]> { public async reInstall(ids: number[]): Promise<Dependence[]> {
@@ -148,9 +145,6 @@ export default class DependenceService {
); );
const docs = await DependenceModel.findAll({ where: { id: ids } }); const docs = await DependenceModel.findAll({ where: { id: ids } });
for (const doc of docs) {
taskLimit.removeQueuedDependency(doc);
}
this.installDependenceOneByOne(docs, true, true); this.installDependenceOneByOne(docs, true, true);
return docs; return docs;
} }
@@ -159,11 +153,13 @@ export default class DependenceService {
const docs = await DependenceModel.findAll({ where: { id: ids } }); const docs = await DependenceModel.findAll({ where: { id: ids } });
for (const doc of docs) { for (const doc of docs) {
taskLimit.removeQueuedDependency(doc); taskLimit.removeQueuedDependency(doc);
const depInstallCommand = getInstallCommand(doc.type, doc.name); const depInstallCommand = InstallDependenceCommandTypes[doc.type];
const depUnInstallCommand = getUninstallCommand(doc.type, doc.name); const depUnInstallCommand = unInstallDependenceCommandTypes[doc.type];
const installCmd = `${depInstallCommand} ${doc.name.trim()}`;
const unInstallCmd = `${depUnInstallCommand} ${doc.name.trim()}`;
const pids = await Promise.all([ const pids = await Promise.all([
getPid(depInstallCommand), getPid(installCmd),
getPid(depUnInstallCommand), getPid(unInstallCmd),
]); ]);
for (const pid of pids) { for (const pid of pids) {
pid && (await killTask(pid)); pid && (await killTask(pid));
@@ -230,9 +226,11 @@ export default class DependenceService {
? 'installDependence' ? 'installDependence'
: 'uninstallDependence'; : 'uninstallDependence';
let depName = dependency.name.trim(); let depName = dependency.name.trim();
const command = isInstall const depRunCommand = (
? getInstallCommand(dependency.type, depName) isInstall
: getUninstallCommand(dependency.type, depName); ? InstallDependenceCommandTypes
: unInstallDependenceCommandTypes
)[dependency.type];
const actionText = isInstall ? '安装' : '删除'; const actionText = isInstall ? '安装' : '删除';
const startTime = dayjs(); const startTime = dayjs();
@@ -248,7 +246,7 @@ export default class DependenceService {
// 判断是否已经安装过依赖 // 判断是否已经安装过依赖
if (isInstall && !force) { if (isInstall && !force) {
const getCommand = getGetCommand(dependency.type, depName); const getCommandPrefix = GetDependenceCommandTypes[dependency.type];
const depVersionStr = versionDependenceCommandTypes[dependency.type]; const depVersionStr = versionDependenceCommandTypes[dependency.type];
let depVersion = ''; let depVersion = '';
if (depName.includes(depVersionStr)) { if (depName.includes(depVersionStr)) {
@@ -265,7 +263,13 @@ export default class DependenceService {
const isLinuxDependence = dependency.type === DependenceTypes.linux; const isLinuxDependence = dependency.type === DependenceTypes.linux;
const isPythonDependence = const isPythonDependence =
dependency.type === DependenceTypes.python3; dependency.type === DependenceTypes.python3;
const depInfo = (await promiseExecSuccess(getCommand)) const depInfo = (
await promiseExecSuccess(
isNodeDependence
? `${getCommandPrefix} | grep "${depName}" | head -1`
: `${getCommandPrefix} ${depName}`,
)
)
.replace(/\s{2,}/, ' ') .replace(/\s{2,}/, ' ')
.replace(/\s+$/, ''); .replace(/\s+$/, '');
@@ -300,9 +304,12 @@ export default class DependenceService {
const proxyStr = dependenceProxyFileExist const proxyStr = dependenceProxyFileExist
? `source ${config.dependenceProxyFile} &&` ? `source ${config.dependenceProxyFile} &&`
: ''; : '';
const cp = spawn(`${proxyStr} ${command}`, { const cp = spawn(
`${proxyStr} ${depRunCommand} ${dependency.name.trim()}`,
{
shell: '/bin/bash', shell: '/bin/bash',
}); },
);
cp.stdout.on('data', async (data) => { cp.stdout.on('data', async (data) => {
this.sockService.sendMessage({ this.sockService.sendMessage({
@@ -346,7 +353,7 @@ export default class DependenceService {
}); });
this.updateLog(depIds, message); this.updateLog(depIds, message);
let status: number; let status = null;
if (isSucceed) { if (isSucceed) {
status = isInstall status = isInstall
? DependenceStatus.installed ? DependenceStatus.installed
+19 -33
View File
@@ -1,8 +1,7 @@
import groupBy from 'lodash/groupBy'; import { Service, Inject } from 'typedi';
import { FindOptions, Op } from 'sequelize';
import { Inject, Service } from 'typedi';
import winston from 'winston'; import winston from 'winston';
import config from '../config'; import config from '../config';
import * as fs from 'fs/promises';
import { import {
Env, Env,
EnvModel, EnvModel,
@@ -12,7 +11,8 @@ import {
minPosition, minPosition,
stepPosition, stepPosition,
} from '../data/env'; } from '../data/env';
import { writeFileWithLock } from '../shared/utils'; import groupBy from 'lodash/groupBy';
import { FindOptions, Op } from 'sequelize';
@Service() @Service()
export default class EnvService { export default class EnvService {
@@ -40,7 +40,7 @@ export default class EnvService {
} }
public async insert(payloads: Env[]): Promise<Env[]> { public async insert(payloads: Env[]): Promise<Env[]> {
const result: Env[] = []; const result = [];
for (const env of payloads) { for (const env of payloads) {
const doc = await EnvModel.create(env, { returning: true }); const doc = await EnvModel.create(env, { returning: true });
result.push(doc); result.push(doc);
@@ -61,7 +61,7 @@ export default class EnvService {
return await this.getDb({ id: payload.id }); return await this.getDb({ id: payload.id });
} }
public async remove(ids: number[]) { public async remove(ids: string[]) {
await EnvModel.destroy({ where: { id: ids } }); await EnvModel.destroy({ where: { id: ids } });
await this.set_envs(); await this.set_envs();
} }
@@ -146,11 +146,10 @@ export default class EnvService {
} }
try { try {
const result = await this.find(condition, [ const result = await this.find(condition, [
['isPinned', 'DESC'],
['position', 'DESC'], ['position', 'DESC'],
['createdAt', 'ASC'], ['createdAt', 'ASC'],
]); ]);
return result; return result as any;
} catch (error) { } catch (error) {
throw error; throw error;
} }
@@ -161,7 +160,7 @@ export default class EnvService {
where: { ...query }, where: { ...query },
order: [...sort], order: [...sort],
}); });
return docs.map((x) => x.get({ plain: true })); return docs;
} }
public async getDb(query: FindOptions<Env>['where']): Promise<Env> { public async getDb(query: FindOptions<Env>['where']): Promise<Env> {
@@ -172,7 +171,7 @@ export default class EnvService {
return doc.get({ plain: true }); return doc.get({ plain: true });
} }
public async disabled(ids: number[]) { public async disabled(ids: string[]) {
await EnvModel.update( await EnvModel.update(
{ status: EnvStatus.disabled }, { status: EnvStatus.disabled },
{ where: { id: ids } }, { where: { id: ids } },
@@ -180,24 +179,16 @@ export default class EnvService {
await this.set_envs(); await this.set_envs();
} }
public async enabled(ids: number[]) { public async enabled(ids: string[]) {
await EnvModel.update({ status: EnvStatus.normal }, { where: { id: ids } }); await EnvModel.update({ status: EnvStatus.normal }, { where: { id: ids } });
await this.set_envs(); await this.set_envs();
} }
public async updateNames({ ids, name }: { ids: number[]; name: string }) { public async updateNames({ ids, name }: { ids: string[]; name: string }) {
await EnvModel.update({ name }, { where: { id: ids } }); await EnvModel.update({ name }, { where: { id: ids } });
await this.set_envs(); await this.set_envs();
} }
public async pin(ids: number[]) {
await EnvModel.update({ isPinned: 1 }, { where: { id: ids } });
}
public async unPin(ids: number[]) {
await EnvModel.update({ isPinned: 0 }, { where: { id: ids } });
}
public async set_envs() { public async set_envs() {
const envs = await this.envs('', { const envs = await this.envs('', {
name: { [Op.not]: null }, name: { [Op.not]: null },
@@ -219,23 +210,18 @@ export default class EnvService {
.replace(/'/g, "'\\''") .replace(/'/g, "'\\''")
.trim(); .trim();
env_string += `export ${key}='${value}'\n`; env_string += `export ${key}='${value}'\n`;
const _env_value = `${group const _env_value = `'${group
.map((x) => x.value) .map((x) => x.value)
.join('&') .join('&')
.replace(/\\/g, '\\\\')}`; .replace(/\\/g, '\\\\')
js_env_string += `process.env.${key}=\`${_env_value.replace( .replace(/'/g, "\\'")}'`;
/\`/g, js_env_string += `process.env.${key}=${_env_value};\n`;
'\\`', py_env_string += `os.environ['${key}']=${_env_value}\n`;
)}\`;\n`;
py_env_string += `os.environ['${key}']='''${_env_value.replace(
/\'/g,
"\\'",
)}'''\n`;
} }
} }
} }
await writeFileWithLock(config.envFile, env_string); await fs.writeFile(config.envFile, env_string);
await writeFileWithLock(config.jsEnvFile, js_env_string); await fs.writeFile(config.jsEnvFile, js_env_string);
await writeFileWithLock(config.pyEnvFile, py_env_string); await fs.writeFile(config.pyEnvFile, py_env_string);
} }
} }
-64
View File
@@ -1,64 +0,0 @@
import { Server, ServerCredentials } from '@grpc/grpc-js';
import { CronService } from '../protos/cron';
import { HealthService } from '../protos/health';
import { ApiService } from '../protos/api';
import { addCron } from '../schedule/addCron';
import { delCron } from '../schedule/delCron';
import { check } from '../schedule/health';
import * as Api from '../schedule/api';
import Logger from '../loaders/logger';
import { promisify } from 'util';
import config from '../config';
import { metricsService } from './metrics';
import { Service } from 'typedi';
@Service()
export class GrpcServerService {
private server: Server = new Server({ 'grpc.enable_http_proxy': 0 });
async initialize() {
try {
this.server.addService(HealthService, { check });
this.server.addService(CronService, { addCron, delCron });
this.server.addService(ApiService, Api);
const grpcPort = config.grpcPort;
const bindAsync = promisify(this.server.bindAsync).bind(this.server);
await bindAsync(
`0.0.0.0:${grpcPort}`,
ServerCredentials.createInsecure(),
);
Logger.debug(`✌️ gRPC service started successfully`);
metricsService.record('grpc_service_start', 1, {
port: grpcPort.toString(),
});
return grpcPort;
} catch (err) {
Logger.error('Failed to start gRPC service:', err);
throw err;
}
}
async shutdown() {
try {
if (this.server) {
await new Promise((resolve) => {
this.server.tryShutdown(() => {
Logger.debug('gRPC service stopped');
metricsService.record('grpc_service_stop', 1);
resolve(null);
});
});
}
} catch (err) {
Logger.error('Error while shutting down gRPC service:', err);
throw err;
}
}
getServer() {
return this.server;
}
}
-72
View File
@@ -1,72 +0,0 @@
import { Service } from 'typedi';
import Logger from '../loaders/logger';
import { GrpcServerService } from './grpc';
import { HttpServerService } from './http';
interface HealthStatus {
status: 'ok' | 'error';
services: {
http: boolean;
grpc: boolean;
};
metrics: {
uptime: number;
memory: {
used: number;
total: number;
};
};
}
@Service()
export class HealthService {
private startTime = Date.now();
constructor(
private grpcServerService: GrpcServerService,
private httpServerService: HttpServerService,
) {}
async check(): Promise<HealthStatus> {
const status: HealthStatus = {
status: 'ok',
services: {
http: true,
grpc: true,
},
metrics: {
uptime: Math.floor((Date.now() - this.startTime) / 1000),
memory: {
used: process.memoryUsage().heapUsed,
total: process.memoryUsage().heapTotal,
},
},
};
try {
const httpServer = this.httpServerService.getServer();
if (!httpServer) {
status.services.http = false;
status.status = 'error';
}
} catch (err) {
status.services.http = false;
status.status = 'error';
Logger.error('HTTP server check failed:', err);
}
try {
const grpcServer = this.grpcServerService.getServer();
if (!grpcServer) {
status.services.grpc = false;
status.status = 'error';
}
} catch (err) {
status.services.grpc = false;
status.status = 'error';
Logger.error('gRPC server check failed:', err);
}
return status;
}
}
-53
View File
@@ -1,53 +0,0 @@
import express from 'express';
import Logger from '../loaders/logger';
import { metricsService } from './metrics';
import { Service } from 'typedi';
import { Server } from 'http';
@Service()
export class HttpServerService {
private server?: Server = undefined;
async initialize(expressApp: express.Application, port: number) {
try {
return new Promise((resolve, reject) => {
this.server = expressApp.listen(port, '0.0.0.0', () => {
Logger.debug(`✌️ HTTP service started successfully`);
metricsService.record('http_service_start', 1, {
port: port.toString(),
});
resolve(this.server);
});
this.server?.on('error', (err: Error) => {
Logger.error('Failed to start HTTP service:', err);
reject(err);
});
});
} catch (err) {
Logger.error('Failed to start HTTP service:', err);
throw err;
}
}
async shutdown() {
try {
if (this.server) {
await new Promise((resolve) => {
this.server?.close(() => {
Logger.debug('HTTP service stopped');
metricsService.record('http_service_stop', 1);
resolve(null);
});
});
}
} catch (err) {
Logger.error('Error while shutting down HTTP service:', err);
throw err;
}
}
getServer() {
return this.server;
}
}
-14
View File
@@ -1,14 +0,0 @@
import path from 'path';
import { Inject, Service } from 'typedi';
import winston from 'winston';
import config from '../config';
@Service()
export default class LogService {
constructor(@Inject('logger') private logger: winston.Logger) {}
public checkFilePath(filePath: string, fileName: string) {
const finalPath = path.resolve(config.logPath, filePath, fileName);
return finalPath.startsWith(config.logPath) ? finalPath : '';
}
}
-92
View File
@@ -1,92 +0,0 @@
import { performance } from 'perf_hooks';
import Logger from '../loaders/logger';
interface Metric {
name: string;
value: number;
timestamp: number;
tags?: Record<string, string>;
}
class MetricsService {
private metrics: Metric[] = [];
private static instance: MetricsService;
private constructor() {
// 定期清理旧数据
setInterval(() => {
const oneHourAgo = Date.now() - 3600000;
this.metrics = this.metrics.filter(m => m.timestamp > oneHourAgo);
}, 60000);
}
static getInstance(): MetricsService {
if (!MetricsService.instance) {
MetricsService.instance = new MetricsService();
}
return MetricsService.instance;
}
record(name: string, value: number, tags?: Record<string, string>) {
this.metrics.push({
name,
value,
timestamp: Date.now(),
tags,
});
}
measure(name: string, fn: () => void, tags?: Record<string, string>) {
const start = performance.now();
try {
fn();
} finally {
const duration = performance.now() - start;
this.record(name, duration, tags);
}
}
async measureAsync(name: string, fn: () => Promise<void>, tags?: Record<string, string>) {
const start = performance.now();
try {
await fn();
} finally {
const duration = performance.now() - start;
this.record(name, duration, tags);
}
}
getMetrics(name?: string, tags?: Record<string, string>) {
let filtered = this.metrics;
if (name) {
filtered = filtered.filter(m => m.name === name);
}
if (tags) {
filtered = filtered.filter(m => {
if (!m.tags) return false;
return Object.entries(tags).every(([key, value]) => m.tags![key] === value);
});
}
return {
count: filtered.length,
average: filtered.reduce((acc, curr) => acc + curr.value, 0) / filtered.length,
min: Math.min(...filtered.map(m => m.value)),
max: Math.max(...filtered.map(m => m.value)),
metrics: filtered,
};
}
report() {
const report = {
timestamp: Date.now(),
metrics: this.getMetrics(),
};
Logger.info('性能指标报告:', report);
return report;
}
}
export const metricsService = MetricsService.getInstance();
+107 -217
View File
@@ -1,11 +1,12 @@
import crypto from 'crypto'; import crypto from 'crypto';
import got from 'got';
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import winston from 'winston';
import { parseBody, parseHeaders } from '../config/util'; import { parseBody, parseHeaders } from '../config/util';
import { NotificationInfo } from '../data/notify'; import { NotificationInfo } from '../data/notify';
import UserService from './user'; import UserService from './user';
import { httpClient } from '../config/http';
import { ProxyAgent } from 'undici';
@Service() @Service()
export default class NotificationService { export default class NotificationService {
@@ -32,8 +33,6 @@ export default class NotificationService {
['webhook', this.webhook], ['webhook', this.webhook],
['lark', this.lark], ['lark', this.lark],
['chronocat', this.chronocat], ['chronocat', this.chronocat],
['ntfy', this.ntfy],
['wxPusherBot', this.wxPusherBot],
]); ]);
private title = ''; private title = '';
@@ -44,31 +43,22 @@ export default class NotificationService {
retry: 1, retry: 1,
}; };
constructor() {} constructor(@Inject('logger') private logger: winston.Logger) {}
public async notify( public async notify(
title: string, title: string,
content: string, content: string,
notificationInfo?: NotificationInfo,
): Promise<boolean | undefined> { ): Promise<boolean | undefined> {
let { type, ...rest } = await this.userService.getNotificationMode(); const { type, ...rest } = await this.userService.getNotificationMode();
if (notificationInfo?.type) {
type = notificationInfo?.type;
}
if (type) { if (type) {
this.title = title; this.title = title;
this.content = content; this.content = content;
let params = rest; this.params = rest;
if (notificationInfo) {
const { type: _, ...others } = notificationInfo;
params = { ...rest, ...others };
}
this.params = params;
const notificationModeAction = this.modeMap.get(type); const notificationModeAction = this.modeMap.get(type);
try { try {
return await notificationModeAction?.call(this); return await notificationModeAction?.call(this);
} catch (error: any) { } catch (error: any) {
console.error(error); throw error;
} }
} }
return false; return false;
@@ -93,9 +83,8 @@ export default class NotificationService {
private async gotify() { private async gotify() {
const { gotifyUrl, gotifyToken, gotifyPriority = 1 } = this.params; const { gotifyUrl, gotifyToken, gotifyPriority = 1 } = this.params;
try { try {
const res = await httpClient.post( const res: any = await got
`${gotifyUrl}/message?token=${gotifyToken}`, .post(`${gotifyUrl}/message?token=${gotifyToken}`, {
{
...this.gotOption, ...this.gotOption,
body: `title=${encodeURIComponent( body: `title=${encodeURIComponent(
this.title, this.title,
@@ -105,8 +94,8 @@ export default class NotificationService {
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
}, },
}, })
); .json();
if (typeof res.id === 'number') { if (typeof res.id === 'number') {
return true; return true;
} else { } else {
@@ -120,11 +109,13 @@ export default class NotificationService {
private async goCqHttpBot() { private async goCqHttpBot() {
const { goCqHttpBotQq, goCqHttpBotToken, goCqHttpBotUrl } = this.params; const { goCqHttpBotQq, goCqHttpBotToken, goCqHttpBotUrl } = this.params;
try { try {
const res = await httpClient.post(`${goCqHttpBotUrl}?${goCqHttpBotQq}`, { const res: any = await got
.post(`${goCqHttpBotUrl}?${goCqHttpBotQq}`, {
...this.gotOption, ...this.gotOption,
json: { message: `${this.title}\n${this.content}` }, json: { message: `${this.title}\n${this.content}` },
headers: { Authorization: 'Bearer ' + goCqHttpBotToken }, headers: { Authorization: 'Bearer ' + goCqHttpBotToken },
}); })
.json();
if (res.retcode === 0) { if (res.retcode === 0) {
return true; return true;
} else { } else {
@@ -137,20 +128,17 @@ export default class NotificationService {
private async serverChan() { private async serverChan() {
const { serverChanKey } = this.params; const { serverChanKey } = this.params;
const matchResult = serverChanKey.match(/^sctp(\d+)t/i); const url = serverChanKey.startsWith('SCT')
const url = ? `https://sctapi.ftqq.com/${serverChanKey}.send`
matchResult && matchResult[1] : `https://sc.ftqq.com/${serverChanKey}.send`;
? `https://${matchResult[1]}.push.ft07.com/send/${serverChanKey}.send`
: `https://sctapi.ftqq.com/${serverChanKey}.send`;
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
body: `title=${encodeURIComponent( body: `title=${this.title}&desp=${this.content}`,
this.title,
)}&desp=${encodeURIComponent(this.content)}`,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
}); })
.json();
if (res.errno === 0 || res.data.errno === 0) { if (res.errno === 0 || res.data.errno === 0) {
return true; return true;
} else { } else {
@@ -165,13 +153,15 @@ export default class NotificationService {
const { pushDeerKey, pushDeerUrl } = this.params; const { pushDeerKey, pushDeerUrl } = this.params;
const url = pushDeerUrl || `https://api2.pushdeer.com/message/push`; const url = pushDeerUrl || `https://api2.pushdeer.com/message/push`;
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
body: `pushkey=${pushDeerKey}&text=${encodeURIComponent( body: `pushkey=${pushDeerKey}&text=${encodeURIComponent(
this.title, this.title,
)}&desp=${encodeURIComponent(this.content)}&type=markdown`, )}&desp=${encodeURIComponent(this.content)}&type=markdown`,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
}); })
.json();
if ( if (
res.content.result.length !== undefined && res.content.result.length !== undefined &&
res.content.result.length > 0 res.content.result.length > 0
@@ -186,13 +176,16 @@ export default class NotificationService {
} }
private async chat() { private async chat() {
const { synologyChatUrl } = this.params; const { chatUrl, chatToken } = this.params;
const url = `${chatUrl}${chatToken}`;
try { try {
const res = await httpClient.post(synologyChatUrl, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
body: `payload={"text":"${this.title}\n${this.content}"}`, body: `payload={"text":"${this.title}\n${this.content}"}`,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
}); })
.json();
if (res.success) { if (res.success) {
return true; return true;
} else { } else {
@@ -228,11 +221,13 @@ export default class NotificationService {
url: barkUrl, url: barkUrl,
}; };
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
json: body, json: body,
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}); })
.json();
if (res.code === 200) { if (res.code === 200) {
return true; return true;
} else { } else {
@@ -258,17 +253,29 @@ export default class NotificationService {
}/bot${telegramBotToken}/sendMessage`; }/bot${telegramBotToken}/sendMessage`;
let agent; let agent;
if (telegramBotProxyHost && telegramBotProxyPort) { if (telegramBotProxyHost && telegramBotProxyPort) {
agent = new ProxyAgent({ const options: any = {
uri: `http://${authStr}${telegramBotProxyHost}:${telegramBotProxyPort}`, keepAlive: true,
}); keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
proxy: `http://${authStr}${telegramBotProxyHost}:${telegramBotProxyPort}`,
};
const httpAgent = new HttpProxyAgent(options);
const httpsAgent = new HttpsProxyAgent(options);
agent = {
http: httpAgent,
https: httpsAgent,
};
} }
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
body: `chat_id=${telegramBotUserId}&text=${this.title}\n\n${this.content}&disable_web_page_preview=true`, body: `chat_id=${telegramBotUserId}&text=${this.title}\n\n${this.content}&disable_web_page_preview=true`,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
dispatcher: agent, agent,
}); })
.json();
if (res.ok) { if (res.ok) {
return true; return true;
} else { } else {
@@ -291,7 +298,8 @@ export default class NotificationService {
} }
const url = `https://oapi.dingtalk.com/robot/send?access_token=${dingtalkBotToken}${secretParam}`; const url = `https://oapi.dingtalk.com/robot/send?access_token=${dingtalkBotToken}${secretParam}`;
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
json: { json: {
msgtype: 'text', msgtype: 'text',
@@ -299,7 +307,8 @@ export default class NotificationService {
content: ` ${this.title}\n\n${this.content}`, content: ` ${this.title}\n\n${this.content}`,
}, },
}, },
}); })
.json();
if (res.errcode === 0) { if (res.errcode === 0) {
return true; return true;
} else { } else {
@@ -315,7 +324,8 @@ export default class NotificationService {
this.params; this.params;
const url = `${weWorkOrigin}/cgi-bin/webhook/send?key=${weWorkBotKey}`; const url = `${weWorkOrigin}/cgi-bin/webhook/send?key=${weWorkBotKey}`;
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
json: { json: {
msgtype: 'text', msgtype: 'text',
@@ -323,7 +333,8 @@ export default class NotificationService {
content: ` ${this.title}\n\n${this.content}`, content: ` ${this.title}\n\n${this.content}`,
}, },
}, },
}); })
.json();
if (res.errcode === 0) { if (res.errcode === 0) {
return true; return true;
} else { } else {
@@ -340,13 +351,15 @@ export default class NotificationService {
const [corpid, corpsecret, touser, agentid, thumb_media_id = '1'] = const [corpid, corpsecret, touser, agentid, thumb_media_id = '1'] =
weWorkAppKey.split(','); weWorkAppKey.split(',');
const url = `${weWorkOrigin}/cgi-bin/gettoken`; const url = `${weWorkOrigin}/cgi-bin/gettoken`;
const tokenRes = await httpClient.post(url, { const tokenRes: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
json: { json: {
corpid, corpid,
corpsecret, corpsecret,
}, },
}); })
.json();
let options: any = { let options: any = {
msgtype: 'mpnews', msgtype: 'mpnews',
@@ -387,7 +400,8 @@ export default class NotificationService {
} }
try { try {
const res = await httpClient.post( const res: any = await got
.post(
`${weWorkOrigin}/cgi-bin/message/send?access_token=${tokenRes.access_token}`, `${weWorkOrigin}/cgi-bin/message/send?access_token=${tokenRes.access_token}`,
{ {
...this.gotOption, ...this.gotOption,
@@ -398,7 +412,8 @@ export default class NotificationService {
...options, ...options,
}, },
}, },
); )
.json();
if (res.errcode === 0) { if (res.errcode === 0) {
return true; return true;
@@ -440,12 +455,14 @@ export default class NotificationService {
} }
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
json: { json: {
...json, ...json,
}, },
}); })
.json();
if (res.code === 0) { if (res.code === 0) {
return true; return true;
} else { } else {
@@ -460,11 +477,13 @@ export default class NotificationService {
const { iGotPushKey } = this.params; const { iGotPushKey } = this.params;
const url = `https://push.hellyw.com/${iGotPushKey.toLowerCase()}`; const url = `https://push.hellyw.com/${iGotPushKey.toLowerCase()}`;
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
body: `title=${this.title}&content=${this.content}`, body: `title=${this.title}&content=${this.content}`,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
}); })
.json();
if (res.ret === 0) { if (res.ret === 0) {
return true; return true;
@@ -477,33 +496,20 @@ export default class NotificationService {
} }
private async pushPlus() { private async pushPlus() {
const { const { pushPlusToken, pushPlusUser } = this.params;
pushPlusToken,
pushPlusUser,
pushplusWebhook,
pushPlusTemplate,
pushplusChannel,
pushplusCallbackUrl,
pushplusTo,
} = this.params;
const url = `https://www.pushplus.plus/send`; const url = `https://www.pushplus.plus/send`;
try { try {
let body = { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
json: { json: {
token: `${pushPlusToken}`, token: `${pushPlusToken}`,
title: `${this.title}`, title: `${this.title}`,
content: `${this.content.replace(/[\n\r]/g, '<br>')}`, content: `${this.content.replace(/[\n\r]/g, '<br>')}`,
topic: `${pushPlusUser || ''}`, topic: `${pushPlusUser || ''}`,
template: `${pushPlusTemplate || 'html'}`,
channel: `${pushplusChannel || 'wechat'}`,
webhook: `${pushplusWebhook || ''}`,
callbackUrl: `${pushplusCallbackUrl || ''}`,
to: `${pushplusTo || ''}`,
}, },
}; })
.json();
const res = await httpClient.post(url, body);
if (res.code === 200) { if (res.code === 200) {
return true; return true;
@@ -527,7 +533,8 @@ export default class NotificationService {
const url = `https://www.weplusbot.com/send`; const url = `https://www.weplusbot.com/send`;
try { try {
const res = await httpClient.post(url, { const res: any = await got
.post(url, {
...this.gotOption, ...this.gotOption,
json: { json: {
token: `${wePlusBotToken}`, token: `${wePlusBotToken}`,
@@ -537,7 +544,8 @@ export default class NotificationService {
receiver: `${wePlusBotReceiver || ''}`, receiver: `${wePlusBotReceiver || ''}`,
version: `${wePlusBotVersion || 'pro'}`, version: `${wePlusBotVersion || 'pro'}`,
}, },
}); })
.json();
if (res.code === 200) { if (res.code === 200) {
return true; return true;
@@ -550,35 +558,23 @@ export default class NotificationService {
} }
private async lark() { private async lark() {
let { larkKey, larkSecret } = this.params; let { larkKey } = this.params;
if (!larkKey.startsWith('http')) { if (!larkKey.startsWith('http')) {
larkKey = `https://open.feishu.cn/open-apis/bot/v2/hook/${larkKey}`; larkKey = `https://open.feishu.cn/open-apis/bot/v2/hook/${larkKey}`;
} }
const body: Record<string, any> = { try {
const res: any = await got
.post(larkKey, {
...this.gotOption,
json: {
msg_type: 'text', msg_type: 'text',
content: { text: `${this.title}\n\n${this.content}` }, content: { text: `${this.title}\n\n${this.content}` },
}; },
// Add signature if secret is provided
// Note: Feishu's signature algorithm uses timestamp+"\n"+secret as the HMAC key
// and signs an empty message, which differs from typical HMAC usage
if (larkSecret) {
const timestamp = Math.floor(Date.now() / 1000).toString();
const stringToSign = `${timestamp}\n${larkSecret}`;
const hmac = crypto.createHmac('sha256', stringToSign);
const sign = hmac.digest('base64');
body.timestamp = timestamp;
body.sign = sign;
}
try {
const res = await httpClient.post(larkKey, {
...this.gotOption,
json: body,
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}); })
.json();
if (res.StatusCode === 0 || res.code === 0) { if (res.StatusCode === 0 || res.code === 0) {
return true; return true;
} else { } else {
@@ -590,7 +586,7 @@ export default class NotificationService {
} }
private async email() { private async email() {
const { emailPass, emailService, emailUser, emailTo } = this.params; const { emailPass, emailService, emailUser } = this.params;
try { try {
const transporter = nodemailer.createTransport({ const transporter = nodemailer.createTransport({
@@ -603,7 +599,7 @@ export default class NotificationService {
const info = await transporter.sendMail({ const info = await transporter.sendMail({
from: `"青龙快讯" <${emailUser}>`, from: `"青龙快讯" <${emailUser}>`,
to: emailTo ? emailTo.split(';') : emailUser, to: `${emailUser}`,
subject: `${this.title}`, subject: `${this.title}`,
html: `${this.content.replace(/\n/g, '<br/>')}`, html: `${this.content.replace(/\n/g, '<br/>')}`,
}); });
@@ -623,9 +619,7 @@ export default class NotificationService {
private async pushMe() { private async pushMe() {
const { pushMeKey, pushMeUrl } = this.params; const { pushMeKey, pushMeUrl } = this.params;
try { try {
const res = await httpClient.post<'text'>( const res: any = await got.post(pushMeUrl || 'https://push.i-i.me/', {
pushMeUrl || 'https://push.i-i.me/',
{
...this.gotOption, ...this.gotOption,
json: { json: {
push_key: pushMeKey, push_key: pushMeKey,
@@ -633,112 +627,11 @@ export default class NotificationService {
content: this.content, content: this.content,
}, },
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
},
);
if (res === 'success') {
return true;
} else {
throw new Error(res);
}
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
}
private async ntfy() {
const {
ntfyUrl,
ntfyTopic,
ntfyPriority,
ntfyToken,
ntfyUsername,
ntfyPassword,
ntfyActions,
} = this.params;
// 编码函数
const encodeRfc2047 = (text: string, charset: string = 'UTF-8'): string => {
const encodedText = Buffer.from(text).toString('base64');
return `=?${charset}?B?${encodedText}?=`;
};
try {
const headers: Record<string, string> = {
Title: encodeRfc2047(this.title),
Priority: `${ntfyPriority || '3'}`,
Icon: 'https://qn.whyour.cn/logo.png',
};
if (ntfyToken) {
headers['Authorization'] = `Bearer ${ntfyToken}`;
} else if (ntfyUsername && ntfyPassword) {
headers['Authorization'] = `Basic ${Buffer.from(
`${ntfyUsername}:${ntfyPassword}`,
).toString('base64')}`;
}
if (ntfyActions) {
headers['Actions'] = encodeRfc2047(ntfyActions);
}
const res = await httpClient.request(
`${ntfyUrl || 'https://ntfy.sh'}/${ntfyTopic}`,
{
...this.gotOption,
body: `${this.content}`,
headers: headers,
method: 'POST',
},
);
if (res.statusCode === 200) {
return true;
} else {
throw new Error(await res.body.text());
}
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
}
private async wxPusherBot() {
const { wxPusherBotAppToken, wxPusherBotTopicIds, wxPusherBotUids } =
this.params;
// 处理 topicIds,将分号分隔的字符串转为数组
const topicIds = wxPusherBotTopicIds
? wxPusherBotTopicIds
.split(';')
.map((id) => id.trim())
.filter((id) => id)
.map((id) => parseInt(id))
: [];
// 处理 uids,将分号分隔的字符串转为数组
const uids = wxPusherBotUids
? wxPusherBotUids
.split(';')
.map((uid) => uid.trim())
.filter((uid) => uid)
: [];
// topic_ids 和 uids 至少要有一个
if (!topicIds.length && !uids.length) {
throw new Error('wxPusher 服务的 TopicIds 和 Uids 至少配置一个才行');
}
const url = `https://wxpusher.zjiecode.com/api/send/message`;
try {
const res = await httpClient.post(url, {
...this.gotOption,
json: {
appToken: wxPusherBotAppToken,
content: `<h1>${this.title}</h1><br/><div style='white-space: pre-wrap;'>${this.content}</div>`,
summary: this.title,
contentType: 2,
topicIds: topicIds,
uids: uids,
verifyPayType: 0,
},
}); });
if (res.body === 'success') {
if (res.code === 1000) {
return true; return true;
} else { } else {
throw new Error(JSON.stringify(res)); throw new Error(res.body);
} }
} catch (error: any) { } catch (error: any) {
throw new Error(error.response ? error.response.body : error); throw new Error(error.response ? error.response.body : error);
@@ -784,16 +677,15 @@ export default class NotificationService {
}, },
], ],
}; };
const res = await httpClient.request(url, { const res: any = await got.post(url, {
...this.gotOption, ...this.gotOption,
json: data, json: data,
headers, headers,
method: 'POST',
}); });
if (res.statusCode === 200) { if (res.statusCode === 200) {
return true; return true;
} else { } else {
throw new Error(await res.body.text()); throw new Error(res.body);
} }
} }
} }
@@ -812,7 +704,7 @@ export default class NotificationService {
webhookContentType, webhookContentType,
} = this.params; } = this.params;
if (!webhookUrl?.includes('$title') && !webhookBody?.includes('$title')) { if (!webhookUrl.includes('$title') && !webhookBody.includes('$title')) {
throw new Error('Url 或者 Body 中必须包含 $title'); throw new Error('Url 或者 Body 中必须包含 $title');
} }
@@ -828,17 +720,15 @@ export default class NotificationService {
allowGetBody: true, allowGetBody: true,
...bodyParam, ...bodyParam,
}; };
try { try {
const formatUrl = webhookUrl const formatUrl = webhookUrl
?.replaceAll('$title', encodeURIComponent(this.title)) ?.replaceAll('$title', encodeURIComponent(this.title))
?.replaceAll('$content', encodeURIComponent(this.content)); ?.replaceAll('$content', encodeURIComponent(this.content));
const res = await httpClient.request(formatUrl, options); const res = await got(formatUrl, options);
const text = await res.body.text();
if (String(res.statusCode).startsWith('20')) { if (String(res.statusCode).startsWith('20')) {
return true; return true;
} else { } else {
throw new Error(await res.body.text()); throw new Error(JSON.stringify(res));
} }
} catch (error: any) { } catch (error: any) {
throw new Error(error.response ? error.response.body : error); throw new Error(error.response ? error.response.body : error);
+19 -27
View File
@@ -1,18 +1,19 @@
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import winston from 'winston'; import winston from 'winston';
import { createRandomString } from '../config/util'; import { createRandomString } from '../config/util';
import config from '../config';
import { App, AppModel } from '../data/open'; import { App, AppModel } from '../data/open';
import { v4 as uuidV4 } from 'uuid'; import { v4 as uuidV4 } from 'uuid';
import sequelize, { Op } from 'sequelize'; import sequelize, { Op } from 'sequelize';
import { shareStore } from '../shared/store';
@Service() @Service()
export default class OpenService { export default class OpenService {
constructor(@Inject('logger') private logger: winston.Logger) {} constructor(@Inject('logger') private logger: winston.Logger) {}
public async findApps(): Promise<App[] | null> { public async findTokenByValue(token: string): Promise<App | null> {
const docs = await this.find({}); const docs = await this.find({});
return docs; const doc = docs.filter((x) => x.tokens?.find((y) => y.value === token));
return doc[0];
} }
public async create(payload: App): Promise<App> { public async create(payload: App): Promise<App> {
@@ -20,8 +21,6 @@ export default class OpenService {
tab.client_id = createRandomString(12, 12); tab.client_id = createRandomString(12, 12);
tab.client_secret = createRandomString(24, 24); tab.client_secret = createRandomString(24, 24);
const doc = await this.insert(tab); const doc = await this.insert(tab);
const apps = await this.find({});
await shareStore.updateApps(apps);
return { ...doc, tokens: [] }; return { ...doc, tokens: [] };
} }
@@ -35,19 +34,17 @@ export default class OpenService {
name: payload.name, name: payload.name,
scopes: payload.scopes, scopes: payload.scopes,
id: payload.id, id: payload.id,
} as App); } as any);
return { ...newDoc, tokens: [] }; return { ...newDoc, tokens: [] };
} }
private async updateDb(payload: Partial<App>): Promise<App> { private async updateDb(payload: App): Promise<App> {
await AppModel.update(payload, { where: { id: payload.id } }); await AppModel.update(payload, { where: { id: payload.id } });
const apps = await this.find({}); return await this.getDb({ id: payload.id });
await shareStore.updateApps(apps);
return apps?.find((x) => x.id === payload.id) as App;
} }
public async getDb(query: Record<string, any>): Promise<App> { public async getDb(query: any): Promise<App> {
const doc = await AppModel.findOne({ where: query }); const doc: any = await AppModel.findOne({ where: query });
if (!doc) { if (!doc) {
throw new Error(`App ${JSON.stringify(query)} not found`); throw new Error(`App ${JSON.stringify(query)} not found`);
} }
@@ -56,12 +53,10 @@ export default class OpenService {
public async remove(ids: number[]) { public async remove(ids: number[]) {
await AppModel.destroy({ where: { id: ids } }); await AppModel.destroy({ where: { id: ids } });
const apps = await this.find({});
await shareStore.updateApps(apps);
} }
public async resetSecret(id: number): Promise<App> { public async resetSecret(id: number): Promise<App> {
const tab: Partial<App> = { const tab: any = {
client_secret: createRandomString(24, 24), client_secret: createRandomString(24, 24),
tokens: [], tokens: [],
id, id,
@@ -79,7 +74,7 @@ export default class OpenService {
public async list( public async list(
searchText: string = '', searchText: string = '',
sort: any = {}, sort: any = {},
query: Record<string, any> = {}, query: any = {},
): Promise<App[]> { ): Promise<App[]> {
let condition = { ...query }; let condition = { ...query };
if (searchText) { if (searchText) {
@@ -106,7 +101,7 @@ export default class OpenService {
} }
} }
private async find(query: Record<string, any>, sort?: any): Promise<App[]> { private async find(query: any, sort?: any): Promise<App[]> {
const docs = await AppModel.findAll({ where: { ...query } }); const docs = await AppModel.findAll({ where: { ...query } });
return docs.map((x) => x.get({ plain: true })); return docs.map((x) => x.get({ plain: true }));
} }
@@ -140,8 +135,6 @@ export default class OpenService {
{ tokens }, { tokens },
{ where: { client_id, client_secret } }, { where: { client_id, client_secret } },
); );
const apps = await this.find({});
await shareStore.updateApps(apps);
return { return {
code: 200, code: 200,
data: { data: {
@@ -159,15 +152,14 @@ export default class OpenService {
value: string; value: string;
expiration: number; expiration: number;
}> { }> {
const apps = await shareStore.getApps(); let systemApp = (await AppModel.findOne({
const systemApp = apps?.find((x) => x.name === 'system'); where: { name: 'system' },
})) as App;
if (!systemApp) { if (!systemApp) {
throw new Error('system app not found'); systemApp = await this.create({
} name: 'system',
const now = Math.round(Date.now() / 1000); scopes: ['crons', 'system'],
const currentToken = systemApp.tokens?.find((x) => x.expiration > now); } as App);
if (currentToken) {
return currentToken;
} }
const { data } = await this.authToken({ const { data } = await this.authToken({
client_id: systemApp.client_id, client_id: systemApp.client_id,
+12 -46
View File
@@ -17,7 +17,6 @@ export interface ScheduleTaskType {
command: string; command: string;
name?: string; name?: string;
schedule?: string; schedule?: string;
runOrigin: 'subscription' | 'system' | 'script';
} }
export interface TaskCallbacks { export interface TaskCallbacks {
@@ -41,11 +40,7 @@ export default class ScheduleService {
private intervalSchedule = new ToadScheduler(); private intervalSchedule = new ToadScheduler();
private taskLimitMap = { private maxBuffer = 200 * 1024 * 1024;
system: 'runWithSystemLimit' as const,
script: 'runWithScriptLimit' as const,
subscription: 'runWithSubscriptionLimit' as const,
};
constructor(@Inject('logger') private logger: winston.Logger) { } constructor(@Inject('logger') private logger: winston.Logger) { }
@@ -56,21 +51,12 @@ export default class ScheduleService {
schedule?: string; schedule?: string;
name?: string; name?: string;
command?: string; command?: string;
id: string;
runOrigin: 'subscription' | 'system' | 'script';
}, },
completionTime: 'start' | 'end' = 'end', completionTime: 'start' | 'end' = 'end',
) { ) {
const { runOrigin, ...others } = params; return taskLimit.runWithCronLimit(() => {
return taskLimit[this.taskLimitMap[runOrigin]](others, () => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
this.logger.info( this.logger.info(`[panel][开始执行任务] 参数 ${JSON.stringify({ ...params, command })}`);
`[panel][开始执行任务] 参数: ${JSON.stringify({
...others,
command,
})}`,
);
try { try {
const startTime = dayjs(); const startTime = dayjs();
@@ -104,21 +90,13 @@ export default class ScheduleService {
}); });
cp.on('exit', async (code) => { cp.on('exit', async (code) => {
this.logger.info(
'[panel][执行任务结束] 参数: %s, 退出码: %j',
JSON.stringify({
...others,
command,
}),
code,
);
const endTime = dayjs(); const endTime = dayjs();
await callbacks.onEnd?.( await callbacks.onEnd?.(
cp, cp,
endTime, endTime,
endTime.diff(startTime, 'seconds'), endTime.diff(startTime, 'seconds'),
); );
resolve({ ...others, pid: cp.pid, code }); resolve({ ...params, pid: cp.pid, code });
}); });
} catch (error) { } catch (error) {
this.logger.error( this.logger.error(
@@ -133,13 +111,13 @@ export default class ScheduleService {
} }
async createCronTask( async createCronTask(
{ id = 0, command, name, schedule = '', runOrigin }: ScheduleTaskType, { id = 0, command, name, schedule = '' }: ScheduleTaskType,
callbacks?: TaskCallbacks, callbacks?: TaskCallbacks,
runImmediately = false, runImmediately = false,
) { ) {
const _id = this.formatId(id); const _id = this.formatId(id);
this.logger.info( this.logger.info(
'[panel][创建cron任务] 任务ID: %s, cron: %s, 任务名: %s, 执行命令: %s', '[panel][创建cron任务], 任务ID: %s, cron: %s, 任务名: %s, 执行命令: %s',
_id, _id,
schedule, schedule,
name, name,
@@ -153,8 +131,6 @@ export default class ScheduleService {
name, name,
schedule, schedule,
command, command,
id: _id,
runOrigin,
}); });
}), }),
); );
@@ -164,15 +140,13 @@ export default class ScheduleService {
name, name,
schedule, schedule,
command, command,
id: _id,
runOrigin,
}); });
} }
} }
async cancelCronTask({ id = 0, name }: ScheduleTaskType) { async cancelCronTask({ id = 0, name }: ScheduleTaskType) {
const _id = this.formatId(id); const _id = this.formatId(id);
this.logger.info('[panel][取消定时任务] 任务名: %s', name); this.logger.info('[panel][取消定时任务], 任务名: %s', name);
if (this.scheduleStacks.has(_id)) { if (this.scheduleStacks.has(_id)) {
this.scheduleStacks.get(_id)?.cancel(); this.scheduleStacks.get(_id)?.cancel();
this.scheduleStacks.delete(_id); this.scheduleStacks.delete(_id);
@@ -180,14 +154,14 @@ export default class ScheduleService {
} }
async createIntervalTask( async createIntervalTask(
{ id = 0, command, name = '', runOrigin }: ScheduleTaskType, { id = 0, command, name = '' }: ScheduleTaskType,
schedule: SimpleIntervalSchedule, schedule: SimpleIntervalSchedule,
runImmediately = true, runImmediately = true,
callbacks?: TaskCallbacks, callbacks?: TaskCallbacks,
) { ) {
const _id = this.formatId(id); const _id = this.formatId(id);
this.logger.info( this.logger.info(
'[panel][创建interval任务] 任务ID: %s, 任务名: %s, 执行命令: %s', '[panel][创建interval任务], 任务ID: %s, 任务名: %s, 执行命令: %s',
_id, _id,
name, name,
command, command,
@@ -198,13 +172,11 @@ export default class ScheduleService {
this.runTask(command, callbacks, { this.runTask(command, callbacks, {
name, name,
command, command,
id: _id,
runOrigin,
}); });
}, },
(err) => { (err) => {
this.logger.error( this.logger.error(
'[panel][执行任务失败] 命令: %s, 错误信息: %j', '[执行任务失败] 命令: %s, 错误信息: %j',
command, command,
err, err,
); );
@@ -214,7 +186,7 @@ export default class ScheduleService {
const job = new LongIntervalJob( const job = new LongIntervalJob(
{ runImmediately: false, ...schedule }, { runImmediately: false, ...schedule },
task, task,
{ id: _id }, _id,
); );
this.intervalSchedule.addIntervalJob(job); this.intervalSchedule.addIntervalJob(job);
@@ -223,19 +195,13 @@ export default class ScheduleService {
this.runTask(command, callbacks, { this.runTask(command, callbacks, {
name, name,
command, command,
id: _id,
runOrigin,
}); });
} }
} }
async cancelIntervalTask({ id = 0, name }: ScheduleTaskType) { async cancelIntervalTask({ id = 0, name }: ScheduleTaskType) {
const _id = this.formatId(id); const _id = this.formatId(id);
this.logger.info( this.logger.info('[取消interval任务], 任务ID: %s, 任务名: %s', _id, name);
'[panel][取消interval任务] 任务ID: %s, 任务名: %s',
_id,
name,
);
this.intervalSchedule.removeById(_id); this.intervalSchedule.removeById(_id);
} }
+5 -14
View File
@@ -7,7 +7,6 @@ import ScheduleService, { TaskCallbacks } from './schedule';
import config from '../config'; import config from '../config';
import { TASK_COMMAND } from '../config/const'; import { TASK_COMMAND } from '../config/const';
import { getFileContentByName, getPid, killTask, rmPath } from '../config/util'; import { getFileContentByName, getPid, killTask, rmPath } from '../config/util';
import taskLimit from '../shared/pLimit';
@Service() @Service()
export default class ScriptService { export default class ScriptService {
@@ -44,7 +43,7 @@ export default class ScriptService {
const pid = await this.scheduleService.runTask( const pid = await this.scheduleService.runTask(
`real_time=true ${command}`, `real_time=true ${command}`,
this.taskCallbacks(filePath), this.taskCallbacks(filePath),
{ command, id: relativePath.replace(/ /g, '-'), runOrigin: 'script' }, { command },
'start', 'start',
); );
@@ -54,7 +53,6 @@ export default class ScriptService {
public async stopScript(filePath: string, pid: number) { public async stopScript(filePath: string, pid: number) {
if (!pid) { if (!pid) {
const relativePath = path.relative(config.scriptPath, filePath); const relativePath = path.relative(config.scriptPath, filePath);
taskLimit.removeQueuedCron(relativePath.replace(/ /g, '-'));
pid = (await getPid(`${TASK_COMMAND} ${relativePath} now`)) as number; pid = (await getPid(`${TASK_COMMAND} ${relativePath} now`)) as number;
} }
try { try {
@@ -64,19 +62,12 @@ export default class ScriptService {
return { code: 200 }; return { code: 200 };
} }
public checkFilePath(filePath: string, fileName: string) {
const finalPath = path.resolve(config.scriptPath, filePath, fileName);
return finalPath.startsWith(config.scriptPath) ? finalPath : '';
}
public async getFile(filePath: string, fileName: string) { public async getFile(filePath: string, fileName: string) {
const finalPath = this.checkFilePath(filePath, fileName); let _filePath = join(config.scriptPath, filePath, fileName);
if (filePath.startsWith(config.dataPath)) {
if (!finalPath) { _filePath = join(filePath, fileName);
return '';
} }
const content = await getFileContentByName(_filePath);
const content = await getFileContentByName(finalPath);
return content; return content;
} }
} }
+8 -45
View File
@@ -7,7 +7,6 @@ import { Subscription } from '../data/subscription';
import { formatUrl } from '../config/subscription'; import { formatUrl } from '../config/subscription';
import config from '../config'; import config from '../config';
import { fileExist, rmPath } from '../config/util'; import { fileExist, rmPath } from '../config/util';
import { writeFileWithLock } from '../shared/utils';
@Service() @Service()
export default class SshKeyService { export default class SshKeyService {
@@ -26,13 +25,13 @@ export default class SshKeyService {
if (_exist) { if (_exist) {
config = await fs.readFile(this.sshConfigFilePath, { encoding: 'utf-8' }); config = await fs.readFile(this.sshConfigFilePath, { encoding: 'utf-8' });
} else { } else {
await writeFileWithLock(this.sshConfigFilePath, '', { mode: '600' }); await fs.writeFile(this.sshConfigFilePath, '');
} }
if (!config.includes(this.sshConfigHeader)) { if (!config.includes(this.sshConfigHeader)) {
await writeFileWithLock( await fs.writeFile(
this.sshConfigFilePath, this.sshConfigFilePath,
`${this.sshConfigHeader}\n\n${config}`, `${this.sshConfigHeader}\n\n${config}`,
{ mode: '600' }, { encoding: 'utf-8' },
); );
} }
} }
@@ -42,13 +41,10 @@ export default class SshKeyService {
key: string, key: string,
): Promise<void> { ): Promise<void> {
try { try {
await writeFileWithLock( await fs.writeFile(path.join(this.sshPath, alias), `${key}${os.EOL}`, {
path.join(this.sshPath, alias), encoding: 'utf8',
`${key}${os.EOL}`,
{
mode: '400', mode: '400',
}, });
);
} catch (error) { } catch (error) {
this.logger.error('生成私钥文件失败', error); this.logger.error('生成私钥文件失败', error);
} }
@@ -78,12 +74,11 @@ export default class SshKeyService {
this.sshPath, this.sshPath,
alias, alias,
)}\n StrictHostKeyChecking no\n${proxyStr}`; )}\n StrictHostKeyChecking no\n${proxyStr}`;
await writeFileWithLock( await fs.writeFile(
`${path.join(this.sshPath, `${alias}.config`)}`, `${path.join(this.sshPath, `${alias}.config`)}`,
config, config,
{ {
encoding: 'utf8', encoding: 'utf8',
mode: '600',
}, },
); );
} }
@@ -107,11 +102,7 @@ export default class SshKeyService {
await this.generateSingleSshConfig(alias, host, proxy); await this.generateSingleSshConfig(alias, host, proxy);
} }
public async removeSSHKey( public async removeSSHKey(alias: string, host: string, proxy?: string): Promise<void> {
alias: string,
host: string,
proxy?: string,
): Promise<void> {
await this.removePrivateKeyFile(alias); await this.removePrivateKeyFile(alias);
await this.removeSshConfig(alias); await this.removeSshConfig(alias);
} }
@@ -131,32 +122,4 @@ export default class SshKeyService {
} }
} }
} }
public async addGlobalSSHKey(key: string, alias: string): Promise<void> {
await this.generatePrivateKeyFile(`~global_${alias}`, key);
// Create a global SSH config entry that matches all hosts
// This allows the key to be used for any Git repository
await this.generateGlobalSshConfig(`~global_${alias}`);
}
public async removeGlobalSSHKey(alias: string): Promise<void> {
await this.removePrivateKeyFile(`~global_${alias}`);
await this.removeSshConfig(`~global_${alias}`);
}
private async generateGlobalSshConfig(alias: string) {
// Create a config that matches all hosts, making this key globally available
const config = `Host *\n IdentityFile ${path.join(
this.sshPath,
alias,
)}\n StrictHostKeyChecking no\n`;
await writeFileWithLock(
`${path.join(this.sshPath, `${alias}.config`)}`,
config,
{
encoding: 'utf8',
mode: '600',
},
);
}
} }
+17 -31
View File
@@ -3,7 +3,6 @@ import winston from 'winston';
import config from '../config'; import config from '../config';
import { import {
Subscription, Subscription,
SubscriptionInstance,
SubscriptionModel, SubscriptionModel,
SubscriptionStatus, SubscriptionStatus,
} from '../data/subscription'; } from '../data/subscription';
@@ -30,8 +29,6 @@ import { LOG_END_SYMBOL } from '../config/const';
import { formatCommand, formatUrl } from '../config/subscription'; import { formatCommand, formatUrl } from '../config/subscription';
import { CrontabModel } from '../data/cron'; import { CrontabModel } from '../data/cron';
import CrontabService from './cron'; import CrontabService from './cron';
import taskLimit from '../shared/pLimit';
import { logStreamManager } from '../shared/logStreamManager';
@Service() @Service()
export default class SubscriptionService { export default class SubscriptionService {
@@ -43,12 +40,8 @@ export default class SubscriptionService {
private crontabService: CrontabService, private crontabService: CrontabService,
) {} ) {}
public async list( public async list(searchText?: string): Promise<Subscription[]> {
searchText?: string,
ids?: string,
): Promise<SubscriptionInstance[]> {
let query = {}; let query = {};
const subIds = JSON.parse(ids || '[]');
if (searchText) { if (searchText) {
const reg = { const reg = {
[Op.or]: [ [Op.or]: [
@@ -69,7 +62,7 @@ export default class SubscriptionService {
} }
try { try {
const result = await SubscriptionModel.findAll({ const result = await SubscriptionModel.findAll({
where: { ...query, ...(ids ? { id: subIds } : undefined) }, where: query,
order: [ order: [
['is_disabled', 'ASC'], ['is_disabled', 'ASC'],
['createdAt', 'DESC'], ['createdAt', 'DESC'],
@@ -94,7 +87,7 @@ export default class SubscriptionService {
this.scheduleService.cancelCronTask(doc as any); this.scheduleService.cancelCronTask(doc as any);
needCreate && needCreate &&
(await this.scheduleService.createCronTask( (await this.scheduleService.createCronTask(
{ ...doc, runOrigin: 'subscription' } as any, doc as any,
this.taskCallbacks(doc), this.taskCallbacks(doc),
runImmediately, runImmediately,
)); ));
@@ -103,7 +96,7 @@ export default class SubscriptionService {
const { type, value } = doc.interval_schedule; const { type, value } = doc.interval_schedule;
needCreate && needCreate &&
(await this.scheduleService.createIntervalTask( (await this.scheduleService.createIntervalTask(
{ ...doc, runOrigin: 'subscription' } as any, doc as any,
{ [type]: value } as SimpleIntervalSchedule, { [type]: value } as SimpleIntervalSchedule,
runImmediately, runImmediately,
this.taskCallbacks(doc), this.taskCallbacks(doc),
@@ -137,7 +130,7 @@ export default class SubscriptionService {
let beforeStr = ''; let beforeStr = '';
try { try {
if (doc.sub_before) { if (doc.sub_before) {
await logStreamManager.write(absolutePath, `\n## 执行before命令...\n\n`); await fs.appendFile(absolutePath, `\n## 执行before命令...\n\n`);
beforeStr = await promiseExec(doc.sub_before); beforeStr = await promiseExec(doc.sub_before);
} }
} catch (error: any) { } catch (error: any) {
@@ -145,7 +138,7 @@ export default class SubscriptionService {
(error.stderr && error.stderr.toString()) || JSON.stringify(error); (error.stderr && error.stderr.toString()) || JSON.stringify(error);
} }
if (beforeStr) { if (beforeStr) {
await logStreamManager.write(absolutePath, `${beforeStr}\n`); await fs.appendFile(absolutePath, `${beforeStr}\n`);
} }
}, },
onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => { onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => {
@@ -164,7 +157,7 @@ export default class SubscriptionService {
let afterStr = ''; let afterStr = '';
try { try {
if (sub.sub_after) { if (sub.sub_after) {
await logStreamManager.write(absolutePath, `\n\n## 执行after命令...\n\n`); await fs.appendFile(absolutePath, `\n\n## 执行after命令...\n\n`);
afterStr = await promiseExec(sub.sub_after); afterStr = await promiseExec(sub.sub_after);
} }
} catch (error: any) { } catch (error: any) {
@@ -172,19 +165,16 @@ export default class SubscriptionService {
(error.stderr && error.stderr.toString()) || JSON.stringify(error); (error.stderr && error.stderr.toString()) || JSON.stringify(error);
} }
if (afterStr) { if (afterStr) {
await logStreamManager.write(absolutePath, `${afterStr}\n`); await fs.appendFile(absolutePath, `${afterStr}\n`);
} }
await logStreamManager.write( await fs.appendFile(
absolutePath, absolutePath,
`\n## 执行结束... ${endTime.format( `\n## 执行结束... ${endTime.format(
'YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD HH:mm:ss',
)} 耗时 ${diff}${LOG_END_SYMBOL}`, )} 耗时 ${diff}${LOG_END_SYMBOL}`,
); );
// Close the stream after task completion
await logStreamManager.closeStream(absolutePath);
await SubscriptionModel.update( await SubscriptionModel.update(
{ status: SubscriptionStatus.idle, pid: undefined }, { status: SubscriptionStatus.idle, pid: undefined },
{ where: { id: sub.id } }, { where: { id: sub.id } },
@@ -199,12 +189,12 @@ export default class SubscriptionService {
onError: async (message: string) => { onError: async (message: string) => {
const sub = await this.getDb({ id: doc.id }); const sub = await this.getDb({ id: doc.id });
const absolutePath = await handleLogPath(sub.log_path as string); const absolutePath = await handleLogPath(sub.log_path as string);
await logStreamManager.write(absolutePath, `\n${message}`); await fs.appendFile(absolutePath, `\n${message}`);
}, },
onLog: async (message: string) => { onLog: async (message: string) => {
const sub = await this.getDb({ id: doc.id }); const sub = await this.getDb({ id: doc.id });
const absolutePath = await handleLogPath(sub.log_path as string); const absolutePath = await handleLogPath(sub.log_path as string);
await logStreamManager.write(absolutePath, `\n${message}`); await fs.appendFile(absolutePath, `\n${message}`);
}, },
}; };
} }
@@ -212,12 +202,12 @@ export default class SubscriptionService {
public async create(payload: Subscription): Promise<Subscription> { public async create(payload: Subscription): Promise<Subscription> {
const tab = new Subscription(payload); const tab = new Subscription(payload);
const doc = await this.insert(tab); const doc = await this.insert(tab);
await this.handleTask(doc.get({ plain: true })); await this.handleTask(doc);
await this.setSshConfig(); await this.setSshConfig();
return doc; return doc;
} }
public async insert(payload: Subscription): Promise<SubscriptionInstance> { public async insert(payload: Subscription): Promise<Subscription> {
return await SubscriptionModel.create(payload, { returning: true }); return await SubscriptionModel.create(payload, { returning: true });
} }
@@ -269,7 +259,7 @@ export default class SubscriptionService {
public async remove(ids: number[], query: { force?: boolean }) { public async remove(ids: number[], query: { force?: boolean }) {
const docs = await SubscriptionModel.findAll({ where: { id: ids } }); const docs = await SubscriptionModel.findAll({ where: { id: ids } });
for (const doc of docs) { for (const doc of docs) {
await this.handleTask(doc.get({ plain: true }), false); await this.handleTask(doc, false);
} }
await SubscriptionModel.destroy({ where: { id: ids } }); await SubscriptionModel.destroy({ where: { id: ids } });
await this.setSshConfig(); await this.setSshConfig();
@@ -281,9 +271,7 @@ export default class SubscriptionService {
} }
for (const doc of docs) { for (const doc of docs) {
const filePath = join(config.scriptPath, doc.alias); const filePath = join(config.scriptPath, doc.alias);
const repoPath = join(config.repoPath, doc.alias);
await rmPath(filePath); await rmPath(filePath);
await rmPath(repoPath);
} }
} }
} }
@@ -338,8 +326,6 @@ export default class SubscriptionService {
name: subscription.name, name: subscription.name,
schedule: subscription.schedule, schedule: subscription.schedule,
command, command,
id: String(subscription.id),
runOrigin: 'subscription',
}); });
} }
@@ -348,7 +334,7 @@ export default class SubscriptionService {
const docs = await SubscriptionModel.findAll({ where: { id: ids } }); const docs = await SubscriptionModel.findAll({ where: { id: ids } });
await this.setSshConfig(); await this.setSshConfig();
for (const doc of docs) { for (const doc of docs) {
await this.handleTask(doc.get({ plain: true }), false); await this.handleTask(doc, false);
} }
} }
@@ -357,7 +343,7 @@ export default class SubscriptionService {
const docs = await SubscriptionModel.findAll({ where: { id: ids } }); const docs = await SubscriptionModel.findAll({ where: { id: ids } });
await this.setSshConfig(); await this.setSshConfig();
for (const doc of docs) { for (const doc of docs) {
await this.handleTask(doc.get({ plain: true })); await this.handleTask(doc);
} }
} }
@@ -388,7 +374,7 @@ export default class SubscriptionService {
files.map(async (x) => ({ files.map(async (x) => ({
filename: x, filename: x,
directory: relativeDir.replace(config.logPath, ''), directory: relativeDir.replace(config.logPath, ''),
time: (await fs.lstat(`${dir}/${x}`)).birthtimeMs, time: (await fs.lstat(`${dir}/${x}`)).mtime.getTime(),
})), })),
) )
).sort((a, b) => b.time - a.time); ).sort((a, b) => b.time - a.time);
+22 -154
View File
@@ -1,13 +1,13 @@
import { spawn } from 'cross-spawn'; import { spawn } from 'cross-spawn';
import { Response } from 'express'; import { Response } from 'express';
import fs from 'fs'; import fs from 'fs';
import { Agent, request } from 'undici'; import got from 'got';
import sum from 'lodash/sum'; import sum from 'lodash/sum';
import path from 'path'; import path from 'path';
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import winston from 'winston'; import winston from 'winston';
import config from '../config'; import config from '../config';
import { NotificationModeStringMap, TASK_COMMAND } from '../config/const'; import { TASK_COMMAND } from '../config/const';
import { import {
getPid, getPid,
killTask, killTask,
@@ -15,8 +15,6 @@ import {
parseVersion, parseVersion,
promiseExec, promiseExec,
readDirs, readDirs,
rmPath,
setSystemTimezone,
} from '../config/util'; } from '../config/util';
import { import {
DependenceModel, DependenceModel,
@@ -35,8 +33,6 @@ import taskLimit from '../shared/pLimit';
import NotificationService from './notify'; import NotificationService from './notify';
import ScheduleService, { TaskCallbacks } from './schedule'; import ScheduleService, { TaskCallbacks } from './schedule';
import SockService from './sock'; import SockService from './sock';
import os from 'os';
import dayjs from 'dayjs';
@Service() @Service()
export default class SystemService { export default class SystemService {
@@ -51,21 +47,17 @@ export default class SystemService {
public async getSystemConfig() { public async getSystemConfig() {
const doc = await this.getDb({ type: AuthDataType.systemConfig }); const doc = await this.getDb({ type: AuthDataType.systemConfig });
return { return doc;
...doc,
info: { ...doc.info, timezone: doc.info?.timezone || 'Asia/Shanghai' },
};
} }
private async updateAuthDb(payload: SystemInfo): Promise<SystemInfo> { private async updateAuthDb(payload: SystemInfo): Promise<SystemInfo> {
const { id, ...others } = payload; await SystemModel.upsert({ ...payload });
await SystemModel.update(others, { where: { id } }); const doc = await this.getDb({ type: payload.type });
const doc = await this.getDb({ id });
return doc; return doc;
} }
public async getDb(query: any): Promise<SystemInfo> { public async getDb(query: any): Promise<SystemInfo> {
const doc = await SystemModel.findOne({ where: query }); const doc = await SystemModel.findOne({ where: { ...query } });
if (!doc) { if (!doc) {
throw new Error(`System ${JSON.stringify(query)} not found`); throw new Error(`System ${JSON.stringify(query)} not found`);
} }
@@ -97,22 +89,17 @@ export default class SystemService {
info: { ...oDoc.info, ...info }, info: { ...oDoc.info, ...info },
}); });
const cron = { const cron = {
id: result.id as number, id: result.id || NaN,
name: '删除日志', name: '删除日志',
command: `ql rmlog ${info.logRemoveFrequency}`, command: `ql rmlog ${info.logRemoveFrequency}`,
runOrigin: 'system' as const,
}; };
if (oDoc.info?.logRemoveFrequency) { if (oDoc.info?.logRemoveFrequency) {
await this.scheduleService.cancelIntervalTask(cron); await this.scheduleService.cancelIntervalTask(cron);
} }
if (info.logRemoveFrequency && info.logRemoveFrequency > 0) { if (info.logRemoveFrequency && info.logRemoveFrequency > 0) {
this.scheduleService.createIntervalTask( this.scheduleService.createIntervalTask(cron, {
cron,
{
days: info.logRemoveFrequency, days: info.logRemoveFrequency,
}, });
true,
);
} }
return { code: 200, data: info }; return { code: 200, data: info };
} }
@@ -188,8 +175,6 @@ export default class SystemService {
}, },
{ {
command, command,
id: 'update-node-mirror',
runOrigin: 'system',
}, },
); );
} }
@@ -220,9 +205,6 @@ export default class SystemService {
}); });
let defaultDomain = 'https://dl-cdn.alpinelinux.org'; let defaultDomain = 'https://dl-cdn.alpinelinux.org';
let targetDomain = 'https://dl-cdn.alpinelinux.org'; let targetDomain = 'https://dl-cdn.alpinelinux.org';
if (os.platform() !== 'linux') {
return;
}
const content = await fs.promises.readFile('/etc/apk/repositories', { const content = await fs.promises.readFile('/etc/apk/repositories', {
encoding: 'utf-8', encoding: 'utf-8',
}); });
@@ -264,8 +246,6 @@ export default class SystemService {
}, },
{ {
command, command,
id: 'update-linux-mirror',
runOrigin: 'system',
}, },
); );
} }
@@ -276,17 +256,13 @@ export default class SystemService {
let lastVersionContent; let lastVersionContent;
try { try {
const { body } = await request( const result = await got.get(
`${config.lastVersionFile}?t=${Date.now()}`, `${config.lastVersionFile}?t=${Date.now()}`,
{ {
dispatcher: new Agent({ timeout: 30000,
keepAliveTimeout: 30000,
keepAliveMaxTimeout: 30000,
}),
}, },
); );
const text = await body.text(); lastVersionContent = await parseContentVersion(result.body);
lastVersionContent = parseContentVersion(text);
} catch (error) {} } catch (error) {}
if (!lastVersionContent) { if (!lastVersionContent) {
@@ -361,39 +337,13 @@ export default class SystemService {
public async reloadSystem(target?: 'system' | 'data') { public async reloadSystem(target?: 'system' | 'data') {
const cmd = `real_time=true ql reload ${target || ''}`; const cmd = `real_time=true ql reload ${target || ''}`;
const cp = spawn(cmd, { const cp = spawn(cmd, { shell: '/bin/bash' });
shell: '/bin/bash',
detached: true,
stdio: 'ignore',
});
cp.unref(); cp.unref();
setTimeout(() => {
process.exit(0);
});
return { code: 200 }; return { code: 200 };
} }
public async notify({ public async notify({ title, content }: { title: string; content: string }) {
title, const isSuccess = await this.notificationService.notify(title, content);
content,
notificationInfo,
}: {
title: string;
content: string;
notificationInfo?: NotificationInfo;
}) {
const typeString =
typeof notificationInfo?.type === 'number'
? NotificationModeStringMap[notificationInfo.type]
: undefined;
if (notificationInfo && typeString) {
notificationInfo.type = typeString;
}
const isSuccess = await this.notificationService.notify(
title,
content,
notificationInfo,
);
if (isSuccess) { if (isSuccess) {
return { code: 200, message: '通知发送成功' }; return { code: 200, message: '通知发送成功' };
} else { } else {
@@ -401,15 +351,12 @@ export default class SystemService {
} }
} }
public async run({ command, logPath }: { command: string; logPath?: string }, callback: TaskCallbacks) { public async run({ command }: { command: string }, callback: TaskCallbacks) {
if (!command.startsWith(TASK_COMMAND)) { if (!command.startsWith(TASK_COMMAND)) {
command = `${TASK_COMMAND} ${command}`; command = `${TASK_COMMAND} ${command}`;
} }
const logPathPrefix = logPath ? `real_log_path=${logPath}` : '' this.scheduleService.runTask(`real_time=true ${command}`, callback, {
this.scheduleService.runTask(`${logPathPrefix} real_time=true ${command}`, callback, {
command, command,
id: command.replace(/ /g, '-'),
runOrigin: 'system',
}); });
} }
@@ -435,16 +382,10 @@ export default class SystemService {
} }
} }
public async exportData(res: Response, type?: string[]) { public async exportData(res: Response) {
try { try {
let dataDirs = ['db', 'upload'];
if (type && type.length) {
dataDirs = dataDirs.concat(type.filter((x) => x !== 'base'));
}
const dataPaths = dataDirs.map((dir) => `data/${dir}`);
await promiseExec( await promiseExec(
`cd ${config.dataPath} && cd ../ && tar -zcvf ${config.dataTgzFile `cd ${config.rootPath} && tar -zcvf ${config.dataTgzFile} data/`,
} ${dataPaths.join(' ')}`,
); );
res.download(config.dataTgzFile); res.download(config.dataTgzFile);
} catch (error: any) { } catch (error: any) {
@@ -456,7 +397,7 @@ export default class SystemService {
try { try {
await promiseExec(`rm -rf ${path.join(config.tmpPath, 'data')}`); await promiseExec(`rm -rf ${path.join(config.tmpPath, 'data')}`);
const res = await promiseExec( const res = await promiseExec(
`cd ${config.tmpPath} && tar -zxvf ${config.dataTgzFile}`, `cd ${config.tmpPath} && tar -zxvf data.tgz`,
); );
return { code: 200, data: res }; return { code: 200, data: res };
} catch (error: any) { } catch (error: any) {
@@ -464,25 +405,9 @@ export default class SystemService {
} }
} }
public async getSystemLog( public async getSystemLog(res: Response) {
res: Response,
query: {
startTime?: string;
endTime?: string;
},
) {
const startTime = dayjs(query.startTime || undefined)
.startOf('d')
.valueOf();
const endTime = dayjs(query.endTime || undefined)
.endOf('d')
.valueOf();
const result = await readDirs(config.systemLogPath, config.systemLogPath); const result = await readDirs(config.systemLogPath, config.systemLogPath);
const logs = result const logs = result.reverse().filter((x) => x.title.endsWith('.log'));
.reverse()
.filter((x) => x.title.endsWith('.log'))
.filter((x) => x.createTime >= startTime && x.createTime <= endTime);
res.set({ res.set({
'Content-Length': sum(logs.map((x) => x.size)), 'Content-Length': sum(logs.map((x) => x.size)),
}); });
@@ -504,61 +429,4 @@ export default class SystemService {
} }
})(res, logs); })(res, logs);
} }
public async deleteSystemLog() {
const result = await readDirs(config.systemLogPath, config.systemLogPath);
const logs = result.reverse().filter((x) => x.title.endsWith('.log'));
for (const log of logs) {
await rmPath(path.join(config.systemLogPath, log.title));
}
}
public async updateTimezone(info: SystemModelInfo) {
if (!info.timezone) {
info.timezone = 'Asia/Shanghai';
}
const oDoc = await this.getSystemConfig();
await this.updateAuthDb({
...oDoc,
info: { ...oDoc.info, ...info },
});
const success = await setSystemTimezone(info.timezone);
if (success) {
return { code: 200, data: info };
} else {
return { code: 400, message: '设置时区失败' };
}
}
public async updateGlobalSshKey(info: SystemModelInfo) {
const oDoc = await this.getSystemConfig();
const result = await this.updateAuthDb({
...oDoc,
info: { ...oDoc.info, ...info },
});
// Apply the global SSH key
const SshKeyService = require('./sshKey').default;
const Container = require('typedi').Container;
const sshKeyService = Container.get(SshKeyService);
if (info.globalSshKey) {
await sshKeyService.addGlobalSSHKey(info.globalSshKey, 'global');
} else {
await sshKeyService.removeGlobalSSHKey('global');
}
return { code: 200, data: result };
}
public async cleanDependence(type: 'node' | 'python3') {
if (!type || !['node', 'python3'].includes(type)) {
return { code: 400, message: '参数错误' };
}
try {
const finalPath = path.join(config.dependenceCachePath, type);
await fs.promises.rm(finalPath, { recursive: true });
} catch (error) { }
return { code: 200 };
}
} }
+94 -211
View File
@@ -1,7 +1,15 @@
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import winston from 'winston'; import winston from 'winston';
import { createRandomString } from '../config/util'; import {
createFile,
createRandomString,
fileExist,
getNetIp,
getPlatform,
safeJSONParse,
} from '../config/util';
import config from '../config'; import config from '../config';
import * as fs from 'fs/promises';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import { authenticator } from '@otplib/preset-default'; import { authenticator } from '@otplib/preset-default';
import { import {
@@ -10,8 +18,6 @@ import {
SystemModel, SystemModel,
SystemModelInfo, SystemModelInfo,
LoginStatus, LoginStatus,
AuthInfo,
TokenInfo,
} from '../data/system'; } from '../data/system';
import { NotificationInfo } from '../data/notify'; import { NotificationInfo } from '../data/notify';
import NotificationService from './notify'; import NotificationService from './notify';
@@ -22,9 +28,6 @@ import dayjs from 'dayjs';
import IP2Region from 'ip2region'; import IP2Region from 'ip2region';
import requestIp from 'request-ip'; import requestIp from 'request-ip';
import uniq from 'lodash/uniq'; import uniq from 'lodash/uniq';
import pickBy from 'lodash/pickBy';
import isNil from 'lodash/isNil';
import { shareStore } from '../shared/store';
@Service() @Service()
export default class UserService { export default class UserService {
@@ -45,9 +48,15 @@ export default class UserService {
req: Request, req: Request,
needTwoFactor = true, needTwoFactor = true,
): Promise<any> { ): Promise<any> {
const _exist = await fileExist(config.authConfigFile);
if (!_exist) {
return this.initAuthInfo();
}
let { username, password } = payloads; let { username, password } = payloads;
const content = await this.getAuthInfo(); const content = await this.getAuthInfo();
const timestamp = Date.now(); const timestamp = Date.now();
if (content) {
let { let {
username: cUsername, username: cUsername,
password: cPassword, password: cPassword,
@@ -56,9 +65,21 @@ export default class UserService {
lastip, lastip,
lastaddr, lastaddr,
twoFactorActivated, twoFactorActivated,
twoFactorActived,
tokens = {}, tokens = {},
platform, platform,
} = content; } = content;
// patch old field
twoFactorActivated = twoFactorActivated || twoFactorActived;
if (
(cUsername === 'admin' && cPassword === 'admin') ||
!cUsername ||
!cPassword
) {
return this.initAuthInfo();
}
const retriesTime = Math.pow(3, retries) * 1000; const retriesTime = Math.pow(3, retries) * 1000;
if (retries > 2 && timestamp - lastlogon < retriesTime) { if (retries > 2 && timestamp - lastlogon < retriesTime) {
const waitTime = Math.ceil( const waitTime = Math.ceil(
@@ -77,7 +98,7 @@ export default class UserService {
twoFactorActivated && twoFactorActivated &&
needTwoFactor needTwoFactor
) { ) {
await this.updateAuthInfo(content, { this.updateAuthInfo(content, {
isTwoFactorChecking: true, isTwoFactorChecking: true,
}); });
return { return {
@@ -92,33 +113,24 @@ export default class UserService {
let address = ''; let address = '';
if (ipAddress) { if (ipAddress) {
const { country, province, city, isp } = ipAddress; const { country, province, city, isp } = ipAddress;
address = uniq([country, province, city, isp]).filter(Boolean).join(' '); address = uniq([country, province, city, isp])
.filter(Boolean)
.join(' ');
} }
if (username === cUsername && password === cPassword) { if (username === cUsername && password === cPassword) {
const data = createRandomString(50, 100); const data = createRandomString(50, 100);
const expiration = twoFactorActivated ? '60d' : '20d'; const expiration = twoFactorActivated ? 60 : 20;
let token = jwt.sign({ data }, config.jwt.secret, { let token = jwt.sign({ data }, config.secret as any, {
expiresIn: config.jwt.expiresIn || expiration, expiresIn: 60 * 60 * 24 * expiration,
algorithm: 'HS384', algorithm: 'HS384',
}); });
const tokenInfo: TokenInfo = { this.updateAuthInfo(content, {
value: token,
timestamp,
ip,
address,
platform: req.platform,
};
const updatedTokens = this.addTokenToList(
tokens,
req.platform,
tokenInfo,
);
await this.updateAuthInfo(content, {
token, token,
tokens: updatedTokens, tokens: {
...tokens,
[req.platform]: token,
},
lastlogon: timestamp, lastlogon: timestamp,
retries: 0, retries: 0,
lastip: ip, lastip: ip,
@@ -142,20 +154,12 @@ export default class UserService {
status: LoginStatus.success, status: LoginStatus.success,
}, },
}); });
this.getLoginLog();
return { return {
code: 200, code: 200,
data: { data: { token, lastip, lastaddr, lastlogon, retries, platform },
token,
lastip,
lastaddr,
lastlogon,
retries,
platform,
},
}; };
} else { } else {
await this.updateAuthInfo(content, { this.updateAuthInfo(content, {
retries: retries + 1, retries: retries + 1,
lastlogon: timestamp, lastlogon: timestamp,
lastip: ip, lastip: ip,
@@ -178,7 +182,6 @@ export default class UserService {
status: LoginStatus.fail, status: LoginStatus.fail,
}, },
}); });
this.getLoginLog();
if (retries > 2) { if (retries > 2) {
const waitTime = Math.round(Math.pow(3, retries + 1)); const waitTime = Math.round(Math.pow(3, retries + 1));
return { return {
@@ -190,39 +193,16 @@ export default class UserService {
return { code: 400, message: config.authError }; return { code: 400, message: config.authError };
} }
} }
} else {
return this.initAuthInfo();
}
} }
public async logout(platform: string, tokenValue: string): Promise<any> { public async logout(platform: string): Promise<any> {
if (!platform || !tokenValue) {
this.logger.warn('Invalid logout parameters - empty platform or token');
return;
}
const authInfo = await this.getAuthInfo(); const authInfo = await this.getAuthInfo();
this.updateAuthInfo(authInfo, {
// Verify the token exists before attempting to remove it token: '',
const tokenExists = this.findTokenInList( tokens: { ...authInfo.tokens, [platform]: '' },
authInfo.tokens,
platform,
tokenValue,
);
if (!tokenExists && authInfo.token !== tokenValue) {
// Token not found, but don't throw error - user may have already logged out
this.logger.info(
`Logout attempted for non-existent token on platform: ${platform}`,
);
return;
}
const updatedTokens = this.removeTokenFromList(
authInfo.tokens,
platform,
tokenValue,
);
await this.updateAuthInfo(authInfo, {
token: authInfo.token === tokenValue ? '' : authInfo.token,
tokens: updatedTokens,
}); });
} }
@@ -235,9 +215,8 @@ export default class UserService {
(a, b) => b.info!.timestamp! - a.info!.timestamp!, (a, b) => b.info!.timestamp! - a.info!.timestamp!,
); );
if (result.length > 100) { if (result.length > 100) {
const ids = result.slice(100).map((x) => x.id!);
await SystemModel.destroy({ await SystemModel.destroy({
where: { id: ids }, where: { id: result[result.length - 1].id },
}); });
} }
return result.map((x) => x.info); return result.map((x) => x.info);
@@ -250,6 +229,20 @@ export default class UserService {
return doc; return doc;
} }
private async initAuthInfo() {
await fs.writeFile(
config.authConfigFile,
JSON.stringify({
username: 'admin',
password: 'admin',
}),
);
return {
code: 100,
message: '未找到认证文件,重新初始化',
};
}
public async updateUsernameAndPassword({ public async updateUsernameAndPassword({
username, username,
password, password,
@@ -261,21 +254,35 @@ export default class UserService {
return { code: 400, message: '密码不能设置为admin' }; return { code: 400, message: '密码不能设置为admin' };
} }
const authInfo = await this.getAuthInfo(); const authInfo = await this.getAuthInfo();
await this.updateAuthInfo(authInfo, { username, password }); this.updateAuthInfo(authInfo, { username, password });
return { code: 200, message: '更新成功' }; return { code: 200, message: '更新成功' };
} }
public async updateAvatar(avatar: string) { public async updateAvatar(avatar: string) {
const authInfo = await this.getAuthInfo(); const authInfo = await this.getAuthInfo();
await this.updateAuthInfo(authInfo, { avatar }); this.updateAuthInfo(authInfo, { avatar });
return { code: 200, data: avatar, message: '更新成功' }; return { code: 200, data: avatar, message: '更新成功' };
} }
public async getUserInfo(): Promise<any> {
const authFileExist = await fileExist(config.authConfigFile);
if (!authFileExist) {
await createFile(
config.authConfigFile,
JSON.stringify({
username: 'admin',
password: 'admin',
}),
);
}
return await this.getAuthInfo();
}
public async initTwoFactor() { public async initTwoFactor() {
const secret = authenticator.generateSecret(); const secret = authenticator.generateSecret();
const authInfo = await this.getAuthInfo(); const authInfo = await this.getAuthInfo();
const otpauth = authenticator.keyuri(authInfo.username, 'qinglong', secret); const otpauth = authenticator.keyuri(authInfo.username, 'qinglong', secret);
await this.updateAuthInfo(authInfo, { twoFactorSecret: secret }); this.updateAuthInfo(authInfo, { twoFactorSecret: secret });
return { secret, url: otpauth }; return { secret, url: otpauth };
} }
@@ -286,7 +293,7 @@ export default class UserService {
secret: authInfo.twoFactorSecret, secret: authInfo.twoFactorSecret,
}); });
if (isValid) { if (isValid) {
await this.updateAuthInfo(authInfo, { twoFactorActivated: true }); this.updateAuthInfo(authInfo, { twoFactorActivated: true });
} }
return isValid; return isValid;
} }
@@ -311,17 +318,8 @@ export default class UserService {
if (isValid) { if (isValid) {
return this.login({ username, password }, req, false); return this.login({ username, password }, req, false);
} else { } else {
const ip = requestIp.getClientIp(req) || ''; const { ip, address } = await getNetIp(req);
const query = new IP2Region(); this.updateAuthInfo(authInfo, {
const ipAddress = query.search(ip);
let address = '';
if (ipAddress) {
const { country, province, city, isp } = ipAddress;
address = uniq([country, province, city, isp])
.filter(Boolean)
.join(' ');
}
await this.updateAuthInfo(authInfo, {
lastip: ip, lastip: ip,
lastaddr: address, lastaddr: address,
platform: req.platform, platform: req.platform,
@@ -332,29 +330,24 @@ export default class UserService {
public async deactiveTwoFactor() { public async deactiveTwoFactor() {
const authInfo = await this.getAuthInfo(); const authInfo = await this.getAuthInfo();
await this.updateAuthInfo(authInfo, { this.updateAuthInfo(authInfo, {
twoFactorActivated: false, twoFactorActivated: false,
twoFactorActived: false,
twoFactorSecret: '', twoFactorSecret: '',
}); });
return true; return true;
} }
public async getAuthInfo() { private async getAuthInfo() {
const authInfo = await shareStore.getAuthInfo(); const content = await fs.readFile(config.authConfigFile, 'utf8');
if (authInfo) { return safeJSONParse(content);
return authInfo;
}
const doc = await this.getDb({ type: AuthDataType.authConfig });
return (doc.info || {}) as AuthInfo;
} }
private async updateAuthInfo(authInfo: AuthInfo, info: Partial<AuthInfo>) { private async updateAuthInfo(authInfo: any, info: any) {
const result = { ...authInfo, ...info }; await fs.writeFile(
await shareStore.updateAuthInfo(result); config.authConfigFile,
await this.updateAuthDb({ JSON.stringify({ ...authInfo, ...info }),
type: AuthDataType.authConfig, );
info: result,
});
} }
public async getNotificationMode(): Promise<NotificationInfo> { public async getNotificationMode(): Promise<NotificationInfo> {
@@ -363,7 +356,7 @@ export default class UserService {
} }
private async updateAuthDb(payload: SystemInfo): Promise<any> { private async updateAuthDb(payload: SystemInfo): Promise<any> {
let doc = await SystemModel.findOne({ where: { type: payload.type } }); let doc = await SystemModel.findOne({ type: payload.type });
if (doc) { if (doc) {
const updateResult = await SystemModel.update(payload, { const updateResult = await SystemModel.update(payload, {
where: { id: doc.id }, where: { id: doc.id },
@@ -401,114 +394,4 @@ export default class UserService {
return { code: 400, message: '通知发送失败,请检查参数' }; return { code: 400, message: '通知发送失败,请检查参数' };
} }
} }
private normalizeTokens(
tokens: Record<string, string | TokenInfo[]>,
): Record<string, TokenInfo[]> {
const normalized: Record<string, TokenInfo[]> = {};
for (const [platform, value] of Object.entries(tokens)) {
if (typeof value === 'string') {
// Legacy format: convert string token to TokenInfo array
if (value) {
normalized[platform] = [
{
value,
timestamp: Date.now(),
ip: '',
address: '',
platform,
},
];
} else {
normalized[platform] = [];
}
} else {
// Already in new format
normalized[platform] = value || [];
}
}
return normalized;
}
private addTokenToList(
tokens: Record<string, string | TokenInfo[]>,
platform: string,
tokenInfo: TokenInfo,
maxTokensPerPlatform: number = config.maxTokensPerPlatform,
): Record<string, TokenInfo[]> {
// Validate maxTokensPerPlatform parameter
if (!Number.isInteger(maxTokensPerPlatform) || maxTokensPerPlatform < 1) {
this.logger.warn(
`Invalid maxTokensPerPlatform value: ${maxTokensPerPlatform}, using default`,
);
maxTokensPerPlatform = config.maxTokensPerPlatform;
}
const normalized = this.normalizeTokens(tokens);
if (!normalized[platform]) {
normalized[platform] = [];
}
// Add new token
normalized[platform].unshift(tokenInfo);
// Limit the number of active tokens per platform
if (normalized[platform].length > maxTokensPerPlatform) {
normalized[platform] = normalized[platform].slice(
0,
maxTokensPerPlatform,
);
}
return normalized;
}
private removeTokenFromList(
tokens: Record<string, string | TokenInfo[]>,
platform: string,
tokenValue: string,
): Record<string, TokenInfo[]> {
const normalized = this.normalizeTokens(tokens);
if (normalized[platform]) {
normalized[platform] = normalized[platform].filter(
(t) => t.value !== tokenValue,
);
}
return normalized;
}
private findTokenInList(
tokens: Record<string, string | TokenInfo[]>,
platform: string,
tokenValue: string,
): TokenInfo | undefined {
const normalized = this.normalizeTokens(tokens);
if (normalized[platform]) {
return normalized[platform].find((t) => t.value === tokenValue);
}
return undefined;
}
public async resetAuthInfo(info: Partial<AuthInfo>) {
const { retries, twoFactorActivated, password, username } = info;
const authInfo = await this.getAuthInfo();
const payload = pickBy(
{
retries,
twoFactorActivated,
password,
username,
},
(x) => !isNil(x),
);
await this.updateAuthInfo(authInfo, payload);
}
} }
-46
View File
@@ -1,46 +0,0 @@
import { AuthInfo, TokenInfo } from '../data/system';
/**
* Validates if a token exists in the authentication info.
* Supports both legacy string tokens and new TokenInfo array format.
*
* @param authInfo - The authentication information
* @param headerToken - The token to validate
* @param platform - The platform (desktop, mobile)
* @returns true if the token is valid, false otherwise
*/
export function isValidToken(
authInfo: AuthInfo | null | undefined,
headerToken: string,
platform: string,
): boolean {
if (!authInfo || !headerToken) {
return false;
}
const { token = '', tokens = {} } = authInfo;
// Check legacy token field
if (headerToken === token) {
return true;
}
// Check platform-specific tokens (support both legacy string and new TokenInfo[] format)
const platformTokens = tokens[platform];
// Handle null/undefined platformTokens
if (platformTokens === null || platformTokens === undefined) {
return false;
}
if (typeof platformTokens === 'string') {
// Legacy format: single string token
return headerToken === platformTokens;
} else if (Array.isArray(platformTokens)) {
// New format: array of TokenInfo objects
return platformTokens.some((t: TokenInfo) => t && t.value === headerToken);
}
// Unexpected type - log warning and reject
return false;
}
-33
View File
@@ -1,33 +0,0 @@
import { Dependence } from '../data/dependence';
import { ICron } from '../protos/cron';
export type Override<
T,
K extends Partial<{ [P in keyof T]: any }> | string,
> = K extends string
? Omit<T, K> & { [P in keyof T]: T[P] | unknown }
: Omit<T, keyof K> & K;
export type TCron = Override<Partial<ICron>, { id: string }>;
export interface IDependencyFn<T> {
(): Promise<T>;
dependency?: Dependence;
}
export interface ICronFn<T> {
(): Promise<T>;
cron?: TCron;
}
export interface ISchedule {
schedule?: string;
name?: string;
command?: string;
id: string;
}
export interface IScheduleFn<T> {
(): Promise<T>;
schedule?: ISchedule;
}
-110
View File
@@ -1,110 +0,0 @@
import { createWriteStream, WriteStream } from 'fs';
import { EventEmitter } from 'events';
/**
* Manages write streams for log files to improve performance by avoiding repeated file opens
*/
export class LogStreamManager extends EventEmitter {
private streams: Map<string, WriteStream> = new Map();
private pendingWrites: Map<string, Promise<void>> = new Map();
/**
* Write data to a log file using a managed stream
* @param filePath - Absolute path to the log file
* @param data - Data to write to the log file
*/
async write(filePath: string, data: string): Promise<void> {
// Wait for any pending writes to this file to complete
const pending = this.pendingWrites.get(filePath);
if (pending) {
await pending;
}
// Create a new promise for this write operation
const writePromise = new Promise<void>((resolve, reject) => {
let stream = this.streams.get(filePath);
if (!stream) {
// Create a new write stream if one doesn't exist
stream = createWriteStream(filePath, { flags: 'a' });
this.streams.set(filePath, stream);
// Handle stream errors
stream.on('error', (error) => {
this.emit('error', { filePath, error });
// Remove the stream from the map on error
this.streams.delete(filePath);
reject(error);
});
}
// Write the data
const canContinue = stream.write(data, 'utf8', (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
// Handle backpressure
if (!canContinue) {
stream.once('drain', () => {
// Stream is ready for more data
});
}
});
this.pendingWrites.set(filePath, writePromise);
try {
await writePromise;
} finally {
this.pendingWrites.delete(filePath);
}
}
/**
* Close the stream for a specific file path
* @param filePath - Absolute path to the log file
*/
async closeStream(filePath: string): Promise<void> {
// Wait for any pending writes to complete
const pending = this.pendingWrites.get(filePath);
if (pending) {
await pending.catch(() => {
// Ignore errors on pending writes during close
});
}
const stream = this.streams.get(filePath);
if (stream) {
return new Promise<void>((resolve) => {
stream.end(() => {
this.streams.delete(filePath);
resolve();
});
});
}
}
/**
* Close all open streams
*/
async closeAll(): Promise<void> {
const closePromises = Array.from(this.streams.keys()).map((filePath) =>
this.closeStream(filePath),
);
await Promise.all(closePromises);
}
/**
* Get the number of open streams
*/
getOpenStreamCount(): number {
return this.streams.size;
}
}
// Export a singleton instance for shared use
export const logStreamManager = new LogStreamManager();
+5 -106
View File
@@ -3,23 +3,14 @@ import os from 'os';
import { AuthDataType, SystemModel } from '../data/system'; import { AuthDataType, SystemModel } from '../data/system';
import Logger from '../loaders/logger'; import Logger from '../loaders/logger';
import { Dependence } from '../data/dependence'; import { Dependence } from '../data/dependence';
import NotificationService from '../services/notify';
import {
ICronFn,
IDependencyFn,
ISchedule,
IScheduleFn,
TCron,
} from './interface';
import config from '../config';
import { credentials } from '@grpc/grpc-js';
import { ApiClient } from '../protos/api';
interface IDependencyFn<T> {
(): Promise<T>;
dependency?: Dependence;
}
class TaskLimit { class TaskLimit {
private dependenyLimit = new PQueue({ concurrency: 1 }); private dependenyLimit = new PQueue({ concurrency: 1 });
private queuedDependencyIds = new Set<number>([]); private queuedDependencyIds = new Set<number>([]);
private queuedCrons = new Map<string, ICronFn<any>[]>();
private repeatCronNotifyMap = new Map<string, number>();
private updateLogLimit = new PQueue({ concurrency: 1 }); private updateLogLimit = new PQueue({ concurrency: 1 });
private cronLimit = new PQueue({ private cronLimit = new PQueue({
concurrency: Math.max(os.cpus().length, 4), concurrency: Math.max(os.cpus().length, 4),
@@ -27,20 +18,6 @@ class TaskLimit {
private manualCronoLimit = new PQueue({ private manualCronoLimit = new PQueue({
concurrency: Math.max(os.cpus().length, 4), concurrency: Math.max(os.cpus().length, 4),
}); });
private subscriptionLimit = new PQueue({
concurrency: Math.max(os.cpus().length, 4),
});
private scriptLimit = new PQueue({
concurrency: Math.max(os.cpus().length, 4),
});
private systemLimit = new PQueue({
concurrency: Math.max(os.cpus().length, 4),
});
private client = new ApiClient(
`0.0.0.0:${config.grpcPort}`,
credentials.createInsecure(),
{ 'grpc.enable_http_proxy': 0 },
);
get cronLimitActiveCount() { get cronLimitActiveCount() {
return this.cronLimit.pending; return this.cronLimit.pending;
@@ -54,8 +31,6 @@ class TaskLimit {
return [...this.queuedDependencyIds.values()][0]; return [...this.queuedDependencyIds.values()][0];
} }
private notificationService: NotificationService = new NotificationService();
constructor() { constructor() {
this.setCustomLimit(); this.setCustomLimit();
this.handleEvents(); this.handleEvents();
@@ -96,16 +71,6 @@ class TaskLimit {
} }
} }
public removeQueuedCron(id: string) {
if (this.queuedCrons.has(id)) {
const runs = this.queuedCrons.get(id);
if (runs && runs.length > 0) {
runs.pop();
this.queuedCrons.set(id, runs);
}
}
}
public async setCustomLimit(limit?: number) { public async setCustomLimit(limit?: number) {
if (limit) { if (limit) {
this.cronLimit.concurrency = limit; this.cronLimit.concurrency = limit;
@@ -123,35 +88,9 @@ class TaskLimit {
} }
public async runWithCronLimit<T>( public async runWithCronLimit<T>(
cron: TCron, fn: () => Promise<T>,
fn: ICronFn<T>,
options?: Partial<QueueAddOptions>, options?: Partial<QueueAddOptions>,
): Promise<T | void> { ): Promise<T | void> {
fn.cron = cron;
let runs = this.queuedCrons.get(cron.id);
const result = runs?.length ? [...runs, fn] : [fn];
const repeatTimes = this.repeatCronNotifyMap.get(cron.id) || 0;
if (result?.length > 5) {
if (repeatTimes < 3) {
this.repeatCronNotifyMap.set(cron.id, repeatTimes + 1);
this.client.systemNotify(
{
title: '任务重复运行',
content: `任务:${cron.name},命令:${cron.command},定时:${cron.schedule},处于运行中的超过 5 个,请检查定时设置`,
},
(err, res) => {
if (err) {
Logger.error(
`[schedule][任务重复运行] 通知失败 ${JSON.stringify(err)}`,
);
}
},
);
}
Logger.warn(`[schedule][任务重复运行] 参数 ${JSON.stringify(cron)}`);
return;
}
this.queuedCrons.set(cron.id, result);
return this.cronLimit.add(fn, options); return this.cronLimit.add(fn, options);
} }
@@ -162,46 +101,6 @@ class TaskLimit {
return this.manualCronoLimit.add(fn, options); return this.manualCronoLimit.add(fn, options);
} }
public async runWithSubscriptionLimit<T>(
schedule: TCron,
fn: IScheduleFn<T>,
options?: Partial<QueueAddOptions>,
): Promise<T | void> {
fn.schedule = schedule;
return this.subscriptionLimit.add(fn, options);
}
public async runWithSystemLimit<T>(
schedule: TCron,
fn: IScheduleFn<T>,
options?: Partial<QueueAddOptions>,
): Promise<T | void> {
fn.schedule = schedule;
return this.systemLimit.add(fn, options);
}
public async runWithScriptLimit<T>(
schedule: ISchedule,
fn: IScheduleFn<T>,
options?: Partial<QueueAddOptions>,
): Promise<T | void> {
fn.schedule = schedule;
return this.scriptLimit.add(fn, options);
}
public async waitDependencyQueueDone(): Promise<void> {
if (this.dependenyLimit.size === 0 && this.dependenyLimit.pending === 0) {
return;
}
return new Promise((resolve) => {
const onIdle = () => {
this.dependenyLimit.removeListener('idle', onIdle);
resolve();
};
this.dependenyLimit.on('idle', onIdle);
});
}
public runDependeny<T>( public runDependeny<T>(
dependency: Dependence, dependency: Dependence,
fn: IDependencyFn<T>, fn: IDependencyFn<T>,
+4 -54
View File
@@ -1,52 +1,11 @@
import { spawn } from 'cross-spawn'; import { spawn } from 'cross-spawn';
import taskLimit from './pLimit'; import taskLimit from './pLimit';
import Logger from '../loaders/logger'; import Logger from '../loaders/logger';
import { ICron } from '../protos/cron';
import { CrontabModel, CrontabStatus } from '../data/cron';
import { killTask } from '../config/util';
export function runCron(cmd: string, cron: ICron): Promise<number | void> { export function runCron(cmd: string, options?: { schedule: string; extraSchedules: Array<{ schedule: string }>; name: string }): Promise<number | void> {
return taskLimit.runWithCronLimit(cron, () => { return taskLimit.runWithCronLimit(() => {
return new Promise(async (resolve: any) => { return new Promise(async (resolve: any) => {
// Check if the cron is already running and stop it (only if multiple instances are not allowed) Logger.info(`[schedule][开始执行任务] 参数 ${JSON.stringify({ ...options, command: cmd })}`);
try {
const existingCron = await CrontabModel.findOne({
where: { id: Number(cron.id) },
});
// Default to single instance mode (0) for backward compatibility
const allowSingleInstances =
existingCron?.allow_multiple_instances === 0;
if (
allowSingleInstances &&
existingCron &&
existingCron.pid &&
(existingCron.status === CrontabStatus.running ||
existingCron.status === CrontabStatus.queued)
) {
Logger.info(
`[schedule][停止已运行任务] 任务ID: ${cron.id}, PID: ${existingCron.pid}`,
);
await killTask(existingCron.pid);
// Update the status to idle after killing
await CrontabModel.update(
{ status: CrontabStatus.idle, pid: undefined },
{ where: { id: Number(cron.id) } },
);
}
} catch (error) {
Logger.error(
`[schedule][检查已运行任务失败] 任务ID: ${cron.id}, 错误: ${error}`,
);
}
Logger.info(
`[schedule][开始执行任务] 参数 ${JSON.stringify({
...cron,
command: cmd,
})}`,
);
const cp = spawn(cmd, { shell: '/bin/bash' }); const cp = spawn(cmd, { shell: '/bin/bash' });
cp.stderr.on('data', (data) => { cp.stderr.on('data', (data) => {
@@ -65,16 +24,7 @@ export function runCron(cmd: string, cron: ICron): Promise<number | void> {
}); });
cp.on('exit', async (code) => { cp.on('exit', async (code) => {
taskLimit.removeQueuedCron(cron.id); resolve({ ...options, command: cmd, pid: cp.pid, code });
Logger.info(
'[schedule][执行任务结束] 参数: %s, 退出码: %j',
JSON.stringify({
...cron,
command: cmd,
}),
code,
);
resolve({ ...cron, command: cmd, pid: cp.pid, code });
}); });
}); });
}); });
-34
View File
@@ -1,34 +0,0 @@
import { AuthInfo } from '../data/system';
import { App } from '../data/open';
import Keyv from 'keyv';
import KeyvSqlite from '@keyv/sqlite';
import config from '../config';
import path from 'path';
export enum EKeyv {
'apps' = 'apps',
'authInfo' = 'authInfo',
}
export interface IKeyvStore {
apps: App[];
authInfo: AuthInfo;
}
const keyvSqlite = new KeyvSqlite(path.join(config.dbPath, 'keyv.sqlite'));
export const keyvStore = new Keyv<IKeyvStore>({ store: keyvSqlite });
export const shareStore = {
getAuthInfo() {
return keyvStore.get<IKeyvStore['authInfo']>(EKeyv.authInfo);
},
updateAuthInfo(value: IKeyvStore['authInfo']) {
return keyvStore.set<IKeyvStore['authInfo']>(EKeyv.authInfo, value);
},
getApps() {
return keyvStore.get<IKeyvStore['apps']>(EKeyv.apps);
},
updateApps(apps: App[]) {
return keyvStore.set<IKeyvStore['apps']>(EKeyv.apps, apps);
},
};
-42
View File
@@ -1,42 +0,0 @@
import { lock } from 'proper-lockfile';
import os from 'os';
import path from 'path';
import { writeFile, open, chmod } from 'fs/promises';
import { fileExist } from '../config/util';
function getUniqueLockPath(filePath: string) {
const sanitizedPath = filePath
.replace(/[<>:"/\\|?*]/g, '_')
.replace(/^_/, '');
return path.join(os.tmpdir(), `${sanitizedPath}.ql_lock`);
}
export async function writeFileWithLock(
filePath: string,
content: string,
options: Parameters<typeof writeFile>[2] = {},
) {
if (typeof options === 'string') {
options = { encoding: options };
}
if (!(await fileExist(filePath))) {
const fileHandle = await open(filePath, 'w');
fileHandle.close();
}
const lockfilePath = getUniqueLockPath(filePath);
const release = await lock(filePath, {
retries: {
retries: 10,
factor: 2,
minTimeout: 100,
maxTimeout: 3000,
},
lockfilePath,
});
await writeFile(filePath, content, { encoding: 'utf8', ...options });
if (options?.mode) {
await chmod(filePath, options.mode);
}
await release();
}
+11 -2
View File
@@ -2,10 +2,10 @@ import 'reflect-metadata';
import OpenService from './services/open'; import OpenService from './services/open';
import { Container } from 'typedi'; import { Container } from 'typedi';
import LoggerInstance from './loaders/logger'; import LoggerInstance from './loaders/logger';
import fs from 'fs';
import config from './config'; import config from './config';
import path from 'path'; import path from 'path';
import os from 'os'; import os from 'os';
import { writeFileWithLock } from './shared/utils';
const tokenFile = path.join(config.configPath, 'token.json'); const tokenFile = path.join(config.configPath, 'token.json');
@@ -25,7 +25,16 @@ async function getToken() {
} }
async function writeFile(data: any) { async function writeFile(data: any) {
await writeFileWithLock(tokenFile, `${JSON.stringify(data)}${os.EOL}`); return new Promise<void>((resolve, reject) => {
fs.writeFile(
tokenFile,
`${JSON.stringify(data)}${os.EOL}`,
{ encoding: 'utf8' },
() => {
resolve();
},
);
});
} }
getToken(); getToken();
-11
View File
@@ -1,11 +0,0 @@
/// <reference types="express" />
export {};
declare global {
namespace Express {
interface Request {
platform: 'desktop' | 'mobile';
}
}
}
+27
View File
@@ -0,0 +1,27 @@
import 'reflect-metadata'; // We need this in order to use @Decorators
import config from './config';
import express from 'express';
import depInjectorLoader from './loaders/depInjector';
import Logger from './loaders/logger';
async function startServer() {
const app = express();
depInjectorLoader();
await require('./loaders/update').default({ app });
app
.listen(config.updatePort, '0.0.0.0', () => {
Logger.debug(`✌️ 更新服务启动成功!`);
console.debug(`✌️ 更新服务启动成功!`);
process.send?.('ready');
})
.on('error', (err) => {
Logger.error(err);
console.error(err);
process.exit(1);
});
}
startServer();
-85
View File
@@ -1,85 +0,0 @@
import { Joi } from 'celebrate';
import CronExpressionParser from 'cron-parser';
import { ScheduleType } from '../interface/schedule';
import path from 'path';
import config from '../config';
const validateSchedule = (value: string, helpers: any) => {
if (
value.startsWith(ScheduleType.ONCE) ||
value.startsWith(ScheduleType.BOOT)
) {
return value;
}
try {
if (CronExpressionParser.parse(value).hasNext()) {
return value;
}
} catch (e) {
return helpers.error('any.invalid');
}
return helpers.error('any.invalid');
};
export const scheduleSchema = Joi.string()
.required()
.custom(validateSchedule)
.messages({
'any.invalid': '无效的定时规则',
'string.empty': '定时规则不能为空',
});
export const commonCronSchema = {
name: Joi.string().optional(),
command: Joi.string().required(),
schedule: scheduleSchema,
labels: Joi.array().optional(),
sub_id: Joi.number().optional().allow(null),
extra_schedules: Joi.array().optional().allow(null),
task_before: Joi.string().optional().allow('').allow(null),
task_after: Joi.string().optional().allow('').allow(null),
log_name: Joi.string()
.optional()
.allow('')
.allow(null)
.custom((value, helpers) => {
if (!value) return value;
// Check if it's an absolute path
if (value.startsWith('/')) {
// Allow /dev/null as special case
if (value === '/dev/null') {
return value;
}
// For other absolute paths, ensure they are within the safe log directory
const normalizedValue = path.normalize(value);
const normalizedLogPath = path.normalize(config.logPath);
if (!normalizedValue.startsWith(normalizedLogPath)) {
return helpers.error('string.unsafePath');
}
return value;
}
if (
!/^(?!.*(?:^|\/)\.{1,2}(?:\/|$))(?:\/)?(?:[\w.-]+\/)*[\w.-]+\/?$/.test(
value,
)
) {
return helpers.error('string.pattern.base');
}
if (value.length > 100) {
return helpers.error('string.max');
}
return value;
})
.messages({
'string.pattern.base': '日志名称只能包含字母、数字、下划线和连字符',
'string.max': '日志名称不能超过100个字符',
'string.unsafePath': '绝对路径必须在日志目录内或使用 /dev/null',
}),
allow_multiple_instances: Joi.number().optional().valid(0, 1).allow(null),
};
Executable
+274
View File
@@ -0,0 +1,274 @@
#!/usr/bin/env zx
import path from 'path';
const dir_root = process.env.QL_DIR;
const file_auth_token = path.join(dir_root, 'static/auth.json');
const token_file = path.join(dir_root, 'static/build/token.js');
let token;
const createToken = async () => {
let tokenCommand = `tsx ${dir_root}/back/token.ts`;
if (await fs.exists(token_file)) {
tokenCommand = `node ${token_file}`;
}
token = (await $([tokenCommand])).stdout.trim();
};
const getToken = async () => {
if (fs.existsSync(file_auth_token)) {
const authTokenData = JSON.parse(fs.readFileSync(file_auth_token, 'utf8'));
token = authTokenData.value;
const expiration = authTokenData.expiration;
const currentTimeStamp = Math.floor(Date.now() / 1000);
if (currentTimeStamp >= expiration) {
await createToken();
}
} else {
await createToken();
}
};
export const addCronApi = async (schedule, command, name, subId = null) => {
const currentTimeStamp = Math.floor(Date.now() / 1000);
const data = {
name,
command,
schedule,
sub_id: subId,
};
try {
const response = await fetch(
`http://0.0.0.0:5600/open/crons?t=${currentTimeStamp}`,
{
method: 'POST',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'User-Agent': 'Mozilla/5.0',
'Content-Type': 'application/json;charset=UTF-8',
},
body: JSON.stringify(data),
},
);
const responseData = await response.json();
const { code, message } = responseData;
if (code === 200) {
console.log(`${name} -> 添加成功`);
} else {
console.log(`${name} -> 添加失败(${message})`);
}
} catch (error) {
console.error(`${name} -> 添加失败(${error.message})`);
}
};
export const updateCronApi = async (schedule, command, name, id) => {
const currentTimeStamp = Math.floor(Date.now() / 1000);
const data = {
name,
command,
schedule,
id,
};
try {
const response = await fetch(
`http://0.0.0.0:5600/open/crons?t=${currentTimeStamp}`,
{
method: 'PUT',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'User-Agent': 'Mozilla/5.0',
'Content-Type': 'application/json;charset=UTF-8',
},
body: JSON.stringify(data),
},
);
const responseData = await response.json();
const { code, message } = responseData;
if (code === 200) {
console.log(`${name} -> 更新成功`);
} else {
console.log(`${name} -> 更新失败(${message})`);
}
} catch (error) {
console.error(`${name} -> 更新失败(${error.message})`);
}
};
export const updateCronCommandApi = async (command, id) => {
const currentTimeStamp = Math.floor(Date.now() / 1000);
const data = {
command,
id,
};
try {
const response = await fetch(
`http://0.0.0.0:5600/open/crons?t=${currentTimeStamp}`,
{
method: 'PUT',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'User-Agent': 'Mozilla/5.0',
'Content-Type': 'application/json;charset=UTF-8',
},
body: JSON.stringify(data),
},
);
const responseData = await response.json();
const { code, message } = responseData;
if (code === 200) {
console.log(`${command} -> 更新成功`);
} else {
console.log(`${command} -> 更新失败(${message})`);
}
} catch (error) {
console.error(`${command} -> 更新失败(${error.message})`);
}
};
export const delCronApi = async (ids) => {
const currentTimeStamp = Math.floor(Date.now() / 1000);
try {
const response = await fetch(
`http://0.0.0.0:5600/open/crons?t=${currentTimeStamp}`,
{
method: 'DELETE',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'User-Agent': 'Mozilla/5.0',
'Content-Type': 'application/json;charset=UTF-8',
},
body: JSON.stringify(ids),
},
);
const responseData = await response.json();
const { code, message } = responseData;
if (code === 200) {
console.log('成功');
} else {
console.log(`失败(${message})`);
}
} catch (error) {
console.error(`删除失败(${error.message})`);
}
};
export const updateCron = async (
ids,
status,
pid,
logPath,
lastExecutingTime = 0,
runningTime = 0,
) => {
const currentTimeStamp = Math.floor(Date.now() / 1000);
const data = {
ids,
status,
pid,
log_path: logPath,
last_execution_time: lastExecutingTime,
last_running_time: runningTime,
};
try {
const response = await fetch(
`http://0.0.0.0:5600/open/crons/status?t=${currentTimeStamp}`,
{
method: 'PUT',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'User-Agent': 'Mozilla/5.0',
'Content-Type': 'application/json;charset=UTF-8',
},
body: JSON.stringify(data),
},
);
const responseData = await response.json();
const { code, message } = responseData;
if (code !== 200) {
console.log(`更新任务状态失败(${message})`);
}
} catch (error) {
console.error(`更新任务状态失败(${error.message})`);
}
};
export const notifyApi = async (title, content) => {
const currentTimeStamp = Math.floor(Date.now() / 1000);
const data = {
title,
content,
};
try {
const response = await fetch(
`http://0.0.0.0:5600/open/system/notify?t=${currentTimeStamp}`,
{
method: 'PUT',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'User-Agent': 'Mozilla/5.0',
'Content-Type': 'application/json;charset=UTF-8',
},
body: JSON.stringify(data),
},
);
const responseData = await response.json();
const { code, message } = responseData;
if (code === 200) {
console.log('通知发送成功🎉');
} else {
console.log(`通知失败(${message})`);
}
} catch (error) {
console.error(`通知失败(${error.message})`);
}
};
export const findCronApi = async (params) => {
const currentTimeStamp = Math.floor(Date.now() / 1000);
try {
const response = await fetch(
`http://0.0.0.0:5600/open/crons/detail?${params}&t=${currentTimeStamp}`,
{
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'User-Agent': 'Mozilla/5.0',
'Content-Type': 'application/json;charset=UTF-8',
},
},
);
const responseData = await response.json();
const { data } = responseData;
if (data === 'null') {
console.log('');
} else {
const { name } = data;
console.log(name);
}
} catch (error) {
console.error(`查找失败(${error.message})`);
}
};
await getToken();
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env zx
import 'zx/globals';
let initialVars = [];
export const storeEnvVars = async () => {
const stdout = (await $`env`).lines();
initialVars = stdout.map((line) => line.split('=')[0]);
};
export const restoreEnvVars = async () => {
const stdout = (await $`env`).lines();
const currentVars = stdout.map((line) => line.split('=')[0]);
for (const key of currentVars) {
if (!initialVars.includes(key)) {
await $`unset ${key}`;
}
}
};
await storeEnvVars();
Executable
+302
View File
@@ -0,0 +1,302 @@
#!/usr/bin/env zx
import {
dirScripts,
dirLog,
handleTaskStart,
runTaskBefore,
runTaskAfter,
handleTaskEnd,
globalState,
formatLogTime,
} from './share.mjs';
import { basename, dirname } from 'path';
function defineProgram(fileParam) {
if (fileParam.endsWith('.js') || fileParam.endsWith('.mjs')) {
return 'node';
} else if (fileParam.endsWith('.py') || fileParam.endsWith('.pyc')) {
return 'python3';
} else if (fileParam.endsWith('.sh')) {
return 'bash';
} else if (fileParam.endsWith('.ts')) {
return $`command -v tsx`
.then(() => 'tsx')
.catch(() => 'ts-node-transpile-only');
} else {
return '';
}
}
async function randomDelay(fileParam) {
const randomDelayMax = process.env.RandomDelay;
if (randomDelayMax && randomDelayMax > 0) {
const fileExtensions = process.env.RandomDelayFileExtensions || 'js';
const ignoredMinutes = process.env.RandomDelayIgnoredMinutes || '0 30';
const currentMin = new Date().getMinutes();
if (
fileExtensions.split(' ').some((ext) => fileParam.endsWith(`.${ext}`))
) {
if (
ignoredMinutes.split(' ').some((min) => parseInt(min) === currentMin)
) {
return;
}
const delaySecond = Math.floor(Math.random() * randomDelayMax) + 1;
console.log(
`任务随机延迟 ${delaySecond} 秒,配置文件参数 RandomDelay 置空可取消延迟`,
);
await sleep(delaySecond * 1000);
}
}
}
async function genArrayScripts() {
const arrayScripts = [];
const arrayScriptsName = [];
const files = await fs.readdir(dirScripts);
for (const file of files) {
if (file.endsWith('.js') && file !== 'sendNotify.js') {
arrayScripts.push(file);
const content = await fs.readFile(`${dirScripts}/${file}`, 'utf8');
const match = content.match(/new Env\(['"]([^'"]+)['"]\)/);
arrayScriptsName.push(match ? match[1] : '<未识别出活动名称>');
}
}
return { arrayScripts, arrayScriptsName };
}
async function usage() {
const { arrayScripts, arrayScriptsName } = await genArrayScripts();
console.log(
`task命令运行本程序自动添加进crontab的脚本,需要输入脚本的绝对路径或去掉 “${dirScripts}/” 目录后的相对路径(定时任务中请写作相对路径),用法为:`,
);
console.log(
`1.$cmdTask <fileName> # 依次执行,如果设置了随机延迟,将随机延迟一定秒数`,
);
console.log(
`2.$cmdTask <fileName> now # 依次执行,无论是否设置了随机延迟,均立即运行,前台会输出日志,同时记录在日志文件中`,
);
console.log(
`3.$cmdTask <fileName> conc <环境变量名称> <账号编号,空格分隔>(可选的) # 并发执行,无论是否设置了随机延迟,均立即运行,前台不产生日志,直接记录在日志文件中,且可指定账号执行`,
);
console.log(
`4.$cmdTask <fileName> desi <环境变量名称> <账号编号,空格分隔> # 指定账号执行,无论是否设置了随机延迟,均立即运行`,
);
if (arrayScripts.length > 0) {
console.log(`\n当前有以下脚本可以运行:`);
arrayScripts.forEach((script, i) =>
console.log(`${i + 1}. ${arrayScriptsName[i]}${script}`),
);
} else {
console.log(`\n暂无脚本可以执行`);
}
}
export function parseDuration(d) {
if (typeof d == 'number') {
if (isNaN(d) || d < 0) throw new Error(`Invalid duration: "${d}".`);
return d;
} else if (/\d+s/.test(d)) {
return +d.slice(0, -1) * 1000;
} else if (/\d+ms/.test(d)) {
return +d.slice(0, -2);
} else if (/\d+m/.test(d)) {
return +d.slice(0, -1) * 1000 * 60;
} else if (/\d+h/.test(d)) {
return +d.slice(0, -1) * 1000 * 60 * 60;
} else if (/\d+d/.test(d)) {
return +d.slice(0, -1) * 1000 * 60 * 60 * 24;
}
throw new Error(`Unknown duration: "${d}".`);
}
async function runWithTimeout(command) {
if (globalState.commandTimeoutTime) {
const timeoutNumber = parseDuration(globalState.commandTimeoutTime);
await $([command]).timeout(timeoutNumber).nothrow().pipe(process.stdout);
} else {
await $([command]).nothrow().pipe(process.stdout);
}
}
async function runNormal(fileParam, scriptParams) {
if (
!scriptParams.includes('now') &&
process.env.realTime !== 'true' &&
process.env.noDelay !== 'true'
) {
await randomDelay(fileParam);
}
cd(dirScripts);
const relativePath = dirname(fileParam);
if (!fileParam.startsWith('/') && relativePath) {
cd(relativePath);
fileParam = fileParam.replace(`${relativePath}/`, '');
}
await runWithTimeout(
`${globalState.whichProgram} ${fileParam} ${scriptParams}`,
);
}
async function runConcurrent(fileParam, envParam, numParam, scriptParams) {
if (!envParam || !numParam) {
console.log(`缺少并发运行的环境变量参数 task xxx.js conc Test 1 3`);
return;
}
const array = (process.env[envParam] || '').split('&');
const runArr = expandRange(numParam, array.length);
const arrayRun = runArr.map((i) => array[i - 1]).filter(Boolean);
const singleLogTime = formatLogTime(new Date());
cd(dirScripts);
const relativePath = dirname(fileParam);
if (relativePath && fileParam.includes('/')) {
cd(relativePath);
fileParam = fileParam.replace(`${relativePath}/`, '');
}
await Promise.all(
arrayRun.map(async (env, i) => {
const singleLogPath = `${dirLog}/${globalState.logDir}/${singleLogTime}_${
i + 1
}.log`;
await runWithTimeout(
`${envParam}="${env.replace('"', '\\"')}" ${
globalState.whichProgram
} ${fileParam} ${scriptParams} &>${singleLogPath}`,
);
}),
);
for (let i = 0; i < arrayRun.length; i++) {
const singleLogPath = `${dirLog}/${globalState.logDir}/${singleLogTime}_${
i + 1
}.log`;
const log = await fs.readFile(singleLogPath, 'utf8');
console.log(log);
await fs.unlink(singleLogPath);
}
}
async function runDesignated(fileParam, envParam, numParam, scriptParams) {
if (!envParam || !numParam) {
console.log(`缺少单独运行的参数 task xxx.js desi Test 1 3`);
return;
}
const array = (process.env[envParam] || '').split('&');
const runArr = expandRange(numParam, array.length);
const arrayRun = runArr.map((i) => array[i - 1]).filter(Boolean);
const cookieStr = arrayRun.join('&');
cd(dirScripts);
const relativePath = dirname(fileParam);
if (relativePath && fileParam.includes('/')) {
cd(relativePath);
fileParam = fileParam.replace(`${relativePath}/`, '');
}
console.log('cookieStr', cookieStr.length, arrayRun.length);
// ${envParam}="${cookieStr.replace('"', '\\"')}"
await runWithTimeout(
`${globalState.whichProgram} ${fileParam} ${scriptParams}`,
);
}
async function runElse(fileParam, scriptParams) {
cd(dirScripts);
const relativePath = dirname(fileParam);
if (relativePath && fileParam.includes('/')) {
cd(relativePath);
fileParam = fileParam.replace(`${relativePath}/`, './');
}
await runWithTimeout(
`${globalState.whichProgram} ${fileParam} ${scriptParams}`,
);
}
function expandRange(rangeStr, max) {
const tempRangeStr = rangeStr
.replace(/-max/g, `-${max}`)
.replace(/max-/g, `${max}-`);
return tempRangeStr.split(' ').flatMap((part) => {
const rangeMatch = part.match(/^(\d+)([-~_])(\d+)$/);
if (rangeMatch) {
const [, start, , end] = rangeMatch.map(Number);
return Array.from({ length: end - start + 1 }, (_, i) => start + i);
}
return Number(part);
});
}
async function main(taskShellParams, scriptParams) {
const [fileParam, action, envParam, ...others] = taskShellParams;
if (taskShellParams.length === 0) {
return await usage();
}
if (fileParam && /\.(js|py|pyc|sh|ts)$/.test(fileParam)) {
const filePath = fileParam.startsWith('/')
? fileParam
: path.join(dirScripts, fileParam);
if (!(await fs.exists(filePath))) {
console.log(`文件不存在 ${fileParam}`);
return;
}
switch (action) {
case undefined:
return await runNormal(fileParam, scriptParams);
case 'now':
return await runNormal(fileParam, scriptParams);
case 'conc':
return await runConcurrent(
fileParam,
envParam,
others.join(' '),
scriptParams,
);
case 'desi':
return await runDesignated(
fileParam,
envParam,
others.join(' '),
scriptParams,
);
}
}
await runElse(fileParam, taskShellParams.slice(1).concat(scriptParams));
}
async function run() {
const taskArgv = minimist(process.argv.slice(3), {
'--': true,
});
const { _: taskShellParams, m, '--': scriptParams, GlobalState } = taskArgv;
const cacheState = JSON.parse(GlobalState || '{}');
for (const key in cacheState) {
globalState[key] = cacheState[key];
}
if (m) {
globalState.commandTimeoutTime = m;
}
globalState.whichProgram = await defineProgram(taskShellParams[0]);
await handleTaskStart();
await runTaskBefore();
await main(taskShellParams, scriptParams);
await runTaskAfter();
await handleTaskEnd();
}
async function singleHandle() {}
const signals = ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT', 'SIGTSTP'];
signals.forEach((sig) => process.on(sig, singleHandle));
await run();
Executable
+500
View File
@@ -0,0 +1,500 @@
#!/usr/bin/env zx
import 'zx/globals';
// $.verbose = true;
import { updateCron, notifyApi } from './api.mjs';
import { restoreEnvVars } from './env.mjs';
export const dirRoot = process.env.QL_DIR;
export const dirTmp = path.join(dirRoot, '.tmp');
export const dirData = process.env.QL_DATA_DIR
? process.env.QL_DATA_DIR.endsWith('/')
? process.env.QL_DATA_DIR.slice(-1)
: process.env.QL_DATA_DIR
: path.join(dirRoot, 'data');
export const dirShell = path.join(dirRoot, 'shell');
export const dirCli = path.join(dirRoot, 'cli');
export const dirSample = path.join(dirRoot, 'sample');
export const dirStatic = path.join(dirRoot, 'static');
export const dirConfig = path.join(dirData, 'config');
export const dirScripts = path.join(dirData, 'scripts');
export const dirRepo = path.join(dirData, 'repo');
export const dirRaw = path.join(dirData, 'raw');
export const dirLog = path.join(dirData, 'log');
export const dirDb = path.join(dirData, 'db');
export const dirDep = path.join(dirData, 'deps');
export const dirListTmp = path.join(dirLog, '.tmp');
export const dirUpdateLog = path.join(dirLog, 'update');
export const qlStaticRepo = path.join(dirRepo, 'static');
export const fileConfigSample = path.join(dirSample, 'config.sample.sh');
export const fileEnv = path.join(dirConfig, 'env.sh');
export const jsFileEnv = path.join(dirConfig, 'env.js');
export const fileConfigUser = path.join(dirConfig, 'config.sh');
export const fileAuthSample = path.join(dirSample, 'auth.sample.json');
export const fileAuthUser = path.join(dirConfig, 'auth.json');
export const fileAuthToken = path.join(dirConfig, 'token.json');
export const fileExtraShell = path.join(dirConfig, 'extra.sh');
export const fileTaskBefore = path.join(dirConfig, 'task_before.sh');
export const fileTaskAfter = path.join(dirConfig, 'task_after.sh');
export const fileTaskSample = path.join(dirSample, 'task.sample.sh');
export const fileExtraSample = path.join(dirSample, 'extra.sample.sh');
export const fileNotifyJsSample = path.join(dirSample, 'notify.js');
export const fileNotifyPySample = path.join(dirSample, 'notify.py');
export const fileTestJsSample = path.join(dirSample, 'ql_sample.js');
export const fileTestPySample = path.join(dirSample, 'ql_sample.py');
export const fileNotifyPy = path.join(dirScripts, 'notify.py');
export const fileNotifyJs = path.join(dirScripts, 'sendNotify.js');
export const fileTestJs = path.join(dirScripts, 'ql_sample.js');
export const fileTestPy = path.join(dirScripts, 'ql_sample.py');
export const nginxAppConf = path.join(dirRoot, 'docker/front.conf');
export const nginxConf = path.join(dirRoot, 'docker/nginx.conf');
export const depNotifyPy = path.join(dirDep, 'notify.py');
export const depNotifyJs = path.join(dirDep, 'sendNotify.js');
export const listCrontabUser = path.join(dirConfig, 'crontab.list');
export const listCrontabSample = path.join(dirSample, 'crontab.sample.list');
export const listOwnScripts = path.join(dirListTmp, 'own_scripts.list');
export const listOwnUser = path.join(dirListTmp, 'own_user.list');
export const listOwnAdd = path.join(dirListTmp, 'own_add.list');
export const listOwnDrop = path.join(dirListTmp, 'own_drop.list');
export const globalState = {};
export const initEnv = () => {
$.prefix +=
'export NODE_PATH=/usr/local/bin:/usr/local/pnpm-global/5/node_modules:/usr/local/lib/node_modules:/root/.local/share/pnpm/global/5/node_modules;';
$.prefix += 'export PYTHONUNBUFFERED=1;';
$.prefix += 'export TERM=xterm-color;';
};
export const importConfig = async () => {
if (await fs.exists(fileConfigUser)) {
$.prefix += (await fs.readFile(fileConfigUser, 'utf8'));
}
// if (process.env.LOAD_ENV !== 'false' && (await fs.exists(fileEnv))) {
// $.prefix += (await fs.readFile(fileEnv, 'utf8'));
// }
require(jsFileEnv)
globalState.qlBaseUrl = process.env.QlBaseUrl || '/';
globalState.qlPort = process.env.QlPort || '5700';
globalState.commandTimeoutTime = process.env.CommandTimeoutTime;
globalState.fileExtensions = process.env.RepoFileExtensions || 'js py';
globalState.proxyUrl = process.env.ProxyUrl || '';
globalState.currentBranch = process.env.QL_BRANCH;
if (process.env.DefaultCronRule) {
globalState.defaultCron = process.env.DefaultCronRule;
} else {
globalState.defaultCron = `${Math.floor(Math.random() * 60)} ${Math.floor(
Math.random() * 24,
)} * * *`;
}
globalState.cpuWarn = process.env.CpuWarn;
globalState.memWarn = process.env.MemoryWarn;
globalState.diskWarn = process.env.DiskWarn;
};
export const setProxy = (proxy) => {
if (proxy) {
globalState.proxyUrl = proxy;
}
if (globalState.proxyUrl) {
$`export http_proxy=${globalState.proxyUrl}`;
$`export https_proxy=${globalState.proxyUrl}`;
}
};
export const unsetProxy = () => {
$`unset http_proxy`;
$`unset https_proxy`;
};
export const makeDir = async (dir) => {
if (!(await fs.exists(dir))) {
await fs.mkdir(dir, { recursive: true });
}
};
export const detectTermux = () => {
globalState.isTermux = process.env.PATH?.includes('com.termux') ? 1 : 0;
};
export const detectMacos = () => {
globalState.isMacos = os.type() === 'Darwin' ? 1 : 0;
};
export const genRandomNum = (number) => {
return Math.floor(Math.random() * number);
};
export const fixConfig = async () => {
await makeDir(dirTmp);
await makeDir(dirStatic);
await makeDir(dirData);
await makeDir(dirConfig);
await makeDir(dirLog);
await makeDir(dirDb);
await makeDir(dirScripts);
await makeDir(dirListTmp);
await makeDir(dirRepo);
await makeDir(dirRaw);
await makeDir(dirUpdateLog);
await makeDir(dirDep);
if (!(await fs.exists(fileConfigUser))) {
console.log(
`复制一份 ${fileConfigSample}${fileConfigUser},随后请按注释编辑你的配置文件:${fileConfigUser}`,
);
await fs.copyFile(fileConfigSample, fileConfigUser);
}
if (!(await fs.exists(fileEnv))) {
console.log(
'检测到config配置目录下不存在env.sh,创建一个空文件用于初始化...',
);
await fs.writeFile(fileEnv, '');
}
if (!(await fs.exists(fileTaskBefore))) {
console.log(`复制一份 ${fileTaskSample}${fileTaskBefore}`);
await fs.copyFile(fileTaskSample, fileTaskBefore);
}
if (!(await fs.exists(fileTaskAfter))) {
console.log(`复制一份 ${fileTaskSample}${fileTaskAfter}`);
await fs.copyFile(fileTaskSample, fileTaskAfter);
}
if (!(await fs.exists(fileExtraShell))) {
console.log(`复制一份 ${fileExtraSample}${fileExtraShell}`);
await fs.copyFile(fileExtraSample, fileExtraShell);
}
if (!(await fs.exists(fileAuthUser))) {
console.log(`复制一份 ${fileAuthSample}${fileAuthUser}`);
await fs.copyFile(fileAuthSample, fileAuthUser);
}
if (!(await fs.exists(fileNotifyPy))) {
console.log(`复制一份 ${fileNotifyPySample}${fileNotifyPy}`);
await fs.copyFile(fileNotifyPySample, fileNotifyPy);
}
if (!(await fs.exists(fileNotifyJs))) {
console.log(`复制一份 ${fileNotifyJsSample}${fileNotifyJs}`);
await fs.copyFile(fileNotifyJsSample, fileNotifyJs);
}
if (!(await fs.exists(fileTestJs))) {
await fs.copyFile(fileTestJsSample, fileTestJs);
}
if (!(await fs.exists(fileTestPy))) {
await fs.copyFile(fileTestPySample, fileTestPy);
}
if (await fs.exists('/etc/nginx/conf.d/default.conf')) {
console.log('检测到你可能未修改过默认nginx配置,将帮你删除');
await fs.unlink('/etc/nginx/conf.d/default.conf');
}
if (!(await fs.exists(depNotifyJs))) {
console.log(`复制一份 ${fileNotifyJsSample}${depNotifyJs}`);
await fs.copyFile(fileNotifyJsSample, depNotifyJs);
}
if (!(await fs.exists(depNotifyPy))) {
console.log(`复制一份 ${fileNotifyPySample}${depNotifyPy}`);
await fs.copyFile(fileNotifyPySample, depNotifyPy);
}
};
export const npmInstallSub = async () => {
if (globalState.isTermux === 1) {
await $`npm install --production --no-bin-links`;
} else if (!(await $`command -v pnpm`)) {
await $`npm install --production`;
} else {
await $`pnpm install --loglevel error --production`;
}
};
export const npmInstall = async (dirWork) => {
const dirCurrent = process.cwd();
await $`cd ${dirWork}`;
console.log(`安装 ${dirWork} 依赖包...`);
await npmInstallSub();
await $`cd ${dirCurrent}`;
};
export const diffAndCopy = async (copySource, copyTo) => {
if (
!(await fs.exists(copyTo)) ||
(await $`diff ${copySource} ${copyTo}`).exitCode !== 0
) {
await fs.copyFile(copySource, copyTo);
}
};
export const gitCloneScripts = async (url, dir, branch, proxy) => {
const partCmd = branch ? `-b ${branch}` : '';
console.log(`开始拉取仓库 ${globalState.uniqPath}${dir}`);
setProxy(proxy);
const res = await $`git clone -q --depth=1 ${partCmd} ${url} ${dir}`;
globalState.exitStatus = res.exitCode;
unsetProxy();
};
export const randomRange = (begin, end) => {
return Math.floor(Math.random() * (end - begin) + begin);
};
export const deletePm2 = async () => {
await $`cd ${dirRoot}`;
await $`pm2 delete ecosystem.config.js`;
};
export const reloadPm2 = async () => {
await $`cd ${dirRoot}`;
restoreEnvVars();
await $`pm2 flush &>/dev/null`;
await $`pm2 startOrGracefulReload ecosystem.config.js`;
};
export const reloadUpdate = async () => {
await $`cd ${dirRoot}`;
restoreEnvVars();
await $`pm2 flush &>/dev/null`;
await $`pm2 startOrGracefulReload other.config.js`;
};
export const diffTime = (beginTime, endTime) => {
let diffTime;
if (globalState.isMacos === 1) {
diffTime = (+new Date(endTime) - +new Date(beginTime)) / 1000;
} else {
diffTime =
(new Date(endTime).getTime() - new Date(beginTime).getTime()) / 1000;
}
return diffTime;
};
export const formatTime = (time) => {
// 秒
return new Date(time).toLocaleString();
};
function pad(n, min = 10) {
return n < min ? '0' + n : n;
}
export function formatDate(date) {
const year = date.getFullYear();
const month = pad(date.getMonth() + 1);
const day = pad(date.getDate());
const hour = pad(date.getHours());
const minute = pad(date.getMinutes());
const second = pad(date.getSeconds());
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
export const formatLogTime = (date) => {
const year = date.getFullYear();
const month = pad(date.getMonth() + 1);
const day = pad(date.getDate());
const hour = pad(date.getHours());
const minute = pad(date.getMinutes());
const second = pad(date.getSeconds());
const milliSecond = pad(date.getMilliseconds(), 100);
return `${year}-${month}-${day}-${hour}-${minute}-${second}-${milliSecond}`;
};
export const formatTimestamp = (date) => {
return Math.floor(date.getTime() / 1000);
};
export const patchVersion = async () => {
await $`git config --global pull.rebase false`;
if (await fs.exists(path.join(dirRoot, 'db/cookie.db'))) {
console.log('检测到旧的db文件,拷贝为新db...');
await $`mv ${path.join(dirRoot, 'db/cookie.db')} ${path.join(
dirRoot,
'db/env.db',
)}`;
await $`rm -rf ${path.join(dirRoot, 'db/cookie.db')}`;
}
if (await fs.exists(path.join(dirRoot, 'db'))) {
console.log('检测到旧的db目录,拷贝到data目录...');
await $`cp -rf ${path.join(dirRoot, 'config')} ${dirData}`;
}
if (await fs.exists(path.join(dirRoot, 'scripts'))) {
console.log('检测到旧的scripts目录,拷贝到data目录...');
await $`cp -rf ${path.join(dirRoot, 'scripts')} ${dirData}`;
}
if (await fs.exists(path.join(dirRoot, 'log'))) {
console.log('检测到旧的log目录,拷贝到data目录...');
await $`cp -rf ${path.join(dirRoot, 'log')} ${dirData}`;
}
if (await fs.exists(path.join(dirRoot, 'config'))) {
console.log('检测到旧的config目录,拷贝到data目录...');
await $`cp -rf ${path.join(dirRoot, 'config')} ${dirData}`;
}
};
export const initNginx = async () => {
await fs.copyFile(nginxConf, '/etc/nginx/nginx.conf');
await fs.copyFile(nginxAppConf, '/etc/nginx/conf.d/front.conf');
let locationUrl = '/';
let aliasStr = '';
let rootStr = '';
let qlBaseUrl = globalState.qlBaseUrl;
let qlPort = globalState.qlPort;
if (qlBaseUrl !== '/') {
if (!qlBaseUrl.startsWith('/')) {
qlBaseUrl = `/${qlBaseUrl}`;
}
if (!qlBaseUrl.endsWith('/')) {
qlBaseUrl = `${qlBaseUrl}/`;
}
locationUrl = `^~${qlBaseUrl.slice(0, -1)}`;
aliasStr = `alias ${path.join(dirStatic, 'dist')};`;
const file = await fs.readFile(
path.join(dirStatic, 'dist/index.html'),
'utf8',
);
if (!file.includes(`<base href="${qlBaseUrl}">`)) {
await fs.writeFile(
path.join(dirStatic, 'dist/index.html'),
`<base href="${qlBaseUrl}">\n${file}`,
);
}
} else {
rootStr = `root ${path.join(dirStatic, 'dist')};`;
}
await $`sed -i "s,QL_ALIAS_CONFIG,${aliasStr},g" /etc/nginx/conf.d/front.conf`;
await $`sed -i "s,QL_ROOT_CONFIG,${rootStr},g" /etc/nginx/conf.d/front.conf`;
await $`sed -i "s,QL_BASE_URL_LOCATION,${locationUrl},g" /etc/nginx/conf.d/front.conf`;
let ipv6Str = '';
const ipv6 = await $`ip a | grep inet6`;
if (ipv6.stdout.trim()) {
ipv6Str = 'listen [::]:${qlPort} ipv6only=on;';
}
const ipv4Str = `listen ${qlPort};`;
await $`sed -i "s,IPV6_CONFIG,${ipv6Str},g" /etc/nginx/conf.d/front.conf`;
await $`sed -i "s,IPV4_CONFIG,${ipv4Str},g" /etc/nginx/conf.d/front.conf`;
};
async function checkServer() {
const cpuWarn = parseInt(process.env.cpuWarn || '0');
const memWarn = parseInt(process.env.memWarn || '0');
const diskWarn = parseInt(process.env.diskWarn || '0');
if (cpuWarn && memWarn && diskWarn) {
const topResult = await $`top -b -n 1`;
const cpuUse = parseInt(
topResult.stdout.match(/CPU\s+(\d+)\%/)?.[1] || '0',
);
const memFree = parseInt(
(await $`free -m`).stdout.match(/Mem:\s+(\d+)/)?.[1] || '0',
);
const memTotal = parseInt(
(await $`free -m`).stdout.match(/Mem:\s+\d+\s+(\d+)/)?.[1] || '0',
);
const diskUse = parseInt(
(await $`df -P`).stdout.match(/\/dev.*\s+(\d+)\%/)?.[1] || '0',
);
if (memFree && memTotal && diskUse && cpuUse) {
const memUse = Math.floor((memFree * 100) / memTotal);
if (cpuUse > cpuWarn || memFree < memWarn || diskUse > diskWarn) {
const resource = topResult.stdout
.split('\n')
.slice(7, 17)
.map((line) => line.replace(/\s+/g, ' '))
.join('\\n');
await notifyApi(
'服务器资源异常警告',
`当前CPU占用 ${cpuUse}% 内存占用 ${memUse}% 磁盘占用 ${diskUse}% \n资源占用详情 \n\n ${resource}`,
);
}
}
}
}
export const handleTaskStart = async () => {
if (globalState.ID) {
await updateCron(
[globalState.ID],
'0',
String(process.pid),
globalState.logPath,
globalState.beginTimestamp,
);
}
console.log(`## 开始执行... ${globalState.beginTime}\n`);
};
export const runTaskBefore = async () => {
if (globalState.isMacos === 0) {
await checkServer();
}
await $`. ${fileTaskBefore} "$@"`;
if (globalState.taskBefore) {
console.log('执行前置命令');
await $`eval ${globalState.taskBefore}`;
console.log('执行前置命令结束');
}
};
export const runTaskAfter = async () => {
await $`. ${fileTaskAfter} "$@"`;
if (globalState.taskAfter) {
console.log('执行后置命令');
await $`eval "${globalState.taskAfter}"`;
console.log('执行后置命令结束');
}
};
export const handleTaskEnd = async () => {
const etime = new Date();
const endTime = formatDate(etime);
const endTimestamp = formatTimestamp(etime);
let diffTime = endTimestamp - globalState.beginTimestamp;
if (diffTime === 0) {
diffTime = 1;
}
if (globalState.ID) {
await updateCron(
[globalState.ID],
'1',
`${process.pid}`,
globalState.logPath,
globalState.beginTimestamp,
diffTime,
);
}
console.log(`\n## 执行结束... ${endTime} 耗时 ${diffTime}`);
};
initEnv();
detectTermux();
detectMacos();
await importConfig();
Executable
+94
View File
@@ -0,0 +1,94 @@
#!/usr/bin/env zx
import 'zx/globals';
import { PassThrough } from 'node:stream';
import {
listCrontabUser,
formatLogTime,
formatTimestamp,
formatDate,
dirLog,
dirCli,
globalState,
handleTaskEnd,
} from './share.mjs';
import './api.mjs';
// $.verbose = true;
async function singleHandle() {}
const signals = ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT', 'SIGTSTP'];
signals.forEach((sig) => process.on(sig, singleHandle));
export async function handleLogPath(fileParam) {
let ID = $.env.ID;
if (!ID) {
const grepResult =
await $`grep -E "task.* ${fileParam}" ${listCrontabUser}`.nothrow();
ID = grepResult.stdout.match(/ID=(\d+)/)?.[1];
}
globalState.ID = ID;
const suffix = ID && parseInt(ID, 10) > 0 ? `_${ID}` : '';
globalState.time = new Date();
const logTime = formatLogTime(globalState.time);
let logDirTmp = path.basename(fileParam);
let logDirTmpPath = '';
if (fileParam.includes('/')) {
logDirTmpPath = path.isAbsolute(fileParam)
? fileParam.substring(1)
: fileParam;
logDirTmpPath = path.basename(path.dirname(logDirTmpPath));
}
if (logDirTmpPath) {
logDirTmp = `${logDirTmpPath}_${logDirTmp}`;
}
const logDir = `${logDirTmp.replace(/\.[^/.]+$/, '')}${suffix}`;
globalState.logDir = logDir;
globalState.logPath = `${logDir}/${logTime}.log`;
if ($.env.real_log_path) {
globalState.logPath = realLogPath;
}
await $`mkdir -p ${dirLog}/${logDir}`;
}
export function initBeginTime() {
globalState.beginTime = formatDate(globalState.time);
globalState.beginTimestamp = formatTimestamp(globalState.time);
}
async function main() {
const taskArgv = minimist(process.argv.slice(3), {
'--': true,
});
const {
_: [scriptFile],
} = taskArgv;
await handleLogPath(scriptFile);
initBeginTime();
cd(`${dirCli}`);
const logStream = fs.createWriteStream(`${dirLog}/${globalState.logPath}`);
const passThrough = new PassThrough();
passThrough.pipe(logStream);
const p = $`./otask.mjs ${process.argv.slice(
3,
)} --GlobalState=${JSON.stringify(globalState)} 2>&1`.nothrow();
p.stdout.pipe(passThrough).pipe(process.stdout);
await p;
}
main()
.then(() => {
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
+13 -19
View File
@@ -1,9 +1,9 @@
FROM python:3.10-alpine3.18 AS builder FROM python:3.10-alpine3.18 as builder
COPY package.json .npmrc pnpm-lock.yaml /tmp/build/ COPY package.json .npmrc pnpm-lock.yaml /tmp/build/
RUN set -x \ RUN set -x \
&& apk update \ && apk update \
&& apk add nodejs npm git \ && apk add nodejs npm git \
&& npm i -g pnpm@8.3.1 pm2 ts-node \ && npm i -g pnpm@8.3.1 pm2 tsx \
&& cd /tmp/build \ && cd /tmp/build \
&& pnpm install --prod && pnpm install --prod
@@ -13,13 +13,15 @@ ARG QL_MAINTAINER="whyour"
LABEL maintainer="${QL_MAINTAINER}" LABEL maintainer="${QL_MAINTAINER}"
ARG QL_URL=https://github.com/${QL_MAINTAINER}/qinglong.git ARG QL_URL=https://github.com/${QL_MAINTAINER}/qinglong.git
ARG QL_BRANCH=develop ARG QL_BRANCH=develop
ARG PYTHON_SHORT_VERSION=3.10
ENV QL_DIR=/ql \ ENV PNPM_HOME=/root/.local/share/pnpm \
QL_BRANCH=${QL_BRANCH} \ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/share/pnpm:/root/.local/share/pnpm/global/5/node_modules:$PNPM_HOME \
NODE_PATH=/usr/local/bin:/usr/local/pnpm-global/5/node_modules:/usr/local/lib/node_modules:/root/.local/share/pnpm/global/5/node_modules \
LANG=C.UTF-8 \ LANG=C.UTF-8 \
SHELL=/bin/bash \ SHELL=/bin/bash \
PS1="\u@\h:\w \$ " PS1="\u@\h:\w \$ " \
QL_DIR=/ql \
QL_BRANCH=${QL_BRANCH}
VOLUME /ql/data VOLUME /ql/data
@@ -39,6 +41,7 @@ RUN set -x \
tzdata \ tzdata \
perl \ perl \
openssl \ openssl \
nginx \
nodejs \ nodejs \
jq \ jq \
openssh \ openssh \
@@ -50,9 +53,11 @@ RUN set -x \
&& apk update \ && apk update \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \ && echo "Asia/Shanghai" > /etc/timezone \
&& git config --global user.email "qinglong@users.noreply.github.com" \ && git config --global user.email "qinglong@@users.noreply.github.com" \
&& git config --global user.name "qinglong" \ && git config --global user.name "qinglong" \
&& git config --global http.postBuffer 524288000 \ && git config --global http.postBuffer 524288000 \
&& rm -rf /root/.pnpm-store \
&& rm -rf /root/.local/share/pnpm/store \
&& rm -rf /root/.cache \ && rm -rf /root/.cache \
&& ulimit -c 0 && ulimit -c 0
@@ -67,22 +72,11 @@ RUN git clone --depth=1 -b ${QL_BRANCH} ${QL_URL} ${QL_DIR} \
&& cp -rf /static/* ${QL_DIR}/static \ && cp -rf /static/* ${QL_DIR}/static \
&& rm -rf /static && rm -rf /static
ENV PNPM_HOME=${QL_DIR}/data/dep_cache/node \
PYTHON_HOME=${QL_DIR}/data/dep_cache/python3 \
PYTHONUSERBASE=${QL_DIR}/data/dep_cache/python3
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin \
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules:${PNPM_HOME}/global/5/node_modules \
PIP_CACHE_DIR=${PYTHON_HOME}/pip \
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages
RUN pip3 install --prefix ${PYTHON_HOME} requests
COPY --from=builder /tmp/build/node_modules/. /ql/node_modules/ COPY --from=builder /tmp/build/node_modules/. /ql/node_modules/
WORKDIR ${QL_DIR} WORKDIR ${QL_DIR}
HEALTHCHECK --interval=5s --timeout=2s --retries=20 \ HEALTHCHECK --interval=5s --timeout=2s --retries=20 \
CMD curl -sf --noproxy '*' http://127.0.0.1:5700/api/health || exit 1 CMD curl -sf --noproxy '*' http://127.0.0.1:5400/api/health || exit 1
ENTRYPOINT ["./docker/docker-entrypoint.sh"] ENTRYPOINT ["./docker/docker-entrypoint.sh"]
+13 -19
View File
@@ -1,9 +1,9 @@
FROM python:3.11-alpine3.18 AS builder FROM python:3.11-alpine3.18 as builder
COPY package.json .npmrc pnpm-lock.yaml /tmp/build/ COPY package.json .npmrc pnpm-lock.yaml /tmp/build/
RUN set -x \ RUN set -x \
&& apk update \ && apk update \
&& apk add nodejs npm git \ && apk add nodejs npm git \
&& npm i -g pnpm@8.3.1 pm2 ts-node \ && npm i -g pnpm@8.3.1 pm2 tsx \
&& cd /tmp/build \ && cd /tmp/build \
&& pnpm install --prod && pnpm install --prod
@@ -13,13 +13,15 @@ ARG QL_MAINTAINER="whyour"
LABEL maintainer="${QL_MAINTAINER}" LABEL maintainer="${QL_MAINTAINER}"
ARG QL_URL=https://github.com/${QL_MAINTAINER}/qinglong.git ARG QL_URL=https://github.com/${QL_MAINTAINER}/qinglong.git
ARG QL_BRANCH=develop ARG QL_BRANCH=develop
ARG PYTHON_SHORT_VERSION=3.11
ENV QL_DIR=/ql \ ENV PNPM_HOME=/root/.local/share/pnpm \
QL_BRANCH=${QL_BRANCH} \ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/share/pnpm:/root/.local/share/pnpm/global/5/node_modules:$PNPM_HOME \
NODE_PATH=/usr/local/bin:/usr/local/pnpm-global/5/node_modules:/usr/local/lib/node_modules:/root/.local/share/pnpm/global/5/node_modules \
LANG=C.UTF-8 \ LANG=C.UTF-8 \
SHELL=/bin/bash \ SHELL=/bin/bash \
PS1="\u@\h:\w \$ " PS1="\u@\h:\w \$ " \
QL_DIR=/ql \
QL_BRANCH=${QL_BRANCH}
VOLUME /ql/data VOLUME /ql/data
@@ -39,6 +41,7 @@ RUN set -x \
tzdata \ tzdata \
perl \ perl \
openssl \ openssl \
nginx \
nodejs \ nodejs \
jq \ jq \
openssh \ openssh \
@@ -50,9 +53,11 @@ RUN set -x \
&& apk update \ && apk update \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \ && echo "Asia/Shanghai" > /etc/timezone \
&& git config --global user.email "qinglong@users.noreply.github.com" \ && git config --global user.email "qinglong@@users.noreply.github.com" \
&& git config --global user.name "qinglong" \ && git config --global user.name "qinglong" \
&& git config --global http.postBuffer 524288000 \ && git config --global http.postBuffer 524288000 \
&& rm -rf /root/.pnpm-store \
&& rm -rf /root/.local/share/pnpm/store \
&& rm -rf /root/.cache \ && rm -rf /root/.cache \
&& ulimit -c 0 && ulimit -c 0
@@ -67,22 +72,11 @@ RUN git clone --depth=1 -b ${QL_BRANCH} ${QL_URL} ${QL_DIR} \
&& cp -rf /static/* ${QL_DIR}/static \ && cp -rf /static/* ${QL_DIR}/static \
&& rm -rf /static && rm -rf /static
ENV PNPM_HOME=${QL_DIR}/data/dep_cache/node \
PYTHON_HOME=${QL_DIR}/data/dep_cache/python3 \
PYTHONUSERBASE=${QL_DIR}/data/dep_cache/python3
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin \
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules:${PNPM_HOME}/global/5/node_modules \
PIP_CACHE_DIR=${PYTHON_HOME}/pip \
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages
RUN pip3 install --prefix ${PYTHON_HOME} requests
COPY --from=builder /tmp/build/node_modules/. /ql/node_modules/ COPY --from=builder /tmp/build/node_modules/. /ql/node_modules/
WORKDIR ${QL_DIR} WORKDIR ${QL_DIR}
HEALTHCHECK --interval=5s --timeout=2s --retries=20 \ HEALTHCHECK --interval=5s --timeout=2s --retries=20 \
CMD curl -sf --noproxy '*' http://127.0.0.1:5700/api/health || exit 1 CMD curl -sf --noproxy '*' http://127.0.0.1:5400/api/health || exit 1
ENTRYPOINT ["./docker/docker-entrypoint.sh"] ENTRYPOINT ["./docker/docker-entrypoint.sh"]
+8 -3
View File
@@ -1,10 +1,15 @@
version: '2'
services: services:
web: web:
image: whyour/qinglong:latest # 基于 Debian 的版本:whyour/qinglong:debian # alpine 基础镜像版本
image: whyour/qinglong:latest
# debian-slim 基础镜像版本
# image: whyour/qinglong:debian
volumes: volumes:
- ./data:/ql/data - ./data:/ql/data
ports: ports:
- "5700:5700" - "0.0.0.0:5700:5700"
environment: environment:
QlBaseUrl: '/' # 部署路径非必须,以斜杠开头和结尾,比如 /test/ # 部署路径非必须,以斜杠开头和结尾,比如 /test/
QlBaseUrl: '/'
restart: unless-stopped restart: unless-stopped
+22 -35
View File
@@ -1,56 +1,43 @@
#!/bin/bash #!/bin/bash
export PATH="$HOME/bin:$PATH"
dir_shell=/ql/shell dir_shell=/ql/shell
. $dir_shell/env.sh
. $dir_shell/share.sh . $dir_shell/share.sh
export_ql_envs() { echo -e "======================1. 检测配置文件========================\n"
export BACK_PORT="${ql_port}" make_dir /etc/nginx/conf.d
export GRPC_PORT="${ql_grpc_port}" make_dir /run/nginx
} init_nginx
log_with_style() {
local level="$1"
local message="$2"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
printf "\n[%s] [%7s] %s\n" "${timestamp}" "${level}" "${message}"
}
# Fix DNS resolution issues in Alpine Linux
# Alpine uses musl libc which has known DNS resolver issues with certain domains
# Adding ndots:0 prevents unnecessary search domain appending
if [ -f /etc/alpine-release ]; then
if ! grep -q "^options ndots:0" /etc/resolv.conf 2>/dev/null; then
echo "options ndots:0" >> /etc/resolv.conf
log_with_style "INFO" "🔧 0. 已配置 DNS 解析优化 (ndots:0)"
fi
fi
log_with_style "INFO" "🚀 1. 检测配置文件..."
load_ql_envs
export_ql_envs
. $dir_shell/env.sh
import_config "$@"
fix_config fix_config
# Try to initialize PM2, but don't fail if it doesn't work pm2 l &>/dev/null
pm2 l &>/dev/null || log_with_style "WARN" "PM2 初始化可能失败,将在启动时尝试使用备用方案"
log_with_style "INFO" "⚙️ 2. 启动 pm2 服务..." echo -e "======================2. 安装依赖========================\n"
patch_version
echo -e "======================3. 启动nginx========================\n"
nginx -s reload 2>/dev/null || nginx -c /etc/nginx/nginx.conf
echo -e "nginx启动成功...\n"
echo -e "======================4. 启动pm2服务========================\n"
reload_update
reload_pm2 reload_pm2
if [[ $AutoStartBot == true ]]; then if [[ $AutoStartBot == true ]]; then
log_with_style "INFO" "🤖 3. 启动 bot..." echo -e "======================5. 启动bot========================\n"
nohup ql bot >$dir_log/bot.log 2>&1 & nohup ql bot >$dir_log/bot.log 2>&1 &
echo -e "bot后台启动中...\n"
fi fi
if [[ $EnableExtraShell == true ]]; then if [[ $EnableExtraShell == true ]]; then
log_with_style "INFO" "🛠️ 4. 执行自定义脚本..." echo -e "====================6. 执行自定义脚本========================\n"
nohup ql extra >$dir_log/extra.log 2>&1 & nohup ql extra >$dir_log/extra.log 2>&1 &
echo -e "自定义脚本后台执行中...\n"
fi fi
log_with_style "SUCCESS" "🎉 容器启动成功!" echo -e "############################################################\n"
echo -e "容器启动成功..."
echo -e "############################################################\n"
crond -f >/dev/null crond -f >/dev/null
+93
View File
@@ -0,0 +1,93 @@
upstream baseApi {
server 0.0.0.0:5600;
}
upstream publicApi {
server 0.0.0.0:5400;
}
upstream updateApi {
server 0.0.0.0:5300;
}
map $http_upgrade $connection_upgrade {
default keep-alive;
'websocket' upgrade;
}
server {
IPV4_CONFIG
IPV6_CONFIG
ssl_session_timeout 5m;
location QL_BASE_URLapi/update/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://updateApi/api/;
proxy_buffering off;
proxy_redirect default;
proxy_connect_timeout 1800;
proxy_send_timeout 1800;
proxy_read_timeout 1800;
}
location QL_BASE_URLapi/public/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://publicApi/api/;
proxy_buffering off;
proxy_redirect default;
proxy_connect_timeout 1800;
proxy_send_timeout 1800;
proxy_read_timeout 1800;
}
location QL_BASE_URLapi/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://baseApi/api/;
proxy_buffering off;
proxy_redirect default;
proxy_connect_timeout 1800;
proxy_send_timeout 1800;
proxy_read_timeout 1800;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location QL_BASE_URLopen/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://baseApi/open/;
proxy_buffering off;
proxy_redirect default;
proxy_connect_timeout 1800;
proxy_send_timeout 1800;
proxy_read_timeout 1800;
}
gzip on;
gzip_static on;
gzip_types text/plain application/json application/javascript application/x-javascript text/css application/xml text/javascript;
gzip_proxied any;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.0;
QL_ROOT_CONFIG
location QL_BASE_URL_LOCATION {
QL_ALIAS_CONFIG
index index.html index.htm;
try_files $uri QL_BASE_URLindex.html;
}
location ~ .*\.(html)$ {
add_header Cache-Control no-cache;
}
}
+45
View File
@@ -0,0 +1,45 @@
user root;
worker_processes auto;
pcre_jit on;
error_log /var/log/nginx/error.log warn;
include /etc/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
client_max_body_size 4096m;
client_body_buffer_size 20m;
keepalive_timeout 65;
sendfile on;
tcp_nodelay on;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:2m;
gzip on;
gzip_static on;
gzip_types text/plain application/json application/javascript application/x-javascript text/css application/xml text/javascript;
gzip_proxied any;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.0;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
include /etc/nginx/conf.d/*.conf;
}
+25 -5
View File
@@ -1,14 +1,14 @@
module.exports = { module.exports = {
apps: [ apps: [
{ {
name: 'qinglong', name: 'schedule',
max_restarts: 5, max_restarts: 10,
kill_timeout: 1000, kill_timeout: 15000,
wait_ready: true, wait_ready: true,
listen_timeout: 5000, listen_timeout: 10000,
source_map_support: true, source_map_support: true,
time: true, time: true,
script: 'static/build/app.js', script: 'static/build/schedule/index.js',
env: { env: {
http_proxy: '', http_proxy: '',
https_proxy: '', https_proxy: '',
@@ -18,5 +18,25 @@ module.exports = {
ALL_PROXY: '', ALL_PROXY: '',
}, },
}, },
{
name: 'public',
max_restarts: 10,
kill_timeout: 15000,
wait_ready: true,
listen_timeout: 10000,
source_map_support: true,
time: true,
script: 'static/build/public.js',
},
{
name: 'panel',
max_restarts: 10,
kill_timeout: 15000,
wait_ready: true,
listen_timeout: 10000,
source_map_support: true,
time: true,
script: 'static/build/app.js',
},
], ],
}; };
+2 -12
View File
@@ -1,15 +1,5 @@
{ {
"watch": [ "watch": ["back", ".env"],
"back",
".env"
],
"ext": "js,ts,json", "ext": "js,ts,json",
"env": { "exec": "ts-node -P tsconfig.back.json ./back/app.ts"
"NODE_ENV": "development",
"TS_NODE_PROJECT": "./back/tsconfig.json"
},
"verbose": true,
"execMap": {
"ts": "node --require ts-node/register"
}
} }

Some files were not shown because too many files have changed in this diff Show More