Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Roxbili/a1b94467f48cac4483ac29c3dd6c40ae to your computer and use it in GitHub Desktop.
Save Roxbili/a1b94467f48cac4483ac29c3dd6c40ae to your computer and use it in GitHub Desktop.
Ubuntu shadowsocks + privoxy + genpac 实现自动代理科学上网 + git 代理配置

分辨需要设置的代理

一、HTTP 形式

走 HTTP 代理

如果要设置全局代理,可以依照这样设置:

git config --global http.proxy "http://127.0.0.1:8080"
git config --global https.proxy "http://127.0.0.1:8080"

不推荐直接用全局代理,因为如果挂了全局代理,这样如果需要克隆coding之类的国内仓库,会奇慢无比, 所以我建议使用这条命令,只对github进行代理,对国内的仓库不影响(摘自知乎 汪小九)

git config --global http.https://github.com.proxy "https://127.0.0.1:1080"
git config --global https.https://github.com.proxy "https://127.0.0.1:1080"

走 socks5 代理(如 Shadowsocks)

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

同上

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

取消设置

git config --global --unset http.proxy
git config --global --unset https.proxy

二、SSH 形式

修改 ~/.ssh/config 文件(不存在则新建):

# 必须是 github.com
Host github.com
   HostName github.com
   User git
   # 走 HTTP 代理
   # ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=8080
   # 走 socks5 代理(如 Shadowsocks)
   # ProxyCommand nc -v -x 127.0.0.1:1080 %h %p

方法一

客户端Shadowsocks-qt5

安装后可直接跳转第3步进行自动代理设置的步骤(未尝试过)。

方法二

1. Shadowsocks

初步安装

  • 更新软件源
sudo apt update
sudo apt upgrade
  • 下载pip
    sudo apt-get install python-pip

  • 下载Shadowsocks
    sudo pip install shadowsocks

  • 若在执行过程中出现黄色警告,改用如下命令:
    sudo -H pip install shadowsocks

配置shadowsocks

  • 在/home路径下建一个shadowsocks.conf文本文件 sudo vim shadowsocks.conf

  • 在文件中添加如下信息:

{
    "server":"ip",
    "server_port":1018,
    "local_address":"127.0.0.1",
    "local_port":1080,
    "password":"*******",
    "timeout":300,
    "method":"aes-256-cfb"
}
  • 具体含义如下
{
    "server":"服务器ip地址",
    "server_port":服务器端口,
    "local_address":"本地ip",
    "local_port":本地服务监听的端口,
    "password":"*******",
    "timeout":超时时间间隔(秒),
    "method":"加密方法"
}
  • 配置完成之后运行如下命令,start表示开启服务,stop便是关闭服务。
    sudo sslocal -c /home/shadowsocks.conf -d start

2. 部署privoxy

  • 安装privoxy
    sudo apt-get install privoxy

  • 配置privoxy
    sudo gedit /etc/privoxy/config

  1. 找到4.1节 listen-address,看是否存在listen-address 127.0.0.1:8118,如果不存在,则添加上;如果存在但被注释掉了,则取消注释;
  2. 找到5.2节, 查看是否存在forward-socks5 / 127.0.0.1:1080 .(后面的.很重要,不能舍弃),如果不存在则添加或修改。
  • 重启privoxy
    sudo /etc/init.d/privoxy restart

3. 自动代理设置

  • 安装genepac
    sudo pip install genpac

  • 新建shadowsocks文件夹存放pac文件

mkdir ~/shadowsocks
cd shadowsocks
  • 生成pac文件
    genpac --format=pac --pac-proxy="SOCKS5 127.0.0.1:1080" -o autoproxy.pac

  • 设置系统网络代理 在网络设置里选择自动的方式,url为shadowsocks里的pac文件,如:
    file:///home/XXXX/shadowsocks/autoproxy.pac
    (XXXX为用户名)

4. 开机启动

sudo sslocal -c /home/shadowsocks.conf -d start
sudo service privoxy start

当然也可以设置开机自动启动脚本,见其它笔记

方法一, 编辑rc.loacl脚本

Ubuntu开机之后会执行/etc/rc.local文件中的脚本,所以我们可以直接在/etc/rc.local中添加启动脚本。
当然要添加到语句:exit 0 前面才行。

方法二, 添加一个Ubuntu的开机启动服务。

    1. 新建个脚本文件new_service.sh: scientific_network.sh
#!/bin/bash
### BEGIN INIT INFO
# Provides: scientific_network.sh
# Required-Start:    $local_fs $syslog $remote_fs dbus
# Required-Stop:     $local_fs $syslog $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start scientific_network.sh
### END INIT INFO

sudo sslocal -c /home/shadowsocks.conf -d start
sudo service privoxy start

exit 0

(上述的注释不可少, 否则设置优先级的时候会有不知名的错误发生)

    1. 设置权限
sudo chmod 755 scientific_network.sh
#或者
sudo chmod +x scientific_network.sh
    1. 把脚本放置到启动目录下
sudo mv scientific_network.sh /etc/init.d/
    1. 将脚本添加到启动脚本 (执行如下指令,在这里90表明一个优先级,越高表示执行的越晚)
cd /etc/init.d/
sudo update-rc.d scientific_network.sh defaults 90
  • x. 移除Ubuntu开机脚本
sudo update-rc.d -f scientific_network.sh remove
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment