使用套接字创建代理

时间:2015-10-23 22:47:21

标签: sockets delphi proxy

我尝试编写一个代理,将流量从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。

1 个答案:

答案 0 :(得分:1)

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

每次OnClientRead从Web浏览器接收原始数据时,都会触发TServerSocket事件。请勿在收到的每个数据块上连接和断开TClientSocket。您应该在服务器的TClientSocket事件中关联OnClientConnect,在服务器的OnClientDisconnect事件中关闭该连接,然后使用ReceiveBuf() / SendBuf()活动中的OnClientRead代替ReceiveText() / SendText()

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

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

您还需要考虑到Web浏览器可以并且可能会创建与您的代理的多个连接。因此,您的代理需要多个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
  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;
  end;

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

procedure TMyBuffer.Send(Data: Pointer; DataLen);
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;
  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
  FMyBuffer.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船只预装了德里,并有一个TIdMappedPortTCP组件,完全符合您的要求。 Indy 9和10也有一个TIdHTTPProxyServer组件(Delphi 6随Indy 8一起提供,但是Indy的新版本仍然支持Delphi 6)。