命令行终端下使用小飞机(ss)

作者: aries 分类: 杂七杂八 发布时间: 2018-12-07 11:03 ė 2287次浏览 6 0评论

Mac版

由于某些不可抗拒力,我们开发人员要用一些墙外的资源,不得不借助一些工具,所以就有了小飞机(ss),问题是对于经常在命令行终端下工作的码农们,SS无法正常工作。因为在终端下不支持socks5代理,只支持http代理,这就很尴尬了。wget、curl、git、brew等命令行工具都会变得很慢。

执行命令

vim ~/.bash_profile

~/.bash_profile 里加入开关函数,使用起来更方便

function proxy_off(){
    unset http_proxy
    unset https_proxy
    unset sock5_proxy
    echo -e "已关闭代理"
}

function proxy_on() {
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy="http://127.0.0.1"
export https_proxy=$http_proxy
export sock5_proxy="sock5://127.0.0.1:1080"
echo -e "已开启代理"
}

Windows CMD版

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

# 如果需要帐号密码的话
set http_proxy_user=user
set http_proxy_pass=pass
set https_proxy_user=user
set https_proxy_pass=pass

# 恢复
set http_proxy=

set https_proxy=

# Ubuntu 下命令为 export
# export http_proxy=http://127.0.0.1

要点

  1. 一定要加 http://,直接写域名或者 IP 不行。
  2. httphttps 都要设置。

然后如果想验证是否成功配置了代理的话,用 ping 命令是不可以的

ping 还是不行的原因

ping的协议不是https,也不是https,是ICMP协议。

验证方式

curl -vv http://www.google.com,用这条命令来验证,如果有返回html的结果表示代理设置成功。

Windows Power Shell版

# NOTE: registry keys for IE 8, may vary for other versions
$regPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'

function Clear-Proxy
{
Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 0
Set-ItemProperty -Path $regPath -Name ProxyServer -Value ''
Set-ItemProperty -Path $regPath -Name ProxyOverride -Value ''

<span class="highlight-o">[</span>Environment<span class="highlight-o">]</span>::SetEnvironmentVariable<span class="highlight-o">(</span><span class="highlight-s1">&#39;http_proxy&#39;</span>, <span class="highlight-nv">$null</span>, <span class="highlight-s1">&#39;User&#39;</span><span class="highlight-o">)</span>
<span class="highlight-o">[</span>Environment<span class="highlight-o">]</span>::SetEnvironmentVariable<span class="highlight-o">(</span><span class="highlight-s1">&#39;https_proxy&#39;</span>, <span class="highlight-nv">$null</span>, <span class="highlight-s1">&#39;User&#39;</span><span class="highlight-o">)</span>

}

function Set-Proxy
{
$proxy = 'http://example.com'

Set-ItemProperty -Path <span class="highlight-nv">$regPath</span> -Name ProxyEnable -Value <span class="highlight-m">1</span>
Set-ItemProperty -Path <span class="highlight-nv">$regPath</span> -Name ProxyServer -Value <span class="highlight-nv">$proxy</span>
Set-ItemProperty -Path <span class="highlight-nv">$regPath</span> -Name ProxyOverride -Value <span class="highlight-s1">&#39;&lt;local&gt;&#39;</span>

<span class="highlight-o">[</span>Environment<span class="highlight-o">]</span>::SetEnvironmentVariable<span class="highlight-o">(</span><span class="highlight-s1">&#39;http_proxy&#39;</span>, <span class="highlight-nv">$proxy</span>, <span class="highlight-s1">&#39;User&#39;</span><span class="highlight-o">)</span>
<span class="highlight-o">[</span>Environment<span class="highlight-o">]</span>::SetEnvironmentVariable<span class="highlight-o">(</span><span class="highlight-s1">&#39;https_proxy&#39;</span>, <span class="highlight-nv">$proxy</span>, <span class="highlight-s1">&#39;User&#39;</span><span class="highlight-o">)</span>

}

纠结于应该用 set 还是 export 还有一个判断方法是,敲一下这两个命令,如果返回一个长长的列表,就表示应该用这个命令,反之,如果返回找不到这个命令,就不应该用这个命令。

Git版

设置 HTTP 代理:

git config --global http.proxy http://127.0.0.1:8118
git config --global https.proxy http://127.0.0.1:8118

设置 SOCKS5 代理:

git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080

Git 取消代理设置:

git config --global --unset http.proxy
git config --global --unset https.proxy
换一个
暂无评论
Ɣ回顶部