2012-01-27 19:12:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: ts=4
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Copyright (c) 2006 Mehdi Abaakouk
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 3 as
|
|
|
|
# published by the Free Software Foundation
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
#
|
|
|
|
###
|
2012-01-20 00:12:19 +01:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
from dns import resolver, reversename
|
2012-01-20 00:12:19 +01:00
|
|
|
import socket
|
2012-05-28 13:51:16 +02:00
|
|
|
import pickle
|
2012-10-16 08:00:10 +02:00
|
|
|
import xml.parsers.expat
|
2012-01-20 00:12:19 +01:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
from flask import Flask
|
|
|
|
|
|
|
|
|
2014-04-20 16:58:25 +02:00
|
|
|
resolv = resolver.Resolver()
|
|
|
|
resolv.timeout = 0.5
|
|
|
|
resolv.lifetime = 1
|
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_pyfile('lg.cfg')
|
|
|
|
|
2015-01-21 18:03:55 +01:00
|
|
|
|
2012-01-20 00:12:19 +01:00
|
|
|
def resolve(n, q):
|
2015-01-21 18:03:55 +01:00
|
|
|
return str(resolv.query(n, q)[0])
|
|
|
|
|
2012-01-20 00:12:19 +01:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
def resolve_ptr(ip):
|
|
|
|
ptr = str(resolve(reversename.from_address(ip), 'PTR')).lower()
|
|
|
|
ptr = ptr.replace(app.config.get('ROUTER_NAME_REMOVE', ''), '')
|
|
|
|
return ptr
|
|
|
|
|
|
|
|
|
2012-01-20 00:12:19 +01:00
|
|
|
def mask_is_valid(n):
|
2015-01-21 18:03:55 +01:00
|
|
|
if not n:
|
|
|
|
return True
|
|
|
|
try:
|
|
|
|
mask = int(n)
|
|
|
|
return (mask >= 1 and mask <= 128)
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2012-01-20 00:12:19 +01:00
|
|
|
|
|
|
|
def ipv4_is_valid(n):
|
|
|
|
try:
|
|
|
|
socket.inet_pton(socket.AF_INET, n)
|
|
|
|
return True
|
|
|
|
except socket.error:
|
|
|
|
return False
|
|
|
|
|
2015-01-21 18:03:55 +01:00
|
|
|
|
2012-01-20 00:12:19 +01:00
|
|
|
def ipv6_is_valid(n):
|
|
|
|
try:
|
|
|
|
socket.inet_pton(socket.AF_INET6, n)
|
|
|
|
return True
|
|
|
|
except socket.error:
|
|
|
|
return False
|
|
|
|
|
2015-01-21 18:03:55 +01:00
|
|
|
|
2012-05-28 13:51:16 +02:00
|
|
|
def save_cache_pickle(filename, data):
|
2015-01-21 18:03:55 +01:00
|
|
|
output = open(filename, 'wb')
|
|
|
|
pickle.dump(data, output)
|
|
|
|
output.close()
|
|
|
|
|
|
|
|
|
|
|
|
def load_cache_pickle(filename, default=None):
|
|
|
|
try:
|
|
|
|
pkl_file = open(filename, 'rb')
|
|
|
|
except IOError:
|
|
|
|
return default
|
|
|
|
try:
|
|
|
|
data = pickle.load(pkl_file)
|
|
|
|
except:
|
|
|
|
data = default
|
|
|
|
pkl_file.close()
|
|
|
|
return data
|
|
|
|
|
2012-01-20 00:12:19 +01:00
|
|
|
|
2012-10-16 08:00:10 +02:00
|
|
|
def unescape(s):
|
|
|
|
want_unicode = False
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
s = s.encode("utf-8")
|
|
|
|
want_unicode = True
|
|
|
|
|
|
|
|
# the rest of this assumes that `s` is UTF-8
|
|
|
|
list = []
|
|
|
|
|
|
|
|
# create and initialize a parser object
|
|
|
|
p = xml.parsers.expat.ParserCreate("utf-8")
|
|
|
|
p.buffer_text = True
|
|
|
|
p.returns_unicode = want_unicode
|
|
|
|
p.CharacterDataHandler = list.append
|
|
|
|
|
|
|
|
# parse the data wrapped in a dummy element
|
|
|
|
# (needed so the "document" is well-formed)
|
|
|
|
p.Parse("<e>", 0)
|
|
|
|
p.Parse(s, 0)
|
|
|
|
p.Parse("</e>", 1)
|
|
|
|
|
|
|
|
# join the extracted strings and return
|
|
|
|
es = ""
|
|
|
|
if want_unicode:
|
|
|
|
es = u""
|
|
|
|
return es.join(list)
|