diff --git a/src/proxy/tinymitm/proxy.cpp b/src/proxy/tinymitm/proxy.cpp index 3676092..5156b16 100644 --- a/src/proxy/tinymitm/proxy.cpp +++ b/src/proxy/tinymitm/proxy.cpp @@ -254,11 +254,13 @@ bool TinyMITMProxy::init() while (_running) { SOCKET client; - std::unique_lock lock(_queueMutex); - _queueCond.wait(lock, [this]() { return !_clientQueue.empty() || !_running; }); - if (!_running && _clientQueue.empty()) return; - client = _clientQueue.front(); - _clientQueue.pop(); + { + std::unique_lock lock(_queueMutex); + _queueCond.wait(lock, [this]() { return !_clientQueue.empty() || !_running; }); + if (!_running && _clientQueue.empty()) return; + client = _clientQueue.front(); + _clientQueue.pop(); + } this->handleClient(client); } }); @@ -327,11 +329,19 @@ void TinyMITMProxy::handleClient(SOCKET clientSocket) initial CONNECT peek */ int n = recv(clientGuard, buf, TINYMITM_CLIENT_BUFF_SIZE - 1, 0); - if (n <= 0) return; + if (n <= 0) + { + TINYMITM_WRITELOG(error, "recv failed or connection closed immediately"); + return; + } buf[n] = '\0'; std::string req(buf); - if (req.find("CONNECT ") != 0) return; + if (req.find("CONNECT ") != 0) + { + TINYMITM_WRITELOG(error, "handleClient was fed a request that was not a CONNECT request"); + return; + } /* port parsing @@ -344,6 +354,7 @@ void TinyMITMProxy::handleClient(SOCKET clientSocket) /* remote connection + remote connection */ addrinfo hints{}, *rawRes; hints.ai_family = AF_INET; @@ -379,8 +390,19 @@ void TinyMITMProxy::handleClient(SOCKET clientSocket) setNonBlocking(clientGuard, true); setNonBlocking(remoteGuard, true); - if (!doHandshake(clientSSL.get(), clientGuard, true)) return; - if (!doHandshake(remoteSSL.get(), remoteGuard, false)) return; + TINYMITM_WRITELOG(verbose, "Starting handshakes for {}", host); + if (!doHandshake(clientSSL.get(), clientGuard, true)) + { + TINYMITM_WRITELOG(error, "Client handshake failed for: {}", host); + return; + } + if (!doHandshake(remoteSSL.get(), remoteGuard, false)) + { + TINYMITM_WRITELOG(error, "Remote handshake failed for: {}", host); + return; + } + + TINYMITM_WRITELOG(verbose, "Established tunnel to {}", host); /* traffic loop @@ -437,9 +459,11 @@ void TinyMITMProxy::handleClient(SOCKET clientSocket) if (!clientStream.parseHeaders()) break; std::string headers = clientStream.buffer.substr(0, clientStream.headersEnd + 4); std::string path = "/"; + size_t s1 = headers.find(' '), s2 = headers.find(' ', s1 + 1); if (s1 != std::string::npos && s2 != std::string::npos) path = headers.substr(s1 + 1, s2 - s1 - 1); + pendingUrls.push_back("https://" + host + path); } @@ -506,6 +530,8 @@ void TinyMITMProxy::handleClient(SOCKET clientSocket) if (blockOutgoing) { + TINYMITM_WRITELOG(verbose, "blocked request to: {}", host); + std::string mockHeaders = "HTTP/1.1 500 Internal Server Error\r\n" "Content-Type: text/plain\r\n" "Connection: close\r\n\r\n";