void main_loop(int server_socket) { int client_socket, pid; socklen_t client_address_length; static struct sockaddr_in client_address; while(1) { client_address_length = sizeof(client_address); if((client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_length)) < 0) printf("Error when accept()"); if((pid = fork()) < 0) { printf("Error when fork()"); } else { if(pid == 0) { close(server_socket); web_handler(client_socket); } else { close(client_socket); } } } }
static int event_handler(struct mg_connection *conn, enum mg_event ev) { if(ev == MG_REQUEST) { if(conn->is_websocket) { ws_handler(conn, FLAG_WS_DATA); return MG_TRUE; } else { int ret = web_handler(conn); if(ret < 0) { const char* srvfilepath = strlen(conn->uri) <= 1 ? "index.html" : conn->uri; if(srvfilepath[0] == '/') srvfilepath++; mg_send_file(conn, srvfilepath, NULL); return MG_MORE; } return MG_TRUE; } } else if(ev == MG_WS_CONNECT) { conn->connection_param = malloc(25); const char* key = mg_get_header(conn, "Sec-WebSocket-Key"); strncpy(conn->connection_param, key, 24); char *p = (char*)(conn->connection_param); p[24] = '\0'; ws_handler(conn, FLAG_WS_OPEN); return MG_TRUE; } else if(ev == MG_CLOSE) { if(conn->is_websocket) { ws_handler(conn, FLAG_WS_CLOSE); } free(conn->connection_param); return MG_TRUE; } else if(ev == MG_AUTH) { return MG_TRUE; } return MG_FALSE; }
//method that talks with the browser void *client_handler(void *sock_desc) { int msg_size; char buf[BUF_SIZE], request[BUF_SIZE]; int sock = *(int*)sock_desc; while ((msg_size = recv(sock, buf, BUF_SIZE, 0)) > 0) { buf[msg_size] = 0; //figure out what we want to get char tokenize[msg_size+1]; strcpy(tokenize, buf); char * token = strtok(tokenize, " "); token = strtok(NULL, " "); //complete address char * pch = token + 1; char address[200]; if(pch) { strcpy(address, pch); } //document path char * pch2 = strchr(pch, '/'); char document[200]; if(pch2) { strcpy(document, pch2); } else { strcpy(document, "/"); } //host or "domain" char host[200], ip[100]; int diff; if(pch && pch2) { diff = pch2 - pch; } else { diff = strlen(address); } strncpy(host, address, diff); //check if site is blocked FILE * blacklist; char blBuffer[100]; blacklist = fopen("blacklist.txt", "r"); int blocked = 0; while (fgets(blBuffer, 100, blacklist) != NULL ) { if(strncmp(host, blBuffer, diff) == 0) { blocked = 1; break; } } fclose(blacklist); if(blocked == 1) { //this is blocked //this whole block of code will read "blocked_response.txt" and send the contents to the browser FILE * isBlocked; isBlocked = fopen("blocked_response.txt", "r"); char blockedSend[BUF_SIZE]; char blockedBuffer[10000]; while(fgets(blockedBuffer, 10000, isBlocked) != NULL) { strcat(blockedSend, blockedBuffer); memset(blockedBuffer,0,strlen(blockedBuffer)); } strcat(blockedSend, "\r\n\r\n"); sendRes(blockedSend, sock); memset(blockedSend,0,strlen(blockedSend)); printf("%s", "This is blocked!"); fflush(stdout); fclose(isBlocked); } else { //CHECK FOR CACHE FILE FILE * fp; fp = fopen(host, "r"); if(!fp) { //make the file fp = fopen(host, "w"); //build our request strncpy(request, "GET ", 4); strcat(request, document); strcat(request, " HTTP/1.1\r\nHost: "); strncat(request, host, diff); strcat(request, "\r\nUser-Agent: WebProxy/0.99"); strcat(request, "\r\n\r\n"); host[diff] = '\0'; hostname_to_ip(host, ip); printf("%s\n", request); //send our request char * returned = web_handler(ip, request); //filter for bad words FILE * badwords; badwords = fopen("badwords.txt", "r"); char badBuffer[100]; while(fgets(badBuffer, 100, badwords) != NULL) { int length = strlen(badBuffer); badBuffer[length-1] = '\0'; returned = str_replace(returned, badBuffer, "***REMOVED***"); } sendRes(returned, sock); //print final version to cache file fprintf(fp, "%s", returned); } else { //read the file char toSend[BUF_SIZE]; char fileBuffer[10000]; while(fgets(fileBuffer, 10000, fp) != NULL) { strcat(toSend, fileBuffer); memset(fileBuffer,0,strlen(fileBuffer)); } //send cached content to browser sendRes(toSend, sock); memset(toSend,0,strlen(toSend)); } fclose(fp); } memset(buf,0,strlen(buf)); //memset(address,0,strlen(address)); memset(document,0,strlen(document)); memset(ip,0,strlen(ip)); } close(sock); free(sock_desc); threadCount++; }