From e7d2c58c472db3cd8d156ecb28315d4420c510f8 Mon Sep 17 00:00:00 2001 From: vzakharchenko Date: Fri, 29 Oct 2021 00:38:07 +0300 Subject: [PATCH] replaced xml2json with xml2js --- jslib/public.js | 4 ++-- package.json | 5 +++-- src/ListSMS.ts | 39 ++++++++++++++++----------------------- src/MobileData.ts | 17 +++++++---------- src/startSession.ts | 4 ++-- 5 files changed, 30 insertions(+), 39 deletions(-) diff --git a/jslib/public.js b/jslib/public.js index df6a443..e42c692 100644 --- a/jslib/public.js +++ b/jslib/public.js @@ -1,7 +1,7 @@ /* eslint-disable */ const {restCalls} = require("../src/utils/DefaultRestCalls"); -const parser = require('xml2json'); +const parser = require('xml2js'); const CryptoJS = require('crypto-js'); const {RSAKey} = require('./rsa'); @@ -107,7 +107,7 @@ async function getPublicKey(session){ 'cookie': `sessionId=${session.SesInfo}`, __RequestVerificationToken: session.TokInfo }); - const message = JSON.parse(parser.toJson(resp)); + const message = await parser.parseStringPromise(resp); return message.response; } diff --git a/package.json b/package.json index 9625672..357342a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "e3372h-320-cli", - "version": "1.0.0", + "version": "1.0.1", "description": "e3372h-320 client", "main": "index.js", "scripts": { @@ -24,7 +24,7 @@ "axios": "^0.24.0", "crypto-js": "^4.1.1", "get-random-values": "^1.2.2", - "xml2json": "^0.12.0", + "xml2js": "^0.4.23", "yargs": "^17.2.1" }, "repository": { @@ -40,6 +40,7 @@ ], "devDependencies": { "@shopify/eslint-plugin": "^40.4.0", + "@types/xml2js": "^0.4.9", "@types/xml2json": "^0.11.4", "@types/yargs": "^17.0.4", "@typescript-eslint/eslint-plugin": "^5.2.0", diff --git a/src/ListSMS.ts b/src/ListSMS.ts index 94e06e4..752dac8 100644 --- a/src/ListSMS.ts +++ b/src/ListSMS.ts @@ -1,6 +1,6 @@ import fs from "fs"; -import parser from 'xml2json'; +import parser from 'xml2js'; import {SessionData} from './startSession'; import {restCalls} from "./utils/DefaultRestCalls"; @@ -34,7 +34,7 @@ export async function getSMSByUsers(sessionData: SessionData, 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8;enc', Cookie: `SessionId=${sessionData.SesInfo}`, }); - const pwdret = JSON.parse(parser.toJson(resp)); + const pwdret = await parser.parseStringPromise((resp)); const ret = huawei.dataDecrypt(scram, smsNonce, smsSalt, nonceStr, pwdret); if (exportFormat !== 'hide') { @@ -42,11 +42,10 @@ export async function getSMSByUsers(sessionData: SessionData, await saveFile(exportFile, ret); console.info(`xml file ${exportFile} created`); } else if (exportFormat === 'json') { - await saveFile(exportFile, parser.toJson(ret)); + await saveFile(exportFile, JSON.stringify(await parser.parseStringPromise(ret))); console.info(`json file ${exportFile} created`); } else { - const text = parser.toJson(ret); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(ret); if (Array.isArray(json.response.messages.message)) { json.response.messages.message.forEach((message: any) => { console.log(`MessageId: ${message.index} Phone: ${message.phone} Message: ${JSON.stringify(message.content)}`); @@ -59,8 +58,7 @@ export async function getSMSByUsers(sessionData: SessionData, } if (deleteAfter) { - const text = parser.toJson(ret); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(ret); const messages = json.response.messages.message; for (let i = 0; i < messages.length; i++) { await deleteMessage(sessionData, messages[i].index); @@ -79,8 +77,7 @@ export async function getContactSMSPages(sessionData: SessionData, Cookie: `SessionId=${sessionData.SesInfo}`, }); - const text = parser.toJson(resp); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(resp); let number = Math.floor(json.response.count / 21); if (number > 0) { number += 1; @@ -90,7 +87,7 @@ export async function getContactSMSPages(sessionData: SessionData, await saveFile(exportFile, resp); console.info(`xml file ${exportFile} created`); } else if (exportFormat === 'json') { - await saveFile(exportFile, parser.toJson(resp)); + await saveFile(exportFile, JSON.stringify(await parser.parseStringPromise(resp))); console.info(`json file ${exportFile} created`); } else { console.info(`${number}`); @@ -103,8 +100,7 @@ export async function getSMSPages(sessionData: SessionData, exportFile: string, exportFormat: ExportFormat) { const resp = await restCalls.fetchData(`http://${sessionData.url}/api/sms/sms-count`, 'GET'); - const text = parser.toJson(resp); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(resp); let number = Math.floor((json.response.LocalInbox + json.response.LocalOutbox) / 21); if (number > 0) { number += 1; @@ -114,7 +110,7 @@ export async function getSMSPages(sessionData: SessionData, await saveFile(exportFile, resp); console.info(`xml file ${exportFile} created`); } else if (exportFormat === 'json') { - await saveFile(exportFile, parser.toJson(resp)); + await saveFile(exportFile, JSON.stringify(await parser.parseStringPromise(resp))); console.info(`json file ${exportFile} created`); } else { console.info(`${number}`); @@ -131,10 +127,9 @@ export async function deleteMessage(sessionData: SessionData, 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8;enc', Cookie: `SessionId=${sessionData.SesInfo}`, }); - const text = parser.toJson(resp); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(resp); if (json.response !== 'OK') { - throw new Error(`Delete message error: ${text}`); + throw new Error(`Delete message error: ${JSON.stringify(json)}`); } console.info('Message or Contact deleted'); } @@ -153,10 +148,9 @@ export async function sendMessage(sessionData: SessionData, 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8;enc', Cookie: `SessionId=${sessionData.SesInfo}`, }); - const text = parser.toJson(resp); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(resp); if (json.response !== 'OK') { - throw new Error(`Delete message error: ${text}`); + throw new Error(`Delete message error: ${JSON.stringify(json)}`); } console.info('Message sent'); } @@ -183,17 +177,16 @@ export async function getSMSContacts(sessionData: SessionData, __RequestVerificationToken: sessionData.TokInfo, Cookie: `SessionId=${sessionData.SesInfo}`, }); - const pwdret = JSON.parse(parser.toJson(resp)); + const pwdret = await parser.parseStringPromise(resp); const ret = huawei.dataDecrypt(scram, smsNonce, smsSalt, nonceStr, pwdret); if (exportFormat === 'xml') { await saveFile(exportFile, ret); console.info(`xml file ${exportFile} created`); } else if (exportFormat === 'json') { - await saveFile(exportFile, parser.toJson(ret)); + await saveFile(exportFile, JSON.stringify(await parser.parseStringPromise(ret))); console.info(`json file ${exportFile} created`); } else { - const text = parser.toJson(ret); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(ret); if (Array.isArray(json.response.messages.message)) { json.response.messages.message.forEach((message: any) => { console.log(`MessageId: ${message.index} Phone: ${message.phone} lastMessage: ${JSON.stringify(message.content)}`); diff --git a/src/MobileData.ts b/src/MobileData.ts index 161ac57..6b133b1 100644 --- a/src/MobileData.ts +++ b/src/MobileData.ts @@ -1,6 +1,6 @@ import fs from "fs"; -import parser from "xml2json"; +import parser from "xml2js"; import {SessionData} from "./startSession"; import {restCalls} from "./utils/DefaultRestCalls"; @@ -19,10 +19,9 @@ export async function controlMobileData(sessionData: SessionData, mobileStatus: __RequestVerificationToken: sessionData.TokInfo, Cookie: `SessionId=${sessionData.SesInfo}`, }); - const text = parser.toJson(resp); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(resp); if (json.response !== 'OK') { - throw new Error(`Control Mobile Data error: ${text}`); + throw new Error(`Control Mobile Data error: ${JSON.stringify(json)}`); } console.log(`Control Mobile Data changed to ${mobileStatus}`); } @@ -33,10 +32,9 @@ export async function reconnect(sessionData: SessionData) { __RequestVerificationToken: sessionData.TokInfo, Cookie: `SessionId=${sessionData.SesInfo}`, }); - const text = parser.toJson(resp); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(resp); if (json.response !== 'OK') { - throw new Error(`Reconnecting error: ${text}`); + throw new Error(`Reconnecting error: ${JSON.stringify(json)}`); } console.log('Reconnected'); } @@ -52,11 +50,10 @@ export async function status(sessionData: SessionData, exportFile: string, await saveFile(exportFile, resp); console.info(`xml file ${exportFile} created`); } else if (exportFormat === 'json') { - await saveFile(exportFile, parser.toJson(resp)); + await saveFile(exportFile, JSON.stringify(await parser.parseStringPromise(resp))); console.info(`json file ${exportFile} created`); } else { - const text = parser.toJson(resp); - const json = JSON.parse(text); + const json = await parser.parseStringPromise(resp); const response = json.response; Object.keys(response).forEach((key) => { console.info(`${key}=${response[key]}`); diff --git a/src/startSession.ts b/src/startSession.ts index b5f90e1..855c070 100644 --- a/src/startSession.ts +++ b/src/startSession.ts @@ -1,4 +1,4 @@ -import parser from 'xml2json'; +import parser from 'xml2js'; import {restCalls} from "./utils/DefaultRestCalls"; @@ -29,7 +29,7 @@ type SessionData0 = { // async function getSessionId(url: string): Promise { const resp = await restCalls.fetchData(`http://${url}/api/webserver/SesTokInfo`, 'GET'); - const message = JSON.parse(parser.toJson(resp)); + const message = await parser.parseStringPromise(resp); return message.response; } //