Compare commits

...

3 Commits

Author SHA1 Message Date
Mike Sutton
e159252b4c cleaned up 2022-11-16 01:03:08 +01:00
Mike Sutton
9d256dfcba added services 2022-11-16 01:02:18 +01:00
Mike Sutton
329e66c949 combined consumer and proxy into minimal rails backed 2022-11-16 01:01:10 +01:00
58 changed files with 1166 additions and 261 deletions

View File

@ -1 +0,0 @@
bettermail_consumer

View File

@ -1,88 +0,0 @@
# frozen_string_literal: true
require 'dotenv/load'
require 'pg'
require 'mail'
require 'bunny'
# Server class
class BettermailConsumer
def initialize
@pg = PG::Connection.new(ENV['DATABASE_URL'])
end
def start
begin
puts ' [*] Waiting for messages. To exit press CTRL+C'
mail_queue = bunny_channel.queue(ENV['RABBIT_MAIL_QUEUE'])
mail_queue.subscribe(block: true) do |_delivery_info, _properties, body|
mail = Mail.read_from_string(body)
puts mail
end
sns_queue = bunny_channel.queue(ENV['RABBIT_SNS_QUEUE'])
sns_queue.subscribe(block: true) do |_delivery_info, _properties, body|
puts " [x] Received #{_delivery_info}"
end
rescue Interrupt => _
stop
end
end
def stop
@bunny_conn&.close
@pg&.close
end
def bunny_channel
if @bunny_channel.nil?
@bunny_conn = Bunny.new
@bunny_conn.start
@bunny_channel = @bunny_conn.create_channel
end
@bunny_channel
end
end
service_name='BetterMail::Consumer'
# Create a new server instance for listening at localhost interfaces 127.0.0.1:2525
# and accepting a maximum of 4 simultaneous connections per default
service = BettermailConsumer.new
# save flag for Ctrl-C pressed
flag_status_ctrl_c_pressed = false
# try to gracefully shutdown on Ctrl-C
trap('INT') do
# print an empty line right after ^C
puts
# notify flag about Ctrl-C was pressed
flag_status_ctrl_c_pressed = true
# signal exit to app
exit 0
end
# Output for debug
puts("Starting #{service_name}")
# setup exit code
at_exit do
# check to shutdown connection
if service
# Output for debug
puts('Ctrl-C interrupted, exit now...') if flag_status_ctrl_c_pressed
# info about shutdown
puts("Shutdown #{service_name}...")
# stop all threads and connections gracefully
service.stop
end
# Output for debug
puts "#{service_name} stopped!"
end
# Start the server
service.start
# Run on server forever
service.join

View File

@ -1 +0,0 @@
ruby ./bettermail_consumer.rb

View File

@ -1 +0,0 @@
bettermail_proxy

View File

@ -1,7 +0,0 @@
source 'https://rubygems.org'
ruby '2.7.2'
gem 'dotenv'
gem 'bunny'
gem 'midi-smtp-server', '~> 3.0.1'
gem 'mail'
gem 'openssl'

View File

@ -1,34 +0,0 @@
GEM
remote: https://rubygems.org/
specs:
amq-protocol (2.3.2)
bunny (2.19.0)
amq-protocol (~> 2.3, >= 2.3.1)
sorted_set (~> 1, >= 1.0.2)
dotenv (2.8.1)
mail (2.7.1)
mini_mime (>= 0.1.1)
midi-smtp-server (3.0.3)
mini_mime (1.1.2)
openssl (3.0.1)
rbtree (0.4.5)
set (1.0.3)
sorted_set (1.0.3)
rbtree
set (~> 1.0)
PLATFORMS
ruby
DEPENDENCIES
bunny
dotenv
mail
midi-smtp-server (~> 3.0.1)
openssl
RUBY VERSION
ruby 2.7.2p137
BUNDLED WITH
2.1.4

View File

@ -1,122 +0,0 @@
# frozen_string_literal: true
require 'dotenv/load'
require 'midi-smtp-server'
require 'mail'
require 'bunny'
# Server class
class BettermailProxy < MidiSmtpServer::Smtpd
def stop(wait_seconds_before_close: nil, gracefully: nil)
super()
@bunny_conn&.close
end
def bunny
if @bunny.nil?
@bunny_conn = Bunny.new
@bunny_conn.start
ch = @bunny_conn.create_channel
ch.direct(ENV['RABBIT_CHANNEL'])
ch.queue(ENV['RABBIT_MAIL_QUEUE'])
@bunny = ch.default_exchange
end
@bunny
end
# update local welcome and helo response
def on_connect_event(ctx)
ctx[:server][:local_response] = 'Client connected'
ctx[:server][:helo_response] = 'Welcome to the BetterMail proxy - an SMTP drop in replacement'
end
def on_message_data_headers_event(ctx)
# check and append all headers
ctx[:message][:data] << "X-bettermail: Received by proxy at #{Time.now}" << ctx[:message][:crlf]
end
# check the authentication
# if any value returned, that will be used for ongoing processing
# otherwise the original value will be used for authorization_id
def on_auth_event(ctx, authorization_id, authentication_id, authentication)
# to proceed this test use commands ...
# auth plain
# > AGFkbWluaXN0cmF0b3IAcGFzc3dvcmQ=
# auth login
# > YWRtaW5pc3RyYXRvcg==
# > cGFzc3dvcmQ=
# if authorization_id == '' && authentication_id == 'administrator' && authentication == 'password'
# # yes
# return 'supervisor'
# end
# # otherwise exit with authentication exception
# raise MidiSmtpServer::Smtpd535Exception
logger.debug("Authenticated id: #{authentication_id} with authentication: #{authentication} from: #{ctx[:server][:remote_ip]}:#{ctx[:server][:remote_port]}")
authentication_id
end
# get each message after DATA <message> .
def on_message_data_event(ctx)
# Just decode message once to make sure, that this message ist readable
mail = Mail.read_from_string(ctx[:message][:data])
logger.info('Message good. Sending to queue for processing')
bunny.publish(mail.to_s, :headers => { 'x-smtp' => mail.header.to_s }, :routing_key => ENV["RABBIT_MAIL_QUEUE"])
logger.info('Done')
end
end
# Create a new server instance for listening at localhost interfaces 127.0.0.1:2525
# and accepting a maximum of 4 simultaneous connections per default
service = BettermailProxy.new(
ports: ENV['PROXY_PORTS'],
hosts: ENV['PROXY_HOSTS'],
max_processings: 10,
auth_mode: :AUTH_REQUIRED,
tls_mode: :TLS_REQUIRED,
tls_cert_path: './ssl/cert.pem',
tls_key_path: './ssl/key.pem'
)
service_name='BetterMail::SMTPProxy'
# Create a new server instance for listening at localhost interfaces 127.0.0.1:2525
# and accepting a maximum of 4 simultaneous connections per default
# save flag for Ctrl-C pressed
flag_status_ctrl_c_pressed = false
# try to gracefully shutdown on Ctrl-C
trap('INT') do
# print an empty line right after ^C
puts
# notify flag about Ctrl-C was pressed
flag_status_ctrl_c_pressed = true
# signal exit to app
exit(0)
end
# Output for debug
puts("Starting #{service_name}")
# setup exit code
at_exit do
# check to shutdown connection
if service
# Output for debug
puts('Ctrl-C interrupted, exit now...') if flag_status_ctrl_c_pressed
# info about shutdown
puts("Shutdown #{service_name}...")
# stop all threads and connections gracefully
service.stop
end
# Output for debug
puts "#{service_name} stopped!"
end
# Start the server
service.start
# Run on server forever
service.join

View File

@ -1 +0,0 @@
ruby ./bettermail_proxy.rb

8
services/.gitattributes vendored Normal file
View File

@ -0,0 +1,8 @@
# See https://git-scm.com/docs/gitattributes for more about git attribute files.
# Mark the database schema as having been generated.
db/schema.rb linguist-generated
# Mark any vendored files as having been vendored.
vendor/* linguist-vendored

30
services/.gitignore vendored Normal file
View File

@ -0,0 +1,30 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-*
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep
/public/assets
.byebug_history
# Ignore master key for decrypting credentials and more.
/config/master.key

1
services/.ruby-gemset Normal file
View File

@ -0,0 +1 @@
bettermail_services

17
services/Gemfile Normal file
View File

@ -0,0 +1,17 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.2'
gem 'dotenv-rails'
gem 'bunny'
gem 'mail'
gem 'pg'
gem 'rails', '~> 6.1.7'
gem 'midi-smtp-server', '~> 3.0.1'
gem 'openssl'
group :development do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'listen', '~> 3.3'
gem 'spring'
end

173
services/Gemfile.lock Normal file
View File

@ -0,0 +1,173 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (6.1.7)
actionpack (= 6.1.7)
activesupport (= 6.1.7)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.7)
actionpack (= 6.1.7)
activejob (= 6.1.7)
activerecord (= 6.1.7)
activestorage (= 6.1.7)
activesupport (= 6.1.7)
mail (>= 2.7.1)
actionmailer (6.1.7)
actionpack (= 6.1.7)
actionview (= 6.1.7)
activejob (= 6.1.7)
activesupport (= 6.1.7)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.7)
actionview (= 6.1.7)
activesupport (= 6.1.7)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.7)
actionpack (= 6.1.7)
activerecord (= 6.1.7)
activestorage (= 6.1.7)
activesupport (= 6.1.7)
nokogiri (>= 1.8.5)
actionview (6.1.7)
activesupport (= 6.1.7)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.1.7)
activesupport (= 6.1.7)
globalid (>= 0.3.6)
activemodel (6.1.7)
activesupport (= 6.1.7)
activerecord (6.1.7)
activemodel (= 6.1.7)
activesupport (= 6.1.7)
activestorage (6.1.7)
actionpack (= 6.1.7)
activejob (= 6.1.7)
activerecord (= 6.1.7)
activesupport (= 6.1.7)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (6.1.7)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
amq-protocol (2.3.2)
builder (3.2.4)
bunny (2.19.0)
amq-protocol (~> 2.3, >= 2.3.1)
sorted_set (~> 1, >= 1.0.2)
concurrent-ruby (1.1.10)
crass (1.0.6)
dotenv (2.8.1)
dotenv-rails (2.8.1)
dotenv (= 2.8.1)
railties (>= 3.2)
erubi (1.11.0)
ffi (1.15.5)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
listen (3.7.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loofah (2.19.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
method_source (1.0.0)
midi-smtp-server (3.0.3)
mini_mime (1.1.2)
mini_portile2 (2.8.0)
minitest (5.16.3)
nio4r (2.5.8)
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
openssl (3.0.1)
pg (1.4.4)
racc (1.6.0)
rack (2.2.4)
rack-test (2.0.2)
rack (>= 1.3)
rails (6.1.7)
actioncable (= 6.1.7)
actionmailbox (= 6.1.7)
actionmailer (= 6.1.7)
actionpack (= 6.1.7)
actiontext (= 6.1.7)
actionview (= 6.1.7)
activejob (= 6.1.7)
activemodel (= 6.1.7)
activerecord (= 6.1.7)
activestorage (= 6.1.7)
activesupport (= 6.1.7)
bundler (>= 1.15.0)
railties (= 6.1.7)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.3)
loofah (~> 2.3)
railties (6.1.7)
actionpack (= 6.1.7)
activesupport (= 6.1.7)
method_source
rake (>= 12.2)
thor (~> 1.0)
rake (13.0.6)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbtree (0.4.5)
set (1.0.3)
sorted_set (1.0.3)
rbtree
set (~> 1.0)
spring (4.1.0)
sprockets (4.1.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
thor (1.2.1)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.6)
PLATFORMS
ruby
DEPENDENCIES
bunny
dotenv-rails
listen (~> 3.3)
mail
midi-smtp-server (~> 3.0.1)
openssl
pg
rails (~> 6.1.7)
spring
RUBY VERSION
ruby 2.7.2p137
BUNDLED WITH
2.1.4

24
services/README.md Normal file
View File

@ -0,0 +1,24 @@
# README
This README would normally document whatever steps are necessary to get the
application up and running.
Things you may want to cover:
* Ruby version
* System dependencies
* Configuration
* Database creation
* Database initialization
* How to run the test suite
* Services (job queues, cache servers, search engines, etc.)
* Deployment instructions
* ...

6
services/Rakefile Normal file
View File

@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require_relative "config/application"
Rails.application.load_tasks

View File

View File

@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

View File

View File

@ -0,0 +1,4 @@
class MailRecipient < ApplicationRecord
belongs_to :received_mail
belongs_to :recipient
end

View File

@ -0,0 +1,4 @@
class ReceivedMail < ApplicationRecord
belongs_to :sender
has_many :mail_recipients
end

View File

@ -0,0 +1,3 @@
class Recipient < ApplicationRecord
has_many :emails, foreign_key: :recipient_id, class_name: 'MailRecipient'
end

View File

@ -0,0 +1,3 @@
class Sender < ApplicationRecord
has_many :sent_mails, foreign_key: :sender_id, class_name: 'ReceivedMail'
end

114
services/bin/bundle Executable file
View File

@ -0,0 +1,114 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'bundle' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require "rubygems"
m = Module.new do
module_function
def invoked_as_script?
File.expand_path($0) == File.expand_path(__FILE__)
end
def env_var_version
ENV["BUNDLER_VERSION"]
end
def cli_arg_version
return unless invoked_as_script? # don't want to hijack other binstubs
return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
bundler_version = nil
update_index = nil
ARGV.each_with_index do |a, i|
if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
bundler_version = a
end
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
bundler_version = $1
update_index = i
end
bundler_version
end
def gemfile
gemfile = ENV["BUNDLE_GEMFILE"]
return gemfile if gemfile && !gemfile.empty?
File.expand_path("../../Gemfile", __FILE__)
end
def lockfile
lockfile =
case File.basename(gemfile)
when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
else "#{gemfile}.lock"
end
File.expand_path(lockfile)
end
def lockfile_version
return unless File.file?(lockfile)
lockfile_contents = File.read(lockfile)
return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
Regexp.last_match(1)
end
def bundler_requirement
@bundler_requirement ||=
env_var_version || cli_arg_version ||
bundler_requirement_for(lockfile_version)
end
def bundler_requirement_for(version)
return "#{Gem::Requirement.default}.a" unless version
bundler_gem_version = Gem::Version.new(version)
requirement = bundler_gem_version.approximate_recommendation
return requirement unless Gem.rubygems_version < Gem::Version.new("2.7.0")
requirement += ".a" if bundler_gem_version.prerelease?
requirement
end
def load_bundler!
ENV["BUNDLE_GEMFILE"] ||= gemfile
activate_bundler
end
def activate_bundler
gem_error = activation_error_handling do
gem "bundler", bundler_requirement
end
return if gem_error.nil?
require_error = activation_error_handling do
require "bundler/version"
end
return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION))
warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`"
exit 42
end
def activation_error_handling
yield
nil
rescue StandardError, LoadError => e
e
end
end
m.load_bundler!
if m.invoked_as_script?
load Gem.bin_path("bundler", "bundle")
end

4
services/bin/rails Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative "../config/boot"
require "rails/commands"

4
services/bin/rake Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require_relative "../config/boot"
require "rake"
Rake.application.run

33
services/bin/setup Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env ruby
require "fileutils"
# path to your application root.
APP_ROOT = File.expand_path('..', __dir__)
def system!(*args)
system(*args) || abort("\n== Command #{args} failed ==")
end
FileUtils.chdir APP_ROOT do
# This script is a way to set up or update your development environment automatically.
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
# Add necessary setup steps to this file.
puts '== Installing dependencies =='
system! 'gem install bundler --conservative'
system('bundle check') || system!('bundle install')
# puts "\n== Copying sample files =="
# unless File.exist?('config/database.yml')
# FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
# end
puts "\n== Preparing database =="
system! 'bin/rails db:prepare'
puts "\n== Removing old logs and tempfiles =="
system! 'bin/rails log:clear tmp:clear'
puts "\n== Restarting application server =="
system! 'bin/rails restart'
end

6
services/config.ru Normal file
View File

@ -0,0 +1,6 @@
# This file is used by Rack-based servers to start the application.
require_relative "config/environment"
run Rails.application
Rails.application.load_server

View File

@ -0,0 +1,40 @@
require_relative "boot"
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_record/railtie"
# require "active_job/railtie"
# require "active_storage/engine"
# require "action_controller/railtie"
# require "action_mailer/railtie"
# require "action_mailbox/engine"
# require "action_text/engine"
# require "action_view/railtie"
# require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Consumer
class Application < Rails::Application
config.autoload_paths << Rails.root.join('lib')
config.eager_load_paths << Rails.root.join('lib')
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
# Don't generate system test files.
config.generators.system_tests = nil
end
end

3
services/config/boot.rb Normal file
View File

@ -0,0 +1,3 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require "bundler/setup" # Set up gems listed in the Gemfile.

View File

@ -0,0 +1 @@
3bgXrcBmEQAiInnCmLeo+uzTTZv/XdKvZnr5agIilz+zG5sdTcS6NKTDodS3Z9zla1eH/YPVW+b/yZbTpqf56hu/OY4WO09NA1lWsrWxIgstXCZA6GxUOMnLs1naaYuyV5L/QUo0nvL2HER3wimYr8oG0uF50tRSBdnU21XMXmrKue6noNy1FHfXzRbmqcr2DrYaEPDfEBgFVCtaRR3/lcHstUIu5LgbKThgJFUY1NCkjRQUcGKD94rACQ1FaVoJw+33syRnEEVqEE/pJDX03U+domMLf7JLsu9BqAiBxtyiy/ZmaqZ+bP+R/NiXzuLxrc7x4Lup0cExBalfRwf0PLmOYOoRfuFOhuOtkYRL7dwIVkgm8uf+vtBWLLNFdwwSgK3BsnZqp2HdUH9iLaJUBZEIxuJe0tUj3avg--3B+JnjSZfI0pkw11--gObP0g/Ae5hz20kfQwnfhA==

View File

@ -0,0 +1,5 @@
# Load the Rails application.
require_relative "application"
# Initialize the Rails application.
Rails.application.initialize!

View File

@ -0,0 +1,68 @@
require "active_support/core_ext/integer/time"
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded any time
# it changes. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports.
config.consider_all_requests_local = true
# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise exceptions for disallowed deprecations.
config.active_support.disallowed_deprecation = :raise
# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Suppress logger output for asset requests.
config.assets.quiet = true
# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true
# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true
end

View File

@ -0,0 +1,103 @@
require "active_support/core_ext/integer/time"
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
# config.require_master_key = true
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Compress CSS using a preprocessor.
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.asset_host = 'http://assets.example.com'
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# Include generic and useful information about system operation, but avoid logging too much
# information to avoid inadvertent exposure of personally identifiable information (PII).
config.log_level = :info
# Prepend all log lines with the following tags.
config.log_tags = [ :request_id ]
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Log disallowed deprecations.
config.active_support.disallowed_deprecation = :log
# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
# Use a different logger for distributed setups.
# require "syslog/logger"
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
# Inserts middleware to perform automatic connection switching.
# The `database_selector` hash is used to pass options to the DatabaseSelector
# middleware. The `delay` is used to determine how long to wait after a write
# to send a subsequent read to the primary.
#
# The `database_resolver` class is used by the middleware to determine which
# database is appropriate to use based on the time delay.
#
# The `database_resolver_context` class is used by the middleware to set
# timestamps for the last write to the primary. The resolver uses the context
# class timestamps to determine how long to wait before reading from the
# replica.
#
# By default Rails will store a last write timestamp in the session. The
# DatabaseSelector middleware is designed as such you can define your own
# strategy for connection switching and pass that into the middleware through
# these configuration options.
# config.active_record.database_selector = { delay: 2.seconds }
# config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
# config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
end

View File

@ -0,0 +1,49 @@
require "active_support/core_ext/integer/time"
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
}
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.cache_store = :null_store
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
# Raise exceptions for disallowed deprecations.
config.active_support.disallowed_deprecation = :raise
# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []
# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true
# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true
end

View File

@ -0,0 +1,33 @@
# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
# I18n.t 'hello'
#
# In views, this is aliased to just `t`:
#
# <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
# I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# The following keys must be escaped otherwise they will not be retrieved by
# the default I18n backend:
#
# true, false, on, off, yes, no
#
# Instead, surround them with single quotes.
#
# en:
# 'true': 'foo'
#
# To learn more, please read the Rails Internationalization guide
# available at https://guides.rubyonrails.org/i18n.html.
en:
hello: "Hello world"

43
services/config/puma.rb Normal file
View File

@ -0,0 +1,43 @@
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

View File

@ -0,0 +1,3 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

View File

@ -0,0 +1,39 @@
class Setup < ActiveRecord::Migration[6.1]
def change
enable_extension 'uuid-ossp'
enable_extension 'pgcrypto'
create_table :senders, id: :string do |t|
t.timestamps
end
create_table :recipients, id: :string do |t|
t.string :verification_status, default: 'unverified', nullable: false
t.datetime :last_verified_at, nullable: true
t.string :bounce_status , default: 'unknown'
t.datetime :bounce_status_changed_at, nullable: true
t.timestamps
end
create_table :received_mails, id: :uuid do |t|
t.string :sender_id, nullable: false
t.string :message_id, nullable: false
t.binary :body, nullable: false
t.string :delivery_status, default: 'queued', nullable: false
t.string :delivery_status_changed_at
t.timestamps
end
create_table :mail_recipients, id: false do |t|
t.uuid :received_mail_id
t.string :recipient_id
t.string :delivery_status, default: 'delivered', nullable: false
t.string :delivery_status_changed_at
t.timestamps
end
add_index :received_mails, :sender_id
add_index :received_mails, :message_id, unique: true
add_index :mail_recipients, %i[received_mail_id recipient_id], unique: true
end
end

56
services/db/schema.rb generated Normal file
View File

@ -0,0 +1,56 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_11_15_164901) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
enable_extension "uuid-ossp"
create_table "mail_recipients", id: false, force: :cascade do |t|
t.uuid "received_mail_id"
t.string "recipient_id"
t.string "delivery_status", default: "delivered"
t.string "delivery_status_changed_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["received_mail_id", "recipient_id"], name: "index_mail_recipients_on_received_mail_id_and_recipient_id", unique: true
end
create_table "received_mails", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "sender_id"
t.string "message_id"
t.binary "body"
t.string "delivery_status", default: "queued"
t.string "delivery_status_changed_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["message_id"], name: "index_received_mails_on_message_id", unique: true
t.index ["sender_id"], name: "index_received_mails_on_sender_id"
end
create_table "recipients", id: :string, force: :cascade do |t|
t.string "verification_status", default: "unverified"
t.datetime "last_verified_at"
t.string "bounce_status", default: "unknown"
t.datetime "bounce_status_changed_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "senders", id: :string, force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

7
services/db/seeds.rb Normal file
View File

@ -0,0 +1,7 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

View File

View File

@ -0,0 +1,63 @@
# frozen_string_literal: true
require 'mail'
require 'bunny'
# in module for neatness
module Bettermail
# The General purpose consumer
class Consumer
def initialize
end
def default_smtp_settings
{
address: ENV['DEFAULT_SMTP_HOST'],
port: ENV['DEFAULT_SMTP_PORT'],
user_name: ENV['DEFAULT_SMTP_UID'],
password: ENV['DEFAULT_SMTP_PWD'],
enable_starttls_auto: true,
authentication: 'plain',
}
end
def start
begin
puts ' [*] Waiting for messages. To exit press CTRL+C'
new_mail_queue = bunny_channel.queue(ENV['RABBIT_MAIL_QUEUE'])
new_mail_queue.subscribe(block: true) do |_delivery_info, _properties, body|
instructions = JSON.parse(body)
recipient = Recipient.create_or_find_by(id: instructions["recipient"])
email = ReceivedMail.find_by(message_id: instructions["message"])
recipient.emails.create(received_mail: email, delivery_status: 'queued')
end
sns_queue = bunny_channel.queue(ENV['RABBIT_SNS_QUEUE'])
sns_queue.subscribe(block: true) do |_delivery_info, _properties, body|
puts " [x] Received #{_delivery_info}"
end
rescue Interrupt => _
stop
end
end
def send_mail(mail)
mail.delivery_method :smtp, default_smtp_settings
mail.deliver!
end
def stop
@bunny_conn&.close
end
def bunny_channel
if @bunny_channel.nil?
@bunny_conn = Bunny.new
@bunny_conn.start
@bunny_channel = @bunny_conn.create_channel
end
@bunny_channel
end
end
end

View File

@ -0,0 +1,79 @@
# frozen_string_literal: true
require 'midi-smtp-server'
require 'mail'
require 'bunny'
require 'securerandom'
# in module for neatness
module Bettermail
# The SMTPProxy
class Proxy < MidiSmtpServer::Smtpd
def stop(wait_seconds_before_close: nil, gracefully: nil)
super()
@bunny_conn&.close
end
def bunny
if @bunny.nil?
@bunny_conn = Bunny.new
@bunny_conn.start
ch = @bunny_conn.create_channel
ch.direct(ENV['RABBIT_CHANNEL'])
ch.queue(ENV['RABBIT_MAIL_QUEUE'])
@bunny = ch.default_exchange
end
@bunny
end
# update local welcome and helo response
def on_connect_event(ctx)
ctx[:server][:local_response] = 'Client connected'
ctx[:server][:helo_response] = 'Welcome to the BetterMail proxy - an SMTP drop in replacement'
end
def on_message_data_headers_event(ctx)
# check and append all headers
ctx[:message][:data] << "X-bettermail: Received by proxy at #{Time.now}" << ctx[:message][:crlf]
end
# check the authentication
# if any value returned, that will be used for ongoing processing
# otherwise the original value will be used for authorization_id
def on_auth_event(ctx, authorization_id, authentication_id, authentication)
# to proceed this test use commands ...
# auth plain
# > AGFkbWluaXN0cmF0b3IAcGFzc3dvcmQ=
# auth login
# > YWRtaW5pc3RyYXRvcg==
# > cGFzc3dvcmQ=
# if authorization_id == '' && authentication_id == 'administrator' && authentication == 'password'
# # yes
# return 'supervisor'
# end
# # otherwise exit with authentication exception
# raise MidiSmtpServer::Smtpd535Exception
logger.debug("Authenticated id: #{authentication_id} with authentication: #{authentication} from: #{ctx[:server][:remote_ip]}:#{ctx[:server][:remote_port]}")
authentication_id
end
def save_mail(ctx, sender)
mail = Mail.read_from_string(ctx[:message][:data])
sender = Sender.create_or_find_by(id: sender)
message_id = "#{SecureRandom.uuid}@bettermail.wizewerx.tech"
saved_mail = sender.sent_mails.create(message_id: message_id, body: mail.to_s)
message_id
end
def on_mail_from_event(ctx, mail_from_data)
# tag the message here OR write it once to the database
ctx[:message][:message_id] = save_mail(ctx, mail_from_data)
end
# simple rewrite and return value
def on_rcpt_to_event(ctx, data)
bunny.publish({message: ctx[:message][:message_id], recipient: data}.to_json, :headers => { }, :routing_key => ENV["RABBIT_MAIL_QUEUE"])
end
end
end

0
services/lib/tasks/.keep Normal file
View File

View File

@ -0,0 +1,46 @@
namespace :consumer do
task start: :environment do
service_name = 'BetterMail::Consumer'
# Create a new server instance for listening at localhost interfaces 127.0.0.1:2525
# and accepting a maximum of 4 simultaneous connections per default
service = Bettermail::Consumer.new
# save flag for Ctrl-C pressed
flag_status_ctrl_c_pressed = false
# try to gracefully shutdown on Ctrl-C
trap('INT') do
# print an empty line right after ^C
puts
# notify flag about Ctrl-C was pressed
flag_status_ctrl_c_pressed = true
# signal exit to app
exit 0
end
# Output for debug
puts("Starting #{service_name}")
# setup exit code
at_exit do
# check to shutdown connection
if service
# Output for debug
puts('Ctrl-C interrupted, exit now...') if flag_status_ctrl_c_pressed
# info about shutdown
puts("Shutdown #{service_name}...")
# stop all threads and connections gracefully
service.stop
end
# Output for debug
puts "#{service_name} stopped!"
end
# Start the server
service.start
# Run on server forever
service.join
end
end

View File

@ -0,0 +1,58 @@
namespace :proxy do
task start: :environment do
# Create a new server instance for listening at localhost interfaces 127.0.0.1:2525
# and accepting a maximum of 4 simultaneous connections per default
service = Bettermail::Proxy.new(
ports: ENV['PROXY_PORTS'],
hosts: ENV['PROXY_HOSTS'],
max_processings: 10,
auth_mode: :AUTH_REQUIRED,
tls_mode: :TLS_REQUIRED,
tls_cert_path: './ssl/cert.pem',
tls_key_path: './ssl/key.pem'
)
service_name = 'BetterMail::SMTPProxy'
# Create a new server instance for listening at localhost interfaces 127.0.0.1:2525
# and accepting a maximum of 4 simultaneous connections per default
# save flag for Ctrl-C pressed
flag_status_ctrl_c_pressed = false
# try to gracefully shutdown on Ctrl-C
trap('INT') do
# print an empty line right after ^C
puts
# notify flag about Ctrl-C was pressed
flag_status_ctrl_c_pressed = true
# signal exit to app
exit(0)
end
# Output for debug
puts("Starting #{service_name}")
# setup exit code
at_exit do
# check to shutdown connection
if service
# Output for debug
puts('Ctrl-C interrupted, exit now...') if flag_status_ctrl_c_pressed
# info about shutdown
puts("Shutdown #{service_name}...")
# stop all threads and connections gracefully
service.stop
end
# Output for debug
puts "#{service_name} stopped!"
end
# Start the server
service.start
# Run on server forever
service.join
end
end

0
services/log/.keep Normal file
View File

0
services/tmp/.keep Normal file
View File

0
services/tmp/pids/.keep Normal file
View File

0
services/vendor/.keep vendored Normal file
View File

1
tests/.ruby-gemset Normal file
View File

@ -0,0 +1 @@
bettermail_tests

View File

@ -1,6 +1,5 @@
source 'https://rubygems.org'
ruby '2.7.2'
gem 'dotenv'
gem 'bunny'
gem 'mail'
gem 'pg'

View File

@ -5,11 +5,9 @@ GEM
bunny (2.19.0)
amq-protocol (~> 2.3, >= 2.3.1)
sorted_set (~> 1, >= 1.0.2)
dotenv (2.8.1)
mail (2.7.1)
mini_mime (>= 0.1.1)
mini_mime (1.1.2)
pg (1.4.4)
rbtree (0.4.5)
set (1.0.3)
sorted_set (1.0.3)
@ -21,9 +19,7 @@ PLATFORMS
DEPENDENCIES
bunny
dotenv
mail
pg
RUBY VERSION
ruby 2.7.2p137

View File

@ -0,0 +1,31 @@
# frozen_string_literal: true
require 'mail'
require 'bunny'
require 'securerandom'
@default_smtp_settings = {
address: 'localhost',
port: '2025',
user_name: 'hello',
password: 'world',
enable_starttls_auto: true,
authentication: 'plain',
openssl_verify_mode: 'none'
}
def send_mail(index)
mail = Mail.new(
from: "test-#{index}-#{SecureRandom.uuid}@thefactory.local",
to: "#{SecureRandom.uuid}@acme.local",
cc: "cc-#{SecureRandom.uuid}@acme.local",
bcc: "bcc-#{SecureRandom.uuid}@acme.local",
subject: "Testing #{index}",
)
mail.delivery_method :smtp, @default_smtp_settings
mail.deliver!
end
number_of_emails = ARGV[0] || 1
send_mail number_of_emails