예제 #1
0
    def __init__(self):

        self.dendrite = neuron.Dendrite()

        self.synapse = neuron.Synapse()

        self.interfaces = list(utils.physical_ifaces())
예제 #2
0
    def register(self, data):
        # settings.configure must have been called before this imort
        from django.contrib.auth.hashers import make_password

        # check elan-center connectivity
        self.check_connectivity()

        if not data:
            return {'status': 'available'}

        data['interfaces'] = sorted(utils.physical_ifaces())

        result = self.dendrite.call(
            'elan-center/register',
            data)  # raises RequestError if registration fails

        # store this admin in conf (should be overridien once Axon correctly configured)
        self.dendrite.publish_conf('administrator', [
            dict(login=data['login'], password=make_password(data['password']))
        ])

        synapse.set(ACCOUNT_ID_PATH, result['account'])
        synapse.set(AGENT_ID_PATH, result['id'])
        synapse.set(AGENT_UUID_PATH, result['uuid'])

        # delay configuration so that caller has time to receive response...
        def delayed_action():
            configure_axon()
            # wait axon really started.
            time.sleep(5.0)
            session.notify_current_sessions()
            device.notify_known_hostnames()
            device.notify_known_fingerprints()

        t = threading.Timer(3.0, delayed_action)
        t.start()

        return {'status': 'registered'}
                        help="just generate default configuration if needed",
                        action="store_true")
    args = parser.parse_args()

    dendrite = Dendrite()

    try:
        vlans = dendrite.get_conf('vlans', timeout=5)
    except ConnectionFailed:
        vlans = None

    if vlans is None:
        # Default vlan conf: first 2 interfaces bridged
        default_vlans = []
        count = 0
        for interface in physical_ifaces():
            count += 1
            default_vlans.append({'interface': interface})
            if count == 2:
                break
        if args.default_conf_only and not NetworkConfigurator.vlans_conf_files_exists():
            NetworkConfigurator.generate_vlans_conf_files(default_vlans)

    if not args.default_conf_only:
        configurator = AccessControlConfigurator()
        # Generate nginx captive portal conf if needed
        if vlans is None and not os.path.exists(NGINX_CAPTIVE_PORTAL):
            configurator.new_vlan_conf(
                    default_vlans,
                    skip_vlans_conf_files=True
            )
예제 #4
0
def dashboard(request, context=None):
    if context is None:
        context = {}

    dendrite = Dendrite()

    registered = is_registered()

    try:
        # will raise error if not connected
        dendrite.call('check-connectivity', timeout=2)
        is_connected = True
        connectivity_error = ''
    except RequestTimeout:
        is_connected = None  # Unknown
        connectivity_error = 'Connectivity check not implemented'
    except RequestError as e:
        is_connected = False
        connectivity_error = e.error_str

    registration_available = False
    registration_error = ''
    if not registered:
        try:
            dendrite.call('register', timeout=2)
            registration_available = True
        except RequestTimeout:
            registration_available = False
            registration_error = 'Registration service not implemented'
        except RequestError as e:
            registration_available = False
            registration_error = e.error_str

    current_ipv4 = netconf.get_current_ipv4(cidr=True)
    current_ipv4['ips'] = [
        *map(
            lambda x: dict(zip(['address', 'prefix_length'], x.split('/', 1))),
            current_ipv4['ips'])
    ]
    current_ipv6 = netconf.get_current_ipv6(cidr=True)
    current_ipv6['ips'] = [
        *map(
            lambda x: dict(zip(['address', 'prefix_length'], x.split('/', 1))),
            current_ipv6['ips'])
    ]

    context.update(
        registration_available=registration_available,
        registration_error=registration_error,
        is_admin=bool(request.session.get('admin', False)),
        is_connected=is_connected,
        connectivity_error=connectivity_error,
        is_registered=registered,
        interfaces={
            iface: {
                'up': is_iface_up(iface)
            }
            for iface in physical_ifaces()
        },
        ipv4=current_ipv4,
        ipv6=current_ipv6,
    )
    if not context.get('location', ''):
        # TODO:
        context['location'] = ''

    ip_conf = NetworkConfiguration()
    if not context.get('ipv4_form', None):
        context['ipv4_form'] = Ip4ConfigurationForm(
            initial=ip_conf.get_ipv4_conf())
    if not context.get('ipv6_form', None):
        context['ipv6_form'] = Ip6ConfigurationForm(
            initial=ip_conf.get_ipv6_conf())

    return render(request, 'captive-portal/dashboard.html', context)