引言:
Linux是一款免费的而且在性能和安全性等各方面都很出色的操作系统。它的发行遵循GNU组织的GPL许可协议,用户在获得操作系统的同时还可以得到操作系统的源代码。基于Linux已经有了很多成熟的应用。其中代理服务就是一种在Linux中非常广泛的应用。
代理服务简介:
随着互联网的飞速发展,IP地址资源非常紧缺。为了缓解这样的矛盾,一般在局域网中并不是所有的主机都有合法的IP地址。而是采用代理服务器的方式将局域网中拥有局域网IP地址的主机联入互联网。代理服务器一般都有两个网络接口(或网卡),一个网络接口连接互联网,使用的是公网IP,而另一个网络接口连接内部局域网,使用局域网IP地址(如192.168系列或者172.16系列)。在Linux当中ftp/http代理使用最多的性能最好的代理软件应该是Squid,但是,对于OICQ这类使用UDP的软件Squid则无法实现。而Socks5正是解决这类问题的服务器软件。本文就Linux下Sock5代理的安装与使用进行讨论。
Socks5代理简介:
Socks5其实是一种代理协议socks的第五个版本,它工作在OSI模型中的第五层——会话层,很好的实现了认证,保密性等问题。它有非常详细的访问控制,比网络层有更多的控制手段,而且使用Socks5的代理服务器可隐藏内部网络结构,但是,它的性能比一些底层协议(如:网络层协议)要差一些。
Socks5代理服务器的安装:
在Linux的发行版当中一般都没有自带,所以必须下载源代码安装。
1.Socks5代理服务器源代码的下载网址为http://archive.socks.permeo.com/cgi-bin/download.pl,在此网址的下拉列表框中选择:“socks5 v1.0 release 11 – UNIX Source (current)”,然后提交,接下来填写一张表单后即可下载,下载下来的文件应该为“socks5-v1.0r11.tar.gz”。
2.下载补丁的网址为:http://archive.socks.permeo.com/patch-socks5.html,在此网址中,将文件“socks5-v1.0r11.patch1.txt”保存到本地。
3.在下载文件的目录下用下列命令对“socks5-v1.0r11.tar.gz”解压缩。将会生成目录“socks5-v1.0r11”。
命令:
- gzip -d socks5-v1.0r11.tar.gz
- tar -xvf socks5-v1.0r11.tar
或者
- tar –zxvf socks5-v1.0r11.tar.gz
4.将补丁文件拷贝到目录“socks5-v1.0r11”下,并且并在此目录下执行如下命令打补丁。
命令:
- patch < socks5-v1.0r11.patch1.txt
根据提示输入“include/sigfix.h”即可。
5.在“socks5-v1.0r11”目录下执行如下命令编译和安装Socks5代理。
命令:
- ./configure --prefix=/usr/local/socks5 --enable-threads
- make;make install
Socks5代理服务器的配置:
在“/etc/”目录下创建“socks5.conf”文件。这个文件是socks5代理服务器的最主要的文件,在这个文件当中我们可以详细的设置访问权限,有关详细的设置方法请参考man手册,这里介绍最简单的方法,将源代码目录下的“examples/socks5.conf.singlehomed”文件复制到“/etc/”目录。在源代码目录下执行如下命令:
cp examples/socks5.conf.singlehomed /etc/socks5.conf
在命令行启动socks5代理服务器可以使用如下命令:
- /usr/local/socks5/bin/socks5 -t -f –s
若启动成功会在屏幕上显示类似“25519:000000: Socks5 starting at Mon Feb 24 09:37:04 2003 in threading mode”的一行信息。如果有用户使用代理服务器则在屏幕上将出现访问记录。
Socks5代理客户端的配置
将需要使用socks5代理的程序设置为使用刚配好的代理的内部IP(例如:socks5代理服务器的内部IP为192.168.0.1),端口设置为1080(这是socks代理服务器的默认端口)。设置完成之后就可以使用了,当进行通信时。在服务器的屏幕上会出现通信记录。
记录代理日志
上面给大家介绍了socks5代理的安装和简要配置,但是,用户的使用记录只在屏幕上显示,按照网络管理的有要求还必须有使用日志。下面介绍一下如何记录代理日志。
将下面的内容
- /usr/local/socks5/bin/socks5 -t -s 2>> /var/log/socks.log &
加到“/etc/rc.d/rc.local”文件的尾部。在每次启动系统时自动启动socks5代理。“2>>/var/log/socks.log”是用来将socks5代理的错误输出信息存储到日志中。以后就可以从日志文件中查看用户的使用记录。
总结:
本文介绍了在Linux下实现socks代理的详细过程,其中涉及到一些概念不能详细论述请参考相关的文档。另外有关配置文件“/etc/socks5.conf”有很多权限设置选项,本文只是使用了系统自带的一个例子文件,在实际应用中要作权限设置时请参考man手册(使用命令:man socks5.conf查看)。
**************************************************************************************
sock5代理服务器-SOCKS5篇
1.安装SOCKS5
# tar -zxvf socks5-v1.0r11.tar.gz
# cd socks5-v1.0r11
# patch -p0 < socks-trans-v1.3-patch
# ./configure --with-threads
# make
# make install
2.配置SOCKS5
# cd examples
# cp socks5.conf.singlehomed /etc/socks5.conf
# vi /etc/socks5.conf
======+======+======+======+======+======
permit u - - - - -
set SOCKS5_BINDINFC 192.168.0.8:1080
auth - - u
set SOCKS5_NOIDENT
set SOCKS5_TIMEOUT 15
set SOCKS5_V4SUPPORT
set SOCKS5_MAXCHILD 25
======+======+======+======+======+======
3.SOCKS5用户认证
# vi /etc/socks5.passwd
======+======+======+======+======+======
userA passwdA
userB passwdB
userC passwdC
======+======+======+======+======+======
4.测试与运行SOCKS5
测试SOCKS5
# socks5 -f -s
XXXXX: Socks5 starting at Mon Jun 21 01:23:45 2002 in normal mode
显示上面的信息表示服务配置没有问题。
启动SOCKS5
# touch /var/log/socks5
# socks5 -t -s 2>> /var/log/socks5
停止SOCKS5
# stopsocks -KILL
重新加载配置文件
# stopsocks -HUP
附录->高级配置:
1.使用非默认端口:比如为8888:
# socks5 -b 8888 -t -s 2>> /var/log/socks5
相应的关服务命令为:
# stopsocks -p 8888 –KILL
如果想限制只是某个IP段(192.168.0.0/255.255.255.0)可以访问,可以改成
permit - - 192.168.0. - - -
或者只允许某个IP(192.168.0.10)可以访问使用,可以改成
permit - - 192.168.0.10 - - -
2.使用自建的密码文件,比如/etc/my.passwd。这时,只要修改/etc/socks5.conf文件,在其中新加一条项数:
set SOCKS5_PWDFILE /etc/my.passwd
3.指定SOCKS v5绑定的ip地址和监听的端口。如果不指定绑定的IP将使用0.0.0.0
set SOCKS5_BINDINFC 192.168.0.8:1080
4.忽略ident请求。当客户机没有运行identd时,使用SOCKS5_NOIDENT将降低超时值
set SOCKS5_NOIDENT
5.指定连接停顿最长时间(分钟),超过最大值后,socks5断开连接
set SOCKS5_TIMEOUT 15
6.让SOCKS V4 协议的请求,默认不接受
set SOCKS5_V4SUPPORT
7.指定同时存在的最大子进程数,Socks5预设为64
set SOCKS5_MAXCHILD 25
socks5.conf中permit后面的6个“-”的含义是
permit auth cmd src-host dest-host src-port dest-port [user-list]
附录,socks5 server配置文件(只要定制好自己的配置方式后,将条目写到/etc/socks5.conf中即可。有人用cp example目录下的文件方法来生成配
置文件,个人不提倡这样,反正我也出现过错,自己按格式来保证不出错)
socks5.conf通常由以下几个方面的内容构成:
-ban host:定义拒绝服务的客户列表
-authentication:定义Socks5服务器使用的用户认证方法
-interface:定义Socks5服务器绑定的ip地址和服务端口
-variables and flags:定义Socks5服务器运行的环境
-proxies:定义客户可以通过Socks5服务器访问的地址列表以及Socks5服务器访问这些地址的方法
-access control:定义Socks5服务器接受或拒绝客户连接的规则
下面我们分别来讲述这些条目对应的语法:
ban host
语法:ban source-host source-port
说明:Socks5服务器将拒绝接受来自source-host:source-port的客户连接。
authentication
语法:auth source-host source-port auth-methods
说明:对于来自source-host:source-port的客户连接,Socks5服务器将使用
auth-methods所定义的用户认证方法。对于没有定义认证方法的客户将使用任何可以使用的认证方法。
interface
语法:interface hostpatern portpattern interface-address
说明:来自source-host:source-port的客户连接由interface-address处理;目的地址为source-host:source-port的客户连接由Socks5 代理服务器从
interface-address发出连接请求。
variables
语法:set variable value
说明:定义Socks5运行参数,Socks5有以下一些常用的运行参数:
SOCKS5_BINDINTFC hostort
hostort 指定socks5运行的主机和端口号,用于代替缺省的端口。忽略时,socks5用0.0.0.0作为主机值。
SOCKS5_CONFFILE filename
Filename 指定配置文件。在许多系统中,缺省是/etc/socks5.conf。在运行socks5之前,设置这个变量。如果有多个socks5
daemon运行,为每个daemon使用不同的配置文件。
SOCKS5_DEMAND_IDENT
当客户没有响应ident 请求时,认证失败。使用SOCKS5_DEMAND_IDENT确认每个连接有一个关联的用户名。
SOCKS_ENCRYPT
如果可能的话,请求下一个socks5进程加密数据。SOCKS5_ENCRYPT 仅仅在编译socks5时包括了GSS-API认证时,才有意义。
SOCKS5_FORCE_ENCRYPT
当认证方式支持加密时,强迫客户加密数据。
SOCKS5_IDENTFILE filename
Filename指定存储ident信息的文件名。在许多系统中,缺省是/tmp/socks5.ident。当有多个socks5 daemon运行时,SOCKS5_IDENTFILE非常有用。
SOCKS5_MAXCHILD val
val指定同时存在的最大子进程数。Socks5预设为64。可以降低预设置。不能超过64。Socks5运行在线程模式时,忽略此参数。当运行在oneshot或inet
d模式时,此参数不发生作用。
SOCKS_NOIDENT
忽略ident请求。当客户机没有运行identd时,使用SOCKS5_NOIDENT将降低超时值。
SOCKS_NOINTCHK
请求下一个socks5进程执行没有完整检查的代理请求。只有在编译时加入GSS-API认证时,SOCKS5_NOINTCHK才发生作用。
SOCKS_NONETMASKCHECK
指示daemon忽略检查主机的子网掩码。缺省时,daemon检查掩码,如果在同一子网时,在检查配置文件之前,直接连接。
SOCKS5_REVERSEMAP
总是试图影射地址到主机名。缺省时,socks5只有当主机名或域名在配置文件中使用时才影射。设置后,log文件将纪录主机名,这将降低性能。
SOCKS5_SERVICENAME
总是影射端口号到服务名。缺省时,socks5只有当服务名在配置文件中使用时才影射。设置后,log文件将纪录服务名,这将降低性能。
SOCKS5_PASSWD [password]
当socks5 daemon连接到其它socks服务器时,如果采用Username/Password 认证,用它来指定密码。
SOCKS5_PIDFILE filename
指定存储socks5进程ID的文件名。Socks5缺省存贮PID在/tmp/socks5.pid。你可以用—bindintfc参数或设置SOCKS5_BINDINTFC环境变量运行socks5在
不同于缺省端口的其它端口。当运行在不同于缺省端口的其它端口时,socks5存贮PID在/tmp/socks5.pid-port。
SOCKS5_PWDFILE filename
指定密码文件。在许多系统中,缺省是/etc/socks5.passwd。
SOCKS5_TIMEOUT minutes
指定连接停顿最长时间。超过最大值后,socks5断开连接。忽略此值时,缺省是15。
SOCKS5_UDPPORTRANGE port1-port2
指定一个Socks5用来发送UDP包的UDP端口范围。
SOCKS5_USER [user id]
当socks5 daemon连接到其它socks server时,如果采用Username/Password认证,用此变量指定用户名。
SOCKS5_V4SUPPORT
缺省时,socks5只接受SOCKS5协议(rfc 1928)的请求。设此变量后,socks5将接受SOCKS V4 协议的请求。
proxies
语法:proxy-type dest-host dest-port proxy-list
说明:当客户请求的目的为dest-host:dest-port时,Socks5将使用proxy-list中的代理服务器请求数据。
access control
语法:permit auth cmd src-host dest-host src-port dest-port [user-list]
deny auth cmd src-host dest-host src-port dest-port [user-list]
说明:通过这两条语句所定义的规则来进行客户访问控制。
我们再对以上语法作进一步的解释:
host的表示方法:
-:表示任意主机
n1.:表示n1.0.0.0/255.0.0.0
n1.n2.:表示n1.n2.0.0/255.255.0.0
n1.n2.n3.:表示n1.n2.n3.0/255.255.255.0
.domain.name:表示主机名以.domain.name结尾的主机
some.domain.name:表示主机名为some.domain.name的主机
port的表示方法
-:表示任意端口
service name:用/etc/service中定义的服务名来表示,如telnet
port number:直接指定数字端口,如80
[port_start,port_end]:指定一个端口范围,如[1024,6000]表示从端口1024到6000,(1024,6000)表示从端口1025到5999
auth的值
n:无用户认证
u:使用username/password用户认证方法
k:使用Kerberos用户认证方法
-:使用任何可用的用户认证方法
cmd的值
-:任何命令
c:connect
b:bind
u:UDP
p:ping
t:traceroute
user的值
-:任何用户
proxy的值
socks5: Socks 5
socks4: SOCKS 版本4
noproxy:不使用代理而直接连接
server的值
host: 指定服务器的hostname,使用缺省服务端口
hostort:指定服务器的hostname和该服务的监听端口
- #!/bin/bash
- RETVAL=0
- prog="socks5"
- start(){
- #FILE=/tmp/socks5.pid-1080
- #if [ $FILE ]
- /usr/local/bin/socks5 -t -s 2> /var/log/socks5
- RETVAL=$?
- return $RETVAL
- }
- stop(){
- echo -n -e $"Stopping $prog: \t[ OK ]\n"
- /usr/local/bin/stopsocks -KILL
- RETVAL=$?
- return $RETVAL
- }
- reload(){
- echo -n -e $"Reloading configuration:\t[ OK ]\n"
- PID=`cat /tmp/socks5.pid-1080`
- kill -HUP -$PID
- RETVAL=$?
- return $RETVAL
- }
- restart(){
- stop
- start
- }
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status $prog
- ;;
- restart)
- restart
- ;;
- reload)
- reload
- ;;
- *)
- echo $"Usage: $0 {start|stop|status|restart|reload}"
- RETVAL=1
- esac
man socks5
- [root@ad log]# man socks5
- socks5(1) socks5(1)
- NAME
- socks5 - A daemon that implements the SOCKS v5 protocol to execute
- proxy requests for a client
- SYNOPSIS
- socks5 [options]
- DESCRIPTION
- socks5 is a proxy server that implements the SOCKS v5 protocol. As a
- proxy server, socks5 authenticates, authorizes, and processes network
- requests for clients. It establishes connections to application hosts
- for client applications. When the client attempts to access the net-
- work, the client connects to the socks5 daemon instead of the applica-
- tion host.
- Following authentication, clients request that socks5 perform network
- activities for the client. The activities might include bind, connect,
- sendto, ping, or traceroute. The socks5 protocol is independent of
- application protocols, and can assist with different networking ser-
- vices, including telnet, ftp, finger, whois, gopher, and WWW access.
- The socks5 daemon supports two authentication methods:
- Username/Password - Refer to socks5.passwd(5) for additional infor-
- mation.
- Kerberos 5 - Requires that you build socks5 with the Kerberos
- libraries. For additional information about using Ker-
- beros 5 (GSS-API) authentication, please refer to the
- Kerberos man page.
- socks5 can run:
- standalone - the default
- preforking - include the -p option
- threaded - if your operating system supports POSIX threads and
- you configure socks5 with the --with-threads option.
- Include the -t option to run socks5 in threaded mode.
- All operating systems impose limits on the number of
- threads and open file descriptors per process.
- through inetd - include the -i option
- You can control some aspects of how the socks5 daemon works through the
- configuration file, environment variables, and options. Refer to the
- socks5.conf(5) documentation for additional information about the
- socks5 configuration file. The settings in the configuration file take
- precedence over the environment variable and option settings. The envi-
- ronment variable settings take precedence over the option settings.
- OPTIONS
- Separate multiple options with white space.
- -b [host:port]| --bindintfc [host:port]
- host:port specifies the host and the port number on that host on
- which socks5 runs, instead of the default port. When you omit
- host, socks5 uses 0.0.0.0 as the host value.
- -d [val] |--debug [val]
- Turn on debug mode and optionally set the debug level to val. If
- you omit val, socks5 sends only warning messages. Valid values
- for val are 1, 2, and 3. If you specify other values, socks5
- assumes 3. Valid values and their meanings are:
- 1 LOG_NOTICE Reports non-error conditions that may require
- special handling
- 2 LOG_INFO Reports informational messages
- 3 LOG_DEBUG Reports information useful for debugging
- -f |--foreground
- Run in the foreground, and prevent dissociation from the parent
- shell when running standalone. --foreground is most useful for
- debugging when you start and stop the daemon frequently.
- -i |--inetd
- Run socks5 through inetd.
- -n val |--nchildren val
- Set the maximum number of child processes that can exist at one
- time to val.
- socks5 presets the maximum number of child processes to 64. Use
- this option to lower that preset value. You cannot set the maximum
- number of child processes to a value that exceeds the operating
- system limit.
- socks5 ignores this value in threaded mode, and it is not useful
- in oneshot and inetd mode.
- -o |--oneshot
- Service one request in the foreground, turn on debugging, send
- output to stderr, and exit. The oneshot option is most useful
- during debugging.
- -p |--prefork
- Run socks5 as a preforking process. Also include the --nchildren
- option to control the number of forked processes.
- -s |--stderr
- Log all output to stderr only, instead of the syslog. Logging
- output to stderr is most useful in debug mode, which generates
- more output.
- -t | --threaded
- Run socks5 in threaded mode, if your operating system supports
- POSIX threads and you built socks5 with the --with-threads
- option. When threaded mode is not available, socks5 prints a
- warning and reverts to standalone mode.
- -v |--version
- Print a message containing the current socks5 version to stderr.
- ENVIRONMENT
- Environment variables control how socks5 operates. Set them in the
- environment, or include them in the configuration file. When the envi-
- ronment and the configuration file contain conflicting settings, the
- configuration files has precedence over the environment.
- To set the variables in the configuration file, use the syntax:
- set variable [value]
- SOCKS5_BINDINTFC host:port
- host:port specifies the host and the port number on that host on
- which socks5 runs, instead of the default port. When you omit
- host, socks5 uses 0.0.0.0 as the host value.
- SOCKS5_CONFFILE filename
- filename specifies the configuration file. In most systems, the
- default is /etc/socks5.conf. Set the environment before running
- socks5. With multiple socks5 daemons, use a different configura-
- tion file for each daemon.
- SOCKS5_DEMAND_IDENT
- Cause No authentication to fail when the client fails to respond
- to an ident request. Use SOCKS5_DEMAND_IDENT to ensure that each
- connection has an associated username.
- SOCKS5_ENCRYPT
- Request that the next socks5 process encrypt the data, if possi-
- ble. SOCKS5_ENCRYPT is only useful when the socks5 build
- includes GSS-API authentication.
- SOCKS5_FORCE_ENCRYPT
- Force the client to encrypt the data when the authentication
- method supports encryption.
- SOCKS5_IDENTFILE filename
- filename specifies the filename that stores the ident informa-
- tion. In most systems, the default is /tmp/socks5.ident.
- SOCKS5_IDENTFILE is most useful with multiple socks5 daemons.
- SOCKS5_MAXCHILD val
- val specifies the maximum number of child processes that can
- exist at one time.
- socks5 presets the maximum number of child processes to 64. Use
- this environment variable to lower that preset value. You cannot
- set the maximum number of child processes to a value that exceeds
- the operating system limit.
- socks5 ignores this value in threaded mode, and it is not useful
- in oneshot and inetd mode.
- SOCKS5_NOIDENT
- Omit ident requests. Use SOCKS5_NOIDENT to reduce timeouts when
- you are certain clients are not running identd's.
- SOCKS5_NOINTCHK
- Request that the next socks5 process perform the proxy request
- without integrity checking. SOCKS5_NOINTCHK is only useful when
- the socks5 build includes GSS-API authentication.
- SOCKS5_NONETMASKCHECK
- Instruct the daemon to disregard checking the server host's net-
- mask. By default, the daemon checks the netmask and connects
- directly to hosts on the same subnet before consulting the con-
- figuration file.
- SOCKS5_REVERSEMAP
- Always attempt to map addresses to hostnames. By default,
- socks5 only maps when hostnames or domains are used in the con-
- figuration file. When set, the log file will contain hostnames,
- but degrades performance.
- SOCKS5_SERVICENAME
- Always map port numbers to service names. By default, socks5
- only maps when services are used in the configuration file.
- When set, the log file will contain services, but degrades per-
- formance.
- SOCKS5_PASSWD [password]
- Specify the password for Username/Password authentication when
- the socks5 daemon connects to another socks server.
- SOCKS5_PIDFILE filename
- Specify the filename that stores the socks5 process ID. socks5
- stores the PID in /tmp/socks5.pid. You can run socks5 on a port
- other than the default port by including the --bindintfc command
- line option or setting the SOCKS5_BINDINTFC environment vari-
- able. When you run socks5 on a port other than the default port,
- socks5 stores the PID in /tmp/socks5.pid-port. Use SOCKS5_PID-
- FILE to specify an alternate filename.
- SOCKS5_PWDFILE filename
- Specify the password file. In most systems, the default is
- /etc/socks5.passwd.
- SOCKS5_TIMEOUT minutes
- minutes specifies the maximum number of minutes the connection
- remains idle. After minutes number of minutes, socks5 discon-
- nects. When you omit SOCKS5_TIMEOUT, socks5 disconnects after
- the connection is idle for 15 minutes.
- SOCKS5_UDPPORTRANGE port1-port2
- Specify a range of UDP ports. port1-port2 specifies a range of
- UDP ports that socks5 uses to send UDP packets.
- SOCKS5_USER [user id]
- Specify the username for Username/Password authentication when
- the socks5 daemon connects to another socks server.
- SOCKS5_V4SUPPORT
- By default, socks5 will only accept SOCKS V5 protocol (rfc 1928)
- requests. When set, socks5 will accept SOCKS V4 protocol
- requests.
- REQUEST FOR COMMENTS
- RFC1928
- SOCKS protocol Version 5
- RFC1929
- Username/Password Authentication for SOCKS V5
- RFC1961
- GSS-API Authentication Method for SOCKS Version 5
- SEE ALSO
- socks5.conf(5)
- AUTHOR
- NWSL SOCKS5 Development Team
- Send comments to socks5-comments@socks.nec.com
- 7 Oct 1998 socks5(1)
而且直接配置文件是效率最高的,通过其它驱动效率都相对较低,BDB
这个测试不太准确,看官方的测试结果:http://bind-dlz.sourceforg
为什么使用BDB时QPS这么低? 我在bind版本基本相似的环境中测试的
It is quite useful and interesting too.
VIRT 的上限是64G,也就是36位, cat /proc/cpuinfo的结果是:addre
昨天要准备用线程重写webbench,试验了下Fedora Linux 2.6.35.14
不明白您的具体的意思是什么?
已经发送到你QQ邮箱