#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
//#include "run.cpp"
#define PORT 8080
#define BUFFER_SIZE 1024
#include <stdio.h>
#define MAX_FILE_SIZE 1024
char *run(char*cmd) {
char *echo;
FILE *fp = popen(cmd, "r");
if (fp == NULL) {
perror("popen");
return NULL;
}
size_t len = fread(echo, 1, MAX_FILE_SIZE, fp);
echo[len] = '\0';
printf("%s",echo);
pclose(fp);
return echo;
}
struct dialog {
char *body;
}msg;
void handle_post(int client_socket, char *data) {
printf("Received POST data: %s\n", data);
const char *response =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 24\r\n"
"\r\n"
"Hello, POST!";
send(client_socket, response, strlen(response), 0);
}
void handle_get(int client_socket, const char *uri) {
if (strcmp(uri, "/") == 0) {
// Jeśli URI to "/", wyślij stronę HTML z formularzem
FILE *fp = fopen("console.html", "r");
if (fp == NULL) {
perror("Failed to open console.html");
close(client_socket);
return;
}
// Odczytanie zawartości pliku
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *file_content = (char *)malloc(file_size + 1);
fread(file_content, 1, file_size, fp);
file_content[file_size] = '\0';
fclose(fp);
char response[BUFFER_SIZE];
snprintf(response, sizeof(response),
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Content-Length: %ld\r\n"
"\r\n%s",
file_size, file_content);
send(client_socket, response, strlen(response), 0);
free(file_content);
}
if (strcmp(uri, "/console") == 0) {
handle_post(client_socket, run(msg.body));
}
else {
// Domyślna odpowiedź dla innych URI
const char *response =
"HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 13\r\n"
"\r\n"
"404 Not Found";
send(client_socket, response, strlen(response), 0);
}
}
void parse_request_line(char *req, char *method, char *uri) {
sscanf(req, "%s %s", method, uri);
}
void create_console_html(char*msg) {
FILE *fp = fopen("console.html", "w");
if (fp == NULL) {
perror("Failed to create console.html");
return;
}
// char*cmd=(char*)malloc(sizeof(run(msg)));
// cmd=run(msg);
fprintf(fp,
"<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Simple Console</title></head><body><h1>Simple Server Console</h1><form method=\"POST\" action=\"/console\"><label for=\"command\">Enter command:</label><br><input type=\"text\" id=\"command\" name=\"command\" size=\"50\"><br><br><input type=\"submit\" value=\"Execute\"></form><h2>Output:</h2><pre id=\"output\">%s<!-- Output the command will be displayed here --></pre></body></html>",msg);
fclose(fp);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];
printf("https://127.0.0.1:8080");
// Create socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Bind the socket to the specified port
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
close(server_socket);
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_socket, 10) < 0) {
perror("Listen failed");
close(server_socket);
exit(EXIT_FAILURE);
}
// Inform the user how to access the server
// printf("Server is listening on https://127.0.0.1:%d...\n", PORT);
// Create the HTML console template
create_console_html("");
// Main loop: accept and handle requests
while (1) {
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket < 0) {
perror("Accept failed");
continue;
}
int received = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (received < 0) {
perror("recv failed");
close(client_socket);
continue;
}
buffer[received] = '\0'; // Null-terminate the received data
char method[20], uri[1024];
parse_request_line(buffer, method, uri);
if (strcmp(method, "GET") == 0) {
handle_get(client_socket, uri);
} else if (strcmp(method, "POST") == 0) {
msg.body = strstr(buffer, "\r\n\r\n");
if (msg.body) {
msg.body += 12; // Move pointer past the "\r\n\r\n"
}
char response[BUFFER_SIZE];
snprintf(response, sizeof(response)+sizeof(run(msg.body)),
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n" "\r\n%s", run(msg.body));
create_console_html(response);
send(client_socket, response, strlen(response), 0);
}
close(client_socket);
}
close(server_socket);
return 0;
}
VPN termux about China.net
Sunday, 25 August 2024
TCP C server console
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment