VPN termux about China.net

Tuesday 13 August 2024

ServerC try this

 #include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <time.h>

#include <dirent.h>

#define PORT 1200

int i = 0;

#define MAX_FILE_SIZE 2024

#define MAX_LINES 100

#define MAX_LINE_LENGTH 80

char lines[MAX_LINES][MAX_LINE_LENGTH];

char *run_2(int a)

{

FILE *fp = popen("ls", "r");

if (fp == NULL)

{

perror("fopen");

return NULL;

}

int i = 0;

while (fgets(lines[i], MAX_LINE_LENGTH, fp) != NULL && i < MAX_LINES)

{

lines[i][strcspn(lines[i], "\n")] = '\0';

i++;

}

fclose(fp);

printf("%s", lines[a]);

return lines[a];

}


char *loadFile(char *file2)

{

char *buffer = (char *)malloc(MAX_FILE_SIZE + 1);

if (!buffer)

{

perror("Error allocating memory");

return NULL;

}

FILE *file = fopen(file2, "r");

if (!file)

{

perror("Error opening file");

free(buffer);

return NULL;

}

size_t len = fread(buffer, 1, MAX_FILE_SIZE, file);

if (ferror(file))

{

perror("Error reading file");

fclose(file);

free(buffer);

return NULL;

}

fclose(file);

buffer[len] = '\0';

return buffer;

free(buffer);

}


int map_method_to_int(const char *method)

{

if (strcmp(method, "GET") == 0)

{

return 1;

}

else if (strcmp(method, "POST") == 0)

{

return 2;

}

else if (strcmp(method, "PUT") == 0)

{

return 3;

}

else

{

return 0; // Method not implemented or unknown

}

}


void saveFile(char *data)

{

FILE *fp = fopen("tmp.html", "w");

if (fp != NULL)

{

fprintf(fp, "%s", data);

fclose(fp);

}

else

{

perror("fopen");

}

}


void parse_request_line(char *req, char *method, char *uri)

{

sscanf(req, "%s %s", method, uri);

}


// Send a basic HTTP response

/* to jest potrzebne zeby wyslac list do przegladarki odpowiedz serwera */

void send_response(int connfd, const char *status, const char *content_type, char *body)

{

char response[1024];

snprintf(response, sizeof(response), "HTTP/1.1 %s\r\nContent-Type: %s\r\n\r\n/sdcard%s",

status, content_type, body);

send(connfd, response, strlen(response), 0);

}


int main(int argc, char *argv[])

{

int listenfd = 0, connfd = 0;

struct sockaddr_in serv_addr;

struct sockaddr_in cliaddr;

socklen_t cliaddrlen = sizeof(cliaddr);

char buffer[1024];


/****************************/

/* obsluga polaczenoa tcp */

if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)

{

perror("socket failed");

exit(EXIT_FAILURE);

}

printf("Socket created successfully\n");

/* familiada tcp */

serv_addr.sin_family = AF_INET;

/* ip 127.0.0.1= inaddr_any */

serv_addr.sin_addr.s_addr = INADDR_ANY;

/* port */

serv_addr.sin_port = htons(PORT);

/* bind to pobicie ip sprawdza czy mozna utwozyc serwer na danym ip jesli

   tak to go tworzy */

if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)

{

perror("bind failed");

exit(EXIT_FAILURE);

}

printf("Binding successful\n");

/* nasluch max 10 klijentow */

if (listen(listenfd, 10) < 0)

{

perror("listen");

exit(EXIT_FAILURE);

}

/* printf console viewer conect */

printf("http://127.0.0.1:1200\n");

printf("Listening...\n");

/*****************************/

/* engine- silnik */

while (1)

{

connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddrlen);

if (connfd < 0)

{

perror("accept");

continue;

}


if (recv(connfd, buffer, sizeof(buffer), 0) < 0)

{

perror("recv");

close(connfd);

continue;

}

char method[20], uri[1024];

parse_request_line(buffer, method, uri);

i++;

int method_code = map_method_to_int(method);

switch (method_code)

{

case 1:

{ // GET Method

char *page = (char *)malloc(120);

if (page)

{

sprintf(page, "/%s", run_2(4));

printf("%s", page);

send_response(connfd, "200 OK", "text/html", loadFile(run_2(i)));

free(page);

}

else if (strcmp(uri, "/console") == 0)

{

char *content = loadFile("console.html");

if (content)

{

send_response(connfd, "200 OK", "text/html", content);

printf("sdcard/ %s\n", content);

}

printf("console");

}

else

{

char error[20] = { "Page not found" };

send_response(connfd, "404 Not Found", "text/plain", error);

}

break;

}

case 2:

{ // POST Method

// Implement POST logic here

break;

}

case 3:

{ // PUT Method

// Implement PUT logic here

break;

}

default:

{ // Method not implemented

char error[24] = { "Method not implemented" };

send_response(connfd, "501 Not Implemented", "text/plain", error);

break;

}

}

close(connfd);

}

close(listenfd);

return 0;

}


No comments:

Post a Comment