Add NS-USBloader network-stopper feature

and also a bit fixed and corrected WaitSendNetworkData() function at network_util.cpp.
This commit is contained in:
Dmitry Isaenko 2020-09-29 02:48:33 +03:00 committed by HookedBehemoth
parent dd2390f8c6
commit eed1817688
7 changed files with 60 additions and 15 deletions

View file

@ -23,7 +23,7 @@ SOFTWARE.
namespace netInstStuff {
void OnUnwound();
void sendExitCommands();
void sendExitCommands(std::string url);
void installTitleNet(std::vector<std::string> ourUrlList, int ourStorage, std::vector<std::string> urlListAltNames, std::string ourSource);
std::vector<std::string> OnSelected();
}

View file

@ -78,6 +78,8 @@ namespace tin::network
int StreamDataRange(size_t offset, size_t size, std::function<size_t (u8* bytes, size_t size)> streamFunc);
};
void NSULDrop(std::string url);
size_t WaitReceiveNetworkData(int sockfd, void* buf, size_t len);
size_t WaitSendNetworkData(int sockfd, void* buf, size_t len);
}

View file

@ -12,6 +12,7 @@ namespace inst::util {
bool removeDirectory(std::string dir);
bool copyFile(std::string inFile, std::string outFile);
std::string formatUrlString(std::string ourString);
std::string formatUrlLink(std::string ourString);
std::string shortenString(std::string ourString, int ourLength, bool isFile);
std::string readTextFromFile(std::string ourFile);
std::string softwareKeyboard(std::string guideText, std::string initialText, int LenMax);

View file

@ -108,15 +108,14 @@ namespace netInstStuff{
curl_global_cleanup();
}
void sendExitCommands()
void sendExitCommands(std::string url)
{
LOG_DEBUG("Telling the server we're done installing\n");
// Send 1 byte ack to close the server, OG tinfoil compatibility
u8 ack = 0;
tin::network::WaitSendNetworkData(m_clientSocket, &ack, sizeof(u8));
// Send 'DEAD\r\n' so ns-usbloader knows we're done
u8 nsUsbAck [6] = {0x44,0x45,0x41,0x44,0x0D,0x0A};
tin::network::WaitSendNetworkData(m_clientSocket, &nsUsbAck, sizeof(u8) * 6);
// Send 'DROP' header so ns-usbloader knows we're done
tin::network::NSULDrop(url);
}
void installTitleNet(std::vector<std::string> ourUrlList, int ourStorage, std::vector<std::string> urlListAltNames, std::string ourSource)
@ -189,7 +188,7 @@ namespace netInstStuff{
inst::util::setClockSpeed(2, previousClockValues[2]);
}
sendExitCommands();
sendExitCommands(inst::util::formatUrlLink(ourUrlList[0]));
OnUnwound();
if(nspInstalled) {

View file

@ -156,8 +156,12 @@ namespace inst::ui {
void netInstPage::onInput(u64 Down, u64 Up, u64 Held, pu::ui::Touch Pos) {
if (Down & KEY_B) {
if (this->menu->GetItems().size() > 0) {}
netInstStuff::sendExitCommands();
if (this->menu->GetItems().size() > 0){
if (this->selectedUrls.size() == 0) {
this->selectTitle(this->menu->GetSelectedIndex());
}
netInstStuff::sendExitCommands(inst::util::formatUrlLink(this->selectedUrls[0]));
}
netInstStuff::OnUnwound();
mainApp->LoadLayout(mainApp->mainPage);
}
@ -180,8 +184,6 @@ namespace inst::ui {
if (Down & KEY_PLUS) {
if (this->selectedUrls.size() == 0) {
this->selectTitle(this->menu->GetSelectedIndex());
this->startInstall(false);
return;
}
this->startInstall(false);
}

View file

@ -245,15 +245,48 @@ namespace tin::network
size_t WaitSendNetworkData(int sockfd, void* buf, size_t len)
{
errno = 0;
int ret = 0;
size_t written = 0;
while ((((ret = send(sockfd, (u8*)buf + written, len - written, 0)) > 0 && (written += ret) < len) || errno == EAGAIN))
while (written < len)
{
if (hidKeysDown(CONTROLLER_P1_AUTO) & KEY_B) // Break if user clicks 'B'
break;
errno = 0;
ret = send(sockfd, (u8*)buf + written, len - written, 0);
if (ret < 0){ // If error
if (errno == EWOULDBLOCK || errno == EAGAIN){ // Is it because other side is busy?
sleep(5);
continue;
}
break; // No? Die.
}
written += ret;
}
return written;
}
void NSULDrop(std::string url)
{
CURL* curl = curl_easy_init();
if (!curl)
{
THROW_FORMAT("Failed to initialize curl\n");
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DROP");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "tinfoil");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 50);
curl_easy_perform(curl); // ignore returning value
curl_easy_cleanup(curl);
}
}

View file

@ -140,6 +140,14 @@ namespace inst::util {
return finalString;
}
std::string formatUrlLink(std::string ourString){
std::string::size_type pos = ourString.find('/');
if (pos != std::string::npos)
return ourString.substr(0, pos);
else
return ourString;
}
std::string shortenString(std::string ourString, int ourLength, bool isFile) {
std::filesystem::path ourStringAsAPath = ourString;
std::string ourExtension = ourStringAsAPath.extension().string();