terminal走代理

shell的配置文件中添加一些简单脚本即可实现终端走代理。

一、http代理

#proxy 
HTTP='127.0.0.1:7890'
defp=$HTTP

# No Proxy
function noproxy
{
    unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY all_proxy ALL_PROXY ftp_proxy FTP_PROXY dns_proxy DNS_PROXY JAVA_OPTS GRADLE_OPTS MAVEN_OPTS
    echo "clear proxy done"
}

function setproxy
{
    if [ $# -eq 0 ]
    then
        inArg=$defp
    else
        inArg=$1
    fi
    HOST=$(echo $inArg |cut -d: -f1)
    PORT=$(echo $inArg |cut -d: -f2)
    http_proxy=http://$HOST:$PORT
    HTTP_PROXY=$http_proxy
    all_proxy=$http_proxy
    ALL_PROXY=$http_proxy
    ftp_proxy=$http_proxy
    FTP_PROXY=$http_proxy
    dns_proxy=$http_proxy
    DNS_PROXY=$http_proxy
    https_proxy=$http_proxy
    HTTPS_PROXY=$https_proxy
    JAVA_OPTS="-Dhttp.proxyHost=$HOST -Dhttp.proxyPort=$PORT -Dhttps.proxyHost=$HOST -Dhttps.proxyPort=$PORT"
    GRADLE_OPTS="-Dgradle.user.home=$HOME/.gradle"
    MAVEN_OPTS=$JAVA_OPTS
    no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com,.huaweicloud.com,.coding.net,.ruby-china.org"
    echo "current proxy is ${http_proxy}"
    export no_proxy http_proxy HTTP_PROXY https_proxy HTTPS_PROXY all_proxy ALL_PROXY ftp_proxy FTP_PROXY dns_proxy DNS_PROXY JAVA_OPTS GRADLE_OPTS MAVEN_OPTS
}

这段代码添加了两个函数,setproxyhttp_proxy设置为想要的代理地址,noproxy则去除代理设置。setproxy函数默认代理127.0.0.1:7890,这是Clash默认的http代理。如果想要终端打开默认开始代理,在最后加上一行setproxy即可。

二、socks5代理

在配置文件中添加新的函数setsocks5,代码如下:

function setsocks5
{
    export ALL_PROXY=socks5://127.0.0.1:1080
}

Last updated

Was this helpful?