测试. 终端使用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)
- 解决方案1: 使用proxychains4(https://github.com/haad/proxychains)
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.