golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.24k stars 17.47k forks source link

x/net/proxy: SOCKS5 Timeout is not working properly #37549

Open qwatros opened 4 years ago

qwatros commented 4 years ago

After successful connection to the proxy timeout doesn't work any more. So it's hanging a long time trying to connect from proxy to an "addr" endpoint (I suppose).

It' would be good to have a global socket timeout option.

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

1.13

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/root666/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/root666/work/go/"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build754930586=/tmp/go-build -gno-record-gcc-switches"

What did you do?

func Client(socks, addr string) (net.Conn, error) {
    dialer, err := proxy.SOCKS5("tcp", socks, nil, &net.Dialer{Timeout: 3 * time.Second})
    if err != nil {
        return nil, err
    }
    conn, err := dialer.Dial("tcp", addr)
    if err != nil {
        return nil, err
    }
    return conn, err
}

What did you expect to see?

Timeout with 3 seconds

What did you see instead?

Timeout more then 10 seconds.

dmitshur commented 4 years ago

/cc @mikioh @bradfitz @ianlancetaylor per owners.

bradfitz commented 4 years ago

I think this is a dup of another bug. (or a pending CL or so)

The net.Dialer's timeout is only for the TCP dial. The SOCKS5 dial doesn't support contexts yet.

ceo0x commented 3 years ago

did you manage to make it work ? i think the timeout you are setting is only for the forward proxy parameter, that dialer is used when connecting to the proxy only , not used when trying to connect to "addr"

Karmaz95 commented 2 years ago

BUMP: Did you manage to make it work?

hut8 commented 2 years ago

It looks like this might be relevant: https://github.com/argoheyard/lang-net/commit/99897ddc6eea1f0fb70629190c917a2cf3d8451b

spac3wh1te commented 2 years ago

try this

func main() {
    dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:7890",
        &proxy.Auth{User: "username", Password: "password"},
        nil,
    )

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()
    conn, err := dialer.(proxy.ContextDialer).DialContext(ctx, "tcp", "127.0.0.1:80")
    if err != nil {
        fmt.Println("error")
        fmt.Println(err)
    } else {
        fmt.Println("success")
        fmt.Println(conn)
    }
}