Awoo-Installer/source/netInstall.cpp

316 lines
12 KiB
C++
Raw Normal View History

2019-11-01 20:00:28 +00:00
/*
Copyright (c) 2017-2018 Adubbz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <cstring>
#include <sys/socket.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <sstream>
#include <curl/curl.h>
#include <thread>
#include <switch.h>
#include "util/network_util.hpp"
#include "install/install_nsp_remote.hpp"
#include "install/http_nsp.hpp"
2019-11-29 08:23:11 +00:00
#include "install/install_xci.hpp"
#include "install/http_xci.hpp"
#include "install/install.hpp"
2019-10-22 22:14:37 +00:00
#include "util/error.hpp"
#include "ui/MainApplication.hpp"
#include "netInstall.hpp"
2019-11-30 16:12:45 +00:00
#include "sdInstall.hpp"
#include "util/config.hpp"
#include "util/util.hpp"
2019-11-29 08:23:11 +00:00
#include "util/curl.hpp"
const unsigned int MAX_URL_SIZE = 1024;
const unsigned int MAX_URLS = 256;
const int REMOTE_INSTALL_PORT = 2000;
static int m_serverSocket = 0;
static int m_clientSocket = 0;
namespace inst::ui {
extern MainApplication *mainApp;
2019-10-22 22:14:37 +00:00
void setNetInfoText(std::string ourText){
mainApp->netinstPage->pageInfoText->SetText(ourText);
mainApp->CallForRender();
}
}
2019-10-18 19:01:51 +00:00
namespace netInstStuff{
2019-10-18 19:01:51 +00:00
void InitializeServerSocket() try
{
2019-10-18 19:01:51 +00:00
// Create a socket
m_serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
2019-10-18 19:01:51 +00:00
if (m_serverSocket < -1)
{
THROW_FORMAT("Failed to create a server socket. Error code: %u\n", errno);
}
2019-10-18 19:01:51 +00:00
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(REMOTE_INSTALL_PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
2019-10-18 19:01:51 +00:00
if (bind(m_serverSocket, (struct sockaddr*) &server, sizeof(server)) < 0)
{
THROW_FORMAT("Failed to bind server socket. Error code: %u\n", errno);
}
// Set as non-blocking
fcntl(m_serverSocket, F_SETFL, fcntl(m_serverSocket, F_GETFL, 0) | O_NONBLOCK);
2019-10-18 19:01:51 +00:00
if (listen(m_serverSocket, 5) < 0)
{
THROW_FORMAT("Failed to listen on server socket. Error code: %u\n", errno);
}
}
catch (std::exception& e)
{
2019-11-30 17:12:59 +00:00
LOG_DEBUG("Failed to initialize server socket!\n");
2019-10-18 19:01:51 +00:00
fprintf(stdout, "%s", e.what());
if (m_serverSocket != 0)
{
close(m_serverSocket);
m_serverSocket = 0;
}
inst::ui::mainApp->CreateShowDialog("Failed to initialize server socket!", (std::string)e.what(), {"OK"}, true);
}
2019-10-18 19:01:51 +00:00
void OnUnwound()
{
2019-11-30 17:12:59 +00:00
LOG_DEBUG("unwinding view\n");
2019-10-18 19:01:51 +00:00
if (m_clientSocket != 0)
{
close(m_clientSocket);
m_clientSocket = 0;
}
curl_global_cleanup();
}
2019-11-30 17:06:49 +00:00
void installTitleNet(std::vector<std::string> ourUrlList, int ourStorage, std::vector<std::string> urlListAltNames, std::string ourSource)
{
inst::util::initInstallServices();
2019-10-24 19:53:54 +00:00
inst::ui::loadInstallScreen();
bool nspInstalled = true;
2019-11-14 19:04:40 +00:00
NcmStorageId m_destStorageId = NcmStorageId_SdCard;
2019-11-14 19:04:40 +00:00
if (ourStorage) m_destStorageId = NcmStorageId_BuiltInUser;
unsigned int urlItr;
2019-11-14 01:01:35 +00:00
std::vector<std::string> urlNames;
if (urlListAltNames.size() > 0) {
for (long unsigned int i = 0; i < urlListAltNames.size(); i++) {
2019-11-30 17:06:49 +00:00
urlNames.push_back(inst::util::shortenString(urlListAltNames[i], 38, true));
2019-11-14 01:01:35 +00:00
}
} else {
for (long unsigned int i = 0; i < ourUrlList.size(); i++) {
2019-11-30 17:06:49 +00:00
urlNames.push_back(inst::util::shortenString(inst::util::formatUrlString(ourUrlList[i]), 38, true));
2019-11-14 01:01:35 +00:00
}
}
2019-11-17 20:38:01 +00:00
std::vector<int> previousClockValues;
if (inst::config::overClock) {
previousClockValues.push_back(inst::util::setClockSpeed(0, 1785000000)[0]);
previousClockValues.push_back(inst::util::setClockSpeed(1, 76800000)[0]);
previousClockValues.push_back(inst::util::setClockSpeed(2, 1600000000)[0]);
}
try {
for (urlItr = 0; urlItr < ourUrlList.size(); urlItr++) {
2019-11-30 17:12:59 +00:00
LOG_DEBUG("%s %s\n", "Install request from", ourUrlList[urlItr].c_str());
2019-11-30 17:06:49 +00:00
inst::ui::setTopInstInfoText("Installing " + urlNames[urlItr] + ourSource);
2019-10-24 19:53:54 +00:00
2019-11-30 16:12:45 +00:00
tin::install::Install* installTask;
2019-11-30 16:12:45 +00:00
if (inst::curl::downloadToBuffer(ourUrlList[urlItr], 0x100, 0x103) == "HEAD") {
auto httpXCI = new tin::install::xci::HTTPXCI(ourUrlList[urlItr]);
installTask = new tin::install::xci::XCIInstallTask(m_destStorageId, inst::config::ignoreReqVers, httpXCI);
2019-11-29 08:23:11 +00:00
} else {
2019-11-30 16:12:45 +00:00
auto httpNSP = new tin::install::nsp::HTTPNSP(ourUrlList[urlItr]);
installTask = new tin::install::nsp::RemoteNSPInstall(m_destStorageId, inst::config::ignoreReqVers, httpNSP);
}
2019-11-29 08:23:11 +00:00
2019-11-30 17:12:59 +00:00
LOG_DEBUG("%s\n", "Preparing installation");
inst::ui::setInstInfoText("Preparing installation...");
inst::ui::setInstBarPerc(0);
2019-11-30 16:12:45 +00:00
installTask->Prepare();
2019-11-29 08:23:11 +00:00
2019-11-30 16:12:45 +00:00
installTask->Begin();
}
2019-10-24 19:53:54 +00:00
}
catch (std::exception& e) {
2019-11-30 17:12:59 +00:00
LOG_DEBUG("Failed to install");
LOG_DEBUG("%s", e.what());
2019-10-24 19:53:54 +00:00
fprintf(stdout, "%s", e.what());
2019-11-14 01:01:35 +00:00
inst::ui::setInstInfoText("Failed to install " + urlNames[urlItr]);
inst::ui::setInstBarPerc(0);
std::thread audioThread(inst::util::playAudio,"romfs:/audio/bark.wav");
2019-11-14 01:01:35 +00:00
inst::ui::mainApp->CreateShowDialog("Failed to install " + urlNames[urlItr] + "!", "Partially installed contents can be removed from the System Settings applet.\n\n" + (std::string)e.what(), {"OK"}, true);
audioThread.join();
nspInstalled = false;
}
2019-11-17 20:38:01 +00:00
if (previousClockValues.size() > 0) {
inst::util::setClockSpeed(0, previousClockValues[0]);
inst::util::setClockSpeed(1, previousClockValues[1]);
inst::util::setClockSpeed(2, previousClockValues[2]);
}
2019-11-30 17:12:59 +00:00
LOG_DEBUG("%s\n", "Telling the server we're done installing");
// Send 1 byte ack to close the server
u8 ack = 0;
tin::network::WaitSendNetworkData(m_clientSocket, &ack, sizeof(u8));
if(nspInstalled) {
inst::ui::setInstInfoText("Install complete");
inst::ui::setInstBarPerc(100);
std::thread audioThread(inst::util::playAudio,"romfs:/audio/awoo.wav");
if (ourUrlList.size() > 1) inst::ui::mainApp->CreateShowDialog(std::to_string(ourUrlList.size()) + " files installed successfully!", nspInstStuff::finishedMessage(), {"OK"}, true);
2019-11-14 01:01:35 +00:00
else inst::ui::mainApp->CreateShowDialog(urlNames[0] + " installed!", nspInstStuff::finishedMessage(), {"OK"}, true);
audioThread.join();
2019-10-24 19:53:54 +00:00
}
2019-11-30 17:12:59 +00:00
LOG_DEBUG("Done");
inst::ui::loadMainMenu();
inst::util::deinitInstallServices();
2019-10-24 19:53:54 +00:00
return;
}
2019-10-18 19:01:51 +00:00
std::vector<std::string> OnSelected()
{
u64 freq = armGetSystemTickFreq();
u64 startTime = armGetSystemTick();
2019-10-18 19:01:51 +00:00
OnUnwound();
2019-10-18 19:01:51 +00:00
try
{
2019-10-18 19:01:51 +00:00
ASSERT_OK(curl_global_init(CURL_GLOBAL_ALL), "Curl failed to initialized");
2019-10-18 19:01:51 +00:00
// Initialize the server socket if it hasn't already been
if (m_serverSocket == 0)
{
2019-10-18 19:01:51 +00:00
InitializeServerSocket();
2019-10-18 19:01:51 +00:00
if (m_serverSocket <= 0)
{
THROW_FORMAT("Server socket failed to initialize.\n");
}
}
std::string ourIPAddress = inst::util::getIPAddress();
inst::ui::setNetInfoText("Waiting for a connection... Your Switch's IP Address is: " + ourIPAddress);
2019-11-30 17:12:59 +00:00
LOG_DEBUG("%s %s\n", "Switch IP is ", ourIPAddress.c_str());
LOG_DEBUG("%s\n", "Waiting for network");
LOG_DEBUG("%s\n", "B to cancel");
2019-10-18 19:01:51 +00:00
std::vector<std::string> urls;
2019-10-18 19:01:51 +00:00
while (true)
{
// If we don't update the UI occasionally the Switch basically crashes on this screen if you press the home button
u64 newTime = armGetSystemTick();
if (newTime - startTime >= freq * 0.25) {
startTime = newTime;
inst::ui::mainApp->CallForRender();
}
2019-10-18 19:01:51 +00:00
// Break on input pressed
hidScanInput();
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
2019-10-18 19:01:51 +00:00
if (kDown & KEY_B)
{
break;
}
if (kDown & KEY_Y)
{
return {"supplyUrl"};
}
if (kDown & KEY_X)
{
inst::ui::mainApp->CreateShowDialog("Help", "Files can be installed remotely from your other devices using tools such\nas ns-usbloader in Tinfoil mode. To send files to your Switch, open one\nof these pieces of software on your PC or mobile device, input your\nSwitch's IP address (listed on-screen), select your files, then upload\nto your console! If the software you're using won't let you select\nspecific file types, try renaming the extension to something it accepts.\nAwoo Installer doesn't care about file extensions during net installations!\n\nIf you can't figure it out, just copy your files to your SD card and try\nthe \"Install from SD Card\" option on the main menu!", {"OK"}, true);
}
2019-10-18 19:01:51 +00:00
struct sockaddr_in client;
socklen_t clientLen = sizeof(client);
2019-10-18 19:01:51 +00:00
m_clientSocket = accept(m_serverSocket, (struct sockaddr*)&client, &clientLen);
2019-10-18 19:01:51 +00:00
if (m_clientSocket >= 0)
{
2019-11-30 17:12:59 +00:00
LOG_DEBUG("%s\n", "Server accepted");
2019-10-18 19:01:51 +00:00
u32 size = 0;
tin::network::WaitReceiveNetworkData(m_clientSocket, &size, sizeof(u32));
size = ntohl(size);
2019-11-30 17:12:59 +00:00
LOG_DEBUG("Received url buf size: 0x%x\n", size);
2019-10-18 19:01:51 +00:00
if (size > MAX_URL_SIZE * MAX_URLS)
{
THROW_FORMAT("URL size %x is too large!\n", size);
}
2019-10-18 19:01:51 +00:00
// Make sure the last string is null terminated
auto urlBuf = std::make_unique<char[]>(size+1);
memset(urlBuf.get(), 0, size+1);
2019-10-18 19:01:51 +00:00
tin::network::WaitReceiveNetworkData(m_clientSocket, urlBuf.get(), size);
2019-10-18 19:01:51 +00:00
// Split the string up into individual URLs
std::stringstream urlStream(urlBuf.get());
std::string segment;
std::string nspExt = ".nsp";
2019-11-03 18:18:42 +00:00
std::string nszExt = ".nsz";
2019-10-18 19:01:51 +00:00
while (std::getline(urlStream, segment, '\n')) urls.push_back(segment);
2019-10-18 19:01:51 +00:00
break;
}
else if (errno != EAGAIN)
{
2019-10-18 19:01:51 +00:00
THROW_FORMAT("Failed to open client socket with code %u\n", errno);
}
}
2019-10-18 19:01:51 +00:00
return urls;
}
catch (std::runtime_error& e)
{
2019-11-30 17:12:59 +00:00
LOG_DEBUG("Failed to perform remote install!\n");
LOG_DEBUG("%s", e.what());
2019-10-18 19:01:51 +00:00
fprintf(stdout, "%s", e.what());
inst::ui::mainApp->CreateShowDialog("Failed to perform remote install!", (std::string)e.what(), {"OK"}, true);
return {};
}
}
2019-10-22 22:14:37 +00:00
}