From 0c617976918d22e5a3d2433bd70ff21807522381 Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 6 Feb 2024 15:07:42 +0100 Subject: [PATCH] Initial code for generating uid Signed-off-by: falkTX --- src/systray/utils.cpp | 43 ++++++++++++++++++++++++++++++++++ utils/cxfreeze/mod-ui-setup.py | 5 ---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/systray/utils.cpp b/src/systray/utils.cpp index 74dbdfb..62a9047 100644 --- a/src/systray/utils.cpp +++ b/src/systray/utils.cpp @@ -16,6 +16,7 @@ #define PRE_20H1_DWMWA_USE_IMMERSIVE_DARK_MODE 19 #define DWMWA_USE_IMMERSIVE_DARK_MODE 20 #else +#include #include #include #include @@ -32,6 +33,17 @@ #include #endif +constexpr const uint8_t mod_desktop_hash[] = { + 0xe4, 0xf7, 0x0d, 0xe9, 0x77, 0xb8, 0x47, 0xe0, 0xba, 0x2e, 0x70, 0x14, 0x93, 0x7a, 0xce, 0xa7 +}; + +constexpr uint8_t char2u8(const uint8_t c) +{ + return c >= '0' && c <= '9' ? c - '0' + : c >= 'a' && c <= 'f' ? 0xa + c - 'a' + : 0; +} + void initEvironment() { // base environment details @@ -126,6 +138,37 @@ void initEvironment() const size_t dataDirLen = std::strlen(dataDir); #endif + // generate UID + #if defined(__APPLE__) + // TODO + #elif defined(_WIN32) + // TODO + #else + if (FILE* const f = fopen("/etc/machine-id", "r")) + { + size_t len; + if (fread(path, PATH_MAX - 1, 1, f) == 0 && (len = strlen(path)) >= 33) + { + uint8_t idkey[16]; + for (int i=0; i<16; ++i) + { + idkey[i] = char2u8(path[i*2]) << 4; + idkey[i] |= char2u8(path[i*2+1]) << 0; + } + + QMessageAuthenticationCode qhash(QCryptographicHash::Sha256); + qhash.setKey(QByteArray::fromRawData(reinterpret_cast(idkey), sizeof(idkey))); + qhash.addData(reinterpret_cast(mod_desktop_hash), sizeof(mod_desktop_hash)); + + QByteArray qresult(qhash.result().toHex(':').toUpper()); + qresult.truncate(32 + 15); + + setenv("MOD_DEVICE_UID", qresult.constData(), 1); + } + fclose(f); + } + #endif + // set path to factory pedalboards #ifdef _WIN32 std::memcpy(path, appDir, appDirLen * sizeof(WCHAR)); diff --git a/utils/cxfreeze/mod-ui-setup.py b/utils/cxfreeze/mod-ui-setup.py index f57f8e7..0bba05d 100644 --- a/utils/cxfreeze/mod-ui-setup.py +++ b/utils/cxfreeze/mod-ui-setup.py @@ -67,7 +67,6 @@ def makedirs(path: str): #os.environ['MOD_API_KEY'] = os.path.join(resdir, '..', 'mod_api_key.pub') os.environ['MOD_DEVICE_KEY'] = os.path.join(DEVICE_DIR, 'rsa') os.environ['MOD_DEVICE_TAG'] = os.path.join(DEVICE_DIR, 'tag') -os.environ['MOD_DEVICE_UID'] = os.path.join(DEVICE_DIR, 'uid') from datetime import datetime from random import randint @@ -77,10 +76,6 @@ def makedirs(path: str): with open(os.environ['MOD_DEVICE_TAG'], 'w') as fh: tag = 'MDS-{0}-0-00-000-{1}'.format(datetime.utcnow().strftime('%Y%m%d'), randint(9000, 9999)) fh.write(tag) - if not os.path.isfile(os.environ['MOD_DEVICE_UID']): - with open(os.environ['MOD_DEVICE_UID'], 'w') as fh: - uid = ':'.join(['{0}{1}'.format(randint(0, 9), randint(0, 9)) for i in range(0, 16)]) - fh.write(uid) if not os.path.isfile(os.environ['MOD_DEVICE_KEY']): try: key = RSA.generate(2048)