Skip to content
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

Be more reslient when identifying PROXY protocol connections #818

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions spec/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1288,4 +1288,15 @@ describe LavinMQ::Server do
end
end
end

it "can handle non valid protocol starts" do
LavinMQ::Config.instance.clustering = true
with_amqp_server do |s|
t = TCPSocket.new("localhost", amqp_port(s))
t.print "HTTP"
buf = Bytes.new 8
t.read(buf)
buf.should eq Bytes['A'.ord, 'M'.ord, 'Q'.ord, 'P'.ord, 0, 0, 9, 1]
end
end
end
7 changes: 5 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ end

def with_channel(s : LavinMQ::Server, file = __FILE__, line = __LINE__, **args, &)
name = "lavinmq-spec-#{file}:#{line}"
port = [email protected](TCPServer).first.local_address.port
args = {port: port, name: name}.merge(args)
args = {port: amqp_port(s), name: name}.merge(args)
conn = AMQP::Client.new(**args).connect
ch = conn.channel
yield ch
ensure
conn.try &.close(no_wait: false)
end

def amqp_port(s)
[email protected](TCPServer).first.local_address.port
end

def should_eventually(expectation, timeout = 5.seconds, file = __FILE__, line = __LINE__, &)
sec = Time.monotonic
loop do
Expand Down
7 changes: 5 additions & 2 deletions src/lavinmq/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ module LavinMQ
when 1 then ProxyProtocol::V1.parse(client)
when 2 then ProxyProtocol::V2.parse(client)
else
if client.peek[0, 5] == "PROXY".to_slice &&
# Allow proxy connection from followers
if Config.instance.clustering? &&
client.peek[0, 5]? == "PROXY".to_slice &&
followers.any? { |f| f.remote_address.address == remote_address.address }
# Expect PROXY protocol header if remote address is a follower
ProxyProtocol::V1.parse(client)
elsif client.peek[0, 8] == ProxyProtocol::V2::Signature.to_slice[0, 8] &&
elsif Config.instance.clustering? &&
client.peek[0, 8]? == ProxyProtocol::V2::Signature.to_slice[0, 8] &&
followers.any? { |f| f.remote_address.address == remote_address.address }
# Expect PROXY protocol header if remote address is a follower
ProxyProtocol::V2.parse(client)
Expand Down
Loading