TProxy
Fonte: 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
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
| DATA | Type | Default | Description |
|---|---|---|---|
oSocket | Object | Main TSocket listening for incoming connections | |
cAddress | Character | Local IP address the proxy listens on (auto-detected) | |
cServerIP | Character | "194.224.203.2" | Remote backend server IP address to forward traffic to |
nPort | Numeric | 110 | Local port the proxy listens on |
lDebug | Logical | Enable debug logging to cLogFile | |
cLogFile | Character | "proxy.log" | Path to the debug log file |
Methods
| Method | Description |
|---|---|
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
- TProxy is protocol-agnostic — it works with any TCP-based protocol since it simply relays raw bytes between client and server.
- The
New()method accepts hostnames and resolves them to IPs usingGetHostByName(). - Each incoming connection creates two TSocket objects: one for the client and one for the backend. Both are cleaned up when the connection closes.
- Setting
lDebug := .T.logs all client and server data to the file specified bycLogFile. - The proxy port defaults to 110 (POP3) if not specified.