关于Socket5代理服务器.. (100分)

  • 主题发起人 主题发起人 边尘浪子
  • 开始时间 开始时间

边尘浪子

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个简单的代理....
客户机在第二次协商时 发来了目标地址和目标端口...
代理程序根据这个目标地址和目标端口 连接上目标主机....
可是 我发现 TForm1.ClientSocket1Connect() 响应起来需要很长时间.....

-------------------主要代码如下-----------------------
type
session_record=record
Used :boolean; {启用标志}
SS_Handle :integer; {句柄}
CSocket :TClientSocket; {连接远程服务器的Socket}
Request :boolean; {是否有请求}
request_str :string; {请求数据块}
client_connect :boolean; {客户机连接标志}
remote_connect :boolean; {远程服务器连接标志}
end;

session:session_record;

procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Lines.Clear;
serversocket1.Active:=True;
end;

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
if not session.Used then
begin
session.Used:=True;
session.CSocket:=TClientSocket.Create(nil);
session.CSocket.OnConnect:=ClientSocket1Connect;
session.CSocket.OnDisconnect:=ClientSocket1Disconnect;
session.CSocket.OnError:=ClientSocket1Error;
session.CSocket.OnRead:=ClientSocket1Read;
session.SS_Handle:=socket.SocketHandle;
session.Request:=false;
session.client_connect:=true;
session.remote_connect:=false;
end;
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var
buf:array [0..1] of byte;
begin
Memo1.Lines.Add('[系统]连接到了远程主机....');
session.CSocket.Tag:=socket.SocketHandle;
session.remote_connect:=true; {置 远程主机连通标志}
buf[0]:=$05; {版本标志位}
buf[1]:=$00; {连通标志位}
serversocket1.Socket.Connections[0].SendBuf(buf,2);
end;

procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('[系统]远程主机断开了....');
end;

procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
session.client_connect:=false; {置客户机连接标志}
session.remote_connect:=false; {置远程主机连接标志}
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
recv_string:string;
begin
recv_string:=Socket.ReceiveText();
serversocket1.Socket.Connections[0].SendText(recv_string);
Memo1.Lines.Add('[主机]'+recv_string);
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
i,recv_bytes:integer;
recv_message:String;
target_add:String;
recv_buffer:array [0..2047] of byte;
begin
if not session.remote_connect then
begin
recv_bytes:=Socket.ReceiveBuf(recv_buffer,2048);
//-------------------------------------------------------
recv_message:='[系统]接收到数据(hex):';{初始化 recv_message}
for i:=0 to recv_bytes-1 do
begin
recv_message:=recv_message+inttoHex(recv_buffer,2);
end;
Memo1.Lines.Add(recv_message);
//-------------------------------------------------------
if (recv_bytes=3) and (recv_buffer[0]=$05) and (recv_buffer[1]=$01) then
begin //收到了第一步请求
session.Request:=True;
recv_buffer[1]:=$00;
//返回客户机0500...
serversocket1.Socket.Connections[0].SendBuf(recv_buffer,2);
Memo1.Lines.Add('[系统]程序返回客户机:0500');
end;
if (recv_bytes>3) and (not session.remote_connect) then
begin //收到了第二步请求
target_add:= inttostr(recv_buffer[4]);
target_add:=target_add+'.'+inttostr(recv_buffer[5]);
target_add:=target_add+'.'+inttostr(recv_buffer[6]);
target_add:=target_add+'.'+inttostr(recv_buffer[7]);
session.CSocket.Host:=target_add;
session.CSocket.Port:=recv_buffer[8]*256+recv_buffer[9];
Memo1.lines.add('[系统]目标地址:'+session.CSocket.Host);
Memo1.lines.add('[系统]目标端口:'+inttostr(session.CSocket.Port));
session.CSocket.Active:=True;
end;
end
else
if session.remote_connect then
begin
recv_message:=Socket.ReceiveText();
Memo1.Lines.Add('[客户机]'+recv_message);
session.CSocket.Socket.SendText(recv_message);
end;
end;

procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('客户机断开了....');
end;

----------------------------------------------------------------------
 
后退
顶部