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

aries 发表于 2018-12-07 3284 次浏览

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:8118"
    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:1080

要点

  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 ''

    [Environment]::SetEnvironmentVariable('http_proxy', $null, 'User')
    [Environment]::SetEnvironmentVariable('https_proxy', $null, 'User')
}

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

    Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 1
    Set-ItemProperty -Path $regPath -Name ProxyServer -Value $proxy
    Set-ItemProperty -Path $regPath -Name ProxyOverride -Value '<local>'

    [Environment]::SetEnvironmentVariable('http_proxy', $proxy, 'User')
    [Environment]::SetEnvironmentVariable('https_proxy', $proxy, 'User')
}

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

Windows wsl2版本

执行命令

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,192.168.0.16,127.0.0.1,localaddress,.localdomain.com"
    host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")
    proxy_url="http://$host_ip:1080"
    export http_proxy=$proxy_url
    export https_proxy=$http_proxy
    export sock5_proxy="sock5://$host_ip:1080"
    echo -e "已开启代理"
}

脚本通过 cat /etc/resolv.conf 来获取 DNS 服务器,也就是 Windows 的 IP,再将其中的 IP 部分截取出来,加上代理客户端的端口(如1080,可以根据自己实际情况修改),使用 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

0条评论

如需评论,请填写表单。
换一个

记住我的信息