Fix encoding problem of ping result for non-English Windows

This commit is contained in:
Louis Lam 2021-10-14 00:22:49 +08:00
parent 9b05e86c25
commit e2dbacb383
4 changed files with 124 additions and 10 deletions

93
package-lock.json generated
View file

@ -19,6 +19,7 @@
"axios": "~0.21.4", "axios": "~0.21.4",
"bcryptjs": "~2.4.3", "bcryptjs": "~2.4.3",
"bootstrap": "~5.1.1", "bootstrap": "~5.1.1",
"chardet": "^1.3.0",
"chart.js": "~3.5.1", "chart.js": "~3.5.1",
"chartjs-adapter-dayjs": "~1.0.0", "chartjs-adapter-dayjs": "~1.0.0",
"command-exists": "~1.2.9", "command-exists": "~1.2.9",
@ -28,6 +29,7 @@
"express-basic-auth": "~1.2.0", "express-basic-auth": "~1.2.0",
"form-data": "~4.0.0", "form-data": "~4.0.0",
"http-graceful-shutdown": "~3.1.4", "http-graceful-shutdown": "~3.1.4",
"iconv-lite": "^0.6.3",
"jsonwebtoken": "~8.5.1", "jsonwebtoken": "~8.5.1",
"nodemailer": "~6.6.5", "nodemailer": "~6.6.5",
"notp": "~2.0.3", "notp": "~2.0.3",
@ -64,7 +66,7 @@
"@vitejs/plugin-legacy": "~1.6.1", "@vitejs/plugin-legacy": "~1.6.1",
"@vitejs/plugin-vue": "~1.9.2", "@vitejs/plugin-vue": "~1.9.2",
"@vue/compiler-sfc": "~3.2.19", "@vue/compiler-sfc": "~3.2.19",
"babel-plugin-rewire": "^1.2.0", "babel-plugin-rewire": "~1.2.0",
"core-js": "~3.18.1", "core-js": "~3.18.1",
"cross-env": "~7.0.3", "cross-env": "~7.0.3",
"dns2": "~2.0.1", "dns2": "~2.0.1",
@ -3551,6 +3553,17 @@
"ms": "2.0.0" "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": { "node_modules/body-parser/node_modules/ms": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
@ -3813,6 +3826,11 @@
"url": "https://github.com/sponsors/wooorm" "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": { "node_modules/chart.js": {
"version": "3.5.1", "version": "3.5.1",
"resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.5.1.tgz", "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.5.1.tgz",
@ -6303,11 +6321,11 @@
} }
}, },
"node_modules/iconv-lite": { "node_modules/iconv-lite": {
"version": "0.4.24", "version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"dependencies": { "dependencies": {
"safer-buffer": ">= 2.1.2 < 3" "safer-buffer": ">= 2.1.2 < 3.0.0"
}, },
"engines": { "engines": {
"node": ">=0.10.0" "node": ">=0.10.0"
@ -9527,6 +9545,17 @@
"node": ">= 0.8" "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": { "node_modules/react-is": {
"version": "17.0.2", "version": "17.0.2",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz",
@ -11978,6 +12007,18 @@
"iconv-lite": "0.4.24" "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": { "node_modules/whatwg-mimetype": {
"version": "2.3.0", "version": "2.3.0",
"resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz", "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz",
@ -14905,6 +14946,14 @@
"ms": "2.0.0" "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": { "ms": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
@ -15090,6 +15139,11 @@
"integrity": "sha512-mKKUkUbhPpQlCOfIuZkvSEgktjPFIsZKRRbC6KWVEMvlzblj3i3asQv5ODsrwt0N3pHAEvjP8KTQPHkp0+6jOg==", "integrity": "sha512-mKKUkUbhPpQlCOfIuZkvSEgktjPFIsZKRRbC6KWVEMvlzblj3i3asQv5ODsrwt0N3pHAEvjP8KTQPHkp0+6jOg==",
"dev": true "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": { "chart.js": {
"version": "3.5.1", "version": "3.5.1",
"resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.5.1.tgz", "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.5.1.tgz",
@ -16967,11 +17021,11 @@
"dev": true "dev": true
}, },
"iconv-lite": { "iconv-lite": {
"version": "0.4.24", "version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"requires": { "requires": {
"safer-buffer": ">= 2.1.2 < 3" "safer-buffer": ">= 2.1.2 < 3.0.0"
} }
}, },
"ieee754": { "ieee754": {
@ -19393,6 +19447,16 @@
"http-errors": "1.7.2", "http-errors": "1.7.2",
"iconv-lite": "0.4.24", "iconv-lite": "0.4.24",
"unpipe": "1.0.0" "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": { "react-is": {
@ -21251,6 +21315,17 @@
"dev": true, "dev": true,
"requires": { "requires": {
"iconv-lite": "0.4.24" "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": { "whatwg-mimetype": {

View file

@ -60,9 +60,9 @@
"@popperjs/core": "~2.10.2", "@popperjs/core": "~2.10.2",
"args-parser": "~1.3.0", "args-parser": "~1.3.0",
"axios": "~0.21.4", "axios": "~0.21.4",
"babel-plugin-rewire": "~1.2.0",
"bcryptjs": "~2.4.3", "bcryptjs": "~2.4.3",
"bootstrap": "~5.1.1", "bootstrap": "~5.1.1",
"chardet": "^1.3.0",
"chart.js": "~3.5.1", "chart.js": "~3.5.1",
"chartjs-adapter-dayjs": "~1.0.0", "chartjs-adapter-dayjs": "~1.0.0",
"command-exists": "~1.2.9", "command-exists": "~1.2.9",
@ -72,6 +72,7 @@
"express-basic-auth": "~1.2.0", "express-basic-auth": "~1.2.0",
"form-data": "~4.0.0", "form-data": "~4.0.0",
"http-graceful-shutdown": "~3.1.4", "http-graceful-shutdown": "~3.1.4",
"iconv-lite": "^0.6.3",
"jsonwebtoken": "~8.5.1", "jsonwebtoken": "~8.5.1",
"nodemailer": "~6.6.5", "nodemailer": "~6.6.5",
"notp": "~2.0.3", "notp": "~2.0.3",
@ -108,6 +109,7 @@
"@vitejs/plugin-legacy": "~1.6.1", "@vitejs/plugin-legacy": "~1.6.1",
"@vitejs/plugin-vue": "~1.9.2", "@vitejs/plugin-vue": "~1.9.2",
"@vue/compiler-sfc": "~3.2.19", "@vue/compiler-sfc": "~3.2.19",
"babel-plugin-rewire": "~1.2.0",
"core-js": "~3.18.1", "core-js": "~3.18.1",
"cross-env": "~7.0.3", "cross-env": "~7.0.3",
"dns2": "~2.0.1", "dns2": "~2.0.1",

View file

@ -4,6 +4,8 @@ const net = require("net");
const spawn = require("child_process").spawn; const spawn = require("child_process").spawn;
const events = require("events"); const events = require("events");
const fs = require("fs"); const fs = require("fs");
const util = require("./util-server");
const WIN = /^win/.test(process.platform); const WIN = /^win/.test(process.platform);
const LIN = /^linux/.test(process.platform); const LIN = /^linux/.test(process.platform);
const MAC = /^darwin/.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 this._ping.stdout.on("data", function (data) { // log stdout
if (WIN) {
data = convertOutput(data);
}
this._stdout = (this._stdout || "") + data; this._stdout = (this._stdout || "") + data;
}); });
@ -112,6 +117,9 @@ Ping.prototype.send = function (callback) {
}); });
this._ping.stderr.on("data", function (data) { // log stderr this._ping.stderr.on("data", function (data) { // log stderr
if (WIN) {
data = convertOutput(data);
}
this._stderr = (this._stderr || "") + data; this._stderr = (this._stderr || "") + data;
}); });
@ -157,3 +165,19 @@ Ping.prototype.start = function (callback) {
Ping.prototype.stop = function () { Ping.prototype.stop = function () {
clearInterval(this._i); 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;
}

View file

@ -6,6 +6,8 @@ const passwordHash = require("./password-hash");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const { Resolver } = require("dns"); const { Resolver } = require("dns");
const child_process = require("child_process"); const child_process = require("child_process");
const iconv = require("iconv-lite");
const chardet = require("chardet");
/** /**
* Init or reset JWT secret * Init or reset JWT secret
@ -312,3 +314,14 @@ exports.startUnitTest = async () => {
process.exit(code); 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();
};