plain xml fix

This commit is contained in:
Freddie
2020-05-13 22:18:27 +01:00
parent a093f387ea
commit 0db481305b
6 changed files with 119 additions and 41 deletions

View File

@@ -1,26 +1,16 @@
import { xmlToData, dataToXML, kencode, kitem } from './utils/KBinJSON';
import ejs from 'ejs';
import pug from 'pug';
import { xmlToData, dataToXML, kencode, kitem, kdecode } from './utils/KBinJSON';
import { KonmaiEncrypt } from './utils/KonmaiEncrypt';
import iconv from 'iconv-lite';
import { writeFileSync, readFileSync, write } from 'fs';
const data = {
attr: 0,
text: 'text',
};
// const data = readFileSync('response.bin');
// const decode = kdecode(data);
// console.log(dataToXML(decode));
const xmlData = {
root: { '@attr': { test: data.attr }, 'test': kitem('str', data.text) },
};
const ejsData = xmlToData(
ejs.render('<root test="<%= attr %>"><test><%= text %></test></root>', data)
const request = iconv.encode(
'<call model="GLD:J:A:A:2007072301" srcid="01201000000E7AC029BE">\n<services method="get"/>\n</call>',
'utf8'
);
const pugData = xmlToData(pug.render('root(test=attr)\n test #{text}', data));
console.log(JSON.stringify(xmlData, null, 4));
console.log(JSON.stringify(ejsData, null, 4));
console.log(JSON.stringify(pugData, null, 4));
console.log(kencode(xmlData).compare(kencode(ejsData)));
console.log(kencode(xmlData).compare(kencode(pugData)));
console.log(kencode(xmlData));
console.log(kencode(ejsData));
console.log(kencode(pugData));
const key = new KonmaiEncrypt('1-5ebc47ba-9868');
writeFileSync(`${key.getPublicKey()}.bin`, key.encrypt(request));

View File

@@ -4,6 +4,7 @@ import { facility } from './Facility';
import { pcbtracker } from './PCBTracker';
import { kitem } from '../../utils/KBinJSON';
import { EamuseModuleContainer } from '../EamuseModuleContainer';
import { Logger } from '../../utils/Logger';
export const core = new EamuseModuleContainer();
@@ -29,6 +30,19 @@ core.add('*', 'tax.get_phase', async (info, data, send) => {
});
});
core.add('*', 'dlstatus.progress', async (info, data, send) => {
await send.success();
});
core.add('*', 'posevent.income.sales.sale', async (info, data, send) => {
Logger.debug(JSON.stringify(data, null, 4));
await send.object([
{ '@attr': { status: 0 } },
{ '@attr': { status: 0 } },
{ '@attr': { status: 0 } },
]);
});
core.add(eacoin);
core.add(facility);
core.add(cardmng);

View File

@@ -3,6 +3,7 @@ import { Router } from 'express';
import { EamuseMiddleware, EamuseRoute } from '../middlewares/EamuseMiddleware';
import { core } from './Core';
import { EamuseModuleContainer } from './EamuseModuleContainer';
import { dataToXML } from '../utils/KBinJSON';
export const services = (url: string, modules: EamuseModuleContainer) => {
const routeEamuse = Router();
@@ -27,6 +28,8 @@ export const services = (url: string, modules: EamuseModuleContainer) => {
'lobby2',
'dlstatus',
'netlog',
'sidmgr',
'globby',
];
/* General Information */
@@ -60,6 +63,27 @@ export const services = (url: string, modules: EamuseModuleContainer) => {
services.item.push({ '@attr': { name: moduleName, url } });
}
// <response>
// <services>
// <item name="pcbtracker" url="http://192.168.1.57:60079/+" />
// <item name="posevent" url="http://192.168.1.57:60079/+" />
// <item name="pcbevent" url="http://192.168.1.57:60079/+" />
// <item name="message" url="http://192.168.1.57:60079/+" />
// <item name="facility" url="http://192.168.1.57:60079/+" />
// <item name="userdata" url="http://192.168.1.57:60079/+" />
// <item name="userid" url="http://192.168.1.57:60079/+" />
// <item name="cardmng" url="http://192.168.1.57:60079/+" />
// <item name="sidmgr" url="http://192.168.1.57:60079/+" />
// <item name="local" url="http://192.168.1.57:60079/+" />
// <item name="ntp" url="http://192.168.1.57:60079/+" />
// <item name="globby" url="http://192.168.1.57:5730/+" />
// <item
// name="keepalive"
// url="http://192.168.1.57:60079/+&amp;ga=192.168.1.57:60079&amp;ma=192.168.1.57:60079&amp;pa=192.168.1.57:60079&amp;ia=192.168.1.57:60079&amp;t1=1&amp;t2=5"
// />
// </services>
// </response>;
await send.object(services);
return;
});

View File

@@ -1,5 +1,5 @@
import { RequestHandler } from 'express';
import { findKey, get, has } from 'lodash';
import { findKey, get, has, isArray } from 'lodash';
import {
isKBin,
@@ -14,6 +14,7 @@ import LzKN from '../utils/LzKN';
import { Logger } from '../utils/Logger';
import { EamuseModuleContainer } from '../eamuse/EamuseModuleContainer';
import { EamuseSend } from '../eamuse/EamuseSend';
import { dataToXML } from '../utils/KBinJSON';
// const ACCEPT_AGENTS = ['EAMUSE.XRPC/1.0', 'EAMUSE.Httpac/1.0'];
@@ -118,16 +119,26 @@ export const EamuseMiddleware: RequestHandler = async (req, res, next) => {
return;
}
const eaModule = findKey(get(xml, 'call'), x => has(x, '@attr.method'));
const eaMethod = get(xml, `call.${eaModule}.@attr.method`);
const model = get(xml, 'call.@attr.model');
const eaModule = findKey(
get(xml, 'call'),
x => has(x, '@attr.method') || has(x, '0.@attr.method')
);
if (!eaModule || !eaMethod) {
if (!eaModule) {
res.sendStatus(404);
return;
}
Logger.debug(`${eaModule}.${eaMethod}`);
let moduleObj: any[] = get(xml, `call.${eaModule}`, null);
if (!isArray(moduleObj)) moduleObj = [moduleObj];
const eaMethods: string[] = moduleObj.map(x => get(x, `@attr.method`));
const eaMethod = eaMethods.join('.');
const model = get(xml, 'call.@attr.model');
if (!(process as any).pkg) {
Logger.debug(`${eaModule}.${eaMethod}\n${dataToXML(xml, false)}`);
}
req.body = {
data: xml,

View File

@@ -1,2 +1,2 @@
export const VERSION = 'v1.03 beta';
export const VERSION = 'v1.04 beta';
export const SAFEHEX = 'ADEBEEFA';

View File

@@ -436,15 +436,18 @@ function bufferToString(data: Buffer, encoding: KBinEncoding): string {
}
const str = iconv.decode(data, encoding);
const result = str.substr(0, str.length - 1);
if (result.startsWith(SAFEHEX)) return result;
if (result.length == 16) {
const hex_checker = /^[0-9a-fA-F]+$/;
if (hex_checker.test(result)) {
return 'DEADC0DEFEEDBEEF';
}
}
// if (result.startsWith(SAFEHEX)) return result;
// if (result.length == 16) {
// const hex_checker = /^[0-9a-fA-F]+$/;
// if (hex_checker.test(result)) {
// return 'DEADC0DEFEEDBEEF';
// }
// }
return result;
// replace(/(^|\s*)[0|E][A-F|a-f|0-9]{15}($|\s+)/g, '$1DEADC0DEFEEDBEEF$2');
}
function nodeToBinary(
@@ -764,9 +767,40 @@ export function ip2int(ip: string): number {
);
}
function escape(data: string | number): string {
return data
.toString()
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
function unescape(data: string | number): string {
return data
.toString()
.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&apos;/g, "'")
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
}
function transformJObj(obj: any, toStr = true): void {
if (obj !== null && obj !== undefined) {
const data = obj['@content'];
if (obj['@attr']) {
for (const attr in obj['@attr']) {
if (toStr) {
obj['@attr'][attr] = escape(obj['@attr'][attr]);
} else {
obj['@attr'][attr] = unescape(obj['@attr'][attr]);
}
}
}
if (data !== undefined) {
let vType = 'void';
if (obj['@attr'] && obj['@attr'].__type) {
@@ -776,7 +810,9 @@ function transformJObj(obj: any, toStr = true): void {
obj['@attr'].__count = toSafeInteger(obj['@attr'].__count);
}
if (toStr) {
if (typeof data !== 'object') {
if (typeof data == 'string') {
obj['@content'] = escape(data);
} else if (typeof data !== 'object') {
obj['@content'] = data.toString();
} else {
if (Buffer.isBuffer(data)) {
@@ -796,13 +832,15 @@ function transformJObj(obj: any, toStr = true): void {
} else if (vType === 'ip4') {
const parts = data.split(' ');
obj['@content'] = parts.map(ip2int);
} else if (vType !== 'str') {
} else if (vType !== 'str' && vType !== 'void') {
const parts = data.split(' ');
if (vType.endsWith('64')) {
obj['@content'] = parts.map(v => BigInt(v));
} else {
obj['@content'] = parts.map(v => parseFloat(v));
}
} else {
obj['@content'] = unescape(data);
}
}
}
@@ -827,7 +865,7 @@ function transformJObj(obj: any, toStr = true): void {
}
}
export function dataToXML(data: any): string | Buffer {
export function dataToXML(data: any, header: boolean = true): string {
const options = {
attributeNamePrefix: '',
attrNodeName: '@attr',
@@ -848,7 +886,8 @@ export function dataToXML(data: any): string | Buffer {
transformJObj(data, false);
return '<?xml version="1.0" encoding="UTF-8"?>\n' + xml;
if (header) return "<?xml version='1.0' encoding='UTF-8'?>\n" + xml;
else return xml;
}
export function dataToXMLBuffer(data: any, encoding: KBinEncoding): Buffer {
@@ -877,7 +916,7 @@ export function dataToXMLBuffer(data: any, encoding: KBinEncoding): Buffer {
export function detectXMLEncoding(xml: Buffer): KBinEncoding {
const header = iconv.decode(xml.subarray(0, Math.min(128, xml.length)), 'ascii');
const match = header.match(/<\?xml.*encoding=['|"](.*)['|"].*?>/);
if (match.length < 2) {
if (!match || match.length < 2) {
return 'utf8';
}
const encoding = XML2ICONV[match[1].toLowerCase()];