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

net: Windows build with netgo doesn't repsect hosts file, can not resolve localhost #57757

Closed
wxiaoguang opened this issue Jan 12, 2023 · 5 comments
Labels
NeedsFix The path to resolution is known, but the work has not been done. OS-Windows
Milestone

Comments

@wxiaoguang
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.19.5 windows/amd64
go version go1.20rc2 windows/amd64

What did you do?

Set Windows system DNS to a standard server which doesn't resolve localhost, then use Go net package to resolve a domain defined in local hosts file (C:\Windows\System32\Drivers\etc\hosts).

package main

import (
	"fmt"
	"net"
)

func main() {
	addr, err := net.ResolveTCPAddr("tcp", "localhost:12345")
	fmt.Printf("addr=%v, err=%v\n", addr, err)
}

What did you expect to see?

Go should respect C:\Windows\System32\Drivers\etc\hosts file and resolve localhost correctly with -tags=netgo.

What did you see instead?

Go can not resolve localhost with -tags=netgo

C:\work> go run resolvtest.go
addr=127.0.0.1:12345, err=<nil>

C:\work> go run -tags=netgo resolvtest.go
addr=<nil>, err=lookup localhost on 8.8.8.8:53: no such host
@wxiaoguang wxiaoguang changed the title package: net: Windows build with netgo doesn't repsect hosts file net: Windows build with netgo doesn't repsect hosts file Jan 12, 2023
@wxiaoguang wxiaoguang changed the title net: Windows build with netgo doesn't repsect hosts file net: Windows build with netgo doesn't repsect hosts file, can not resolve localhost Jan 12, 2023
@bcmills
Copy link
Contributor

bcmills commented Jan 12, 2023

@bcmills bcmills added NeedsFix The path to resolution is known, but the work has not been done. OS-Windows labels Jan 12, 2023
@bcmills bcmills added this to the Backlog milestone Jan 12, 2023
@qmuntal
Copy link
Contributor

qmuntal commented Jan 13, 2023

I naively changed etc/hosts to C:/Windows/System32/Drivers/etc/hosts in https://cs.opensource.google/go/go/+/master:src/net/hook.go;l=16 and exercised the ResolveTCPAddr function with some addresses only known to my local hosts file, and all of them could be successfully resolved.

Everything seems to be wired up on the Windows side, only the hosts file path needs to be changed, and probably add some tests.

@zeripath
Copy link

The underlying problem is likely in:

go/src/net/conf.go

Lines 134 to 145 in d52883f

if c.netGo || r.preferGo() {
switch c.goos {
case "windows":
// TODO(bradfitz): implement files-based
// lookup on Windows too? I guess /etc/hosts
// kinda exists on Windows. But for now, only
// do DNS.
fallbackOrder = hostLookupDNS
default:
fallbackOrder = hostLookupFilesDNS
}
}

The result of L141 is that DNS lookups cannot check the /etc/hosts file (or equivalent) and thus I bet the lookups for localhost are going out to an external server and we're getting the external name for the host.

zeripath pushed a commit to go-gitea/gitea that referenced this issue Jan 16, 2023
Backport #22467

Fix #22370 and more.

Before Go 1.19, the `netgo` tag for Windows does nothing.

But Go 1.19 rewrite the net package code for Windows DNS, and there is a
bug:

* golang/go#57757

This PR just removes the `netgo` tag for Windows build, then the Gitea
for Windows can have the old DNS behavior.
zeripath pushed a commit to go-gitea/gitea that referenced this issue Jan 16, 2023
Fix #22370 and more.

Before Go 1.19, the `netgo` tag for Windows does nothing.

But Go 1.19 rewrite the net package code for Windows DNS, and there is a
bug:

* golang/go#57757

This PR just removes the `netgo` tag for Windows build, then the Gitea
for Windows can have the old DNS behavior.
nikita-vanyasin added a commit to arangodb-helper/arangodb that referenced this issue Feb 10, 2023
…iour

 In go1.19 there is no support for 'hosts' file for Windows:
 golang/go#57757

 Disabling netgo will revert behaviour to go1.18 for resolving names
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/467335 mentions this issue: net: respect hosts file when resolving names for Windows

ajanikow pushed a commit to arangodb-helper/arangodb that referenced this issue Feb 14, 2023
…iour (#352)

In go1.19 there is no support for 'hosts' file for Windows:
 golang/go#57757

 Disabling netgo will revert behaviour to go1.18 for resolving names
chadlwilson added a commit to gocd-contrib/gocd-trial-launcher that referenced this issue Mar 16, 2023
Seems there is an issue as noted at gocd/gocd#11398 and golang/go#57757. This reverts to the golang 1.18 behaviour for now.
AskAlexSharov pushed a commit to erigontech/erigon that referenced this issue Apr 26, 2023
golang/go#57757

i think we added netgo tag for the internalcl, since there was an issue
with resolution there on linux at some point, but i guess in windows,
netgo localhost resolution is broken.

i just tested on my windows machine and removing netgo seems to work
with both --internalcl=true and false
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/548095 mentions this issue: doc: document that the Windows hosts file is now respected

@dmitshur dmitshur modified the milestones: Backlog, Go1.22 Dec 9, 2023
gopherbot pushed a commit that referenced this issue Dec 9, 2023
For #57757.
For #61422.

Change-Id: Icbe9a86b43eb0816dc5899a1c14b50ee22db1c5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/548095
Reviewed-by: Alex Brainman <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
For golang#57757.
For golang#61422.

Change-Id: Icbe9a86b43eb0816dc5899a1c14b50ee22db1c5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/548095
Reviewed-by: Alex Brainman <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done. OS-Windows
Projects
None yet
Development

No branches or pull requests

6 participants