Created
November 19, 2019 17:53
-
-
Save textita/ee0460354f8959b0d26e7046147499ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const expressLayouts = require('express-ejs-layouts'); | |
const path = require('path'); | |
const session = require('express-session') | |
const bodyParser = require('body-parser') | |
const cookieParser = require('cookie-parser') | |
const validator = require('validator'); | |
const db = require('./db'); | |
const rangeCheck = require('range_check'); | |
const app = express() | |
const flash = require('connect-flash'); | |
const moment = require('moment'); | |
const mail = require('./mail') | |
const uuidv4 = require('uuid/v4'); | |
const fs = require('fs'); | |
const http = require('http'); | |
const https = require('https'); | |
app.set('views', path.join(__dirname, 'views/pages')); | |
app.set('view engine', 'ejs'); | |
app.set('layout extractScripts', true) | |
app.set('layout extractStyles', true) | |
app.set('trust proxy', 1) // trust first proxy | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
app.use(bodyParser.json()); | |
app.use(expressLayouts); | |
app.use(express.static(__dirname + '/public', { | |
index: false | |
})); | |
app.use('/tutorials', express.static(__dirname + '/public', { | |
index: false | |
})); | |
app.use(cookieParser()); | |
// initialize express-session to allow us track the logged-in user across sessions. | |
app.use(session({ | |
key: 'user_sid', | |
secret: 'somerandonstuffs', | |
resave: false, | |
saveUninitialized: false, | |
cookie: { | |
expires: 600000 | |
} | |
})); | |
app.use(flash()) | |
stripLeadingZeroes = (ip) => { | |
return ip.split('.').map(Number).join('.'); | |
} | |
// Certificate | |
const privateKey = fs.readFileSync('/etc/letsencrypt/live/staticip.io/privkey.pem', 'utf8'); | |
const certificate = fs.readFileSync('/etc/letsencrypt/live/staticip.io/cert.pem', 'utf8'); | |
const ca = fs.readFileSync('/etc/letsencrypt/live/staticip.io/chain.pem', 'utf8'); | |
const credentials = { | |
key: privateKey, | |
cert: certificate, | |
ca: ca | |
}; | |
function ensureSecure(req, res, next) { | |
if (req.secure) { | |
return next(); | |
} | |
res.redirect('https://' + req.hostname + req.url); | |
} | |
app.all('*', ensureSecure); // at top of routing calls | |
app.use((req, res, next) => { | |
res.locals = { | |
user: req.session.user, | |
info: req.flash('info'), | |
success: req.flash('success'), | |
warning: req.flash('warning') | |
}; | |
next(); | |
}); | |
authenticate = (req, res, next) => { | |
if (req.session.user) { | |
return next(); | |
} else | |
return res.redirect('/login') | |
} | |
app.locals.isTrialExpired = (created_on) => { | |
var start = moment(created_on); | |
var now = moment(); | |
return now.diff(start, 'days') > 7; | |
} | |
app.use('/p/*', authenticate) | |
app.get('/sendemail', (req, res) => { | |
var email = req.query.email; | |
var subject = req.query.subject; | |
var body = req.query.body; | |
console.log(email) | |
mail.sendMail(email,subject,body); | |
res.send('ok'); | |
}); | |
app.get('/update', function(req, res) { | |
var token = req.query.token; | |
var domain = req.query.domain + '.staticip.io'; | |
var ip = req.query.ip; | |
db.getDomainbyUid(token, domain, (err, data) => { | |
if (!data) { | |
return res.send('KO') | |
} else { | |
db.updateDomain(data.id, ip, (err, data) => { | |
if (data) { | |
db.updateDomainSOA((err, data) => { | |
return res.send('OK') | |
}); | |
} | |
}) | |
} | |
}) | |
}) | |
app.get('/logout', function(req, res) { | |
req.session.user = null; | |
res.redirect('/'); | |
}); | |
app.get(['/ip-monitor','/ddns-update-client'], function(req, res) { | |
var vm = { | |
page: "download", | |
title: 'DDNS update client', | |
rotateFooter: false, | |
description: 'DDNS update client' | |
}; | |
res.render('ddns-update-client', vm); | |
}); | |
app.get('/tutorials/:folder?/:file?', function(req, res) { | |
folder = req.params.folder ? req.params.folder : ''; | |
file = req.params.file ? req.params.file : ''; | |
res.sendFile(path.join(__dirname, 'public', folder, file, 'index.html')); | |
}); | |
app.post('/subscribe', function(req, res) { | |
var email = req.body.email | |
db.subscribeNewsLetter(email, () => { | |
res.json({ | |
success: true, | |
email: email | |
}) | |
}) | |
}); | |
app.get(['/','/*-static-ip-address'], function(req, res) { | |
cleanUrl = req.url.substring(1).replace(/-/g, " "); | |
country = toTitleCase(cleanUrl.substring(0,req.url.length-18)); | |
function toTitleCase(str) { | |
return str.replace(/\w\S*/g, function(txt){ | |
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); | |
}); | |
} | |
var vm = { | |
page: "home", | |
title: 'Get a fixed static IP Address', | |
heading: req.url != '/' ? 'Need a Static IP Address in ' + country + '?': "Need a Static IP address ?", | |
description : req.url != '/' ? cleanUrl : '', | |
rotateFooter: true | |
}; | |
res.render('index', vm); | |
}); | |
app.get('/contact-us', function(req, res) { | |
var vm = { | |
page: 'contact-us', | |
title: 'Contact Us', | |
rotateFooter: false, | |
name: '', | |
email: '', | |
message: '', | |
errors: [], | |
description:'' | |
}; | |
res.render('contact-us', vm); | |
}); | |
app.get('/confirm-email/:uid', function(req, res) { | |
var uid = req.params.uid; | |
db.getUserByUnverifiedEmail(uid, (err, data) => { | |
if (data) { | |
var email = data.email; | |
if (data) { | |
db.updateUserEmailVerifiedbyUid(uid, (err, data) => { | |
req.flash('success', 'Your email address has been confirmed for %s ', email) | |
res.redirect('/'); | |
}); | |
} | |
} | |
else res.redirect('/'); | |
}) | |
}) | |
app.get('/pricing', function(req, res) { | |
var vm = { | |
page: 'pricing', | |
title: 'Pricing', | |
rotateFooter: false, | |
description: '', | |
}; | |
res.render('pricing', vm); | |
}); | |
app.post('/contact-us', function(req, res) { | |
var name = req.body.name | |
var email = req.body.email | |
var message = req.body.message | |
var vm = { | |
page: 'contact-us', | |
title: 'Contact Us', | |
rotateFooter: false, | |
name: name, | |
email: email, | |
message: message, | |
errors: [] | |
}; | |
if (validator.isEmpty(name)) { | |
vm.errors[0] = 'Please enter your name.'; | |
} | |
if (!validator.isEmail(email)) { | |
vm.errors[1] = 'Please enter a email.'; | |
} | |
if (validator.isEmpty(message)) { | |
vm.errors[2] = 'Please enter a message.'; | |
} | |
if (vm.errors.length > 0) | |
res.render('contact-us', vm); | |
else { | |
req.flash('success', 'Thank you for reaching out. We will get back to you shortly.') | |
const msg = | |
`from: ${name} | |
email: ${email} | |
${message} | |
`; | |
mail.sendMail(email, "contact-us staticip.io", msg) | |
res.redirect("/") | |
} | |
}); | |
app.get('/login', (req, res) => { | |
var vm = { | |
page: 'login', | |
title: 'Log in', | |
email: '', | |
invalidEmail: '', | |
invalidPassword: '', | |
errors: [], | |
description: '', | |
}; | |
res.render('login', vm); | |
}); | |
app.post('/login', function(req, res) { | |
var email = req.body.email | |
var password = req.body.password | |
var vm = { | |
page: 'login', | |
title: 'Log In', | |
email: email, | |
errors: [], | |
description: '' | |
}; | |
if (!validator.isEmail(email)) { | |
vm.errors[0] = 'Please enter a email.'; | |
} | |
if (validator.isEmpty(password)) { | |
vm.errors[1] = 'Please enter a password.'; | |
} | |
if (vm.errors.length > 0) | |
return res.render('login', vm) | |
else { | |
db.getUserByEmailPassword(email, password, (err, data) => { | |
if (data) { | |
if (!data.email_verified) { | |
vm.errors[2] = 'Please verify your email from the email we sent! '; | |
return res.render('login', vm); | |
} else { | |
req.session.user = { | |
user_id: data.user_id, | |
uid: data.uid, | |
email: data.email, | |
trial: data.trial, | |
created_on: data.created_on | |
} | |
return res.redirect("/p/domains") | |
} | |
} else { | |
vm.errors[2] = 'Invalid email or password.'; | |
return res.render('login', vm); | |
} | |
}) | |
} | |
}); | |
app.get('/signup', (req, res) => { | |
var vm= { | |
page: 'signup', | |
title: 'Sign Up', | |
email: '', | |
password: '', | |
confirmPassword: '', | |
errors: [], | |
description: '', | |
}; | |
res.render('signup', vm); | |
}); | |
app.post('/signup', function(req, res) { | |
var email = req.body.email | |
var password = req.body.password | |
var confirmPassword = req.body.confirmPassword | |
var vm= { | |
page: 'signup', | |
title: 'Sign Up', | |
email: email, | |
password: password, | |
confirmPassword: confirmPassword, | |
errors: [], | |
description :'', | |
}; | |
if (!validator.isEmail(email)) { | |
vm.errors[0] = 'Please enter a email.' | |
} | |
if (validator.isEmpty(password)) { | |
vm.errors[1] = 'Please enter a password.' | |
} | |
if (validator.isEmpty(confirmPassword)) { | |
vm.errors[2] = 'Please confirm the password.' | |
} else | |
if (vm.password != vm.confirmPassword) { | |
vm.errors[2] = "Password and confirmation password don't match." | |
} | |
if (vm.errors.length > 0) | |
return res.render('signup', vm) | |
else { | |
db.getUserByEmail(email, (err, data) => { | |
if (data) { | |
vm.errors[3] = email + " account already exists." | |
return res.render('signup', vm) | |
} else { | |
var uid = uuidv4(); | |
const msg = | |
`Welcome to staticip.io | |
Please confirm your email by clicking on this link | |
http://staticip.io/confirm-email/${uid} | |
Thank you.`; | |
db.createUser(email, password, uid, () => { | |
req.flash('info', 'Please check your email and confirm your email address ', email) | |
mail.sendMail(vm.email, "please confirm email for staticip.io", msg) | |
res.redirect("/") | |
}) | |
} | |
}) | |
} | |
}); | |
app.get('/p/domains', function(req, res) { | |
db.getAllDomains(req.session.user.user_id, (err, data) => { | |
var vm= { | |
page: 'domains', | |
title: 'Manage Domains', | |
domains: data, | |
description: '' | |
}; | |
res.render('domains', vm); | |
}); | |
}); | |
app.get('/p/create-domain', function(req, res) { | |
var vm = { | |
page: 'domains', | |
title: 'Create Domain', | |
button_text: 'Save', | |
name: '', | |
ip_address: '', | |
error: '', | |
description: '', | |
}; | |
db.countDomains(req.session.user.user_id, (err, data) => { | |
if (data.total >= 5) { | |
res.redirect("/p/domains") | |
} else res.render('domain', vm); | |
}); | |
}); | |
app.get(['/p/delete-domain/:id'], function(req, res) { | |
var id = req.params.id; | |
db.getDomainById(id, req.session.user.user_id, (err, data) => { | |
if (data) { | |
var vm = { | |
page: 'domains', | |
title: 'Delete Domain', | |
name: data.name, | |
ip_address: data.content, | |
button_text: 'Delete', | |
error: '', | |
description: '' | |
}; | |
res.render('domain', vm); | |
} else | |
res.redirect('/p/domains') | |
}) | |
}); | |
app.post(['/p/create-domain'], function(req, res) { | |
var name = req.body.name | |
var ip_address = req.body.ip_address | |
var vm = { | |
page: 'domains', | |
title: 'Create Domain', | |
name: name, | |
ip_address: ip_address, | |
button_text: 'Save', | |
error: '', | |
description: '' | |
}; | |
db.getDomainbyName(name, (err, data) => { | |
if (data) { | |
req.flash('warning', 'You cant use %s as a domain name as it already exists on staticIP.io ', name) | |
res.redirect('/p/domains'); | |
} else { | |
if (name.includes('.staticip.io')) { | |
vm.error = "Please enter a domain name without the .staticIP.io" | |
} else | |
if (name.includes('.')) { | |
vm.error = "Domain name cannot contain a dot." | |
} else | |
if (['@', 'WWW', 'WWWW', 'MAIL', 'FTP', 'SMTP'].includes(name.toUpperCase())) { | |
vm.error = "That domain mame is now allowed" | |
} else | |
if (validator.isEmpty(name)) { | |
vm.error = "Please enter a domain name." | |
} else | |
if (validator.isEmpty(ip_address)) { | |
vm.error = "Please enter a IP address." | |
} else { | |
ip_address = stripLeadingZeroes(ip_address); | |
if (!validator.isIP(ip_address)) { | |
vm.error = "Invalid IP Address Range." | |
} | |
} | |
if (vm.error != '') | |
return res.render('domain', vm) | |
else { | |
db.createDomain(name, ip_address, req.session.user.user_id, () => { | |
db.updateDomainSOA(() => { | |
res.redirect("/p/domains") | |
}); | |
}) | |
} | |
} | |
}); | |
}); | |
app.get('/lost-password', (req, res) => { | |
var vm = { | |
page: '', | |
title: 'Lost Password', | |
email: '', | |
errors: [], | |
description: '', | |
}; | |
res.render('lost-password', vm); | |
}); | |
app.post('/lost-password', (req, res) => { | |
var email = req.body.email | |
var vm = { | |
page: '', | |
title: 'Lost Password', | |
email: email, | |
errors: [], | |
description:'', | |
}; | |
if (!validator.isEmail(email)) { | |
vm.errors[0] = 'Please enter a email.'; | |
} | |
if (vm.errors.length > 0) | |
return res.render('lost-password', vm) | |
else { | |
db.getUserByEmail(email, (err, data) => { | |
if (data) { | |
const msg = ` Your password for staticip.io is:- | |
password: ${data.password}`; | |
req.flash('success', 'Your password has been sent to ' + email) | |
mail.sendMail(vm.email, "password from staticip.io", msg) | |
return res.redirect("/") | |
} else { | |
vm.errors[1] = email + ' does not exist.'; | |
return res.render('lost-password', vm); | |
} | |
}) | |
} | |
}); | |
app.post('/p/delete-domain/:id', function(req, res) { | |
var id = req.params.id; | |
db.getDomainById(id, req.session.user.user_id, (err, data) => { | |
db.deleteDomain(id, req.session.user.user_id, () => { | |
db.updateDomainSOA(() => { | |
res.redirect("/p/domains") | |
}); | |
}) | |
}) | |
}) | |
console.log('Server running staticIP.io ....') | |
http.createServer(app).listen(80) | |
https.createServer(credentials, app).listen(443) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment