-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_server.c
More file actions
353 lines (319 loc) · 10.1 KB
/
proxy_server.c
File metadata and controls
353 lines (319 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* Multiple simultaneous clients handled by threads; */
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> /* system type defintions */
#include <sys/socket.h> /* network system functions */
#include <netinet/in.h> /* protocol & struct definitions */
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#define BACKLOG 5
#define BUF_SIZE 1024
#define LISTEN_PORT 8888
#define SERVER_PORT 80
int threadCount = BACKLOG;
void *client_handler(void *arg);
int getIpAddress(char*, char*);
int destinationSock(char*);
int blacklist(char*);
int main(int argc, char *argv[]){
int status, *sock_tmp;
pthread_t a_thread;
void *thread_result;
struct sockaddr_in addr_mine;
struct sockaddr_in addr_remote;
int sock_listen;
int sock_aClient;
int addr_size;
int reuseaddr = 1;
sock_listen = socket(AF_INET, SOCK_STREAM, 0);
if (sock_listen < 0) {
perror("socket() failed");
exit(0);
}
memset(&addr_mine, 0, sizeof (addr_mine));
addr_mine.sin_family = AF_INET;
addr_mine.sin_addr.s_addr = htonl(INADDR_ANY);
addr_mine.sin_port = htons((unsigned short)LISTEN_PORT);
// /* Enable the socket to reuse the address */
// if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
// sizeof(int)) == -1) {
// perror("setsockopt");
// close(sock_listen);
// exit(1);
// }
status = bind(sock_listen, (struct sockaddr *) &addr_mine,
sizeof (addr_mine));
if (status < 0) {
perror("bind() failed");
close(sock_listen);
exit(1);
}
status = listen(sock_listen, 5);
if (status < 0) {
perror("listen() failed");
close(sock_listen);
exit(1);
}
addr_size = sizeof(struct sockaddr_in);
printf("waiting for a client\n");
while(1) {
if (threadCount < 1) {
sleep(1);
}
sock_aClient = accept(sock_listen, (struct sockaddr *) &addr_remote,
&addr_size);
if (sock_aClient == -1){
close(sock_listen);
exit(1);
}
printf("Got a connection from %s on port %d\n",
inet_ntoa(addr_remote.sin_addr),
htons(addr_remote.sin_port));
sock_tmp = malloc(1);
*sock_tmp = sock_aClient;
printf("thread count = %d\n", threadCount);
threadCount--;
status = pthread_create(&a_thread, NULL, client_handler,
(void *) sock_tmp);
if (status != 0) {
perror("Thread creation failed");
close(sock_listen);
close(sock_aClient);
free(sock_tmp);
exit(1);
}
}
return 0;
}
void *client_handler(void *sock_desc) {
int msg_size;
char ip[100];
int sock_send;
int i;
int send_len, bytes_sent;
char buf[BUF_SIZE];
int sock = *(int*)sock_desc;
printf("In client_handler\n");
while ((msg_size = recv(sock, buf, BUF_SIZE, 0)) > 0) { //This grabs a from the browser
buf[msg_size] = 0;
char message[BUF_SIZE];
strcpy(message, buf);
char* url = strtok(message, "/");
url = strtok(NULL, " ");
printf("Weburl: %s\n", url);
char tempUrl[100];
strcpy(tempUrl, url);
char* hostname;
char* tempFileRoute;
char fileRoute[200];
strcpy(fileRoute, "/");
if(strstr(url, "/") != NULL)
{
hostname = strtok(tempUrl, "/");
tempFileRoute = strtok(NULL, "\0");
strcat(fileRoute, tempFileRoute);
}
else
{
hostname = (char*) malloc((strlen(url) + 1) * sizeof(char));
strcpy(hostname, url);
}
printf("hostname: %s\n", hostname);
if(blacklist(hostname) == 0)
{
strcpy(buf, "HTTP/1.1 200\r\n");
strcat(buf, "Content-Type: text/html; charset=UTF-8\r\n\r\n");
strcat(buf, "<!DOCTYPE html><html><head><title>Blacklist Page</title></head><body><h1>BLACKLIST</h1><p>THIS SITE IS NOT ALLOWED ON THIS SERVER</p></body></html>");
send_len=strlen(buf);
bytes_sent=send(sock,buf,send_len,0);
}
else
{
int fail = getIpAddress(hostname, ip); //retrives the ip address for website and saves it to ip
if(fail == 1)
{
strcpy(buf, "HTTP/1.1 404\r\n");
strcat(buf, "Content-Type: text/html; charset=UTF-8\r\n\r\n");
strcat(buf, "<!DOCTYPE html><html><head><title>Page Doesn't Exist</title></head><body><h1>404</h1><p>Webpage not found</p></body></html>");
send_len=strlen(buf);
bytes_sent=send(sock,buf,send_len,0);
}
else
{
FILE* inCache;
char fileName[200];
strcpy(fileName, hostname);
strcat(fileName, ".txt");
inCache =fopen(fileName, "r");
if(inCache == NULL)
{
printf("Hasn't visited site yet\n");
printf("\nSending IP: %s\n", ip); //prints the ip
sock_send = destinationSock(ip); //creates a socket to that ip and port 80
strcpy(buf, "GET ");
strcat(buf, fileRoute);
strcat(buf, " HTTP/1.1\r\n");
strcat(buf, "Host: http://www.");
strcat(buf, hostname);
strcat(buf, "\r\n");
strcat(buf, "Connection: keep-alive\r\n");
strcat(buf, "Cache-Control: max-age=0\r\n");
strcat(buf, "Accept: text/html,*/*\r\n");
strcat(buf, "Accept-Encoding: gzip, deflate\r\n");
strcat(buf, "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36\r\n");
strcat(buf, "Accept-Language: en-US,en;q=0.8\r\n\r\n");
printf("Sending Message:\n%s\n", buf);
send_len=strlen(buf);
bytes_sent=send(sock_send,buf,send_len,0);
printf("\n\n");
//this reads in the response 1024 characters and sends that 1024 characters to the browser
//this is also where we would stream buf to a file as well for reading from cache
FILE *fptr;
fptr=fopen(fileName, "a+");
if(fptr ==NULL)
{
printf("FAIL TO OPEN FILE\n");
exit(1);
}
if(strcmp(hostname, "cse.unt.edu") == 0)
{
strcpy(buf, "HTTP/1.1 200\r\n");
strcat(buf, "Content-Type: text/html; charset=UTF-8\r\n\r\n");
send_len=strlen(buf);
bytes_sent=send(sock,buf,send_len,0);
}
while ((msg_size = recv(sock_send, buf, BUF_SIZE, 0)) > 0) {
buf[msg_size] = 0;
char tempBuf[BUF_SIZE];
char sendBuf[BUF_SIZE];
strcpy(tempBuf, buf);
strcpy(sendBuf, "");
//This is out filter
FILE *badWord= fopen("badwords.txt", "r");
if(badWord == NULL) printf("missing bad words file");
else
{
char ch;
char toString[2];
char bWord[50];
strcpy(bWord, "");
while( ( ch = fgetc(badWord) ) != EOF ) { //we read in each character
if(ch == '\n')
{
strcpy(tempBuf, buf);
char* pch2 = strtok(tempBuf, " ");
while(pch2 != NULL)
{
if(strcmp(pch2, bWord) == 0) strcat(sendBuf, "****");
else strcat(sendBuf, pch2);
strcat(sendBuf, " ");
pch2 = strtok(NULL, " ");
}
strcpy(buf, sendBuf);
strcpy(bWord, "");
strcpy(sendBuf, "");
strcpy(tempBuf, "");
}
else
{
sprintf(toString, "%c",ch);
strcat(bWord, toString);
}
}
fclose(badWord);
}
fprintf(fptr, "%s", buf);
send_len=strlen(buf);
bytes_sent=send(sock,buf,send_len,0);
}
fclose(fptr);
printf("\n\nOut of Loop\n");
strcpy(buf, "");
close(sock_send);
}
else
{
printf("reading from cache\n\n");
char ch;
char sendBuf[BUF_SIZE];
while( ( ch = fgetc(inCache) ) != EOF ) {
sprintf(sendBuf, "%c",ch);
send_len=strlen(sendBuf);
bytes_sent=send(sock,sendBuf,send_len,0);
}
fclose(inCache);
}
}
}
}
close(sock);
free(sock_desc);
threadCount++;
// pthread_exit("Thank you for the CPU time");
}
int blacklist(char* hostname)
{
if(strcmp(hostname, "facebook.com") == 0) return 0;
else if(strcmp(hostname, "youtube.com") == 0) return 0;
else if(strcmp(hostname, "hulu.com") == 0) return 0;
else if(strcmp(hostname, "example.com") == 0) return 0;
else if(strcmp(hostname, "virus.com") == 0) return 0;
else return 1;
}
int getIpAddress(char* hostname, char* ip)
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
struct sockaddr_in *h;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
if ( (rv = getaddrinfo( hostname , "http" , &hints , &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
h = (struct sockaddr_in *) p->ai_addr;
strcpy(ip , inet_ntoa( h->sin_addr ) );
}
freeaddrinfo(servinfo); // all done with this structure
return 0;
}
int destinationSock(char* ip)
{
int sock_send;
struct sockaddr_in addr_send;
int i;
sock_send=socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock_send < 0) {
printf("socket() failed\n");
exit(0);
}
else {
printf("socket created\n");
}
/* create socket address structure to connect to */
memset(&addr_send, 0, sizeof (addr_send)); /* zero out structure */
addr_send.sin_family = AF_INET; /* address family */
addr_send.sin_addr.s_addr = inet_addr(ip);
addr_send.sin_port = htons((unsigned short)SERVER_PORT);
/* connect to the server */
i=connect(sock_send, (struct sockaddr *) &addr_send, sizeof (addr_send));
if (i < 0) {
printf("connect() failed\n");
close(sock_send);
exit(0);
}
else {
printf("connect successful\n");
}
return sock_send;
}