用tsocks和proxychains代理Linux下所有软件

境内很多网站又是无法访问 (twitter.com、facebook.com 等),让我们这些网民很无奈。

使用 SSH 翻墙
我曾经介绍过如何使用 SSH 来建立一个 SOCKS 代理服务器,让你能够在 Firefox 里正常访问以上所提起的网址。然而不是所有 Linux 软件都能支持代理服务器。如果你最热爱的 Linux 工具需要访问“被封”的网站,又没有嵌入的代理支持,该怎么办呢?
遇到这种情况当然不要放弃该软件… 毕竟我们用的系统是 Linux 而不是以前让我们咳声叹气,丧失信心的 Windows,总有一个方法去解决问题。

举个例子吧
我 不久前发现了 Twitter 这个网站。我一开始不经常用,也搞不明白别人为什么对这个 web 2.0 服务都着了迷。后来我在推特上跟的人越来越多,跟着我的人亦是日益增多,不知不觉我也迷上了该网站,天天都会上。凡是经常用推特的人一般都会用一个推特的 客户端,这才能跟得上朋友们的状态更新和最火热的网络新闻。本人作为 Ubuntu 的用户,我自然就选了 Gwibber 这个基于 GNOME 的客户端来访问我的推特。这个软件功能很丰富,用起来得心应手,不过总有一个问题让我有点遗憾,就是 Gwibber 还不听从 GNOME 的代理设置。平时这也不是一个很大的问题,但是每遇中国网络封锁较严重时,都会让我暂时无法使用该软件。

解决方案… Tsocks
经过几个 Google 搜索,我最终很高兴地发现 Linux 有一个能够强迫任何软件通过 SOCKS 代理上网的工具,其名就是 tsocks。Tsocks 是一个透明 SOCKS 代理软件,只要你电脑有一个连接到国外服务器的 SSH 隧道,你就能让任何软件翻墙。

安装并配置 Tsocks
以下说明都是为了那些使用 Ubuntu 的 Linux 用户,不过在别的 Linux 发行版下,安装的过程应该与此差不多。

在终端中:
sudo apt-get install tsocks
修改配置文件:
sudo nano /etc/tsocks.conf
将其内容改成以下几行并保存退出:
local = 192.168.1.0/255.255.255.0 #local表示本地的网络,也就是不使用socks代理的网络
server = 127.0.0.1 # SOCKS 服务器的 IP
server_type = 5 # SOCKS 服务版本
server_port = 9999 # SOCKS 服务使用的端口

你可能需要修改一下以上内容,用你自己的 SSH 隧道设置。

运行软件
用 tsocks 运行你的软件很简单,在终端中:
tsocks 你的软件 &
我现在运行 Gwibber 都是这样运行的:
tsocks gwibber &
祝你们翻墙愉快!

EDIT—————–>

我今天还发现了另外一个工具,其功能似乎比 tsocks 要更丰富,配置起来更简单,而且不会那么容易出错。这个工具就是 proxychains。以下有配置方法:
sudo apt-get install proxychains
修改配置文件 (/etc/proxychains.conf),应该如下:
# proxychains.conf VER 2.0
#
# HTTP, SOCKS4, SOCKS5 tunneling proxifier.
#

# The option below identifies how the ProxyList is treated.
# only one option should be uncommented at time,
# otherwise the last appearing option will be accepted
#
# Dynamic - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# at least one proxy must be online to play in chain
# (dead proxies are skipped)
# otherwise EINTR is returned to the app
#
# Strict - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# all proxies must be online to play in chain
# otherwise EINTR is returned to the app
#
# Random - Each connection will be done via random proxy
# (or proxy chain, see chain_len) from the list
# this option is good for scans

dynamic_chain
#strict_chain
#random_chain

# Make sense only if random_chain
chain_len = 2

# Quiet mode (no output)
#quiet_mode

# Write stats about good proxies to proxychains.stats
#write_stats

#Some timeouts in milliseconds
#
tcp_read_time_out 15000
tcp_connect_time_out 10000

[ProxyList]
# ProxyList format
# type host port [user pass]
# (values separated by 'tab' or 'blank')
#
#
# Examples:
#
# socks5 192.168.67.78 1080 lamer secret
# http 192.168.89.3 8080 justu hidden
# socks4 192.168.1.49 1080
# http 192.168.39.93 8080
#
#
# proxy types: http, socks4, socks5
# ( auth types supported: "basic"-http "user/pass"-socks )
#
#http 10.0.0.5 3128
socks5 127.0.0.1 9999
socks4 127.0.0.1 9050
注意事项:

  1. 要选 dynamic_chain 而不是 random_chain
  2. 可以列举几个代理服务器,proxychains 会按顺序用,代理无法访问即自动选用下一个
  3. 代理服务器要根据自己电脑的情况自行调整

运行 proxychains
运行 proxychains 跟运行 tsocks 完全一样。在终端中:
proxychains 你的软件 &
比如说:
proxychains chromium-browser &
我还是推荐你使用 proxychains!

关于tsocks:

http://tsocks.sourceforge.net/

SOCKS servers are a form. of proxy that are commonly used in firewalled LAN environments to allow access between networks, and often to the Internet. The problem is that most applications don't know how to gain access through SOCKS servers. This means that network based applications that don't understand SOCKS are very limited in networks they can reach. An example of this is simple 'telnet'. If you're on a network firewalled from the internet with a SOCKS server for outside access, telnet can't use this server and thus can't telnet out to the Internet.
tsocks' role is to allow these non SOCKS aware applications (e.g telnet, ssh, ftp etc) to use SOCKS without any modification. It does this by intercepting the calls that applications make to establish network connections and negotating them through a SOCKS server as necessary.

toscks可以应用程序不经过任何修改就可以轻松地使用已有socks代理,本人主要遇到的问题是ssh, telnet, git使用代理不是那么方便设置,用socks可以解决我的问题。

另外,tsocks是一个开源免费的项目,使用GPL License。

 

下载和安装tsocks:

  1. rpm包安装:可以到http://pkgs.org/搜索tsocks,找到自己对应发行版的rpm包
  2. Ubuntu等可以apt-get install tsocks来下载并安装
  3. 源码安装:可以到这里下载http://tsocks.sourceforge.net/download.php并自己编译安装

(BTW,我用的RHEL6u1系统,采用的是RPM安装方式)

 

配置tosck.conf

一般来说,tsocks的配置文件在/etc/tsocks.conf,rpm包安装好之后一般都会有这个文件,如果没有,也可以自己创建一个。可能Ubuntu等一些发行版,配置文件有所不同。

将我的配置文件/etc/tsocks.conf分享一下吧:

# This is the configuration for libtsocks (transparent socks)

# Lines beginning with # and blank lines are ignored

#

# This sample configuration shows the simplest (and most common) use of

# tsocks. This is a basic LAN, this machine can access anything on the

# local ethernet (192.168.0.*) but anything else has to use the SOCKS version

# 4 server on the firewall. Further details can be found in the man pages,

# tsocks(8) and tsocks.conf(5) and a more complex example is presented in

# tsocks.conf.complex.example

 

# We can access 10.*.*.* directly

local = 10.0.0.0/255.0.0.0

 

# Otherwise we use the server

server = proxy.xxx.com #your proxy domain or IP address

server_type = 5   #to use socks V5

server_port = 1080 #the port of your porxy

 

使用tsocks:

tsocks [application [applications arguments]]

tsocks [on|off]

举几个实例吧:

tsocks firefox #之后再firefox上浏览网页就会默认使用tsocks配置的代理

tsocks git clone git://github.com/avikivity/kvm.git #使用proxy来git,这就是我用的原因

 

source tsocks on #设置当前shell的tsocks,该命令可能遇到一个小bug在下面有讲到

tsocks show   #查看当前Shell的tsocks设置,tsocks: This shell is socksified.

do_something   #这里就能默认使用tsocks的代理去do something了,前面已经设置了

source tsocks off #关闭当前shell的tsocks代理设置

tsocks show   #再次查看,tsocks: This shell is NOT socksified.

 

 

遇到的问题及解决方案:

13:01:17 libtsocks(2432): SOCKS server proxy-shz.intel.com (10.239.120.36) is not on a local subnet!
设置的代理没有与/etc/tsocks.conf文件中的local设置的网络在同一个子网中。

3:03:26 libtsocks(2451): The SOCKS server (proxy.ABCD.com) listed in the configuration file which needs to be used for this connection is invalid
配置文件的问题,我也忘了是啥时出现的错误了,但我碰到过的。
13:02:50 libtsocks(2441): Error 110 attempting to connect to SOCKS server (Connection timed out)
这个很明显了,就是你设置的proxy连接不上(可能是本来就连不上,或者没在同一个局域网内),请检查和确认配置。

ERROR: ld.so: object '/lib64/libtsocks.so' from LD_PRELOAD cannot be preloaded: ignored.
郁闷坏了,是因为有一次我在rhel5u5上安装了一个rhel6的tsock rpm包,解决办法是到http://pkgs.org/找到一个tsocks for rhel5的rpm包,安装即可。

 

[root@kvm-build ~]# source /usr/bin/tsocks on

basename: invalid option -- 'b'

Try `basename --help' for more information.

看到这样的错误,有可能是你的tsocks有点小bug,不过不影响使用的,接着你用tsocks show来看,一样可以看到提示说socks设置成功的,tsocks: This shell is socksified.

来源: https://sites.google.com/a/pickdreams.org/snail-library/Home/yong-tsocks-heproxychains-dai-lilinux-xia-suo-you-ruan-jian

发表评论