2016-06-25 12:57:47 +02:00
|
|
|
import flask
|
|
|
|
import flask_sqlalchemy
|
|
|
|
import flask_bootstrap
|
|
|
|
import flask_login
|
|
|
|
import flask_script
|
|
|
|
import flask_migrate
|
2016-10-02 14:23:44 +02:00
|
|
|
import flask_babel
|
2017-10-29 19:42:35 +02:00
|
|
|
import flask_limiter
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-02-20 21:11:59 +02:00
|
|
|
import os
|
2016-04-20 22:37:17 +02:00
|
|
|
import docker
|
2017-11-03 22:50:28 +02:00
|
|
|
import socket
|
|
|
|
import uuid
|
2016-02-20 14:57:26 +02:00
|
|
|
|
|
|
|
# Create application
|
2017-09-24 12:00:39 +02:00
|
|
|
app = flask.Flask(__name__)
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-02-20 21:11:59 +02:00
|
|
|
default_config = {
|
2017-11-10 15:55:53 +02:00
|
|
|
# Specific to the admin UI
|
2016-10-29 13:42:39 +02:00
|
|
|
'SQLALCHEMY_DATABASE_URI': 'sqlite:////data/main.db',
|
2016-02-20 21:11:59 +02:00
|
|
|
'SQLALCHEMY_TRACK_MODIFICATIONS': False,
|
2017-11-10 15:55:53 +02:00
|
|
|
'DOCKER_SOCKET': 'unix:///var/run/docker.sock',
|
|
|
|
'BABEL_DEFAULT_LOCALE': 'en',
|
|
|
|
'BABEL_DEFAULT_TIMEZONE': 'UTC',
|
|
|
|
'BOOTSTRAP_SERVE_LOCAL': True,
|
|
|
|
'RATELIMIT_STORAGE_URL': 'redis://redis',
|
|
|
|
'DEBUG': False,
|
|
|
|
# Statistics management
|
2017-11-03 22:50:28 +02:00
|
|
|
'INSTANCE_ID_PATH': '/data/instance',
|
|
|
|
'STATS_ENDPOINT': '0.{}.stats.mailu.io',
|
2017-11-10 15:55:53 +02:00
|
|
|
# Common configuration variables
|
2016-04-20 22:37:17 +02:00
|
|
|
'SECRET_KEY': 'changeMe',
|
2016-10-29 13:42:39 +02:00
|
|
|
'DOMAIN': 'mailu.io',
|
2017-11-10 15:55:53 +02:00
|
|
|
'HOSTNAMES': 'mail.mailu.io,alternative.mailu.io,yetanother.mailu.io',
|
2016-06-25 14:51:02 +02:00
|
|
|
'POSTMASTER': 'postmaster',
|
2017-09-10 18:53:41 +02:00
|
|
|
'TLS_FLAVOR': 'cert',
|
2017-10-29 19:42:35 +02:00
|
|
|
'AUTH_RATELIMIT': '10/minute;1000/hour',
|
2017-11-10 12:56:43 +02:00
|
|
|
'DISABLE_STATISTICS': 'False',
|
2017-11-10 15:55:53 +02:00
|
|
|
# Mail settings
|
|
|
|
'DMARC_RUA': None,
|
|
|
|
'DMARC_RUF': None,
|
2017-11-10 12:56:43 +02:00
|
|
|
'WELCOME': 'False',
|
|
|
|
'WELCOME_SUBJECT': 'Dummy welcome topic',
|
2017-11-10 15:49:36 +02:00
|
|
|
'WELCOME_BODY': 'Dummy welcome body',
|
2017-11-10 15:55:53 +02:00
|
|
|
'DKIM_SELECTOR': 'dkim',
|
|
|
|
'DKIM_PATH': '/dkim/{domain}.{selector}.key',
|
2017-12-03 13:01:03 +02:00
|
|
|
'DEFAULT_QUOTA': 1000000000,
|
2017-11-10 15:55:53 +02:00
|
|
|
# Web settings
|
|
|
|
'SITENAME': 'Mailu',
|
|
|
|
'WEBSITE': 'https://mailu.io',
|
|
|
|
'WEB_ADMIN': '/admin',
|
2017-11-10 16:00:51 +02:00
|
|
|
'WEB_WEBMAIL': '/webmail',
|
2017-11-10 15:55:53 +02:00
|
|
|
# Advanced settings
|
|
|
|
'PASSWORD_SCHEME': 'SHA512-CRYPT',
|
2016-02-20 21:11:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Load configuration from the environment if available
|
|
|
|
for key, value in default_config.items():
|
|
|
|
app.config[key] = os.environ.get(key, value)
|
|
|
|
|
2017-09-17 14:36:44 +02:00
|
|
|
# Base application
|
2016-06-25 12:57:47 +02:00
|
|
|
flask_bootstrap.Bootstrap(app)
|
|
|
|
db = flask_sqlalchemy.SQLAlchemy(app)
|
|
|
|
migrate = flask_migrate.Migrate(app, db)
|
2017-10-29 19:42:35 +02:00
|
|
|
limiter = flask_limiter.Limiter(app, key_func=lambda: current_user.username)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
2017-10-29 14:43:10 +02:00
|
|
|
# Debugging toolbar
|
|
|
|
if app.config.get("DEBUG"):
|
|
|
|
import flask_debugtoolbar
|
|
|
|
toolbar = flask_debugtoolbar.DebugToolbarExtension(app)
|
|
|
|
|
2016-06-25 12:57:47 +02:00
|
|
|
# Manager commnad
|
|
|
|
manager = flask_script.Manager(app)
|
|
|
|
manager.add_command('db', flask_migrate.MigrateCommand)
|
|
|
|
|
2016-11-06 13:48:24 +02:00
|
|
|
# Babel configuration
|
2017-09-17 14:36:44 +02:00
|
|
|
babel = flask_babel.Babel(app)
|
|
|
|
translations = list(map(str, babel.list_translations()))
|
|
|
|
|
2016-11-06 13:48:24 +02:00
|
|
|
@babel.localeselector
|
|
|
|
def get_locale():
|
|
|
|
return flask.request.accept_languages.best_match(translations)
|
|
|
|
|
2017-09-17 14:36:44 +02:00
|
|
|
# Login configuration
|
|
|
|
login_manager = flask_login.LoginManager()
|
|
|
|
login_manager.init_app(app)
|
2017-11-10 15:49:36 +02:00
|
|
|
login_manager.login_view = "ui.login"
|
2017-09-17 14:36:44 +02:00
|
|
|
|
2017-09-17 18:55:54 +02:00
|
|
|
@login_manager.unauthorized_handler
|
|
|
|
def handle_needs_login():
|
|
|
|
return flask.redirect(
|
2017-11-10 15:49:36 +02:00
|
|
|
flask.url_for('ui.login', next=flask.request.endpoint)
|
2017-09-17 18:55:54 +02:00
|
|
|
)
|
|
|
|
|
2017-09-17 14:36:44 +02:00
|
|
|
@app.context_processor
|
2017-10-29 19:13:59 +02:00
|
|
|
def inject_defaults():
|
2017-12-03 13:01:03 +02:00
|
|
|
signup_domains = models.Domain.query.filter_by(signup_enabled=True).all()
|
2017-10-29 19:13:59 +02:00
|
|
|
return dict(
|
|
|
|
current_user=flask_login.current_user,
|
2017-12-03 13:01:03 +02:00
|
|
|
signup_domains=signup_domains,
|
2017-10-29 19:13:59 +02:00
|
|
|
config=app.config
|
|
|
|
)
|
2016-08-21 15:01:07 +02:00
|
|
|
|
2017-09-17 14:36:44 +02:00
|
|
|
# Import views
|
2017-09-24 15:43:23 +02:00
|
|
|
from mailu import ui, internal
|
2017-09-24 12:00:39 +02:00
|
|
|
app.register_blueprint(ui.ui, url_prefix='/ui')
|
2017-09-24 15:43:23 +02:00
|
|
|
app.register_blueprint(internal.internal, url_prefix='/internal')
|
2017-09-17 18:55:54 +02:00
|
|
|
|
|
|
|
# Create the prefix middleware
|
|
|
|
class PrefixMiddleware(object):
|
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
prefix = environ.get('HTTP_X_FORWARDED_PREFIX', '')
|
|
|
|
if prefix:
|
|
|
|
environ['SCRIPT_NAME'] = prefix
|
|
|
|
return self.app(environ, start_response)
|
|
|
|
|
|
|
|
app.wsgi_app = PrefixMiddleware(app.wsgi_app)
|