mirror of
https://github.com/chatmail/relay.git
synced 2026-05-11 08:24:37 +00:00
Compare commits
1 Commits
tmpfs-inde
...
turnserver
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b21b83199 |
@@ -46,6 +46,7 @@ class Config:
|
||||
self.acme_email = params.get("acme_email", "")
|
||||
self.imap_rawlog = params.get("imap_rawlog", "false").lower() == "true"
|
||||
self.imap_compress = params.get("imap_compress", "false").lower() == "true"
|
||||
self.turn_socket_path = params.get("turn_socket_path", "/run/chatmail-turn/turn.socket")
|
||||
if "iroh_relay" not in params:
|
||||
self.iroh_relay = "https://" + params["mail_domain"]
|
||||
self.enable_iroh_relay = True
|
||||
@@ -56,7 +57,6 @@ class Config:
|
||||
self.privacy_mail = params.get("privacy_mail")
|
||||
self.privacy_pdo = params.get("privacy_pdo")
|
||||
self.privacy_supervisor = params.get("privacy_supervisor")
|
||||
self.tmpfs_index = params.get("tmpfs_index", "false").lower() == "true"
|
||||
|
||||
# deprecated option
|
||||
mbdir = params.get("mailboxes_dir", f"/home/vmail/mail/{self.mail_domain}")
|
||||
@@ -112,10 +112,10 @@ def get_default_config_content(mail_domain, **overrides):
|
||||
|
||||
if mail_domain.endswith(".testrun.org"):
|
||||
override_inipath = inidir.joinpath("override-testrun.ini")
|
||||
params = iniconfig.IniConfig(override_inipath)["params"]
|
||||
privacy = iniconfig.IniConfig(override_inipath)["privacy"]
|
||||
lines = []
|
||||
for line in content.split("\n"):
|
||||
for key, value in params.items():
|
||||
for key, value in privacy.items():
|
||||
value_lines = value.format(mail_domain=mail_domain).strip().split("\n")
|
||||
if not line.startswith(f"{key} =") or not value_lines:
|
||||
continue
|
||||
|
||||
@@ -17,14 +17,14 @@ from chatmaild.config import read_config
|
||||
FileEntry = namedtuple("FileEntry", ("path", "mtime", "size"))
|
||||
|
||||
|
||||
def iter_mailboxes(basedir, maxnum, tmpfs_index):
|
||||
def iter_mailboxes(basedir, maxnum):
|
||||
if not os.path.exists(basedir):
|
||||
print_info(f"no mailboxes found at: {basedir}")
|
||||
return
|
||||
|
||||
for name in os_listdir_if_exists(basedir)[:maxnum]:
|
||||
if "@" in name:
|
||||
yield MailboxStat(basedir + "/" + name, name, tmpfs_index)
|
||||
yield MailboxStat(basedir + "/" + name)
|
||||
|
||||
|
||||
def get_file_entry(path):
|
||||
@@ -49,14 +49,11 @@ def os_listdir_if_exists(path):
|
||||
class MailboxStat:
|
||||
last_login = None
|
||||
|
||||
def __init__(self, basedir, name, tmpfs_index):
|
||||
def __init__(self, basedir):
|
||||
self.basedir = str(basedir)
|
||||
self.name = name
|
||||
self.messages = []
|
||||
self.extrafiles = []
|
||||
self.scandir(self.basedir)
|
||||
if tmpfs_index:
|
||||
self.scandir("/dev/shm/" + name)
|
||||
|
||||
def scandir(self, folderdir):
|
||||
for name in os_listdir_if_exists(folderdir):
|
||||
@@ -93,13 +90,11 @@ class Expiry:
|
||||
self.all_files = 0
|
||||
self.start = time.time()
|
||||
|
||||
def remove_mailbox(self, mboxdir, name):
|
||||
def remove_mailbox(self, mboxdir):
|
||||
if self.verbose:
|
||||
print_info(f"removing {mboxdir}")
|
||||
if not self.dry:
|
||||
shutil.rmtree(mboxdir)
|
||||
if self.config.tmpfs_index:
|
||||
shutil.rmtree("/dev/shm/" + name)
|
||||
self.del_mboxes += 1
|
||||
|
||||
def remove_file(self, path, mtime=None):
|
||||
@@ -126,7 +121,7 @@ class Expiry:
|
||||
self.all_mboxes += 1
|
||||
changed = False
|
||||
if mbox.last_login and mbox.last_login < cutoff_without_login:
|
||||
self.remove_mailbox(mbox.basedir, mbox.name)
|
||||
self.remove_mailbox(mbox.basedir)
|
||||
return
|
||||
|
||||
mboxname = os.path.basename(mbox.basedir)
|
||||
@@ -150,9 +145,6 @@ class Expiry:
|
||||
changed = True
|
||||
if changed:
|
||||
self.remove_file(f"{mbox.basedir}/maildirsize")
|
||||
for file in mbox.extrafiles:
|
||||
if "dovecot.index" in file.path.split("/")[-1] and file.size > 500 * 1024:
|
||||
self.remove_file(file.path)
|
||||
|
||||
def get_summary(self):
|
||||
return (
|
||||
@@ -205,9 +197,7 @@ def main(args=None):
|
||||
|
||||
maxnum = int(args.maxnum) if args.maxnum else None
|
||||
exp = Expiry(config, dry=not args.remove, now=now, verbose=args.verbose)
|
||||
for mailbox in iter_mailboxes(
|
||||
str(config.mailboxes_dir), maxnum, config.tmpfs_index
|
||||
):
|
||||
for mailbox in iter_mailboxes(str(config.mailboxes_dir), maxnum=maxnum):
|
||||
exp.process_mailbox_stat(mailbox)
|
||||
print(exp.get_summary())
|
||||
|
||||
|
||||
@@ -159,7 +159,7 @@ def main(args=None):
|
||||
|
||||
maxnum = int(args.maxnum) if args.maxnum else None
|
||||
rep = Report(now=now, min_login_age=int(args.min_login_age), mdir=args.mdir)
|
||||
for mbox in iter_mailboxes(str(config.mailboxes_dir), maxnum, config.tmpfs_index):
|
||||
for mbox in iter_mailboxes(str(config.mailboxes_dir), maxnum=maxnum):
|
||||
rep.process_mailbox_stat(mbox)
|
||||
rep.dump_summary()
|
||||
|
||||
|
||||
@@ -48,9 +48,6 @@ passthrough_senders =
|
||||
# (space-separated, item may start with "@" to whitelist whole recipient domains)
|
||||
passthrough_recipients =
|
||||
|
||||
# store index files in tmpfs (good for disk size and I/O, bad for ram)
|
||||
tmpfs_index = false
|
||||
|
||||
# path to www directory - documented here: https://chatmail.at/doc/relay/getting_started.html#custom-web-pages
|
||||
#www_folder = www
|
||||
|
||||
@@ -58,7 +55,10 @@ tmpfs_index = false
|
||||
# Deployment Details
|
||||
#
|
||||
|
||||
# SMTP outgoing filtermail and reinjection
|
||||
# Path to the TURN server Unix socket
|
||||
turn_socket_path = /run/chatmail-turn/turn.socket
|
||||
|
||||
# SMTP outgoing filtermail and reinjection
|
||||
filtermail_smtp_port = 10080
|
||||
postfix_reinject_port = 10025
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
[params]
|
||||
|
||||
tmpfs_index = true
|
||||
[privacy]
|
||||
|
||||
passthrough_recipients = privacy@testrun.org echo@{mail_domain}
|
||||
|
||||
|
||||
@@ -76,12 +76,13 @@ class Metadata:
|
||||
|
||||
|
||||
class MetadataDictProxy(DictProxy):
|
||||
def __init__(self, notifier, metadata, iroh_relay=None, turn_hostname=None):
|
||||
def __init__(self, notifier, metadata, iroh_relay=None, turn_hostname=None, config=None):
|
||||
super().__init__()
|
||||
self.notifier = notifier
|
||||
self.metadata = metadata
|
||||
self.iroh_relay = iroh_relay
|
||||
self.turn_hostname = turn_hostname
|
||||
self.config = config
|
||||
|
||||
def handle_lookup(self, parts):
|
||||
# Lpriv/43f5f508a7ea0366dff30200c15250e3/devicetoken\tlkj123poi@c2.testrun.org
|
||||
@@ -101,7 +102,7 @@ class MetadataDictProxy(DictProxy):
|
||||
# Handle `GETMETADATA "" /shared/vendor/deltachat/irohrelay`
|
||||
return f"O{self.iroh_relay}\n"
|
||||
elif keyname == "vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn":
|
||||
res = turn_credentials()
|
||||
res = turn_credentials(self.config)
|
||||
port = 3478
|
||||
return f"O{self.turn_hostname}:{port}:{res}\n"
|
||||
|
||||
@@ -146,6 +147,7 @@ def main():
|
||||
metadata=metadata,
|
||||
iroh_relay=iroh_relay,
|
||||
turn_hostname=mail_domain,
|
||||
config=config,
|
||||
)
|
||||
|
||||
dictproxy.serve_forever_from_socket(socket)
|
||||
|
||||
@@ -43,22 +43,20 @@ def create_new_messages(basedir, relpaths, size=1000, days=0):
|
||||
|
||||
@pytest.fixture
|
||||
def mbox1(example_config):
|
||||
addr = "mailbox1@example.org"
|
||||
mboxdir = example_config.mailboxes_dir.joinpath(addr)
|
||||
mboxdir = example_config.mailboxes_dir.joinpath("mailbox1@example.org")
|
||||
mboxdir.mkdir()
|
||||
fill_mbox(mboxdir)
|
||||
return MailboxStat(mboxdir, addr, False)
|
||||
return MailboxStat(mboxdir)
|
||||
|
||||
|
||||
def test_deltachat_folder(example_config):
|
||||
"""Test old setups that might have a .DeltaChat folder where messages also need to get removed."""
|
||||
addr = "mailbox1@example.org"
|
||||
mboxdir = example_config.mailboxes_dir.joinpath(addr)
|
||||
mboxdir = example_config.mailboxes_dir.joinpath("mailbox1@example.org")
|
||||
mboxdir.mkdir()
|
||||
mbox2dir = mboxdir.joinpath(".DeltaChat")
|
||||
mbox2dir.mkdir()
|
||||
fill_mbox(mbox2dir)
|
||||
mb = MailboxStat(mboxdir, addr, False)
|
||||
mb = MailboxStat(mboxdir)
|
||||
assert len(mb.messages) == 2
|
||||
|
||||
|
||||
@@ -71,11 +69,7 @@ def test_filentry_ordering(tmp_path):
|
||||
|
||||
|
||||
def test_no_mailbxoes(tmp_path, capsys):
|
||||
assert [] == list(
|
||||
iter_mailboxes(
|
||||
str(tmp_path.joinpath("notexists")), maxnum=10, tmpfs_index=False
|
||||
)
|
||||
)
|
||||
assert [] == list(iter_mailboxes(str(tmp_path.joinpath("notexists")), maxnum=10))
|
||||
out, err = capsys.readouterr()
|
||||
assert "no mailboxes" in err
|
||||
|
||||
@@ -92,13 +86,13 @@ def test_stats_mailbox(mbox1):
|
||||
|
||||
create_new_messages(mbox1.basedir, ["large-extra"], size=1000)
|
||||
create_new_messages(mbox1.basedir, ["index-something"], size=3)
|
||||
mbox2 = MailboxStat(mbox1.basedir, mbox1.name, False)
|
||||
mbox2 = MailboxStat(mbox1.basedir)
|
||||
assert len(mbox2.extrafiles) == 5
|
||||
assert mbox2.extrafiles[0].size == 1000
|
||||
|
||||
# cope well with mailbox dirs that have no password (for whatever reason)
|
||||
Path(mbox1.basedir).joinpath("password").unlink()
|
||||
mbox3 = MailboxStat(mbox1.basedir, mbox1.name, False)
|
||||
mbox3 = MailboxStat(mbox1.basedir)
|
||||
assert mbox3.last_login is None
|
||||
|
||||
|
||||
|
||||
120
chatmaild/src/chatmaild/tests/test_turnserver.py
Normal file
120
chatmaild/src/chatmaild/tests/test_turnserver.py
Normal file
@@ -0,0 +1,120 @@
|
||||
"""Tests for turnserver functionality, particularly metadata integration."""
|
||||
|
||||
import socket
|
||||
import tempfile
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from chatmaild.config import read_config, write_initial_config
|
||||
from chatmaild.metadata import MetadataDictProxy, Metadata
|
||||
from chatmaild.notifier import Notifier
|
||||
from chatmaild.turnserver import turn_credentials
|
||||
|
||||
|
||||
def test_turn_credentials_function_with_custom_socket():
|
||||
"""Test that turn_credentials function works with a custom socket path from config."""
|
||||
# Create a temporary directory and socket file
|
||||
temp_dir = Path(tempfile.mkdtemp())
|
||||
temp_socket_path = temp_dir / "test_turn.socket"
|
||||
|
||||
# Create a mock TURN credentials server
|
||||
def mock_server():
|
||||
server_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
server_sock.bind(str(temp_socket_path))
|
||||
server_sock.listen(1)
|
||||
|
||||
# Accept connection and send mock credentials
|
||||
conn, addr = server_sock.accept()
|
||||
with conn:
|
||||
conn.send(b"mock_turn_credentials_abc123\n")
|
||||
server_sock.close()
|
||||
|
||||
# Start server in a background thread
|
||||
server_thread = threading.Thread(target=mock_server, daemon=True)
|
||||
server_thread.start()
|
||||
|
||||
# Create a config with custom socket path
|
||||
config_path = temp_dir / "chatmail.ini"
|
||||
write_initial_config(config_path, "test.example.org", {
|
||||
"turn_socket_path": str(temp_socket_path)
|
||||
})
|
||||
config = read_config(config_path)
|
||||
|
||||
# Allow time for server to start
|
||||
import time
|
||||
time.sleep(0.01)
|
||||
|
||||
# Test that turn_credentials can connect using the config
|
||||
credentials = turn_credentials(config)
|
||||
assert credentials == "mock_turn_credentials_abc123"
|
||||
|
||||
server_thread.join(timeout=1) # Clean up thread
|
||||
|
||||
|
||||
def test_metadata_turn_lookup_integration(tmp_path):
|
||||
"""Test that metadata service properly handles TURN metadata lookups."""
|
||||
# Create mock config with custom turn socket path
|
||||
config_path = tmp_path / "chatmail.ini"
|
||||
socket_path = tmp_path / "test_turn.socket"
|
||||
write_initial_config(config_path, "example.org", {
|
||||
"turn_socket_path": str(socket_path)
|
||||
})
|
||||
config = read_config(config_path)
|
||||
|
||||
# Create mock TURN server to return credentials
|
||||
def mock_turn_server():
|
||||
import os
|
||||
os.makedirs(socket_path.parent, exist_ok=True) # Ensure parent directory exists
|
||||
|
||||
server_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
server_sock.bind(str(socket_path))
|
||||
server_sock.listen(1)
|
||||
|
||||
# Accept connection and send mock credentials
|
||||
conn, addr = server_sock.accept()
|
||||
with conn:
|
||||
conn.send(b"test_creds_12345\n")
|
||||
server_sock.close()
|
||||
|
||||
server_thread = threading.Thread(target=mock_turn_server, daemon=True)
|
||||
server_thread.start()
|
||||
|
||||
import time
|
||||
time.sleep(0.01) # Allow server to start
|
||||
|
||||
# Create a MetadataDictProxy with config
|
||||
queue_dir = tmp_path / "queue"
|
||||
queue_dir.mkdir()
|
||||
notifier = Notifier(queue_dir)
|
||||
metadata = Metadata(tmp_path / "vmail")
|
||||
|
||||
dict_proxy = MetadataDictProxy(
|
||||
notifier=notifier,
|
||||
metadata=metadata,
|
||||
iroh_relay="https://example.org",
|
||||
turn_hostname="example.org",
|
||||
config=config
|
||||
)
|
||||
|
||||
# Simulate a lookup for TURN credentials using the correct format
|
||||
# Input: "shared/0123/vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn"
|
||||
# After parts[0].split("/", 2):
|
||||
# - keyparts[0] = "shared"
|
||||
# - keyparts[1] = "0123"
|
||||
# - keyparts[2] = "vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn"
|
||||
# So keyname = keyparts[2] should match "vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn"
|
||||
parts = [
|
||||
"shared/0123/vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn",
|
||||
"dummy@user.org"
|
||||
]
|
||||
|
||||
# Call handle_lookup directly
|
||||
result = dict_proxy.handle_lookup(parts)
|
||||
|
||||
# Verify the response format is correct for TURN credentials
|
||||
assert result.startswith("O") # Output response starts with 'O'
|
||||
assert ":3478:" in result # Contains port 3478
|
||||
assert "test_creds_12345" in result # Contains credentials returned by mock server
|
||||
assert "example.org:3478:test_creds_12345" in result
|
||||
|
||||
server_thread.join(timeout=1) # Clean up thread
|
||||
@@ -2,8 +2,8 @@
|
||||
import socket
|
||||
|
||||
|
||||
def turn_credentials() -> str:
|
||||
def turn_credentials(config) -> str:
|
||||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_socket:
|
||||
client_socket.connect("/run/chatmail-turn/turn.socket")
|
||||
client_socket.connect(config.turn_socket_path)
|
||||
with client_socket.makefile("rb") as file:
|
||||
return file.readline().decode("utf-8").strip()
|
||||
|
||||
@@ -68,11 +68,13 @@ userdb {
|
||||
##
|
||||
|
||||
# Mailboxes are stored in the "mail" directory of the vmail user home.
|
||||
{% if config.tmpfs_index %}
|
||||
mail_location = maildir:{{ config.mailboxes_dir }}/%u:INDEX=/dev/shm/%u
|
||||
{% else %}
|
||||
mail_location = maildir:{{ config.mailboxes_dir }}/%u
|
||||
{% endif %}
|
||||
|
||||
# index/cache files are not very useful for chatmail relay operations
|
||||
# but it's not clear how to disable them completely.
|
||||
# According to https://doc.dovecot.org/2.3/settings/advanced/#core_setting-mail_cache_max_size
|
||||
# if the cache file becomes larger than the specified size, it is truncated by dovecot
|
||||
mail_cache_max_size = 500K
|
||||
|
||||
namespace inbox {
|
||||
inbox = yes
|
||||
|
||||
Reference in New Issue
Block a user