mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
wireguard: Create mixin for reusing generated client conf
Signed-off-by: Frederico Gomes <fredericojfgomes@gmail.com> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
bb6729a99a
commit
60a6ac2a0d
@ -82,6 +82,36 @@ class AddClientView(SuccessMessageMixin, FormView):
|
|||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
|
class SessionClientDataMixin:
|
||||||
|
"""Shared session data loading for auto-client views."""
|
||||||
|
|
||||||
|
def get_session_client_data(self, request):
|
||||||
|
"""Extract client data from session."""
|
||||||
|
next_ip = request.session.get('next_ip')
|
||||||
|
pubkey = request.session.get('client_pubkey')
|
||||||
|
privkey = request.session.get('client_privkey')
|
||||||
|
endpoint = request.session.get('endpoint')
|
||||||
|
|
||||||
|
if not all([next_ip, privkey, pubkey, endpoint]):
|
||||||
|
raise Http404("Session expired")
|
||||||
|
|
||||||
|
return {
|
||||||
|
'next_ip': next_ip,
|
||||||
|
'privkey': privkey,
|
||||||
|
'pubkey': pubkey,
|
||||||
|
'endpoint': endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_client_config(self, request):
|
||||||
|
"""Rebuild client config from session."""
|
||||||
|
data = self.get_session_client_data(request)
|
||||||
|
|
||||||
|
return utils.build_client_config(
|
||||||
|
data['next_ip'], data['privkey'],
|
||||||
|
data['pubkey'], data['endpoint']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AutoAddClientView(SuccessMessageMixin, FormView):
|
class AutoAddClientView(SuccessMessageMixin, FormView):
|
||||||
"""View to add a client with keypair generation."""
|
"""View to add a client with keypair generation."""
|
||||||
form_class = forms.AutoAddClientForm
|
form_class = forms.AutoAddClientForm
|
||||||
@ -115,17 +145,19 @@ class AutoAddClientView(SuccessMessageMixin, FormView):
|
|||||||
settings = connection.get_setting_by_name(setting_name)
|
settings = connection.get_setting_by_name(setting_name)
|
||||||
next_ip = utils._get_next_available_ip_address(settings)
|
next_ip = utils._get_next_available_ip_address(settings)
|
||||||
|
|
||||||
# Add properties to template context
|
data = {
|
||||||
context.update({
|
|
||||||
'domains': filtered_domains,
|
|
||||||
'next_ip': next_ip,
|
'next_ip': next_ip,
|
||||||
'client_privkey': client_privkey,
|
'client_privkey': client_privkey,
|
||||||
'client_pubkey': client_pubkey,
|
'client_pubkey': client_pubkey,
|
||||||
'endpoint': endpoint
|
'endpoint': endpoint
|
||||||
})
|
}
|
||||||
|
|
||||||
# Store pubkey on instance for form_valid()
|
# Add properties to template context
|
||||||
self.request.session['client_pubkey'] = client_pubkey
|
context['domains'] = filtered_domains
|
||||||
|
context.update(data)
|
||||||
|
|
||||||
|
# Store info on instance for reuse
|
||||||
|
self.request.session.update(data)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
messages.warning(f"Client key generation failed: {e}")
|
messages.warning(f"Client key generation failed: {e}")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user