HOME 生活记录运维/编程
作者/来源:yixinu.com
栏目:运维/编程
日期:2013-01-26 07:04:58

服务器环境:

 系统:centos-6.2-i386、网卡:eth0 192.168.1.72 连接外网,eth1 191.2.1.1 连接内网

 服务器名称:ser3.com


实现功能:

 可监控客户端所有的上网记录

 可在WEB端完成这个几服务的所有配置

 可禁止QQ聊天

 根据MAC地址配置客户端,可随时禁止或开启某一台客户端上网

 禁止访问不允许访问的网站

 禁止访问不允许访问的服务器



1、清空之前的规则

iptables -F

iptables -F -t nat


2、开启服务器路由功能

echo 1 > /proc/sys/net/ipv4/ip_forward


3、添加iptables规则,完整规则表如下:

# Generated by iptables-save v1.4.7 on Mon Jan 28 11:52:37 2013
*filter
:INPUT ACCEPT [32:30154]
:FORWARD ACCEPT
:OUTPUT ACCEPT [78:35969]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -p icmp -j ACCEPT 
-A INPUT -i lo -j ACCEPT 
-A INPUT -p tcp -m tcp --dport 22 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -p tcp -m tcp --dport 21 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -p tcp -m tcp --dport 20 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -s 191.2.0.0/16 -p tcp -m tcp --dport 53 -j ACCEPT 
-A INPUT -s 191.2.0.0/16 -p tcp -m tcp --dport 3128 -j ACCEPT 
-A INPUT -s 191.2.0.0/16 -p tcp -m tcp --dport 80 -j ACCEPT 
-A FORWARD -s 191.2.1.3/32 -p all -m mac --mac-source 00:1E:90:E4:35:7C -j ACCEPT 
-A FORWARD -s 191.2.0.0/16 -p all -j DROP
COMMIT
# Completed on Mon Jan 28 11:52:37 2013
# Generated by iptables-save v1.4.7 on Mon Jan 28 11:52:37 2013
*nat
:PREROUTING ACCEPT [102:7702]
:POSTROUTING ACCEPT [257:17946]
:OUTPUT ACCEPT [257:17946]
-A PREROUTING -i eth1 -s 191.2.0.0/16 -p tcp -m multiport --dports 80,21,20,443,70,210,1025:65535,280,488,591,777 -j REDIRECT --to-ports 3128 
-A PREROUTING -i eth1 -s 191.2.0.0/16 -p udp -m multiport --dports 80,21,20,443,70,210,1025:65535,280,488,591,777 -j REDIRECT --to-ports 3128 
#-A PREROUTING -s 191.2.0.0/16 -d 191.2.1.1 -p tcp -m multiport --dports 80,21,20,443,70,210,1025:65535,280,488,591,777 -j DNAT --to-destination 192.168.1.72:3128
#-A PREROUTING -s 191.2.0.0/16 -d 191.2.1.1 -p udp -m multiport --dports 80,21,20,443,70,210,1025:65535,280,488,591,777 -j DNAT --to-destination 192.168.1.72:3128
############
#-A POSTROUTING -s 192.168.1.72 -d 191.2.0.0/16 -p tcp -m multiport --dports 80,21,20,443,70,210,1025:65535,280,488,591,777 -j SNAT --to-source 191.2.1.1:3128
#-A POSTROUTING -s 192.168.1.72 -d 191.2.0.0/16 -p udp -m multiport --dports 80,21,20,443,70,210,1025:65535,280,488,591,777 -j SNAT --to-source 191.2.1.1:3128
############
-A POSTROUTING -s 191.2.0.0/16 -o eth0 -j MASQUERADE 
COMMIT
# Completed on Mon Jan 28 11:52:37 2013

说明:443号端口是QQ2013的通讯端口,如果禁掉,QQ会登录不上,或者马上掉线。


4、安装DNS并开启


5、安装并配置squid

下载squid源码 squid-2.6.STABLE23.tar.gz  

http://www.squid-cache.org

在 /usr/local/src 目录下解压源码并编译  ./configure --enable-icmp --enable-htcp ,安装后文件全部都在/usr/local/squid 目录下


vim /etc/squid/squid.conf

完整配置文件如下:

acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl Safe_ports port 80      # http
acl Safe_ports port 21      # ftp
acl Safe_ports port 443     # https
acl Safe_ports port 70      # gopher
acl Safe_ports port 210     # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280     # http-mgmt
acl Safe_ports port 488     # gss-http
acl Safe_ports port 591     # filemaker
acl Safe_ports port 777     # multiling http
acl CONNECT method CONNECT
#######
acl disableurl1 dstdomain .taobao.com
acl disableurl2 url_regex tao
acl disableip src 191.2.1.3
acl localserver dstdomain .ser3.com
         
http_access deny disableurl1
http_access deny disableurl2
#http_access deny disableip
no_cache deny localserver
         
http_access allow manager localhost
http_access deny CONNECT !SSL_ports
http_access deny !Safe_ports
         
acl clients src 191.2.0.0/16
http_access allow clients
         
acl all src 0.0.0.0/0.0.0.0
http_access deny all
icp_access allow all
##########
http_port 3128 transparent
         
emulate_httpd_log off
strip_query_terms off
         
cache_mem 200 MB 
cache_swap_high  85
cache_swap_low  70 
cache_log /dev/null
cache_dir ufs  /usr/local/squid/var/spool/ 30000 16 256
pid_filename /usr/local/squid/var/logs/squid.pid
#useragent_log /usr/local/squid/var/logs/useragent.log
ident_timeout 1 seconds
visible_hostname ser3.com
         
hierarchy_stoplist cgi-bin ?
cache_access_log /usr/local/squid/var/logs/access.log 
#squid
acl QUERY urlpath_regex cgi-bin ?
cache deny QUERY
refresh_pattern ^ftp:       1440    20% 10080
refresh_pattern ^gopher:    1440    0%  1440
refresh_pattern .       0   20% 4320
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
coredump_dir /usr/local/squid/var/cache
error_directory /usr/local/squid/share/errors/Simplify_Chinese
         
cache_effective_user squid
cache_effective_group squid



17行为拒绝客户端访问的网址,前面的点一定不能忘记写,要不然就不是模糊匹配了,而是精确匹配。

19和22行是禁止某个IP联网

20和25行是不对本地服务器的网站缓存

64行是设置错误信息的模板页面目录



重新加载squid的配置文件

/etc/init.d/squid reload


###############到这里已经完成简单的配置,到客户端配置网关和DNS为191.2.1.1就OK了###########


6、bash程序将squid的访问日志导入mysql数据据

   1)创建数据库和数据表

   数据库名称:squiddog

   表名称:squid_access

   表结构如下:

----------------------------------------------------------
字段      类型      空   默认  注释
id      int(11) 否        
datetime    datetime    否       日期时间 
responsetime    varchar(512)    否       响应时间 
userip      varchar(256)    否       客户端IP 
statuscode  varchar(512)    否       状态码 
datesize    varchar(512)    否       数据大小 
getmod      varchar(512)    否       获取方式 
url     varchar(1024)   否       链接 
clientIdentity  varchar(512)    否       客户端身份 
httpstatuscode  varchar(512)    否       http状态码 
contenttype varchar(512)    否       内容类型

   2)编写bash程序

#!/usr/bin/env bash
   
LOGSPATH="/usr/local/squid/var/logs/"
   
mkdir $LOGSPATH/`date -d now +"%Y-%m"` > /dev/null 2>&1
FILENAME=access_`date -d now +"%Y-%m-%d"`
mv $LOGSPATH/access.log $LOGSPATH/`date -d now +"%Y-%m"`/$FILENAME.log
sed 's/    / /g' $LOGSPATH/`date -d now +"%Y-%m"`/$FILENAME.log  > /tmp/squidlog_tmp1.txt
sed 's/ /	/g' /tmp/squidlog_tmp1.txt  > $LOGSPATH/`date -d now +"%Y-%m"`/$FILENAME.c.log
   
   
kill -HUP `cat /var/run/squid.pid`



7、开发自己的管理系统

   1)配置域名

   添加本地域名,admin1.ser3.com ,为了便于管理

   在/etc/hosts 文件中添加以下代码

191.2.1.1   www.ser3.com admin1.ser3.com admin2.ser3.com

   这里要问:为什么不直接在DNS服务中添加ser3.com的域名?

   答:之前我也以为应该这样,但是不行,始终都访问不了网站,我不知道为什么,整了两个多小时也不行,我想 这应该和DNS的访问顺序有关,因为这台服务器可以访问internet,DNS是从根服务器开始查找,因为国际域名服务器上有ser3.com 这个域名,所以我这里配置了根本没用。


   2)配置apache服务器

   在/etc/httpd/conf/httpd.conf文件中添加编辑以下代码

NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName www.ser3.com
    ErrorLog logs/www.ser3.com-error_log
    CustomLog logs/www.ser3.com-access_log common
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /var/www/adminsquid
    ServerName admin1.ser3.com
    <Directory "/var/www/adminsquid">
        AuthType Basic
        AuthName "Restricted Files"
        AuthUserFile /etc/httpd/conf/password
        Require user squidadmin
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog logs/admin1.ser3.com-error_log
    CustomLog logs/admin1.ser3.com-access_log common
</VirtualHost>

   说明:12到15行是配置了密码认证,为了安全。

   设置admin1.ser3.com的访问密码

htpasswd -c /etc/httpd/conf/password netadmin
New password: netadmin@2013
Re-type new password: netadmin@2013
Adding password for user netadmin


   fsa



分享到:

Copyright © 2013-2014 yixinu.com 湘ICP备14004402号

QQ:316686606  Email: 316686606@qq.com