Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

WEBRick::HTTPServer creates ipv6only socket for host :: #99

Open
pohlarized opened this issue Jan 10, 2023 · 3 comments
Open

WEBRick::HTTPServer creates ipv6only socket for host :: #99

pohlarized opened this issue Jan 10, 2023 · 3 comments

Comments

@pohlarized
Copy link

The IPv6 host :: is kind of analogous to the IPv4 address 0.0.0.0, however it should usually (depending on the OS) listen for traffic coming from IPv4 and IPv6 addresses, unless specified differently by the OS.

However, i have observed that across different linux distros i have tried (manjaro, arch, debian), WEBRick will open its socket in ipv6-only mode.
I assume that the reason is this line as using the Socket.tcp_server_sockets function manually leads to the same result.
However, e.g. using the TCPServer from the socket stdlib opens a socker that's not in ipv6-only mode.

Sample code i have used:

require 'webrick'

server = WEBrick::HTTPServer.new Port: 8080, Host: '::', DocumentRoot: '~/'

trap 'INT' do
  server.shutdown
end

server.start

I have tested this with webrick 1.7.0 on ruby 3.1.1 and on ruby 2.6.3, both yielding the same result.

I have observed the mode using ss -tlne, where the local address is shown as [::]:8080, which indicates ipv6-only mode in ss, while it should be *:8080 in non-ipv6-only mode.
It also says v6only:1 in the process description, while it should say v6only:0.

Also here's the sample code using TCPServer that correctly opens the socket:

require 'socket'

server = TCPServer.new('::', 8080)

loop do
  client = server_socket.accept
  client.puts 'hello'
  client.close
end
@MarkLodato
Copy link

Does anyone have a workaround for this to force webrick to serve on both IPv4 and IPv6?

I'm using jekyll serve, which uses WEBrick under the hood, and chrome randomly chooses IPv4 vs IPv6, even if I explicitly go to http://127.0.0.1:4000 or http://[::1]:4000. This means that half the assets on my page fail to load because some connect over IPv4 and some over IPv6, and only one or the other are actually hitting a listening port.

@tisba
Copy link

tisba commented Aug 15, 2024

Same issue with WEBrick 1.8.1 on macOS under Ruby 3.3.4 (arm64-darwin23).

@tisba
Copy link

tisba commented Sep 21, 2024

hmm, I don't know what I did test back in August, but for me using Host: '::' (or not specifying Host at all) does the right thing. 🙈

$ ruby -rwebrick -e "WEBrick::HTTPServer.new(Port: 8888, Host: '::', DocumentRoot: '.').start"

Will bind correctly:

$ ss -tlne
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process
LISTEN    0         4096                 0.0.0.0:8888              0.0.0.0:*        ino:697550 sk:1 cgroup:/ <->
LISTEN    0         4096                    [::]:8888                 [::]:*        ino:697551 sk:2 cgroup:/ v6only:1 <->

This also works on macOS too. Used Ruby 3.3.5 and WEBrick 1.8.1 for testing.

I also tested with Ruby 3.1.1 and WEBrick 1.7.0, like @pohlarized did, and it also worked as expected.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

No branches or pull requests

3 participants