当前位置:web 服务器 > Apache >>

Linux下搭建基于MYSQL认证,Apache+php管理的Squid代理系统

几个月前自己随便搞了一个Squid+mysql的代理认证,apache+php+mysql的代理帐号管理系统,经验拿来和大家一起分享.呵呵.

  Squid的Mysql的认证并不是很流行,原因并不是他的性能方面的原因,而是简单的代理认证只需要Ncsa之类简单的密码文件操作,而复杂的多功能的有LDAP数据库,但是我选择了Mysql, 因为我看中了它的网络操作的方便性。Apache+php+mysql正是最流行的经典组合.

  我们的代理认证系统最终选择了Squid+Mysql+Php+Apache其中

  Squid+mysql组成了认证的效验和认证以后的代理服务。

  Mysql+Php+Apache组成了前台的代理用户管理系统。这样就能通过web操作来完成用户的管理的操作。

  1 Squid端认证代码的编写

  用c语言编写一段Squid能够加载的认证程序,通过这个认证程序来访问Mysql数据库,完成认证的过程。

  /*

  * mysql_auth.c

  *

  * Copyright 1998 Frank Liu (frank@ctcqnx4.ctc.cummins.com)

  * Distributed under the GPL

  *

  * 26 Sep 1999, version 2:

  * 1. fixed a bug where A_TABLE is defined but never used.

  * (thanks to luciano.ghezzi@linux.it)

  * 2. now you can choose to use either clear text password or

  * encrypted password in the MySQL table.

  * 13 Nov 1998, version 1:

  * initial release

  * Needs to be compiled/linked with MySQL libs.

  * Assuming MySQL header files are installed in /usr/local/mysql/include

  * and MySQL libs in /usr/local/mysql/lib

  *

  *

  * gcc -O2 -Wall -o mysql_auth mysql_auth.c -L /usr/lib/mysql -lmysqlclient

  *

  * Dec, 2002.

  * Modfied by Jiang.

  * Add support for crypt password.

  */

  #include <stdio.h>

  #include <stdlib.h>

  #include <string.h>

  #include "mysql.h"

  /* comment out next line if you use clear text password in MySQL DB */

  /*#define ENCRYPTED_PASS*/

  /* can use NULL for localhost, current user, or no password */

  #define DBHOST "localhost"

  #define DBUSER "root"

  #define DB "www"

  #define DBPASSWORD "*******"

  /* table for the user database for the squid authentication,

  column names for auth username and auth password */

  #define A_TABLE "user1"

  #define A_USERNAME "username"

  #define A_PASSWORD "passwd"

  #define BUFSIZE 256

  void main(int argc, char *argv[])

  {

  char buf[BUFSIZE], qbuf[BUFSIZE];

  char *p;

  MYSQL mysql,*sock;

  MYSQL_RES *res;

  /* make standard output line buffered */

  if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)

  return;

  while (1) {

  if (fgets(buf, BUFSIZE, stdin) == NULL)

  break;

  if ((p = strchr(buf, '\\n')) != NULL)

  *p = '\\0'; /* strip \\n */

  if ((p = strchr(buf, ' ')) == NULL) {

  (void) printf("ERR\\n");

  continue;

  }

  *p++ = '\\0';

  /* buf is username and p is password now */

  if (!(sock = mysql_connect(&mysql, DBHOST, DBUSER, DBPASSWORD)))

  {

  /* couldn't connect to database server */

  (void) printf("ERR\\n");

  continue;

  }

  if (mysql_select_db(sock, DB))

  {

  /* couldn't use the database */

  (void) printf("ERR\\n");

  mysql_close(sock);

  continue;

  }

  sprintf(qbuf, "select " A_USERNAME " from " A_TABLE " where "

  A_USERNAME "='%s' and " A_PASSWORD

  #ifdef ENCRYPTED_PASS

  "=password('%s')", buf, p);

  #else

  "='%s'", buf, p);

  #endif

  if(mysql_query(sock,qbuf) || !(res=mysql_store_result(sock)))

  {

  /* query failed */

  (void) printf("ERR\\n");

  mysql_close(sock);

  continue;

  }

  if ( res->row_count !=0 )

  (void) printf("OK\\n");

  else

  (void) printf("ERR\\n");

  mysql_free_result(res);

  mysql_close(sock);

  }

  exit(0);

  }

  代码核心部分就是对Mysql中一张表的访问从而确定客户提供的用户名和密码是否存在于表中。

  用gcc编译连接

  gcc -I /usr/local/mysql/include -O -o mysql_auth mysql_auth.c \\

  -L /usr/local/mysql/lib -lmysqlclient -lm

  这样就生成了mysql_auth这个认证程序。代码中的注释很关键,可能对你很有帮助.

  2 Squid配置的修改

  auth_param basic program /var/squid/bin/mysql_auth //认证程序的地址

  auth_param basic children 5 //初始启动认证个数

  auth_param basic realm FreeTown Proxy Caching Domain

  auth_param basic credentialsttl 1 hours

  acl authed proxy_auth REQUIRED //代理需要认证

  authenticate_ip_ttl 2 hours

  acl ip_unico max_user_ip –s//每个账号只能使用一个ip

  这些配置都是摸索出来了的,用着还可以

  3 Apache+php前端的代理帐号管理程序的编写

  其实这个过程就是写一个php的网页来使用户和管理员来完成对mysql中存储的那张表的读写过程.

  比如帐号的创建,密码修改,帐号删除等等操作都可以通过这个网页实现.

  我是php新新手,代码写的很垃圾.大家想一想知道知道里面的操作怎么实现了.

  这里要注意一点是,如果在上面那个msql认证的c代码中

  /* comment out next line if you use clear text password in MySQL DB */

  /*#define ENCRYPTED_PASS*/

  如果你注释掉了#define ENCRYPTED_PASS

  那么密码存放的时候就应该使用明文的.

  总结

  这样便能简单方便的实现web操作来做到管理squid的代理认证工作。

  工作也能做到稳定高效。我们做好整个系统后在学校里发布测试,最终用户数量为3000多。每天的代理服务器的连接数300多万。一天的数据流量是40G.。

  发布后基本上这个认证系统没出过问题,事实证明这个系统是稳定高效的,并且和其它流行的认证管理方法相比有自己的特色。

  欢迎大家和我交流,提出意见,本文来自:http://www.xiaoyaxiao.com/2692.html
发表您的意见和建议!
Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,