我有一条到服务器的ssh隧道(通过端口:9988)。我希望通过Go中的此端口路由我的http GET/POST请求。在Java中,我将指定DsocksProxyHost和DsocksProxyPort。我在Go中寻找类似的选项。谢谢您的帮助。

最佳答案

使用以上注释中提供的信息,这是一个有关如何通过SOCKS代理隧道HTTP请求的有效示例:

package main

import (
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "time"

    "golang.org/x/net/proxy"
)

func main() {
    url := "https://example.com"
    socksAddress := "localhost:9998"

    socks, err := proxy.SOCKS5("tcp", socksAddress, nil, &net.Dialer{
        Timeout:   30 * time.Second,
        KeepAlive: 30 * time.Second,
    })
    if err != nil {
        panic(err)
    }

    client := &http.Client{
        Transport: &http.Transport{
            Dial:                socks.Dial,
            TLSHandshakeTimeout: 10 * time.Second,
        },
    }

    res, err := client.Get(url)
    if err != nil {
        panic(err)
    }
    content, err := ioutil.ReadAll(res.Body)
    res.Body.Close()
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s", string(content))
}

关于ssh - 如何在Go中通过隧道路由http Get?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33681662/

10-16 13:46