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
| #include <arpa/inet.h> #include <netdb.h> #include <netinet/in.h> #include <sys/socket.h> #include <unistd.h>
#include <cstring> #include <iostream> #include <sstream> #include <string> #include <thread>
#define PORT 8888 #define BUF_SIZE 8192
bool parse_request(const std::string& request, std::string& host, int& port, std::string& path) { std::istringstream iss(request); std::string method, url, version; iss >> method >> url >> version;
if (url.substr(0, 7) == "http://") { url = url.substr(7); }
size_t pos = url.find('/'); std::string host_port = (pos == std::string::npos) ? url : url.substr(0, pos); path = (pos == std::string::npos) ? "/" : url.substr(pos);
size_t colon = host_port.find(':'); if (colon != std::string::npos) { host = host_port.substr(0, colon); port = std::stoi(host_port.substr(colon + 1)); } else { host = host_port; port = 80; }
return true; }
int connect_remote(const std::string& host, int port) { struct hostent* he = gethostbyname(host.c_str()); if (!he) { std::cerr << "DNS resole failed for host: " << host << std::endl; return -1; }
int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) return -1;
struct sockaddr_in addr{}; addr.sin_family = AF_INET; addr.sin_port = htons(port); memcpy(&addr.sin_addr, he->h_addr, he->h_length);
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { close(sock); return -1; }
return sock; }
void relay_data(int src, int dst) { char buffer[BUF_SIZE]; ssize_t n; while ((n = recv(src, buffer, BUF_SIZE, 0)) > 0) { send(dst, buffer, n, 0); } }
void handle_client(int client_sock) { char buffer[BUF_SIZE]; ssize_t n = recv(client_sock, buffer, BUF_SIZE - 1, 0); if (n < 0) { close(client_sock); return; } buffer[n] = '\0'; std::string request(buffer);
std::string host, path; int port; parse_request(request, host, port, path);
std::cout << "[INFO] Client requested " << host << ":" << port << path << std::endl;
int remote_sock = connect_remote(host, port); if (remote_sock < 0) { std::cerr << "[ERROR] Cannot connect to remote server" << std::endl; close(client_sock); return; }
size_t first_space = request.find(' '); size_t second_space = request.find(' ', first_space + 1); if (first_space != std::string::npos && second_space != std::string::npos) { std::string new_req = request.substr(0, first_space + 1) + path + request.substr(second_space); send(remote_sock, new_req.c_str(), new_req.size(), 0); } else { send(remote_sock, request.c_str(), request.size(), 0); }
relay_data(remote_sock, client_sock);
close(remote_sock); close(client_sock); }
int main() { int listenfd = socket(AF_INET, SOCK_STREAM, 0); if (listenfd < 0) { perror("socket"); return 1; }
int opt = 1; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); struct sockaddr_in addr{}; addr.sin_family = AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = INADDR_ANY;
if (bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { perror("bind"); return 1; }
if (listen(listenfd, 10) < 0) { perror("listen"); return 1; }
std::cout << "[INFO] Proxy server listening on port " << PORT << "..." << std::endl;
while (true) { struct sockaddr_in client_addr{}; socklen_t len = sizeof(client_addr); int connfd = accept(listenfd, (struct sockaddr*)&client_addr, &len); if (connfd < 0) continue; std::thread(handle_client, connfd).detach(); }
close(listenfd); return 0; }
|