<br>用Delphi设计自己的代理服务器 <br> <br> 笔者在编写一个上网计费软件时,涉及到如何对局域网中各工作站上网计费问题。一般来讲,这些工作站通过代理服务器上网,而采用现成的代理服务器软件时,由于代理服务器软件是封闭的系统,很难编写程序获取实时的上网计时信息。因此,考虑是否能编写自己的代理服务器,一方面解决群体上网,另一方面又解决上网的计费问题呢? <br> 经过实验性编程,终于圆满地解决了该问题。现写出来,与各位同行分享。 <br> <br>1、思路 <br>当前流行的浏览器的系统选项中有一个参数,即“通过代理服务器连接”,经过编程测 <br>试,当局域网中一台工作站指定了该属性,再发出Internet请求时,请求数据将发送到所指定的代理服务器上,以下为请求数据包示例: <br> GEThttp://home.microsoft.com/intl/cn/HTTP/1.0 <br> Accept:*/* <br> Accept-Language:zh-cn <br> Accept-Encoding:gzip,deflate <br> User-Agent:Mozilla/4.0(compatible;MSIE5.0;WindowsNT) <br> Host:home.microsoft.com <br> Proxy-Connection:Keep-Alive <br>其中第一行为目标URL及相关方法、协议,“Host”行指定了目标主机的地址。 <br>由此知道了代理服务的过程:接收被代理端的请求、连接真正的主机、接收主机返回的数据、将接收数据发送到被代理端。 <br>为此可编写一个简单的程序,完成上述网络通信重定向问题。 <br>用Delphi设计时,选用ServerSocket作为与被代理工作站通信的套接字控件,选用ClientSocket动态数组作为与远程主机通信的套接字控件。 <br>编程时应解决的一个重要问题是多重连接处理问题,为了加快代理服务的速度和被代理端的响应速度,套接字控件的属性应设为非阻塞型;各通信会话与套接字动态绑定,用套接字的SocketHandle属性值确定属于哪一个会话。 <br>通信的衔接过程如下图所示: <br> <br> 代理服务器 <br> <br> Serversocket <br> (1)接收 <br> 被代理端发送远程主机 <br> (6)(2)(5) <br> BrowserClientSocket(4)WebServer <br> 接收 <br> 发送(3) <br> <br> <br>(1)、被代理端浏览器发出Web请求,代理服务器的Serversocket接收到请求。 <br>(2)、代理服务器程序自动创建一个ClientSocket,并设置主机地址、端口等属性,然后连接远程主机。 <br>(3)、远程连通后激发发送事件,将Serversocket接收到的Web请求数据包发送到远程主机。 <br>(4)、当远程主机返回页面数据时,激发ClientSocket的读事件,读取页面数据。 <br>(5)、代理服务器程序根据绑定信息确定属于ServerSocket控件中的哪一个Socket应该将从主机接收的页面信息发送到被代理端。 <br>(6)、ServerSocket中的对应Socket将页面数据发送到被代理端。 <br> <br>2、程序编写 <br>使用Delphi设计以上通信过程非常简单,主要是ServerSocket、ClientSocket的相关事 <br>件驱动程序的程序编写。下面给出作者编写的实验用代理服务器界面与源程序清单,内含简要功能说明: <br> <br>unitmain; <br> <br>interface <br> <br>uses <br> Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs, <br> ExtCtrls,ScktComp,TrayIcon,Menus,StdCtrls; <br> <br>type <br> session_record=record <br> Used:boolean; <br> SS_Handle:integer; <br> CSocket:TClientSocket; <br> Lookingup:boolean; <br> LookupTime:integer; <br> Request:boolean; <br> request_str:string; <br> client_connected:boolean; <br> remote_connected:boolean; <br>end; <br> <br>type <br> TForm1=class(TForm) <br> ServerSocket1:TServerSocket; <br> ClientSocket1:TClientSocket; <br> Timer2:TTimer; <br> TrayIcon1:TTrayIcon; <br> PopupMenu1:TPopupMenu; <br> N11:TMenuItem; <br> N21:TMenuItem; <br> N1:TMenuItem; <br> N01:TMenuItem; <br> Memo1:TMemo; <br> Edit1:TEdit; <br> Label1:TLabel; <br> Timer1:TTimer; <br> procedureTimer2Timer(Sender:TObject); <br> procedureN11Click(Sender:TObject); <br> procedureFormCreate(Sender:TObject); <br> procedureFormClose(Sender:TObject;varAction:TCloseAction); <br> procedureN21Click(Sender:TObject); <br> procedureN01Click(Sender:TObject); <br> procedureServerSocket1ClientConnect(Sender:TObject; <br> Socket:TCustomWinSocket); <br> procedureServerSocket1ClientDisconnect(Sender:TObject; <br> Socket:TCustomWinSocket); <br> procedureServerSocket1ClientError(Sender:TObject; <br> Socket:TCustomWinSocket;ErrorEvent:TErrorEvent; <br> varErrorCode:Integer); <br> procedureServerSocket1ClientRead(Sender:TObject; <br> Socket:TCustomWinSocket); <br> procedureClientSocket1Connect(Sender:TObject; <br> Socket:TCustomWinSocket); <br> procedureClientSocket1Disconnect(Sender:TObject; <br> Socket:TCustomWinSocket); <br> procedureClientSocket1Error(Sender:TObject;Socket:TCustomWinSocket; <br> ErrorEvent:TErrorEvent;varErrorCode:Integer); <br> procedureClientSocket1Write(Sender:TObject; <br> Socket:TCustomWinSocket); <br> procedureClientSocket1Read(Sender:TObject;Socket:TCustomWinSocket); <br> procedureServerSocket1Listen(Sender:TObject; <br> Socket:TCustomWinSocket); <br> procedureAppException(Sender:TObject;E:Exception); <br> procedureTimer1Timer(Sender:TObject); <br> ** <br> {**declarations} <br> public <br> Service_Enabled:boolean; <br> session:arrayofsession_record; <br> sessions:integer; <br> LookUpTimeOut:integer; <br> InvalidRequests:integer; <br> end; <br> <br>var <br> Form1:TForm1; <br> <br>implementation <br> <br>{$R*.DFM} <br> <br>file://系统启动定时器,启动窗显示完成后,缩小到SystemTray… <br>procedureTForm1.Timer2Timer(Sender:TObject); <br>begin <br> timer2.Enabled:=false; <br> sessions:=0; <br> Application.OnException:=AppException; <br> invalidRequests:=0; <br> LookUpTimeOut:=60000; <br> timer1.Enabled:=true; <br> n11.Enabled:=false; <br> n21.Enabled:=true; <br> serversocket1.Port:=988; <br> serversocket1.Active:=true; <br> form1.hide;{隐藏界面,缩小到SystemTray上} <br>end; <br> <br>file://开启服务菜单项… <br>procedureTForm1.N11Click(Sender:TObject); <br>begin <br> serversocket1.Active:=true; <br>end; <br> <br> <br>file://停止服务菜单项… <br>procedureTForm1.N21Click(Sender:TObject); <br>begin <br> serversocket1.Active:=false; <br> N11.Enabled:=True; <br> N21.Enabled:=False; <br> Service_Enabled:=false; <br>end; <br> <br> <br>file://主窗口建立… <br>procedureTForm1.FormCreate(Sender:TObject); <br>begin <br> Service_Enabled:=false; <br> timer2.Enabled:=true; <br>end; <br> <br>file://窗口关闭时… <br>procedureTForm1.FormClose(Sender:TObject;varAction:TCloseAction); <br>begin <br> timer1.Enabled:=false; <br> ifService_Enabledthen <br> serversocket1.Active:=false; <br>end; <br> <br>file://退出程序按钮… <br>procedureTForm1.N01Click(Sender:TObject); <br>begin <br> form1.Close; <br>end; <br> <br>file://开启代理服务后… <br>procedureTForm1.ServerSocket1Listen(Sender:TObject; <br> Socket:TCustomWinSocket); <br>begin <br> Service_Enabled:=true; <br> N11.Enabled:=false; <br> N21.Enabled:=true; <br>end; <br> <br>file://被代理端连接到代理服务器后,建立一个会话,并与套接字绑定… <br>procedureTForm1.ServerSocket1ClientConnect(Sender:TObject; <br> Socket:TCustomWinSocket); <br>var <br>i,j:integer; <br>begin <br> j:=-1; <br> fori:=1tosessionsdo <br> ifnotsession[i-1].Usedandnotsession[i-1].CSocket.activethen <br> begin <br> j:=i-1; <br> session[j].Used:=true; <br> break; <br> end <br> else <br> ifnotsession[i-1].Usedandsession[i-1].CSocket.activethen <br> session[i-1].CSocket.active:=false; <br> ifj=-1then <br> begin <br> j:=sessions; <br> inc(sessions); <br> setlength(session,sessions); <br> session[j].Used:=true; <br> session[j].CSocket:=TClientSocket.Create(nil); <br> session[j].CSocket.OnConnect:=ClientSocket1Connect; <br> session[j].CSocket.OnDisconnect:=ClientSocket1Disconnect; <br> session[j].CSocket.OnError:=ClientSocket1Error; <br> session[j].CSocket.OnRead:=ClientSocket1Read; <br> session[j].CSocket.OnWrite:=ClientSocket1Write; <br> session[j].Lookingup:=false; <br> end; <br> session[j].SS_Handle:=socket.socketHandle; <br> session[j].Request:=false; <br> session[j].client_connected:=true; <br> session[j].remote_connected:=false; <br> edit1.text:=inttostr(sessions); <br>end; <br> <br>file://被代理端断开时… <br>procedureTForm1.ServerSocket1ClientDisconnect(Sender:TObject; <br> Socket:TCustomWinSocket); <br>var <br>i,j,k:integer; <br>begin <br> fori:=1tosessionsdo <br> if(session[i-1].SS_Handle=socket.SocketHandle)andsession[i-1].Usedthen <br> begin <br> session[i-1].client_connected:=false; <br> ifsession[i-1].remote_connectedthen <br> session[i-1].CSocket.active:=false <br> else <br> session[i-1].Used:=false; <br> break; <br> end; <br> j:=sessions; <br> k:=0; <br> fori:=1tojdo <br> begin <br> ifsession[j-i].Usedthen <br> break; <br> inc(k); <br> end; <br> ifk%26gt;0then <br> begin <br> sessions:=sessions-k; <br> setlength(session,sessions); <br> end; <br> edit1.text:=inttostr(sessions); <br>end; <br> <br>file://通信错误出现时… <br>procedureTForm1.ServerSocket1ClientError(Sender:TObject; <br> Socket:TCustomWinSocket;ErrorEvent:TErrorEvent; <br> varErrorCode:Integer); <br>var <br>i,j,k:integer; <br>begin <br> fori:=1tosessionsdo <br> if(session[i-1].SS_Handle=socket.SocketHandle)andsession[i-1].Usedthen <br> begin <br> session[i-1].client_connected:=false; <br> ifsession[i-1].remote_connectedthen <br> session[i-1].CSocket.active:=false <br> else <br> session[i-1].Used:=false; <br> break; <br> end; <br> j:=sessions; <br> k:=0; <br> fori:=1tojdo <br> begin <br> ifsession[j-i].Usedthen <br> break; <br> inc(k); <br> end; <br> ifk%26gt;0then <br> begin <br> sessions:=sessions-k; <br> setlength(session,sessions); <br> end; <br> edit1.text:=inttostr(sessions); <br> errorcode:=0; <br>end; <br> <br>file://被代理端发送来页面请求时… <br>procedureTForm1.ServerSocket1ClientRead(Sender:TObject; <br> Socket:TCustomWinSocket); <br>var <br>tmp,line,host:string; <br>i,j,port:integer; <br>begin <br> fori:=1tosessionsdo <br> ifsession[i-1].Usedand(session[i-1].SS_Handle=socket.sockethandle)then <br> begin <br> session[i-1].request_str:=socket.ReceiveText; <br> tmp:=session[i-1].request_str; <br> memo1.lines.add(tmp); <br> j:=pos(char(13)+char(10),tmp); <br> whilej%26gt;0do <br> begin <br> line:=copy(tmp,1,j-1); <br> delete(tmp,1,j+1); <br> j:=pos('Host',line); <br> ifj%26gt;0then <br> begin <br> delete(line,1,j+5); <br> j:=pos(':',line); <br> ifj%26gt;0then <br> begin <br> host:=copy(line,1,j-1); <br> delete(line,1,j); <br> try <br> port:=strtoint(line); <br> except <br> port:=80; <br> end; <br> end <br> else <br> begin <br> host:=trim(line); <br> port:=80; <br> end; <br> ifnotsession[i-1].remote_connectedthen <br> begin <br> session[i-1].Request:=true;
|