✏️ Fix: [dev] mail 기능 수정

This commit is contained in:
sm4640
2026-01-13 09:18:27 +09:00
parent caa52fbd0b
commit 6370f50aca
2 changed files with 89 additions and 30 deletions

View File

@@ -1,6 +1,5 @@
import os
import re
import subprocess
from django.conf import settings
from django.shortcuts import render
@@ -8,44 +7,93 @@ from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny, IsAdminUser
from rest_framework.response import Response
from rest_framework import status
from rest_framework.exceptions import NotFound
from rest_framework.exceptions import NotFound, APIException
try:
import docker
except ImportError:
docker = None
LOCAL_RE = re.compile(r"^[a-z0-9](?:[a-z0-9._-]{0,62}[a-z0-9])?$")
# ----------------------------
# Feature toggle
# ----------------------------
def _require_enabled():
# settings.py에 MAIL_SIGNUP_ENABLED 토글이 있으면 사용
if not getattr(settings, "MAIL_SIGNUP_ENABLED", False):
raise NotFound("Not Found") # 숨김(404)
def _run_setup(cmd_args):
# ----------------------------
# Docker-mailserver exec helpers
# ----------------------------
def _docker_client():
"""
./setup.sh -c dms-mailserver email add|list ...
Uses docker socket mounted into this container:
/var/run/docker.sock
"""
setup_sh = getattr(settings, "MAIL_SETUP_SH", None) or os.getenv("MAIL_SETUP_SH", "./setup.sh")
workdir = getattr(settings, "MAIL_SETUP_WORKDIR", None) or os.getenv("MAIL_SETUP_WORKDIR", os.getcwd())
container = getattr(settings, "MAIL_DMS_CONTAINER", None) or os.getenv("MAIL_DMS_CONTAINER", "dms-mailserver")
if docker is None:
raise APIException("Python package 'docker' is not installed in this server image.")
if not os.path.exists(setup_sh):
raise RuntimeError(f"setup.sh not found: {setup_sh}")
sock = getattr(settings, "MAIL_DOCKER_SOCK", None) or os.getenv("MAIL_DOCKER_SOCK", "unix://var/run/docker.sock")
try:
client = docker.DockerClient(base_url=sock)
# ping to ensure socket is usable
client.ping()
return client
except Exception as e:
raise APIException(f"Docker socket is not available. Check /var/run/docker.sock mount. ({e})")
full = [setup_sh, "-c", container] + cmd_args
p = subprocess.run(full, cwd=workdir, capture_output=True, text=True)
out = (p.stdout or "").strip()
err = (p.stderr or "").strip()
if p.returncode != 0:
raise ValueError(err or out or "setup.sh failed")
def _dms_container_name() -> str:
return getattr(settings, "MAIL_DMS_CONTAINER", None) or os.getenv("MAIL_DMS_CONTAINER", "dms-mailserver")
return out
def _dms_exec(setup_args):
"""
Runs docker-mailserver's internal `setup` command:
docker exec dms-mailserver setup <args...>
Example:
_dms_exec(["email", "list"])
_dms_exec(["email", "add", "user@domain", "password"])
"""
client = _docker_client()
name = _dms_container_name()
try:
c = client.containers.get(name)
except Exception as e:
raise APIException(f"Cannot find mailserver container '{name}'. ({e})")
# Try to execute: setup <args...>
try:
res = c.exec_run(["setup", *setup_args], demux=True)
out, err = res.output
stdout = (out or b"").decode("utf-8", errors="ignore").strip()
stderr = (err or b"").decode("utf-8", errors="ignore").strip()
if res.exit_code != 0:
# show meaningful error to API consumer
raise ValueError(stderr or stdout or "docker-mailserver setup failed")
return stdout
except ValueError:
raise
except Exception as e:
raise APIException(f"Failed to exec into '{name}'. ({e})")
def _email_list():
out = _run_setup(["email", "list"])
out = _dms_exec(["email", "list"])
return [ln.strip() for ln in out.splitlines() if ln.strip()]
# ----------------------------
# Views
# ----------------------------
def signup_page(request):
_require_enabled()
return render(request, "mail/signup.html")
@@ -67,16 +115,22 @@ def request_account(request):
return Response({"detail": "초대코드가 올바르지 않습니다."}, status=status.HTTP_403_FORBIDDEN)
if not dms_domain:
return Response({"detail": "서버 설정(MAIL_DMS_DOMAIN)이 비어있습니다."},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response(
{"detail": "서버 설정(MAIL_DMS_DOMAIN)이 비어있습니다."},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
if not LOCAL_RE.match(local):
return Response({"detail": "아이디 형식이 올바르지 않습니다. (영문소문자/숫자/._- 허용)"},
status=status.HTTP_400_BAD_REQUEST)
return Response(
{"detail": "아이디 형식이 올바르지 않습니다. (영문소문자/숫자/._- 허용)"},
status=status.HTTP_400_BAD_REQUEST,
)
if len(password) < 10:
return Response({"detail": "비밀번호는 10자 이상으로 설정하세요."},
status=status.HTTP_400_BAD_REQUEST)
if len(password) < 5:
return Response(
{"detail": "비밀번호는 5자 이상으로 설정하세요."},
status=status.HTTP_400_BAD_REQUEST,
)
address = f"{local}@{dms_domain}"
@@ -85,16 +139,21 @@ def request_account(request):
if address in emails:
return Response({"message": f" {address} 는 이미 존재합니다. (email list에서 확인됨)"})
_run_setup(["email", "add", address, password])
# Create account inside dms-mailserver
_dms_exec(["email", "add", address, password])
# Verify by listing again
emails2 = _email_list()
if address in emails2:
return Response({"message": f"{address} 계정이 잘 만들어졌습니다! (email list에서 확인됨)"})
return Response({"detail": "계정 생성 후 list에서 확인이 안 됩니다."},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response(
{"detail": "계정 생성 후 list에서 확인이 안 됩니다."},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
except ValueError as e:
# setup command returned error
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
return Response({"detail": f"서버 오류: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)