圈圈套圈圈

测试. 终端使用suplente的方法, 以wget举例:
(某些应用依赖于环境变量http_proxy. 这么写就ok了. 只要你的suplente带有cert就没所谓s不s. 只要有cert那就通吃了.)

goproxy-basic/goproxy-customca中的修改如下

    proxyUrl, _ := url.Parse("http://username:password@host:port")
    proxy.Tr.Proxy = http.ProxyURL(proxyUrl)

测试: http_proxy=http://localhost:8080 wget www.baidu.com

修改了他自己提供的默认proxy

// New proxy server, logs to StdErr by default
func NewProxyHttpServer() *ProxyHttpServer {
    proxy := ProxyHttpServer{
        Logger:        log.New(os.Stderr, "", log.LstdFlags),
        reqHandlers:   []ReqHandler{},
        respHandlers:  []RespHandler{},
        httpsHandlers: []HttpsHandler{},
        NonproxyHandler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
            http.Error(w, "This is a proxy server. Does not respond to non-proxy requests.", 500)
        }),
        Tr: &http.Transport{TLSClientConfig: tlsClientSkipVerify,
            Proxy: http.ProxyFromEnvironment},
    }
    proxy.ConnectDial = dialerFromEnv(&proxy)
    return &proxy
}

对于普通的请求:(get.go)

package main

import (
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    resp, err := http.Get("http://localhost:3000")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close() // defered

    log.Println(resp)
    log.Println(ioutil.ReadAll(resp.Body))

    log.Println("Bye")
}

当上面个的地址换成www.baidu.com时无法得到结果(直接断在panic: connection refused)

go build
proxychains4 ./tmp  

完美. 我们的proxychain是可以玩的. (并且golang搞出可执行体之后是可用的. 直接go run却不可以)

  • 解决方案2: 使用stdlib的suplente
    在代码中直接玩suplente也是可以的:(get1.go)
package main

import (
    "io/ioutil"
    "log"
    "net/http"
    "net/url"
)

func main() {
    urlProxy, _ := url.Parse("http://localhost:8080")

    client := &http.Client{
        Transport: &http.Transport{
            Proxy: http.ProxyURL(urlProxy),
        },
    }

    resp, err := client.Get("http://www.baidu.com")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    log.Println(resp)
    log.Println(ioutil.ReadAll(resp.Body))

    log.Println("Bye")
}

*解决方案3: 挂环境变量…….可能是最简单的. (get2.go)

// +build ignore

package main

import (
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "path"
    "syscall"
)

const (
    BodyFile = "index.html"
)

func orError(e error) {
    if e != nil {
        panic(e)
    }
}

func openDefaultFile() {
    //var UID uint32 = 501
    //var GUID uint32 = 100
    //cred := &syscall.Credential{UID, GUID, []uint32{}, false}
    sysProc := &syscall.SysProcAttr{
        //Credential: cred,
        Noctty: true,
    }
    cwd, _ := os.Getwd()
    log.Println("Current working directory:", cwd)
    attr := os.ProcAttr{
        Dir: cwd,
        Env: os.Environ(),
        Files: []*os.File{
            os.Stdin, // os.Stdin
            nil,      // os.Stdout,
            nil,      // os.Stderr,
        },
        Sys: sysProc,
    }

    process, err := os.StartProcess(
        "/usr/bin/xdg-open",
        []string{"xdg-open", path.Join(cwd, "index.html")},
        &attr,
    )
    _ = process
    orError(err)
    err = process.Release()
    orError(err)
    log.Println("You shall read it on your browser")
}

func main() {

    targetUri := "http://www.baidu.com"
    if len(os.Args) > 1 {
        targetUri = os.Args[1]
    }

    client := &http.Client{
        Transport: &http.Transport{
            Proxy: http.ProxyFromEnvironment,
        },
    }

    resp, err := client.Get(targetUri)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    log.Println(resp)

    bytes, err := ioutil.ReadAll(resp.Body)
    orError(err)
    err = ioutil.WriteFile(BodyFile, bytes, 0644)
    orError(err)

    openDefaultFile()

    log.Println("Bye")
}

使用特别简单: http_proxy=localhost:8080 go run get2.go
那么启动我们的goproxy时, 都不需要改go代码了:
http_proxy=http://username:password@host:port ./goproxy-customca -v


附带: 如果要玩https, 需要将ca添加到系统:
gedit /etc/ssl/certs/ca-certificates.crt
    proxyUrl, _ := url.Parse(__defaultProxyURL)  // http://xx:yy@zz:aa
    proxy.Tr.Proxy = http.ProxyURL(proxyUrl)

这样就可以建立起我们的https px.

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值