| 578 | } |
| 579 | |
| 580 | bool Transfer::startReceiver(std::string& outError) |
| 581 | { |
| 582 | if (!Server::isRunning()) { |
| 583 | outError = "HTTP server not available."; |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | { |
| 588 | std::lock_guard<std::mutex> lock(g_receiverMutex); |
| 589 | if (g_receiverRunning) { |
| 590 | return true; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | g_failedAuthAttempts.store(0); |
| 595 | int pin = generatePin(); |
| 596 | std::string token = StringUtils::format("%04d", pin); |
| 597 | std::string ip = Server::getAddress(); |
| 598 | setReceiverNotice(""); |
| 599 | setReceiverCompletedName(""); |
| 600 | g_receiverCompleted.store(false); |
| 601 | size_t pos = ip.find("://"); |
| 602 | if (pos != std::string::npos) { |
| 603 | ip = ip.substr(pos + 3); |
| 604 | } |
| 605 | pos = ip.find(":"); |
| 606 | if (pos != std::string::npos) { |
| 607 | ip = ip.substr(0, pos); |
| 608 | } |
| 609 | |
| 610 | // Publish token/ip/port before the handler is reachable, so an upload that |
| 611 | // arrives the instant it registers validates against the real token. |
| 612 | { |
| 613 | std::lock_guard<std::mutex> lock(g_receiverMutex); |
| 614 | g_token = token; |
| 615 | g_receiverIp = ip; |
| 616 | g_receiverPort = TRANSFER_PORT; |
| 617 | } |
| 618 | |
| 619 | Server::registerHandler("/transfer/info", handleInfo); |
| 620 | Server::registerUploadHandler("/transfer/upload", TEMP_UPLOAD, handleUpload); |
| 621 | |
| 622 | { |
| 623 | std::lock_guard<std::mutex> lock(g_receiverMutex); |
| 624 | g_receiverRunning = true; |
| 625 | } |
| 626 | |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | void Transfer::stopReceiver(void) |
| 631 | { |
nothing calls this directly
no test coverage detected