Skip to content

Commit 3ec0951

Browse files
committed
feat(server.cr): enable UNIXSockets AMQP protocol
1 parent cc880f9 commit 3ec0951

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/lavinmq/connection_info.cr

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ require "socket"
22

33
module LavinMQ
44
class ConnectionInfo
5-
getter remote_address : IPAddress
6-
getter local_address : IPAddress
5+
getter remote_address : Address
6+
getter local_address : Address
77
property? ssl : Bool = false
88
property? ssl_verify : Bool = false
99
property ssl_version : String?
@@ -14,18 +14,12 @@ module LavinMQ
1414

1515
# Remote and local addresses from the server's perspective
1616
def initialize(remote_address, local_address)
17-
@remote_address = IPAddress.new(remote_address)
18-
@local_address = IPAddress.new(local_address)
19-
end
20-
21-
def self.local
22-
src = Socket::IPAddress.new("127.0.0.1", 0)
23-
dst = Socket::IPAddress.new("127.0.0.1", 0)
24-
new(src, dst)
17+
@remote_address = Address.new(remote_address)
18+
@local_address = Address.new(local_address)
2519
end
2620

2721
# Suspecting memory problem with Socket::IPAddress in Crystal 1.15.0
28-
struct IPAddress
22+
struct Address
2923
getter address : String
3024
getter port : UInt16
3125

@@ -35,25 +29,25 @@ module LavinMQ
3529
end
3630

3731
def initialize(address : String)
38-
# is unix
39-
if address.starts_with("/")
32+
if address.starts_with? "/"
4033
@address = address
4134
@port = 0
4235
return
4336
end
44-
parts = address.split(":");
45-
if parts.length != 2
37+
parts = address.split(":")
38+
if parts.size != 2
4639
raise "Socket address should be host:port"
4740
end
48-
return self.new(Socket::IPAddress.new(parts[0], parts[1].to_u16))
41+
@address = parts[0]
42+
@port = parts[1].to_u16
4943
end
5044

5145
def to_s(io)
5246
io << @address << ':' << @port
5347
end
5448

5549
def loopback?
56-
@address == "::1" || @address.starts_with? "127."
50+
@address == "::1" || @address.starts_with?("127.") || @address.starts_with?("/")
5751
end
5852
end
5953
end

src/lavinmq/server.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ module LavinMQ
170170
case @config.unix_proxy_protocol
171171
when 1 then ProxyProtocol::V1.parse(client)
172172
when 2 then ProxyProtocol::V2.parse(client)
173-
else ConnectionInfo.local # TODO: use unix socket address, don't fake local
173+
else ConnectionInfo.new(client.remote_address.path, client.local_address.path)
174174
end
175175
handle_connection(client, conn_info, protocol)
176176
rescue ex

0 commit comments

Comments
 (0)