示例#1
0
def get_domain_log(request, user):
    """获取域名日志"""
    start_time = request.POST.get('start_time', 0)
    end_time = request.POST.get('end_time', 0)
    domain = request.POST.get('domain', '')

    msg = ''
    result_list = []
    pagination = {}

    try:
        msg, start_time, end_time = handle_req_time(start_time, end_time)
        if msg:
            assert False

        body = {
            'user_id': user.id,
            'start_time': start_time,
            'end_time': end_time,
            'domain': domain,
        }
        api_res = APIUrl.post_link('cdn_domain_log', body)
        return_code = api_res.get('return_code', 0)

        if return_code != 0:
            msg = af.PARAME_ERROR
            assert False

        result_list = api_res.get('result', [])

        all_log_list = []
        for channel in result_list:

            url_info = urlparse(channel)

            protocol = url_info.scheme

            log_list = result_list[channel]
            for log in log_list:
                # print(log['time'])
                """2019 10 14 20"""
                time_str = '%s-%s-%s %s:%s' % (
                    log['time'][:4], log['time'][4:6], log['time'][6:8],
                    log['time'][8:10], '00')

                log['time'] = time_str
                log['protocol'] = protocol

                all_log_list.append(log)

        result_list = sorted(all_log_list,
                             key=lambda x: (x['time'], x['protocol']))

        msg, result_list, pagination = data_pagination(request, result_list)
    except AssertionError:
        pass

    return msg, result_list, pagination
示例#2
0
def admin_get_parent_list(request):
    """管理员获取父账号列表"""
    msg = ''
    status = False

    res = {'status': status, 'msg': msg}

    groups = Group.objects.filter(name=GroupProfile.CUSTOMER)
    group_ids = [i.id for i in groups]
    msg, user_list = _get_user_list(request, group_ids)
    if msg:
        res['msg'] = _(msg)
        return json_response(res)

    if user_list:
        user_type = request.POST.get('user_type', '')

        if user_type == 'is_parent':
            user_list = user_list.filter(is_parent=True)
        elif user_type == 'is_agent':
            user_list = user_list.filter(is_agent=True)

    user_list = user_list.filter().exclude(
        is_child=True).order_by('is_active').order_by('-id')

    check_msg, user_list, pagination = data_pagination(request, user_list)

    if check_msg:
        res['msg'] = _(check_msg)
        return json_response(res)

    user_dict_list = []

    for u in user_list:

        user_dict = {
            'id': u.id,
            'username': u.username,
            'user_type': u.type_name,
            'is_active': u.is_active
        }
        user_dict_list.append(user_dict)

    status = True

    res['status'] = status
    res['user_list'] = user_dict_list
    res['page_info'] = pagination

    return json_response(res)
示例#3
0
def user_domain_refresh_status(request, user):
    """用户刷新查询"""

    start_time = request.POST.get('start_time', 0)
    end_time = request.POST.get('end_time', 0)
    url = request.POST.get('url', '')
    refresh_type = request.POST.get('refresh_type', '')
    refresh_status = request.POST.get('refresh_status', '')

    msg = ''
    result_list = []
    pagination = {}
    try:
        start_time = int_check(start_time)
        if start_time is None:
            msg = af.PARAME_ERROR
            assert False

        end_time = int_check(end_time)
        if end_time is None:
            msg = af.PARAME_ERROR
            assert False

        body = {
            'username': user.username,
            'start_time': start_time,
            'end_time': end_time,
            'url': url,
            'type': refresh_type,
            'status': refresh_status,
        }
        api_res = APIUrl.post_link('cdn_domain_refresh_status', body)
        return_code = api_res.get('return_code', 0)

        if return_code != 0:
            assert False

        result_list = api_res.get('result_list', [])

        check_msg, result_list, pagination = data_pagination(
            request, result_list)

        if check_msg:
            msg = check_msg
            assert False

    except AssertionError:
        pass

    return msg, result_list, pagination
示例#4
0
def admin_get_admin_list(request):
    """管理员获取管理员列表"""
    msg = ''
    status = False

    res = {'status': status, 'msg': msg}
    group_id = request.POST.get('groups_id', '')
    if group_id:
        groups = Group.objects.filter(id=group_id)
        group_ids = [i.id for i in groups]
    else:
        admin_group = GroupProfile.group_views()
        group_ids = [i['group_id'] for i in admin_group]

    msg, user_list = _get_user_list(request, group_ids)

    if msg:
        res['msg'] = _(msg)
        return json_response(res)

    check_msg, user_list, pagination = data_pagination(request, user_list)

    if check_msg:
        res['msg'] = _(check_msg)
        return json_response(res)

    user_dict_list = []

    for u in user_list:
        user_group = u.groups.first()

        user_dict = {
            'id': u.id,
            'username': u.username,
            'group_name': user_group.name if user_group else '',
            'is_active': u.is_active
        }
        user_dict_list.append(user_dict)

    status = True

    res['status'] = status
    res['user_list'] = user_dict_list
    res['page_info'] = pagination

    return json_response(res)
示例#5
0
def parent_get_child_list(request):
    """父账号查看子账号列表"""
    msg = ''
    status = False

    res = {
        'status': status,
        'msg': msg
    }

    user = request.user

    username = request.POST.get('username', '').strip()

    check_filter = {
        'parent_username': user.username
    }

    if username:
        check_filter['username'] = username

    user_list = UserProfile.objects.filter(**check_filter)

    check_msg, user_list, pagination = data_pagination(request, user_list)

    if check_msg:
        res['msg'] = _(check_msg)
        return json_response(res)

    user_dict_list = []

    for u in user_list:

        user_perm_strategy = UserPermStrategy.objects.filter(user=u).first()

        user_perm_strategy_dict_list = []

        if user_perm_strategy:
            for i in user_perm_strategy.perm_strategy.all():
                perm_strategy_dict = {
                    'id': i.id,
                    'name': i.name,
                    'remark': i.remark
                }
                user_perm_strategy_dict_list.append(perm_strategy_dict)

        user_dict = {
            'id': u.id,
            'username': u.username,
            'create_time': str(u.date_joined)[:19],
            'remark': u.remark,
            'user_perm_strategy': user_perm_strategy_dict_list
        }
        user_dict_list.append(user_dict)

    status = True

    res['status'] = status
    res['user_list'] = user_dict_list
    res['page_info'] = pagination

    return json_response(res)
示例#6
0
def user_get_waf_self_rule(request):

    msg = ''
    status = False

    res = {
        'status': status,
        'msg': msg
    }

    provider = 'QINGSONG'

    domain_id = request.POST.get('domain_id', '')

    check_rule_name = request.POST.get('rule_name', '')
    check_rule_status = request.POST.get('rule_status', '')

    rule_list = []

    try:
        domain_obj = Domain.objects.filter(id=domain_id).first()
        if not domain_obj:
            msg = af.DOMAIN_NOT_EXIST
            assert False

        body = {
            'channel': '%s://%s' % (domain_obj.protocol, domain_obj.domain)
        }
        api_res = APIUrl.post_link('get_waf_self_rule', body)

        if api_res[provider]['return_code'] == 0:
            api_res = api_res[provider]

            rule_dict_list = api_res.get('data', [])

            for i in rule_dict_list.get('rule_list', []):
                rule_name = i.get('name', '')
                rule_status = str(i.get('enable', '0'))

                if check_rule_name and check_rule_name not in rule_name:
                    continue

                if check_rule_status and rule_status != check_rule_status:
                    continue

                rule_list.append(i)

            check_msg, rule_list, pagination = data_pagination(
                request, rule_list)

            if check_msg:
                res['msg'] = _(check_msg)
                return json_response(res)

            res['page_info'] = pagination

            status = True

    except AssertionError:
        res['msg'] = _(msg)

    res['rule_list'] = rule_list
    res['status'] = status

    return json_response(res)
示例#7
0
def client_cert_list(request):
    """客户端证书列表"""
    msg = ''
    status = False
    user = request.user

    res = {
        'status': status,
        'msg': msg
    }

    cert_name = request.POST.get('cert_name', '')
    status = request.POST.get('status', '[]')

    # cert_name = 'xz_test_0009'
    # status = 1

    try:
        status_list = handle_list(status, dec_int=True)

        if not status_list:
            status_list = [
                CertConf.CERT_CONDUCT, CertConf.CERT_SUCCESS,
                CertConf.CERT_FAIL, CertConf.CERT_TIMEOUT,
                CertConf.CERT_UPDATE
            ]

        body = {
            'user_id': user.id,
            'status': status_list,
        }

        if cert_name:
            body['cert_name'] = cert_name

        api_res = APIUrl.post_link('ssl_cert_query', body)
        print(11111111, api_res)
        return_code = api_res.get('return_code', 0)

        if return_code != 0:
            assert False

        result_list = api_res.get('cert_list', [])

        cert_list = []
        for cert_info in result_list:

            # cert_info['cert_from'] = CertConf.cert_from_name(
            #     cert_info['cert_from'])
            #
            # cert_info['status'] = CertConf.cert_status_name(
            #     cert_info['status'])

            cert_list.append(cert_info)

        result_list = sorted(
            cert_list, key=lambda x: x['create_time'], reverse=True)

        check_msg, result_list, pagination = data_pagination(
            request, result_list)

        if check_msg:
            res['msg'] = check_msg
            return json_response(res)

        res['cert_list'] = result_list
        res['page_info'] = pagination

        status = True
    except Exception as e:
        print(e)
        res['msg'] = _(msg)

    res['status'] = status

    print(res)

    return json_response(res)
示例#8
0
def admin_get_parent_cms_list(request):
    """获取cms用户信息"""
    msg = ''
    status = False

    res = {'status': status, 'msg': msg}

    username = request.POST.get('username', '')
    cms_username = request.POST.get('cms_username', '')

    try:

        parent_list = UserProfile.objects.all()

        if username:
            parent_list = parent_list.filter(username=username)

        username_list = [i.username for i in parent_list]

        body = {'username_list': username_list, 'cms_username': cms_username}

        api_res = APIUrl.post_link('user_query', body)
        user_query = api_res.get('user_query', {})

        username_list = list(user_query.keys())

        user_list = UserProfile.objects.filter(username__in=username_list)

        user_dict_list = []

        for user in user_list:
            username = user.username

            cms_username = user_query[username].get('cms_username', '')
            if not cms_username:
                continue

            user_dict = {
                'id': user.id,
                'username': username,
                'company': user.company,
                'cms_username': cms_username
            }
            user_dict_list.append(user_dict)

        check_msg, user_list, pagination = data_pagination(
            request, user_dict_list)

        if check_msg:
            res['msg'] = check_msg
            return json_response(res)

        status = True

        res['status'] = status
        res['user_list'] = user_list
        res['page_info'] = pagination

    except AssertionError:
        res['msg'] = _(msg)

    res['status'] = status

    return json_response(res)
示例#9
0
def get_domain_list(request, domain_query, cdn_type, domain_status):
    """获取域名列表"""

    check_domain = []
    channel_list = []
    domain_obj_list = []
    for i in domain_query:
        domain = i.domain
        if domain not in check_domain:
            check_domain.append(domain)
            channel_list.append(i.channel)
            domain_obj_list.append(i)

    body = {
        'channel_list': channel_list,
        'cdn_type': cdn_type,
        'domain_status': domain_status
    }

    api_res = APIUrl.post_link('cdn_domain_query', body)

    api_domain_query = api_res.get('domain_query', {})

    domain_dict_list = []

    domain_dict_query = {}
    for i in domain_obj_list:
        domain = i.domain
        channel = i.channel

        api_domain_info = api_domain_query.get(channel, {})

        if not api_domain_info:
            continue

        if 'modify_time' in api_domain_info:
            api_domain_info.pop('modify_time')
        if 'task_id' in api_domain_info:
            api_domain_info.pop('task_id')
        if 'channel_id' in api_domain_info:
            api_domain_info.pop('channel_id')
        if 'dis_cname' in api_domain_info:
            api_domain_info.pop('dis_cname')
        if 'channel' in api_domain_info:
            api_domain_info.pop('channel')

        cdn_type = CDNConf.get_cdn_type_name(api_domain_info['cdn_type'])
        status = CDNConf.get_status_name(api_domain_info['status'])

        api_domain_info['cdn_type'] = cdn_type
        api_domain_info['status'] = status
        api_domain_info['id'] = i.id
        api_domain_info['username'] = i.user.username
        api_domain_info['user_id'] = i.user.id

        if domain not in domain_dict_query:
            domain_dict_query[domain] = api_domain_info
        else:
            old_progress = domain_dict_query[domain]['task_progress']
            now_progress = api_domain_info['task_progress']

            if now_progress < old_progress:
                domain_dict_query[domain] = api_domain_info

    for i in domain_dict_query:
        domain_dict_list.append(domain_dict_query[i])

    domain_dict_list = sorted(domain_dict_list,
                              key=lambda x: x['id'],
                              reverse=True)

    check_msg, domain_dict_list, pagination = data_pagination(
        request, domain_dict_list)

    return check_msg, domain_dict_list, pagination
示例#10
0
def parent_get_sec_domain_list(request):

    msg = ''
    status = False

    res = {'status': status, 'msg': msg}

    url = request.POST.get('domain', '')

    url_info = urlparse(url)
    protocol = url_info.scheme
    netloc = url_info.netloc
    path = url_info.path

    try:
        user = request.user

        domain_query = Domain.objects.filter(user=user)

        if url:
            if protocol and netloc:
                domain_query = domain_query.filter(protocol=protocol,
                                                   domain=netloc)
            else:
                domain_query = domain_query.filter(domain__contains=path)

        domain_name_list = [i.domain for i in domain_query]

        body = {'domain_list': domain_name_list}

        api_res = APIUrl.post_link('domain_query', body)
        api_domain_query = api_res.get('domain_query', [])

        domain_dict_list = []
        for domain_obj in domain_query:

            url = '%s://%s' % (domain_obj.protocol, domain_obj.domain)

            if url not in api_domain_query:
                continue

            domain_info = api_domain_query.get(url, {})

            user_id = domain_info.get('user_id')
            user = UserProfile.objects.filter(id=user_id).first()

            # waf标记
            is_waf = 0
            waf_info = domain_info.get('waf', [])
            if waf_info:
                is_waf = 1

            domain_dict = {
                'username': user.username,
                'WAF': is_waf,
                'domain_id': domain_obj.id,
                'channel_name': url
            }
            if is_waf:
                #---get domain status info.
                '''
                    0:显示“开通waf”
                    1:显示“待审核”
                    2:显示“审核不通过”
                    3:显示“配置”+“统计”
                '''
                status_from_api = APIUrl.post_link('get_domain_status',
                                                   {'channel': url})
                if status_from_api['return_code'] == 0:
                    channel_status = status_from_api['data'].get('status', 0)
                    domain_dict['WAF'] = channel_status
                else:
                    print(
                        f'parent_get_sec_domain_list[get_domain_status api error.] channel: {url}'
                    )

            domain_dict_list.append(domain_dict)

        check_msg, domain_dict_list, pagination = data_pagination(
            request, domain_dict_list)

        if check_msg:
            res['msg'] = _(check_msg)
            return json_response(res)

        res['domain_list'] = domain_dict_list
        res['page_info'] = pagination

        status = True

    except Exception as e:
        res['msg'] = e

    res['status'] = status

    return json_response(res)
示例#11
0
def admin_sec_user_list(request):
    """安全用户列表"""
    msg = ''
    status = False

    res = {
        'status': status,
        'msg': msg
    }

    id_or_username = request.POST.get('id_or_username', '')
    cms_username = request.POST.get('cms_username', '')

    sec_product = Product.objects.filter(code='SECURITY').first()

    try:

        body = {
            'username_list': [],
            'cms_username': '',
            'return_type': 'is_list',
        }

        user_query = sec_product.user_product.all()

        if id_or_username:

            check_res = int_check(id_or_username)

            if check_res is None:
                user_query = user_query.filter(username=id_or_username)
            else:
                user_query = user_query.filter(id=check_res)

        body['username_list'] = [user.username for user in user_query]

        if cms_username:
            body['cms_username'] = cms_username

        api_res = APIUrl.post_link('user_query', body)
        api_user_query = api_res.get('user_query', {})

        cms_user_list = []
        for i in api_user_query:
            username = i.get('username', '')
            user_id = i.get('user_id', 0)
            cms_username = i.get('cms_username', '')
            if not cms_username:
                continue

            user = user_query.filter(id=user_id).first()
            if not user:
                continue

            domain_num = Domain.objects.filter(user=user).count()

            user_info = {
                'user_id': user_id,
                'username': username,
                'cms_username': cms_username,
                'domain_num': domain_num
            }

            cms_user_list.append(user_info)

        cms_user_list = sorted(
            cms_user_list, key=lambda x: x['user_id'], reverse=True)

        check_msg, sec_user_list, pagination = data_pagination(
            request, cms_user_list)

        if check_msg:
            res['msg'] = check_msg
            return json_response(res)

        res['sec_user_list'] = sec_user_list
        res['page_info'] = pagination

        status = True

    except AssertionError:
        res['msg'] = _(msg)

    res['status'] = status

    return json_response(res)
示例#12
0
def admin_get_sec_domain_list(request):
    """管理员查看安全域名"""
    msg = ''
    status = False

    res = {
        'status': status,
        'msg': msg
    }

    id_or_username = request.POST.get('id_or_username', '')
    cms_username = request.POST.get('cms_username', '')
    domain = request.POST.get('domain', '')

    try:
        body = {
            'username_list': [],
            'cms_username': '',
            'return_type': 'is_dict'
        }

        sec_product = Product.objects.filter(code='SECURITY').first()
        user_query = sec_product.user_product.all()

        if id_or_username:

            check_res = int_check(id_or_username)

            if check_res is None:
                user_query = user_query.filter(username=id_or_username)
            else:
                user_query = user_query.filter(id=check_res)

        if user_query:
            body['username_list'] = [user.username for user in user_query]

        if cms_username:
            body['cms_username'] = cms_username

        user_ids = []
        api_res = APIUrl.post_link('user_query', body)
        user_info_list = api_res.get('user_query', {})

        if user_info_list:
            user_ids = [
                user_info_list[i]['user_id'] for i in user_info_list]

        domain_query = Domain.objects.filter(user_id__in=user_ids)

        if domain:
            if domain.startswith('http'):
                protocol, record, domain = parseProtocolRecordDomain(domain)
                domain = f'{record}.{domain}'
            domain_query = Domain.objects.filter(domain__contains=domain)

        domain_query = domain_query.order_by('-id')

        domain_name_list = []
        if domain_query:
            domain_name_list = [i.domain for i in domain_query]

        body = {
            'domain_list': domain_name_list
        }
        api_res = APIUrl.post_link('domain_query', body)
        api_domain_query = api_res.get('domain_query', [])

        domain_dict_list = []
        for domain_obj in domain_query:

            url = '%s://%s' % (domain_obj.protocol, domain_obj.domain)

            if url not in api_domain_query:
                continue

            domain_info = api_domain_query.get(url, {})

            user_id = domain_info.get('user_id')
            user = UserProfile.objects.filter(id=user_id).first()
            username = user.username
            cms_username = ''
            if username in user_info_list:
                cms_username = user_info_list.get(
                    username, {}).get('cms_username', '')

            # waf 服务判断
            is_waf = 0
            strategy_waf = domain_info.get('waf', [])
            if strategy_waf:
                is_waf = 1

            domain_dict = {
                'user_id': user_id,
                'username': username,
                'WAF': is_waf,
                'domain_id': domain_obj.id,
                'channel_name': url,
                'cms_username': cms_username
            }
            if is_waf:
                #---get domain status info.
                '''
                    0:显示“开通waf”
                    1:显示“待审核”
                    2:显示“审核不通过”
                    3:显示“配置”+“统计”
                '''
                status_from_api = APIUrl.post_link('get_domain_status', {'channel': url})
                if status_from_api['return_code'] == 0:
                    channel_status = status_from_api['data'].get('status', 0)
                    domain_dict['WAF'] = channel_status
                else:
                    print(f'admin_get_sec_domain_list[get_domain_status api error.] channel: {url}')

            domain_dict_list.append(domain_dict)

        check_msg, domain_dict_list, pagination = data_pagination(
            request, domain_dict_list)

        if check_msg:
            res['msg'] = _(check_msg)
            return json_response(res)

        res['domain_list'] = domain_dict_list
        res['page_info'] = pagination

        status = True

    except Exception:
        print(traceback.format_exc())

    res['status'] = status

    return json_response(res)
示例#13
0
def admin_cdn_user_list(request):
    """cdn用户列表"""
    msg = ''
    status = False

    res = {'status': status, 'msg': msg}

    id_or_username = request.POST.get('id_or_username', '')
    cms_username = request.POST.get('cms_username', '')

    cdn_product = Product.objects.filter(code='CDN').first()

    try:

        body = {
            'username_list': [],
            'cms_username': '',
            'return_type': 'is_list',
        }

        user_query = cdn_product.user_product.all()

        if id_or_username:

            check_res = int_check(id_or_username)

            if check_res is None:
                user_query = user_query.filter(username=id_or_username)
            else:
                user_query = user_query.filter(id=check_res)

        cms_user_list = []
        if user_query:
            body['username_list'] = [user.username for user in user_query]

            if cms_username:
                body['cms_username'] = cms_username

            api_res = APIUrl.post_link('user_query', body)
            api_user_query = api_res.get('user_query', {})

            print(1111111, api_user_query)

            for i in api_user_query:
                username = i.get('username', '')
                user_id = i.get('user_id', 0)
                cms_username = i.get('cms_username', '')

                cdn_opt = i.get('cdn_opt', [])

                user_info = {
                    'user_id': user_id,
                    'username': username,
                    'cms_username': cms_username,
                    'cdn_opt': cdn_opt
                }

                cms_user_list.append(user_info)

            cms_user_list = sorted(cms_user_list,
                                   key=lambda x: x['user_id'],
                                   reverse=True)

        check_msg, cdn_user_list, pagination = data_pagination(
            request, cms_user_list)

        if check_msg:
            res['msg'] = check_msg
            return json_response(res)

        res['cdn_user_list'] = cdn_user_list
        res['page_info'] = pagination

        status = True

    except AssertionError:
        res['msg'] = _(msg)

    res['status'] = status

    return json_response(res)