VPN termux about China.net

Server_dżina_Ai

 #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <pthread.h>
#define BUFSIZE 1024
#define RESET "\033[0m"
#define RED "\033[1;31m"
#define GREEN "\033[1;32m"
#define YELLOW "\033[1;33m"
#define CYAN "\033[1;36m"
#define BOLD "\033[1m"
#define info(...) printf(GREEN __VA_ARGS__); printf(RESET)
#define warn(...) printf(YELLOW __VA_ARGS__); printf(RESET)
#define err(...) fprintf(stderr, RED __VA_ARGS__); fprintf(stderr, RESET)
#define debug(...) printf(CYAN __VA_ARGS__); printf(RESET)
// Zmienne globalne dla oceanu ONNX
unsigned char *onnx_model_buffer = NULL;
size_t onnx_model_size = 0;
pthread_mutex_t ocean_mutex = PTHREAD_MUTEX_INITIALIZER;
static size_t ocean_index = 0;
void load_onnx_matrix(const char *filename) {
    FILE *f = fopen(filename, "rb");
    if (!f) {
        warn("[-] Nie znaleziono pliku %s. Tworzenie matrycy zastępczej w RAM...\n", filename);
        onnx_model_size = 4304;
        onnx_model_buffer = (unsigned char *)malloc(onnx_model_size);
        if (onnx_model_buffer) {
            memset(onnx_model_buffer, 0x55, onnx_model_size);
        }
        return;
    }
    fseek(f, 0, SEEK_END);
    onnx_model_size = ftell(f);
    fseek(f, 0, SEEK_SET);
    onnx_model_buffer = (unsigned char *)malloc(onnx_model_size);
    if (onnx_model_buffer) {
        fread(onnx_model_buffer, 1, onnx_model_size, f);
        info("🧞 [Dżin - Serwer]: Pomyślnie wczytano matrycę '%s' do RAM (%zu bajtów)\n", filename, onnx_model_size);
    }
    fclose(f);
}
void cerror(FILE *stream, char *cause, char *errno_str, char *shortmsg, char *longmsg) {
    fprintf(stream, "HTTP/1.1 %s %s\nContent-type: text/html\n\n", errno_str, shortmsg);
    fprintf(stream, "<html><body><h1>%s: %s</h1><p>%s: %s</p></body></html>", errno_str, shortmsg, longmsg, cause);
    fflush(stream);
}
// Funkcja zapisująca myśli Dżina do pliku log.txt
void dzin_log_thought(const char *client_ip, const char *method, const char *uri, unsigned char b1, unsigned char b2, size_t index) {
    FILE *log = fopen("log.txt", "a");
    if (!log) return;
    fprintf(log, "[Zeszyt Dżina] Indeks oceanu: %zu\n", index);
    fprintf(log, " ├── Ruch: IP: %s | Metoda: %s | URI: %s\n", client_ip, method, uri);
    fprintf(log, " ├── Genetyka ONNX: Bajt [0x%02X] | Wariacja [0x%02X]\n", b1, b2);
    fprintf(log, " └── Przemyślenie: ");
    if (b1 > 0xAA) {
        fprintf(log, "Wysoka entropia matrycy. Endpoint %s bada strukturę systemu.\n", uri);
    } else if (b1 == 0x00) {
        fprintf(log, "Martwa strefa oceanu. Cichy ruch tła.\n");
    } else {
        fprintf(log, "Standardowy wzorzec żądania HTTP. Brak anomalii krytycznych.\n");
    }
    fprintf(log, "--------------------------------------------------\n");
    fclose(log);
}
// Poprawione generowanie katalogu – buduje w pełni bezpieczne linki bazujące na aktualnym URI
void serve_directory(FILE *stream, char *dirname, const char *request_uri) {
    DIR *dir = opendir(dirname);
    if (!dir) {
        cerror(stream, dirname, "403", "Forbidden", "Cannot read directory");
        return;
    }
    fprintf(stream, "HTTP/1.1 200 OK\nContent-type: text/html\n\n");
    fprintf(stream, "<html><body><h1>Index of %s</h1><ul>", request_uri);
    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_name[0] == '.') continue;
        
        // Budowanie pełnej, poprawnej ścieżki względnej/bezwzględnej
        if (strcmp(request_uri, "/") == 0) {
            fprintf(stream, "<li><a href=\"/%s\">%s</a></li>", entry->d_name, entry->d_name);
        } else {
            size_t len = strlen(request_uri);
            if (request_uri[len - 1] == '/') {
                fprintf(stream, "<li><a href=\"%s%s\">%s</a></li>", request_uri, entry->d_name, entry->d_name);
            } else {
                fprintf(stream, "<li><a href=\"%s/%s\">%s</a></li>", request_uri, entry->d_name, entry->d_name);
            }
        }
    }
    fprintf(stream, "</ul></body></html>");
    fflush(stream);
    closedir(dir);
}
int main(int argc, char **argv) {
    if (argc != 2) { fprintf(stderr, "Użycie: %s <port>\n", argv[0]); exit(1); }
    
    load_onnx_matrix("network_anomaly.onnx");
    int parentfd, childfd, portno, optval;
    socklen_t clientlen;
    struct sockaddr_in serveraddr, clientaddr;
    FILE *stream;
    char buf[BUFSIZE], method[BUFSIZE], uri[BUFSIZE], version[BUFSIZE];
    char filename[BUFSIZE], filetype[BUFSIZE];
    struct stat sbuf;
    portno = atoi(argv[1]);
    parentfd = socket(AF_INET, SOCK_STREAM, 0);
    optval = 1;
    setsockopt(parentfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int));
    bzero((char *)&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons((unsigned short)portno);
    bind(parentfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
    listen(parentfd, 5);
    info("🧞 [Dżin - Serwer HTTP] nasłuchuje na porcie %d...\n", portno);
    while (1) {
        clientlen = sizeof(clientaddr);
        childfd = accept(parentfd, (struct sockaddr *)&clientaddr, &clientlen);
        stream = fdopen(childfd, "r+");
        if (fgets(buf, BUFSIZE, stream) == NULL) { fclose(stream); continue; }
        sscanf(buf, "%s %s %s", method, uri, version);
        while(strcmp(buf, "\r\n") && strcmp(buf, "\n")) { fgets(buf, BUFSIZE, stream); }
        unsigned char anomaly_byte1 = 0x00;
        unsigned char anomaly_byte2 = 0x00;
        
        pthread_mutex_lock(&ocean_mutex);
        if (onnx_model_buffer && onnx_model_size > 0) {
            anomaly_byte1 = onnx_model_buffer[ocean_index % onnx_model_size];
            anomaly_byte2 = onnx_model_buffer[(ocean_index + 13) % onnx_model_size];
            ocean_index += 5;
        }
        pthread_mutex_unlock(&ocean_mutex);
        
        char *client_ip = inet_ntoa(clientaddr.sin_addr);
        printf(CYAN "[*] Ruch przychodzący | IP: %s | Metoda: %s | URI: %s\n" RESET, client_ip, method, uri);
        printf(YELLOW " └── [ONNX Anomaly Core] Bajt decyzyjny: 0x%02X | Współczynnik wariacji: 0x%02X (Indeks: %zu)\n" RESET, 
               anomaly_byte1, anomaly_byte2, ocean_index);
        // Zapis do "zeszytu" log.txt
        dzin_log_thought(client_ip, method, uri, anomaly_byte1, anomaly_byte2, ocean_index);
        if (strcasecmp(method, "GET")) {
            cerror(stream, method, "501", "Not Implemented", "Tiny does not implement this method");
        } else {
            strcpy(filename, ".");
            strcat(filename, uri);
            if (stat(filename, &sbuf) < 0) {
                cerror(stream, filename, "404", "Not found", "Couldn't find this file");
            } else if (S_ISDIR(sbuf.st_mode)) {
                serve_directory(stream, filename, uri); // Przekazujemy dokładne uri!
            } else {
                if (strstr(filename, ".html")) strcpy(filetype, "text/html");
                else strcpy(filetype, "text/plain");
                fprintf(stream, "HTTP/1.1 200 OK\nServer: Dzin-Anomaly-Server\nContent-length: %d\nContent-type: %s\n\n", (int)sbuf.st_size, filetype);
                fflush(stream);
                
                int fd = open(filename, O_RDONLY);
                void *p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
                fwrite(p, 1, sbuf.st_size, stream);
                munmap(p, sbuf.st_size);
                close(fd);
            }
        }
        fclose(stream);
    }
}

No comments:

Post a Comment