From 9344cb1205b8f128c212260ef395cee324b3e700 Mon Sep 17 00:00:00 2001 From: laucyun Date: Wed, 20 Apr 2022 21:30:15 +0800 Subject: [PATCH] Added dockerfile Issue #5 Close #5 --- .env | 20 ++++++++++++++ Dockerfile | 30 +++++++++++++++++++++ dnslog/settings.py | 36 +++++++++++++++---------- dnslog_nginx.conf | 34 ++++++++++++++---------- docker-compose.yml | 62 ++++++++++++++++++++++++++++++++++++++++++++ docker-entrypoint.sh | 26 +++++++++++++++++++ 6 files changed, 180 insertions(+), 28 deletions(-) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 docker-entrypoint.sh diff --git a/.env b/.env new file mode 100644 index 0000000..df8badd --- /dev/null +++ b/.env @@ -0,0 +1,20 @@ +# Compose +COMPOSE_PROJECT_NAME=dnslog +COMPOSE_HTTP_TIMEOUT=3600 +DOCKER_CLIENT_TIMEOUT=3600 +DOCKER_SUBNET=172.16.239.0/24 + +# MySQL +DB_HOST=mysql +DB_PORT=3306 +DB_USER=root +DB_PASSWORD=PucJ86KwPz9sfigZzlGa +DB_NAME=dnslog + +# DNSLog +SECRET_KEY=GeqwBTQCvK4Yg2VneExUxtNGuLR4pQYy6xduMfPAM4oJgdqQ12 +DNS_DOMAIN=eyes.sh +ADMIN_DOMAIN=eyes.sh +NS1_DOMAIN=eyes_dns1.lijiejie.com +NS2_DOMAIN=eyes_dns2.lijiejie.com +SERVER_IP=123.123.123.123 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e6dd914 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +FROM python:3.8 + +ENV TZ Asia/Shanghai +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +ADD . /dnslog +RUN pip install -r /dnslog/requirements.txt +RUN chmod +x /dnslog/docker-entrypoint.sh + +WORKDIR /dnslog +EXPOSE 53 + +ENTRYPOINT ["/dnslog/docker-entrypoint.sh"] + +# Install nginx +RUN apt update -y +RUN apt-get install -y \ + ca-certificates \ + nginx \ + gettext-base \ + && rm -rf /var/lib/apt/lists/* + +RUN ln -sf /dev/stdout /var/log/nginx/access.log \ + && ln -sf /dev/stderr /var/log/nginx/error.log + +RUN cp /dnslog/dnslog_nginx.conf /etc/nginx/conf.d/default.conf + +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/dnslog/settings.py b/dnslog/settings.py index 13a5b34..fad83a5 100644 --- a/dnslog/settings.py +++ b/dnslog/settings.py @@ -6,7 +6,7 @@ from django.utils.translation import gettext_lazy as _ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '#ma=s-l!2obwj%h-6uu^sbw+4%i2w79%v3^ill62k3&7tjf5dc' +SECRET_KEY = os.getenv("SECRET_KEY", "#ma=s-l!2obwj%h-6uu^sbw+4%i2w79%v3^ill62k3&7tjf5dc") DEBUG = False @@ -15,6 +15,13 @@ ALLOWED_HOSTS = ['*'] # SECURE_SSL_REDIRECT = True SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') +# CSRF +CSRF_TRUSTED_ORIGINS = [ + "http://localhost:8080", "http://localhost", + "http://%s" % (os.getenv("ADMIN_DOMAIN", "eyes.sh")), + "https://%s" % (os.getenv("ADMIN_DOMAIN", "eyes.sh")) +] + INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', @@ -59,10 +66,10 @@ WSGI_APPLICATION = 'dnslog.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'DB_NAME', - 'USER': 'DB_USER', - 'PASSWORD': 'DB_PASSWORD', - 'HOST': 'DB_HOST', + 'NAME': os.getenv('DB_NAME', 'mysql'), + 'USER': os.getenv('DB_USER', 'root'), + 'PASSWORD': os.getenv('DB_PASSWORD', 'root'), + 'HOST': os.getenv('DB_HOST', 'localhost'), 'PORT': '3306' } } @@ -80,26 +87,26 @@ LANGUAGES = ( ('en', _('English')), ) -LOCALE_PATHS = (os.path.join(BASE_DIR, 'locale'),) +LOCALE_PATHS = (os.path.join(BASE_DIR, 'locale'), ) STATIC_URL = '/static/' if DEBUG: - STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) + STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), ) else: STATIC_ROOT = os.path.join(BASE_DIR, 'static') # 用于DNS记录的域名 -DNS_DOMAIN = 'eyes.sh' +DNS_DOMAIN = os.getenv('DNS_DOMAIN', 'eyes.sh') # 管理后台域名 -ADMIN_DOMAIN = ['eyes.sh', 'www.eyes.sh'] +ADMIN_DOMAIN = os.getenv('ADMIN_DOMAIN', 'eyes.sh') # NS域名 -NS1_DOMAIN = 'eyes_dns1.lijiejie.com' -NS2_DOMAIN = 'eyes_dns2.lijiejie.com' +NS1_DOMAIN = os.getenv('NS1_DOMAIN', 'eyes_dns1.lijiejie.com') +NS2_DOMAIN = os.getenv('NS2_DOMAIN', 'eyes_dns1.lijiejie.com') # 服务器外网地址 -SERVER_IP = '123.123.123.123' +SERVER_IP = os.getenv('SERVER_IP', '123.123.123.123') if not DEBUG: LOGGING = {} @@ -110,7 +117,8 @@ else: 'disable_existing_loggers': True, 'formatters': { 'verbose': { - 'format': "[%(asctime)s] %(levelname)s [ %(filename)s] [line %(lineno)s] %(message)s", + 'format': + "[%(asctime)s] %(levelname)s [ %(filename)s] [line %(lineno)s] %(message)s", 'datefmt': "%d/%b/%Y %H:%M:%S" }, 'simple': { @@ -137,4 +145,4 @@ else: 'level': 'CRITICAL', } }, - } + } \ No newline at end of file diff --git a/dnslog_nginx.conf b/dnslog_nginx.conf index 8a276bb..1355b38 100644 --- a/dnslog_nginx.conf +++ b/dnslog_nginx.conf @@ -1,18 +1,24 @@ server { - listen 80; - server_name eyes.sh www.eyes.sh *.eyes.sh; - uwsgi_pass_request_headers on; + listen 80; + server_name localhost $ADMIN_DOMAI; + uwsgi_pass_request_headers on; - location = /favicon.ico { access_log off; log_not_found off; } - location /static/ { - alias /dnslog/static/; - } + if ($request_uri ~* "^/$") { + return 302 $scheme://$http_host/login; + } - location / { - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_pass http://127.0.0.1:8000; - } + location = /favicon.ico { + access_log off; log_not_found off; + } + location /static/ { + alias /dnslog/static/; + } + + location / { + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_pass http://127.0.0.1:8000; + } } \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e17f25f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,62 @@ +version: '3' + +services: + mysql: + image: mariadb:10.6 + container_name: mysql + restart: always + command: --character-set-server=utf8 --collation-server=utf8_general_ci + environment: + DB_USER: $DB_USER + DB_PORT: $DB_PORT + MARIADB_ROOT_PASSWORD: $DB_PASSWORD + MARIADB_DATABASE: $DB_NAME + healthcheck: + test: "mysql -h127.0.0.1 -P$$DB_PORT -u$$DB_USER -p$$MARIADB_ROOT_PASSWORD -e 'SHOW DATABASES;'" + interval: 10s + timeout: 5s + retries: 3 + start_period: 30s + networks: + - net + dnslog: + build: + context: . + dockerfile: Dockerfile + image: dnslog + container_name: dnslog + restart: always + environment: + DB_HOST: $DB_HOST + DB_PORT: $DB_PORT + DB_USER: $DB_USER + DB_PASSWORD: $DB_PASSWORD + DB_NAME: $DB_NAME + SECRET_KEY: $SECRET_KEY + DNS_DOMAIN: $DNS_DOMAIN + ADMIN_DOMAIN: $ADMIN_DOMAIN + NS1_DOMAIN: $NS1_DOMAIN + NS2_DOMAIN: $NS2_DOMAIN + SERVER_IP: $SERVER_IP + depends_on: + - mysql + ports: + - 53:53/tcp + - 53:53/udp + - 8080:80/tcp + healthcheck: + test: "wget -nv -t1 --spider http://localhost/login --no-check-certificate > /dev/null" + interval: 10s + timeout: 5s + retries: 3 + start_period: 30s + networks: + - net + +networks: + net: + driver: bridge + ipam: + driver: default + config: + - subnet: $DOCKER_SUBNET diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..19e4a32 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,26 @@ +#! /usr/bin/env sh +set -e + +python /dnslog/manage.py makemigrations logview +python /dnslog/manage.py migrate +python /dnslog/manage.py collectstatic --noinput + +# Run nginx +nginx_count=`ps aux|grep nginx|grep -v grep|wc -l` +if [ $nginx_count -eq 0 ]; then + nginx -g 'daemon off;' & +fi + +# Run zoneresolver.py +zoneresolver_count=`ps aux|grep zoneresolver|grep -v grep|wc -l` +if [ $zoneresolver_count -eq 0 ]; then + python /dnslog/zoneresolver.py & +fi + +# Run gunicorn +gunicorn_count=`ps aux|grep gunicorn|grep -v grep|wc -l` +if [ $gunicorn_count -eq 0 ]; then + gunicorn --workers 5 --bind 0.0.0.0:8000 dnslog.wsgi:application --daemon +fi + +exec "$@" \ No newline at end of file