gpt4 book ai didi

sockets - 使用套接字创建代理

转载 作者:行者123 更新时间:2023-12-02 06:32:39 25 4
gpt4 key购买 nike

我尝试编写一个代理,将流量从 127.0.0.1:80 传递到 192.168.69.1:80 。这是我的代码:

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
s: string;
begin
s := Socket.ReceiveText;

clientsocket1.Open;
clientsocket1.Socket.SendText(s);
s := clientsocket1.Socket.ReceiveText;

socket.SendText(s);
socket.Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ClientSocket1.Host := '192.168.69.1';
ClientSocket1.Port := 80;
ClientSocket1.ClientType := ctBlocking;
ClientSocket1.Active := true;

ServerSocket1.OnClientRead := ServerSocket1ClientRead;
ServerSocket1.Port := 80;
ServerSocket1.Active := true;
end;

ServerSocket 部分有效。我收到 HTTP 请求,并且可以将内容发送回浏览器。但是,ClientSocket 部分失败。我收到 1 个 HTML 页面,并且每个进一步的请求都以空字符串 (ERR_EMPTY_RESPONSE) 结尾。

我做错了什么?

对于这个小工具,我只想使用 Delphi 6 中的基本组件,而不使用第三方 VCL。

最佳答案

这是使用 TServerSocket 实现直通代理的错误方法。

每次 TServerSocket 从 Web 浏览器接收原始数据时,都会触发 OnClientRead 事件。不要在接收到的每个数据 block 上连接和断开 TClientSocket。您应该在服务器的 OnClientConnect 事件中连接 TClientSocket,在服务器的 OnClientDisconnect 事件中关闭该连接,然后使用 ReceiveBuf( )/SendBuf()OnClientRead 事件中,而不是 ReceiveText()/SendText() .

您还需要考虑到网络服务器可能需要多个 OnRead 事件才能发回任何数据。在非阻塞模式下使用 TClientSocket 及其自己的 OnRead 事件处理程序将数据从 Web 服务器发送到 Web 浏览器。

您还必须考虑到,在非阻塞模式下,SendBuf() 可能会失败并出现 WSAEWOULDBLOCK 错误。如果发生这种情况,您必须缓存当前正在发送的数据,并等待调用套接字的 OnWrite 事件,然后才能再次向该套接字发送数据。

您还需要考虑到网络浏览器可以而且很可能会创建与您的代理的多个连接。因此,您的代理将需要多个 TClientSocket 连接,每个网络浏览器连接一个。

至少尝试类似这样的方法(实现正确的 HTTP 代理需要比这更多的逻辑):

type
TMyBuffer = class(TMemoryStream)
private
FSocket: TCustomWinSocket;
public
constructor Create(ASocket: TCustomWinSocket);
procedure Send(Data: Pointer; DataLen: Integer);
procedure Flush;
end;

TMyServerSocketData = class;
TMyClientSocket = class(TClientSocket)
private
FBuffer: TMyBuffer;
FServerSocketData: TMyServerSocketData;
protected
procedure Event(Socket: TCustomWinSocket; SocketEvent: TSocketEvent); override;
procedure Error(Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); override;
public
constructor Create(AServerSocketData: TMyServerSocketData); reintroduce;
destructor Destroy; override;
procedure Send(Data: Pointer; DataLen: Integer);
property ServerSocketData: TMyServerSocketData read FServerSocketData;
end;

TMyServerSocketData = class
private
FBuffer: TMyBuffer;
FClientSocket: TMyClientSocket;
FServerSocket: TCustomWinSocket;
public
constructor Create(AServerSocket: TCustomWinSocket);
destructor Destroy; override;
procedure Send(Data: Pointer; DataLen: Integer);
procedure FlushBuffer;
property ClientSocket: TMyClientSocket read FClientSocket;
property ServerSocket: TCustomWinSocket read FServerSocket;
end;

constructor TMyBuffer.Create(ASocket: TCustomWinSocket);
begin
inherited Create;
FSocket := ASocket;
end;

procedure TMyBuffer.Send(Data: Pointer; DataLen: Integer);
var
PData: PByte;
begin
PData := PByte(Data);
if Self.Size = 0 then
begin
repeat
NumSent := FSocket.SendBuf(PData^, DataLen);
if NumSent < 1 then Break;
Inc(PData, NumSent);
Dec(DataLen, NumSent);
until DataLen = 0;
end;
if DataLen > 0 then
begin
Self.Seek(0, soEnd);
Self.WriteBuffer(PData^, DataLen);
end;
end;

procedure TMyBuffer.Flush;
var
PData: PByte;
DataLen, NumSent: Integer;
begin
if Self.Size = 0 then Exit;
Self.Position := 0;
PData := PByte(Self.Memory);
DataLen := Self.Size;
repeat
NumSent := FSocket.SendBuf(PData^, DataLen);
if NumSent < 1 then Break;
Inc(PData, NumSent);
Dec(DataLen, NumSent);
Self.Seek(NumSent, soCurrent);
until DataLen = 0;
if Self.Position = 0 then Exit;
if Self.Position = Self.Size then
Self.Clear
else begin
Move(PData, Self.Memory, DataLen);
Self.Size := DataLen;
end;
end;

constructor TMyClientSocket.Create(AServerSocketData: TMyServerSocketData);
begin
inherited Create(nil);
ClientType := ctNonBlocking;
FBuffer := TMyBuffer.Create(Socket);
FServerSocketData := AServerSocketData;
end;

destructor TMyClientSocket.Destroy;
begin
FBuffer.Free;
inherited;
end;

procedure TMyClientSocket.Send(Data: Pointer; DataLen: Integer);
begin
FBuffer.Send(Data, DataLen);
end;

procedure TMyClientSocket.Event(Socket: TCustomWinSocket; SocketEvent: TSocketEvent);
var
Buf: array[0..1023] of byte;
BufLen, NumSent: Integer;
PBuf: PByte;
begin
case SocketEvent of
seRead:
begin
if not FServerSocketData.ServerSocket.Connected then Exit;
BufLen := Socket.ReceiveBuf(Buf[0], SizeOf(Buf));
if BufLen > 0 then
FServerSocketData.Send(@Buf[0], BufLen);
end;
seWrite:
FBuffer.Flush;
else
inherited;
end;
end;

procedure TMyClientSocket.Error(Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
FServerSocketData.ServerSocket.Close;
inherited;
end;

constructor TMyServerSocketData.Create(AServerSocket: TCustomWinSocket);
begin
inherited Create;
FBuffer := TMyBuffer.Create(AServerSocket);
FClientSocket := TMyClientSocket.Create(Self);
FServerSocket := AServerSocket;
end;

destructor TMyServerSocketData.Destroy;
begin
FBuffer.Free;
FClientSocket.Free;
inherited;
end;

procedure TMyServerSocketData.Send(Data: Pointer; DataLen: Integer);
begin
FBuffer.Send(Data, DataLen);
end;

procedure TMyServerSocketData.FlushBuffer;
begin
FBuffer.Flush;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.OnClientConnect := ServerSocket1ClientConnect;
ServerSocket1.OnClientDisconnect := ServerSocket1ClientDisconnect;
ServerSocket1.OnClientRead := ServerSocket1ClientRead;
ServerSocket1.Port := 80;
ServerSocket1.ServerType := stNonBlocking;
ServerSocket1.Active := true;
end;

Procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
Data: TMyServerSocketData;
begin
Data := TMyServerSocketData.Create(Socket);
try
Data.ClientSocket.Host := '192.168.69.1';
Data.ClientSocket.Port := 80;
Data.ClientSocket.Open;
except
Data.Free;
raise;
end;
Socket.Data := Data;
end;

Procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
TMyServerSocketData(Socket.Data).Free;
end;

Procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
ClientSocket: TMyClientSocket;
Buf: array[0..1023] of byte;
BufLen: Integer;
begin
ClientSocket := TMyServerSocketData(Socket.Data).ClientSocket;
if not ClientSocket.Socket.Connected then Exit;
BufLen := Socket.ReceiveBuf(Buf[0], SizeOf(Buf));
if BufLen > 0 then
ClientSocket.Send(@Buf[0], BufLen);
end;

Procedure TForm1.ServerSocket1ClientWrite(Sender: TObject;
Socket: TCustomWinSocket);
begin
TMyServerSocketData(Socket.Data).FlushBuffer;
end;

话虽如此,您确实应该重新考虑使用第三方组件。 Indy 随 Delhi 一起预装,并具有一个 TIdMappedPortTCP 组件,该组件完全可以完成您想要完成的任务。 Indy 9 和 10 也有一个 TIdHTTPProxyServer 组件(Delphi 6 随 Indy 8 一起提供,但较新版本的 Indy 仍然支持 Delphi 6)。

关于sockets - 使用套接字创建代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33312504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com