Esempio n. 1
0
def host_get(hst=None):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    if hst:
        resp = host.get_by_id(hst).dict()
        if settings.app.demo_mode:
            utils.demo_set_cache(resp)
        return utils.jsonify(resp)

    hosts = []
    page = flask.request.args.get('page', None)
    page = int(page) if page else page

    for hst in host.iter_hosts_dict(page=page):
        hosts.append(hst)

    if page is not None:
        resp = {
            'page': page,
            'page_total': host.get_host_page_total(),
            'hosts': hosts,
        }
    else:
        resp = hosts

    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 2
0
def admin_get(admin_id=None):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    if not flask.g.administrator.super_user:
            return utils.jsonify({
                'error': REQUIRES_SUPER_USER,
                'error_msg': REQUIRES_SUPER_USER_MSG,
            }, 400)

    if admin_id:
        return utils.jsonify(auth.get_by_id(admin_id).dict())

    admins = []

    for admin in auth.iter_admins():
        admin = admin.dict()
        admin['audit'] = settings.app.auditing == ALL
        admins.append(admin)

    if settings.app.demo_mode:
        utils.demo_set_cache(admins)
    return utils.jsonify(admins)
Esempio n. 3
0
def link_get():
    if not settings.local.sub_plan or \
            'enterprise' not in settings.local.sub_plan:
        return flask.abort(404)

    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    page = flask.request.args.get('page', None)
    page = int(page) if page else page

    links = []
    for lnk in link.iter_links(page):
        links.append(lnk.dict())

    data = {
        'page': page,
        'page_total': link.get_page_total(),
        'links': links,
    }

    if settings.app.demo_mode:
        utils.demo_set_cache(data)
    return utils.jsonify(data)
Esempio n. 4
0
def server_host_get(server_id):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    hosts = []
    svr = server.get_by_id(server_id, fields=('_id', 'status',
        'replica_count', 'hosts', 'instances'))
    active_hosts = set([x['host_id'] for x in svr.instances])
    hosts_offline = svr.replica_count - len(active_hosts) > 0

    for hst in svr.iter_hosts(fields=('_id', 'name',
            'public_address', 'auto_public_address',
            'public_address6', 'auto_public_address6')):
        if svr.status == ONLINE and hst.id in active_hosts:
            status = ONLINE
        elif svr.status == ONLINE and hosts_offline:
            status = OFFLINE
        else:
            status = None

        hosts.append({
            'id': hst.id,
            'server': svr.id,
            'status': status,
            'name': hst.name,
            'address': hst.public_addr,
            'address6': hst.public_addr6,
        })

    if settings.app.demo_mode:
        utils.demo_set_cache(hosts)
    return utils.jsonify(hosts)
Esempio n. 5
0
def server_bandwidth_get(server_id, period):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    resp = server.bandwidth_get(server_id, period)
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 6
0
def host_usage_get(hst, period):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    hst = host.get_by_id(hst)
    resp = hst.usage.get_period(period)
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 7
0
def subscription_get():
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    subscription.update()
    resp = subscription.dict()
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 8
0
def settings_get():
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    response = flask.g.administrator.dict()
    response.update(_dict())

    if settings.app.demo_mode:
        utils.demo_set_cache(response)
    return utils.jsonify(response)
Esempio n. 9
0
def user_audit_get(org_id, user_id):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    org = organization.get_by_id(org_id)
    user = org.get_user(user_id)

    resp = user.get_audit_events()
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 10
0
def server_route_get(server_id):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    svr = server.get_by_id(server_id, fields=('_id', 'network', 'links',
        'network_start', 'network_end', 'routes', 'organizations', 'ipv6'))

    resp = svr.get_routes(include_server_links=True)
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 11
0
def server_link_output_get(server_id):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    resp = {
        'id': server_id,
        'output': server.output_link_get(server_id),
    }
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 12
0
def server_link_get(server_id):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    links = []
    svr = server.get_by_id(server_id, fields=('_id', 'status', 'links',
        'replica_count', 'instances'))
    if not svr:
        return flask.abort(404)
    hosts_offline = svr.replica_count - len(svr.instances) > 0

    if svr.links:
        link_use_local = {}
        link_server_ids = []

        for link in svr.links:
            link_server_id = link['server_id']
            link_use_local[link_server_id] = link['use_local_address']
            link_server_ids.append(link_server_id)

        spec = {
            '_id': {'$in': link_server_ids},
        }
        for link_svr in server.iter_servers(spec=spec, fields=(
                '_id', 'status', 'name', 'replica_count', 'instances')):
            link_hosts_offline = link_svr.replica_count - len(
                link_svr.instances) > 0
            if svr.status == ONLINE:
                if hosts_offline or link_hosts_offline:
                    status = OFFLINE
                elif link_svr.status == ONLINE:
                    status = ONLINE
                else:
                    status = OFFLINE
            else:
                status = None
            links.append({
                'id': link_svr.id,
                'server': svr.id,
                'status': status,
                'name': link_svr.name,
                'address': None,
                'use_local_address': link_use_local[link_svr.id],
            })

    if settings.app.demo_mode:
        utils.demo_set_cache(links)
    return utils.jsonify(links)
Esempio n. 13
0
def server_org_get(server_id):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    orgs = []
    svr = server.get_by_id(server_id, fields=('_id', 'organizations'))
    for org_doc in svr.get_org_fields(fields=('_id', 'name')):
        org_doc['id'] = org_doc.pop('_id')
        org_doc['server'] = svr.id
        orgs.append(org_doc)

    if settings.app.demo_mode:
        utils.demo_set_cache(orgs)
    return utils.jsonify(orgs)
Esempio n. 14
0
def admin_audit_get(admin_id):
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    if not flask.g.administrator.super_user:
        return utils.jsonify({
            'error': REQUIRES_SUPER_USER,
            'error_msg': REQUIRES_SUPER_USER_MSG,
        }, 400)

    admin = auth.get_by_id(admin_id)

    resp = admin.get_audit_events()
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 15
0
def link_location_get(link_id):
    if not settings.local.sub_plan or \
            'enterprise' not in settings.local.sub_plan:
        return flask.abort(404)

    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    lnk = link.get_by_id(link_id)
    if not lnk:
        return flask.abort(404)

    locations = []
    for location_dict in lnk.iter_locations_dict():
        locations.append(location_dict)

    if settings.app.demo_mode:
        utils.demo_set_cache(locations)
    return utils.jsonify(locations)
Esempio n. 16
0
def server_get(server_id=None):
    if server_id:
        if settings.app.demo_mode:
            resp = utils.demo_get_cache()
            if resp:
                return utils.jsonify(resp)

        resp = server.get_dict(server_id)
        if settings.app.demo_mode:
            utils.demo_set_cache(resp)
        return utils.jsonify(resp)

    servers = []
    page = flask.request.args.get('page', None)
    page = int(page) if page else page

    if settings.app.demo_mode:
        resp = utils.demo_get_cache(page)
        if resp:
            return utils.jsonify(resp)

    for svr in server.iter_servers_dict(page=page):
        servers.append(svr)

    if page is not None:
        resp = {
            'page': page,
            'page_total': server.get_server_page_total(),
            'servers': servers,
        }
    else:
        resp = servers

    if settings.app.demo_mode:
        utils.demo_set_cache(resp, page)
    return utils.jsonify(resp)
Esempio n. 17
0
def org_get(org_id=None):
    if org_id:
        if settings.app.demo_mode:
            resp = utils.demo_get_cache()
            if resp:
                return utils.jsonify(resp)

        resp = organization.get_by_id(org_id).dict()
        if settings.app.demo_mode:
            utils.demo_set_cache(resp)
        return utils.jsonify(resp)

    orgs = []
    page = flask.request.args.get('page', None)
    page = int(page) if page else page

    if settings.app.demo_mode:
        resp = utils.demo_get_cache(page)
        if resp:
            return utils.jsonify(resp)

    for org in organization.iter_orgs(page=page):
        orgs.append(org.dict())

    if page is not None:
        resp = {
            'page': page,
            'page_total': organization.get_org_page_total(),
            'organizations': orgs,
        }
    else:
        resp = orgs

    if settings.app.demo_mode:
        utils.demo_set_cache(resp, page)
    return utils.jsonify(resp)
Esempio n. 18
0
def status_get():
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    server_collection = mongo.get_collection('servers')
    clients_collection = mongo.get_collection('clients')
    host_collection = mongo.get_collection('hosts')
    org_collection = mongo.get_collection('organizations')

    users_online = len(clients_collection.distinct("user_id", {
        'type': CERT_CLIENT,
    }))

    response = server_collection.aggregate([
        {'$project': {
            '_id': True,
            'status': True,
        }},
        {'$group': {
            '_id': None,
            'server_count': {'$sum': 1},
            'servers_online': {'$sum': {'$cond': {
                'if': {'$eq': ['$status', ONLINE]},
                'then': 1,
                'else': 0,
            }}},
            'servers': {
                '$push': '$status',
            },
        }},
    ])

    val = None
    for val in response:
        break

    if val:
        server_count = val['server_count']
        servers_online = val['servers_online']
    else:
        server_count = 0
        servers_online = 0

    response = host_collection.aggregate([
        {'$project': {
            '_id': True,
            'status': True,
            'local_networks': True,
        }},
        {'$group': {
            '_id': None,
            'host_count': {'$sum': 1},
            'hosts_online': {'$sum': {'$cond': {
                'if': {'$eq': ['$status', ONLINE]},
                'then': 1,
                'else': 0,
            }}},
            'servers': {
                '$push': '$status',
            },
            'local_networks': {'$push':'$local_networks'},
        }},
    ])

    val = None
    for val in response:
        break

    local_networks = set()
    if val:
        host_count = val['host_count']
        hosts_online = val['hosts_online']

        for hst_networks in val['local_networks']:
            for network in hst_networks:
                local_networks.add(network)
    else:
        host_count = 0
        hosts_online = 0

    user_count = organization.get_user_count_multi()

    orgs_count = org_collection.find({
       'type': ORG_DEFAULT,
    }, {
        '_id': True,
    }).count()

    notification = settings.local.notification

    resp = {
        'org_count': orgs_count,
        'users_online': users_online,
        'user_count': user_count,
        'servers_online': servers_online,
        'server_count': server_count,
        'hosts_online': hosts_online,
        'host_count': host_count,
        'server_version': __version__,
        'current_host': settings.local.host_id,
        'public_ip': settings.local.public_ip,
        'local_networks': list(local_networks),
        'notification': notification,
    }
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 19
0
def status_get():
    if settings.app.demo_mode:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    server_collection = mongo.get_collection('servers')
    clients_collection = mongo.get_collection('clients')
    host_collection = mongo.get_collection('hosts')
    org_collection = mongo.get_collection('organizations')

    users_online = len(clients_collection.distinct("user_id", {
        'type': CERT_CLIENT,
    }))

    response = server_collection.aggregate([
        {'$project': {
            '_id': True,
            'status': True,
        }},
        {'$group': {
            '_id': None,
            'server_count': {'$sum': 1},
            'servers_online': {'$sum': {'$cond': {
                'if': {'$eq': ['$status', ONLINE]},
                'then': 1,
                'else': 0,
            }}},
            'servers': {
                '$push': '$status',
            },
        }},
    ])

    val = None
    for val in response:
        break

    if val:
        server_count = val['server_count']
        servers_online = val['servers_online']
    else:
        server_count = 0
        servers_online = 0

    response = host_collection.aggregate([
        {'$project': {
            '_id': True,
            'status': True,
            'local_networks': True,
        }},
        {'$group': {
            '_id': None,
            'host_count': {'$sum': 1},
            'hosts_online': {'$sum': {'$cond': {
                'if': {'$eq': ['$status', ONLINE]},
                'then': 1,
                'else': 0,
            }}},
            'servers': {
                '$push': '$status',
            },
            'local_networks': {'$push':'$local_networks'},
        }},
    ])

    val = None
    for val in response:
        break

    local_networks = set()
    if val:
        host_count = val['host_count']
        hosts_online = val['hosts_online']

        for hst_networks in val['local_networks']:
            for network in hst_networks:
                local_networks.add(network)
    else:
        host_count = 0
        hosts_online = 0

    user_count = organization.get_user_count_multi()

    orgs_count = org_collection.find({
       'type': ORG_DEFAULT,
    }, {
        '_id': True,
    }).count()

    if settings.local.openssl_heartbleed:
        notification = 'You are running an outdated version of openssl ' + \
            'containting the heartbleed bug. This could allow an attacker ' + \
            'to compromise your server. Please upgrade your openssl ' + \
            'package and restart the pritunl service.'
    else:
        notification = settings.local.notification

    resp = {
        'org_count': orgs_count,
        'users_online': users_online,
        'user_count': user_count,
        'servers_online': servers_online,
        'server_count': server_count,
        'hosts_online': hosts_online,
        'host_count': host_count,
        'server_version': __version__,
        'current_host': settings.local.host_id,
        'public_ip': settings.local.public_ip,
        'local_networks': list(local_networks),
        'notification': notification,
    }
    if settings.app.demo_mode:
        utils.demo_set_cache(resp)
    return utils.jsonify(resp)
Esempio n. 20
0
def user_get(org_id, user_id=None, page=None):
    if settings.app.demo_mode and user_id:
        resp = utils.demo_get_cache()
        if resp:
            return utils.jsonify(resp)

    org = organization.get_by_id(org_id)
    if not org:
        return flask.abort(404)

    if user_id:
        resp = org.get_user(user_id).dict()
        if settings.app.demo_mode:
            utils.demo_set_cache(resp)
        return utils.jsonify(resp)

    page = flask.request.args.get('page', page)
    page = int(page) if page else page
    search = flask.request.args.get('search', None)
    limit = int(flask.request.args.get('limit', settings.user.page_count))
    otp_auth = False
    dns_mapping = False
    server_count = 0
    servers = []

    if settings.app.demo_mode:
        resp = utils.demo_get_cache(page, search, limit)
        if resp:
            return utils.jsonify(resp)

    for svr in org.iter_servers(fields=('name', 'otp_auth', 'dns_mapping')):
        servers.append(svr)
        server_count += 1
        if svr.otp_auth:
            otp_auth = True
        if svr.dns_mapping:
            dns_mapping = True

    users = []
    users_id = []
    users_data = {}
    users_servers = {}
    fields = (
        'organization',
        'organization_name',
        'name',
        'email',
        'pin',
        'type',
        'auth_type',
        'otp_secret',
        'disabled',
        'bypass_secondary',
        'client_to_client',
        'dns_servers',
        'dns_suffix',
        'port_forwarding',
    )
    for usr in org.iter_users(page=page, search=search,
            search_limit=limit, fields=fields):
        users_id.append(usr.id)

        user_dict = usr.dict()
        user_dict['gravatar'] = settings.user.gravatar
        user_dict['audit'] = settings.app.auditing == ALL
        user_dict['status'] = False
        user_dict['sso'] = settings.app.sso
        user_dict['otp_auth'] = otp_auth
        if dns_mapping:
            user_dict['dns_mapping'] = ('%s.%s.vpn' % (
                usr.name.split('@')[0], org.name)).lower()
        else:
            user_dict['dns_mapping'] = None
        user_dict['network_links'] = []

        users_data[usr.id] = user_dict
        users_servers[usr.id] = {}

        server_data = []
        for svr in servers:
            data = {
                'id': svr.id,
                'name': svr.name,
                'status': False,
                'server_id': svr.id,
                'device_name': None,
                'platform': None,
                'real_address': None,
                'virt_address': None,
                'virt_address6': None,
                'connected_since': None
            }
            server_data.append(data)
            users_servers[usr.id][svr.id] = data

        user_dict['servers'] = sorted(server_data, key=lambda x: x['name'])

        users.append(user_dict)

    clients_collection = mongo.get_collection('clients')
    for doc in clients_collection.find({
                'user_id': {'$in': users_id},
            }):
        server_data = users_servers[doc['user_id']].get(doc['server_id'])
        if not server_data:
            continue

        users_data[doc['user_id']]['status'] = True

        if server_data['status']:
            server_data = {
                'name': server_data['name'],
            }
            append = True
        else:
            append = False

        virt_address6 = doc.get('virt_address6')
        if virt_address6:
            server_data['virt_address6'] = virt_address6.split('/')[0]

        server_data['id'] = doc['_id']
        server_data['status'] = True
        server_data['server_id'] = server_data['id']
        server_data['device_name'] = doc['device_name']
        server_data['platform'] = doc['platform']
        server_data['real_address'] = doc['real_address']
        server_data['virt_address'] = doc['virt_address'].split('/')[0]
        server_data['connected_since'] = doc['connected_since']

        if append:
            svrs = users_data[doc['user_id']]['servers']
            svrs.append(server_data)
            users_data[doc['user_id']]['servers'] = sorted(
                svrs, key=lambda x: x['name'])

    net_link_collection = mongo.get_collection('users_net_link')
    for doc in net_link_collection.find({
                'user_id': {'$in': users_id},
            }):
        users_data[doc['user_id']]['network_links'].append(doc['network'])

    ip_addrs_iter = server.multi_get_ip_addr(org_id, users_id)
    for user_id, server_id, addr, addr6 in ip_addrs_iter:
        server_data = users_servers[user_id].get(server_id)
        if server_data:
            if not server_data['virt_address']:
                server_data['virt_address'] = addr
            if not server_data['virt_address6']:
                server_data['virt_address6'] = addr6

    if page is not None:
        resp = {
            'page': page,
            'page_total': org.page_total,
            'server_count': server_count,
            'users': users,
        }
    elif search is not None:
        resp = {
            'search': search,
            'search_more': limit < org.last_search_count,
            'search_limit': limit,
            'search_count': org.last_search_count,
            'search_time':  round((time.time() - flask.g.start), 4),
            'server_count': server_count,
            'users': users,
        }
    else:
        resp = users

    if settings.app.demo_mode and not search:
        utils.demo_set_cache(resp, page, search, limit)
    return utils.jsonify(resp)