如何配置go命令使用代理?

huangapple go评论85阅读模式
英文:

How do I configure go command to use a proxy?

问题

我想运行go install来安装tour,但是我找不到使用代理服务器进行互联网访问的选项。我不仅仅需要这个选项来进行tour,还需要用于一般的Go开发。

如何配置Go使用代理服务器。

英文:

I want to run go install to install the tour, but I can't find the option to use a proxy for internet access. I don't need this just for the tour but for developing in Go in general.

How do I configure Go to use a proxy.

答案1

得分: 139

Go程序可以理解环境变量http_proxyno_proxy,但这还不够,因为go get使用源代码管理器来检索代码。所以你还需要为你的源代码管理器设置HTTP代理设置。对于Mercurial,请使用这个,对于Git,请使用这个

http_proxy的值可以是类似http://user:password@host:port/的形式。用户、密码和端口部分是可选的。no_proxy是一个逗号分隔的服务器列表,不应该通过代理连接。它的值可以是类似foo.com,bar.net:4000的形式。

你可以在你的bash_profile中设置这些环境变量,但如果你想将它们限制在go中使用,你可以像这样运行它:

$ http_proxy=127.0.0.1:8080 go get code.google.com/p/go.crypto/bcrypt

如果这是你总是想要的,可以设置这个别名来避免每次输入代理部分:

$ alias go='http_proxy=127.0.0.1:8080 go'

从现在开始,你可以正常使用go,但它会使用你的HTTP代理。

英文:

Go programs understand environment variables http_proxy and no_proxy, but that's not enough because go get uses source control managers for retrieving code. So you have to set HTTP proxy settings for your SCM too. Use this for Mercurial and this for Git.

http_proxy value can be like http://user:password@host:port/. User, password, and port parts are optional. no_proxy is a comma-separated list of servers that should not be connected through proxy. Its value can be like foo.com,bar.net:4000.

You can set these environment variables in your bash_profile, but if you want to limit their usage to go, you can run it like this:

$ http_proxy=127.0.0.1:8080 go get code.google.com/p/go.crypto/bcrypt

If that's what you always want, set this alias to avoid typing proxy part every time:

$ alias go='http_proxy=127.0.0.1:8080 go'

From now on you can use go normally, but it uses your HTTP proxy.

答案2

得分: 9

在Windows命令行中:

set http_proxy=http://[user]:[pass]@[proxy_ip]:[proxy_port]/
set https_proxy=http://[user]:[pass]@[proxy_ip]:[proxy_port]/

...然后导航到https://github.com/并下载GitHub证书(我将其命名为goland_cert.cer

...现在执行OpenSSL命令将其导出为PEM格式

openssl x509 -inform der -in goland_cert.cer -out goland_cert.pem

...最后在git全局配置中设置证书

git config --global http.sslCAInfo C:/Users/[User]/certs/golang_cert.pem

英文:

On Windows command line:

set http_proxy=http://[user]:[pass]@[proxy_ip]:[proxy_port]/
set https_proxy=http://[user]:[pass]@[proxy_ip]:[proxy_port]/

...then navigate to https://github.com/ and download the GitHub certificate (I set the name as goland_cert.cer)

...now execute the OpenSSL command to export this to PEM format

openssl x509 -inform der -in goland_cert.cer -out goland_cert.pem

...finally set the certificate in git global config

git config --global http.sslCAInfo C:/Users/[User]/certs/golang_cert.pem

答案3

得分: 7

这对我有用:

alias go='http_proxy=http://127.0.0.1:1081/ https_proxy=http://127.0.0.1:1081/ no_proxy=localhost,127.0.0.0/8,::1 go'

注意:对于某些人来说,协议可能不同 https_proxy=http://127.0.0.1:1081

英文:

This works for me:

alias go='http_proxy=http://127.0.0.1:1081/ https_proxy=http://127.0.0.1:1081/ no_proxy=localhost,127.0.0.0/8,::1 go'

Note: for someones, protocol may be different https_proxy=http://127.0.0.1:1081

答案4

得分: 5

你还可以使用https://github.com/cyfdecyf/cow/将http请求映射到socks5流量中,非常方便,如果你被GFW屏蔽了的话。

英文:

you can also map http requests to socks5 traffic by using https://github.com/cyfdecyf/cow/

very handy if you are blocked by GFW

答案5

得分: 4

在系统变量中添加GOPROXY变量名和变量值为your proxy。这对我起作用了。

英文:

Add GOPROXY variable name and Variable value as your proxy in the System variable. This worked for me.

答案6

得分: 3

你可能想要查看 https://github.com/hmgle/graftcp,

$ graftcp-local/graftcp-local -h
graftcp-local/graftcp-local 的使用方法:
  -config string
        配置文件的路径
  -listen string
        监听地址(默认为“:2233”)
  -logfile string
        将日志写入文件
  -loglevel value
        日志级别(0-6)(默认为1)
  -pipepath string
        graftcp 用于发送地址信息的管道路径(默认为“/tmp/graftcplocal.fifo”)
  -service string
        控制系统服务:[“start” “stop” “restart” “install” “uninstall”]
  -socks5 string
        SOCKS5 地址(默认为“127.0.0.1:1080”)
  -syslog
        将日志发送到本地系统日志记录器(Windows 上为 Eventlog,Unix 上为 syslog)

如果你已经在 1080 端口上监听了 shadowsocks,那么你不需要提供任何参数,只需运行 graftcp-local,以代理 go get

$ ./graftcp go get -v golang.org/x/net/proxy
英文:

You may want check https://github.com/hmgle/graftcp,

$ graftcp-local/graftcp-local -h
Usage of graftcp-local/graftcp-local:
  -config string
        Path to the configuration file
  -listen string
        Listen address (default ":2233")
  -logfile string
        Write logs to file
  -loglevel value
        Log level (0-6) (default 1)
  -pipepath string
        Pipe path for graftcp to send address info (default "/tmp/graftcplocal.fifo")
  -service string
        Control the system service: ["start" "stop" "restart" "install" "uninstall"]
  -socks5 string
        SOCKS5 address (default "127.0.0.1:1080")
  -syslog
        Send logs to the local system logger (Eventlog on Windows, syslog on Unix)

If you already have shadowsocks listening on 1080, then you don't need provide any paramaters, just run graftcp-local, to proxy go get

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

答案7

得分: 0

git config [--global] http.proxy http://proxy.example.com:port
git config [--global] https.proxy http://proxy.example.com:port

see https://github.com/golang/go/wiki/GoGetProxyConfig

英文:
git config [--global] http.proxy http://proxy.example.com:port
git config [--global] https.proxy http://proxy.example.com:port

see https://github.com/golang/go/wiki/GoGetProxyConfig

答案8

得分: 0

我用Go命令在Win10系统中设置了一些变量来解决这个问题。

在这里(“使用代理服务器的cf CLI”)中,您可以找到下面描述的信息,并附有图片。您还可以阅读关于以下内容的信息:

  • https_proxy的格式
  • 使用cf v3-ssh使用SOCKS5
  • 在Mac OS或Linux中设置https_proxy

我在这个答案中只包含关于W10的信息,因为这是我测试过的系统。

Windows 10

设置新的路径变量。

  • 在搜索框中输入“path”
  • 选择并打开“编辑系统环境变量(控制面板)”。
  • “系统属性”窗口将打开。
  • 选择“高级”选项卡。
  • 点击“环境变量”按钮,将打开一个同名的窗口。
  • 在“系统变量”部分,点击“新建”按钮。将打开“新建系统变量”窗口。
  • 现在您将添加所需的变量,请按以下方式填写字段:
    • 变量名称:https_proxy
    • 变量值:http://yourUserName:userNamePassword@yourIPaddress:port

最后...

我测试了go的安装命令行。

打开控制台,输入:go install github.com/isacikgoz/gitbatch/cmd/gitbatch@latest

这是来自这个项目的一个示例:https://github.com/isacikgoz/gitbatch

这在我特定的W10 19043系统上有效。

英文:

I solved this problem with the Go command setting some variables in the Win10 system.

Here ("Using the cf CLI with a Proxy Server") you can find the information described below, with images. Also you can read information about:

  • Format of https_proxy
  • Use SOCKS5 with cf v3-ssh
  • Set https_proxy in Mac OS or Linux

I included in this answer only information about W10 because is the one I tested.

Windows 10

Set the new path variable.

  • From the search box, type in path
  • Select and open "Edit the system environment variables (control panel)".
  • "System properties" window will open.
  • Select "Advanced" tab.
  • Press "Environment Variables" button, a window with the same name will open.
  • In "System Variables" section, press the "New" button. "New System Variable" window will open.
  • You'll now add the needed variable, fill the fields as follows:
    • Variable name: https_proxy
    • Variable value:http://yourUserName:userNamePassword@yourIPaddress:port

Lastly...

I tested the installation command line for go.

Open the console, and type in: go install github.com/isacikgoz/gitbatch/cmd/gitbatch@latest

That's an example from this project: https://github.com/isacikgoz/gitbatch

This worked on my particular W10 19043 system.

huangapple
  • 本文由 发表于 2012年4月30日 19:55:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/10383299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定