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

Error: undefined method 'address' for Socket::UNIXAddress (compile-time type is Socket::Address+) #1643

Closed
bettinson opened this issue Jan 7, 2022 · 1 comment · Fixed by #1675
Labels

Comments

@bettinson
Copy link

bettinson commented Jan 7, 2022

Describe the bug
Type mismatch for Socket::UNIXAddress

To Reproduce
ip_address = context.request.remote_address.try(&.address) || "N/A"

from https://luckyframework.org/guides/http-and-routing/http-handlers#remoteiphandler

Screenshots/code

Error: undefined method 'address' for Socket::UNIXAddress (compile-time type is Socket::Address+) 

Fix

ip_address = context.request.remote_address.as(Socket::IPAddress).try(&.address) || "N/A"

Versions (please complete the following information):

  • Lucky version 0.29.0
  • Crystal version Crystal 1.2.2 (2021-11-10)
  • OS: MacOS
@bettinson bettinson added the bug label Jan 7, 2022
@jwoertink
Copy link
Member

jwoertink commented Jan 7, 2022

Yeah, I feel like we should be able to update the handler to better handle this. You shouldn't have to do all the guessing and stuff with it

class Lucky::RemoteIpHandler
include HTTP::Handler
def call(context)
context.request.remote_address = fetch_remote_ip(context)
call_next(context)
end
private def fetch_remote_ip(context : HTTP::Server::Context) : Socket::Address?
request = context.request
if x_forwarded = request.headers["X_FORWARDED_FOR"]?.try(&.split(',').first?).presence
begin
Socket::IPAddress.new(x_forwarded, 0)
rescue Socket::Error
# if the x_forwarded is not a valid ip address we fallback to request.remote_address
request.remote_address
end
else
request.remote_address
end
end
end

jwoertink added a commit that referenced this issue Feb 19, 2022
… name where the IP value comes from. Added new remote_ip method to give you access to the raw string. Fixes #1643
jwoertink added a commit that referenced this issue Mar 7, 2022
* Updating RemoteIpHandler to now include habitat config for the header name where the IP value comes from. Added new remote_ip method to give you access to the raw string. Fixes #1643

* Updating so there's only 1 header to check. Now using the last IP in the header list as per the linked article in the comments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants