Compare commits

...
This repository has been archived on 2022-03-31. You can view files and clone it, but cannot push or open issues or pull requests.

3 Commits

Author SHA1 Message Date
fe61ad7fac
rename file 2022-03-28 18:02:24 +03:00
e449b37f25
update: parser, add send_tx 2022-03-28 18:01:21 +03:00
99fe592d81
feat: homemade scnario executor 2022-03-28 14:03:06 +03:00
6 changed files with 142 additions and 0 deletions

40
src/builder.js Normal file
View File

@ -0,0 +1,40 @@
const crypto = require("crypto");
const scenario = require("./scenario");
const config = require("./config");
function builder(scenarioName, scenarioConfig) {
const scenes = [];
const builtScenario = scenario[scenarioName](scenarioConfig);
const sessionId = crypto.randomBytes(16).toString("hex");
for (const scene of builtScenario) {
scene.sessionId = sessionId;
Object.assign(scene, config);
scenes.push(scene);
}
return scenes;
}
function parseScenario(scenarioString) {
const [scenarioName, scrnarioConfig] = scenarioString.split(" ");
return builder(scenarioName, parseScenarioConfig(scrnarioConfig));
}
function parseScenarioConfig(scenarioConfigString) {
const configs = scenarioConfigString.split(",");
const configObject = {};
for (const conf of configs) {
[key, value] = conf.split("=");
configObject[key] = value;
}
return configObject;
}
module.exports = { parseScenario };

13
src/config.js Normal file
View File

@ -0,0 +1,13 @@
module.exports = {
ussd: {
serviceCode: "*483*061#",
endpoint: "https://ussd.grassecon.net",
timeout: 3000,
},
test: {
waitNextMenu: 3000,
},
user: {
ussdPhone: "254711777734",
},
};

25
src/parser.js Normal file
View File

@ -0,0 +1,25 @@
const setTimeout = require("timers/promises").setTimeout;
const readFile = require("fs").readFile;
const parseScenario = require("./builder").parseScenario;
const request = require("./request").request;
readFile(process.argv[2], "utf8", (err, data) => {
if (err) {
return console.log(err);
}
execute(data);
});
async function execute(input) {
const parsedScenarioFile = input.split("\n");
for (const work of parsedScenarioFile) {
const parsedWork = parseScenario(work);
for (const requestJob of parsedWork) {
await request(requestJob);
await setTimeout(requestJob.test.waitNextMenu);
}
}
}

34
src/request.js Normal file
View File

@ -0,0 +1,34 @@
const phin = require("phin");
const rangi = require("rangi");
async function request(builtObject) {
const requestOptions = {
url: builtObject.ussd.endpoint,
method: "POST",
parse: "string",
timeout: builtObject.ussd.timeout,
form: {
sessionId: builtObject.sessionId,
phoneNumber: builtObject.user.ussdPhone,
serviceCode: builtObject.ussd.serviceCode,
text: builtObject.input,
},
};
try {
console.log(rangi.yellow(`${builtObject.input}\n-------------`));
const { body } = await phin(requestOptions);
if (body.length > 1) {
console.log(rangi.cyan(`${body.slice(4)}\n-------------`));
return;
}
throw new Error("EMPTY_BODY");
} catch (error) {
console.log(error);
}
}
module.exports = { request };

4
src/sample Normal file
View File

@ -0,0 +1,4 @@
reset_pin current_pin=2222,new_pin=3333
reset_pin current_pin=3333,new_pin=2222
send_tx to=0727806655,amount=1,pin=2222
send_tx to=0706533739,amount=1,pin=2222

26
src/scenario.js Normal file
View File

@ -0,0 +1,26 @@
module.exports = {
reset_pin: function (input) {
return [
{ input: `` },
{ input: `3` },
{ input: `3*5` },
{ input: `3*5*1` },
{ input: `3*5*1*${input.current_pin}` },
{ input: `3*5*1*${input.current_pin}*${input.new_pin}` },
{ input: `3*5*1*${input.current_pin}*${input.new_pin}*${input.new_pin}` },
{
input: `3*5*1*${input.current_pin}*${input.new_pin}*${input.new_pin}*99`,
},
];
},
send_tx: function (input) {
return [
{ input: `` },
{ input: `1` },
{ input: `1*${input.to}` },
{ input: `1*${input.to}*${input.amount}` },
{ input: `1*${input.to}*${input.amount}*${input.pin}` },
{ input: `1*${input.to}*${input.amount}*${input.pin}*99` },
];
},
};