Beispiel #1
0
def IPToCitySearch(log_data):
    """IP解析地区"""
    dbFile = "./ip2region.db"
    if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
        print("[Error]: Specified db file is not exists.")
        return 0
    searcher = Ip2Region(dbFile)
    log_data_list = []
    if isinstance(log_data, list):
        for log in log_data:
            ip = log["remote_addr"]
            data = searcher.memorySearch(ip)
            data_list = data["region"].decode('utf-8').split("|")
            get_con = get_coordinates()
            if data_list[3] != "0":
                City = data_list[3]
            else:
                City = data_list[2]
            #print(City)
            if data_list[0] == "中国" and City != "0":
                City_lon = get_con.GetCityCoordinates(City, data_list[0], ip)
            else:
                City_lon = get_con.GetCityCoordinates("0", data_list[0], ip)
            log["request_type"] = request_type(log["request"])
            log.update({
                "geoip": {
                    "country_name": data_list[0],
                    "region_name": data_list[2],
                    "city_name": data_list[3],
                    "location": City_lon
                }
            })
            log_data_list.append(log)
    searcher.close()
    return log_data_list
Beispiel #2
0
def testSearch():
    """
    " ip2region test function
    """
    argLen     = len(sys.argv)
    version    = python_version()
    algorithms = ["binary", "b-tree", "memory"]

    if argLen < 2:
        print("Usage: python testSearcher.py [ip2region db file] [alrogrithm]")
        print("Algorithm: %s" % ", ".join(algorithms))
        return 0

    dbFile = sys.argv[1]

    if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
        print("[Error]: Specified db file is not exists.")
        return 0

    if argLen > 2:
        algorithm = sys.argv[2]
    try:
        algorithms.index(algorithm)
    except Exception as e:
        algorithm = "b-tree"

    searcher = Ip2Region(dbFile)


    line = sys.argv[2].strip()

    if line == "":
        print("[Error]: Invalid ip address.")
    elif line == "quit":
        print("[Info]: Thanks for your use, Bye.")
    if not searcher.isip(line):
        print("[Error]: Invalid ip address.")

    try:
        sTime = time.time()*1000
        if algorithm == "binary":
            data = searcher.binarySearch(line)
        elif algorithm == "memory":
            data = searcher.memorySearch(line)
        else:
            data = searcher.btreeSearch(line)
        eTime = time.time()*1000
        # print("%s|%s in %5f millseconds" % (data["city_id"], data["region"].decode('utf-8'), eTime - sTime))
        print("%s|%s" % (line, data["region"].decode('utf-8')))
    except Exception as e:
        print("[Error]: %s" % e)

    searcher.close()
Beispiel #3
0
def get_iparea():
    target = str(request.args.get('target', ''))
    if not re.match('^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', target):
        if target.startswith('http://') or target.startswith('https://'):
            domain = target.split('/')[2]
        else:
            domain = target.split('/')[0]
        ip = socket.getaddrinfo(domain, 80)[0][4][0]
    else:
        ip = target
    searcher = Ip2Region(os.path.join(basedir, 'app/api/ip2area/ip2region.db'))
    data = searcher.btreeSearch(ip)
    searcher.close()
    return success(data=data)
Beispiel #4
0
def testSearch():
    dbFile    = "ip2region.db"
    method    = 1
    algorithm = "b-tree"
    
    if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
        print "[Error]: Specified db file is not exists."
        return 0

    print "initializing %s..." % (algorithm)
    print "+----------------------------------+"
    print "| ip2region test program           |"
    print "|                                  |"
    print "| Type 'quit' to exit program      |"
    print "+----------------------------------+"

    searcher = Ip2Region(dbFile);

    while True:
        line = raw_input("ip2region>> ")
        line = line.strip()

        if line == "":
            print "[Error]: Invalid ip address."
            continue

        if line == "quit":
            print "[Info]: Thanks for your use, Bye."
            break

        if not searcher.isip(line):
            print "[Error]: Invalid ip address."
            continue

        sTime = time.time() * 1000
        if method == 1:
            data = searcher.btreeSearch(line)
        elif method == 2:
            data = searcher.binarySearch(line)
        else:
            data = searcher.memorySearch(line)
        eTime = time.time() * 1000

        if isinstance(data, dict):
            print "%s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime)
        else:
            print "[Error]: ", data

    searcher.close()
Beispiel #5
0
def get_ip_info_ip2region(db_file, ip):
    '''
    调用 ip2region 解析ip:
    ------------------------------------------------------------
    入参结果如下:
        db_file: 解析ip的db文件,为https://github.com/lionsoul2014/ip2region的data/ip2region.db
        ip: ip地址
    '''
    searcher = Ip2Region(db_file)
    info = searcher.memorySearch(ip)["region"].decode('utf-8').split('|')

    print("****************************************")
    print("您查询的IP地址 %s 来源地是:" % ip)
    print("国家:%s" % (info[0]))
    print("区域:%s" % (info[1]))
    print("省份:%s" % (info[2]))
    print("城市:%s" % (info[3]))
    print("运营商:%s" % (info[4]))
Beispiel #6
0
def csvSearch():
    csvFile = sys.argv[1]
    dbFile    = "ip2region.db"
    #method = 3 #(1:btree 中 2:binary 慢 3:memory 快)
    #algorithm = "memory" #依method設定值來設定
    searcher = Ip2Region(dbFile);
    with open(csvFile,'r') as sf:
        with open('output.csv', 'w') as wf:
            reader = csv.reader(sf,encoding='utf-8')
            wf.write(codecs.BOM_UTF8)
            writer = csv.writer(wf, dialect='excel')
            all = []
            head_row = next(reader)  #讀取欄位名稱
            head_row.append('Country')
            head_row.append('City')
            all.append(head_row)
            for row in reader: ##就readCSV裡的所有資料(以列為單位)
                ipa = row[0] ##請輸入IP所在欄位(從0開始)##
                if not searcher.isip(ipa):
                    print "不合法IP:" ,ipa
                    continue

                data = searcher.memorySearch(ipa)
            
                temp=data["region"].split('|')
                if temp[3] != '0':
                    city = temp[3]
                elif temp[2] != '0':
                    city = temp[2]
                else:
                    city = "空"
                if temp[0] != '0':
                    country = temp[0]
                else:
                    country = "空"
                row.append(country)
                row.append(city)
                all.append(row)
            writer.writerows(all)
Beispiel #7
0
def get_ip_info_batch(db_file, ip_data):
    '''
    调用 ip2region 批量解析ip:
    ------------------------------------------------------------
    入参结果如下:
        db_file: 解析ip的db文件,为https://github.com/lionsoul2014/ip2region的data/ip2region.db
        ip_data: ip地址组成的Series或DataFrame
    ------------------------------------------------------------
    出参结果如下:
        ip_info: ip解析后的Series
    '''
    searcher = Ip2Region(db_file)

    ip_info = pd.Series(index=ip_data.index)

    for index in ip_data.index:
        try:
            info = searcher.memorySearch(
                ip_data[index])['region'].decode('utf-8')
        except OSError:
            info = np.nan
        ip_info[index] = info

    return ip_info
def testSearch():
    """
    " ip2region test function
    """
    argLen = len(sys.argv)
    version = python_version()
    algorithms = ["binary", "b-tree", "memory"]

    if argLen < 2:
        print("Usage: python testSearcher.py [ip2region db file] [alrogrithm]")
        print("Algorithm: %s" % ", ".join(algorithms))
        return 0

    dbFile = sys.argv[1]

    if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
        print("[Error]: Specified db file is not exists.")
        return 0

    if argLen > 2:
        algorithm = sys.argv[2]
    try:
        algorithms.index(algorithm)
    except Exception as e:
        algorithm = "b-tree"

    print("initializing %s..." % (algorithm))
    print("+----------------------------------+")
    print("| ip2region test program           |")
    print("| Author: [email protected]. |")
    print("| Type 'quit' to exit program      |")
    print("+----------------------------------+")

    searcher = Ip2Region(dbFile)

    while True:
        if version[:1] == "2":
            line = raw_input("ip2region>> ")
        else:
            line = input("ip2region>> ")
        line = line.strip()

        if line == "":
            print("[Error]: Invalid ip address.")
            continue

        if line == "quit":
            print("[Info]: Thanks for your use, Bye.")
            break

        if not searcher.isip(line):
            print("[Error]: Invalid ip address.")
            continue

        try:
            sTime = time.time() * 1000
            if algorithm == "binary":
                data = searcher.binarySearch(line)
            elif algorithm == "memory":
                data = searcher.memorySearch(line)
            else:
                data = searcher.btreeSearch(line)
            eTime = time.time() * 1000
            print("%s|%s in %5f millseconds" %
                  (data["city_id"], data["region"].decode('utf-8'),
                   eTime - sTime))
        except Exception as e:
            print("[Error]: %s" % e)

    searcher.close()
Beispiel #9
0
#-*- coding:utf-8 -*-
"""
    IP转化地理为值接口
    原作者:https://github.com/lionsoul2014/ip2region
    修改:csy
"""
import struct, sys, os, time
from ip2Region import Ip2Region
"""
    google 216.58.200.46            region 美国|0|0|0|0
    taobao 140.205.220.96           region 中国|华东|上海市|上海市|阿里巴巴
    hitwh.edu.cn 202.102.144.56     region 中国|华东|山东省|威海市|联通
"""


def get_ip_geoinfo(searcher, ip):
    data = searcher.btreeSearch(ip)
    geo_info = data['region'].split('|')
    std_geo_info = dict(country=geo_info[0],
                        region=geo_info[2],
                        city=geo_info[3],
                        oper=geo_info[4])
    return std_geo_info


if __name__ == '__main__':
    searcher = Ip2Region("ip2region.db")
    geo_info = get_ip_geoinfo(searcher, "221.231.138.20")
    for key in geo_info:
        print key, geo_info[key]
Beispiel #10
0
def testSearch():
    """
    " ip2region test function
    """
    llen = len(sys.argv)

    if llen < 2:
        print "Usage: python testSearcher.py [ip2region db file] [alrogrithm]"
        print "Algorithm: binary or b-tree"
        return 0

    dbFile    = sys.argv[1]
    method    = 1
    algorithm = "b-tree"

    if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
        print "[Error]: Specified db file is not exists."
        return 0

    if llen > 2:
        algorithm = sys.argv[2]
        if algorithm == "binary":
            method = 2
        elif algorithm == "memory":
            method = 3

    print "initializing %s..." % (algorithm)
    print "+----------------------------------+"
    print "| ip2region test program           |"
    print "| Author: [email protected]. |"
    print "| Type 'quit' to exit program      |"
    print "+----------------------------------+"

    searcher = Ip2Region(dbFile);

    while True:
        line = raw_input("ip2region>> ")
        line = line.strip()

        if line == "":
            print "[Error]: Invalid ip address."
            continue

        if line == "quit":
            print "[Info]: Thanks for your use, Bye."
            break

        if not searcher.isip(line):
            print "[Error]: Invalid ip address."
            continue

        sTime = time.time() * 1000
        if method == 1:
            print "method-1"
            data = searcher.btreeSearch(line)
        elif method == 2:
            print "method-2"
            data = searcher.binarySearch(line)
        else:
            print "method-3"
            data = searcher.memorySearch(line)
        eTime = time.time() * 1000

        if isinstance(data, dict):
            print "%s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime)
        else:
            print "[Error]: ", data

    searcher.close()
Beispiel #11
0
def process_log(lines, pattern, processor, arguments):
    searcher = Ip2Region(arguments["Ip2RegionFile"])
    records = parse_log(lines, pattern, searcher)
    processor.process(records)
    print(processor.report())
Beispiel #12
0
def testSearch():
    """
    " ip2region test function
    """
    argLen     = len(sys.argv)
    version    = python_version()
    algorithms = ["binary", "b-tree", "memory"]

    if argLen < 2:


        return 0

    dbFile = sys.argv[1]

    if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):

        return 0

    if argLen > 2:
        algorithm = sys.argv[2]
    try:
        algorithms.index(algorithm)
    except Exception as e:
        algorithm = "b-tree"








    searcher = Ip2Region(dbFile)

    while True:
        if version[:1] == "2":
            line = raw_input("ip2region>> ")
        else:
            line = input("ip2region>> ")
        line = line.strip()

        if line == "":

            continue

        if line == "quit":

            break

        if not searcher.isip(line):

            continue

        try:
            sTime = time.time()*1000
            if algorithm == "binary":
                data = searcher.binarySearch(line)
            elif algorithm == "memory":
                data = searcher.memorySearch(line)
            else:
                data = searcher.btreeSearch(line)
            eTime = time.time()*1000

        except Exception as e:


    searcher.close()
Beispiel #13
0
from flask import Flask, jsonify
from ip2Region import Ip2Region

app = Flask(__name__)
searcher = Ip2Region('data/ip2region.db')


@app.route('/ip2region/<ip>')
def query(ip):
    if not searcher.isip(ip):
        return jsonify({
            'error_code': 4000,
            'desc': '[Error]: Invalid ip address.'
        })

    data = searcher.btreeSearch(ip)
    if isinstance(data, dict):
        region_list = data['region'].decode('utf8').split('|')
        data = {
            'country': region_list[0],
            'province': region_list[2],
            'city': region_list[3],
        }
        return jsonify(data)
    else:
        return jsonify({'error_code': 4001, 'desc': '[Error]: Query failed.'})


if __name__ == '__main__':
    app.run(host='0.0.0.0')
Beispiel #14
0
#coding=utf-8
import os, sys
import struct, time
from ip2Region import Ip2Region
import logging

def get_file_db():
    CURRENT_ROOT = os.path.realpath(os.path.dirname(__file__))
    file_path = os.path.join(CURRENT_ROOT,"data", 'ip2region.db')
    return file_path


ip_searcher = Ip2Region(get_file_db())

def search_ip(ip):
    global ip_searcher
    
    if not ip_searcher.isip(ip):
        return ""

    data = ip_searcher.btreeSearch(ip)
    return data

def get_ip_info(ip):
    data = search_ip(ip)
    if data:
        city_id = data.get("city_id")
        area = data.get("region","")
        datas = area.split('|')
        province = datas[-3]
        city = datas[-2]
Beispiel #15
0
    def run(self):
        self.__lock.acquire()
        try:
            sTime = time.time() * 1000
            data = self.__searcher.memorySearch("49.220.138.233")
            eTime = time.time() * 1000
            print("%s|%s in %5f millseconds" %
                  (data["city_id"], data["region"].decode('utf-8'),
                   eTime - sTime))
        finally:
            self.__lock.release()


if __name__ == "__main__":
    threads = []
    searcher = Ip2Region("/data/ip2region/data/ip2region.db")
    lock = threading.Lock()

    for i in range(10000):
        t = BeachmarkThread(searcher, lock)
        threads.append(t)

    sTime = time.time() * 1000
    for t in threads:
        t.start()

    for t in threads:
        t.join()
    eTime = time.time() * 1000

    print("Beachmark done: %5f" % (eTime - sTime))
Beispiel #16
0
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 21 15:42:19 2022
IP信息查询:分内网IP、国内一级行政区、国外国家
@author: HJY
"""

from ip2Region import Ip2Region
import pandas as pd

search = Ip2Region('ip2region.db')


def ipsearch(ip):
    re = search.memorySearch(ip)['region'].decode('utf-8').split('|')
    if re[-1] == '内网IP':
        return re[-1]
    elif re[0] == '中国':
        return re[2]
    else:
        return re[0]


df = pd.read_csv('test.csv')
df['region'] = df.apply(lambda x: ipsearch(x.ip), axis=1)

df.to_csv('result.csv')
Beispiel #17
0
reload(sys)
sys.setdefaultencoding('utf-8')

lock0 = threading.Lock()
lock1 = threading.Lock()
threads = []
dnslist = [['114.114.114.114'], ['114.114.115.115'], ['223.5.5.5'],
           ['223.6.6.6'], ['180.76.76.76'], ['1.2.4.8'], ['119.29.29.29'],
           ['182.254.116.116'], ['123.125.81.6'], ['140.207.198.6'],
           ['208.67.222.222'], ['208.67.220.220'], ['101.226.4.6']]

dbFile = './ip2region/data/ip2region.db'
method = 1
algorithm = "b-tree"

searcher = Ip2Region(dbFile)


def gethttpcode():
    conn = MySQLdb.connect(host='172.26.253.3',
                           port=3306,
                           user='******',
                           passwd='platform')
    conn.set_character_set('utf8')
    cur = conn.cursor()
    cur.execute("SET NAMES utf8")
    cur.execute("use malicious_domain_sys;")
    conn.commit()
    i = 0
    while 1 == 1:
        ip_addr = ''
Beispiel #18
0
 def __init__(self, db_file):
     self.searcher = Ip2Region(db_file)
Beispiel #19
0
    PRAGMA = "Pragma"
    PROXY_AUTHORIZATION = "Proxy-Authorization"
    PROXY_CONNECTION = "Proxy-Connection"
    RANGE = "Range"
    REFERER = "Referer"
    SERVER = "Server"
    SET_COOKIE = "Set-Cookie"
    TRANSFER_ENCODING = "Transfer-Encoding"
    URI = "URI"
    USER_AGENT = "User-Agent"
    VIA = "Via"
    X_POWERED_BY = "X-Powered-By"


results = []
searcher = Ip2Region('db/ip2region.db')
identify = False


def check_waf(headers):
    retval = re.search(r"wangzhan\.360\.cn",
                       headers.get("X-Powered-By-360wzb", ""),
                       re.I) is not None
    if retval:
        return "360"
    retval = re.search(r"\AAL[_-]?(SESS|LB)=", headers.get("SET-COOKIE", ""),
                       re.I) is not None
    if retval:
        return "airlock"
    retval = re.search(r"MISS", headers.get("X-Powered-By-Anquanbao", ""),
                       re.I) is not None