1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> #include <windows.h> #include <ws2tcpip.h>
#define BUF_SIZE 8192 #define LISTEN_PORT 8080
char* strcasestr_local(const char* haystack, const char* needle) { if (!haystack || !needle) return NULL;
size_t needle_len = strlen(needle); if (needle_len == 0) return (char*)haystack;
for (; *haystack; haystack++) { if (_strnicmp(haystack, needle, needle_len) == 0) return (char*)haystack; } return NULL; }
int init_winsock() { WSADATA wsaData; return WSAStartup(MAKEWORD(2, 2), &wsaData); }
typedef struct { SOCKET client_sock; SOCKET remote_sock; } SocketPair;
DWORD WINAPI forward_data(void* arg) { SocketPair* sockets = (SocketPair*)arg; SOCKET src = sockets->client_sock; SOCKET dst = sockets->remote_sock;
char buf[BUF_SIZE]; int n;
while ((n = recv(src, buf, BUF_SIZE, 0)) > 0) { int sent = 0; while (sent < n) { int r = send(dst, buf + sent, n - sent, 0); if (r <= 0) goto out; sent += r; } }
out: free(sockets); return 0; }
int parse_http_request(SOCKET client, char* host, int host_size, int* port, int* is_https, char* path, int path_size) { char buffer[BUF_SIZE]; int total = 0, n;
while ((n = recv(client, buffer + total, BUF_SIZE - total - 1, 0)) > 0) { total += n; buffer[total] = '\0'; if (strstr(buffer, "\r\n\r\n")) break; }
if (n <= 0) return -1;
if (strncmp(buffer, "CONNECT ", 8) == 0) { *is_https = 1; *port = 443;
if (sscanf(buffer, "CONNECT %255[^:]:%d", host, port) == 2) return 0;
return -1; }
*is_https = 0; *port = 80;
char method[16]; if (sscanf(buffer, "%15s %255s", method, path) != 2) return -1;
char* host_hdr = strcasestr_local(buffer, "Host:"); if (!host_hdr) return -1;
host_hdr += 5; while (*host_hdr == ' ' || *host_hdr == '\t') host_hdr++;
char tmp[256]; if (sscanf(host_hdr, "%255s", tmp) != 1) return -1;
char* colon = strchr(tmp, ':'); if (colon) { *colon = '\0'; *port = atoi(colon + 1); }
strncpy(host, tmp, host_size - 1); host[host_size - 1] = '\0';
return 0; }
DWORD WINAPI handle_client_thread(LPVOID param) { SOCKET client = (SOCKET)param;
char host[256] = {0}; char path[512] = "/"; int port = 0, is_https = 0;
if (parse_http_request(client, host, sizeof(host), &port, &is_https, path, sizeof(path)) < 0) { fprintf(stderr, "Failed to parse request\n"); closesocket(client); return 0; }
printf("Connecting to %s:%d (https=%d)\n", host, port, is_https);
SOCKET remote = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (remote == INVALID_SOCKET) { fprintf(stderr, "Socket creation failed\n"); closesocket(client); return 0; }
struct addrinfo hints = {0}, *res = NULL; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM;
char port_str[16]; sprintf(port_str, "%d", port);
if (getaddrinfo(host, port_str, &hints, &res) != 0) { fprintf(stderr, "DNS resolve failed: %s\n", host); closesocket(client); closesocket(remote); return 0; }
if (connect(remote, res->ai_addr, res->ai_addrlen) == SOCKET_ERROR) { fprintf(stderr, "Connect failed\n"); freeaddrinfo(res); closesocket(client); closesocket(remote); return 0; } freeaddrinfo(res);
if (is_https) { send(client, "HTTP/1.1 200 Connection Established\r\n\r\n", 40, 0); } else { char req[BUF_SIZE]; int len = snprintf(req, BUF_SIZE, "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host); send(remote, req, len, 0); }
SocketPair* p1 = malloc(sizeof(SocketPair)); SocketPair* p2 = malloc(sizeof(SocketPair)); p1->client_sock = client; p1->remote_sock = remote; p2->client_sock = remote; p2->remote_sock = client;
HANDLE t1 = CreateThread(NULL, 0, forward_data, p1, 0, NULL); HANDLE t2 = CreateThread(NULL, 0, forward_data, p2, 0, NULL);
WaitForSingleObject(t1, INFINITE); WaitForSingleObject(t2, INFINITE);
CloseHandle(t1); CloseHandle(t2);
closesocket(client); closesocket(remote); return 0; }
int main() { init_winsock();
SOCKET server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in addr = {0}; addr.sin_family = AF_INET; addr.sin_port = htons(LISTEN_PORT); addr.sin_addr.s_addr = INADDR_ANY;
bind(server, (struct sockaddr*)&addr, sizeof(addr)); listen(server, 10);
printf("Proxy listening on %d...\n", LISTEN_PORT);
while (1) { struct sockaddr_in cli; int len = sizeof(cli); SOCKET client = accept(server, (struct sockaddr*)&cli, &len);
HANDLE t = CreateThread(NULL, 0, handle_client_thread, (LPVOID)client, 0, NULL); CloseHandle(t); }
return 0; }
|