From 89fed4666d81fbbcfe704aa5a88e0270eafb367a Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 24 Apr 2024 00:32:04 +0800 Subject: [PATCH] fix: fix ping action throw error in windows --- pnpm-lock.yaml | 10 ++++++++++ src/server/model/monitor/provider/ping.ts | 11 ++++++++++- src/server/package.json | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 734d1b4..f33ec37 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -377,6 +377,9 @@ importers: bcryptjs: specifier: ^2.4.3 version: 2.4.3 + chardet: + specifier: ^2.0.0 + version: 2.0.0 compose-middleware: specifier: ^5.0.1 version: 5.0.1 @@ -410,6 +413,9 @@ importers: fs-extra: specifier: ^11.2.0 version: 11.2.0 + iconv-lite: + specifier: ^0.6.3 + version: 0.6.3 is-localhost-ip: specifier: ^2.0.0 version: 2.0.0 @@ -12042,6 +12048,10 @@ packages: resolution: {integrity: sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==} dev: true + /chardet@2.0.0: + resolution: {integrity: sha512-xVgPpulCooDjY6zH4m9YW3jbkaBe3FKIAvF5sj5t7aBNsVl2ljIE+xwJ4iNgiDZHFQvNIpjdKdVOQvvk5ZfxbQ==} + dev: false + /charenc@0.0.2: resolution: {integrity: sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==} diff --git a/src/server/model/monitor/provider/ping.ts b/src/server/model/monitor/provider/ping.ts index e0518d6..f01ef9b 100644 --- a/src/server/model/monitor/provider/ping.ts +++ b/src/server/model/monitor/provider/ping.ts @@ -1,6 +1,8 @@ import { MonitorProvider } from './type'; import pingUtils from 'ping'; import os from 'os'; +import chardet from 'chardet'; +import iconv from 'iconv-lite'; export const ping: MonitorProvider<{ hostname: string; @@ -39,7 +41,7 @@ function pingAction(hostname: string, packetSize = 56) { resolve(res.time); } else { if (isWindows) { - reject(new Error(exports.convertToUTF8(res.output))); + reject(new Error(convertToUTF8(res.output))); } else { reject(new Error(res.output)); } @@ -50,3 +52,10 @@ function pingAction(hostname: string, packetSize = 56) { }); }); } + +function convertToUTF8(body: string) { + const buffer = Buffer.from(body); + const guessEncoding = chardet.detect(buffer) ?? 'UTF-8'; + const str = iconv.decode(buffer, guessEncoding); + return str.toString(); +} diff --git a/src/server/package.json b/src/server/package.json index ae8569d..2cf1d6e 100644 --- a/src/server/package.json +++ b/src/server/package.json @@ -28,6 +28,7 @@ "axios": "^1.5.0", "badge-maker": "^3.3.1", "bcryptjs": "^2.4.3", + "chardet": "^2.0.0", "compose-middleware": "^5.0.1", "compression": "^1.7.4", "cors": "^2.8.5", @@ -39,6 +40,7 @@ "express-async-errors": "^3.1.1", "express-validator": "^7.0.1", "fs-extra": "^11.2.0", + "iconv-lite": "^0.6.3", "is-localhost-ip": "^2.0.0", "isolated-vm": "^4.7.2", "jsonwebtoken": "^9.0.2",