Parcourir la source

Don't use the user's address as target when he/sshe comes from dn42

Baptiste Jonglez il y a 10 ans
Parent
commit
ea13cf8ec1
2 fichiers modifiés avec 11 ajouts et 2 suppressions
  1. 10 1
      peerfinder.py
  2. 1 1
      templates/home.html

+ 10 - 1
peerfinder.py

@@ -8,7 +8,7 @@ from flask.ext.migrate import Migrate, MigrateCommand
 #from flask import session, request, url_for, redirect, render_template
 
 import netaddr
-from netaddr import IPAddress
+from netaddr import IPAddress, IPSet
 # Hack for python3
 from netaddr.strategy.ipv4 import packed_to_int as unpack_v4
 from netaddr.strategy.ipv6 import packed_to_int as unpack_v6
@@ -16,6 +16,8 @@ import socket
 from datetime import datetime, timedelta
 from uuid import uuid4
 
+DN42 = IPSet(['172.22.0.0/15', '172.31.0.0/16', '10.0.0.0/8'])
+
 app = Flask(__name__)
 app.config.from_pyfile('config.py')
 db = SQLAlchemy(app)
@@ -52,6 +54,13 @@ def ipaddress_pp(addr):
     except netaddr.AddrConversionError:
         return a.ipv6()
 
+@app.template_filter()
+def not_dn42(addr):
+    """Filter the input address if it is part of dn42"""
+    a = IPAddress(addr)
+    if a in DN42:
+        return ""
+    return a
 
 class Target(db.Model):
     """Target IP to ping"""

+ 1 - 1
templates/home.html

@@ -22,7 +22,7 @@ latency from various points in the network towards you.</p>
 <p>
 <form action="/submit" method="POST">
 <label for="target">Target:</label>
-<input type="text" name="target" id="target" value="{{ request.remote_addr|ipaddress_pp }}"/><br />
+<input type="text" name="target" id="target" value="{{ request.remote_addr|ipaddress_pp|not_dn42 }}"/><br />
 <input type="checkbox" id="public" name="public" />
 <label for="public">Public?</label><br />
 <input type="submit" value="Launch" />