-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
Go version
go version go1.23.3 windows/amd64
Output of go env in your module/workspace:
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOENV=C:\Users\Administrator\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Administrator\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Administrator\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.23.3
set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\Administrator\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build1513341593=/tmp/go-build -gno-record-gcc-switchesWhat did you do?
I am practicing with a simple port scanner. At first I used net.Dial and the program completes very fast as the following shows:

However when I switch to net.DialTimeout, the program beomes mysteriously slow (30~40 times slower). The side effect seems to be system level, rollback to net.Dial and recompile can't solve the problem. The side effect is gone after a restart.


I make some test and found out the side effect seems to happen after net.DialTimeout connect a non local address. DialTimeout on 127.0.0.1 is fine, after connecting a nonlocal ip address, the forementioned side effect is triggered. Dialing localhost becomes very slow(both net.Dial and net.DialTimeout ), and the only way to solve it is to restart.

Here is the full code:
package main
import (
"flag"
"fmt"
"net"
"sync"
"time"
)
var (
ip string
minport int
maxport int
timeout int
)
func init() {
flag.StringVar(&ip, "ip", "127.0.0.1", "Ip to scan.")
flag.IntVar(&minport, "p1", 0, "Port to scan from.")
flag.IntVar(&maxport, "p2", 1024, "Port to scan stop.")
flag.IntVar(&timeout, "t", 1000, "Time out in microseconds")
flag.Parse()
}
func main() {
start := time.Now()
ports := []int{}
wg := &sync.WaitGroup{}
mutex := &sync.Mutex{}
for p := minport; p <= maxport; p++ {
wg.Add(1)
go portScan(ip, p, wg, &ports, mutex)
}
wg.Wait()
elasped := time.Since(start)
fmt.Println("Scanned from ", minport, " to ", maxport, " in ", elasped)
fmt.Println(ports)
}
func portScan(ip string, port int, wg *sync.WaitGroup, ports *[]int, mutex *sync.Mutex) {
defer wg.Done()
address := fmt.Sprintf("%s:%d", ip, port)
fmt.Println("Connecting ", address)
conn, err := net.Dial("tcp", address)
// conn, err := net.DialTimeout("tcp", address, 500*time.Microsecond)
if err == nil {
defer conn.Close()
// fmt.Println("Connection successful.")
// localAddr := conn.LocalAddr().String()
// remoteAddr := conn.RemoteAddr().String()
// fmt.Println("Local Address:", localAddr)
// fmt.Println("Remote Address:", remoteAddr)
mutex.Lock()
*ports = append(*ports, port)
mutex.Unlock()
} else {
// fmt.Println("Error", err)
}
}
What did you see happen?
As above.
What did you expect to see?
As above.