手头上刚好有个GEC(google compute engine),原本为了测试七牛CDN国际线路。想着空着也是空着,何不物尽其用,搭建一个个人自用的proxy呢?说干就干。

Proxy

在网上搜了一圈,发现了个轻量级的开源代理软件 —— tinyproxy。几乎不太用配置。

  1. 通过apt-get安装

    1
    
    sudo apt-get install tinyproxy
    
  2. 安装完成,检查服务是否启动

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    spencer_shou@test-instance:~$ sudo systemctl status tinyproxy
    ● tinyproxy.service - Tinyproxy lightweight HTTP Proxy
       Loaded: loaded (/lib/systemd/system/tinyproxy.service; enabled; vendor preset: enabled)
        Active: active (running) since Wed 2020-08-12 09:36:29 UTC; 2 weeks 5 days ago
      Docs: man:tinyproxy(8)
            man:tinyproxy.conf(5)
    Process: 4578 ExecStart=/usr/sbin/tinyproxy (code=exited, status=0/SUCCESS)
    Main PID: 4582 (tinyproxy)
        Tasks: 22 (limit: 4915)
    CGroup: /system.slice/tinyproxy.service
            ├─ 4582 /usr/sbin/tinyproxy
            ├─ 4583 /usr/sbin/tinyproxy
            ├─ 4584 /usr/sbin/tinyproxy
            ├─ 4585 /usr/sbin/tinyproxy
            ├─ 4586 /usr/sbin/tinyproxy
            ├─ 4587 /usr/sbin/tinyproxy
            ├─ 4588 /usr/sbin/tinyproxy
            ├─ 4589 /usr/sbin/tinyproxy
            ├─ 4590 /usr/sbin/tinyproxy
            ├─ 4591 /usr/sbin/tinyproxy
            ├─ 4592 /usr/sbin/tinyproxy
            ├─ 4615 /usr/sbin/tinyproxy
            ├─ 4616 /usr/sbin/tinyproxy
            ├─ 4617 /usr/sbin/tinyproxy
            ├─ 4622 /usr/sbin/tinyproxy
            ├─ 4792 /usr/sbin/tinyproxy
            ├─ 4793 /usr/sbin/tinyproxy
            ├─ 5304 /usr/sbin/tinyproxy
            ├─ 5305 /usr/sbin/tinyproxy
            ├─ 5306 /usr/sbin/tinyproxy
            ├─ 5309 /usr/sbin/tinyproxy
            └─30934 /usr/sbin/tinyproxy
    
  3. 修改配置/etc/tinyproxy/tinyproxy.conf

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    #
    # Port: Specify the port which tinyproxy will listen on.  Please note
    # that should you choose to run on a port lower than 1024 you will need
    # to start tinyproxy using root.
    #
    Port 8888 # 默认端口 8888, 按需修改
    
    # Allow: Customization of authorization controls. If there are any
    # access control keywords then the default action is to DENY. Otherwise,
    # the default action is ALLOW.
    #
    # The order of the controls are important. All incoming connections are
    # tested against the controls based on order.
    #
    Allow 127.0.0.1  # 只允许访问的IP,必须完全相同
    #Allow localhost
    #Allow 192.168.0.0/16
    #Allow 172.16.0.0/12
    #Allow 10.0.0.0/8
    
  4. 如果修改了配置文件,重置服务

    1
    
    sudo systemctl reload-or-restart tinyproxy
    
  5. 检查端口是否打开

    1
    2
    3
    
    spencer_shou@test-instance:~$ sudo netstat -ntlp | grep 8888
    tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      4582/tinyproxy
    tcp6       0      0 :::8888                 :::*                    LISTEN      4582/tinyproxy
    

至此,proxy部分完成。这里有个问题,我的配置里面只允许127.0.0.1访问。同时,GCE里面也没有暴露外部端口,那该怎么访问呢?答案就是使用SSH隧道

1
2
3
4
5
为什么这么设置:

1.安全方面的考虑,网络上的端口开得越少越好,减少被攻击的风险。
2.我使用的网络是ADSL的,没有固定的IP,不太好配置。
3.网络流量是要收费的嘎。

SSH隧道

端口转发示意图

万事具备只欠东风。只要一条命令就可以搞定,直接来个本地端口转发吧。(把本地端口映射到远端服务器的端口)

1
2
3
4
# ssh -L [本地端口]:[服务端IP]:[服务端端口] [用户名]@[远端地址或者域名]
$ ssh -L 8888:127.0.0.1:8888 spencer@35.132.35.124

# 注意,这里的127.0.0.1:8888必须和前面的配置文件里面一致,否者访问不了

用了这条命令后,SSH隧道就建立好了。(相当于SSH登入到远程机器一样)

使用代理

1
2
export http_proxy=127.0.0.1:8888
export https_proxy=127.0.0.1:8888

References

  1. 实战SSH端口转发
  2. 轻量级代理Tinyproxy