-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·69 lines (60 loc) · 2 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// server.js
// set up ======================================================================
// get all the tools we need
var express = require('express');
var schedule = require('node-schedule');
var port = process.env.PORT || 8000;
var app = express();
var request = require('request');
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '@gmail.com',
pass: ''
}
});
// setup e-mail data with unicode symbols
var mailOptions = {
from: 'Text ✔ <@gmail.com>', // sender address
to: '@gmail.com', // list of receivers
subject: 'Your IP ✔', // Subject line
text: '', // plaintext body
html: '' // html body
};
var old_ip = null;
app.set('port', process.env.PORT || 3000);
// launch ======================================================================
app.listen(app.get('port'), function(){
var options = {
url: 'http://ifconfig.me/ip',
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'
}
};
function sendEmail(text) {
mailOptions.text = text;
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
body = body.trim();
if(old_ip == null) {
old_ip = body;
sendEmail(old_ip);
}
else if(old_ip != body) { console.log('old ip: ' + old_ip + ' new ip: ' + body); old_ip = body; sendEmail(old_ip);}
else if(old_ip == body) { console.log('old ip: ' + old_ip + ' new ip: ' + body); }
}
}
var j = schedule.scheduleJob('0 * * * * *', function(){
console.log(new Date());
request(options, callback);
});
});