コード例 #1
0
ファイル: api.py プロジェクト: WangZhijian/ghost_sa
def who_am_i():
  if request.headers.get('X-Forwarded-For') is None:
    ip = request.remote_addr#服务器直接暴露
  else:
    ip = request.headers.get('X-Forwarded-For') #获取SLB真实地址
  ip_city,ip_is_good = get_addr(ip)
  ip_asn,ip_asn_is_good = get_asn(ip)
  if ip_is_good ==0:
    ip_city = '{}'
  if ip_asn_is_good ==0:
    ip_asn = '{}'
  User_Agent = request.headers.get('User-Agent')[0:2047] if request.headers.get('User-Agent') else None#Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
  Host = request.headers.get('Host') #: 10.16.5.241:5000
  Connection = request.headers.get('Connection')#: keep-alive
  Pragma = request.headers.get('Pragma')#: no-cache
  Cache_Control = request.headers.get('Cache-Control')#: no-cache
  Accept = request.headers.get('Accept')[0:254] if request.headers.get('Accept') else None#: image/webp,image/apng,image/*,*/*;q=0.8
  Accept_Encoding = request.headers.get('Accept-Encoding')[0:254] if request.headers.get('Accept-Encoding') else None#: gzip, deflate
  Accept_Language = request.headers.get('Accept-Language')[0:254] if request.headers.get('Accept-Language') else None#: zh-CN,zh;q=0.9
  ua_platform = request.user_agent.platform #客户端操作系统
  ua_browser = request.user_agent.browser #客户端的浏览器
  ua_version = request.user_agent.version #客户端浏览器的版本
  ua_language = request.user_agent.language #客户端浏览器的语言
  url = request.url
  referrer = request.referrer

  returnjson = {'ip':ip,'ip_city':ip_city,'ip_asn':ip_asn,'ip_is_good':ip_is_good,'ip_asn_is_good':ip_asn_is_good,'User_Agent':User_Agent,'Host':Host,'Connection':Connection,'Pragma':Pragma,'Cache_Control':Cache_Control,'Accept':Accept,'Accept_Encoding':Accept_Encoding,'Accept_Language':Accept_Language,'ua_platform':ua_platform,'ua_browser':ua_browser,'ua_version':ua_version,'ua_language':ua_language,'url':url,'referrer':referrer}
  return jsonify(returnjson)
コード例 #2
0
ファイル: subscribe.py プロジェクト: xiaodin1/ghost_sa
def realtime_subscribe(broker):
    consumer = KafkaConsumer('event_topic', bootstrap_servers=[broker])
    for message in consumer:
        data = json.loads(message.value.decode('utf-8'))
        # pprint.pprint(message.value.decode('utf-8'))
        json_data = json.dumps(data)
        # pprint.pprint(json_data)
        remark = ''
        project = data['project']
        ua_platform = data['properties']['$os'] if '$os' in data[
            'properties'] else ''  #客户端操作系统
        ua_browser = data['properties']['$browser'] if '$browser' in data[
            'properties'] else ''  #客户端的浏览器
        ua_version = data['properties'][
            '$browser_version'] if '$browser_version' in data[
                'properties'] else ''  #客户端浏览器的版本
        ip = data['properties']['$ip'] if '$ip' in data['properties'] else ''
        ip_city, ip_is_good = get_addr(ip)
        ip_asn, ip_asn_is_good = get_asn(ip)
        if ip_is_good == 0:
            ip_city = '{}'
        if ip_asn_is_good == 0:
            ip_asn = '{}'
        referrer = data['properties'][
            '$latest_referrer'] if '$latest_referrer' in data[
                'properties'] else ''
        insert_data(project=project,
                    data_decode=data,
                    User_Agent='',
                    Host='',
                    Connection='',
                    Pragma='',
                    Cache_Control='',
                    Accept='',
                    Accept_Encoding='',
                    Accept_Language='',
                    ip=ip,
                    ip_city=ip_city,
                    ip_asn=ip_asn,
                    url='',
                    referrer=referrer,
                    remark=remark,
                    ua_platform=ua_platform,
                    ua_browser=ua_browser,
                    ua_version=ua_version,
                    ua_language='',
                    ip_is_good=ip_is_good,
                    ip_asn_is_good=ip_asn_is_good,
                    use_kafka=admin.use_kafka)
コード例 #3
0
ファイル: import_from_sa.py プロジェクト: xiaodin1/ghost_sa
def upload_events_from_pickle_to_sql(project='tvcbook', remark='production'):
    #所有的文件
    filelist = []
    dirpath = os.path.join('data_export', project, remark, 'events')
    for maindir, subdir, file_name_list in os.walk(dirpath):
        # print("1:",maindir) #当前主目录
        # print("2:",subdir) #当前主目录下的所有目录
        # print("3:",str(file_name_list))  #当前主目录下的所有文件
        # file_name_list.sort()
        # subdir.sort()
        for filename in file_name_list:
            apath = os.path.join(maindir, filename)  #合并成一个完整路径
            filelist.append(apath)
    #   print(file_name_list)
    filelist.sort()
    # print(filelist)
    for pkl in filelist:
        # print(pkl)
        with open(pkl, "rb") as f2:
            results = pickle._loads(f2.read())
        # p = multiprocessing.Pool(processes = 3)
        for item in results:
            # # print(item)
            try:
                itemdict = json.loads(item)
                all_json = {
                    "properties": itemdict,
                    "distinct_id": itemdict["distinct_id"],
                    "event": itemdict["event"],
                    "type": "track"
                }
                # first_id = itemdict['first_id'] if 'first_id' in itemdict else None
                # second_id = itemdict['second_id'] if 'second_id' in itemdict else None
                # unionid = itemdict['unionid'] if 'unionid' in itemdict else None
                # id = itemdict['id'] if 'id' in itemdict else None
                ip_city, ip_is_good = get_addr(itemdict["$ip"])
                ip_asn, ip_asn_is_good = get_asn(itemdict["$ip"])
                if ip_is_good == 0:
                    ip_city = '{}'
                if ip_asn_is_good == 0:
                    ip_asn = '{}'
                print(all_json)
                created_at = time.mktime(
                    time.strptime(itemdict["time"].split('.')[0],
                                  '%Y-%m-%d %H:%M:%S'))
                # all_json = json.dumps(itemdict,ensure_ascii=False)
                insert_data(project='tvcbook',
                            data_decode=all_json,
                            User_Agent=None,
                            Host=None,
                            Connection=None,
                            Pragma=None,
                            Cache_Control=None,
                            Accept=None,
                            Accept_Encoding=None,
                            Accept_Language=None,
                            ip=itemdict["$ip"] if "$ip" in itemdict else None,
                            ip_city=ip_city,
                            ip_asn=ip_asn,
                            url=None,
                            referrer=itemdict["$referrer"]
                            if "$referrer" in itemdict else None,
                            remark=remark,
                            ua_platform=itemdict["$lib"]
                            if "$lib" in itemdict else None,
                            ua_browser=itemdict["$browser"]
                            if "$browser" in itemdict else None,
                            ua_version=itemdict["$browser_version"]
                            if "$browser_version" in itemdict else None,
                            ua_language=None,
                            ip_is_good=ip_is_good,
                            ip_asn_is_good=ip_asn_is_good,
                            created_at=created_at)
            except Exception:
                error = traceback.format_exc()
                write_to_log(filename='import_from_sa',
                             defname='upload_events_from_pickle_to_sql',
                             result=error)
        # f2.close()
        #   p.apply_async(func=insert_data,kwds={
        #     "project":"tvcbook",
        #     "data_decode":all_json,
        #     "User_Agent":None,
        #     "Host":None,
        #     "Connection":None,
        #     "Pragma":None,
        #     "Cache_Control":None,
        #     "Accept":None,
        #     "Accept_Encoding":None,
        #     "Accept_Language":None,
        #     "ip":itemdict["$ip"] if "$ip" in itemdict else None,
        #     "ip_city":ip_city,
        #     "ip_asn":ip_asn,
        #     "url":None,
        #     "referrer":itemdict["$referrer"] if "$referrer" in itemdict else None,
        #     "remark":'production',
        #     "ua_platform":itemdict["$lib"] if "$lib" in itemdict else None,
        #     "ua_browser":itemdict["$browser"] if "$browser" in itemdict else None,
        #     "ua_version":itemdict["$browser_version"] if "$browser_version" in itemdict else None,
        #     "ua_language":None,
        #     "ip_is_good":ip_is_good,
        #     "ip_asn_is_good":ip_asn_is_good,
        #     "created_at":created_at})
        #   # insert_data
        # p.close()
        # p.join()
        os.remove(pkl)
コード例 #4
0
def get_data():
    remark = request.args.get(
        'remark') if 'remark' in request.args else 'normal'
    project = request.args.get('project')
    User_Agent = request.headers.get(
        'User-Agent'
    )  #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
    Host = request.headers.get('Host')  #: 10.16.5.241:5000
    Connection = request.headers.get('Connection')  #: keep-alive
    Pragma = request.headers.get('Pragma')  #: no-cache
    Cache_Control = request.headers.get('Cache-Control')  #: no-cache
    Accept = request.headers.get('Accept')[0:254] if request.headers.get(
        'Accept') else None  #: image/webp,image/apng,image/*,*/*;q=0.8
    Accept_Encoding = request.headers.get(
        'Accept-Encoding')[0:254] if request.headers.get(
            'Accept-Encoding') else None  #: gzip, deflate
    Accept_Language = request.headers.get(
        'Accept-Language')[0:254] if request.headers.get(
            'Accept-Language') else None  #: zh-CN,zh;q=0.9
    ua_platform = request.user_agent.platform  #客户端操作系统
    ua_browser = request.user_agent.browser  #客户端的浏览器
    ua_version = request.user_agent.version  #客户端浏览器的版本
    ua_language = request.user_agent.language  #客户端浏览器的语言
    ext = request.args.get('ext')
    url = request.url
    # ip = '124.115.214.179' #测试西安bug
    # ip = '36.5.99.68' #测试安徽bug
    if request.headers.get('X-Forwarded-For') is None:
        ip = request.remote_addr  #服务器直接暴露
    else:
        ip = request.headers.get('X-Forwarded-For')  #获取SLB真实地址
    ip_city, ip_is_good = get_addr(ip)
    ip_asn, ip_asn_is_good = get_asn(ip)
    if ip_is_good == 0:
        ip_city = '{}'
    if ip_asn_is_good == 0:
        ip_asn = '{}'
    referrer = request.referrer
    if request.method == 'POST':
        # print(request.form.get())
        if 'data_list' in request.form:
            data_list = request.form.get('data_list')
            de64 = base64.b64decode(
                urllib.parse.unquote(data_list).encode('utf-8'))
            try:
                data_decodes = json.loads(gzip.decompress(de64))
            except:
                data_decodes = json.loads(de64)
            for data_decode in data_decodes:
                insert_data(project=project,
                            data_decode=data_decode,
                            User_Agent=User_Agent,
                            Host=Host,
                            Connection=Connection,
                            Pragma=Pragma,
                            Cache_Control=Cache_Control,
                            Accept=Accept,
                            Accept_Encoding=Accept_Encoding,
                            Accept_Language=Accept_Language,
                            ip=ip,
                            ip_city=ip_city,
                            ip_asn=ip_asn,
                            url=url,
                            referrer=referrer,
                            remark=remark,
                            ua_platform=ua_platform,
                            ua_browser=ua_browser,
                            ua_version=ua_version,
                            ua_language=ua_language,
                            ip_is_good=ip_is_good,
                            ip_asn_is_good=ip_asn_is_good)
        elif 'data' in request.form:
            # print(request.cookies)
            data = request.form.get('data')
            de64 = base64.b64decode(urllib.parse.unquote(data).encode('utf-8'))
            try:
                data_decode = json.loads(gzip.decompress(de64))
            except:
                data_decode = json.loads(de64)
            insert_data(project=project,
                        data_decode=data_decode,
                        User_Agent=User_Agent,
                        Host=Host,
                        Connection=Connection,
                        Pragma=Pragma,
                        Cache_Control=Cache_Control,
                        Accept=Accept,
                        Accept_Encoding=Accept_Encoding,
                        Accept_Language=Accept_Language,
                        ip=ip,
                        ip_city=ip_city,
                        ip_asn=ip_asn,
                        url=url,
                        referrer=referrer,
                        remark=remark,
                        ua_platform=ua_platform,
                        ua_browser=ua_browser,
                        ua_version=ua_version,
                        ua_language=ua_language,
                        ip_is_good=ip_is_good,
                        ip_asn_is_good=ip_asn_is_good)
        else:
            write_to_log(filename='api',
                         defname='get_datas',
                         result=str(request.form))
            # print(request.form)
    elif request.method == 'GET':
        # try:
        if 'data' in request.args:
            data = request.args.get('data')
            de64 = base64.b64decode(urllib.parse.unquote(data).encode('utf-8'))
            try:
                data_decode = json.loads(gzip.decompress(de64))
            except:
                data_decode = json.loads(de64)
            insert_data(project=project,
                        data_decode=data_decode,
                        User_Agent=User_Agent,
                        Host=Host,
                        Connection=Connection,
                        Pragma=Pragma,
                        Cache_Control=Cache_Control,
                        Accept=Accept,
                        Accept_Encoding=Accept_Encoding,
                        Accept_Language=Accept_Language,
                        ip=ip,
                        ip_city=ip_city,
                        ip_asn=ip_asn,
                        url=url,
                        referrer=referrer,
                        remark=remark,
                        ua_platform=ua_platform,
                        ua_browser=ua_browser,
                        ua_version=ua_version,
                        ua_language=ua_language,
                        ip_is_good=ip_is_good,
                        ip_asn_is_good=ip_asn_is_good)
        else:
            write_to_log(filename='api', defname='get_datas', result=url)
    else:
        write_to_log(filename='api',
                     defname='get_datas',
                     result=str(request.method) + url)
    bitimage1 = os.path.join('image', '43byte.gif')
    with open(bitimage1, 'rb') as f:
        returnimage = f.read()
    return Response(returnimage, mimetype="image/gif")
コード例 #5
0
ファイル: url_tools.py プロジェクト: zhouxm/ghost_sa
def get_req_info():
    remark = request.args.get('remark', 'normal')
    User_Agent = request.headers.get(
        'User-Agent'
    )[0:767] if request.headers.get(
        'User-Agent'
    ) else None  #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
    if get_url_params('user_agent'):
        User_Agent = get_url_params('user_agent')
    if User_Agent and User_Agent != '' and any(
        [pt in User_Agent.lower() for pt in admin.bot_list]):
        remark = 'spider'
    Host = request.headers.get('Host')  #: 10.16.5.241:5000
    if get_url_params('host'):
        Host = get_url_params('host')
    Connection = request.headers.get('Connection')  #: keep-alive
    Pragma = request.headers.get('Pragma')  #: no-cache
    Cache_Control = request.headers.get('Cache-Control')  #: no-cache
    Accept = request.headers.get('Accept')[0:254] if request.headers.get(
        'Accept') else None  #: image/webp,image/apng,image/*,*/*;q=0.8
    Accept_Encoding = request.headers.get(
        'Accept-Encoding')[0:254] if request.headers.get(
            'Accept-Encoding') else None  #: gzip, deflate
    Accept_Language = request.headers.get(
        'Accept-Language')[0:254] if request.headers.get(
            'Accept-Language') else None  #: zh-CN,zh;q=0.9
    ua_platform = request.user_agent.platform  #客户端操作系统
    ua_browser = request.user_agent.browser  #客户端的浏览器
    ua_version = request.user_agent.version  #客户端浏览器的版本
    ua_language = request.user_agent.language  #客户端浏览器的语言
    url = request.url
    if get_url_params('request_uri'):
        url = get_url_params('request_uri')
    if get_url_params('http_x_forward_for') and get_url_params(
            'http_x_forward_for') != '' and get_url_params(
                'http_x_forward_for').count('.') == 3:
        ip = get_url_params('http_x_forward_for')
    elif get_url_params('remote_addr') and get_url_params(
            'remote_addr') != '' and get_url_params('remote_addr').count(
                '.') == 3:
        ip = get_url_params('remote_addr')
    elif get_url_params('ip') and get_url_params(
            'ip') != '' and get_url_params('ip').count('.') == 3:
        ip = get_url_params('ip')
    elif request.headers.get('X-Forwarded-For'):
        ip = request.headers.get('X-Forwarded-For')  #获取SLB真实地址
    else:
        ip = request.remote_addr  #服务器直接暴露
    # ip = '124.115.214.179' #测试西安bug
    # ip = '36.5.99.68' #测试安徽bug
    ip_city, ip_is_good = get_addr(ip)
    ip_asn, ip_asn_is_good = get_asn(ip)
    referrer = request.referrer[0:2047] if request.referrer else None
    if get_url_params('http_referrer') and get_url_params(
            'http_referrer') != '$http_referrer':
        referrer = get_url_params('http_referrer')[0:2047]
    elif get_url_params('http_referer') and get_url_params(
            'http_referer') != '$http_referer':
        referrer = get_url_params('http_referer')[0:2047]
    return {
        'remark': remark,
        'User_Agent': User_Agent,
        'Host': Host,
        'Connection': Connection,
        'Pragma': Pragma,
        'Cache_Control': Cache_Control,
        'Accept': Accept,
        'Accept_Encoding': Accept_Encoding,
        'Accept_Language': Accept_Language,
        'ua_platform': ua_platform,
        'ua_browser': ua_browser,
        'ua_version': ua_version,
        'ua_language': ua_language,
        'url': url,
        'ip': ip,
        'ip_city': ip_city,
        'ip_is_good': ip_is_good,
        'ip_asn': ip_asn,
        'ip_asn_is_good': ip_asn_is_good,
        'referrer': referrer
    }
コード例 #6
0
ファイル: api.py プロジェクト: gaoyang-projects/ghost_sa
def installation_track():
    start_time = time.time()
    project = request.args.get('project')
    User_Agent = request.headers.get(
        'User-Agent'
    )  #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
    Host = request.headers.get('Host')  #: 10.16.5.241:5000
    Connection = request.headers.get('Connection')  #: keep-alive
    Pragma = request.headers.get('Pragma')  #: no-cache
    Cache_Control = request.headers.get('Cache-Control')  #: no-cache
    Accept = request.headers.get('Accept')[0:254] if request.headers.get(
        'Accept') else None  #: image/webp,image/apng,image/*,*/*;q=0.8
    Accept_Encoding = request.headers.get(
        'Accept-Encoding')[0:254] if request.headers.get(
            'Accept-Encoding') else None  #: gzip, deflate
    remark = request.args.get(
        'remark') if 'remark' in request.args else 'normal'
    Accept_Language = request.headers.get(
        'Accept-Language')[0:254] if request.headers.get(
            'Accept-Language') else None  #: zh-CN,zh;q=0.9
    ua_platform = request.user_agent.platform  #客户端操作系统
    ua_browser = request.user_agent.browser  #客户端的浏览器
    ua_version = request.user_agent.version  #客户端浏览器的版本
    ua_language = request.user_agent.language  #客户端浏览器的语言
    ext = request.args.get('ext')
    url = request.url
    args = request.args.to_dict(request.args)
    # ip = '124.115.214.179' #测试西安bug
    # ip = '36.5.99.68' #测试安徽bug
    if 'ip' in args:
        ip = args['ip']
    elif request.headers.get('X-Forwarded-For') is not None:
        ip = request.headers.get('X-Forwarded-For')  #获取SLB真实地址
    else:
        ip = request.remote_addr  #服务器直接暴露
    ip_city, ip_is_good = get_addr(ip)
    ip_asn, ip_asn_is_good = get_asn(ip)
    if ip_is_good == 0:
        ip_city = '{}'
    if ip_asn_is_good == 0:
        ip_asn = '{}'
    referrer = request.referrer
    try:
        insert_installation_track(project=project,
                                  data_decode=args,
                                  User_Agent=User_Agent,
                                  Host=Host,
                                  Connection=Connection,
                                  Pragma=Pragma,
                                  Cache_Control=Cache_Control,
                                  Accept=Accept,
                                  Accept_Encoding=Accept_Encoding,
                                  Accept_Language=Accept_Language,
                                  ip=ip,
                                  ip_city=ip_city,
                                  ip_asn=ip_asn,
                                  url=url,
                                  referrer=referrer,
                                  remark=remark,
                                  ua_platform=ua_platform,
                                  ua_browser=ua_browser,
                                  ua_version=ua_version,
                                  ua_language=ua_language,
                                  ip_is_good=ip_is_good,
                                  ip_asn_is_good=ip_asn_is_good)
        bitimage1 = os.path.join('image', '43byte.gif')
        with open(bitimage1, 'rb') as f:
            returnimage = f.read()
        return Response(returnimage, mimetype="image/gif")
    except Exception:
        error = traceback.format_exc()
        write_to_log(filename='api',
                     defname='installation_track',
                     result=error)
コード例 #7
0
ファイル: api.py プロジェクト: WangZhijian/ghost_sa
def check_exist_distinct_id():
  start_time = time.time()
  password = request.args.get('password')
  project = request.args.get('project')
  distinct_id = request.args.get('distinct_id')
  query_from = request.args.get('query_from')

  User_Agent = request.headers.get('User-Agent')[0:2047] if request.headers.get('User-Agent') else None #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
  Host = request.headers.get('Host') #: 10.16.5.241:5000
  Connection = request.headers.get('Connection')#: keep-alive
  Pragma = request.headers.get('Pragma')#: no-cache
  Cache_Control = request.headers.get('Cache-Control')#: no-cache
  Accept = request.headers.get('Accept')[0:254] if request.headers.get('Accept') else None#: image/webp,image/apng,image/*,*/*;q=0.8
  Accept_Encoding = request.headers.get('Accept-Encoding')[0:254] if request.headers.get('Accept-Encoding') else None #: gzip, deflate
  remark = request.args.get('remark') if 'remark' in request.args else 'normal'
  Accept_Language = request.headers.get('Accept-Language')[0:254] if request.headers.get('Accept-Language') else None#: zh-CN,zh;q=0.9
  ua_platform = request.user_agent.platform #客户端操作系统
  ua_browser = request.user_agent.browser #客户端的浏览器
  ua_version = request.user_agent.version #客户端浏览器的版本
  ua_language = request.user_agent.language #客户端浏览器的语言
  ext = request.args.get('ext')
  url = request.url
  args = request.args.to_dict(request.args)
  data = {"properties":args}
  # ip = '124.115.214.179' #测试西安bug
  # ip = '36.5.99.68' #测试安徽bug
  if  'ip' in args and len(args['ip']) - len( args['ip'].replace('.','') ) == 3:#判断IP里是否存在IP地址
    ip = args['ip']
  elif request.headers.get('X-Forwarded-For') is not None:
    ip = request.headers.get('X-Forwarded-For') #获取SLB真实地址
  else:
    ip = request.remote_addr#服务器直接暴露
  ip_city,ip_is_good = get_addr(ip)
  ip_asn,ip_asn_is_good = get_asn(ip)
  if ip_is_good ==0:
    ip_city = '{}'
  if ip_asn_is_good ==0:
    ip_asn = '{}'
  referrer = request.referrer

  if password == admin.admin_password and project and distinct_id and query_from:#只有正确的密码才能触发动作
    try:
      if ',' in distinct_id:
        #兼容七麦多个id一次性递入,返回格式兼容七麦
        distinct_id_list = distinct_id.split(',')
        returnjson = {'result':'success','results_count':len(distinct_id_list),'query_from':query_from}
        for distinct_id in distinct_id_list:
          results_count= distinct_id_query(distinct_id=distinct_id,project=project)
          returnjson[distinct_id] = results_count
        time_cost = time.time() - start_time
        returnjson['timecost'] = round(time_cost,4)
        data['returnjson'] = returnjson
        insert_event(table=project,alljson=json.dumps(data),track_id=0,distinct_id=query_from,lib='ghost_sa',event='check_exist',type_1='ghost_sa_func',User_Agent=User_Agent,Host=Host,Connection=Connection,Pragma=Pragma,Cache_Control=Cache_Control,Accept=Accept,Accept_Encoding=Accept_Encoding,Accept_Language=Accept_Language,ip=ip,ip_city=ip_city,ip_asn=ip_asn,url=url,referrer=referrer,remark=remark,ua_platform=ua_platform,ua_browser=ua_browser,ua_version=ua_version,ua_language=ua_language,created_at=start_time)
        if admin.use_properties is True:
          properties_key = []
          for keys in data.keys():
            properties_key.append(keys)
          insert_properties(project=project,lib='ghost_sa',remark=remark,event='check_exist',properties=json.dumps(properties_key),properties_len=len(data.keys()),created_at=start_time,updated_at=start_time)
        return jsonify(returnjson)
      else:
        #正常一条一递,兼容有米和七麦的标准
        results_count= distinct_id_query(distinct_id=distinct_id,project=project)
        # key=['distinct_id','event','type','all_json','host','user_agent','ip','url','remark','created_at']
        if results_count== 0 :
          row = {}
          # pending_result.append(dict(zip(key,item)))
          time_cost = time.time() - start_time
          # returnjson = {'result':'success','results_count':results_count,'code':0,'msg':'not_exists'}
          returnjson = {'result':'success','results_count':results_count,'timecost':round(time_cost,4),'code':0,'msg':'not_exists','query_from':query_from}
          returnjson[distinct_id] = results_count
          data['returnjson'] = returnjson
          insert_event(table=project,alljson=json.dumps(data),track_id=0,distinct_id=query_from,lib='ghost_sa',event='check_exist',type_1='ghost_sa_func',User_Agent=User_Agent,Host=Host,Connection=Connection,Pragma=Pragma,Cache_Control=Cache_Control,Accept=Accept,Accept_Encoding=Accept_Encoding,Accept_Language=Accept_Language,ip=ip,ip_city=ip_city,ip_asn=ip_asn,url=url,referrer=referrer,remark=remark,ua_platform=ua_platform,ua_browser=ua_browser,ua_version=ua_version,ua_language=ua_language,created_at=start_time)
          if admin.use_properties is True:
            properties_key = []
            for keys in data.keys():
              properties_key.append(keys)
            insert_properties(project=project,lib='ghost_sa',remark=remark,event='check_exist',properties=json.dumps(properties_key),properties_len=len(data.keys()),created_at=start_time,updated_at=start_time)
          return jsonify(returnjson)
        time_cost = time.time() - start_time
        returnjson = {'result':'success','results_count':results_count,'timecost':round(time_cost,4),'code':-1,'msg':'exists','query_from':query_from}
        returnjson[distinct_id] = results_count
        # returnjson = {'result':'success','results_count':results_count,'code':-1,'msg':'exists'}
        data['returnjson'] = returnjson
        insert_event(table=project,alljson=json.dumps(data),track_id=0,distinct_id=query_from,lib='ghost_sa',event='check_exist',type_1='ghost_sa_func',User_Agent=User_Agent,Host=Host,Connection=Connection,Pragma=Pragma,Cache_Control=Cache_Control,Accept=Accept,Accept_Encoding=Accept_Encoding,Accept_Language=Accept_Language,ip=ip,ip_city=ip_city,ip_asn=ip_asn,url=url,referrer=referrer,remark=remark,ua_platform=ua_platform,ua_browser=ua_browser,ua_version=ua_version,ua_language=ua_language,created_at=start_time)
        if admin.use_properties is True:
          properties_key = []
          for keys in data.keys():
            properties_key.append(keys)
          insert_properties(project=project,lib='ghost_sa',remark=remark,event='check_exist',properties=json.dumps(properties_key),properties_len=len(data.keys()),created_at=start_time,updated_at=start_time)
        return jsonify(returnjson)
    except Exception:
      error = traceback.format_exc()
      write_to_log(filename='api',defname='check_exist_distinct_id',result=error)
  else:
    return jsonify('参数不正确')
コード例 #8
0
ファイル: api.py プロジェクト: WangZhijian/ghost_sa
def installation_track():
  start_time = time.time()
  project = request.args.get('project')
  if project:
    User_Agent = request.headers.get('User-Agent') #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
    Host = request.headers.get('Host') #: 10.16.5.241:5000
    Connection = request.headers.get('Connection')#: keep-alive
    Pragma = request.headers.get('Pragma')#: no-cache
    Cache_Control = request.headers.get('Cache-Control')#: no-cache
    Accept = request.headers.get('Accept')[0:254] if request.headers.get('Accept') else None#: image/webp,image/apng,image/*,*/*;q=0.8
    Accept_Encoding = request.headers.get('Accept-Encoding')[0:254] if request.headers.get('Accept-Encoding') else None #: gzip, deflate
    remark = request.args.get('remark') if 'remark' in request.args else 'normal'
    Accept_Language = request.headers.get('Accept-Language')[0:254] if request.headers.get('Accept-Language') else None#: zh-CN,zh;q=0.9
    ua_platform = request.user_agent.platform #客户端操作系统
    ua_browser = request.user_agent.browser #客户端的浏览器
    ua_version = request.user_agent.version #客户端浏览器的版本
    ua_language = request.user_agent.language #客户端浏览器的语言
    ext = request.args.get('ext')
    url = request.url
    args = request.args.to_dict(request.args)
    data = {"properties":args}
    # ip = '124.115.214.179' #测试西安bug
    # ip = '36.5.99.68' #测试安徽bug
    if  'ip' in args and len(args['ip']) - len( args['ip'].replace('.','') ) == 3:#判断IP里是否存在IP地址
      ip = args['ip']
    elif request.headers.get('X-Forwarded-For') is not None:
      ip = request.headers.get('X-Forwarded-For') #获取SLB真实地址
    else:
      ip = request.remote_addr#服务器直接暴露
    ip_city,ip_is_good = get_addr(ip)
    ip_asn,ip_asn_is_good = get_asn(ip)
    if ip_is_good ==0:
      ip_city = '{}'
    if ip_asn_is_good ==0:
      ip_asn = '{}'
    referrer = request.referrer
    try:
      if 'properties' in data and 'is_offerwall' in  data['properties'] and data['properties']['is_offerwall']=='1':
        count_event,count_user,time_cost = insert_installation_track(project=project,data_decode=data,User_Agent=User_Agent,Host=Host,Connection=Connection,Pragma=Pragma, Cache_Control=Cache_Control, Accept=Accept, Accept_Encoding=Accept_Encoding, Accept_Language=Accept_Language, ip=ip, ip_city=ip_city,ip_asn=ip_asn, url=url, referrer=referrer, remark=remark, ua_platform=ua_platform, ua_browser=ua_browser, ua_version=ua_version, ua_language=ua_language, ip_is_good=ip_is_good, ip_asn_is_good=ip_asn_is_good)
        if count_event and count_event>0 or count_user and count_user>0:
          code = 0 #有米标准
          msg = "success" #有米标准
          result = 1 #七麦标准
          error = '成功' #七麦标准
        else:
          code = -1 #有米标准
          msg = 'failed' #有米标准
          result = 0 #七麦标准
          error = '没有记录插入' #七麦标准
        returnjson = {'count_event':count_event,'count_user':count_user,'timecost':round(time_cost,4),'code':code,'msg':msg,'args':args,'result':result,'error':error}
        return jsonify(returnjson)
      else:
        insert_installation_track(project=project,data_decode=data,User_Agent=User_Agent,Host=Host,Connection=Connection,Pragma=Pragma, Cache_Control=Cache_Control, Accept=Accept, Accept_Encoding=Accept_Encoding, Accept_Language=Accept_Language, ip=ip, ip_city=ip_city,ip_asn=ip_asn, url=url, referrer=referrer, remark=remark, ua_platform=ua_platform, ua_browser=ua_browser, ua_version=ua_version, ua_language=ua_language, ip_is_good=ip_is_good, ip_asn_is_good=ip_asn_is_good)
        bitimage1 = os.path.join('image','43byte.gif')
        with open(bitimage1, 'rb') as f:
          returnimage = f.read()
        return Response(returnimage, mimetype="image/gif")
    except Exception:
      error = traceback.format_exc()
      write_to_log(filename='api',defname='installation_track',result=error)
  else:
    bitimage1 = os.path.join('image','43byte.gif')
    with open(bitimage1, 'rb') as f:
      returnimage = f.read()
    return Response(returnimage, mimetype="image/gif")