如何路由http通过Go中的隧道获取? [英] How to route http Get via tunnel in Go?

查看:165
本文介绍了如何路由http通过Go中的隧道获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器有一个ssh隧道(通过端口:9998)。我希望我的http GET / POST请求通过Go中的此端口进行路由。在java中我会指定DsocksProxyHost和DsocksProxyPort。我正在寻找Go中的类似选项。感谢您的帮助。

I have a ssh tunnel to my server (via port: 9998). I want my http GET/POST requests to be routed through this port in Go. In java I would specify the DsocksProxyHost and DsocksProxyPort. I am looking for a similar option in Go. Thank you for the help in advance.

推荐答案

使用上述评论提供的信息,通过SOCKS代理传输HTTP请求:

Using the information provided in the above comments, here is a working example on how to tunnel HTTP requests through a SOCKS proxy:

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))
}

这篇关于如何路由http通过Go中的隧道获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆