basic proxy and consumer done

This commit is contained in:
Mike Sutton 2022-11-15 01:03:15 +01:00
parent 14e163a1a5
commit d3be685b08
15 changed files with 334 additions and 45 deletions

View File

@ -50,7 +50,7 @@ steps:
- export BM_COMPONENT=portal
- export BUILD_TARGET=wizewerx/bettermail_${BM_COMPONENT}:${DRONE_COMMIT_BRANCH}
- docker pull $BUILD_TARGET
- docker build --cache-from $BUILD_TARGET --build-arg GEM_CACHE=$BUILD_TARGET -f ./consumer/docker/Dockerfile -t $BUILD_TARGET .
- docker build --cache-from $BUILD_TARGET --build-arg GEM_CACHE=$BUILD_TARGET -f ./${BM_COMPONENT}/docker/Dockerfile -t $BUILD_TARGET .
- docker push $BUILD_TARGET
- name: proxy
image: wizewerx/wizewerx-docker-compose
@ -65,7 +65,7 @@ steps:
- export BM_COMPONENT=proxy
- export BUILD_TARGET=wizewerx/bettermail_${BM_COMPONENT}:${DRONE_COMMIT_BRANCH}
- docker pull $BUILD_TARGET
- docker build --cache-from $BUILD_TARGET --build-arg GEM_CACHE=$BUILD_TARGET -f ./consumer/docker/Dockerfile -t $BUILD_TARGET .
- docker build --cache-from $BUILD_TARGET --build-arg GEM_CACHE=$BUILD_TARGET -f ./${BM_COMPONENT}/docker/Dockerfile -t $BUILD_TARGET .
- docker push $BUILD_TARGET
- name: notifications
image: wizewerx/wizewerx-docker-compose
@ -80,7 +80,7 @@ steps:
- export BM_COMPONENT=notifications
- export BUILD_TARGET=wizewerx/bettermail_${BM_COMPONENT}:${DRONE_COMMIT_BRANCH}
- docker pull $BUILD_TARGET
- docker build --cache-from $BUILD_TARGET --build-arg GEM_CACHE=$BUILD_TARGET -f ./consumer/docker/Dockerfile -t $BUILD_TARGET .
- docker build --cache-from $BUILD_TARGET --build-arg GEM_CACHE=$BUILD_TARGET -f ./${BM_COMPONENT}/docker/Dockerfile -t $BUILD_TARGET .
- docker push $BUILD_TARGET
- name: notify_end
image: plugins/slack

1
consumer/.ruby-gemset Normal file
View File

@ -0,0 +1 @@
bettermail_consumer

1
consumer/.ruby-version Normal file
View File

@ -0,0 +1 @@
ruby-2.7.2

View File

@ -1,15 +1,6 @@
source 'https://rubygems.org'
ruby '2.6.5'
gem 'dotenv-rails'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
group :development do
gem 'web-console', '>= 3.3.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
ruby '2.7.2'
gem 'dotenv'
gem 'bunny'
gem 'mail'
gem 'pg'

32
consumer/Gemfile.lock Normal file
View File

@ -0,0 +1,32 @@
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)
mini_mime (1.1.2)
pg (1.4.4)
rbtree (0.4.5)
set (1.0.3)
sorted_set (1.0.3)
rbtree
set (~> 1.0)
PLATFORMS
ruby
DEPENDENCIES
bunny
dotenv
mail
pg
RUBY VERSION
ruby 2.7.2p137
BUNDLED WITH
2.1.4

View File

@ -0,0 +1,88 @@
# 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

1
consumer/start.sh Executable file
View File

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

1
proxy/.ruby-gemset Normal file
View File

@ -0,0 +1 @@
bettermail_proxy

1
proxy/.ruby-version Normal file
View File

@ -0,0 +1 @@
ruby-2.7.2

View File

@ -1,17 +1,7 @@
source 'https://rubygems.org'
ruby '2.6.5'
gem 'dotenv-rails'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
group :development do
gem 'web-console', '>= 3.3.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
ruby '2.7.2'
gem 'dotenv'
gem 'bunny'
gem 'midi-smtp-server', '~> 3.0.1'
gem 'openssl', '~> 2.1.0'
gem 'mail'
gem 'openssl'

34
proxy/Gemfile.lock Normal file
View File

@ -0,0 +1,34 @@
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,24 +1,87 @@
# 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])
# Publish to rabbit
@bunny_exchange.publish(mail.to_s, :headers => { 'x-smtp' => mail.header.to_s }, :routing_key => "new_email")
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
server = MySmtpd.new
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
@ -30,29 +93,30 @@ trap('INT') do
# notify flag about Ctrl-C was pressed
flag_status_ctrl_c_pressed = true
# signal exit to app
exit 0
exit(0)
end
# Output for debug
server.logger.info("Starting MySmtpd [#{MidiSmtpServer::VERSION::STRING}|#{MidiSmtpServer::VERSION::DATE}] (Basic usage) ...")
puts("Starting #{service_name}")
# setup exit code
at_exit do
# check to shutdown connection
if server
if service
# Output for debug
server.logger.info('Ctrl-C interrupted, exit now...') if flag_status_ctrl_c_pressed
puts('Ctrl-C interrupted, exit now...') if flag_status_ctrl_c_pressed
# info about shutdown
server.logger.info('Shutdown MySmtpd...')
puts("Shutdown #{service_name}...")
# stop all threads and connections gracefully
server.stop
service.stop
end
# Output for debug
server.logger.info('MySmtpd down!')
puts "#{service_name} stopped!"
end
# Start the server
server.start
service.start
# Run on server forever
server.join
service.join

32
proxy/ssl/cert.pem Normal file
View File

@ -0,0 +1,32 @@
-----BEGIN CERTIFICATE-----
MIIFbTCCA1WgAwIBAgIUAf9HBsl8FOGp9rGyrroaRmBra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