1
1
12rbah2022-04-21 08:48:49
go
12rbah, 2022-04-21 08:48:49

Why can't I make a request through proxy socks5?

There was such a problem that I use vpn psiphon on ubuntu, which can be used through a local socks5 proxy, if I specify this proxy in mozilla, then everything works fine and I can access any site, such as facebook, but if I specify this proxy in in go code, I can make a request to normal sites, but if I try to make a request to facebook, I get this error:
panic: Get " https://www.facebook.com/ ": socks connect tcp 127.0.0.1:3080 -> www.facebook.com:443 : unknown error host unreachable

What could be the problem?

package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "golang.org/x/net/proxy"
    "log"
    "time"
)

func main() {
    proxyUrl := "127.0.0.1:3080"
    dialer, err := proxy.SOCKS5("tcp", proxyUrl, nil, 		&net.Dialer{
      Timeout:   30 * time.Second,
      KeepAlive: 30 * time.Second,
    })
    if err != nil {
    log.Fatalf("Socks5 dialer initialization failed: %v", err)
  }
   
    dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
        return dialer.Dial(network, address)
    }
    transport := &http.Transport{DialContext: dialContext,
        DisableKeepAlives: true}
        
    cl := &http.Client{Transport: transport}

    resp, err := cl.Get("https://www.facebook.com/")
    if err != nil {
        panic(err)
    }
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("body read failed")
    }
    fmt.Println(string(body))
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question