用Delphi設計自己的代理伺服器
筆者在撰寫一個上網計費軟體時,涉及如何對區域網路中各工作站上網計費問題。一般來講,這些工作站透過代理伺服器上網,而採用現成的代理伺服器軟體時,由於代理伺服器軟體是封閉的系統,很難編寫程式來取得即時的上網計時資訊。因此,考慮是否能編寫自己的代理伺服器,一方面解決群組上網,另一方面又解決上網的計費問題呢?
經過實驗性編程,終於圓滿地解決了這個問題。現寫出來,與各位同行分享。
1、 思路
目前流行的瀏覽器的系統選項中有一個參數,即“通過代理伺服器連接”,經過編程測
試,當當局網域網路中一台工作站指定了該屬性,再發出Internet請求時,請求資料將傳送到所指定的代理伺服器上,以下為請求資料包範例:
GET http://home.microsoft.com/intl/cn/ HTTP/1.0
Accept: */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)
Host: home.microsoft.com
PRoxy-Connection: Keep-Alive
其中第一個行為目標URL及相關方法、協議,「Host」行指定了目標主機的位址。
由此知道了代理服務的過程:接收被代理端的請求、連接真正的主機、接收主機回傳的資料、將接收資料傳送到被代理端。
為此可編寫一個簡單的程序,完成上述網路通訊重定向問題。
用Delphi設計時,選用ServerSocket作為與被代理工作站通訊的套接字控件,選用ClientSocket動態數組作為與遠端主機通訊的套接字控件。
編程時應解決的一個重要問題是多重連線處理問題,為了加快代理服務的速度和被代理端的回應速度,套接字控制項的屬性應設為非阻塞型;各通訊會話與套接字動態綁定,用套接字的SocketHandle屬性值決定屬於哪一個會話。
通訊的銜接過程如下圖所示:
代理伺服器
Serversocket
(1) 接收
被代理端發送遠端主機
(6) (2) (5)
Browser ClientSocket (4) Web Server
接收
發送(3)
(1)、被代理端瀏覽器發出Web請求,代理伺服器的Serversocket接收到請求。
(2)、代理伺服器程式自動建立一個ClientSocket,並設定主機位址、連接埠等屬性,然後連接遠端主機。
(3)、遠端連通後激發發送事件,將Serversocket接收的Web請求封包傳送到遠端主機。
(4)、當遠端主機回傳頁面資料時,激發ClientSocket的讀取事件,讀取頁面資料。
(5)、代理伺服器程式根據綁定資訊決定屬於ServerSocket控制項中的哪一個Socket應該將從主機接收的頁面資訊傳送到被代理端。
(6)、ServerSocket中的對應Socket將頁面資料傳送到被代理端。
2、 程式編寫
使用Delphi設計以上通訊過程非常簡單,主要是ServerSocket、ClientSocket的相關事
件驅動程式的程式編寫。以下給出作者編寫的實驗用代理伺服器介面與原始程式清單,內含簡要功能說明:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, ScktComp, TrayIcon, Menus, StdCtrls;
type
session_record=record
Used: boolean; {會話記錄是否可用}
SS_Handle: integer; {代理伺服器套接字句柄}
CSocket: TClientSocket; {用於連接遠端的套接字}
Lookingup: boolean; {是否正在尋找伺服器}
LookupTime: integer; {查找伺服器時間}
Request: boolean; {是否有請求}
request_str: string; {請求資料塊}
client_connected: boolean; {客戶機線上標誌}
remote_connected: boolean; {遠端伺服器連線標誌}
end;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
ClientSocket1: TClientSocket;
Timer2: TTimer;
TrayIcon1: TTrayIcon;
PopupMenu1: TPopupMenu;
N11: TMenuItem;
N21: TMenuItem;
N1: TMenuItem;
N01: TMenuItem;
Memo1: TMemo;
Edit1: TEdit;
Label1: TLabel;
Timer1: TTimer;
procedure Timer2Timer(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N21Click(Sender: TObject);
procedure N01Click(Sender: TObject);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ClientSocket1Write(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
procedure AppException(Sender: TObject; E: Exception);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
Service_Enabled: boolean; {代理服務是否開啟}
session: array of session_record; {會話數組}
sessions: integer; {會話數}
LookUpTimeOut: integer; {連線逾時值}
InvalidRequests: integer; {無效請求數}
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
file://系統啟動定時器,啟動窗顯示完成後,縮小到System Tray…
procedure TForm1.Timer2Timer(Sender: TObject);
begin
timer2.Enabled:=false; {關閉定時器}
sessions:=0; {會話數=0}
application.OnException := AppException; {為了屏蔽代理伺服器出現的例外}
invalidRequests:=0; {0錯誤}
LookUpTimeOut:=60000; {超時值=1分鐘}
timer1.Enabled:=true; {開啟定時器}
n11.Enabled:=false; {開啟服務選單項目失效}
n21.Enabled:=true; {關閉服務選單項目有效}
serversocket1.Port:=988; {代理伺服器連接埠=988}
serversocket1.Active:=true; {開啟服務}
form1.hide; {隱藏介面,縮小到System Tray上}
end;
file://開啟服務選單項目…
procedure TForm1.N11Click(Sender: TObject);
begin
serversocket1.Active:=true; {開啟服務}
end;
file://停止服務選單項目…
procedure TForm1.N21Click(Sender: TObject);
begin
serversocket1.Active:=false; {停止服務}
N11.Enabled:=True;
N21.Enabled:=False;
Service_Enabled:=false; {標誌清除}
end;
file://主視窗建立…
procedure TForm1.FormCreate(Sender: TObject);
begin
Service_Enabled:=false;
timer2.Enabled:=true; {視窗建立時,開啟計時器}
end;
file://視窗關閉時…
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
timer1.Enabled:=false; {關閉定時器}
if Service_Enabled then
serversocket1.Active:=false; {退出程式時關閉服務}
end;
file://退出程式按鈕…
procedure TForm1.N01Click(Sender: TObject);
begin
form1.Close; {退出程式}
end;
file://開啟代理服務後…
procedure TForm1.ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
begin
Service_Enabled:=true; {置正在服務標誌}
N11.Enabled:=false;
N21.Enabled:=true;
end;
file://被代理端連接到代理伺服器後,建立一個會話,並與套接字綁定…
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j: integer;
begin
j:=-1;
for i:=1 to sessions do {查找是否有空白項}
if not session[i-1].Used and not session[i-1].CSocket.active then
begin
j:=i-1; {有,分配它}
session[j].Used:=true; {置為在用}
break;
end
else
if not session[i-1].Used and session[i-1].CSocket.active then
session[i-1].CSocket.active:=false;
if j=-1 then
begin {無,新增一個}
j:=sessions;
inc(sessions);
setlength(session,sessions);
session[j].Used:=true; {置為在用}
session[j].CSocket:=TClientSocket.Create(nil);
session[j].CSocket.OnConnect:=ClientSocket1Connect;
session[j].CSocket.OnDisconnect:=ClientSocket1Disconnect;
session[j].CSocket.OnError:=ClientSocket1Error;
session[j].CSocket.OnRead:=ClientSocket1Read;
session[j].CSocket.OnWrite:=ClientSocket1Write;
session[j].Lookingup:=false;
end;
session[j].SS_Handle:=socket.socketHandle; {保存句柄,實現綁定}
session[j].Request:=false; {無請求}
session[j].client_connected:=true; {客戶機已連線}
session[j].remote_connected:=false; {遠端未連線}
edit1.text:=inttostr(sessions);
end;
file://被代理端斷開時…
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j,k: integer;
begin
for i:=1 to sessions do
if (session[i-1].SS_Handle=socket.SocketHandle) and session[i-1].Used then
begin
session[i-1].client_connected:=false; {客戶機未連線}
if session[i-1].remote_connected then
session[i-1].CSocket.active:=false {假如遠端尚連接,斷開它}
else
session[i-1].Used:=false; {假如兩者都斷開,則置釋放資源標誌}
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do {統計會話陣列尾端有幾個未用項}
begin
if session[ji].Used then
break;
inc(k);
end;
if k>0 then {修正會話數組,釋放尾部未用項}
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.text:=inttostr(sessions);
end;
file://通訊錯誤出現時…
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var
i,j,k: integer;
begin
for i:=1 to sessions do
if (session[i-1].SS_Handle=socket.SocketHandle) and session[i-1].Used then
begin
session[i-1].client_connected:=false; {客戶機未連線}
if session[i-1].remote_connected then
session[i-1].CSocket.active:=false {假如遠端尚連接,斷開它}
else
session[i-1].Used:=false; {假如兩者都斷開,則置釋放資源標誌}
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do
begin
if session[ji].Used then
break;
inc(k);
end;
if k>0 then
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.text:=inttostr(sessions);
errorcode:=0;
end;
file://被代理端傳送來頁面請求時…
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
tmp,line,host: string;
i,j,port: integer;
begin
for i:=1 to sessions do {判斷是哪一個會話}
if session[i-1].Used and (session[i-1].SS_Handle=socket.sockethandle) then
begin
session[i-1].request_str:=socket.ReceiveText; {儲存請求資料}
tmp:=session[i-1].request_str; {存放到暫存變數}
memo1.lines.add(tmp);
j:=pos(char(13)+char(10),tmp); {一行標誌}
while j>0 do {逐行掃描請求文本,查找主機位址}
begin
line:=copy(tmp,1,j-1); {取一行}
delete(tmp,1,j+1); {刪除一行}
j:=pos('Host',line); {主機位址標誌}
if j>0 then
begin
delete(line,1,j+5); {刪除前面的無效字元}
j:=pos(':',line);
if j>0 then
begin
host:=copy(line,1,j-1);
delete(line,1,j);
try
port:=strtoint(line);
except
port:=80;
end;
end
else
begin
host:=trim(line); {取得主機位址}
port:=80;
end;
if not session[i-1].remote_connected then {假如遠徵尚未連接}
begin
session[i-1].Request:=true; {置請求資料就緒標誌}
session[i-1].CSocket.host:=host; {設定遠端主機位址}
session[i-1].CSocket.port:=port; {設定埠}
session[i-1].CSocket.active:=true; {連接遠端主機}
session[i-1].Lookingup:=true; {置標誌}
session[i-1].LookupTime:=0; {從0開始計時}
end
else
{假如遠端已連接,直接發送請求}
session[i-1].CSocket.socket.sendtext(session[i-1].request_str);
break; {停止掃描請求文字}
end;
j:=pos(char(13)+char(10),tmp); {指向下一行}
end;
break; {停止循環}
end;
end;
file://當連接遠端主機成功時…
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var
i: integer;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.socket.sockethandle=socket.SocketHandle) and session[i-1].Used then
begin
session[i-1].CSocket.tag:=socket.SocketHandle;
session[i-1].remote_connected:=true; {置遠端主機已連通標誌}
session[i-1].Lookingup:=false; {清標誌}
break;
end;
end;
file://當遠端主機斷開時…
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j,k: integer;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.tag=socket.SocketHandle) and session[i-1].Used then
begin
session[i-1].remote_connected:=false; {置為未連接}
if not session[i-1].client_connected then
session[i-1].Used:=false {假如客戶機已斷開,則置釋放資源標誌}
else
for k:=1 to serversocket1.Socket.ActiveConnections do
if (serversocket1.Socket.Connections[k-1].SocketHandle=session[i-1].SS_Handle) and session[i-1].used then
begin
serversocket1.Socket.Connections[k-1].Close;
break;
end;
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do
begin
if session[ji].Used then
break;
inc(k);
end;
if k>0 then {修正會話數組}
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.text:=inttostr(sessions);
end;
file://與遠端主機通訊發生錯誤時…
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var
i,j,k: integer;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.tag=socket.SocketHandle) and session[i-1].Used then
begin
socket.close;
session[i-1].remote_connected:=false; {置為未連接}
if not session[i-1].client_connected then
session[i-1].Used:=false {假如客戶機已斷開,則置釋放資源標誌}
else
for k:=1 to serversocket1.Socket.ActiveConnections do
if (serversocket1.Socket.Connections[k-1].SocketHandle=session[i-1].SS_Handle) and session[i-1].used then
begin
serversocket1.Socket.Connections[k-1].Close;
break;
end;
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do
begin
if session[ji].Used then
break;
inc(k);
end;
errorcode:=0;
if k>0 then {修正會話數組}
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.text:=inttostr(sessions);
end;
file://向遠端主機發送頁面請求…
procedure TForm1.ClientSocket1Write(Sender: TObject;
Socket: TCustomWinSocket);
var
i: integer;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.tag=socket.SocketHandle) and session[i-1].Used then
begin
if session[i-1].Request then
begin
socket.SendText(session[i-1].request_str); {假如有請求,發送}
session[i-1].Request:=false; {清標誌}
end;
break;
end;
end;
file://遠端主機發來頁面資料時…
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j: integer;
rec_bytes: integer; {傳回的資料塊長度}
rec_Buffer: array[0..2047] of char; {傳回的資料塊緩衝區}
begin
for i:=1 to sessions do
if (session[i-1].CSocket.tag=socket.SocketHandle) and session[i-1].Used then
begin
rec_bytes:=socket.ReceiveBuf(rec_buffer,2048); {接收資料}
for j:=1 to serversocket1.Socket.ActiveConnections do
if serversocket1.Socket.Connections[j-1].SocketHandle=session[i-1].SS_Handle then
begin
serversocket1.Socket.Connections[j-1].SendBuf(rec_buffer,rec_bytes); {傳送資料}
break;
end;
break;
end;
end;
file://「頁面找不到」等錯誤訊息出現時…
procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
inc(invalidrequests);
end;
file://尋找遠端主機定時…
procedure TForm1.Timer1Timer(Sender: TObject);
var
i,j: integer;
begin
for i:=1 to sessions do
if session[i-1].Used and session[i-1].Lookingup then {假如正在連接}
begin
inc(session[i-1].LookupTime);
if session[i-1].LookupTime>lookuptimeout then {假如超時}
begin
session[i-1].Lookingup:=false;
session[i-1].CSocket.active:=false; {停止查找}
for j:=1 to serversocket1.Socket.ActiveConnections do
if serversocket1.Socket.Connections[j-1].SocketHandle=session[i-1].SS_Handle then
begin
serversocket1.Socket.Connections[j-1].Close; {斷開客戶機}
break;
end;
end;
end;
end;
end.
3、 後記
由於這種設計想法僅在被代理端和遠端主機之間增加了一個重定向功能,被代理端原
有的快取技術等特點均保留,因此效率較高。經過測試,利用1個33.6K的Modem上網時,三到十個被代理工作站同時上網,仍有較好的反應速度。由於被代理工作站和代理伺服器工作站之間的連線一般是透過高速鏈路,瓶頸主要出現在代理伺服器的上網方式。
透過上述方法,作者成功開發了一套完善的代理伺服器軟體並與機房計費系統完全集
成,實現了利用一台工作站完成上網代理、上網計費、用機計費等功能。 有程式設計經驗的朋友完全可以另行增加代理伺服器功能,如設定禁止存取網站、統計客戶流量、Web存取清單等等。
作者 Blog: http://blog.csdn.net/BExpress/