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

Open
wxiaoguang opened this issue Jan 12, 2023 · 3 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
Member

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.
fsologureng pushed a commit to fsologureng/gitea that referenced this issue Jan 24, 2023
Fix go-gitea#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.
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

4 participants