proxy_dual.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * =====================================================================================
  3. * PROXY VPN DUAL (TCP + TLS) - ULTIMATE BARE-METAL EDITION V10
  4. * Correcciones Finales:
  5. * 1. Anti-Flood reprogramado: Reseteo por segundo para permitir Navegadores/VPNs.
  6. * 2. SSL_pending afinado para conexiones SSH directas.
  7. * Compilación: gcc -O3 -o proxy_dual proxy_dual.c -lssl -lcrypto -lpthread
  8. * =====================================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <arpa/inet.h>
  15. #include <sys/socket.h>
  16. #include <sys/select.h>
  17. #include <pthread.h>
  18. #include <signal.h>
  19. #include <time.h>
  20. #include <openssl/ssl.h>
  21. #include <openssl/err.h>
  22. // --- CONFIGURACIÓN BASE ---
  23. #define DEFAULT_PORT_TCP 80
  24. #define DEFAULT_PORT_TLS 443
  25. #define SSH_HOST "127.0.0.1"
  26. #define SSH_PORT 22
  27. #define BUFLEN 16384
  28. #define MAX_CONNECTIONS 500
  29. #define LOG_FILE "/root/proxy-dual.log"
  30. #define CERT_FILE "/root/cert.pem"
  31. #define KEY_FILE "/root/key.pem"
  32. // --- SEGURIDAD ANTI-FLOOD (Afiliado a la perfección) ---
  33. #define MAX_TRACKED_IPS 200
  34. #define AUTO_BAN_STRIKES 20 // 20 conexiones permitidas POR SEGUNDO
  35. #define BAN_TIME 3600 // Castigo de 1 hora
  36. typedef struct {
  37. char ip[INET6_ADDRSTRLEN];
  38. time_t last_connect;
  39. int strikes;
  40. time_t ban_until;
  41. } ip_record_t;
  42. ip_record_t ip_database[MAX_TRACKED_IPS];
  43. pthread_mutex_t ip_db_mutex = PTHREAD_MUTEX_INITIALIZER;
  44. // --- RESPUESTAS FAKE WEB ---
  45. const char *FAKE_WEB_TCP = "HTTP/1.1 400 Bad Request\r\nServer: nginx/1.24.0\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n<html><body><center><h1>400 Bad Request</h1></center><hr><center>nginx/1.24.0</center></body></html>\r\n";
  46. const char *FAKE_WEB_TLS = "HTTP/1.1 400 OK\r\nServer: nginx/1.21.0\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n<html><body><center><h1>400 Bad Request</h1></center></body></html>\r\n";
  47. const char *MENSAJES[] = {"🚀 CONEXION ESTABLECIDA", "🛡️ CIFRADO MILITAR ACTIVO", "🔋 MODO SIGILO SSL OK", "Pfsense", "OPNsense", "VyOS", "Claro", "Google", "TNSR", "🌐 BYPASS OK"};
  48. #define NUM_MENSAJES (sizeof(MENSAJES) / sizeof(MENSAJES[0]))
  49. int mensaje_idx = 0;
  50. pthread_mutex_t msg_mutex = PTHREAD_MUTEX_INITIALIZER;
  51. int active_connections = 0;
  52. pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER;
  53. pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
  54. typedef struct {
  55. int client_fd;
  56. struct sockaddr_storage addr;
  57. int is_tls;
  58. SSL_CTX *ssl_ctx;
  59. } client_data_t;
  60. // --- REGISTRO LOG ---
  61. void write_log(const char *ip, const char *proto, const char *msg) {
  62. pthread_mutex_lock(&log_mutex);
  63. FILE *f = fopen(LOG_FILE, "a");
  64. if (f) {
  65. time_t now = time(NULL);
  66. struct tm *t = localtime(&now);
  67. char ts[64];
  68. strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", t);
  69. fprintf(f, "[%s] [%s] [%s] %s\n", ts, proto, ip ? ip : "SISTEMA", msg);
  70. printf("[%s] [%s] [%s] %s\n", ts, proto, ip ? ip : "SISTEMA", msg);
  71. fclose(f);
  72. }
  73. pthread_mutex_unlock(&log_mutex);
  74. }
  75. // --- NUEVO MOTOR ANTI-FLOOD (Tolerante a Navegadores) ---
  76. int check_and_update_ip(const char *ip) {
  77. pthread_mutex_lock(&ip_db_mutex);
  78. time_t now = time(NULL);
  79. int empty_slot = -1;
  80. int found = 0;
  81. for (int i = 0; i < MAX_TRACKED_IPS; i++) {
  82. if (ip_database[i].ip[0] == '\0' && empty_slot == -1) empty_slot = i;
  83. else if (strcmp(ip_database[i].ip, ip) == 0) {
  84. found = 1;
  85. if (ip_database[i].ban_until > now) {
  86. pthread_mutex_unlock(&ip_db_mutex);
  87. return 0; // Baneado
  88. }
  89. if (now == ip_database[i].last_connect) {
  90. ip_database[i].strikes++;
  91. if (ip_database[i].strikes > AUTO_BAN_STRIKES) {
  92. ip_database[i].ban_until = now + BAN_TIME;
  93. pthread_mutex_unlock(&ip_db_mutex);
  94. return -1; // Nuevo Ban
  95. }
  96. } else {
  97. ip_database[i].strikes = 1; // Un segundo nuevo, reseteamos el contador
  98. ip_database[i].last_connect = now;
  99. }
  100. break;
  101. }
  102. }
  103. if (!found && empty_slot != -1) {
  104. strcpy(ip_database[empty_slot].ip, ip);
  105. ip_database[empty_slot].last_connect = now;
  106. ip_database[empty_slot].strikes = 1;
  107. ip_database[empty_slot].ban_until = 0;
  108. }
  109. pthread_mutex_unlock(&ip_db_mutex);
  110. return 1;
  111. }
  112. SSL_CTX *create_ssl_context() {
  113. SSL_load_error_strings();
  114. OpenSSL_add_ssl_algorithms();
  115. SSL_CTX *ctx = SSL_CTX_new(TLS_server_method());
  116. if (!ctx) exit(1);
  117. SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM);
  118. SSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM);
  119. return ctx;
  120. }
  121. int create_server_socket(int port) {
  122. int s_sock = socket(AF_INET6, SOCK_STREAM, 0);
  123. if (s_sock < 0) return -1;
  124. int opt = 1; setsockopt(s_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  125. int no = 0; setsockopt(s_sock, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));
  126. struct sockaddr_in6 addr;
  127. memset(&addr, 0, sizeof(addr));
  128. addr.sin6_family = AF_INET6;
  129. addr.sin6_addr = in6addr_any;
  130. addr.sin6_port = htons(port);
  131. if (bind(s_sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) return -1;
  132. listen(s_sock, 600);
  133. return s_sock;
  134. }
  135. void *connection_handler(void *arg) {
  136. client_data_t *data = (client_data_t *)arg;
  137. int client_sock = data->client_fd;
  138. int is_tls = data->is_tls;
  139. SSL_CTX *ctx = data->ssl_ctx;
  140. char client_ip[INET6_ADDRSTRLEN];
  141. if (data->addr.ss_family == AF_INET) {
  142. struct sockaddr_in *s = (struct sockaddr_in *)&data->addr;
  143. inet_ntop(AF_INET, &s->sin_addr, client_ip, sizeof(client_ip));
  144. } else {
  145. struct sockaddr_in6 *s = (struct sockaddr_in6 *)&data->addr;
  146. inet_ntop(AF_INET6, &s->sin6_addr, client_ip, sizeof(client_ip));
  147. }
  148. free(data);
  149. const char *proto_name = is_tls ? "TLS" : "TCP";
  150. int sec_status = check_and_update_ip(client_ip);
  151. if (sec_status == 0) {
  152. close(client_sock); pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex); pthread_exit(NULL);
  153. } else if (sec_status == -1) {
  154. write_log(client_ip, proto_name, "⛔ IP Baneada (Flood de >20 req/s)");
  155. close(client_sock); pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex); pthread_exit(NULL);
  156. }
  157. SSL *ssl = NULL;
  158. if (is_tls) {
  159. ssl = SSL_new(ctx);
  160. SSL_set_fd(ssl, client_sock);
  161. if (SSL_accept(ssl) <= 0) {
  162. SSL_free(ssl); close(client_sock);
  163. pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex);
  164. pthread_exit(NULL);
  165. }
  166. }
  167. fd_set init_fds; FD_ZERO(&init_fds); FD_SET(client_sock, &init_fds);
  168. struct timeval init_tv = {3, 0};
  169. char buffer[BUFLEN];
  170. int bytes_read = 0;
  171. int ssl_has_data = is_tls ? SSL_pending(ssl) : 0;
  172. if (ssl_has_data > 0 || select(client_sock + 1, &init_fds, NULL, NULL, &init_tv) > 0) {
  173. if (is_tls) bytes_read = SSL_read(ssl, buffer, sizeof(buffer)-1);
  174. else bytes_read = recv(client_sock, buffer, sizeof(buffer)-1, 0);
  175. }
  176. int target_sock = -1;
  177. long tx_bytes = 0, rx_bytes = 0;
  178. if (bytes_read > 0) {
  179. buffer[bytes_read] = '\0';
  180. struct sockaddr_in t_addr;
  181. target_sock = socket(AF_INET, SOCK_STREAM, 0);
  182. t_addr.sin_family = AF_INET;
  183. t_addr.sin_port = htons(SSH_PORT);
  184. inet_pton(AF_INET, SSH_HOST, &t_addr.sin_addr);
  185. if (connect(target_sock, (struct sockaddr *)&t_addr, sizeof(t_addr)) < 0) goto cleanup;
  186. if (strncmp(buffer, "SSH-", 4) == 0) {
  187. send(target_sock, buffer, bytes_read, 0);
  188. } else if (strstr(buffer, "HTTP/") != NULL && strstr(buffer, "Upgrade: websocket") == NULL) {
  189. write_log(client_ip, proto_name, "🕵️ Escáner detectado. Fake Web OK.");
  190. if (is_tls) SSL_write(ssl, FAKE_WEB_TLS, strlen(FAKE_WEB_TLS));
  191. else send(client_sock, FAKE_WEB_TCP, strlen(FAKE_WEB_TCP), 0);
  192. goto cleanup;
  193. } else {
  194. pthread_mutex_lock(&msg_mutex);
  195. const char *status_msg = MENSAJES[mensaje_idx];
  196. mensaje_idx = (mensaje_idx + 1) % NUM_MENSAJES;
  197. pthread_mutex_unlock(&msg_mutex);
  198. char response[1024];
  199. snprintf(response, sizeof(response),
  200. "HTTP/1.1 101 %s\r\n"
  201. "Server: nginx/1.24.0\r\n"
  202. "X-Forwarded-For: 127.0.0.1\r\n"
  203. "Content-Type: text/html; charset=UTF-8\r\n"
  204. "Proxy-Connection: keep-alive\r\n"
  205. "Cache-Control: no-cache\r\n"
  206. "X-Proxy-Agent: Gemini-Ultra-Dual-C\r\n"
  207. "Connection: Upgrade\r\n"
  208. "Upgrade: websocket\r\n\r\n", status_msg);
  209. if (is_tls) SSL_write(ssl, response, strlen(response));
  210. else send(client_sock, response, strlen(response), 0);
  211. }
  212. } else {
  213. struct sockaddr_in t_addr;
  214. target_sock = socket(AF_INET, SOCK_STREAM, 0);
  215. t_addr.sin_family = AF_INET;
  216. t_addr.sin_port = htons(SSH_PORT);
  217. inet_pton(AF_INET, SSH_HOST, &t_addr.sin_addr);
  218. if (connect(target_sock, (struct sockaddr *)&t_addr, sizeof(t_addr)) != 0) goto cleanup;
  219. }
  220. int max_fd = (client_sock > target_sock) ? client_sock : target_sock;
  221. while (1) {
  222. fd_set readfds; FD_ZERO(&readfds); FD_SET(client_sock, &readfds); FD_SET(target_sock, &readfds);
  223. struct timeval select_tv = {300, 0};
  224. int pending = is_tls ? SSL_pending(ssl) : 0;
  225. if (pending == 0) {
  226. if (select(max_fd + 1, &readfds, NULL, NULL, &select_tv) <= 0) break;
  227. }
  228. if (pending > 0 || FD_ISSET(client_sock, &readfds)) {
  229. int b = is_tls ? SSL_read(ssl, buffer, BUFLEN) : recv(client_sock, buffer, BUFLEN, 0);
  230. if (b <= 0) break;
  231. send(target_sock, buffer, b, 0);
  232. rx_bytes += b;
  233. }
  234. if (FD_ISSET(target_sock, &readfds)) {
  235. int b = recv(target_sock, buffer, BUFLEN, 0);
  236. if (b <= 0) break;
  237. if (is_tls) SSL_write(ssl, buffer, b);
  238. else send(client_sock, buffer, b, 0);
  239. tx_bytes += b;
  240. }
  241. }
  242. cleanup:
  243. if (target_sock != -1) close(target_sock);
  244. if (is_tls) { SSL_shutdown(ssl); SSL_free(ssl); }
  245. close(client_sock);
  246. pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex);
  247. pthread_exit(NULL);
  248. }
  249. int main(int argc, char **argv) {
  250. int port_tcp = DEFAULT_PORT_TCP, port_tls = DEFAULT_PORT_TLS;
  251. if (argc >= 3) { port_tcp = atoi(argv[1]); port_tls = atoi(argv[2]); }
  252. memset(ip_database, 0, sizeof(ip_database));
  253. signal(SIGPIPE, SIG_IGN);
  254. SSL_CTX *ctx = create_ssl_context();
  255. int server_tcp = create_server_socket(port_tcp);
  256. int server_tls = create_server_socket(port_tls);
  257. if (server_tcp < 0 || server_tls < 0) exit(1);
  258. write_log(NULL, "SISTEMA", "🚀 PROXY DUAL BARE-METAL INICIADO (Ultimate V10)");
  259. write_log(NULL, "SISTEMA", "🛡️ Módulos: Anti-Flood Tolerante, IPv4/IPv6, Multi-hilo, Headers OK");
  260. int max_server_fd = (server_tcp > server_tls) ? server_tcp : server_tls;
  261. while (1) {
  262. fd_set master_fds; FD_ZERO(&master_fds); FD_SET(server_tcp, &master_fds); FD_SET(server_tls, &master_fds);
  263. if (select(max_server_fd + 1, &master_fds, NULL, NULL, NULL) < 0) continue;
  264. int active_sock = -1, is_tls = 0;
  265. if (FD_ISSET(server_tcp, &master_fds)) { active_sock = server_tcp; is_tls = 0; }
  266. else if (FD_ISSET(server_tls, &master_fds)) { active_sock = server_tls; is_tls = 1; }
  267. struct sockaddr_storage c_addr;
  268. socklen_t c_len = sizeof(c_addr);
  269. int client_sock = accept(active_sock, (struct sockaddr *)&c_addr, &c_len);
  270. if (client_sock < 0) continue;
  271. pthread_mutex_lock(&conn_mutex);
  272. if (active_connections >= MAX_CONNECTIONS) { pthread_mutex_unlock(&conn_mutex); close(client_sock); continue; }
  273. active_connections++; pthread_mutex_unlock(&conn_mutex);
  274. client_data_t *d = malloc(sizeof(client_data_t));
  275. d->client_fd = client_sock; d->addr = c_addr; d->is_tls = is_tls; d->ssl_ctx = ctx;
  276. pthread_t tid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  277. if (pthread_create(&tid, &attr, connection_handler, d) != 0) {
  278. close(client_sock); free(d);
  279. pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex);
  280. }
  281. pthread_attr_destroy(&attr);
  282. }
  283. return 0;
  284. }