proxy_dual.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * =====================================================================================
  3. * PROXY VPN DUAL (TCP + TLS) - GRADO ENTERPRISE NATIVO EN C
  4. * Mejoras: Soporte DUAL-STACK (IPv4 e IPv6 simultáneo en el mismo puerto).
  5. * Compilación: gcc -O3 -o proxy_dual proxy_dual.c -lssl -lcrypto -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 <openssl/ssl.h>
  19. #include <openssl/err.h>
  20. // --- CONFIGURACIÓN BASE ---
  21. #define DEFAULT_PORT_TCP 80
  22. #define DEFAULT_PORT_TLS 443
  23. #define SSH_HOST "127.0.0.1"
  24. #define SSH_PORT 22
  25. #define BUFLEN 16384
  26. #define MAX_CONNECTIONS 500
  27. #define LOG_FILE "/root/proxy-dual.log"
  28. #define CERT_FILE "/root/cert.pem"
  29. #define KEY_FILE "/root/key.pem"
  30. 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";
  31. const char *FAKE_WEB_TLS = "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";
  32. const char *MENSAJES[] = {"🚀 CONEXION ESTABLECIDA", "🛡️ CIFRADO MILITAR ACTIVO", "Pfsense", "OPNsense", "VyOS", "Claro", "Altice", "🌐 BYPASS OK"};
  33. #define NUM_MENSAJES (sizeof(MENSAJES) / sizeof(MENSAJES[0]))
  34. int mensaje_idx = 0;
  35. pthread_mutex_t msg_mutex = PTHREAD_MUTEX_INITIALIZER;
  36. int active_connections = 0;
  37. pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER;
  38. pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
  39. // ESTRUCTURA MODIFICADA: Usamos sockaddr_storage para soportar tanto IPv4 como IPv6
  40. typedef struct {
  41. int client_fd;
  42. struct sockaddr_storage addr;
  43. int is_tls;
  44. SSL_CTX *ssl_ctx;
  45. } client_data_t;
  46. void write_log(const char *ip, const char *proto, const char *msg) {
  47. pthread_mutex_lock(&log_mutex);
  48. FILE *f = fopen(LOG_FILE, "a");
  49. if (f) {
  50. time_t now = time(NULL);
  51. struct tm *t = localtime(&now);
  52. char ts[64];
  53. strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", t);
  54. fprintf(f, "[%s] [%s] [%s] %s\n", ts, proto, ip ? ip : "SISTEMA", msg);
  55. printf("[%s] [%s] [%s] %s\n", ts, proto, ip ? ip : "SISTEMA", msg);
  56. fclose(f);
  57. }
  58. pthread_mutex_unlock(&log_mutex);
  59. }
  60. SSL_CTX *create_ssl_context() {
  61. SSL_load_error_strings();
  62. OpenSSL_add_ssl_algorithms();
  63. SSL_CTX *ctx = SSL_CTX_new(TLS_server_method());
  64. if (!ctx) { exit(1); }
  65. SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM);
  66. SSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM);
  67. return ctx;
  68. }
  69. // CREACIÓN DE SOCKET DUAL-STACK
  70. int create_server_socket(int port) {
  71. // 1. Crear socket IPv6 (AF_INET6)
  72. int s_sock = socket(AF_INET6, SOCK_STREAM, 0);
  73. if (s_sock < 0) return -1;
  74. int opt = 1;
  75. setsockopt(s_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  76. // 2. MAGIA DUAL-STACK: Apagar la restricción de solo-IPv6
  77. int no = 0;
  78. setsockopt(s_sock, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));
  79. // 3. Bind a in6addr_any (Equivale a "::")
  80. struct sockaddr_in6 addr;
  81. memset(&addr, 0, sizeof(addr));
  82. addr.sin6_family = AF_INET6;
  83. addr.sin6_addr = in6addr_any;
  84. addr.sin6_port = htons(port);
  85. if (bind(s_sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) return -1;
  86. listen(s_sock, 600);
  87. return s_sock;
  88. }
  89. void *connection_handler(void *arg) {
  90. client_data_t *data = (client_data_t *)arg;
  91. int client_sock = data->client_fd;
  92. int is_tls = data->is_tls;
  93. SSL_CTX *ctx = data->ssl_ctx;
  94. // EXTRAER IP (IPv4 o IPv6)
  95. char client_ip[INET6_ADDRSTRLEN];
  96. if (data->addr.ss_family == AF_INET) {
  97. struct sockaddr_in *s = (struct sockaddr_in *)&data->addr;
  98. inet_ntop(AF_INET, &s->sin_addr, client_ip, sizeof(client_ip));
  99. } else { // AF_INET6
  100. struct sockaddr_in6 *s = (struct sockaddr_in6 *)&data->addr;
  101. inet_ntop(AF_INET6, &s->sin6_addr, client_ip, sizeof(client_ip));
  102. }
  103. free(data);
  104. const char *proto_name = is_tls ? "TLS" : "TCP";
  105. SSL *ssl = NULL;
  106. if (is_tls) {
  107. ssl = SSL_new(ctx);
  108. SSL_set_fd(ssl, client_sock);
  109. if (SSL_accept(ssl) <= 0) {
  110. SSL_free(ssl); close(client_sock);
  111. pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex);
  112. pthread_exit(NULL);
  113. }
  114. }
  115. struct timeval tv = {3, 0};
  116. setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  117. char buffer[BUFLEN];
  118. int bytes_read = is_tls ? SSL_read(ssl, buffer, sizeof(buffer)-1) : recv(client_sock, buffer, sizeof(buffer)-1, 0);
  119. int target_sock = -1;
  120. long tx_bytes = 0, rx_bytes = 0;
  121. if (bytes_read > 0) {
  122. buffer[bytes_read] = '\0';
  123. struct sockaddr_in t_addr;
  124. target_sock = socket(AF_INET, SOCK_STREAM, 0);
  125. t_addr.sin_family = AF_INET;
  126. t_addr.sin_port = htons(SSH_PORT);
  127. inet_pton(AF_INET, SSH_HOST, &t_addr.sin_addr);
  128. if (connect(target_sock, (struct sockaddr *)&t_addr, sizeof(t_addr)) < 0) goto cleanup;
  129. if (strncmp(buffer, "SSH-", 4) == 0) {
  130. send(target_sock, buffer, bytes_read, 0);
  131. } else if (strstr(buffer, "HTTP/") != NULL && strstr(buffer, "Upgrade: websocket") == NULL) {
  132. if (is_tls) SSL_write(ssl, FAKE_WEB_TLS, strlen(FAKE_WEB_TLS));
  133. else send(client_sock, FAKE_WEB_TCP, strlen(FAKE_WEB_TCP), 0);
  134. goto cleanup;
  135. } else {
  136. pthread_mutex_lock(&msg_mutex);
  137. const char *status_msg = MENSAJES[mensaje_idx];
  138. mensaje_idx = (mensaje_idx + 1) % NUM_MENSAJES;
  139. pthread_mutex_unlock(&msg_mutex);
  140. char response[1024];
  141. snprintf(response, sizeof(response),
  142. "HTTP/1.1 101 %s\r\nServer: nginx/1.24.0\r\nX-Proxy-Agent: Gemini-Ultra-Dual\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n", status_msg);
  143. if (is_tls) SSL_write(ssl, response, strlen(response));
  144. else send(client_sock, response, strlen(response), 0);
  145. }
  146. } else {
  147. struct sockaddr_in t_addr;
  148. target_sock = socket(AF_INET, SOCK_STREAM, 0);
  149. t_addr.sin_family = AF_INET;
  150. t_addr.sin_port = htons(SSH_PORT);
  151. inet_pton(AF_INET, SSH_HOST, &t_addr.sin_addr);
  152. if (connect(target_sock, (struct sockaddr *)&t_addr, sizeof(t_addr)) != 0) goto cleanup;
  153. }
  154. tv.tv_sec = 0;
  155. setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  156. int max_fd = (client_sock > target_sock) ? client_sock : target_sock;
  157. while (1) {
  158. fd_set readfds; FD_ZERO(&readfds); FD_SET(client_sock, &readfds); FD_SET(target_sock, &readfds);
  159. struct timeval select_tv = {300, 0};
  160. int pending = is_tls ? SSL_pending(ssl) : 0;
  161. if (pending == 0) {
  162. if (select(max_fd + 1, &readfds, NULL, NULL, &select_tv) <= 0) break;
  163. }
  164. if (pending > 0 || FD_ISSET(client_sock, &readfds)) {
  165. int b = is_tls ? SSL_read(ssl, buffer, BUFLEN) : recv(client_sock, buffer, BUFLEN, 0);
  166. if (b <= 0) break;
  167. send(target_sock, buffer, b, 0);
  168. rx_bytes += b;
  169. }
  170. if (FD_ISSET(target_sock, &readfds)) {
  171. int b = recv(target_sock, buffer, BUFLEN, 0);
  172. if (b <= 0) break;
  173. if (is_tls) SSL_write(ssl, buffer, b);
  174. else send(client_sock, buffer, b, 0);
  175. tx_bytes += b;
  176. }
  177. }
  178. cleanup:
  179. if (target_sock != -1) close(target_sock);
  180. if (is_tls) { SSL_shutdown(ssl); SSL_free(ssl); }
  181. close(client_sock);
  182. pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex);
  183. pthread_exit(NULL);
  184. }
  185. int main(int argc, char **argv) {
  186. int port_tcp = DEFAULT_PORT_TCP, port_tls = DEFAULT_PORT_TLS;
  187. if (argc >= 3) { port_tcp = atoi(argv[1]); port_tls = atoi(argv[2]); }
  188. signal(SIGPIPE, SIG_IGN);
  189. SSL_CTX *ctx = create_ssl_context();
  190. int server_tcp = create_server_socket(port_tcp);
  191. int server_tls = create_server_socket(port_tls);
  192. if (server_tcp < 0 || server_tls < 0) exit(1);
  193. write_log(NULL, "SISTEMA", "🚀 PROXY DUAL ENTERPRISE INICIADO (Soporte IPv4 + IPv6)");
  194. int max_server_fd = (server_tcp > server_tls) ? server_tcp : server_tls;
  195. while (1) {
  196. fd_set master_fds; FD_ZERO(&master_fds); FD_SET(server_tcp, &master_fds); FD_SET(server_tls, &master_fds);
  197. if (select(max_server_fd + 1, &master_fds, NULL, NULL, NULL) < 0) continue;
  198. int active_sock = -1, is_tls = 0;
  199. if (FD_ISSET(server_tcp, &master_fds)) { active_sock = server_tcp; is_tls = 0; }
  200. else if (FD_ISSET(server_tls, &master_fds)) { active_sock = server_tls; is_tls = 1; }
  201. struct sockaddr_storage c_addr;
  202. socklen_t c_len = sizeof(c_addr);
  203. int client_sock = accept(active_sock, (struct sockaddr *)&c_addr, &c_len);
  204. if (client_sock < 0) continue;
  205. pthread_mutex_lock(&conn_mutex);
  206. if (active_connections >= MAX_CONNECTIONS) { pthread_mutex_unlock(&conn_mutex); close(client_sock); continue; }
  207. active_connections++; pthread_mutex_unlock(&conn_mutex);
  208. client_data_t *d = malloc(sizeof(client_data_t));
  209. d->client_fd = client_sock; d->addr = c_addr; d->is_tls = is_tls; d->ssl_ctx = ctx;
  210. pthread_t tid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  211. if (pthread_create(&tid, &attr, connection_handler, d) != 0) {
  212. close(client_sock); free(d);
  213. pthread_mutex_lock(&conn_mutex); active_connections--; pthread_mutex_unlock(&conn_mutex);
  214. }
  215. pthread_attr_destroy(&attr);
  216. }
  217. return 0;
  218. }