Ubuntu Server 安装 shadowsocks 客户端

Ubuntu Server 安装 shadowsocks 客户端

安装步骤

  1. 安装 shadowsocks

    # 如果python-pip没安装,则安装。已经安装则忽略
    $ apt-get install python-pip
    # 安装shadowsocks
    $ pip install shadowsocks
  2. 配置 shadowsocks

    # 配置文件shadowsocks.json文件
    $ sudo vi /etc/shadowsocks/config.json

    填写如下配置项

    {
     "server": "您服务器地址",
     "server_port": 9999,
     "local_address": "127.0.0.1",
     "local_port": 1080,
     "password": "密码",
     "timeout": 600,
     "method": "aes-256-cfb"
    }
  3. 启动 shadowsocks

    # 后台启动(此命令启动后,会启动一个sock5代理服务)
    $ sslocal -c /etc/shadowsocks/config.json -d start
    # 查看启动进程
    $ ps -ef | grep sslocal

    注意:启动 shawdowsocks 服务后,只是启动一个 sock5 代理服务,此时我们的 http/https 还是无法访问,此时就需要将 http 服务转换为 sock5。我们可以借助 polipo 来实现。


polipo / privoxy 二者选其一使用


polipo 实现全局代理

  1. 安装配置 polipo

    # 安装polipo
    $ sudo apt-get install polipo
    # 配置polipo
    $ sudo vi /etc/polipo/config
    # 配置项目如下:
    logSyslog = true
    logFile = /var/log/polipo/polipo.log
    proxyAddress = "0.0.0.0"
    socksParentProxy = "127.0.0.1:1080"
    socksProxyType = socks5
    chunkHighMark = 50331648
    objectHighMark = 16384
    serverMaxSlots = 64
    serverSlots = 16
    serverSlots1 = 32
    # 配置成功后,重启polipo服务
    $ sudo /etc/init.d/polipo restart
  2. 设置终端命令行代理

    polipo 的默认端口号是 8123

    # 设置http_proxy、https_proxy代理
    $ export http_proxy=http://127.0.0.1:8123;export https_proxy=http://127.0.0.1:8123;
    # 注:如果要取消代理,则执行如下命令
    $ unset http_proxy
    $ unset https_proxy
  3. 测试

    # ip.sb站点测试,如果返回ip为shadowsocks的服务地址,则说明成功
    $ curl ip.sb
    # ip.cn站点测试
    $ curl ip.cn
    # curl测试,返回html内容则表示成功
    $ curl www.google.com
  4. 设置终端长期代理

    # 修改bash配置.bashrc
    $ sudo vi /.bashrc
    # 配置添加
    export http_proxy=http://127.0.0.1:8123;export https_proxy=http://127.0.0.1:8123;
    # 重启source
    $ source ~/.bashrc
  5. 服务器重启后随即启动配置

    # 服务器重启后,手工重启shadowsocks
    $ sslocal -c /etc/shadowsocks.json -d start
    # 配置随机启动
  6. 附录:git 代理设置

    9.1. 基本知识:git clone有两种形式,一个是走http(https),另一种是ssh。两种方式的代理设置不一样。两种方式示例如下:

    # 使用ssh方式(Clone with SSH)
    $ git clone git@github.com:enpenguc/linux-backup.git
    # 使用https方式(Clone with HTTPS)
    $ git clone https://github.com/enpenguc/linux-backup.git

    9.2. http(https)代理设置如下:

    # git设置https代理
    $ git config --global https.proxy https://127.0.0.1:8123
    # 或者走socks5 代理(如Shadowsocks客户端,MAC下sock5端口设置为1087)
    $ git config --global https.proxy "socks5://127.0.0.1:1087"
    # 设置了https代理后即可通过https方式clone代码
    $ git clone https://github.com/enpenguc/linux-backup.git
    # 另附:取消https代理命令,如下
    $ git config --global --unset https.proxy

    9.3. ssh方式:

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

    # 修改~/.ssh/config文件
    $ vim ~/.ssh/config

    9.3.2 填入一下配置(注意方式1方式2开启一个即可,如当前开启方式2:走 socks5 代理(如 Shadowsocks)

    # 必须是 github.com
    Host github.com
      HostName github.com
      User git
      # 方式1:走 HTTP 代理
      # ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=1087
      # 方式2:走 socks5 代理(如 Shadowsocks,当前mac的socks配置端口为1086)
      ProxyCommand nc -v -x 127.0.0.1:1086 %h %p

    9.3.3 配置好ssh代理后,即可使用ssh方式clone代码

    # 使用ssh方式(Clone with SSH)
    $ git clone git@github.com:enpenguc/linux-backup.git

privoxy 实现全局和自动代理

privoxy 可以配置 .action 格式的代理规则文件。通过控制规则文件实现全局和自动代理。

action 文件可以手动编辑,也可以从 gfwlist 生成。
下面将先介绍 privoxy 的安装配置,再介绍 action 文件的生成。

安装配置

安装 privoxy:

apt-get update
apt-get install privoxy

进入目录 /etc/privoxy,可以看到目录结构大致为:

  • config 配置文件,这个文件很长。。
  • *.action 代理规则文件
  • *.filter 过滤规则文件
  • trust 不造干嘛用
  • templates/ 同上

开始修改配置文件。

privoxy 有 filter (过滤)的功能,可以用来实现广告拦截。不过这里只希望实现自动代理,在配置文件中把 filter 部分注释掉:

# 大约在435行
# filterfile default.filter
# filterfile user.filter      # User customizations

我们将使用自定义的 action 文件,所以把默认的 action 文件注释掉,并添加自定义文件:

# 386行左右
# 默认的 action 文件
# actionsfile match-all.action # Actions that are applied to all sites and maybe overruled later on.
# actionsfile default.action   # Main actions file
# actionsfile user.action      # User customizations
# 自定义 action 文件
actionsfile my.action

可以指定转换后的 HTTP 代理地址,这里直接使用默认端口 8118:

# 785行左右(注释不要打开,经过测试默认会启用8118)
listen-address  127.0.0.1:8118
listen-address  [::1]:8118

如果代理规则直接写在配置文件 config 中,那么代理规则和本地 SS 代理地址是写在一起的:

# / 代表匹配全部 URL,即全局代理
forward-socks5 / 127.0.0.1:1080 .

# 根据规则自动代理
forward-socks5 .google.com 127.0.0.1:1080 .

注意!每行最后还有一个点。

实现全局代理就是第一种写法了。

但是如果要自动代理,第二种直接写在配置文件里的做法其实不太合适,更合适的做法是写成 action 文件,配置文件中只管引用。

把上面的注释掉
新建 action 文件 my.action,内容如下:

# 这一行表示本 action 文件中所有条目都使用代理
{+forward-override{forward-socks5 127.0.0.1:1080 .}}
# 添加一条规则
.google.com

把 privoxy 转换后的地址 http://127.0.0.1:8118 添加到环境变量,可以参照 polipo 部分。

启动 privoxy,这时应该可以正常访问 Google 了:

service privoxy start
curl www.google.com

***以下配置用不到可以不设置***

下面看一下怎么用 gfwlist 生成 action 文件。

生成 action 文件

配置文件 config 或 action 文件修改后不需要重启 privoxy。

使用的工具是 gfwlist2privoxy。这个工具很简单,文档就几行,写得也很清楚。

安装:

pip install gfwlist2privoxy

gfwlist2privoxy 不支持 python3.x,安装时注意使用的是 pip2 还是 pip3。
参数说明:

-i/--input 输入,本地 gfwlist 文件或文件 URL。这里使用上面的 gfwlist
-f/ --file 输出,即生成的 action 文件的目录。这里输出到 /etc/privoxy/gfwlist.action
-p/ --proxy SS 代理地址,生成后可以修改。这里是 127.0.0.1:1081
-t/ --type 代理类型,生成后也可以修改。这里是 socks5
--user-rule 用户自定义规则文件,这个文件中的规则会被追加到 gfwlist 生成的规则后面

示例:

gfwlist2privoxy -i https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt -f /etc/privoxy/gfwlist.action -p 127.0.0.1:1081 -t socks5

得到文件 /etc/privoxy/gfwlist.action:

# gfwlist.action generated by gfwlist2privoxy, 2018-08-02 07:36:00 +0000
# https://github.com/snachx/gfwlist2privoxy

{+forward-override{forward-socks5 127.0.0.1:1081 .}}

# 规则列表
...

最后,把 /etc/privoxy/config 中的 actionsfile my.action 改为 actionsfile gfwlist.action 就完成了。

相关指令

# 启动shadowsocks代理
 sslocal -c /etc/shadowsocks/config-hongkong.json #香港服务器
 sslocal -c /etc/shadowsocks/config-america.json  #美国服务器

#privoxy-端口监听转发器
 service privoxy start #启动
 service privoxy stop  #关闭

# polipo-端口监听转发器
 /etc/init.d/polipo start #启动
 /etc/init.d/polipo stop  #关闭

FAQ

  1. 环境变量的配置 很多教程都只添加了 http_proxy 一项,但是实际使用中发现也需要设置 https_proxy

  2. 另外,关于地址的写法,只写 127.0.0.1:8123 时,遇到过有软件不能识别的情况,改为写完整的地址 http://127.0.0.1:8123/ 就不会有问题了。

  3. ping 测试不通, ping www.google.com没用响应

    ping 测试不通,这是正常的。原因 sock 代理走 TCP/UDP 协议,而 ping 命令走 ICMP 协议。ping 一个网站地址,操作系统需要请求 DNS 服务器,解析到 IP 地址,才能够发出 ICMP 包。ping 不通 google.com 只能说明当前的网络 DNS 服务器不解释此域名。

发表回复

京ICP备15027918号-1