-
Notifications
You must be signed in to change notification settings - Fork 51
Adding support for TLS connections from client to amqproxy #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chadknutson
wants to merge
1
commit into
main
Choose a base branch
from
tls_listener
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| require "socket" | ||
|
|
||
| module AMQProxy | ||
| class ConnectionInfo | ||
| getter remote_address : IPAddress | ||
| getter local_address : IPAddress | ||
| property? ssl : Bool = false | ||
| property? ssl_verify : Bool = false | ||
| property ssl_version : String? | ||
| property ssl_cipher : String? | ||
| property ssl_key_alg : String? | ||
| property ssl_sig_alg : String? | ||
|
Comment on lines
+3
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LMQ codebase spotted!!! 🔥 |
||
| property ssl_cn : String? | ||
|
|
||
| # Remote and local addresses from the server's perspective | ||
| def initialize(remote_address, local_address) | ||
| @remote_address = IPAddress.new(remote_address) | ||
| @local_address = IPAddress.new(local_address) | ||
| end | ||
|
|
||
| def self.local | ||
| src = Socket::IPAddress.new("127.0.0.1", 0) | ||
| dst = Socket::IPAddress.new("127.0.0.1", 0) | ||
| new(src, dst) | ||
| end | ||
|
|
||
| # Suspecting memory problem with Socket::IPAddress in Crystal 1.15.0 | ||
| struct IPAddress | ||
| getter address : String | ||
| getter port : UInt16 | ||
|
|
||
| def initialize(ip_address : Socket::IPAddress) | ||
| @address = ip_address.address | ||
| @port = ip_address.port.to_u16! | ||
| end | ||
|
|
||
| def to_s(io) | ||
| io << @address << ':' << @port | ||
| end | ||
|
|
||
| def loopback? | ||
| @address == "::1" || @address.starts_with? "127." | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,14 @@ require "uri" | |
| require "./channel_pool" | ||
| require "./client" | ||
| require "./upstream" | ||
| require "./connection_info" | ||
|
|
||
| module AMQProxy | ||
| class Server | ||
| Log = ::Log.for(self) | ||
| @clients_lock = Mutex.new | ||
| @clients = Array(Client).new | ||
| @closed = false | ||
|
|
||
| def self.new(url : URI) | ||
| tls = url.scheme == "amqps" | ||
|
|
@@ -46,15 +48,65 @@ module AMQProxy | |
| loop do | ||
| socket = server.accept? || break | ||
| begin | ||
| addr = socket.remote_address | ||
| spawn handle_connection(socket, addr), name: "Client#read_loop #{addr}" | ||
| remote_addr = socket.remote_address | ||
| set_socket_options(socket) | ||
| conn_info = ConnectionInfo.new(remote_addr, socket.local_address) | ||
| spawn handle_connection(socket, conn_info), name: "Client#read_loop #{remote_addr}" | ||
| rescue IO::Error | ||
| next | ||
| end | ||
| end | ||
| Log.info { "Proxy stopping accepting connections" } | ||
| end | ||
|
|
||
| def listen_tls(address, port, context) | ||
| listen_tls(TCPServer.new(address, port), context) | ||
| end | ||
|
|
||
| def listen_tls(s : TCPServer, context) | ||
| # @listeners[s] = protocol | ||
| Log.info { "Listening on #{s.local_address} (TLS)" } | ||
| loop do # do not try to use while | ||
| client = s.accept? || break | ||
| next client.close if @closed | ||
| accept_tls(client, context) | ||
| end | ||
| rescue ex : IO::Error | OpenSSL::Error | ||
| abort "Unrecoverable error in TLS listener: #{ex.inspect_with_backtrace}" | ||
| # ensure | ||
| # @listeners.delete(s) | ||
|
Comment on lines
+76
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for those comments? |
||
| end | ||
|
|
||
| private def accept_tls(client, context) | ||
| if context | ||
| spawn(name: "Accept TLS socket") do | ||
| remote_addr = client.remote_address | ||
| set_socket_options(client) | ||
| ssl_client = OpenSSL::SSL::Socket::Server.new(client, context, sync_close: true) | ||
| # ssl_client = OpenSSL::SSL::Socket.new(client, context, sync_close: true) | ||
| set_buffer_size(ssl_client) | ||
| Log.debug { "#{remote_addr} connected with #{ssl_client.tls_version} #{ssl_client.cipher}" } | ||
| conn_info = ConnectionInfo.new(remote_addr, client.local_address) | ||
| conn_info.ssl = true | ||
| conn_info.ssl_version = ssl_client.tls_version | ||
| conn_info.ssl_cipher = ssl_client.cipher | ||
| handle_connection(ssl_client, conn_info) | ||
| rescue ex | ||
| Log.warn(exception: ex) { "Error accepting TLS connection from #{remote_addr}" } | ||
| client.close rescue nil | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def listen_tls(bind, port, context) | ||
| listen_tls(TCPServer.new(bind, port), context) | ||
| end | ||
|
|
||
| private def set_buffer_size(socket) | ||
| socket.sync = true | ||
| socket.read_buffering = false | ||
| end | ||
|
|
||
| def stop_accepting_clients | ||
| @server.try &.close | ||
| end | ||
|
|
@@ -73,20 +125,35 @@ module AMQProxy | |
| end | ||
| end | ||
|
|
||
| private def handle_connection(socket, remote_address) | ||
| c = Client.new(socket) | ||
| private def handle_connection(socket, connection_info) | ||
| c = Client.new(socket, connection_info) | ||
| active_client(c) do | ||
| channel_pool = @channel_pools[c.credentials] | ||
| c.read_loop(channel_pool) | ||
| end | ||
| rescue IO::EOFError | ||
| # Client closed connection before/while negotiating | ||
| rescue ex # only raise from constructor, when negotating | ||
| Log.debug(exception: ex) { "Client negotiation failure (#{remote_address}) #{ex.inspect}" } | ||
| Log.debug(exception: ex) { "Client negotiation failure (#{connection_info.remote_address}) #{ex.inspect}" } | ||
| ensure | ||
| socket.close rescue nil | ||
| end | ||
|
|
||
| private def set_socket_options(socket) | ||
| # Note: Very minimal support for socket options for now | ||
| unless socket.remote_address.loopback? | ||
| # if keepalive = @config.tcp_keepalive | ||
| socket.keepalive = true | ||
| socket.tcp_keepalive_idle = 60 | ||
| socket.tcp_keepalive_interval = 10 | ||
| socket.tcp_keepalive_count = 3 | ||
| # end | ||
| end | ||
| socket.tcp_nodelay = true # if @config.tcp_nodelay? | ||
| # @config.tcp_recv_buffer_size.try { |v| socket.recv_buffer_size = v } | ||
| # @config.tcp_send_buffer_size.try { |v| socket.send_buffer_size = v } | ||
| end | ||
|
|
||
| private def active_client(client, &) | ||
| @clients_lock.synchronize do | ||
| @clients << client | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm i might be missing some Crystal. But I dont understand this??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copy/pasted much of the TLS functionality from LavinMQ. Commented lines (mentioned in your other comment) are from a testing procedure -- they either did not work or were not necessary. Such commented lines should be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a nil guard.
@tls_contextmay be nil, but the thanks to the guardtlswon't be nil.