// JP opened flex table

Click to See Complete Forum and Search --> : Reverse HTTP Redirect Help


tdioz
December 30th, 2007, 04:32 AM
I am writing a reverse-connecting HTTP redirection, and I am having a lot of trouble... here is a basic outline of what it will be doing:

-> The server is ran on the computer that is going to be using the redirection.
-> The client is ran on the computer that is going to serve as the "proxy."

The server will listen on two ports:
-> 442: The client connects to the server on port 442
-> 8080: The web browser connects to the server on port 8080 (on the machine that is going to be using the redirection)

The client connects to two ports:
-> 80: The client connects to the actual web server on port (assumed) 80
-> 442: The client connects to the server on port 80

Port specifics:
-> 442: This port will be used to transfer data from the web browser and the web server
-> 8080: The web browser connects to this port (specifically 127.0.0.1:8080) and the server will send this data to the client

(If you see any flaws in this theory please notify me :P)

Now I have come up with some code for this however it is far from finished (I can even spot places where it is obvious there is a problem) and this gets very complicated for me and makes my head hurt, here is what I have:

SERVER:


#include <iostream>
#include <winsock.h>
#pragma comment( lib, "ws2_32" )

SOCKET sClient;
SOCKET sServer;

unsigned long __stdcall ClientServer( void* pVoid )
{
char Buffer[512];
int recvd;
SOCKADDR_IN sSin;
SOCKET sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

sSin.sin_family = AF_INET;
sSin.sin_port = htons(442);
sSin.sin_addr.s_addr = INADDR_ANY;

ZeroMemory(&sSin.sin_zero, 8);

if(bind( sSocket, (SOCKADDR*)&sSin, sizeof( SOCKADDR_IN ) ) == -1)
{
std::cout << "Error binding on port 442.\n";
std::cout << WSAGetLastError( ) << std::endl;
return 0;
}

if(listen( sSocket, 15 ) == -1)
{
std::cout << "Error listening on socket\n";
return 0;
}

sServer = accept(sSocket, 0, 0);

while( 1 )
{
recvd = recv(sServer, Buffer, sizeof( Buffer ), 0);
send(sClient, Buffer, recvd, 0);

recvd = recv(sClient, Buffer, sizeof( Buffer ), 0);
send(sServer, Buffer, recvd, 0);
}

return 0;
};

unsigned long __stdcall BrowserServer( void* pVoid )
{
char Buffer[ 512 ];
int recvd;
SOCKADDR_IN sSin;
SOCKET sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

sSin.sin_family = AF_INET;
sSin.sin_port = htons(8080);
sSin.sin_addr.s_addr = INADDR_ANY;

ZeroMemory(&sSin.sin_zero, 8);

if(bind( sSocket, (SOCKADDR*)&sSin, sizeof( SOCKADDR_IN ) ) == -1)
{
std::cout << "Error binding on port 8080.\n";
return 0;
}

if(listen( sSocket, 15 ) == -1)
{
std::cout << "Error listening on socket\n";
return 0;
}

sServer = accept(sSocket, 0, 0);

while( 1 )
{
recvd = recv(sClient, Buffer, sizeof( Buffer ), 0);
send(sServer, Buffer, recvd, 0);

recvd = recv( sServer, Buffer, sizeof( Buffer ), 0 );
send(sClient, Buffer, recvd, 0);
}

return 0;
};

int main( )
{
WSADATA wsaData;
WSAStartup(0x202, &wsaData);

sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

CreateThread(0, 0, &ClientServer, 0, 0, 0);
CreateThread(0, 0, &BrowserServer, 0, 0, 0);

getchar();

return 0;
};


CLIENT:

#include <winsock.h>
#pragma comment( lib, "ws2_32" )

int main( )
{
int recvd;
char Buffer[512];
WSADATA wsaData;
SOCKADDR_IN sSinWeb, sSinServer;
SOCKET sWeb, sServer;

WSAStartup(0x202, &wsaData);

sSinWeb.sin_family = AF_INET;
sSinWeb.sin_port = htons(80);
sSinWeb.sin_addr.s_addr = inet_addr("64.233.167.99");

sSinServer.sin_family = AF_INET;
sSinServer.sin_port = htons(442);
sSinServer.sin_addr.s_addr = inet_addr("127.0.0.1");

sWeb = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

connect(sWeb, (SOCKADDR *) &sSinWeb, sizeof( sSinWeb ));
connect(sServer, (SOCKADDR *) &sSinServer, sizeof( sSinServer ));

while( 1 )
{
recvd = recv( sServer, Buffer, sizeof( Buffer ), 0);
send(sWeb, Buffer, recvd, 0);

recvd = recv( sWeb, Buffer, sizeof( Buffer ), 0 ;
send(sServer, Buffer, recvd, 0);
}

return 0;
};


Any help will be greatly appreciated, Thanks!

//JP added flex table