proxy_v3_nativo.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * =====================================================================================
  3. * PROXY MULTIFILAMENTADO PROFESIONAL - VERSIÓN 3 (NATIVO EN C)
  4. * Mejoras: Registro de Logs detallados y Monitoreo de tráfico.
  5. * Compilación: gcc -O3 -o proxy_v3 proxy_v3_nativo.c -lpthread
  6. * =====================================================================================
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <arpa/inet.h>
  13. #include <sys/socket.h>
  14. #include <sys/select.h>
  15. #include <pthread.h>
  16. #include <signal.h>
  17. #include <time.h>
  18. #include <sys/stat.h>
  19. // --- CONFIGURACIÓN BASE ---
  20. #define DEFAULT_PORT 8080
  21. #define SSH_HOST "127.0.0.1"
  22. #define SSH_PORT 22
  23. #define BUFLEN 16384
  24. #define MAX_CONNECTIONS 300
  25. #define LOG_FILE "/root/proxy-v3.log"
  26. // --- FAKE WEB ROBUSTA (Nginx 1.24) ---
  27. const char *FAKE_WEB_RESPONSE =
  28. "HTTP/1.1 400 Bad Request\r\n"
  29. "Server: nginx/1.24.0\r\n"
  30. "Content-Type: text/html\r\n"
  31. "Content-Length: 157\r\n"
  32. "Connection: close\r\n\r\n"
  33. "<html>\r\n<head><title>400 Bad Request</title></head>\r\n"
  34. "<body>\r\n<center><h1>400 Bad Request</h1></center>\r\n"
  35. "<hr><center>nginx/1.24.0</center>\r\n</body>\r\n</html>\r\n";
  36. const char *HTTP_101_RESPONSE =
  37. "HTTP/1.1 101 Switching Protocols\r\n"
  38. "Server: nginx/1.24.0\r\n"
  39. "Upgrade: websocket\r\n"
  40. "Connection: Upgrade\r\n"
  41. "X-Proxy-Agent: Gemini-Ultra-Robust-v3\r\n\r\n";
  42. // Mutexes para sincronización
  43. pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
  44. pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER;
  45. int active_connections = 0;
  46. typedef struct {
  47. int client_fd;
  48. struct sockaddr_in addr;
  49. } client_data_t;
  50. // --- FUNCIÓN DE LOGS (ESTILO V6 TLS) ---
  51. void write_log(const char *ip, const char *msg) {
  52. pthread_mutex_lock(&log_mutex);
  53. FILE *f = fopen(LOG_FILE, "a");
  54. if (f) {
  55. time_t now = time(NULL);
  56. struct tm *t = localtime(&now);
  57. char ts[64];
  58. strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", t);
  59. if (ip) {
  60. fprintf(f, "[%s] [%s] %s\n", ts, ip, msg);
  61. printf("[%s] [%s] %s\n", ts, ip, msg);
  62. } else {
  63. fprintf(f, "[%s] %s\n", ts, msg);
  64. printf("[%s] %s\n", ts, msg);
  65. }
  66. fclose(f);
  67. }
  68. pthread_mutex_unlock(&log_mutex);
  69. }
  70. // --- MANEJADOR DE CONEXIÓN ---
  71. void *connection_handler(void *arg) {
  72. client_data_t *data = (client_data_t *)arg;
  73. int client_sock = data->client_fd;
  74. char client_ip[INET_ADDRSTRLEN];
  75. inet_ntop(AF_INET, &(data->addr.sin_addr), client_ip, INET_ADDRSTRLEN);
  76. free(data);
  77. struct timeval tv;
  78. tv.tv_sec = 3; tv.tv_usec = 0;
  79. setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  80. char buffer[BUFLEN];
  81. int bytes_read = recv(client_sock, buffer, sizeof(buffer) - 1, 0);
  82. int target_sock = -1;
  83. long tx_bytes = 0, rx_bytes = 0;
  84. if (bytes_read > 0) {
  85. buffer[bytes_read] = '\0';
  86. // Conectar a SSH local
  87. struct sockaddr_in t_addr;
  88. target_sock = socket(AF_INET, SOCK_STREAM, 0);
  89. t_addr.sin_family = AF_INET;
  90. t_addr.sin_port = htons(SSH_PORT);
  91. inet_pton(AF_INET, SSH_HOST, &t_addr.sin_addr);
  92. if (connect(target_sock, (struct sockaddr *)&t_addr, sizeof(t_addr)) < 0) {
  93. write_log(client_ip, "❌ Error: No se pudo conectar al SSH local");
  94. goto cleanup;
  95. }
  96. if (strncmp(buffer, "SSH-", 4) == 0) {
  97. write_log(client_ip, "✅ Túnel cifrado (Modo SSH Directo)");
  98. send(target_sock, buffer, bytes_read, 0);
  99. rx_bytes += bytes_read;
  100. } else if (strstr(buffer, "HTTP/") != NULL && strstr(buffer, "Upgrade: websocket") == NULL) {
  101. write_log(client_ip, "🕵️ Escáner detectado. Respondiendo 400 Bad Request Fake Web.");
  102. send(client_sock, FAKE_WEB_RESPONSE, strlen(FAKE_WEB_RESPONSE), 0);
  103. goto cleanup;
  104. } else {
  105. write_log(client_ip, "✅ Túnel cifrado (Modo WebSocket HTTP): Gemini-V3");
  106. send(client_sock, HTTP_101_RESPONSE, strlen(HTTP_101_RESPONSE), 0);
  107. tx_bytes += strlen(HTTP_101_RESPONSE);
  108. char *h_end = strstr(buffer, "\r\n\r\n");
  109. if (h_end) {
  110. int h_len = (h_end - buffer) + 4;
  111. if (bytes_read > h_len) {
  112. send(target_sock, buffer + h_len, bytes_read - h_len, 0);
  113. rx_bytes += (bytes_read - h_len);
  114. }
  115. }
  116. }
  117. } else {
  118. // Posible Stunnel o NetMod Silencioso
  119. struct sockaddr_in t_addr;
  120. target_sock = socket(AF_INET, SOCK_STREAM, 0);
  121. t_addr.sin_family = AF_INET;
  122. t_addr.sin_port = htons(SSH_PORT);
  123. inet_pton(AF_INET, SSH_HOST, &t_addr.sin_addr);
  124. if (connect(target_sock, (struct sockaddr *)&t_addr, sizeof(t_addr)) == 0) {
  125. write_log(client_ip, "✅ Túnel cifrado (Modo Stunnel Silencioso)");
  126. } else {
  127. goto cleanup;
  128. }
  129. }
  130. // --- PUENTE DE DATOS ---
  131. tv.tv_sec = 0;
  132. setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  133. int max_fd = (client_sock > target_sock) ? client_sock : target_sock;
  134. while (1) {
  135. fd_set fds;
  136. FD_ZERO(&fds);
  137. FD_SET(client_sock, &fds);
  138. FD_SET(target_sock, &fds);
  139. struct timeval sel_tv = {300, 0};
  140. if (select(max_fd + 1, &fds, NULL, NULL, &sel_tv) <= 0) break;
  141. if (FD_ISSET(client_sock, &fds)) {
  142. int b = recv(client_sock, buffer, BUFLEN, 0);
  143. if (b <= 0) break;
  144. send(target_sock, buffer, b, 0);
  145. rx_bytes += b;
  146. }
  147. if (FD_ISSET(target_sock, &fds)) {
  148. int b = recv(target_sock, buffer, BUFLEN, 0);
  149. if (b <= 0) break;
  150. send(client_sock, buffer, b, 0);
  151. tx_bytes += b;
  152. }
  153. }
  154. cleanup:
  155. // Log de tráfico final
  156. double total_mb = (double)(tx_bytes + rx_bytes) / (1024.0 * 1024.0);
  157. if (total_mb > 0.01) {
  158. char stat_msg[128];
  159. snprintf(stat_msg, sizeof(stat_msg), "[*] Conexión finalizada. Tráfico consumido: %.2f MB", total_mb);
  160. write_log(client_ip, stat_msg);
  161. }
  162. if (target_sock != -1) close(target_sock);
  163. close(client_sock);
  164. pthread_mutex_lock(&conn_mutex);
  165. active_connections--;
  166. pthread_mutex_unlock(&conn_mutex);
  167. pthread_exit(NULL);
  168. }
  169. int main(int argc, char **argv) {
  170. int port = DEFAULT_PORT;
  171. if (argc > 1) port = atoi(argv[1]);
  172. signal(SIGPIPE, SIG_IGN);
  173. int s_sock = socket(AF_INET, SOCK_STREAM, 0);
  174. int opt = 1;
  175. setsockopt(s_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  176. struct sockaddr_in s_addr;
  177. s_addr.sin_family = AF_INET;
  178. s_addr.sin_addr.s_addr = INADDR_ANY;
  179. s_addr.sin_port = htons(port);
  180. if (bind(s_sock, (struct sockaddr *)&s_addr, sizeof(s_addr)) < 0) {
  181. perror("Error Bind");
  182. exit(1);
  183. }
  184. listen(s_sock, 500);
  185. write_log(NULL, "=====================================================");
  186. char start_msg[128];
  187. snprintf(start_msg, sizeof(start_msg), "🚀 PROXY NATIVO V3 (TCP) INICIADO - PUERTO %d", port);
  188. write_log(NULL, start_msg);
  189. write_log(NULL, "🛡️ Backlog: 500 | Fake-Web: Nginx 1.24 | Logs: ACTIVADOS");
  190. write_log(NULL, "=====================================================");
  191. while (1) {
  192. struct sockaddr_in c_addr;
  193. socklen_t c_len = sizeof(c_addr);
  194. int c_sock = accept(s_sock, (struct sockaddr *)&c_addr, &c_len);
  195. if (c_sock < 0) continue;
  196. pthread_mutex_lock(&conn_mutex);
  197. if (active_connections >= MAX_CONNECTIONS) {
  198. pthread_mutex_unlock(&conn_mutex);
  199. close(c_sock);
  200. continue;
  201. }
  202. active_connections++;
  203. pthread_mutex_unlock(&conn_mutex);
  204. client_data_t *d = malloc(sizeof(client_data_t));
  205. d->client_fd = c_sock;
  206. d->addr = c_addr;
  207. pthread_t tid;
  208. pthread_attr_t attr;
  209. pthread_attr_init(&attr);
  210. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  211. if (pthread_create(&tid, &attr, connection_handler, d) != 0) {
  212. close(c_sock);
  213. free(d);
  214. pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex);
  215. }
  216. pthread_attr_destroy(&attr);
  217. }
  218. close(s_sock);
  219. return 0;
  220. }