From 2e42a6d82b07e55c2416a4598d9b878db18c3c8a Mon Sep 17 00:00:00 2001 From: dsumberac <146445066+dsTw5@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:54:57 +0100 Subject: [PATCH] Add files via upload --- transfers/assets/index.html | 103 ++++++++++++++++++++------- transfers/functions/fetch_sid.js | 85 ++++++++-------------- transfers/functions/get_account.js | 2 +- transfers/functions/list_accounts.js | 35 +++++++++ transfers/functions/transfer.js | 6 +- 5 files changed, 145 insertions(+), 86 deletions(-) create mode 100644 transfers/functions/list_accounts.js diff --git a/transfers/assets/index.html b/transfers/assets/index.html index 05c4a9af..cfcac821 100644 --- a/transfers/assets/index.html +++ b/transfers/assets/index.html @@ -247,7 +247,7 @@

info.style.display = "none"; error.style.display = "none"; status_acc.style.display = "none"; - + data = { phone: phoneSID, laccount: losingAccountSID, @@ -269,6 +269,7 @@

if (json.er === 0) { error.style.display = ""; + info.style.display = "none"; error.innerHTML = `Wrong password.`; console.log(response); return; @@ -276,11 +277,15 @@

if (response.status === 200) { info.style.display = ""; - info.innerHTML = `
Success:
${JSON.stringify(json, null, 2)}
`; + info.innerHTML = `
Success :
${JSON.stringify( + json, + null, + 2, + )}
`; } else { console.error(json.error); error.style.display = ""; - error.innerHTML = `Something Wrong: ${json.error} - if you see the error "An internal server error has occurred", check that the Bundle SID is correct, present on the gaining account and approved for the number type you are transferring`; + error.innerHTML = `Something Wrong: ${json.error}
If you see the error "An internal server error has occurred", check that the Bundle SID is correct, present on the gaining account and approved for the number type you are transferring`; } } catch (err) { //console.log (err); @@ -291,9 +296,12 @@

} async function number_fetch() { + let adata = null; let fdata = null; + let aresponse = null; let fresponse = null; let json2 = null; + let ajson = null; let sid = null; let asid = null; let phoneNumber = null; @@ -308,40 +316,81 @@

warning.style.display = "none"; status_acc.style.display = "none"; - fdata = { - phone: phoneNumber, + adata = { pass: password, }; - fresponse = await fetch("./fetch_sid", { + aresponse = await fetch("./list_accounts", { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify(fdata), + body: JSON.stringify(adata), }); - console.log("return from function"); - json2 = await fresponse.json(); + console.log("return from Acc function"); + console.log(aresponse); + if (aresponse.status === 200) { + ajson = await aresponse.json(); + console.log(ajson); - if (json2.er === 0) { + if (ajson.er === 0) { + warning.style.display = ""; + status_acc.style.display="none"; + success.style.display = "none"; + warning.innerHTML = `Wrong password.`; + return; + } + } else { warning.style.display = ""; - warning.innerHTML = `Wrong password.`; - + warning.innerHTML = `Something went wrong with getting the list of accounts:
Server error is ${aresponse.message}
Server status is ${aresponse.status}`; return; } + for (a = 0; a < ajson.length; a++) { + fdata = { + phone: phoneNumber, + sub: ajson[a], + pass: password, + }; + + fresponse = await fetch("./fetch_sid", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(fdata), + }); + console.log("return from function"); + json2 = await fresponse.json(); + success.style.display = ""; + success.innerHTML = `
Checked ${a} accounts.
`; + + if (json2.er === 0) { + warning.style.display = ""; + success.style.display = "none"; + status_acc.style.display="none"; + warning.innerHTML = `Wrong password.`; + return; + } + if (json2 !== "not here") { + console.log(json2); + sid = json2.SID; + console.log(sid); + asid = json2.Acc; + phoneNumber = json2.pnumber; + numStatus = json2.status; + addReq = json2.addressReq; + break; + } else if (json2 === "not here") { + json2 = "number not found"; + } + } + if (json2 === "number not found") { warning.style.display = ""; + success.style.display = "none"; warning.innerHTML = `
 ${JSON.stringify(json2, null, 2)}
`; } else { - console.log(json2); - sid = json2.SID; - console.log(sid); - asid = json2.Acc; - phoneNumber = json2.pnumber; - numStatus = json2.status; - addReq=json2.addressReq; - if (fresponse.status === 200) { success.style.display = ""; success.innerHTML = `
Number ${JSON.stringify(
@@ -359,15 +408,12 @@ 

)}
Number Status ${JSON.stringify( numStatus, null, - 2 - )}
Address Requirements ${JSON.stringify( - addReq, - null, - 2 - )}

`; + 2, + )}
Address Requirements ${JSON.stringify(addReq, null, 2)} `; } else { console.error(json2.error); warning.style.display = ""; + success.style.display = "none"; warning.innerHTML = `Something Wrong: ${json2.error}`; } } @@ -412,11 +458,16 @@

acc = await response.json(); if (acc.er === 0) { + status_acc.style.display = ""; + warning.style.display="none"; + warning.innerHTML=""; status_acc.innerHTML = `Wrong password.`; return; } status_acc.style.display = "none"; + warning.style.display="none"; + warning.innerHTML=""; masking.innerHTML = acc.acc; status_acc.innerHTML = ``; } diff --git a/transfers/functions/fetch_sid.js b/transfers/functions/fetch_sid.js index cf6e8b4e..b4c09d78 100644 --- a/transfers/functions/fetch_sid.js +++ b/transfers/functions/fetch_sid.js @@ -1,78 +1,51 @@ exports.handler = async function (context, event, callback) { - let phoneNumber = event.phone; let finalData = null; - let allAccounts = []; - let i = 0; - let numAccounts = 1; - let subA = null; - let subClient = null; let sSid = null; let pNum = null; let numStatus = null; - let addReq=null; - let sub = null; - let subResp = null; + let addReq = null; const accountSid = process.env.ACCOUNT_SID; const authToken = process.env.AUTH_TOKEN; - const client = require("twilio")(accountSid, authToken); - const password = event.pass; + const client = require("twilio")(accountSid, authToken, { + accountSid: event.sub, + }); + const response = new Twilio.Response(); response.appendHeader("Content-Type", "application/json"); - if (password != process.env.Password) { + if (event.pass !== process.env.Password) { finalData = { er: 0 }; return callback(null, finalData); } try { - sub = await client.api.v2010.accounts - .list() - .then((accounts) => accounts.forEach((a) => (allAccounts[i++] = a.sid))); - - numAccounts = allAccounts.length; - try { - for (let j = 0; j < numAccounts; j++) { - subA = allAccounts[j].replace(/\"/g, ""); - - subClient = require("twilio")(accountSid, authToken, { - accountSid: subA, - }); - - subResp = await subClient.incomingPhoneNumbers - .list({ phoneNumber: phoneNumber }) - .then((incomingPhoneNumbers) => - incomingPhoneNumbers.forEach((k) => { - pNum = k.phoneNumber; - sSid = k.sid; - numStatus = k.status; - addReq = k.addressRequirements; - }), - ); - - finalData = { - Acc: subA, - pnumber: pNum, - SID: sSid, - status: numStatus, - addressReq: addReq, - }; - if (pNum != null) { - response.setStatusCode(200); - response.setBody(sSid); - return callback(null, finalData); - } - } - + await client.incomingPhoneNumbers + .list({ phoneNumber: event.phone }) + .then((incomingPhoneNumbers) => + incomingPhoneNumbers.forEach((k) => { + pNum = k.phoneNumber; + sSid = k.sid; + numStatus = k.status; + addReq = k.addressRequirements; + }), + ); + + if (pNum !== null) { + finalData = { + Acc: event.sub, + pnumber: pNum, + SID: sSid, + status: numStatus, + addressReq: addReq, + }; response.setStatusCode(200); - response.setBody("number not found"); - return callback(null, response); - } catch (error) { - console.error(error.message); - response.setStatusCode(error.status || 400); - response.setBody({ error: error.message }); + response.setBody(finalData); return callback(null, response); } + response.setStatusCode(200); + response.setBody("not here"); + return callback(null, response); } catch (error) { console.error(error.message); response.setStatusCode(error.status || 400); diff --git a/transfers/functions/get_account.js b/transfers/functions/get_account.js index 23fcf943..d2119640 100644 --- a/transfers/functions/get_account.js +++ b/transfers/functions/get_account.js @@ -3,7 +3,7 @@ exports.handler = async function (context, event, callback) { const accountSid = { acc: process.env.ACCOUNT_SID }; const password = event.pass; - if (password != process.env.Password) { + if (password !== process.env.Password) { finalData = { er: 0 }; return callback(null, finalData); } diff --git a/transfers/functions/list_accounts.js b/transfers/functions/list_accounts.js new file mode 100644 index 00000000..17dbaac5 --- /dev/null +++ b/transfers/functions/list_accounts.js @@ -0,0 +1,35 @@ +exports.handler = async function (context, event, callback) { + let finalData = null; + let i = 0; + + const accountSid = process.env.ACCOUNT_SID; + const authToken = process.env.AUTH_TOKEN; + const client = require("twilio")(accountSid, authToken); + const response = new Twilio.Response(); + const allAccounts = []; + + response.appendHeader("Content-Type", "application/json"); + + if (event.pass !== process.env.Password) { + finalData = { er: 0 }; + return callback(null, finalData); + } + + try { + sub = await client.api.v2010.accounts.list().then((accounts) => + accounts.forEach((a) => { + allAccounts[i] = a.sid; + i = i + 1; + }), + ); + + response.setStatusCode(200); + response.setBody(allAccounts); + return callback(null, response); + } catch (error) { + console.error(error.message); + response.setStatusCode(error.status || 400); + response.setBody({ error: error.message }); + return callback(null, response); + } +}; diff --git a/transfers/functions/transfer.js b/transfers/functions/transfer.js index 658b795d..15798052 100644 --- a/transfers/functions/transfer.js +++ b/transfers/functions/transfer.js @@ -6,7 +6,7 @@ exports.handler = async function (context, event, callback) { const response = new Twilio.Response(); response.appendHeader("Content-Type", "application/json"); - if (event.pass != process.env.Password) { + if (event.pass !== process.env.Password) { finalData = { er: 0 }; return callback(null, finalData); } @@ -17,12 +17,12 @@ exports.handler = async function (context, event, callback) { }); resp = await client - .incomingPhoneNumbers(event.phone) //number SID + .incomingPhoneNumbers(event.phone) // number SID .update({ accountSid: event.gaccount, bundleSid: event.bSID, addressSid: event.aSID, - }); //account sid where the number should be transferred to + }); // account sid where the number should be transferred to response.setStatusCode(200); response.setBody(resp);