Hello experts,
I am writing a local proxy. I am getting responce from the server to the socket in the proxy and also I have send the response to the client socket. But it loads the website sometimes and sometime it just buffering or show can’t reach the website. what am I missing in this proxy application.
In this I have redirected the traffic from WFP at FWPM_LAYER_ALE_AUTH_CONNECT_REDIRECT_V4 layer to my proxy.
In the proxy I have created a listening socket, bind it, listen on it and accept connection in a loop. And called the following function in loop.
`
#define CONTEXT_SIZE 2048
#define RECORDS_SIZE 4096
#define BUFFER_SIZE 20480
void proxyClientToServer(SOCKET clientSock)
{
// Retrieve the redirect context
BYTE redirectContext[CONTEXT_SIZE];
BYTE redirectRecords[RECORDS_SIZE];
DWORD bytesReturned;
memset(redirectContext, 0, CONTEXT_SIZE);
WSAIoctl(clientSock, SIO_QUERY_WFP_CONNECTION_REDIRECT_CONTEXT, NULL, 0, redirectContext, CONTEXT_SIZE, &bytesReturned, NULL, NULL);
// Extract original destination IP and port from the context
SOCKADDR_IN originalDestAddr;
memcpy(&originalDestAddr.sin_port, redirectContext + sizeof(USHORT), sizeof(USHORT));
memcpy(&originalDestAddr.sin_addr, redirectContext + sizeof(ULONG), sizeof(ULONG));
// Retrieve the existing redirect records
WSAIoctl(clientSock, SIO_QUERY_WFP_CONNECTION_REDIRECT_RECORDS, NULL, 0, redirectRecords, RECORDS_SIZE, &bytesReturned, NULL, NULL);
// Create a socket for the server
SOCKET serverSock = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); // socket(AF_INET, SOCK_STREAM, 0);
// Set the redirect records on the socket
WSAIoctl(serverSock, SIO_SET_WFP_CONNECTION_REDIRECT_RECORDS, redirectRecords, sizeof(redirectRecords), NULL, 0, &bytesReturned, NULL, NULL);
// Connect to the server
SOCKADDR_IN serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr = originalDestAddr.sin_addr;
serverAddr.sin_port = originalDestAddr.sin_port;
connect(serverSock, (SOCKADDR*)&serverAddr, sizeof(serverAddr));
int time = 5000;
setsockopt(clientSock, SOL_SOCKET, SO_RCVTIMEO, (char*)&time, sizeof(time)) ;
// Proxy data from client to server
char buffer[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = recv(clientSock, buffer, BUFFER_SIZE, 0)) > 0)
{
printf("Received request to client:\n");
send(serverSock, buffer, bytesRead, 0); //Send reques to the server
// Receive the response from the server
char buff[BUFFER_SIZE];
int bytesReceived = recv(serverSock, buff, BUFFER_SIZE, 0);
if (bytesReceived > 0)
{
// Forward the response back to the client
if (send(clientSock, buff, bytesReceived, 0) == SOCKET_ERROR)
{
printf("Failed to send response to client: %ld\n", WSAGetLastError());
break;
}
}
}
closesocket(serverSock);
}`
I have did all the error handlings but to reduce the code I removed it.