From d82bceb8b64e9d9648b6ea9c54a92e5f6a3c675b Mon Sep 17 00:00:00 2001 From: William Harrison Date: Fri, 4 Nov 2022 16:46:39 +0800 Subject: [PATCH 1/4] add invalid domain check + cleanup --- scripts/addRecords.js | 33 ++++++++++++++++-------------- utils/checkInfo.js | 20 +++++++++++------- utils/checkRecords.js | 16 +++++++++------ utils/getJSON.js | 47 ++++++++++++++++++++++++++++--------------- utils/utils.js | 13 ++++++------ 5 files changed, 78 insertions(+), 51 deletions(-) diff --git a/scripts/addRecords.js b/scripts/addRecords.js index c2c285d..0a8b10f 100644 --- a/scripts/addRecords.js +++ b/scripts/addRecords.js @@ -1,27 +1,30 @@ -const fetch = require('node-fetch'); -const getJSON = require('../utils/getJSON.js'); +const fetch = require("node-fetch"); +const getJSON = require("../utils/getJSON.js"); const data = getJSON(process.env.FILES); async function addRecord(type, name, value) { - const response = await fetch(`https://api.is-a-good.dev/api/is-a-good-dev/zones/add?apiKey=${process.env.API_KEY}&type=${type}&name=${name}&content=${value}`); const data = await response.json(); - return data + + return data; } (async () => { try { - if (!data) return - const records = Object.keys(data.target) - for (const i in records) { - recordType = records[i] - name = data.target[recordType].name; - value = data.target[recordType].value; - const result = await addRecord(recordType, name, value); - console.log(result); - } + if(!data) return; + + const records = Object.keys(data.target); + + for(const i in records) { + recordType = records[i]; + name = data.target[recordType].name; + value = data.target[recordType].value; + + const res = await addRecord(recordType, name, value); + console.log(res); + } } catch (e) { - console.log("Error adding records") - console.log(e) + console.log("Failed to add records."); + console.log(e); } })(); diff --git a/utils/checkInfo.js b/utils/checkInfo.js index 4548923..2d03691 100644 --- a/utils/checkInfo.js +++ b/utils/checkInfo.js @@ -1,16 +1,22 @@ -const fetch = require('node-fetch'); -const core = require('@actions/core'); +const fetch = require("node-fetch"); +const core = require("@actions/core"); async function checkEmail(email) { - console.log(`Checking: ${email}`) + console.log(`Checking: ${email}`); + const url = `https://api.is-a-good.dev/email-check?key=6lPyUV2dX8&email=${encodeURIComponent(email)}`; + const options = { - method: 'GET' - }; + method: "GET" + } + const res = await fetch(url, options).then(res => res.json()); console.log(res); - core.setOutput('infoReason', res.reason); - if (res.data.valid == true) return true; + + core.setOutput("infoReason", res.reason); + + if(res.data.valid) return true; + return false; } diff --git a/utils/checkRecords.js b/utils/checkRecords.js index 4145130..61d8ed1 100644 --- a/utils/checkRecords.js +++ b/utils/checkRecords.js @@ -1,12 +1,16 @@ -const { checkIfValidIP, checkIfValidFQDN } = require('./utils.js'); +const { checkIfValidIP, checkIfValidFQDN } = require("./utils.js"); + function checkRecords(data) { - const recordType = Object.keys(data.target)[0] - if (recordType.toLowerCase() === "a") { - return checkIfValidIP(data.target[recordType].value) + const recordType = Object.keys(data.target)[0]; + + if(recordType.toLowerCase() === "a") { + return checkIfValidIP(data.target[recordType].value); } - if (recordType.toLowerCase() === "cname") { - return checkIfValidFQDN(data.target[recordType].value) + + if(recordType.toLowerCase() === "cname") { + return checkIfValidFQDN(data.target[recordType].value); } return false; } + module.exports = checkRecords; diff --git a/utils/getJSON.js b/utils/getJSON.js index f4ef63b..bb27126 100644 --- a/utils/getJSON.js +++ b/utils/getJSON.js @@ -1,23 +1,38 @@ -const fs = require('fs'); +const fs = require("fs"); + function getFileExtension(filename) { return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined; } -function getJSON(file) { - const path = `${process.env.actions_path}/${file}`; //json file path. - const ext = getFileExtension(file) - if (!ext) return false; //if no file extension, return. - if (ext != "json") return false; //if file extension is not ".json" return. + +const invalidDomains = [ + "support" +] + +function getJSON(file, filename) { + const path = `${process.env.actions_path}/${file}`; // File path. + const ext = getFileExtension(file); + + if(!ext) return false; // If no file extension, return. + if(ext != "json") return false; // If file extension is not ".json" return. + + invalidDomains.forEach(domain => { + if(filename === domain) return false; + }) + try { - if (fs.existsSync(path)) { //check if file exists in domain directory - //it exists - const rawdata = fs.readFileSync(path); //read the file - const data = JSON.parse(rawdata); //parse it - return data; //return true or false, depending if tests pass or fail. - }; - return false; //it doesn't exist + if(fs.existsSync(path)) { // Check if file exists in domain directory + // It exists + const rawdata = fs.readFileSync(path); // Read the file + const data = JSON.parse(rawdata); // Parse it + return data; // Return true or false, depending if tests pass or fail. + } + + return false; // It doesn't exist } catch(err) { console.error(err); - }; + } + return false; -}; -module.exports = getJSON; +} + +module.exports = getJSON; \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js index c4b192e..d8c2741 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -1,14 +1,13 @@ module.exports.checkIfValidIP = function(str) { - // Regular expression to check if string is a IP address - const regexExp = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gi + // Check if the IP address is valid + const regexExp = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gi; return regexExp.test(str); -}; +} module.exports.checkIfValidFQDN = function(str) { - // Regular expression to check if string is a FQDN - const regexExp = /(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)/gi + // Check if the FQDN is valid + const regexExp = /(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)/gi; return regexExp.test(str); -}; - +} \ No newline at end of file From ca8d3f6db8fa19a88b1859b3c5a77db2bc1dc42f Mon Sep 17 00:00:00 2001 From: William Harrison Date: Fri, 4 Nov 2022 16:49:39 +0800 Subject: [PATCH 2/4] update invalid domains --- utils/getJSON.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/utils/getJSON.js b/utils/getJSON.js index bb27126..14909ea 100644 --- a/utils/getJSON.js +++ b/utils/getJSON.js @@ -5,7 +5,22 @@ function getFileExtension(filename) { } const invalidDomains = [ - "support" + "_acme-challenge", + "_github-challenge-is-a-good-dev", + "_github-pages-challenge-is-a-good-dev", + "help", + "no-reply", + "noreply", + "notification", + "notifications", + "support", + "ww", + "ww1", + "ww2", + "ww3", + "ww4", + "wwww", + "your-domain-name" ] function getJSON(file, filename) { @@ -35,4 +50,4 @@ function getJSON(file, filename) { return false; } -module.exports = getJSON; \ No newline at end of file +module.exports = getJSON; From 83a017a6142c4db141cf5e889b6ed865c43cb2f9 Mon Sep 17 00:00:00 2001 From: William Harrison Date: Fri, 4 Nov 2022 16:51:07 +0800 Subject: [PATCH 3/4] move invalid domains to seperate file --- utils/getJSON.js | 20 +------------------- utils/invalid-domains.json | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 19 deletions(-) create mode 100644 utils/invalid-domains.json diff --git a/utils/getJSON.js b/utils/getJSON.js index 14909ea..c6de376 100644 --- a/utils/getJSON.js +++ b/utils/getJSON.js @@ -1,28 +1,10 @@ const fs = require("fs"); +const invalidDomains = require("./invalid-domains.json"); function getFileExtension(filename) { return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined; } -const invalidDomains = [ - "_acme-challenge", - "_github-challenge-is-a-good-dev", - "_github-pages-challenge-is-a-good-dev", - "help", - "no-reply", - "noreply", - "notification", - "notifications", - "support", - "ww", - "ww1", - "ww2", - "ww3", - "ww4", - "wwww", - "your-domain-name" -] - function getJSON(file, filename) { const path = `${process.env.actions_path}/${file}`; // File path. const ext = getFileExtension(file); diff --git a/utils/invalid-domains.json b/utils/invalid-domains.json new file mode 100644 index 0000000..2bc21a6 --- /dev/null +++ b/utils/invalid-domains.json @@ -0,0 +1,18 @@ +[ + "_acme-challenge", + "_github-challenge-is-a-good-dev", + "_github-pages-challenge-is-a-good-dev", + "help", + "no-reply", + "noreply", + "notification", + "notifications", + "support", + "ww", + "ww1", + "ww2", + "ww3", + "ww4", + "wwww", + "your-domain-name" +] \ No newline at end of file From ce91b937454c0f04030b292dd5c16f88f7e5ccaf Mon Sep 17 00:00:00 2001 From: William Harrison Date: Sat, 5 Nov 2022 11:14:06 +0800 Subject: [PATCH 4/4] fix formatting --- scripts/addRecords.js | 12 ++++++------ utils/checkInfo.js | 10 +++++----- utils/checkRecords.js | 7 ++++--- utils/getJSON.js | 12 ++++++------ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/scripts/addRecords.js b/scripts/addRecords.js index 0a8b10f..4a3aa6c 100644 --- a/scripts/addRecords.js +++ b/scripts/addRecords.js @@ -1,5 +1,5 @@ -const fetch = require("node-fetch"); -const getJSON = require("../utils/getJSON.js"); +const fetch = require('node-fetch'); +const getJSON = require('../utils/getJSON.js'); const data = getJSON(process.env.FILES); async function addRecord(type, name, value) { @@ -11,11 +11,11 @@ async function addRecord(type, name, value) { (async () => { try { - if(!data) return; + if (!data) return; const records = Object.keys(data.target); - for(const i in records) { + for (const i in records) { recordType = records[i]; name = data.target[recordType].name; value = data.target[recordType].value; @@ -23,8 +23,8 @@ async function addRecord(type, name, value) { const res = await addRecord(recordType, name, value); console.log(res); } - } catch (e) { - console.log("Failed to add records."); + } catch(e) { + console.log('Failed to add records.'); console.log(e); } })(); diff --git a/utils/checkInfo.js b/utils/checkInfo.js index 2d03691..d2fd215 100644 --- a/utils/checkInfo.js +++ b/utils/checkInfo.js @@ -1,5 +1,5 @@ -const fetch = require("node-fetch"); -const core = require("@actions/core"); +const fetch = require('node-fetch'); +const core = require('@actions/core'); async function checkEmail(email) { console.log(`Checking: ${email}`); @@ -7,15 +7,15 @@ async function checkEmail(email) { const url = `https://api.is-a-good.dev/email-check?key=6lPyUV2dX8&email=${encodeURIComponent(email)}`; const options = { - method: "GET" + method: 'GET' } const res = await fetch(url, options).then(res => res.json()); console.log(res); - core.setOutput("infoReason", res.reason); + core.setOutput('infoReason', res.reason); - if(res.data.valid) return true; + if (res.data.valid) return true; return false; } diff --git a/utils/checkRecords.js b/utils/checkRecords.js index 61d8ed1..ace3dbe 100644 --- a/utils/checkRecords.js +++ b/utils/checkRecords.js @@ -1,15 +1,16 @@ -const { checkIfValidIP, checkIfValidFQDN } = require("./utils.js"); +const { checkIfValidIP, checkIfValidFQDN } = require('./utils.js'); function checkRecords(data) { const recordType = Object.keys(data.target)[0]; - if(recordType.toLowerCase() === "a") { + if (recordType.toLowerCase() === 'a') { return checkIfValidIP(data.target[recordType].value); } - if(recordType.toLowerCase() === "cname") { + if (recordType.toLowerCase() === 'cname') { return checkIfValidFQDN(data.target[recordType].value); } + return false; } diff --git a/utils/getJSON.js b/utils/getJSON.js index c6de376..c096a67 100644 --- a/utils/getJSON.js +++ b/utils/getJSON.js @@ -1,5 +1,5 @@ -const fs = require("fs"); -const invalidDomains = require("./invalid-domains.json"); +const fs = require('fs'); +const invalidDomains = require('./invalid-domains.json'); function getFileExtension(filename) { return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined; @@ -9,15 +9,15 @@ function getJSON(file, filename) { const path = `${process.env.actions_path}/${file}`; // File path. const ext = getFileExtension(file); - if(!ext) return false; // If no file extension, return. - if(ext != "json") return false; // If file extension is not ".json" return. + if (!ext) return false; // If no file extension, return. + if (ext != 'json') return false; // If file extension is not '.json' return. invalidDomains.forEach(domain => { - if(filename === domain) return false; + if (filename === domain) return false; }) try { - if(fs.existsSync(path)) { // Check if file exists in domain directory + if (fs.existsSync(path)) { // Check if file exists in domain directory // It exists const rawdata = fs.readFileSync(path); // Read the file const data = JSON.parse(rawdata); // Parse it