Skip to content

Commit

Permalink
Merge pull request #239 from Stivius/fix/display_hash
Browse files Browse the repository at this point in the history
fix: add current drive detection #238
  • Loading branch information
dasgarner authored Apr 22, 2021
2 parents a7d744f + 5c8f599 commit 1f0e919
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
45 changes: 30 additions & 15 deletions player/common/system/HardwareKeyGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

#include "common/Utils.hpp"
#include "common/system/System.hpp"
#include "config/AppConfig.hpp"

#include <boost/process/child.hpp>
#include <boost/process/io.hpp>
#include <regex>

namespace bp = boost::process;

HardwareKey HardwareKeyGenerator::generate()
{
auto key = cpuid() + static_cast<std::string>(System::macAddress()) + volumeSerial();
Expand Down Expand Up @@ -37,35 +40,47 @@ inline void HardwareKeyGenerator::nativeCpuid(unsigned int* eax,

std::string HardwareKeyGenerator::volumeSerial()
{
namespace bp = boost::process;
const std::string SERIAL_PREFIX{"ID_SERIAL_SHORT"};

bp::pipe volumeInfo;
bp::ipstream stream;
auto line = executeAndGrepFirstLine("udevadm info " + currentDrive(), SERIAL_PREFIX);

bp::child udevadm("udevadm info /dev/sda", bp::std_out > volumeInfo);
bp::child grep("grep ID_SERIAL_SHORT", bp::std_in<volumeInfo, bp::std_out> stream);
return retrieveResult(std::regex{SERIAL_PREFIX + "=(.+)"}, line);
}

auto volumeSerial = retrieveVolumeSerial(stream);
std::string HardwareKeyGenerator::currentDrive()
{
const std::string DEVICE_PREFIX{"/dev/"};

udevadm.wait();
grep.wait();
auto line = executeAndGrepFirstLine("df " + AppConfig::playerBinary(), DEVICE_PREFIX);

return volumeSerial;
return DEVICE_PREFIX + retrieveResult(std::regex{DEVICE_PREFIX + "([^\\s]+)"}, line);
}

std::string HardwareKeyGenerator::retrieveVolumeSerial(boost::process::ipstream& stream)
std::string HardwareKeyGenerator::retrieveResult(const std::regex& regex, const std::string& line)
{
const int SERIAL_CAPTURE_GROUP = 1;
const std::regex SERIAL_REGEX{"ID_SERIAL_SHORT=(.+)"};

std::string line;
std::getline(stream, line);

std::smatch result;
if (std::regex_search(line, result, SERIAL_REGEX))
if (std::regex_search(line, result, regex))
{
return result[SERIAL_CAPTURE_GROUP].str();
}

return {};
}

std::string HardwareKeyGenerator::executeAndGrepFirstLine(const std::string& command, const std::string& grepSearch)
{
bp::pipe pipe;
bp::ipstream stream;

bp::child commandProcess(command, bp::std_out > pipe);
bp::child grepProcess("grep " + grepSearch, bp::std_in<pipe, bp::std_out> stream);

commandProcess.wait();
grepProcess.wait();

std::string line;
std::getline(stream, line);
return line;
}
7 changes: 5 additions & 2 deletions player/common/system/HardwareKeyGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#include "common/system/HardwareKey.hpp"

#include <boost/process/io.hpp>
#include <ios>
#include <regex>
#include <string>

class HardwareKeyGenerator
Expand All @@ -15,5 +16,7 @@ class HardwareKeyGenerator
static void nativeCpuid(unsigned int* eax, unsigned int* ebx, unsigned int* ecx, unsigned int* edx);
static std::string macAddress();
static std::string volumeSerial();
static std::string retrieveVolumeSerial(boost::process::ipstream& stream);
static std::string currentDrive();
static std::string executeAndGrepFirstLine(const std::string& command, const std::string& grepSearch);
static std::string retrieveResult(const std::regex& regex, const std::string& line);
};

0 comments on commit 1f0e919

Please sign in to comment.