TProxy

Fuente: source/classes/tproxy.prg

TProxy is FiveWin's multi-protocol TCP proxy server. It listens on a local port, accepts incoming connections, and forwards traffic to a remote backend server. The proxy works transparently for any TCP-based protocol (HTTP, POP3, SMTP, FTP, etc.) by creating paired socket connections between the client and the backend server and relaying data bidirectionally.

Architecture

flowchart LR subgraph "Client" A[Client App] end subgraph "TProxy" B[Listening Socket
nPort] C[Client Socket] D[Backend Socket] end subgraph "Backend" E[Remote Server
cServerIP] end A -->|"connects to
local port"| B B --> C C -->|"relays data"| D D -->|"forwards to"| E E -->|"response"| D D -->|"relays back"| C C -->|"response"| A

Key DATA Members

DATATypeDefaultDescription
oSocketObjectMain TSocket listening for incoming connections
cAddressCharacterLocal IP address the proxy listens on (auto-detected)
cServerIPCharacter"194.224.203.2"Remote backend server IP address to forward traffic to
nPortNumeric110Local port the proxy listens on
lDebugLogicalEnable debug logging to cLogFile
cLogFileCharacter"proxy.log"Path to the debug log file

Methods

MethodDescription
New( nPort, cServerIP )Create a proxy instance. Creates the listening TSocket and sets up the accept callback. Defaults to port 110 (POP3). Resolves hostnames to IP addresses automatically.
Activate()Start listening for incoming connections. Delegates to oSocket:Listen().
End()Stop the proxy and release the listening socket. Delegates to oSocket:End().
OnAccept()Internal: Called when a new client connects. Creates a paired client socket and backend socket, sets up bidirectional data relay with read/close callbacks.
OnRead( oSocket, oServer )Internal: Called when data arrives from the client. Forwards the data to the backend server.

Example: POP3 Proxy

#include "FiveWin.ch"

function Main()

   local oProxy, oWnd

   DEFINE WINDOW oWnd TITLE "POP3 Proxy - Port 8080"

   // Proxy on port 8080, forwarding to the mail server
   oProxy := TProxy():New( 8080, "mail.example.com" )
   oProxy:lDebug := .T.
   oProxy:Activate()

   ACTIVATE WINDOW oWnd VALID ( oProxy:End(), .T. )

return nil

When a client connects to port 8080, TProxy creates a backend connection to mail.example.com:8080 (same port) and relays all data bidirectionally. With lDebug := .T., all traffic is logged to proxy.log.

Notes

Ver También