Mac上git和sourcetree使用终端代理访问被不可描述的主机

自己的部署的Git服务器被不可描述了。由于没有VPN,只有代理服务器V2Ray和SSR,因此需要通过命令行代理来Push代码。

通过命令行启动Sourcetree:/Applications/Sourcetree.app/Contents/MacOS/Sourcetree就可以通过代理访问网络啦!

方法1:
在终端中直接运行命令
export http_proxy=http://proxyAddress:port
这个办法的好处是简单直接,并且影响面很小(只对当前终端有效,退出就不行了)。
如果你用的是ss代理,在当前终端运行以下命令,那么wget curl 这类网络命令都会经过ss代理
export ALL_PROXY=socks5://127.0.0.1:1080

方法2:
把代理服务器地址写入shell配置文件.bashrc或者.zshrc 或者.bash_profile
直接在.bashrc或者.zshrc或者.bash_profile添加下面内容
export http_proxy=”http://localhost:port”
export https_proxy=”http://localhost:port”
以使用shadowsocks代理为例,ss的代理端口为1080,那么应该设置为
export http_proxy=”socks5://127.0.0.1:1080″
export https_proxy=”socks5://127.0.0.1:1080″
或者直接设置
ALL_PROXYexport ALL_PROXY=socks5://127.0.0.1:1080
localhost就是一个域名,域名默认指向 127.0.0.1,两者是一样的。

然后ESC后:wq保存文件,接着在终端中执行source ~/.bashrc
或者退出当前终端再起一个终端。
这个办法的好处是把代理服务器永久保存了,下次就可以直接用了。或者通过设置alias简写来简化操作,每次要用的时候输入setproxy,不用了就unsetproxy。

alias setproxy=”export ALL_PROXY=socks5://127.0.0.1:1080″
alias unsetproxy=”unset ALL_PROXY”
alias ip=”curl -i http://ip.cn”

命令行测试如下:
MacBook:srv gavin$ setproxy
MacBook:srv gavin$ git push
Everything up-to-date
MacBook:srv gavin$ unsetproxy
MacBook:srv gavin$ git push
fatal: unable to access ‘http://xxx/srv.git/’: Failed to connect to xxx port xx: Operation timed out

方法3:
改相应工具的配置,比如apt的配置sudo vim /etc/apt/apt.conf
在文件末尾加入下面这行
Acquire::http::Proxy “http://proxyAddress:port”
保存apt.conf文件即可。

方法4:
利用proxychains在终端使用socks5代理

补充:如果代理服务器需要登陆,这时可以直接把用户名和密码写进去http_proxy=http://userName:password@proxyAddress:port

参考: https://blog.fazero.me/2015/09/15/%E8%AE%A9%E7%BB%88%E7%AB%AF%E8%B5%B0%E4%BB%A3%E7%90%86%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%B3%95/

欢迎关注我的微信公众号:

 

如无特殊说明,文章均为本站原创,转载请注明出处!

发表回复

您的电子邮箱地址不会被公开。