Add NGINXConfigurator.py

This commit is contained in:
prance 2022-01-27 22:46:33 +01:00
parent 768fd10de0
commit 4072c90e68
7 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,51 @@
import subprocess
class NGINXConfigurator:
_config_path = "/etc/nginx/conf.d/muor.conf"
_location_preamble_path = "configs/muor_before_location_block.conf"
_location_epilouge_path = "configs/muor_after_location_block.conf"
@classmethod
def refresh_config(cls, sessions):
"""
Expects sessions in form of list of (uid, port, sessionid) entries.
"""
config = cls._get_config(sessions)
with open(cls._config_path, 'w') as F:
F.write(config)
completed_process = subprocess.run(["nginx", "-s", "reload"])
return completed_process.returncode
@classmethod
def _get_config(cls, sessions):
config = "\n".join(
cls._get_upstream_entry(uid, port) for uid, port, sessionid in
sessions)
config += cls._get_locations_preamble()
config += "\n".join(
cls._get_location_entry(uid, sessionid) for uid, port, sessionid in
sessions)
config += cls._get_locations_epilogue()
return config
@classmethod
def _get_locations_preamble(cls):
with open(cls._location_preamble_path) as F:
return F.read()
@classmethod
def _get_locations_epilogue(cls):
with open(cls._location_epilouge_path) as F:
return F.read()
@classmethod
def _get_upstream_entry(cls, uid, port):
return f"upstream {uid}" + " {\n" \
+ f" server localhost:{port}" + "\n}"
@classmethod
def _get_location_entry(cls, uid, sessionid):
return f'if ($cookie_sessionid = "{sessionid})"' + " {\n" \
+ f"proxy_pass http://{uid};" + "\n}"

View File

@ -0,0 +1,19 @@
if ($cookie_sessionid = ""){
uwsgi_read_timeout 600;
uwsgi_send_timeout 600;
uwsgi_connect_timeout 60;
uwsgi_ignore_client_abort on;
uwsgi_pass django;
}
}
ssl_certificate_key /ssl/private.pem;
ssl_certificate /ssl/certs.pem;
ssl_protocols TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
ssl_prefer_server_ciphers on;
# add_header Strict-Transport-Security max-age=31536000;
}

View File

@ -0,0 +1,33 @@
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80 reuseport;
server_name randomsec.projektstudencki.pl;
return 301 https://randomsec.projektstudencki.pl;
}
# configuration of the server
server {
# the port your site will be served on
listen 443 ssl http2 reuseport;
# the domain name it will serve for
server_name randomsec.projektstudencki.pl
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /path/to/static; #TODO add path
expires 30d;
access_log off;
add_header Pragma public;
add_header Cache-Control "public";
}
# Finally, send all non-media requests to the Django server.
location / {

View File

@ -0,0 +1,59 @@
worker_processes 1;
events {
worker_connections 1024;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 120;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
}

View File

@ -18,6 +18,7 @@ from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import path, include
from django.views.generic import TemplateView, RedirectView
from . import views
from .views import SignUpView
urlpatterns = [
@ -26,4 +27,5 @@ urlpatterns = [
path('signup/', SignUpView.as_view(), name='signup'),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('images/favicon.ico'))),
path('test/', views.test, name='test')
]

View File

@ -1,5 +1,6 @@
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.http import JsonResponse
from django.urls import reverse_lazy
from django.views import generic
@ -9,8 +10,15 @@ class SignUpForm(UserCreationForm):
model = User
fields = ('username', 'email',)
def save(self, commit=True):
return super().save(commit)
class SignUpView(generic.CreateView):
form_class = SignUpForm
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
def test(request):
return JsonResponse(request.COOKIES)

Binary file not shown.