initial commit
Signed-off-by: Phillip Nicholas Thern <pt@ftt.gmbh>
This commit is contained in:
parent
d3113ce4e4
commit
7a33c9197f
|
@ -0,0 +1,96 @@
|
|||
const http = require("http")
|
||||
const { request } = require("https")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const app = (req, res) => {
|
||||
|
||||
const [email, key] = Buffer.from(req.headers.authorization.replace(/[Bb]asic /, ""), "base64").toString().split(":")
|
||||
const CFHeaders = { "X-Auth-Key": key, "X-Auth-Email": email, "Content-Type": "application/json" }
|
||||
const url = new URL(req.url, `http://${req.headers.host}`)
|
||||
|
||||
//console.log(url.searchParams.d)
|
||||
|
||||
const CFReq = request(`https://api.cloudflare.com/client/v4/zones?name=${url.searchParams.get("d").match(/\w+\.\w+\.?$/)[0]}`, { method: "GET", headers: CFHeaders }, (response) => {
|
||||
const data = [];
|
||||
|
||||
response.on("data", (d) => {
|
||||
data.push(d)
|
||||
})
|
||||
|
||||
response.on("end", () => {
|
||||
const { success, result } = JSON.parse(data.map((Buf) => Buffer.from(Buf).toString()).join(""))
|
||||
|
||||
data.splice(0, data.length);
|
||||
|
||||
if (success) {
|
||||
const ZCReq = request(`https://api.cloudflare.com/client/v4/zones/${result[0].id}/dns_records?proxied=false&name=${url.searchParams.get("d")}&match=all`, { method: "GET", headers: CFHeaders }, (response) => {
|
||||
response.on("data", (d) => {
|
||||
data.push(d)
|
||||
})
|
||||
response.on("end", () => {
|
||||
const ZCReqRes = JSON.parse(data.map((Buf) => Buffer.from(Buf).toString()).join(""))
|
||||
if (ZCReqRes.success) {
|
||||
if (ZCReqRes.result.length == 0 || !ZCReqRes.result.find(({ type }) => type == "A"||type=="CNAME")) {
|
||||
const createCFEntryReq = request(`https://api.cloudflare.com/client/v4/zones/${result[0].id}/dns_records`, { method: "POST", headers: CFHeaders }, (response) => {
|
||||
data.splice(0, data.length);
|
||||
|
||||
response.on("data", (d) => {
|
||||
data.push(d)
|
||||
})
|
||||
response.on("end", () => {
|
||||
const createCFEntryReqRes = JSON.parse(data.map((Buf) => Buffer.from(Buf).toString()).join(""))
|
||||
if (!createCFEntryReqRes.success) {
|
||||
res.writeHead(500, "An Error occurred while creating an A record")
|
||||
}
|
||||
res.end()
|
||||
|
||||
})
|
||||
})
|
||||
createCFEntryReq.write(JSON.stringify({ type: "A", name: url.searchParams.get("d"), content: url.searchParams.get("ip"), ttl: 60 }))
|
||||
createCFEntryReq.end()
|
||||
} else {
|
||||
const updateCFEntryReq = request(`https://api.cloudflare.com/client/v4/zones/${result[0].id}/dns_records/${ZCReqRes.result.find(({ type }) => type == "A").id}`, { method: "PATCH", headers: CFHeaders }, (response) => {
|
||||
data.splice(0, data.length);
|
||||
|
||||
response.on("data", (d) => {
|
||||
data.push(d)
|
||||
})
|
||||
response.on("end", () => {
|
||||
const updateCFEntryReqRes = JSON.parse(data.map((Buf) => Buffer.from(Buf).toString()).join(""))
|
||||
if (!updateCFEntryReqRes.success) {
|
||||
res.writeHead(500, "An Error occurred while patching an A record")
|
||||
}
|
||||
res.end()
|
||||
|
||||
})
|
||||
})
|
||||
updateCFEntryReq.write(JSON.stringify({ content: url.searchParams.get("ip"), ttl: 60, type:"A" }))
|
||||
updateCFEntryReq.end()
|
||||
}
|
||||
} else {
|
||||
res.writeHead(500, "An Error occurred while querying CloudFlare Zone Records")
|
||||
res.end()
|
||||
}
|
||||
})
|
||||
})
|
||||
ZCReq.end()
|
||||
} else {
|
||||
console.log(data.map((Buf) => Buffer.from(Buf).toString()).join(""))
|
||||
res.writeHead(500, "An Error occurred while querying the Zone List")
|
||||
res.end()
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
CFReq.end()
|
||||
|
||||
}
|
||||
|
||||
const server = http.createServer(app)
|
||||
|
||||
server.listen(5555)
|
Loading…
Reference in New Issue