mirror of
				https://github.com/KevinMidboe/zoff.git
				synced 2025-10-29 18:00:23 +00:00 
			
		
		
		
	Implemented recaptcha
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -3,6 +3,7 @@ server/config/mailconfig.js | |||||||
| server/config/api_key.js | server/config/api_key.js | ||||||
| server/config/mongo_config.js | server/config/mongo_config.js | ||||||
| server/config/cert_config.js | server/config/cert_config.js | ||||||
|  | server/config/recaptcha.js | ||||||
| server/public/assets/dist/callback.min.js | server/public/assets/dist/callback.min.js | ||||||
| server/public/assets/dist/embed.min.js | server/public/assets/dist/embed.min.js | ||||||
| server/public/assets/dist/main.min.js | server/public/assets/dist/main.min.js | ||||||
|   | |||||||
| @@ -22,7 +22,7 @@ api_key.js | |||||||
| mongo_config.js | mongo_config.js | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| in ```/server/config```. There are ```*.example.js``` files for all the ones mentioned above. If you're going to deploy the server with a certificate, you also need to create the ```cert_config.js``` in ```/server/config/```. If you want the mailing to work, take a look at ```mailconfig.example.js```. You'll need ```mailconfig.js``` for this to work. | in ```/server/config```. There are ```*.example.js``` files for all the ones mentioned above. If you're going to deploy the server with a certificate, you also need to create the ```cert_config.js``` in ```/server/config/```. If you want the mailing to work, take a look at ```mailconfig.example.js``` and ```recaptcha.example.js```. You'll need ```mailconfig.js``` and ```recaptcha.js``` for this to work. | ||||||
|  |  | ||||||
| Use ```$ npm start``` to start the server. | Use ```$ npm start``` to start the server. | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										2689
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										2689
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -34,6 +34,7 @@ | |||||||
|     "emoji-strip": "^0.0.3", |     "emoji-strip": "^0.0.3", | ||||||
|     "express": "^4.15.2", |     "express": "^4.15.2", | ||||||
|     "express-handlebars": "^3.0.0", |     "express-handlebars": "^3.0.0", | ||||||
|  |     "express-recaptcha": "^3.0.1", | ||||||
|     "express-subdomain": "^1.0.5", |     "express-subdomain": "^1.0.5", | ||||||
|     "gulp": "^3.9.1", |     "gulp": "^3.9.1", | ||||||
|     "gulp-concat": "^2.6.1", |     "gulp-concat": "^2.6.1", | ||||||
|   | |||||||
							
								
								
									
										6
									
								
								server/config/recaptcha.example.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								server/config/recaptcha.example.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | var recaptcha = { | ||||||
|  |     site: "xxxx", | ||||||
|  |     key: "xxxxx", | ||||||
|  | } | ||||||
|  |  | ||||||
|  | module.exports = recaptcha; | ||||||
| @@ -123,6 +123,7 @@ app.use(function (req, res, next) { | |||||||
| app.use('/service-worker.js', function(req, res) { | app.use('/service-worker.js', function(req, res) { | ||||||
| 	res.sendFile(publicPath + '/service-worker.js'); | 	res.sendFile(publicPath + '/service-worker.js'); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.use('/', ico_router); | app.use('/', ico_router); | ||||||
| app.use('/', api); | app.use('/', api); | ||||||
| app.use('/', router); | app.use('/', router); | ||||||
|   | |||||||
| @@ -229,10 +229,15 @@ var Helper = { | |||||||
|             $("#send-loader").removeClass("hide"); |             $("#send-loader").removeClass("hide"); | ||||||
|             $("#contact-form-from").attr("disabled", "true"); |             $("#contact-form-from").attr("disabled", "true"); | ||||||
|             $("#contact-form-message").attr("disabled", "true"); |             $("#contact-form-message").attr("disabled", "true"); | ||||||
|  |             var captcha_response = grecaptcha.getResponse(); | ||||||
|  |             console.log(captcha_response); | ||||||
|             $.ajax({ |             $.ajax({ | ||||||
|                 type: "POST", |                 type: "POST", | ||||||
|                 data: {from: from, message: message}, |                 data: { | ||||||
|  |                     from: from, | ||||||
|  |                     message: message, | ||||||
|  |                     "g-recaptcha-response": captcha_response, | ||||||
|  |                 }, | ||||||
|                 url: "/api/mail", |                 url: "/api/mail", | ||||||
|                 success: function(data){ |                 success: function(data){ | ||||||
|                     if(data == "success"){ |                     if(data == "success"){ | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
|         <h4>Contact</h4> |         <h4>Contact</h4> | ||||||
|         <div id="contact-container"> |         <div id="contact-container"> | ||||||
|             <p>Found a bug, or just want to contact the team?</p> |             <p>Found a bug, or just want to contact the team?</p> | ||||||
|             <form id="contact-form" method="post" onsubmit="return false;"> |             <form id="contact-form" method="post"> | ||||||
|                 <div class="input-field"> |                 <div class="input-field"> | ||||||
|                     <input id="contact-form-from" name="from" type="email" autocomplete="off" class="validate" /> |                     <input id="contact-form-from" name="from" type="email" autocomplete="off" class="validate" /> | ||||||
|                     <label for="contact-form-from" class="noselect">Email</label> |                     <label for="contact-form-from" class="noselect">Email</label> | ||||||
| @@ -12,6 +12,7 @@ | |||||||
|                     <input id="contact-form-message" name="message" type="text" autocomplete="off"> |                     <input id="contact-form-message" name="message" type="text" autocomplete="off"> | ||||||
|                     <label for="contact-form-message" class="noselect">Message</label> |                     <label for="contact-form-message" class="noselect">Message</label> | ||||||
|                 </div> |                 </div> | ||||||
|  |                 {{{captcha}}} | ||||||
|                 <div class="valign hide" id="send-loader"> |                 <div class="valign hide" id="send-loader"> | ||||||
|                     <div class="preloader-wrapper small active"> |                     <div class="preloader-wrapper small active"> | ||||||
|                         {{> spinner}} |                         {{> spinner}} | ||||||
|   | |||||||
| @@ -84,8 +84,14 @@ router.route('/api/imageblob').post(function(req, res) { | |||||||
| var nodemailer = require('nodemailer'); | var nodemailer = require('nodemailer'); | ||||||
| try { | try { | ||||||
|     var mailconfig = require(path.join(__dirname, '../config/mailconfig.js')); |     var mailconfig = require(path.join(__dirname, '../config/mailconfig.js')); | ||||||
|  |     var recaptcha_config = require(path.join(__dirname, '../config/recaptcha.js')); | ||||||
|  |     var Recaptcha = require('express-recaptcha'); | ||||||
|  |     var RECAPTCHA_SITE_KEY = recaptcha_config.site; | ||||||
|  |     var RECAPTCHA_SECRET_KEY = recaptcha_config.key; | ||||||
|  |     var recaptcha = new Recaptcha(RECAPTCHA_SITE_KEY, RECAPTCHA_SECRET_KEY); | ||||||
|  |  | ||||||
|    router.route('/api/mail').post(function(req, res) { |     router.route('/api/mail').post(recaptcha.middleware.verify, function(req, res) { | ||||||
|  |         if(req.recaptcha.error == null) { | ||||||
|             let transporter = nodemailer.createTransport(mailconfig); |             let transporter = nodemailer.createTransport(mailconfig); | ||||||
|  |  | ||||||
|             transporter.verify(function(error, success) { |             transporter.verify(function(error, success) { | ||||||
| @@ -106,6 +112,7 @@ try { | |||||||
|                     transporter.sendMail(msg, (error, info) => { |                     transporter.sendMail(msg, (error, info) => { | ||||||
|                         if (error) { |                         if (error) { | ||||||
|                             res.send("failed"); |                             res.send("failed"); | ||||||
|  |                             transporter.close(); | ||||||
|                             return; |                             return; | ||||||
|                         } |                         } | ||||||
|                         res.send("success"); |                         res.send("success"); | ||||||
| @@ -113,9 +120,13 @@ try { | |||||||
|                     }); |                     }); | ||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
|  |         } else { | ||||||
|  |             res.send("failed"); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|     }); |     }); | ||||||
| } catch(e) { | } catch(e) { | ||||||
|    console.log("Mail not setup and wont work"); |     console.log("Mail is not configured and wont work"); | ||||||
|     console.log("Seems you forgot to create a mailconfig.js in /server/config/. Have a look at the mailconfig.example.js."); |     console.log("Seems you forgot to create a mailconfig.js in /server/config/. Have a look at the mailconfig.example.js."); | ||||||
|     router.route('/api/mail').post(function(req, res) { |     router.route('/api/mail').post(function(req, res) { | ||||||
|         console.log("Someone tried to send a mail, but the mailsystem hasn't been enabled..") |         console.log("Someone tried to send a mail, but the mailsystem hasn't been enabled..") | ||||||
|   | |||||||
| @@ -2,8 +2,30 @@ var express = require('express'); | |||||||
| var router = express.Router(); | var router = express.Router(); | ||||||
| var path = require('path'); | var path = require('path'); | ||||||
| var year = new Date().getYear()+1900; | var year = new Date().getYear()+1900; | ||||||
|  | var path = require('path'); | ||||||
|  |  | ||||||
| router.use(function(req, res, next) { | try { | ||||||
|  |     var Recaptcha = require('express-recaptcha'); | ||||||
|  |     var recaptcha_config = require(path.join(path.join(__dirname, '../config/'), 'recaptcha.js')); | ||||||
|  |     var RECAPTCHA_SITE_KEY = recaptcha_config.site; | ||||||
|  |     var RECAPTCHA_SECRET_KEY = recaptcha_config.key; | ||||||
|  |     var recaptcha = new Recaptcha(RECAPTCHA_SITE_KEY, RECAPTCHA_SECRET_KEY); | ||||||
|  | } catch(e) { | ||||||
|  |     console.log("Error - missing file"); | ||||||
|  |     console.log("Seems you forgot to create the file recaptcha.js in /server/config/. Have a look at recaptcha.example.js."); | ||||||
|  |     var recaptcha = { | ||||||
|  |         middleware: { | ||||||
|  |             render: (req, res, next) => { | ||||||
|  |                 res.recaptcha = "" | ||||||
|  |                 next() | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | router.use(recaptcha.middleware.render, function(req, res, next) { | ||||||
|     next(); // make sure we go to the next routes and don't stop here |     next(); // make sure we go to the next routes and don't stop here | ||||||
| }); | }); | ||||||
|  |  | ||||||
| @@ -19,6 +41,7 @@ router.route('/').post(function(req, res, next){ | |||||||
|     root(req, res, next); |     root(req, res, next); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  |  | ||||||
| function root(req, res, next) { | function root(req, res, next) { | ||||||
|     try{ |     try{ | ||||||
|         var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0]; |         var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0]; | ||||||
| @@ -30,7 +53,8 @@ function root(req, res, next) { | |||||||
|         if(subdomain[0] == "remote") { |         if(subdomain[0] == "remote") { | ||||||
|             var data = { |             var data = { | ||||||
|                 year: year, |                 year: year, | ||||||
|                 javascript_file: "remote.min.js" |                 javascript_file: "remote.min.js", | ||||||
|  |                 captcha: res.recaptcha | ||||||
|             } |             } | ||||||
|             res.render('layouts/remote', data); |             res.render('layouts/remote', data); | ||||||
|         } else if(subdomain[0] == "www") { |         } else if(subdomain[0] == "www") { | ||||||
| @@ -39,7 +63,9 @@ function root(req, res, next) { | |||||||
|             var data = { |             var data = { | ||||||
|                 year: year, |                 year: year, | ||||||
|                 javascript_file: "main.min.js", |                 javascript_file: "main.min.js", | ||||||
|  |                 captcha: res.recaptcha | ||||||
|             } |             } | ||||||
|  |             console.log(data.recaptcha); | ||||||
|             res.render('layouts/frontpage', data); |             res.render('layouts/frontpage', data); | ||||||
|         } |         } | ||||||
|     } catch(e) { |     } catch(e) { | ||||||
| @@ -59,7 +85,8 @@ function channel(req, res, next) { | |||||||
|         if(subdomain[0] == "remote") { |         if(subdomain[0] == "remote") { | ||||||
|             var data = { |             var data = { | ||||||
|                 year: year, |                 year: year, | ||||||
|                 javascript_file: "remote.min.js" |                 javascript_file: "remote.min.js", | ||||||
|  |                 captcha: res.recaptcha | ||||||
|             } |             } | ||||||
|             res.render('layouts/remote', data); |             res.render('layouts/remote', data); | ||||||
|         } else if(subdomain.length >= 2 && subdomain[0] == "www") { |         } else if(subdomain.length >= 2 && subdomain[0] == "www") { | ||||||
| @@ -74,8 +101,11 @@ function channel(req, res, next) { | |||||||
|                     title: "404: File Not Found", |                     title: "404: File Not Found", | ||||||
|                     list_name: capitalizeFirstLetter(req.params.channel_name), |                     list_name: capitalizeFirstLetter(req.params.channel_name), | ||||||
|                     year: year, |                     year: year, | ||||||
|                     javascript_file: "main.min.js" |                     javascript_file: "main.min.js", | ||||||
|  |                     captcha: res.recaptcha, | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|  |  | ||||||
|                 if(req.params.channel_name == "404") { |                 if(req.params.channel_name == "404") { | ||||||
|                     res.status(404); |                     res.status(404); | ||||||
|                 } |                 } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user