go get 使用 proxy

1. go get 使用 proxy

1. go get 命令不能使用 proxychains4 代理的原因

proxychains4 只能 hook 动态编译的程序达到修改系统调用 connect 的目的, 而 go 是静态编译的, 故 proxychains4 无法生效。

来自作者的答复: https://github.com/rofl0r/proxychains-ng/issues/199

proxychains hooks to dynamically loaded libc.

1. go doesn't even use libc, it uses its own syscall wrappers
2. go doesn't use dynamic linking, it uses static linking

so there's no way we could use this technique with go. the only way to hook into go binaries would be by using ptrace to hook the system calls themselves, which is hairy and platform-dependent - i.e. it means it would be a lot more work to produce and maintain.

so i fear it is out of scope for this utility.

也就是说, 所有使用 golang 编写的程序都无法使用 proxychains4 进行代理, go get 调用的也是 golang 编写的程序。

2. golang 程序使用代理的几种方案

2.1. 对下面几种方案的总结

目前使用下来, polipo 是最稳妥的一种解决方案:

  • 在使用 graftcp 过程中出现了 HTTPS 无法握手的现象。

polipo 虽然旧, 而且不再维护了, 但确是一种非常可靠稳妥的工具。

2.2. polipo(意大利语, 章鱼)

2.2.1. 简介

用 C 语言编写的代理程序, 通过设置大部分程序都识别的的环境变量: http_proxy 和 https_proxy, 将请求通过代理服务器以 http 或 https 的方式发出去, 代理服务器支持 sock4 和 sock5 协议。

Polipo is no longer maintained. Sorry.

2.2.2. 安装

git clone https://github.com/jech/polipo.git
cd polipo
make

make 的会生成二进制文件: polipo

2.2.3. 使用

  1. 首先设置配置文件
$ cp config.sample config

socksParentProxy = "192.168.1.16:1080"
socksProxyType = socks5
dnsQueryIPv6 = no
disableVia=false // 当前终端全局代理模式, 所有流量都会走代理
  1. 设置终端代理
export http_proxy="http://127.0.0.1:8123/"
export https_proxy="http://127.0.0.1:8123/"

如果想要删除临时的环境变量, 可以使用 unset
unset http_proxy
unset https_proxy
  1. 启动 polipo
./polipo -c config // 后面的 config 是配置文件的全路径

2.3. graftcp

graftcp 代理不稳定, 不要使用了, 还是使用 polipo 吧, 虽然很久之前就不再维护了, 但是万年老稳。

This tool graftcp can handle the Go program, by using ptrace.

项目地址: https://github.com/hmgle/graftcp

2.3.1. 简介

graftcp 可以把任何指定程序 (应用程序、脚本、shell 等) 的 TCP 连接重定向到 SOCKS5 代理。

对比 tsocks、proxychains 或 proxyChains-ng, graftcp 并不使用 LD_PRELOAD 技巧来劫持共享库的 connect()、getaddrinfo() 等系列函数达到重定向目的, 这种方法只对使用动态链接编译的程序有效, 对于静态链接编译出来的程序, 例如默认选项编译的 Go 程序, proxychains-ng 就无效了。graftcp 使用 ptrace(2) 系统调用跟踪或修改任意指定程序的 connect 信息, 对任何程序都有效。工作原理后面将会解释。

2.3.2. 安装

graftcp 在 Linux 系统内运行。 graftcp-local 使用 Go 编写, Go 环境是必需的。

git clone https://github.com/hmgle/graftcp.git
cd graftcp
make

make 执行完后, 需要修改配置文件, 在 graftcp-local/ 目录下:

$ cp example-graftcp-local.conf graftcp-local.conf
$ gedit graftcp-local.conf

## SOCKS5 address (default "127.0.0.1:1080")
socks5 = 192.168.1.16:1080

可以安装到系统中:

$ sudo make install

这个安装并不会复制文件到系统中, 而是使用当前目录下的二进制文件。为了方便使用 graftcp, 可以将 graftcp 这个二进制文件复制到系统中去, 它会自动和 graftcp-local 进行通信交互:

$ sudo cp graftcp /usr/local/bin/

2.3.3. 控制

可以使用以下命令对 graftcp 的进程 / 服务进行控制:

$ ./graftcp-local -service install
$ ./graftcp-local -service start
$ ./graftcp-local -service stop

$ sudo ps -ef | grep graftcp

2.3.4. 使用

通过 graftcp 安装来自 golang.org 的 Go 包:

graftcp go get -v golang.org/x/net/proxy

通过 graftcp 打开 Chromium / Chrome / Firefox 浏览器, 网页的所有请求都会重定向到 SOCKS5 代理:

graftcp chromium-browser
http://ident.me

通过 graftcp 打开当前目录下的程序:

$ graftcp ./echo-myip

通过 graftcp 启动 Bash / Zsh / Fish, 在这个新开的 shell 里面执行的任何新命令产生的 TCP 连接都会重定向到 SOCKS5 代理:

$ graftcp bash
$ curl ident.me

通过本人测试, 使用 graftcp 启动 bash 的方式最稳定、靠谱。

2.4. cow(编译很复杂, 不推荐使用)

2.4.1. 简介

HTTP proxy written in Go. COW can automatically identify blocked sites and use parent proxies to access.

2.4.2. 安装

$ git clone https://github.com/cyfdecyf/cow.git

使用传统的 Golang 程序编译方法对其进行编译。将拉下来的目录放到 $GOPATH 里面, 然后编译:

$ mkdir bin
$ go build -o bin/cow cow

然后设置配置文件, Unix 系统上为 ~/.cow/rc, Windows 上为 COW 所在目录的 rc.txt 文件:

$ cp doc/sample-config/rc ~/.cow/

listen = http://127.0.0.1:7777
alwaysProxy = true
proxy = socks5://192.168.1.16:1080

2.4.3. 使用

  1. 启动 cow
$ ./cow &
  1. 设置全局代理
export http_proxy=http://127.0.0.1:7777
export https_proxy=http://127.0.0.1:7777

3. Windows 下 go get 科学上网

在 windows 环境中, 网上以前的经验是使用一个软件把 ss 的 socks5 代理转成 http 代理, 然后使用 set http_proxy=http://127.0.0.1:1081 这样来达到目的。

实际上不需要那么麻烦, 经过测试, go get 命令直接就可以支持 socks5 代理。

新建一个批处理 goget.bat, 放到 PATH 环境下。脚本如下:

@echo off

set http_proxy=socks5://127.0.0.1:1080
set https_proxy=socks5://127.0.0.1:1080

go get -u -v %*

echo ...

pause

如果用的是 git 的命令行,则应该这样:

export http_proxy=socks5://127.0.0.1:1080
export https_proxy=socks5://127.0.0.1:1080

export // 查看效果

使用的时候直接打开命令行输入 goget golang.org/x/crypto 来看看效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云满笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值