mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Unify all code for generating random data into functions in utils
This commit is contained in:
@@ -12,9 +12,13 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <random>
|
||||
|
||||
#include <systemd/sd-id128.h>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "internal/platform/implementation/linux/utils.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
@@ -47,5 +51,52 @@ std::optional<Uuid> UuidFromString(const std::string &uuid_str) {
|
||||
|
||||
return Uuid(uuid.qwords[0], uuid.qwords[1]);
|
||||
}
|
||||
|
||||
std::optional<std::string> NewUuidStr() {
|
||||
sd_id128_t id;
|
||||
char id_cstr[SD_ID128_UUID_STRING_MAX];
|
||||
if (auto ret = sd_id128_randomize(&id); ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": could not generate a random UUID: "
|
||||
<< std::strerror(ret);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return std::string(sd_id128_to_uuid_string(id, id_cstr));
|
||||
}
|
||||
|
||||
std::string RandString(std::string allowed_chars, size_t length) {
|
||||
thread_local static std::random_device device{};
|
||||
|
||||
std::mt19937 gen{device()};
|
||||
std::uniform_int_distribution<size_t> dist(0, allowed_chars.length() - 1);
|
||||
|
||||
std::string s;
|
||||
s.reserve(length);
|
||||
|
||||
for (auto i = 0; i < length; i++) {
|
||||
s += allowed_chars[dist(gen)];
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string RandSSID() {
|
||||
std::string allowed_chars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789";
|
||||
|
||||
return absl::StrCat("DIRECT-", RandString(allowed_chars, 25));
|
||||
}
|
||||
|
||||
std::string RandWPAPassphrase() {
|
||||
std::string allowed_chars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789"
|
||||
"!\"#$%&'()*+,-./[\\]^_`~{|}";
|
||||
|
||||
return RandString(allowed_chars, 63);
|
||||
}
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -27,6 +27,10 @@ namespace nearby {
|
||||
namespace linux {
|
||||
|
||||
std::optional<Uuid> UuidFromString(const std::string &uuid_str);
|
||||
std::optional<std::string> NewUuidStr();
|
||||
|
||||
std::string RandSSID();
|
||||
std::string RandWPAPassphrase();
|
||||
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -28,9 +28,28 @@ TEST(UtilsTests, UuidFromStringRoundTrip) {
|
||||
auto nearby_uuid = UuidFromString(input);
|
||||
EXPECT_TRUE(nearby_uuid.has_value());
|
||||
|
||||
|
||||
EXPECT_EQ(absl::AsciiStrToLower(std::string{*nearby_uuid}),
|
||||
"b5209043-f493-4b38-8c34-810aa3cd1407");
|
||||
}
|
||||
|
||||
TEST(UtilsTests, GenNewUuid) {
|
||||
auto uuid_str = NewUuidStr();
|
||||
EXPECT_TRUE(uuid_str.has_value());
|
||||
auto uuid = UuidFromString(*uuid_str);
|
||||
EXPECT_TRUE(uuid.has_value());
|
||||
EXPECT_EQ(absl::AsciiStrToLower(std::string{*uuid}),
|
||||
*uuid_str);
|
||||
}
|
||||
|
||||
TEST(UtilsTests, GenRandSSID) {
|
||||
std::string ssid = RandSSID();
|
||||
EXPECT_EQ(ssid.length(), 32);
|
||||
EXPECT_EQ(ssid.find("DIRECT-"), 0);
|
||||
}
|
||||
|
||||
TEST(UtilsTests, GenRandRandWPAPassphrase) {
|
||||
std::string password = RandWPAPassphrase();
|
||||
EXPECT_EQ(password.length(), 63);
|
||||
}
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -19,10 +19,9 @@
|
||||
#include <memory>
|
||||
#include <random>
|
||||
|
||||
#include <systemd/sd-id128.h>
|
||||
|
||||
#include "internal/platform/implementation/linux/dbus.h"
|
||||
#include "internal/platform/implementation/linux/network_manager.h"
|
||||
#include "internal/platform/implementation/linux/utils.h"
|
||||
#include "internal/platform/implementation/linux/wifi_hotspot.h"
|
||||
#include "internal/platform/implementation/linux/wifi_hotspot_server_socket.h"
|
||||
#include "internal/platform/implementation/linux/wifi_hotspot_socket.h"
|
||||
@@ -135,64 +134,41 @@ bool NetworkManagerWifiHotspotMedium::StartWifiHotspot(
|
||||
return false;
|
||||
}
|
||||
|
||||
sd_id128_t id;
|
||||
if (auto ret = sd_id128_randomize(&id); ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": error generating a 128-bit ID: "
|
||||
<< std::strerror(ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
char id_cstr[SD_ID128_UUID_STRING_MAX];
|
||||
sd_id128_to_string(id, id_cstr);
|
||||
|
||||
std::string ssid = absl::StrCat("DIRECT-", id_cstr);
|
||||
ssid.resize(32);
|
||||
std::string ssid = RandSSID();
|
||||
hotspot_credentials->SetSSID(ssid);
|
||||
|
||||
if (auto ret = sd_id128_randomize(&id); ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": error generating a 128-bit ID: "
|
||||
<< std::strerror(ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
sd_id128_to_string(id, id_cstr);
|
||||
std::string password = std::string(id_cstr, 15);
|
||||
std::string password = RandWPAPassphrase();
|
||||
hotspot_credentials->SetPassword(password);
|
||||
|
||||
if (auto ret = sd_id128_randomize(&id); ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": error generating a 128-bit ID: "
|
||||
<< std::strerror(ret);
|
||||
auto connection_id = NewUuidStr();
|
||||
if (!connection_id.has_value()) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": could not generate a connection UUID";
|
||||
return false;
|
||||
}
|
||||
sd_id128_to_uuid_string(id, id_cstr);
|
||||
|
||||
std::vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
|
||||
std::map<std::string, std::map<std::string, sdbus::Variant>>
|
||||
connection_settings{
|
||||
{
|
||||
"connection",
|
||||
std::map<std::string, sdbus::Variant>{
|
||||
{"uuid", std::string(id_cstr)},
|
||||
{"id", "Google Nearby Hotspot"},
|
||||
{"type", "802-11-wireless"},
|
||||
{"zone", "Public"}},
|
||||
{{"uuid", *connection_id},
|
||||
{"id", "Google Nearby Hotspot"},
|
||||
{"type", "802-11-wireless"},
|
||||
{"zone", "Public"}},
|
||||
},
|
||||
{"802-11-wireless",
|
||||
std::map<std::string, sdbus::Variant>{
|
||||
{"assigned-mac-address", "random"},
|
||||
{"ap-isolation", networkmanager::constants::kNMTernaryFalse},
|
||||
{"mode", "ap"},
|
||||
{"ssid", ssid_bytes},
|
||||
{"security", "802-11-wireless-security"}}},
|
||||
{{"assigned-mac-address", "random"},
|
||||
{"ap-isolation", networkmanager::constants::kNMTernaryFalse},
|
||||
{"mode", "ap"},
|
||||
{"ssid", std::vector<uint8_t>(ssid.begin(), ssid.end())},
|
||||
{"security", "802-11-wireless-security"}}},
|
||||
{"802-11-wireless-security",
|
||||
std::map<std::string, sdbus::Variant>{
|
||||
{"pmf", networkmanager::constants::setting::
|
||||
kWirelessSecurityPMFDisable},
|
||||
{"key-mgmt", "wpa-psk"},
|
||||
{"psk", password}}},
|
||||
{"ipv4", std::map<std::string, sdbus::Variant>{{"method", "shared"}}},
|
||||
{{"pmf",
|
||||
networkmanager::constants::setting::kWirelessSecurityPMFDisable},
|
||||
{"key-mgmt", "wpa-psk"},
|
||||
{"psk", password}}},
|
||||
{"ipv4", {{"method", "shared"}}},
|
||||
{"ipv6",
|
||||
std::map<std::string, sdbus::Variant>{
|
||||
{
|
||||
{"addr-gen-mode", networkmanager::constants::setting::
|
||||
kIP6ConfigAddrGenModeStablePrivacy},
|
||||
{"method", "shared"},
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "internal/platform/implementation/linux/dbus.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/networkmanager/device_wireless_client.h"
|
||||
#include "internal/platform/implementation/linux/network_manager_active_connection.h"
|
||||
#include "internal/platform/implementation/linux/utils.h"
|
||||
#include "internal/platform/implementation/linux/wifi_medium.h"
|
||||
#include "internal/platform/implementation/wifi.h"
|
||||
|
||||
@@ -229,21 +230,10 @@ api::WifiConnectionStatus NetworkManagerWifiMedium::ConnectToNetwork(
|
||||
return api::WifiConnectionStatus::kConnectionFailure;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> ssid_bytes(ssid.begin(), ssid.end());
|
||||
std::string connection_id;
|
||||
|
||||
{
|
||||
sd_id128_t id;
|
||||
char id_cstr[SD_ID128_UUID_STRING_MAX];
|
||||
|
||||
if (auto ret = sd_id128_randomize(&id); ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": could not generate a connection UUID";
|
||||
return api::WifiConnectionStatus::kUnknown;
|
||||
}
|
||||
|
||||
sd_id128_to_uuid_string(id, id_cstr);
|
||||
connection_id = std::string(id_cstr);
|
||||
auto connection_id = NewUuidStr();
|
||||
if (!connection_id.has_value()) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": could not generate a connection UUID";
|
||||
return api::WifiConnectionStatus::kUnknown;
|
||||
}
|
||||
|
||||
auto [auth_alg, key_mgmt] = AuthAlgAndKeyMgmt(auth_type);
|
||||
@@ -251,22 +241,21 @@ api::WifiConnectionStatus NetworkManagerWifiMedium::ConnectToNetwork(
|
||||
std::map<std::string, std::map<std::string, sdbus::Variant>>
|
||||
connection_settings{
|
||||
{"connection",
|
||||
std::map<std::string, sdbus::Variant>{
|
||||
{"uuid", connection_id},
|
||||
{
|
||||
{"uuid", *connection_id},
|
||||
{"autoconnect", true},
|
||||
{"id", std::string(ssid)},
|
||||
{"type", "802-11-wireless"},
|
||||
{"zone", "Public"},
|
||||
}},
|
||||
{"802-11-wireless",
|
||||
std::map<std::string, sdbus::Variant>{
|
||||
{"ssid", ssid_bytes},
|
||||
{
|
||||
{"ssid", std::vector<uint8_t>(ssid.begin(), ssid.end())},
|
||||
{"mode", "infrastructure"},
|
||||
{"security", "802-11-wireless-security"},
|
||||
{"assigned-mac-address", "random"},
|
||||
}},
|
||||
{"802-11-wireless-security",
|
||||
std::map<std::string, sdbus::Variant>{{"key-mgmt", key_mgmt}}}};
|
||||
{"802-11-wireless-security", {{"key-mgmt", key_mgmt}}}};
|
||||
if (!password.empty()) {
|
||||
connection_settings["802-11-wireless-security"]["psk"] =
|
||||
std::string(password);
|
||||
|
||||
Reference in New Issue
Block a user