Skip to content

Commit

Permalink
Merge pull request #32 from lgbt-sh/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
cvyl authored Nov 26, 2023
2 parents 23896cc + 1553376 commit ea83d24
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/register.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Register
description: Register a Subdomain
description: Register a Subdomain (do not edit the title!)
title: "Register"
labels: []
projects: []
Expand Down
103 changes: 46 additions & 57 deletions edit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
const array = process.env.EVENT_ISSUE_BODY.split("### ")
const flare = require("cloudflare")

/**
* REGEX Values for all necessary inputs.
*/
const nonascii = /[^\u0000-\u007F]+/
const DNS_ZONE_ID = "6f122d28e700b9f3ec930007e1ccb1b1";
const TTL = 60;
const subdomain = array[1][1]
const cf = flare({
token: process.env.CF_TOKEN,
})


// dark magic starts here, dont ask.
array.forEach((item, index) => {
array[index] = item.split("\n\n")
})

const original = array[3][1]
array[3][1] = original.split("\n")[0]
array[3].push(original.split("\n")[1])
// End of the dark magic.

if (
!(
array.length == 4 &&
Expand All @@ -19,68 +36,40 @@ if (
array[3][0] == "Agreement" &&
array[3][1] == "- [X] I have ensured that this subdomain is mine" &&
array[3][2] == undefined &&
!array[1][1].includes(" ") &&
!subdomain.includes(" ") &&
!array[2][1].includes(" ") &&
!nonascii.test(array[1][1]) &&
!nonascii.test(subdomain) &&
!nonascii.test(array[2][1]) &&
array[1][1] != ".lgbt.sh" &&
array[1][1].endsWith(".lgbt.sh")
)
) {
return console.log(
"not planned|Format invalid! It's usually because you didn't check the agreements, or the domain/record you entered is invalid!|"+array[1][1]
)
return logErrorAndExit("Format invalid! It's usually because you didn't check the agreements, or the domain/record you entered is invalid!", subdomain)
}
var flare = require("cloudflare")
var cf = flare({
token: process.env.CF_TOKEN,
})
const ipv4 =
/^\s*((25[0-5]|2[0-4][0-9]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A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/gm
const hostname =
/^\s*((?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?)*\.?)\s*$/gm
cf.dnsRecords.browse("6f122d28e700b9f3ec930007e1ccb1b1").then((records) => {
const availabilityFilter = records.result.filter((record) => {
return (
record.name == array[1][1] &&
record.comment == process.env.EVENT_USER_LOGIN
)
})
if (availabilityFilter[0]) {
var type = "invalid"
if (hostname.test(array[2][1])) type = "CNAME"
if (ipv4.test(array[2][1])) type = "A"
if (ipv6.test(array[2][1])) type = "AAAA"
if (type == "hostname" && !array[1][1].includes(".")) type = "invalid"
if (type == "invalid") {
return console.log(
"not planned|The record destination you entered is invalid!|"+array[1][1]
)



cf.dnsRecords.browse(DNS_ZONE_ID)
.then((dnsRecords) => {
const dnsAvailable = dnsRecords.result.filter((foundDNS) => { return (foundDNS.name == subdomain && foundDNS.comment == process.env.EVENT_USER_LOGIN) })

if (!dnsAvailable[0])
logErrorAndExit(`This subdomain is not yours or the subdomain is not found!`, subdomain)

const recordType = getValidRecordType(array);

return cf.dnsRecords.edit(DNS_ZONE_ID, availabilityFilter[0].id, {
content: array[2][1],
name: subdomain,
proxied: false,
type: recordType,
ttl: TTL,
comment: process.env.EVENT_USER_LOGIN,
});
}).then((cloudFlareResponse) => {
if (!cloudFlareResponse.success) {
return logErrorAndExit(`CloudFlare Error: ${cloudFlareResponse.errors[0].message}`, subdomain);
}
cf.dnsRecords
.edit("6f122d28e700b9f3ec930007e1ccb1b1", availabilityFilter[0].id, {
content: array[2][1],
name: array[1][1],
proxied: false,
type,
ttl: 60,
comment: process.env.EVENT_USER_LOGIN,
})
.then((response) => {
if (!response.success) {
return console.log(
`not planned|CloudFlare Error:${response.errors[0].message}|${array[1][1]}`
)
}
return console.log(
"completed|Your subdomain has been successfully edited!|"+array[1][1]
)
})
} else {
return console.log(
"not planned|This subdomain is not yours or the subdomain is not found!|"+array[1][1]
)
}
})

logSuccess(subdomain);
}).catch((error) => console.error("An error occurred:", error));
51 changes: 51 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* REGEX VALUES
* */
const ipv4 =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/gm
const ipv6 =
/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/gm
const hostname =
/^\s*(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,63}\s*$/gm


// Encapsulated methods for testing the REGEX, improves readability.
function isValidCNAME(value) {
return hostname.test(value);
}

function isValidIPv4(value) {
return ipv4.test(value);
}

function isValidIPv6(value) {
return ipv6.test(value);
}

function getValidRecordType(array) {
// Check for valid record types
switch (true) {
// Check for CNAME
case isValidCNAME(array[2][1]):
return "CNAME";

// Check for IPv4
case isValidIPv4(array[2][1]):
return "A";

// Check for IPv6
case isValidIPv6(array[2][1]):
return "AAAA";

default:
return "invalid";
}
}

function logErrorAndExit(message, subdomain) {
console.log(`not planned|${message}|${subdomain}`);
}

function logSuccess(subdomain) {
console.log(`completed|Your subdomain has been successfully edited!|${subdomain}`);
}

0 comments on commit ea83d24

Please sign in to comment.