1- addEventListener ( 'fetch' , event => {
1+ addEventListener ( 'fetch' , ( event ) => {
22 event . respondWith ( handleRequest ( event . request ) )
33} )
44
5- const API = 'https://api.cloudflare.com/client/v4' ;
5+ const API = 'https://api.cloudflare.com/client/v4'
66
77function response ( obj , status = 200 ) {
88 return new Response ( JSON . stringify ( obj ) , {
99 headers : {
10- 'content-type' : 'application/json'
10+ 'content-type' : 'application/json' ,
1111 } ,
1212 status,
1313 } )
@@ -17,42 +17,39 @@ async function dnsRecords(url, method, body = null) {
1717 return await fetch ( `${ API } /zones/${ CF_ZONE } /${ url } ` , {
1818 method,
1919 headers : {
20- ' authorization' : 'Bearer ' + CF_TOKEN ,
20+ authorization : 'Bearer ' + CF_TOKEN ,
2121 'content-type' : 'application/json' ,
2222 } ,
2323 body : body === null ? null : JSON . stringify ( body ) ,
24- } ) ;
24+ } )
2525}
2626
2727async function handleUpdate ( type , ip , existingRecord ) {
28- let cfResponse = null ;
28+ let cfResponse = null
2929 if ( ip && existingRecord ) {
3030 if ( existingRecord . content === ip ) {
31- return ;
31+ return
3232 }
3333
3434 // UPDATE
35- console . log ( `updating record from ${ existingRecord . content } to ${ ip } ` ) ;
35+ console . log ( `updating record from ${ existingRecord . content } to ${ ip } ` )
3636 cfResponse = await dnsRecords ( `dns_records/${ existingRecord . id } ` , 'PATCH' , {
3737 content : ip ,
38- } ) ;
39-
38+ } )
4039 } else if ( ip && ! existingRecord ) {
4140 // CREATE
42- console . log ( `creating record with ${ ip } ` ) ;
41+ console . log ( `creating record with ${ ip } ` )
4342 cfResponse = await dnsRecords ( `dns_records` , 'POST' , {
4443 type,
4544 name : DOMAIN ,
4645 content : ip ,
4746 ttl : 1 ,
4847 proxied : true ,
49- } ) ;
50-
48+ } )
5149 } else if ( ! ip && existingRecord ) {
5250 // DELETE
53- console . log ( `deleting record` ) ;
54- cfResponse = await dnsRecords ( `dns_records/${ existingRecord . id } ` , 'DELETE' ) ;
55-
51+ console . log ( `deleting record` )
52+ cfResponse = await dnsRecords ( `dns_records/${ existingRecord . id } ` , 'DELETE' )
5653 } else {
5754 console . log ( 'doing nothing' , ip , existingRecord , DOMAIN )
5855 }
@@ -61,25 +58,28 @@ async function handleUpdate(type, ip, existingRecord) {
6158 return
6259 }
6360
64- const cfResponseObject = await cfResponse . json ( ) ;
65- console . log ( cfResponseObject ) ;
61+ const cfResponseObject = await cfResponse . json ( )
62+ console . log ( cfResponseObject )
6663 if ( cfResponseObject . success !== true ) {
67- return cfResponseObject . errors ;
64+ return cfResponseObject . errors
6865 }
6966}
7067
7168async function handleRequest ( request ) {
72- const requestUrl = new URL ( request . url ) ;
69+ const requestUrl = new URL ( request . url )
7370 if ( requestUrl . pathname === '/current-ip' ) {
7471 return response ( {
75- ip : request . headers . get ( 'cf-connecting-ip' )
72+ ip : request . headers . get ( 'cf-connecting-ip' ) ,
7673 } )
7774 }
7875
7976 if ( requestUrl . pathname !== '/dyndns' ) {
80- return response ( {
81- success : false
82- } , 404 )
77+ return response (
78+ {
79+ success : false ,
80+ } ,
81+ 404 ,
82+ )
8383 }
8484
8585 const params = requestUrl . searchParams
@@ -89,39 +89,39 @@ async function handleRequest(request) {
8989 const password = params . get ( 'password' )
9090
9191 if ( password !== PASSWORD ) {
92- return response ( { success : false , errors : [ 'invalid password' ] } , 401 ) ;
92+ return response ( { success : false , errors : [ 'invalid password' ] } , 401 )
9393 }
9494
9595 const cfResponse = await fetch ( `${ API } /zones/${ CF_ZONE } /dns_records` , {
9696 headers : {
97- ' authorization' : 'Bearer ' + CF_TOKEN
97+ authorization : 'Bearer ' + CF_TOKEN ,
9898 } ,
99- } ) ;
99+ } )
100100
101- const cfResponseObject = await cfResponse . json ( ) ;
101+ const cfResponseObject = await cfResponse . json ( )
102102
103103 if ( cfResponseObject . success !== true ) {
104- return response ( { success : false , errors : cfResponseObject . errors } ) ;
104+ return response ( { success : false , errors : cfResponseObject . errors } )
105105 }
106106
107- let existing4Record = null ;
108- let existing6Record = null ;
107+ let existing4Record = null
108+ let existing6Record = null
109109 for ( const record of cfResponseObject . result ) {
110110 if ( record . name !== DOMAIN ) {
111- continue ;
111+ continue
112112 }
113113
114114 if ( record . type === 'A' ) {
115- existing4Record = record ;
115+ existing4Record = record
116116 } else if ( record . type === 'AAAA' ) {
117- existing6Record = record ;
117+ existing6Record = record
118118 }
119119 }
120120
121- console . log ( cfResponseObject ) ;
121+ console . log ( cfResponseObject )
122122
123- const v4Errors = await handleUpdate ( 'A' , ipv4 , existing4Record ) ;
124- const v6Errors = await handleUpdate ( 'AAAA' , ipv6 , existing6Record ) ;
123+ const v4Errors = await handleUpdate ( 'A' , ipv4 , existing4Record )
124+ const v6Errors = await handleUpdate ( 'AAAA' , ipv6 , existing6Record )
125125
126126 const errors = { }
127127 let success = true
@@ -134,6 +134,5 @@ async function handleRequest(request) {
134134 errors . ipv6 = v6Errors
135135 }
136136
137-
138137 return response ( { success, errors } )
139138}
0 commit comments