mirror of
https://github.com/whyour/qinglong.git
synced 2026-10-16 22:35:32 +08:00
fix(notify): audit system and JS/Python script notification results
This commit is contained in:
+14
-3
@@ -85,9 +85,13 @@ add_cron_api() {
|
||||
sub_id="null"
|
||||
fi
|
||||
|
||||
local payload
|
||||
payload=$(jq -cn --arg name "$name" --arg command "$command" \
|
||||
--arg schedule "$schedule" --argjson sub_id "$sub_id" \
|
||||
'{name: $name, command: $command, schedule: $schedule, sub_id: $sub_id}') || return $?
|
||||
local api=$(ql_api_request \
|
||||
"http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
|
||||
--data-raw "{\"name\":\"${name//\"/\\\"}\",\"command\":\"${command//\"/\\\"}\",\"schedule\":\"$schedule\",\"sub_id\":$sub_id}" \
|
||||
--data-raw "$payload" \
|
||||
-X POST)
|
||||
code=$(echo "$api" | jq -r .code)
|
||||
message=$(echo "$api" | jq -r .message)
|
||||
@@ -112,10 +116,14 @@ update_cron_api() {
|
||||
local id="$4"
|
||||
fi
|
||||
|
||||
local payload
|
||||
payload=$(jq -cn --arg name "$name" --arg command "$command" \
|
||||
--arg schedule "$schedule" --arg id "$id" \
|
||||
'{name: $name, command: $command, schedule: $schedule, id: $id}') || return $?
|
||||
local api=$(ql_api_request \
|
||||
"http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
|
||||
-X PUT \
|
||||
--data-raw "{\"name\":\"${name//\"/\\\"}\",\"command\":\"${command//\"/\\\"}\",\"schedule\":\"$schedule\",\"id\":\"$id\"}")
|
||||
--data-raw "$payload")
|
||||
code=$(echo "$api" | jq -r .code)
|
||||
message=$(echo "$api" | jq -r .message)
|
||||
if [[ $code == 200 ]]; then
|
||||
@@ -135,10 +143,13 @@ update_cron_command_api() {
|
||||
local id="$2"
|
||||
fi
|
||||
|
||||
local payload
|
||||
payload=$(jq -cn --arg command "$command" --arg id "$id" \
|
||||
'{command: $command, id: $id}') || return $?
|
||||
local api=$(ql_api_request \
|
||||
"http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
|
||||
-X PUT \
|
||||
--data-raw "{\"command\":\"${command//\"/\\\"}\",\"id\":\"$id\"}")
|
||||
--data-raw "$payload")
|
||||
code=$(echo "$api" | jq -r .code)
|
||||
message=$(echo "$api" | jq -r .message)
|
||||
if [[ $code == 200 ]]; then
|
||||
|
||||
@@ -47,6 +47,7 @@ class GrpcClient {
|
||||
'updateEnvNames',
|
||||
'getEnvById',
|
||||
'systemNotify',
|
||||
'recordNotifyLog',
|
||||
'getCronDetail',
|
||||
'createCron',
|
||||
'updateCron',
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
|
||||
// A reporting failure must never terminate a script or change its send result.
|
||||
module.exports = async function reportNotificationAudit(payload) {
|
||||
let client;
|
||||
try {
|
||||
if (!process.env.QL_DIR) return;
|
||||
const grpc = require('@grpc/grpc-js');
|
||||
const loader = require('@grpc/proto-loader');
|
||||
const certDir = path.join(
|
||||
process.env.QL_DATA_DIR || path.join(process.env.QL_DIR, 'data'),
|
||||
'config/grpc',
|
||||
);
|
||||
const credentials = grpc.credentials.createSsl(
|
||||
fs.readFileSync(path.join(certDir, 'ca.crt')),
|
||||
fs.readFileSync(path.join(certDir, 'client.key')),
|
||||
fs.readFileSync(path.join(certDir, 'client.crt')),
|
||||
);
|
||||
const definition = loader.loadSync(
|
||||
path.join(process.env.QL_DIR, 'back/protos/api.proto'),
|
||||
{ keepCase: true, defaults: true },
|
||||
);
|
||||
const Api = grpc.loadPackageDefinition(definition).com.ql.api.Api;
|
||||
client = new Api(
|
||||
`localhost:${process.env.GRPC_PORT || '5500'}`,
|
||||
credentials,
|
||||
{ 'grpc.enable_http_proxy': 0 },
|
||||
);
|
||||
await new Promise((resolve) => {
|
||||
client.RecordNotifyLog(
|
||||
payload,
|
||||
new grpc.Metadata(),
|
||||
{ deadline: new Date(Date.now() + 1500) },
|
||||
() => resolve(),
|
||||
);
|
||||
});
|
||||
} catch (_) {
|
||||
// Missing certificates, old panels and offline services are best effort.
|
||||
} finally {
|
||||
if (client) client.close();
|
||||
}
|
||||
};
|
||||
+6
-2
@@ -99,7 +99,9 @@ add_cron() {
|
||||
cron_name=$(grep "new Env" $file | awk -F "\(" '{print $2}' | awk -F "\)" '{print $1}' | sed 's:.*\('\''\|"\)\([^"'\'']*\)\('\''\|"\).*:\2:' | sed 's:"::g' | sed "s:'::g" | head -1)
|
||||
[[ -z $cron_name ]] && cron_name=$(grep "name:" $file | awk -F ":" '{print $2}' | head -1 | xargs)
|
||||
[[ -z $cron_name ]] && cron_name=$(basename "$file_name")
|
||||
result=$(add_cron_api "${cron_line}:${cmd_task} ${file}:${cron_name}:${SUB_ID}")
|
||||
cron_name=${cron_name//$'\r'/}
|
||||
cron_line=${cron_line//$'\r'/}
|
||||
result=$(add_cron_api "$cron_line" "${cmd_task} ${file}" "$cron_name" "$SUB_ID")
|
||||
echo -e "$result"
|
||||
if [[ $detail ]]; then
|
||||
detail="${detail}${result}\n"
|
||||
@@ -190,7 +192,9 @@ update_raw() {
|
||||
[[ -z $cron_line ]] && cron_line=$(grep "cron:" $raw_file_name | awk -F ":" '{print $2}' | head -1 | xargs)
|
||||
[[ -z $cron_line ]] && cron_line=$(grep "cron " $raw_file_name | awk -F "cron \"" '{print $2}' | awk -F "\" " '{print $1}' | head -1 | xargs)
|
||||
[[ -z $cron_line ]] && cron_line="$default_cron"
|
||||
result=$(add_cron_api "${cron_line}:${cmd_task} ${filename}:${cron_name}:${SUB_ID}")
|
||||
cron_name=${cron_name//$'\r'/}
|
||||
cron_line=${cron_line//$'\r'/}
|
||||
result=$(add_cron_api "$cron_line" "${cmd_task} ${filename}" "$cron_name" "$SUB_ID")
|
||||
echo -e "$result\n"
|
||||
notify_api "新增任务通知" "\n$result"
|
||||
# update_cron_api "$cron_line:$cmd_task $filename:$cron_name:$cron_id"
|
||||
|
||||
Reference in New Issue
Block a user