From ba841740fb143ee41422cbe68bb9057a53a4ffcf Mon Sep 17 00:00:00 2001 From: cheetah Date: Sun, 13 Nov 2022 23:43:27 +0000 Subject: [PATCH] added eMessage Puppeteering Connector --- config.json | 4 + html/index.html | 16 ++++ index.js | 5 + package.json | 1 + .../connectors/eMessagePuppeteerConnector.js | 96 +++++++++++++++++++ types/connectors/index.js | 1 + 6 files changed, 123 insertions(+) create mode 100644 types/connectors/eMessagePuppeteerConnector.js diff --git a/config.json b/config.json index 20f6a04..545b487 100644 --- a/config.json +++ b/config.json @@ -20,6 +20,10 @@ "enabled": false, "duplexTimeout": 180 }, + "emPuppettering": { + "enabled": true, + "duplexTimeout": 180 + }, "emessage": { "enabled": false, "username": "", diff --git a/html/index.html b/html/index.html index 7534391..1fb90c7 100644 --- a/html/index.html +++ b/html/index.html @@ -78,6 +78,16 @@ +
+ eMessage Puppeteering Connector + + +
+ + +
+ + emPuppettering
eMessage Connector @@ -138,6 +148,9 @@ $('#ecityruf_enabled').attr('checked', config.connectors.ecityruf.enabled) $('#ecityruf_duplex_timeout').val(config.connectors.ecityruf.duplexTimeout) + $('#emp_enabled').attr('checked', config.connectors.emPuppettering.enabled) + $('#emp_duplex_timeout').val(config.connectors.emPuppettering.duplexTimeout) + $('#emessage_enabled').attr('checked', config.connectors.emessage.enabled) $('#emessage_username').val(config.connectors.emessage.username) $('#emessage_password').val(config.connectors.emessage.password) @@ -166,6 +179,9 @@ // connectors.ecityruf newConfig.connectors.ecityruf.enabled = $('#ecityruf_enabled').prop('checked') newConfig.connectors.ecityruf.duplexTimeout = parseInt($('#ecityruf_duplex_timeout').val()) + // connectors.emPuppettering + newConfig.connectors.emPuppettering.enabled = $('#emp_enabled').prop('checked') + newConfig.connectors.emPuppettering.duplexTimeout = parseInt($('#emp_duplex_timeout').val()) // connectors.emessage newConfig.connectors.emessage.enabled = $('#emessage_enabled').prop('checked') diff --git a/index.js b/index.js index a097184..685a4ca 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,11 @@ if (!!config.connectors.ecityruf && config.connectors.ecityruf.enabled === true) if (!!config.connectors.emessage && config.connectors.emessage.enabled === true) { types.ConnectorRegistry.register(new types.Connectors.eMessageConnector(connection)) } +if (!!config.connectors.emPuppettering && config.connectors.emPuppettering.enabled === true) { + types.ConnectorRegistry.register(new types.Connectors.eMessagePuppeteerConnector(connection)) +} + + types.ConnectorRegistry.register(new types.Connectors.DummyConnector()) types.DeviceRegistry.register(new types.devices.GenericPager()) diff --git a/package.json b/package.json index c9fd0c6..de66079 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "md5": "^2.3.0", "mqtt": "^4.2.6", "node-crc": "^1.3.0", + "puppeteer": "^19.2.2", "querystring": "^0.2.1", "socket.io": "^4.0.2" } diff --git a/types/connectors/eMessagePuppeteerConnector.js b/types/connectors/eMessagePuppeteerConnector.js new file mode 100644 index 0000000..58ac7db --- /dev/null +++ b/types/connectors/eMessagePuppeteerConnector.js @@ -0,0 +1,96 @@ +const Connector = require("./Connector") +const config = require('../../config.json') +const boskrypt = require('../../boskrypt') +const md5 = require('md5') +const axios = require('axios') +const puppeteer = require('puppeteer') +function sleep(ms) { return new Promise(r=>setTimeout(r, ms))} +// [ "ecityruf", "123456789" ] +class eMessagePuppeteerConnector extends Connector { + constructor (amqpConnMngr) { + super(amqpConnMngr) + this.name = "em-puppet" + this.duplexCapable = true + this.supportBOSkrypt = true + } + async transmitMessage(msg, params) { + const UUID = this.name+':'+md5(JSON.stringify([this.name,...params])) + const target = params[0] + if (target.split('#').length !== 2) throw 'No valid eMessage Parameter ' + const serviceName = target.split('#')[ 0 ], identifier = target.split('#')[ 1 ] + + const $device = msg.routingParams.device + let payloadBuffer = Buffer.from(msg.payload) + const boskryptSupport = require("../DeviceRegistry").Devices[ $device ].supportBOSkrypt || false + console.log('device supports boskrypt', boskryptSupport) + if (boskryptSupport && !!config.pagers[ $device ] && config.pagers[ $device ].boskrypt.enabled) { + //payloadBuffer + const keyTable = config.pagers[ $device ].boskrypt.keys + const FunkrufNummer = "em-"+identifier + console.log('LUT ', FunkrufNummer, 'key=', keyTable[ FunkrufNummer ]) + if (!!keyTable[ FunkrufNummer ]) { + payloadBuffer = Buffer.from(boskrypt.encrypt(payloadBuffer, keyTable[ FunkrufNummer ], 0)) + } + } + + let sendPromise = new Promise(async (res, rej) => { + const browser = await puppeteer.launch({ headless: true }) + this.connectorRegistry.reportState(msg, UUID, 'puppettering') + const page = await browser.newPage() + await page.goto('https://ruf.emessage.de/') + + const serviceSelect = 'select[name="pageSendForm:serviceSelect"]' + await page.waitForSelector(serviceSelect) + await page.select(serviceSelect, serviceName) + await page.click(serviceSelect) + await page.click(serviceSelect) + + await sleep(1e3) + + const selectServiceButton = 'input[name="pageSendForm:selectServiceButton"]' + await page.waitForSelector(selectServiceButton) + await page.click(selectServiceButton) + + + const pagerNumberInput = 'input[name="pageSendForm:pagerNumberInput"]' + await page.waitForSelector(pagerNumberInput) + await page.type(pagerNumberInput, identifier) + + const validatePagerNumberButton = 'input[id="pageSendForm:validatePagerNumberButton"]' + await page.waitForSelector(validatePagerNumberButton) + await page.click(validatePagerNumberButton) + + const pageContentInput = 'textarea[id="pageSendForm:pageContentInput"]' + await page.waitForSelector(pageContentInput) + await page.type(pageContentInput, payloadBuffer.toString('ascii')) + + const sendPageButton = 'input[id="pageSendForm:sendPageButton"]' + await page.waitForSelector(sendPageButton) + await page.click(sendPageButton) + + const messageSentResult = 'span[id="pageSendForm:messageSentResult"]' + await page.waitForSelector(messageSentResult) + + let $messageSentResult = await page.$(messageSentResult) + let value = await page.evaluate(el => el.textContent, $messageSentResult) + // pageSendForm:messageSentResult + if (value.indexOf("Meldung : OK") > -1) { + this.connectorRegistry.reportState(msg, UUID, 'transit') + await browser.close() + return res(true) + } else { + this.connectorRegistry.reportState(msg, UUID, 'fail') + await browser.close() + return rej(value) + } + }) + sendPromise + .catch((err) => { + console.error(err) + this.connectorRegistry.reportFail(msg, UUID) + return false + }) + + } +} +module.exports = eMessagePuppeteerConnector \ No newline at end of file diff --git a/types/connectors/index.js b/types/connectors/index.js index bf8b821..d3a5118 100644 --- a/types/connectors/index.js +++ b/types/connectors/index.js @@ -2,6 +2,7 @@ module.exports = { DAPNETConnector: require("./DAPNETConnector"), eCityrufConnector: require('./eCityrufConnector'), eMessageConnector: require('./eMessageConnector'), + eMessagePuppeteerConnector: require('./eMessagePuppeteerConnector'), LoRaWANConnector: require("./LoRaWANConnector"), POCSAGConnector: require("./POCSAGConnector"), DummyConnector: require("./DummyConnector"),