自己写的Socks5代理的源码,还有些问题没有解决,请大家指教!(2)

quharry 2005-01-03 05:55:07
要实现功能Http、Socks5(无验证)代理。
现出现如下问题,1、Http代理可用,但不能实现多线程。
2、Socks5代理IPV4可用,域名方式下载时不能获取文件大小,出现实况转播情况。
由于文件太大,分成了两张贴子!

=========================================================================================

procedure TransLate(Sock:integer);
Function SearchSubstr(Buf:array of Byte;Len:integer):Pchar;
var
Pstr,TarStr,PosStr,ResStr:Pchar;
url:string;
Pos1,Len1:integer;
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);
url:='http://'+Result;
if TarGetPort<>80 then url:=url+':'+inttostr(TarGetPort);
Pos1:=Pos(url,Pchar(@Buf[0]));
Len1:=Length(url);
// Delete(pchar(@buf[0]),Pos1,Len1);
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);
// ShowMessage(TarGetHost);
if TarGethost=Nil then
begin
CloseSocket(Sock);
SetLength(Buf,0);
Exit;
end;
SendBuf(Buf,Len,sock);
SetLength(Buf,0);
end;

procedure THExeThread.Execute;
begin
TransLate(ClientSock);
end;

Destructor THExeThread.Destroy;
begin
inherited Destroy;
end;

end.

//======================Socks5 Proxy 监听线程=====================
unit SLisThread;

interface

uses
classes,winsock,windows,SExeThread;

type
TSLisThread=Class(TThread)
private
SLisSocket:TSocket;
SLisPort:integer;
protected
procedure Execute;override;
public
constructor CreateSLisThread(Port:Integer);
Destructor Destroy;override;
end;

implementation

uses main;

constructor TSLisThread.CreateSLisThread(Port:Integer);
begin
Inherited create(True);
SLisPort:=Port;
FreeonTerminate:=True;
Resume; //开始线程
end;

procedure TSLisThread.Execute;
var
AcceptSocket:TSocket;
TempThread:TSExeThread;
sA:Sockaddr_in;
sALen:integer;
MyBuf:array[0..256] of Byte;
ReIP:string;
begin
SLisSocket:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP);
if SLisSocket=INVALID_SOCKET then Exit;
sA.sin_family:=PF_INET;
sA.sin_port:=htons(SLisPort);
sA.sin_addr.S_addr:=INADDR_ANY;
sALen:=Sizeof(sA);
//绑定
if bind(SLisSocket,sA,sALen)=SOCKET_ERROR then
begin
closesocket(SLisSocket);
Exit;
end;
//开始监听
Listen(SLisSocket,5);
while not Terminated do
begin
AcceptSocket:=accept(SLisSocket,@sA,@sALen); //有客户端连接
GetPeerName(SLisSocket,sA,sALen); //获取客户端信息
ReIP:=inet_ntoa(sA.sin_addr);
if ReIP='162.168.2.40' then //进行客户端IP过滤
begin
if AcceptSocket=INVALID_SOCKET then Continue;
recv(AcceptSocket,mybuf,257,0);
if mybuf[0]=$05 then
begin
if (mybuf[1]=$01) and (mybuf[2]=$00) then
begin
mybuf[0]:=$05;mybuf[1]:=$00;
end else begin
mybuf[0]:=$05;mybuf[1]:=$FF;
end;
send(AcceptSocket,mybuf,2,0);
TempThread:=TSExeThread.CreateExeThread(AcceptSocket); //建立Socks5执行线程
if TempThread<>Nil then TempThread.resume else Closesocket(AcceptSocket);
end;
end else begin
Form1.Memo1.Lines.Add('非法客户端!,连接被拒绝!');
// closesocket(AcceptSocket);
end;
end;
closesocket(SLisSocket);
end;

Destructor TSLisThread.Destroy;
begin
if SLisSocket<> INVALID_SOCKET then closesocket(SLisSocket);
postmessage(form1.Handle,WM_SOCKTHREADDONMSG,self.ThreadID,0); //向主线条发送自定义关闭消息
inherited Destroy;
end;

end.
...全文
133 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jim138 2005-01-05
  • 打赏
  • 举报
回复
DING :)))
Kshape 2005-01-05
  • 打赏
  • 举报
回复
帮你顶
Kevin_Lmx 2005-01-04
  • 打赏
  • 举报
回复
MK,回头看看。
^_^
quharry 2005-01-04
  • 打赏
  • 举报
回复
大家帮帮忙吧!
高手都去哪儿啦?
自己顶一下!
jackie168 2005-01-03
  • 打赏
  • 举报
回复
2楼
S.F. 2005-01-03
  • 打赏
  • 举报
回复
1楼

1,594

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧