我找了一个HttpProxy.pas,但是不会调用(100分)

  • 主题发起人 主题发起人 cn0754
  • 开始时间 开始时间
C

cn0754

Unregistered / Unconfirmed
GUEST, unregistred user!
这两天想找个API写的HTTP PROXY服务器.但是还真不好找,..基本上都是VCL的HTTP代理服务
不过真的找了很久,发现这个东西,
============================
unit HttpProxy;


interface

uses
Classes,WinSock,SysUtils;


const
ListenPort=6363;

DEFAULTHTTPPORT=80;

MaxChangeLen=$2000;


type
THttpProxy = class(TThread)
private
{ Private declarations }
LisSocket:TSocket;

protected
procedure Execute;
override;

end;


implementation
var
TargetPort:integer;

Targethost:PChar;

{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,

Synchronize(UpdateCaption);


and UpdateCaption could look like,

procedure HttpProxy.UpdateCaption;

begin

Form1.Caption := 'Updated in a thread';

end;
}

{ HttpProxy }
Procedure SendBuf(Buf:array of Byte;Len:Integer;Sock:TSocket);

var
Client:TSocket;

cA:Sockaddr_in;

HostInfo:PHostEnt;

HostAddr:integer;

begin

Client:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP);

if Client=INVALID_SOCKET then

begin

CloseSocket(Sock);

Exit;

end;

cA.sin_family:=PF_INET;

cA.sin_port:=htons(TargetPort);

HostAddr:=inet_addr(TargetHost);

if (HostAddr=-1) then

begin

HostInfo:=GetHostByName(TargetHost);

if HostInfo<>Nil then

cA.sin_addr:=(Pinaddr(HostInfo.h_addr_list^))^
else
begin

StrDisPose(TargetHost);

CloseSocket(Sock);

Exit;

end;

end else
cA.sin_addr.S_addr:=HostAddr;

StrDisPose(TargetHost);

if connect(Client,cA,SizeOf(cA))<>0 then

begin

closesocket(Client);

CloseSocket(Sock);

Exit;

end else
begin

Send(Client,Buf[0],Len,0);

try
Len:=Recv(Client,buf[0],MaxChangeLen,0);

While Len>0 do

begin

Send(Sock,Buf[0],Len,0);

Len:=Recv(Client,Buf[0],MaxChangeLen,0);

end;

finally
CloseSocket(Client);

CloseSocket(Sock);

end;

end;

end;


Procedure TransLate(Sock:integer);

Function SearchSubStr(Buf:array of byte;Len:integer):Pchar;

var
PStr,TarStr,PosStr,ResStr:Pchar;

begin

Pstr:=StrAlloc(Len+1);

PStr:=StrLCopy(PStr,PChar(@Buf[0]),Len);

TarStr:=StrPos(PStr,#13#10+'Host:');

if TarStr=Nil then

begin

StrDispose(PStr);

Result:=Nil;

end else
begin

TarStr:=Tarstr+8;

PosStr:=StrPos(TarStr,#13#10);

ResStr:=StrAlloc(PosStr-TarStr+1);

ResStr:=StrLCopy(ResStr,TarStr,PosStr-TarStr);

TarStr:=StrSCan(ResStr,':');

if TarStr<>Nil then

begin

TarStr:=TarStr+1;

TargetPort:=StrToInt(StrPas(TarStr));

Result:=StrLCopy(ResStr,ResStr,TarStr-ResStr-1);

end else
begin

Result:=ResStr;

StrDispose(PStr);

end;

end;

end;

var
Buf:array of Byte;

Len:integer;

begin

SetLength(Buf,MaxChangeLen);

Len:=recv(Sock,Buf[0],MaxChangeLen,0);

if Len<=0 then

begin

CloseSocket(Sock);

SetLength(Buf,0);

Exit;

end;

TargetPort:=DEFAULTHTTPPORT;

TargetHost:=SearchSubStr(Buf,Len);

if TargetHost=Nil then

begin

CloseSocket(Sock);

SetLength(Buf,0);

Exit;

end;

Sendbuf(Buf,Len,Sock);

SetLength(Buf,0);

end;


procedure THttpProxy.Execute;

var
AcceptSocket:TSocket;

sA:Sockaddr_in;

sALen:integer;

begin

if Terminated then
Resume;

{ Place thread code here }
LisSocket:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP);

if LisSocket=INVALID_SOCKET then
exit;

sA.sin_family:=PF_INET;

sA.sin_port:=htons(ListenPort);

sA.sin_addr.S_addr:=INADDR_ANY;

sALen:=Sizeof(sA);


if Bind(LisSocket,sA,sALen)=SOCKET_ERROR then

begin

Closesocket(LisSocket);

Exit;

end;


Listen(LisSocket,5);

while not Terminated do

begin

AcceptSocket:=Accept(LisSocket,@sA,@sALen);

if AcceptSocket=INVALID_SOCKET then
Continue;

TransLate(AcceptSocket);

end;

end;


end.

=============================================================
想请问大家这个HTTPPROXY单元完整吗.如果想调用的话大概需要怎么做,谢谢
最好能给个直接开代理的例子.谢谢
因为自己学习DELPHI不久.麻烦各位了
 
直接调用Execute就可以建立代理服务器,监听端口6363,默认端口80.
 

Similar threads

后退
顶部