From e2dbacb383acdd981a5f09d07c3a4e6ee96770d5 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Thu, 14 Oct 2021 00:22:49 +0800 Subject: [PATCH] Fix encoding problem of ping result for non-English Windows --- package-lock.json | 93 ++++++++++++++++++++++++++++++++++++++----- package.json | 4 +- server/ping-lite.js | 24 +++++++++++ server/util-server.js | 13 ++++++ 4 files changed, 124 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 296362633..31eeb4c6e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "axios": "~0.21.4", "bcryptjs": "~2.4.3", "bootstrap": "~5.1.1", + "chardet": "^1.3.0", "chart.js": "~3.5.1", "chartjs-adapter-dayjs": "~1.0.0", "command-exists": "~1.2.9", @@ -28,6 +29,7 @@ "express-basic-auth": "~1.2.0", "form-data": "~4.0.0", "http-graceful-shutdown": "~3.1.4", + "iconv-lite": "^0.6.3", "jsonwebtoken": "~8.5.1", "nodemailer": "~6.6.5", "notp": "~2.0.3", @@ -64,7 +66,7 @@ "@vitejs/plugin-legacy": "~1.6.1", "@vitejs/plugin-vue": "~1.9.2", "@vue/compiler-sfc": "~3.2.19", - "babel-plugin-rewire": "^1.2.0", + "babel-plugin-rewire": "~1.2.0", "core-js": "~3.18.1", "cross-env": "~7.0.3", "dns2": "~2.0.1", @@ -3551,6 +3553,17 @@ "ms": "2.0.0" } }, + "node_modules/body-parser/node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/body-parser/node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -3813,6 +3826,11 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/chardet": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/chardet/-/chardet-1.3.0.tgz", + "integrity": "sha512-cyTQGGptIjIT+CMGT5J/0l9c6Fb+565GCFjjeUTKxUO7w3oR+FcNCMEKTn5xtVKaLFmladN7QF68IiQsv5Fbdw==" + }, "node_modules/chart.js": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.5.1.tgz", @@ -6303,11 +6321,11 @@ } }, "node_modules/iconv-lite": { - "version": "0.4.24", - "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", - "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", "dependencies": { - "safer-buffer": ">= 2.1.2 < 3" + "safer-buffer": ">= 2.1.2 < 3.0.0" }, "engines": { "node": ">=0.10.0" @@ -9527,6 +9545,17 @@ "node": ">= 0.8" } }, + "node_modules/raw-body/node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", @@ -11978,6 +12007,18 @@ "iconv-lite": "0.4.24" } }, + "node_modules/whatwg-encoding/node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dev": true, + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/whatwg-mimetype": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz", @@ -14905,6 +14946,14 @@ "ms": "2.0.0" } }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -15090,6 +15139,11 @@ "integrity": "sha512-mKKUkUbhPpQlCOfIuZkvSEgktjPFIsZKRRbC6KWVEMvlzblj3i3asQv5ODsrwt0N3pHAEvjP8KTQPHkp0+6jOg==", "dev": true }, + "chardet": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/chardet/-/chardet-1.3.0.tgz", + "integrity": "sha512-cyTQGGptIjIT+CMGT5J/0l9c6Fb+565GCFjjeUTKxUO7w3oR+FcNCMEKTn5xtVKaLFmladN7QF68IiQsv5Fbdw==" + }, "chart.js": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.5.1.tgz", @@ -16967,11 +17021,11 @@ "dev": true }, "iconv-lite": { - "version": "0.4.24", - "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", - "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", "requires": { - "safer-buffer": ">= 2.1.2 < 3" + "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "ieee754": { @@ -19393,6 +19447,16 @@ "http-errors": "1.7.2", "iconv-lite": "0.4.24", "unpipe": "1.0.0" + }, + "dependencies": { + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + } } }, "react-is": { @@ -21251,6 +21315,17 @@ "dev": true, "requires": { "iconv-lite": "0.4.24" + }, + "dependencies": { + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dev": true, + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + } } }, "whatwg-mimetype": { diff --git a/package.json b/package.json index 03112518a..d117e7f2b 100644 --- a/package.json +++ b/package.json @@ -60,9 +60,9 @@ "@popperjs/core": "~2.10.2", "args-parser": "~1.3.0", "axios": "~0.21.4", - "babel-plugin-rewire": "~1.2.0", "bcryptjs": "~2.4.3", "bootstrap": "~5.1.1", + "chardet": "^1.3.0", "chart.js": "~3.5.1", "chartjs-adapter-dayjs": "~1.0.0", "command-exists": "~1.2.9", @@ -72,6 +72,7 @@ "express-basic-auth": "~1.2.0", "form-data": "~4.0.0", "http-graceful-shutdown": "~3.1.4", + "iconv-lite": "^0.6.3", "jsonwebtoken": "~8.5.1", "nodemailer": "~6.6.5", "notp": "~2.0.3", @@ -108,6 +109,7 @@ "@vitejs/plugin-legacy": "~1.6.1", "@vitejs/plugin-vue": "~1.9.2", "@vue/compiler-sfc": "~3.2.19", + "babel-plugin-rewire": "~1.2.0", "core-js": "~3.18.1", "cross-env": "~7.0.3", "dns2": "~2.0.1", diff --git a/server/ping-lite.js b/server/ping-lite.js index 0af0e9706..291bf20d3 100644 --- a/server/ping-lite.js +++ b/server/ping-lite.js @@ -4,6 +4,8 @@ const net = require("net"); const spawn = require("child_process").spawn; const events = require("events"); const fs = require("fs"); +const util = require("./util-server"); + const WIN = /^win/.test(process.platform); const LIN = /^linux/.test(process.platform); const MAC = /^darwin/.test(process.platform); @@ -101,6 +103,9 @@ Ping.prototype.send = function (callback) { }); this._ping.stdout.on("data", function (data) { // log stdout + if (WIN) { + data = convertOutput(data); + } this._stdout = (this._stdout || "") + data; }); @@ -112,6 +117,9 @@ Ping.prototype.send = function (callback) { }); this._ping.stderr.on("data", function (data) { // log stderr + if (WIN) { + data = convertOutput(data); + } this._stderr = (this._stderr || "") + data; }); @@ -157,3 +165,19 @@ Ping.prototype.start = function (callback) { Ping.prototype.stop = function () { clearInterval(this._i); }; + +/** + * Try to convert to UTF-8 for Windows, as the ping's output on Windows is not UTF-8 and could be in other languages + * Thank @pemassi + * https://github.com/louislam/uptime-kuma/issues/570#issuecomment-941984094 + * @param data + * @returns {string} + */ +function convertOutput(data) { + if (WIN) { + if (data) { + return util.convertToUTF8(data); + } + } + return data; +} diff --git a/server/util-server.js b/server/util-server.js index 5620d674b..aecb74e8e 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -6,6 +6,8 @@ const passwordHash = require("./password-hash"); const dayjs = require("dayjs"); const { Resolver } = require("dns"); const child_process = require("child_process"); +const iconv = require("iconv-lite"); +const chardet = require("chardet"); /** * Init or reset JWT secret @@ -312,3 +314,14 @@ exports.startUnitTest = async () => { process.exit(code); }); }; + +/** + * @param body : Buffer + * @returns {string} + */ +exports.convertToUTF8 = (body) => { + const guessEncoding = chardet.detect(body); + debug("Guess Encoding: " + guessEncoding); + const str = iconv.decode(body, guessEncoding); + return str.toString(); +};