Cloudflare
· 2 min read
Hey.
I was revisiting a few scripts, this is what I found.
IP update
The ISP is changing the ip now and then.
So I keep sending myself and email with my ip from my parent's house.
It's running on a cron like this.
* * * * * beside /home/beside/mail >> /home/beside/cron.log 2>&1
#!/bin/bash
/home/beside/node-v14.18.0-linux-x64/bin/node /home/beside/code/mail/index.js
Mailing IP if necessary
async function main() {
const oldIP = fs.readFileSync(__dirname+"/ip.txt", 'utf-8')
const command = 'VARIABLE=$(curl -s https://ipinfo.io/ip) && echo $VARIABLE'
exec(command, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
if (oldIP == stdout) {
console.log("same")
return
}
fs.writeFileSync(__dirname+"/ip.txt", stdout)
console.log(`changed to: ${stdout}`);
return transporter.sendMail({
from: '"IP Daemon" <daniel.622.guerrero@gmail.com>', // sender address
to: "daniel.622.guerrero@gmail.com", // list of receivers
subject: "New IP" , // Subject line
text: `This one: ${stdout}`, // plain text body
}).then((info) => {
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
});
});
Commands on CLI
IP check
const response = await fetch('https://ipinfo.io/ip')
const ip = await response.text()
this.log(ip)
Cloudflare DNS update
After trying with ddclient and failing miserably I used the cloudflare api endpoints
const updateResponse = await fetch(updateURL, {
method: 'PUT',
headers: [['Content-Type', 'application/json'], ['Authorization', token]],
body: JSON.stringify({
type: 'A',
name: flags.name,
content: flags.ip,
ttl: 3600,
proxied: true,
}),
})
So after running this
daemon ip cloudflare --name journal -i '190.51.23.246'
I get this

