forked from InnovativeDev-hub/new-sights
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-enhanced-webserver.c
More file actions
144 lines (123 loc) · 4.45 KB
/
ai-enhanced-webserver.c
File metadata and controls
144 lines (123 loc) · 4.45 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#include <ctype.h>
#include <pthread.h>
#define PORT 8080
#define BUFFER_SIZE 4096
#define LOG_FILE "server.log"
// List of sensitive words for content filtering
const char *sensitive_words[] = {"password", "secret", "classified", "private", NULL};
// Function to log server activity
void log_request(const char *client_ip, const char *request) {
FILE *log_file = fopen(LOG_FILE, "a");
if (log_file == NULL) {
perror("Failed to open log file");
return;
}
time_t now = time(NULL);
char *time_str = ctime(&now);
time_str[strcspn(time_str, "\n")] = '\0'; // Remove newline character
fprintf(log_file, "[%s] Client: %s Requested: %s\n", time_str, client_ip, request);
fclose(log_file);
}
// Function to check if the request contains sensitive words
int contains_sensitive_word(const char *request) {
for (int i = 0; sensitive_words[i] != NULL; i++) {
if (strstr(request, sensitive_words[i]) != NULL) {
return 1; // Sensitive word found
}
}
return 0;
}
// Function to process the client request
void *handle_client(void *arg) {
int client_socket = *(int *)arg;
free(arg);
char buffer[BUFFER_SIZE];
int bytes_read = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (bytes_read <= 0) {
close(client_socket);
pthread_exit(NULL);
}
buffer[bytes_read] = '\0';
printf("Received request: %s\n", buffer);
// Get client IP address
struct sockaddr_in client_addr;
socklen_t addr_size = sizeof(client_addr);
getpeername(client_socket, (struct sockaddr *)&client_addr, &addr_size);
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, sizeof(client_ip));
// Log the request
log_request(client_ip, buffer);
// Check for sensitive words
if (contains_sensitive_word(buffer)) {
const char *blocked_response =
"HTTP/1.1 403 Forbidden\r\n"
"Content-Type: text/plain\r\n\r\n"
"Access Denied: Sensitive content detected.\r\n";
send(client_socket, blocked_response, strlen(blocked_response), 0);
} else {
// Simulated AI response (this can be connected to an actual AI API)
const char *ai_response =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n\r\n"
"<!DOCTYPE html>"
"<html><head><title>AI-Enhanced Server</title></head>"
"<body><h1>Welcome to the AI-powered Web Server!</h1>"
"<p>This page is dynamically generated using AI logic.</p>"
"</body></html>";
send(client_socket, ai_response, strlen(ai_response), 0);
}
close(client_socket);
pthread_exit(NULL);
}
// Function to start the server
void start_server() {
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Bind failed");
close(server_socket);
exit(EXIT_FAILURE);
}
if (listen(server_socket, 10) == -1) {
perror("Listen failed");
close(server_socket);
exit(EXIT_FAILURE);
}
printf("Server is running on port %d...\n", PORT);
while (1) {
struct sockaddr_in client_addr;
socklen_t client_addr_size = sizeof(client_addr);
int client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_size);
if (client_socket == -1) {
perror("Failed to accept client connection");
continue;
}
int *client_sock_ptr = malloc(sizeof(int));
*client_sock_ptr = client_socket;
pthread_t thread_id;
if (pthread_create(&thread_id, NULL, handle_client, client_sock_ptr) != 0) {
perror("Failed to create thread");
close(client_socket);
} else {
pthread_detach(thread_id);
}
}
close(server_socket);
}
int main() {
start_server();
return 0;
}