I now start the listener in the main.c file rather than proxy given that I didn't feel proxy was the right place if a normal (non-proxied) request came in. webserver.{c,h} and proxy.{c,h} had some changes relating to this. The config changed slightly - we now create a folder in ~/.config/ called yaip. This is where certificates and so on will be stored along with the user configuration I created a helper function to get files inside this directory (it changes based on xdg_config_home) and updated relevant tests. In ssl.{c,h} I have started work. If they don't exist, the tool now creates and stores a key and certificate for the CA that this tool will need to pretend to be. I still need to write tests for this.
87 lines
2.2 KiB
C
87 lines
2.2 KiB
C
#include "proxy.h"
|
|
|
|
Response *upstreamGetResponse(Request *request){
|
|
//Here we pretend to be a client
|
|
|
|
int client_fd = 0;
|
|
struct sockaddr_in address;
|
|
memset( &address, 0, sizeof(address) );
|
|
struct hostent *host = gethostbyname(request->host);
|
|
Response *rsp = NULL;
|
|
|
|
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
|
|
perror("socket failed");
|
|
return NULL;
|
|
}
|
|
|
|
|
|
address.sin_family = AF_INET;
|
|
address.sin_port = htons( 80 );
|
|
// We want the request to go out to whatever the host was resolved to
|
|
memcpy( &address.sin_addr, host->h_addr_list[0], host->h_length );
|
|
|
|
if((connect(client_fd, (struct sockaddr *)&address, sizeof(address)))<0) {
|
|
perror("connect failed");
|
|
return NULL;
|
|
}
|
|
|
|
char *toSend = requestToString(request);
|
|
|
|
if ( write( client_fd, toSend, strlen(toSend) ) != strlen(toSend) ){
|
|
perror( "Write Error" );
|
|
return NULL;
|
|
}
|
|
|
|
rsp = newResponseFromSocket( client_fd );
|
|
|
|
return rsp;
|
|
|
|
}
|
|
|
|
void proxyRequest(Request *request, int client){
|
|
|
|
if ( strcmp( request->method, "CONNECT" ) == 0 ){
|
|
// If it is a connect request, we are dealing with https
|
|
|
|
// I am basically doing the same thing that mitmproxy does here
|
|
// We start by responding with 200 Connection Established which
|
|
// in a normal proxy would mean that we have established a
|
|
// connection with the remote host. However, we haven't because we
|
|
// are going to pretend to be the host to the client and pretend to
|
|
// be the client to the host
|
|
|
|
Response *response = newResponse();
|
|
connectionEstablished(response);
|
|
char *responseStr = responseToString(response);
|
|
send(client , responseStr, strlen(responseStr) , 0 );
|
|
|
|
|
|
|
|
|
|
|
|
//SSL_CTX *ctx;
|
|
//SSL *ssl;
|
|
//char buf[1024] = {0};
|
|
//int bytes;
|
|
|
|
//SSL_library_init();
|
|
//ctx = InitServerCTX(config);
|
|
//ssl = SSL_new(ctx);
|
|
//SSL_set_fd( ssl, client );
|
|
|
|
//if ( SSL_accept(ssl) == -1 ){
|
|
// ERR_print_errors_fp(stderr);
|
|
//} else {
|
|
// bytes = SSL_read(ssl, buf, sizeof(buf));
|
|
// buf[bytes] = '\0';
|
|
// printf("%s", buf);
|
|
//}
|
|
|
|
} else {
|
|
Response *response = upstreamGetResponse(request);
|
|
char *responseStr = responseToString( response );
|
|
send(client , responseStr, strlen(responseStr) , 0 );
|
|
free( responseStr );
|
|
freeResponse( response );
|
|
}
|
|
}
|