Skip to content

Commit

Permalink
Apparently I never ran prettier on this
Browse files Browse the repository at this point in the history
  • Loading branch information
bendem committed Apr 13, 2020
1 parent a7ff2e0 commit c61d10c
Showing 1 changed file with 37 additions and 38 deletions.
75 changes: 37 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
addEventListener('fetch', event => {
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request))
})

const API = 'https://api.cloudflare.com/client/v4';
const API = 'https://api.cloudflare.com/client/v4'

function response(obj, status = 200) {
return new Response(JSON.stringify(obj), {
headers: {
'content-type': 'application/json'
'content-type': 'application/json',
},
status,
})
Expand All @@ -17,42 +17,39 @@ async function dnsRecords(url, method, body = null) {
return await fetch(`${API}/zones/${CF_ZONE}/${url}`, {
method,
headers: {
'authorization': 'Bearer ' + CF_TOKEN,
authorization: 'Bearer ' + CF_TOKEN,
'content-type': 'application/json',
},
body: body === null ? null : JSON.stringify(body),
});
})
}

async function handleUpdate(type, ip, existingRecord) {
let cfResponse = null;
let cfResponse = null
if (ip && existingRecord) {
if (existingRecord.content === ip) {
return;
return
}

// UPDATE
console.log(`updating record from ${existingRecord.content} to ${ip}`);
console.log(`updating record from ${existingRecord.content} to ${ip}`)
cfResponse = await dnsRecords(`dns_records/${existingRecord.id}`, 'PATCH', {
content: ip,
});

})
} else if (ip && !existingRecord) {
// CREATE
console.log(`creating record with ${ip}`);
console.log(`creating record with ${ip}`)
cfResponse = await dnsRecords(`dns_records`, 'POST', {
type,
name: DOMAIN,
content: ip,
ttl: 1,
proxied: true,
});

})
} else if (!ip && existingRecord) {
// DELETE
console.log(`deleting record`);
cfResponse = await dnsRecords(`dns_records/${existingRecord.id}`, 'DELETE');

console.log(`deleting record`)
cfResponse = await dnsRecords(`dns_records/${existingRecord.id}`, 'DELETE')
} else {
console.log('doing nothing', ip, existingRecord, DOMAIN)
}
Expand All @@ -61,25 +58,28 @@ async function handleUpdate(type, ip, existingRecord) {
return
}

const cfResponseObject = await cfResponse.json();
console.log(cfResponseObject);
const cfResponseObject = await cfResponse.json()
console.log(cfResponseObject)
if (cfResponseObject.success !== true) {
return cfResponseObject.errors;
return cfResponseObject.errors
}
}

async function handleRequest(request) {
const requestUrl = new URL(request.url);
const requestUrl = new URL(request.url)
if (requestUrl.pathname === '/current-ip') {
return response({
ip: request.headers.get('cf-connecting-ip')
ip: request.headers.get('cf-connecting-ip'),
})
}

if (requestUrl.pathname !== '/dyndns') {
return response({
success: false
}, 404)
return response(
{
success: false,
},
404,
)
}

const params = requestUrl.searchParams
Expand All @@ -89,39 +89,39 @@ async function handleRequest(request) {
const password = params.get('password')

if (password !== PASSWORD) {
return response({ success: false, errors: ['invalid password'] }, 401);
return response({ success: false, errors: ['invalid password'] }, 401)
}

const cfResponse = await fetch(`${API}/zones/${CF_ZONE}/dns_records`, {
headers: {
'authorization': 'Bearer ' + CF_TOKEN
authorization: 'Bearer ' + CF_TOKEN,
},
});
})

const cfResponseObject = await cfResponse.json();
const cfResponseObject = await cfResponse.json()

if (cfResponseObject.success !== true) {
return response({ success: false, errors: cfResponseObject.errors });
return response({ success: false, errors: cfResponseObject.errors })
}

let existing4Record = null;
let existing6Record = null;
let existing4Record = null
let existing6Record = null
for (const record of cfResponseObject.result) {
if (record.name !== DOMAIN) {
continue;
continue
}

if (record.type === 'A') {
existing4Record = record;
existing4Record = record
} else if (record.type === 'AAAA') {
existing6Record = record;
existing6Record = record
}
}

console.log(cfResponseObject);
console.log(cfResponseObject)

const v4Errors = await handleUpdate('A', ipv4, existing4Record);
const v6Errors = await handleUpdate('AAAA', ipv6, existing6Record);
const v4Errors = await handleUpdate('A', ipv4, existing4Record)
const v6Errors = await handleUpdate('AAAA', ipv6, existing6Record)

const errors = {}
let success = true
Expand All @@ -134,6 +134,5 @@ async function handleRequest(request) {
errors.ipv6 = v6Errors
}


return response({ success, errors })
}

0 comments on commit c61d10c

Please sign in to comment.