首页  >  问答  >  正文

python - Linux终端全局代理翻墙

我想通过设置HTTP_PROXY和HTTPS_PROXY方法实现代理,格式如下

$ export HTTP_PROXY="http[socks5]://user:pass@server_address:port/"
$ export HTTPS_PROXY="http[socks5]://user:pass@server_address:port/"

昨天我买了一台阿里云的服务器,经过SSH -D user@server然后通过Chrome下的proxy插件可以实现翻墙。现在我想让用设置环境变量的方式代理实现终端下也能访问Goole等网站的页面,请问我应该怎样在我的阿里云服务器上面配置呢?

附录:
自己在Python爬虫里面通过下面的方式利用Shadowsocks已经可以爬取墙外的网站了,现在主要想在终端下使用wget, youtube-dl等命令翻墙。

 proxy_handler = SocksiPyHandler(socks.SOCKS5, '127.0.0.1', 1080)
PHP中文网PHP中文网2740 天前1986

全部回复(2)我来回复

  • 高洛峰

    高洛峰2017-04-18 09:36:01

    针对你标题想实现全局代理的目的, 可以看这篇文章其中的iptables部分iptables部分

    https://linuxaria.com/article...

    我摘抄其中iptables配置如下:

    #!/bin/bash
    # Create new chain
    iptables -t nat -N REDSOCKS
     
    # Ignore LANs and some other reserved addresses.
    iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
    iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
    iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
    iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
    iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
    iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
    iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
    iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
     
    # Anything else should be redirected to port 31338
    iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 31338
     
    # Any tcp connection made by `linuxaria' should be redirected, put your username here.
    iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner linuxaria -j REDSOCKS
    

    此配置实现了把所有除本地局域网连接以外的TCP连接全部转发到 31338 端口, 显然你应该用代理软件提前监听这个端口, 当然也可以是其他任意指定的代理监听端口.

    PS: 全局代理其实很不实用, 最好分应用代理, 至于你说的要让wget这些命令也使用代理的话, 用之前临时exportenv就好了

    比如我的代理是扔在192.168.100.100上面, 端口1080, 我要wget下 YouTube:

    $ export https_proxy=http://192.168.100.100:1080
    $ wget www.youtube.com
    --2016-09-06 13:00:28--  http://www.youtube.com/
    Connecting to 192.168.100.100:1080... connected.
    Proxy request sent, awaiting response... 301 Moved Permanently
    Location: https://www.youtube.com/ [following]
    --2016-09-06 13:00:32--  https://www.youtube.com/
    Connecting to 192.168.100.100:1080... connected.
    Proxy request sent, awaiting response... 200 OK
    Length: unspecified [text/html]
    Saving to: “index.html”
    
        [           <=>                                 ] 391,815      133K/s   in 2.9s    
    
    2016-09-06 13:00:36 (133 KB/s) - “index.html” saved [391815]

    轻松加愉快, 当然有的应用可能不支持Socks5协议, 直接搜"socks5 to http"一堆答案, 我一般用privoxy

    https://linuxaria.com/article...🎜 🎜我摘抄其中iptables配置如下:🎜 rrreee 🎜此配置实现了把所有除本地局域网连接以外的TCP连接全部转发到 31338 端口, 显然你应该用代理软件提前监听这个端口, 当然也可以是其他任意指定的代理监听端口.🎜 🎜PS: 全局代理其实很不实用, 最好分应用代理, 至于你说的要让wget这些命令也使用代理的话, 用之前临时exportenv就好了🎜 🎜比如我的代理是扔在192.168.100.100上面, 端口1080, 我要wget下 YouTube:🎜 rrreee 🎜轻松加愉快, 当然有的应用可能不支持Socks5协议, 直接搜"socks5 to http"一堆答案, 我一般用privoxy🎜

    回复
    0
  • PHPz

    PHPz2017-04-18 09:36:01

    关于代理相关的,推荐两个工具 proxychains 和 privoxy,像wget, youtube-dl 是可以在参数中指定代理的,所以你如果有 ss 服务器,在本地设置好 ss 代理就好。我觉得还是针对应用使用代理,而不是搞系统级别的全局代理。

    回复
    0
  • 取消回复