常见问题
产品介绍
代码示例

golang示例代码

    Socks5代理代码教程

    

    package main

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

    //函数的参数为socks5 IP
    func SocksDialer(socks5 string) proxy.Dialer {

        //创建socks5拨号器
        dialer, err := proxy.SOCKS5("tcp", socks5, nil, proxy.Direct)
        if err != nil {
            fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err)
            os.Exit(1)
        }
        return dialer
    }

    //创建http客户端,利用拨号器返回的对象来发送get请求到目标网站
    func httpClient(socksIp,targetUrl  string) {

        //创建http客户端
        httpTransport := &http.Transport{}
        httpClient := &http.Client{Transport: httpTransport}
        dialer := SocksDialer(socksIp)

        //设置自己的socks5作为拨号器
        httpTransport.Dial = dialer.Dial
        if resp, err := httpClient.Get(targetUrl); err != nil {
            log.Fatalln(err)
        } else {
            defer resp.Body.Close()
            body, _ := ioutil.ReadAll(resp.Body)
            fmt.Printf("%s\n", body)
        }
    }

    //程序入口
    func main() {

        //socks5代理IP
        socksIp := "27.204.81.21:31814"

        //目标网站
        targetUrl := "http://ip.cip.cc"
        httpClient(socksIp,targetUrl)

    }

    

    结果已经把socks5 IP返回了

    ```

    27.204.81.21


    Process finished with exit code 0

    ```