1

Topic: HTTPS-proxy

Good afternoon! Prompt please, at me such situation, wrote the HTTP\HTTPS the proxy server, added listening port on 127.0.0.1:8080 address in system adjustments of proxy Windows. Accordingly this proxy by default use IE, Chrome and Opera, only in FF it is necessary to register it manually. At once I will make a reservation, HTTP the problem works normally, and here with HTTPS. I will short describe algorithm of my actions: I Create listening socket In a cycle I wait a connection from the browser I Accept a connection I Create a new flow and I transfer it a new socket as parameter I Accept in a locking mode the data from browser CONNECT example.com:443 HTTP/1.1 Host: example.com:443 Connection: keep-alive User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 Parsju this packet also I pull out from it host name and port I Create a new socket and I incorporate to this server I Send to the browser answer HTTP/1.1 200 Connection Established I Transport two connections in a non-blocking mode I Start to drive packets between  and a Web server, thus in the ciphered data I do not climb, simply I transfer packets from the browser to the server and it is reverse. All problem that when I send to the browser answer HTTP/1.1 200 Connection Established\r\n\r\n it breaks connection, and Chrome in particular produces this error - ERR_TUNNEL_CONNECTION_FAILED. Thought can that with system, but delivered the same SmallProxy and it perfectly works. Looked  its answer, it transfers the same as I. Prompt because of what the browser tears a connection.   for the help! DWORD WINAPI session_thread (LPVOID session_connect) {client_struct *conn = session_connect; request_struct *request = NULL; response_struct *response = NULL; proxy_struct *proxy = NULL; fd_set read_fds; LPVOID proxy_buffer = NULL; LPVOID request_buffer = NULL; LPVOID response_buffer = NULL; DWORD dwResult = 0; LONG from_timeout = BROWSER_TIMEOUT; LONG to_timeout = SERVER_TIMEOUT; LONG nbio = TRUE; LONG nRead = 0; LONG nSent = 0; LONG nProxySize = 0; LONG nReqSize = 0; LONG nRespSize = 0; BOOL bFlag = FALSE; BOOL is_ssl = FALSE; CHAR from_buff [MAX_PACKET_SIZE]; CHAR to_buff [MAX_PACKET_SIZE]; CHAR szResponse [] = "HTTP/1.1 200 Connection Established\r\n\r\n"; if (setsockopt (conn-> fsock, SOL_SOCKET, SO_RCVTIMEO, (PCHAR) &from_timeout, sizeof (LONG))) {goto cleanup;} ZeroMemory (&from_buff, sizeof (from_buff)); do {nRead = recv (conn-> fsock, &from_buff, MAX_PACKET_SIZE, 0); if (nRead> 0) {if (! bFlag) {proxy_buffer = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, nRead); if (proxy_buffer == NULL) {goto cleanup;} bFlag = TRUE;} else {proxy_buffer = HeapReAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, proxy_buffer, nRead + nProxySize); if (proxy_buffer == NULL) {goto cleanup;}} CopyMemory (proxy_buffer + nProxySize, &from_buff, nRead); nProxySize = nProxySize + nRead;}} while (nRead> 0); if (nRead) {proxy = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof (proxy_struct)); proxy-> header = proxy_buffer; proxy-> header_size = nProxySize; dwResult = proxy_handler (proxy); if (! dwResult) {goto cleanup;}} else {goto cleanup;} CopyMemory (&conn->to, &proxy->sa, sizeof (struct sockaddr_in)); if ((conn-> tsock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {goto cleanup;} if (connect (conn-> tsock, (struct sockaddr *) &conn->to, sizeof (struct sockaddr_in)) == SOCKET_ERROR) {goto cleanup;} if (ntohs (conn-> to.sin_port) %2! = 0) {is_ssl = TRUE;} if (is_ssl) {send (conn-> fsock, &szResponse, sizeof (szResponse), 0);} else {send (conn-> tsock, proxy_buffer, nProxySize, 0);} if (ioctlsocket (conn-> tsock, FIONBIO, &nbio) == SOCKET_ERROR) {goto cleanup;} if (ioctlsocket (conn-> fsock, FIONBIO, &nbio) == SOCKET_ERROR) {goto cleanup;} for (; wink {FD_ZERO (&read_fds); FD_SET (conn-> tsock, &read_fds); FD_SET (conn-> fsock, &read_fds); if (select (0, &read_fds, NULL, NULL, NULL) == SOCKET_ERROR) {goto cleanup;} if (FD_ISSET (conn-> fsock, &read_fds)) {if ((nRead = recv (conn-> fsock, &from_buff, MAX_PACKET_SIZE, 0))> 0) {nSent = send (conn-> tsock, &from_buff, nRead, 0); if (nSent <0 && ((WSAGetLastError ()! = WSAEWOULDBLOCK))) {goto cleanup;}} else {goto cleanup;}} if (FD_ISSET (conn-> tsock, &read_fds)) {if ((nRead = recv (conn-> tsock, &to_buff, MAX_PACKET_SIZE, 0))> 0) {nSent = send (conn-> fsock, &to_buff, nRead, 0); if (nSent <0 && ((WSAGetLastError ()! = WSAEWOULDBLOCK))) {goto cleanup;}} else {goto cleanup;} }} cleanup: if (conn-> fsock) shutdown (conn-> fsock, SD_BOTH); if (conn-> fsock) closesocket (conn-> fsock); if (conn-> tsock) shutdown (conn-> tsock, SD_BOTH); if (conn-> tsock) closesocket (conn-> tsock); if (request_buffer) HeapFree (GetProcessHeap (), 0, request_buffer); if (response_buffer) HeapFree (GetProcessHeap (), 0, response_buffer); if (proxy_buffer) HeapFree (GetProcessHeap (), 0, proxy_buffer); if (request) HeapFree (GetProcessHeap (), 0, request); if (response) HeapFree (GetProcessHeap (), 0, response); if (proxy) HeapFree (GetProcessHeap (), 0, proxy); if (session_connect) HeapFree (GetProcessHeap (), 0, session_connect); return 0;}