sneako/finch

Can't connect to IPv6 address

danschultzer opened this issue · 1 comments

It's not possible to connect to an IPv6 address directly (using google ipv6 below as an example):

iex(1)> :get |> Finch.build("http://[2607:f8b0:4007:814::200e]/", [], nil) |> Finch.request(MyFinch) 
{:error, %Mint.TransportError{reason: :nxdomain}}

I was digging through Mint where I had to do this to get it to connect:

iex(1)> {:ok, addr} = :inet.parse_address('2607:f8b0:4007:814::200e')
{:ok, {9735, 63664, 16391, 2068, 0, 0, 0, 8206}}

iex(2)> Mint.HTTP.connect(:http, addr, 80, [inet6: true, hostname: "google.com"])
{:ok,
 %Mint.HTTP1{
   host: "google.com",
   port: 80,
   request: nil,
   streaming_request: nil,
   socket: #Port<0.29>,
   transport: Mint.Core.Transport.TCP,
   mode: :active,
   scheme_as_string: "http",
   requests: {[], []},
   state: :open,
   buffer: "",
   proxy_headers: [],
   private: %{}
 }}

In Finch it doesn't seem possible to pass in the parsed IP address tuple. It must be a binary. But it would be a convoluted mess of overriding the %URI{} with the IP address tuple to do it that way anyway.

This might be something in Mint, and I'll continue digging through this till I find out what's missing here.

Other clients

:hackney does connect successfully:

iex(1)> :hackney.get('http://[2607:f8b0:4007:814::200e]/', [{'host', 'google.com'}])
[error] got this answer: []
[error] got this answer: true
{:ok, 301,
 [
   {"Location", "http://www.google.com/"},
   {"Content-Type", "text/html; charset=UTF-8"},
   {"Date", "Sun, 19 Feb 2023 04:10:58 GMT"},
   {"Expires", "Tue, 21 Mar 2023 04:10:58 GMT"},
   {"Cache-Control", "public, max-age=2592000"},
   {"Server", "gws"},
   {"Content-Length", "219"},
   {"X-XSS-Protection", "0"},
   {"X-Frame-Options", "SAMEORIGIN"}
 ], #Reference<0.1429924583.3129999362.69182>}

:httpc does connect successfully:

iex(1)> :httpc.set_options([{:ipfamily, :inet6}])                                                           
:ok

iex(2)> :httpc.request(:get, {'http://[2607:f8b0:4007:814::200e]/', [{'host', 'google.com'}]}, [], [])
{:ok,
 {{'HTTP/1.1', 200, 'OK'},
  [
    {'cache-control', 'private, max-age=0'},
    {'date', 'Sun, 19 Feb 2023 15:13:16 GMT'},
    {'accept-ranges', 'none'},
    {'server', 'gws'},
    {'vary', 'Accept-Encoding'},
    {'content-length', '52015'},
    {'content-type', 'text/html; charset=ISO-8859-1'},
    {'expires', '-1'},
    {'p3p', 'CP="This is not a P3P policy! See g.co/p3phelp for more info."'},
    {'x-xss-protection', '0'},
    {'x-frame-options', 'SAMEORIGIN'},
    {'set-cookie',
     '1P_JAR=2023-02-19-15; expires=Tue, 21-Mar-2023 15:13:16 GMT; path=/; domain=.google.com; Secure'},
    {'set-cookie',
     'AEC=ARSKqsJfy5urMwakqUMup3riKe6QoA3NhU2s8iwElT80SH9JliWW-8NH7Jw; expires=Fri, 18-Aug-2023 15:13:16 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax'},
    {'set-cookie',
     'NID=511=nznBy75ym7JHn55gT7EJHzgTV499VNRh-4VbfMRRDzcKZPwOZx3Lb208ByMQqxR3K_-cAf3gyP7NLpBtpBnHAqnV4msjJd6Ud8jHw8cgrUL-i2QlYziCjdPF0hNRsy4MB1cSmqHqVOAQDqMJgjk40g2bvLT-Av0cdKhVekdGOVE; expires=Mon, 21-Aug-2023 15:13:16 GMT; path=/; domain=.google.com; HttpOnly'}
  ],
  '<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en">...'' ++ ...}}

Nevermind, I didn't test carefully late last night. The :inet6 config wasn't set correctly. All good now, and this is what I'm seeing:

iex(1)> {:ok, pid} = Finch.start_link(name: MyFinch, pools: %{default: [conn_opts: [transport_opts: [inet6: true]]]})                                              

iex(2)> :get |> Finch.build("http://[2607:f8b0:4007:814::200e]/", [{"host", "google.com"}], nil) |> Finch.request(MyFinch) 
{:ok,
 %Finch.Response{
   status: 301,
   body: "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.com/\">here</A>.\r\n</BODY></HTML>\r\n",
   headers: [
     {"location", "http://www.google.com/"},
     {"content-type", "text/html; charset=UTF-8"},
     {"date", "Sun, 19 Feb 2023 15:17:48 GMT"},
     {"expires", "Tue, 21 Mar 2023 15:17:48 GMT"},
     {"cache-control", "public, max-age=2592000"},
     {"server", "gws"},
     {"content-length", "219"},
     {"x-xss-protection", "0"},
     {"x-frame-options", "SAMEORIGIN"}
   ]
 }}

Sorry for the noise!