|
|
|
@@ -9,50 +9,6 @@
|
|
|
|
|
|
|
|
|
|
#define TIME_NOW() std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch().count()
|
|
|
|
|
|
|
|
|
|
typedef boost::property_tree::ptree::path_type json_path;
|
|
|
|
|
|
|
|
|
|
// static int calculateSubnetMask(const std::string& subnet_mask) {
|
|
|
|
|
// int mask = 0;
|
|
|
|
|
// std::istringstream iss(subnet_mask);
|
|
|
|
|
// std::string octet;
|
|
|
|
|
// while (std::getline(iss, octet, '.')) {
|
|
|
|
|
// int octet_value = std::stoi(octet);
|
|
|
|
|
// for (int i = 7; i >= 0; i--) {
|
|
|
|
|
// if (octet_value & (1 << i)) {
|
|
|
|
|
// mask++;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return mask;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Преобразует строку вида `1.2.3.4/24` в пару строк вида `1.2.3.4` `255.255.255.0`
|
|
|
|
|
*/
|
|
|
|
|
// std::pair<std::string, std::string> splitIpAndMask(const std::string& input) {
|
|
|
|
|
// auto pos = input.find('/');
|
|
|
|
|
// if (pos == std::string::npos) {
|
|
|
|
|
// // Обработка ошибки: нет символа '/'
|
|
|
|
|
// throw std::runtime_error("address not contains mask");
|
|
|
|
|
// }
|
|
|
|
|
// std::string ip = input.substr(0, pos);
|
|
|
|
|
// const unsigned int mask_int = std::stoul(input.substr(pos + 1));
|
|
|
|
|
//
|
|
|
|
|
// if (mask_int > 32) {
|
|
|
|
|
// throw std::runtime_error("invalid mask");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// std::string mask_binary = std::string(mask_int, '1') + std::string(32 - mask_int, '0');
|
|
|
|
|
// std::string mask_str;
|
|
|
|
|
//
|
|
|
|
|
// for (unsigned int i = 0; i < 4; ++i) {
|
|
|
|
|
// std::string octet = mask_binary.substr(i * 8u, 8);
|
|
|
|
|
// int octet_value = std::stoi(octet, nullptr, 2);
|
|
|
|
|
// mask_str += std::to_string(octet_value) + (i < 3 ? "." : "");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return std::make_pair(ip, mask_str);
|
|
|
|
|
// }
|
|
|
|
|
static inline void rtrim(std::string &s) {
|
|
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
|
|
|
|
|
return !std::isspace(ch);
|
|
|
|
@@ -94,14 +50,13 @@ std::string makeTimepointFromMillis(int64_t unix_time_ms) {
|
|
|
|
|
#ifdef API_OBJECT_DEBUG_METRICS_ENABLE
|
|
|
|
|
api_driver::obj::StatisticsLogger::StatisticsLogger(): timeStart(TIME_NOW()) {}
|
|
|
|
|
|
|
|
|
|
std::string api_driver::obj::StatisticsLogger::getSettings() {
|
|
|
|
|
nlohmann::json api_driver::obj::StatisticsLogger::getSettings() {
|
|
|
|
|
std::lock_guard _lock(mutex);
|
|
|
|
|
std::stringstream res;
|
|
|
|
|
res << "{\"en\":" << boolAsStr(this->logEn);
|
|
|
|
|
res << ",\"logPeriodMs\":" << logPeriodMs;
|
|
|
|
|
res << ",\"maxAgeMs\":" << maxAgeMs;
|
|
|
|
|
res << '}';
|
|
|
|
|
return res.str();
|
|
|
|
|
nlohmann::json res;
|
|
|
|
|
res["en"] = this->logEn;
|
|
|
|
|
res["logPeriodMs"] = logPeriodMs.load();
|
|
|
|
|
res["maxAgeMs"] = maxAgeMs.load();
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void api_driver::obj::StatisticsLogger::setSettings(const nlohmann::json& data) {
|
|
|
|
@@ -178,6 +133,49 @@ api_driver::obj::StatisticsLogger::~StatisticsLogger() = default;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef API_OBJECT_NETWORK_SETTINGS_ENABLE
|
|
|
|
|
static int calculateSubnetMask(const std::string& subnet_mask) {
|
|
|
|
|
int mask = 0;
|
|
|
|
|
std::istringstream iss(subnet_mask);
|
|
|
|
|
std::string octet;
|
|
|
|
|
while (std::getline(iss, octet, '.')) {
|
|
|
|
|
int octet_value = std::stoi(octet);
|
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
|
|
|
if (octet_value & (1 << i)) {
|
|
|
|
|
mask++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Преобразует строку вида `1.2.3.4/24` в пару строк вида `1.2.3.4` `255.255.255.0`
|
|
|
|
|
*/
|
|
|
|
|
std::pair<std::string, std::string> splitIpAndMask(const std::string& input) {
|
|
|
|
|
auto pos = input.find('/');
|
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
|
// Обработка ошибки: нет символа '/'
|
|
|
|
|
throw std::runtime_error("address not contains mask");
|
|
|
|
|
}
|
|
|
|
|
std::string ip = input.substr(0, pos);
|
|
|
|
|
const unsigned int mask_int = std::stoul(input.substr(pos + 1));
|
|
|
|
|
|
|
|
|
|
if (mask_int > 32) {
|
|
|
|
|
throw std::runtime_error("invalid mask");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string mask_binary = std::string(mask_int, '1') + std::string(32 - mask_int, '0');
|
|
|
|
|
std::string mask_str;
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 4; ++i) {
|
|
|
|
|
std::string octet = mask_binary.substr(i * 8u, 8);
|
|
|
|
|
int octet_value = std::stoi(octet, nullptr, 2);
|
|
|
|
|
mask_str += std::to_string(octet_value) + (i < 3 ? "." : "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::make_pair(ip, mask_str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api_driver::obj::TerminalNetworkSettings::TerminalNetworkSettings() { loadDefaults(); }
|
|
|
|
|
|
|
|
|
|
api_driver::obj::TerminalNetworkSettings::TerminalNetworkSettings(const TerminalNetworkSettings &src) = default;
|
|
|
|
@@ -185,7 +183,7 @@ api_driver::obj::TerminalNetworkSettings::TerminalNetworkSettings(const Terminal
|
|
|
|
|
api_driver::obj::TerminalNetworkSettings & api_driver::obj::TerminalNetworkSettings::operator=(const TerminalNetworkSettings &src) = default;
|
|
|
|
|
|
|
|
|
|
void api_driver::obj::TerminalNetworkSettings::loadDefaults() {
|
|
|
|
|
managementIp = "0.0.0.0";
|
|
|
|
|
managementIp = "0.0.0.0/24";
|
|
|
|
|
managementGateway = "";
|
|
|
|
|
isL2 = true;
|
|
|
|
|
dataIp = "0.0.0.0";
|
|
|
|
@@ -197,8 +195,8 @@ void api_driver::obj::TerminalNetworkSettings::updateCallback(proxy::CpProxy &cp
|
|
|
|
|
loadDefaults();
|
|
|
|
|
try {
|
|
|
|
|
managementIp = cp.getNetwork("addr");
|
|
|
|
|
// s.managementIp += "/";
|
|
|
|
|
// s.managementIp += std::to_string(calculateSubnetMask(cp.getNetwork("mask")));
|
|
|
|
|
managementIp += "/";
|
|
|
|
|
managementIp += std::to_string(calculateSubnetMask(cp.getNetwork("mask")));
|
|
|
|
|
|
|
|
|
|
managementGateway = cp.getNetwork("gateway");
|
|
|
|
|
if (cp.getNetwork("mode") == "tun") {
|
|
|
|
@@ -228,12 +226,13 @@ void api_driver::obj::TerminalNetworkSettings::updateFromJson(const nlohmann::js
|
|
|
|
|
void api_driver::obj::TerminalNetworkSettings::store(proxy::CpProxy& cp) {
|
|
|
|
|
try {
|
|
|
|
|
cp.setNetwork("mode", isL2 ? "tap" : "tun");
|
|
|
|
|
cp.setNetwork("addr", managementIp);
|
|
|
|
|
auto [mAddr, mMask] = splitIpAndMask(managementIp);
|
|
|
|
|
cp.setNetwork("addr", mAddr);
|
|
|
|
|
cp.setNetwork("mask", mMask);
|
|
|
|
|
|
|
|
|
|
if (!isL2) {
|
|
|
|
|
cp.setNetwork("addr_data", dataIp);
|
|
|
|
|
}
|
|
|
|
|
cp.setNetwork("mask", "255.255.255.0");
|
|
|
|
|
cp.setNetwork("gateway", managementGateway);
|
|
|
|
|
|
|
|
|
|
// cp.setNetwork("data_mtu", std::to_string(dataMtu));
|
|
|
|
|