|
|
@ -1,12 +1,12 @@ |
|
|
|
#include "proxy.h" |
|
|
|
#include "proxy.h" |
|
|
|
|
|
|
|
|
|
|
|
void proxy_startListener(unsigned int port){ |
|
|
|
void proxy_startListener(unsigned int port){ |
|
|
|
// https://www.geeksforgeeks.org/socket-programming-cc/
|
|
|
|
|
|
|
|
int server_fd, new_socket; |
|
|
|
int server_fd, new_socket; |
|
|
|
struct sockaddr_in address; |
|
|
|
struct sockaddr_in address; |
|
|
|
int opt = 1; |
|
|
|
int opt = 1; |
|
|
|
int addrlen = sizeof(address); |
|
|
|
int addrlen = sizeof(address); |
|
|
|
char line[1024] = {0}; |
|
|
|
char *response; |
|
|
|
|
|
|
|
|
|
|
|
// Creating socket file descriptor
|
|
|
|
// Creating socket file descriptor
|
|
|
|
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { |
|
|
|
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { |
|
|
@ -15,19 +15,20 @@ void proxy_startListener(unsigned int port){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
address.sin_family = AF_INET; |
|
|
|
address.sin_family = AF_INET; |
|
|
|
address.sin_addr.s_addr = htonl(INADDR_ANY); |
|
|
|
address.sin_addr.s_addr = INADDR_ANY; |
|
|
|
address.sin_port = htons( port ); |
|
|
|
address.sin_port = htons( port ); |
|
|
|
|
|
|
|
|
|
|
|
// Forcefully attaching socket to the port 8080
|
|
|
|
// Forcefully attaching socket to the port 8080
|
|
|
|
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) { |
|
|
|
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) != 0) { |
|
|
|
perror("bind failed"); |
|
|
|
perror("bind failed"); |
|
|
|
return ; |
|
|
|
return ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (listen(server_fd, 3) < 0) { |
|
|
|
if (listen(server_fd, 3) != 0) { |
|
|
|
perror("listen"); |
|
|
|
perror("listen"); |
|
|
|
return ; |
|
|
|
return ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while ( true ){ |
|
|
|
while ( true ){ |
|
|
|
printf("Listening\n"); |
|
|
|
printf("Listening\n"); |
|
|
|
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
|
|
|
|
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
|
|
|
@ -37,6 +38,17 @@ void proxy_startListener(unsigned int port){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Request *request = newRequestFromSocket(new_socket); |
|
|
|
Request *request = newRequestFromSocket(new_socket); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( strcmp( request->host, "" ) == 0 ){ |
|
|
|
|
|
|
|
response = webserverGetResponse(request); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
printf("This is a proxy request\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
send(new_socket , response, strlen(response) , 0 ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//close(new_socket);
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|