Explorar o código

vpn check / country

lesion %!s(int64=6) %!d(string=hai) anos
pai
achega
d603a1aabb
Modificáronse 4 ficheiros con 32 adicións e 8 borrados
  1. 4 4
      routes/index.js
  2. 14 4
      routes/utils.js
  3. 7 0
      views/vpnnook.pug
  4. 7 0
      views/vpnok.pug

+ 4 - 4
routes/index.js

@@ -1,7 +1,7 @@
 const express = require('express')
 const router = express.Router()
 
-const { fromTor, fromVpn } = require('./utils')
+const { fromTor, fromVpn, checkCountry } = require('./utils')
 
 // magic
 const render = template => (req, res) => res.render(template)
@@ -19,9 +19,9 @@ router.get('/metadata', render('metadata'))
 router.get('/mail', render('mail'))
 
 // level V (vpn -> read mail)
-router.get('/vpn', fromTor, fromVpn, (req, res) => {
-  if (!req.isTor && req.isVpn) render('vpnok')
-  else render('vpnnook')
+router.get('/vpn', fromTor, fromVpn, checkCountry, (req, res) => {
+  if (!req.fromTor && req.fromVpn) render('vpnok', req.geoinfo )
+  else render('vpnnook', req.geoinfo )
 })
 
 module.exports = router

+ 14 - 4
routes/utils.js

@@ -24,16 +24,26 @@ const fromVpn = async (req, res, next) => {
   const sourceIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress
   const baseUrl = 'https://check.getipintel.net/check.php?ip='
   const url = `${baseUrl}${sourceIp}&contact=anna@fugadalcontrollo.org`
-  console.log(url);
   request(url, 
     (err, res, body) => {
       if (!err && res.statusCode === 200) {
-        console.log(body)
+        req.fromVpn = parseFloat(body)>0.5
       } else {
-        console.log(err, body)
+        req.fromVpn = false
       }
+      next()
     })
 }
 
+const checkCountry = (req, res, next) => {
+  request(`http://ip-api.com/json/${sourceIp}`, (err, res, body) => {
+    if (!err && res.statusCode === 200) {
+      console.log(body);
+      req.geoinfo = JSON.parse(body);
+      console.log(res.geoinfo);
+    }
+    next();
+  })
+}
 
-module.exports = { fromTor, fromVpn }
+module.exports = { fromTor, fromVpn, checkCountry }

+ 7 - 0
views/vpnnook.pug

@@ -0,0 +1,7 @@
+extends layout
+
+block content
+  p.
+    E no, arrivi da una {{country}} / {{city}}
+
+

+ 7 - 0
views/vpnok.pug

@@ -0,0 +1,7 @@
+extends layout
+
+block content
+  p.
+    Evviva, arrivi da una VPN {{country}} / {{city}}
+
+