Merge branch 'master' into feature/wifi-5420--add-sysctl-context

Signed-off-by: Dmitry Dunaev <dmitry@opsfleet.com>
This commit is contained in:
Dmitry Dunaev
2021-11-18 12:36:49 +03:00
committed by Dmitry Dunaev
54 changed files with 1435 additions and 991 deletions

View File

@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.13)
project(owgw VERSION 2.4.0)
project(owgw VERSION 2.5.0)
set(CMAKE_CXX_STANDARD 17)
@@ -104,7 +104,7 @@ add_executable( owgw
src/TelemetryStream.cpp src/TelemetryStream.h
src/framework/ConfigurationValidator.cpp src/framework/ConfigurationValidator.h
src/ConfigurationCache.cpp src/ConfigurationCache.h
)
src/CapabilitiesCache.h)
if(NOT SMALL_BUILD)

View File

@@ -26,9 +26,9 @@ Poco may take several minutes depending on the platform you are building on.
### Ubuntu
These instructions have proven to work on Ubuntu 20.4.
```
sudo apt install git cmake g++ libssl-dev libmariadb-dev unixodbc-dev
sudo apt install git cmake g++ libssl-dev libmariadb-dev
sudo apt install libpq-dev libaprutil1-dev apache2-dev libboost-all-dev
sudo apt install librdkafka-dev liblua5.3-dev libmysqlclient-dev
sudo apt install librdkafka-dev libmysqlclient-dev default-libmysqlclient-dev
git clone https://github.com/stephb9959/poco
cd poco

2
build
View File

@@ -1 +1 @@
5
13

View File

@@ -25,7 +25,7 @@ spec:
annotations:
checksum/config: {{ include "owgw.config" . | sha256sum }}
{{- if .Values.podSecurityPolicy.enabled }}
kubernetes.io/psp: {{ include "owgw.fullname" . }}-{{ .Release.Namespace }}-unsafe-sysctl
kubernetes.io/psp: {{ include "owgw.fullname" . }}-{{ .Release.Namespace }}-owgw-unsafe-sysctl
{{- end }}
{{- with .Values.podAnnotations }}
{{- toYaml . | nindent 8 }}

View File

@@ -3,7 +3,7 @@
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: {{ include "owgw.fullname" . }}-{{ .Release.Namespace }}-unsafe-sysctl
name: {{ include "owgw.fullname" . }}-{{ .Release.Namespace }}-owgw-unsafe-sysctl
labels:
app.kubernetes.io/name: {{ include "owgw.name" . }}
helm.sh/chart: {{ include "owgw.chart" . }}

16
helm/templates/role.yaml Normal file
View File

@@ -0,0 +1,16 @@
{{- if .Values.podSecurityPolicy.enabled }}
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ include "owgw.fullname" . }}-owgw-use-unsafe-sysctl
rules:
- apiGroups:
- policy
resources:
- podsecuritypolicies
verbs:
- use
resourceNames:
- {{ include "owgw.fullname" . }}-{{ .Release.Namespace }}-owgw-unsafe-sysctl
{{- end }}

View File

@@ -0,0 +1,15 @@
{{- if .Values.podSecurityPolicy.enabled }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ include "owgw.fullname" . }}-owgw-use-unsafe-sysctl-to-default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ include "owgw.fullname" . }}-owgw-use-unsafe-sysctl
subjects:
- kind: ServiceAccount
name: default
namespace: {{ .Release.Namespace }}
{{- end }}

View File

@@ -2,7 +2,7 @@ openapi: 3.0.1
info:
title: uCentral gateway API
description: A process to manage configuration for devices.
version: 2.0.0
version: 2.5.0
license:
name: BSD3
url: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
@@ -51,6 +51,16 @@ components:
properties:
ErrorCode:
type: integer
enum:
- 0 # Success
- 1 # PASSWORD_CHANGE_REQUIRED,
- 2 # INVALID_CREDENTIALS,
- 3 # PASSWORD_ALREADY_USED,
- 4 # USERNAME_PENDING_VERIFICATION,
- 5 # PASSWORD_INVALID,
- 6 # INTERNAL_ERROR,
- 7 # ACCESS_DENIED,
- 8 # INVALID_TOKEN
ErrorDetails:
type: string
ErrorDescription:

77
src/CapabilitiesCache.h Normal file
View File

@@ -0,0 +1,77 @@
//
// Created by stephane bourque on 2021-11-17.
//
#pragma once
#include "framework/MicroService.h"
#include "nlohmann/json.hpp"
namespace OpenWifi {
static const std::string CapabilitiesCacheFileName{"/capabilities_cache.json"};
class CapabilitiesCache {
public:
static CapabilitiesCache * instance() {
static CapabilitiesCache * instance = new CapabilitiesCache;
return instance;
}
inline void Add(const std::string & DeviceType, const std::string & Platform) {
std::lock_guard G(Mutex_);
if(!Loaded_)
LoadIt();
auto P = Poco::toUpper(Platform);
auto Hint = Caps_.find(DeviceType);
if(Hint==Caps_.end()) {
Caps_.insert(std::make_pair(DeviceType,P));
SaveIt();
}
if(Hint->second != P) {
Hint->second = P;
SaveIt();
}
}
inline std::string Get(const std::string & DeviceType) {
std::lock_guard G(Mutex_);
if(!Loaded_) {
LoadIt();
}
auto Hint = Caps_.find(DeviceType);
if(Hint==Caps_.end())
return "AP";
return Hint->second;
}
private:
std::mutex Mutex_;
std::atomic_bool Loaded_=false;
std::map<std::string,std::string> Caps_;
inline void LoadIt() {
try {
std::ifstream i(MicroService::instance().DataDir()+CapabilitiesCacheFileName);
nlohmann::json cache;
i >> cache;
for(const auto &[Type,Platform]:cache.items()) {
std::cout << "Type:" << Type << std::endl;
Caps_[Type] = Platform;
}
} catch(...) {
}
Loaded_ = true;
}
inline void SaveIt() {
std::ofstream i(MicroService::instance().DataDir()+CapabilitiesCacheFileName);
nlohmann::json cache(Caps_);
i << cache;
}
};
}

View File

@@ -68,8 +68,8 @@ namespace OpenWifi {
bool GetCommand(uint64_t Id, const std::string & SerialNumber, CommandTag &T);
static CommandManager *instance() {
static CommandManager instance;
return &instance;
static CommandManager *instance_ = new CommandManager;
return instance_;
}
inline bool Running() const { return Running_; }

View File

@@ -26,11 +26,8 @@
#include "framework/MicroService.h"
namespace OpenWifi {
class Daemon *Daemon::instance_ = nullptr;
class Daemon *Daemon::instance() {
if (instance_ == nullptr) {
instance_ = new Daemon(vDAEMON_PROPERTIES_FILENAME,
static Daemon instance(vDAEMON_PROPERTIES_FILENAME,
vDAEMON_ROOT_ENV_VAR,
vDAEMON_CONFIG_ENV_VAR,
vDAEMON_APP_NAME,
@@ -47,35 +44,46 @@ namespace OpenWifi {
StorageArchiver(),
TelemetryStream()
});
return &instance;
}
return instance_;
}
static const std::vector<std::pair<std::string,std::string>> DefaultDeviceTypes{
{"cig_wf160d","AP"},
{"cig_wf188","AP"},
{"cig_wf188n","AP"},
{"cig_wf194c","AP"},
{"cig_wf194c4","AP"},
{"edgecore_eap101","AP"},
{"edgecore_eap102","AP"},
{"edgecore_ecs4100-12ph","AP"},
{"edgecore_ecw5211","AP"},
{"edgecore_ecw5410","AP"},
{"edgecore_oap100","AP"},
{"edgecore_spw2ac1200","SWITCH"},
{"edgecore_spw2ac1200-lan-poe","SWITCH"},
{"edgecore_ssw2ac2600","SWITCH"},
{"hfcl_ion4","AP"},
{"indio_um-305ac","AP"},
{"linksys_e8450-ubi","AP"},
{"linksys_ea6350","AP"},
{"linksys_ea6350-v4","AP"},
{"linksys_ea8300","AP"},
{"mikrotik_nand","AP"},
{"tp-link_ec420-g1","AP"},
{"tplink_cpe210_v3","AP"},
{"tplink_cpe510_v3","AP"},
{"tplink_eap225_outdoor_v1","AP"},
{"tplink_ec420","AP"},
{"tplink_ex227","AP"},
{"tplink_ex228","AP"},
{"tplink_ex447","AP"},
{"wallys_dr40x9","AP"}
};
void Daemon::initialize() {
Config::Config::Init();
AutoProvisioning_ = config().getBool("openwifi.autoprovisioning",false);
// DeviceTypeIdentifications_
Types::StringVec Keys;
config().keys("openwifi.devicetypes",Keys);
for(const auto & i:Keys)
{
std::string Line = config().getString("openwifi.devicetypes."+i);
auto P1 = Line.find_first_of(':');
auto Type = Line.substr(0, P1);
auto List = Line.substr(P1+1);
Types::StringVec Tokens = Utils::Split(List);
auto Entry = DeviceTypeIdentifications_.find(Type);
if(DeviceTypeIdentifications_.end() == Entry) {
std::set<std::string> S;
S.insert(Tokens.begin(),Tokens.end());
DeviceTypeIdentifications_[Type] = S;
} else {
Entry->second.insert(Tokens.begin(),Tokens.end());
}
}
DeviceTypes_ = DefaultDeviceTypes;
}
void MicroServicePostInitialization() {
@@ -83,22 +91,20 @@ namespace OpenWifi {
}
[[nodiscard]] std::string Daemon::IdentifyDevice(const std::string & Id ) const {
for(const auto &[Type,List]:DeviceTypeIdentifications_)
for(const auto &[DeviceType,Type]:DeviceTypes_)
{
if(List.find(Id)!=List.end())
if(Id == DeviceType)
return Type;
}
return "AP";
}
}
int main(int argc, char **argv) {
try {
auto App = OpenWifi::Daemon::instance();
auto ExitCode = App->run(argc, argv);
delete App;
return ExitCode;
} catch (Poco::Exception &exc) {

View File

@@ -54,9 +54,8 @@ namespace OpenWifi {
inline DeviceDashboard & GetDashboard() { return DB_; }
Poco::Logger & Log() { return Poco::Logger::get(AppName()); }
private:
static Daemon *instance_;
bool AutoProvisioning_ = false;
Types::StringMapStringSet DeviceTypeIdentifications_;
std::vector<std::pair<std::string,std::string>> DeviceTypes_;
DeviceDashboard DB_;
};

View File

@@ -16,10 +16,6 @@
namespace OpenWifi {
DeviceRegistry::DeviceRegistry() noexcept:
SubSystemServer("DeviceRegistry", "DevStatus", "devicestatus") {
}
int DeviceRegistry::Start() {
std::lock_guard Guard(Mutex_);
Logger_.notice("Starting ");
@@ -96,13 +92,13 @@ namespace OpenWifi {
}
}
GWObjects::ConnectionState * DeviceRegistry::Register(const std::string & SerialNumber, WSConnection *Ptr)
std::shared_ptr<DeviceRegistry::ConnectionEntry> DeviceRegistry::Register(const std::string & SerialNumber, WSConnection *Ptr)
{
std::lock_guard Guard(Mutex_);
auto Device = Devices_.find(SerialNumber);
if( Device == Devices_.end()) {
auto E = std::make_unique<ConnectionEntry>();
auto E = Devices_[SerialNumber] = std::make_shared<ConnectionEntry>();
E->WSConn_ = Ptr;
E->Conn_.SerialNumber = SerialNumber;
@@ -114,9 +110,7 @@ namespace OpenWifi {
E->Conn_.TX = 0 ;
E->Conn_.RX = 0;
E->Conn_.VerifiedCertificate = GWObjects::CertificateValidation::NO_CERTIFICATE;
auto R=&E->Conn_;
Devices_[SerialNumber] = std::move(E);
return R;
return E;
}
else
{
@@ -124,7 +118,7 @@ namespace OpenWifi {
Device->second->Conn_.Connected = true;
Device->second->Conn_.LastContact = std::time(nullptr);
Device->second->Conn_.VerifiedCertificate = GWObjects::CertificateValidation::NO_CERTIFICATE;
return &Device->second->Conn_;
return Device->second;
}
}
@@ -141,15 +135,7 @@ namespace OpenWifi {
void DeviceRegistry::UnRegister(const std::string & SerialNumber, WSConnection *Ptr) {
std::lock_guard Guard(Mutex_);
auto Device = Devices_.find(SerialNumber);
if( Device != Devices_.end() && Device->second->WSConn_==Ptr) {
Device->second->Conn_.Address = "";
Device->second->WSConn_ = nullptr;
Device->second->Conn_.Connected = false;
Device->second->Conn_.VerifiedCertificate = GWObjects::NO_CERTIFICATE;
}
Devices_.erase(SerialNumber);
}
bool DeviceRegistry::SendFrame(const std::string & SerialNumber, const std::string & Payload) {
@@ -238,12 +224,12 @@ namespace OpenWifi {
std::lock_guard Guard(Mutex_);
for(auto const &[SerialNumber,Connection]:Devices_) {
Types::UpdateCountedMap(D.status, Connection->Conn_.Connected ? "connected" : "not connected");
Types::UpdateCountedMap(D.vendors, OUIServer()->GetManufacturer(SerialNumber));
Types::UpdateCountedMap(D.certificates, ComputeCertificateTag(Connection->Conn_.VerifiedCertificate));
Types::UpdateCountedMap(D.lastContact, ComputeUpLastContactTag(Connection->Conn_.LastContact));
Types::UpdateCountedMap(D.healths, ComputeSanityTag(Connection->LastHealthcheck.Sanity));
Types::UpdateCountedMap(D.deviceType, Connection->Conn_.Compatible);
UpdateCountedMap(D.status, Connection->Conn_.Connected ? "connected" : "not connected");
UpdateCountedMap(D.vendors, OUIServer()->GetManufacturer(SerialNumber));
UpdateCountedMap(D.certificates, ComputeCertificateTag(Connection->Conn_.VerifiedCertificate));
UpdateCountedMap(D.lastContact, ComputeUpLastContactTag(Connection->Conn_.LastContact));
UpdateCountedMap(D.healths, ComputeSanityTag(Connection->LastHealthcheck.Sanity));
UpdateCountedMap(D.deviceType, Connection->Conn_.Compatible);
if(!Connection->LastStats.empty()) {
Poco::JSON::Parser P;
@@ -252,21 +238,21 @@ namespace OpenWifi {
if(RawObject->has("unit")) {
auto Unit = RawObject->getObject("unit");
if (Unit->has("uptime")) {
Types::UpdateCountedMap(D.upTimes, ComputeUpTimeTag(Unit->get("uptime")));
UpdateCountedMap(D.upTimes, ComputeUpTimeTag(Unit->get("uptime")));
}
if (Unit->has("memory")) {
auto Memory = Unit->getObject("memory");
uint64_t Free = Memory->get("free");
uint64_t Total = Memory->get("total");
Types::UpdateCountedMap(D.memoryUsed, ComputeFreeMemoryTag(Free, Total));
UpdateCountedMap(D.memoryUsed, ComputeFreeMemoryTag(Free, Total));
}
if (Unit->has("load")) {
auto Load = Unit->getArray("load");
Types::UpdateCountedMap(D.load1,
UpdateCountedMap(D.load1,
ComputeLoadTag(Load->getElement<uint64_t>(0)));
Types::UpdateCountedMap(D.load5,
UpdateCountedMap(D.load5,
ComputeLoadTag(Load->getElement<uint64_t>(1)));
Types::UpdateCountedMap(D.load15,
UpdateCountedMap(D.load15,
ComputeLoadTag(Load->getElement<uint64_t>(2)));
}
}

View File

@@ -29,8 +29,8 @@ namespace OpenWifi {
};
static DeviceRegistry *instance() {
static DeviceRegistry instance;
return &instance;
static DeviceRegistry *instance_ = new DeviceRegistry;
return instance_;
}
int Start() override;
@@ -41,7 +41,7 @@ namespace OpenWifi {
void SetState(const std::string & SerialNumber, GWObjects::ConnectionState & State);
bool GetHealthcheck(const std::string &SerialNumber, GWObjects::HealthCheck & CheckData);
void SetHealthcheck(const std::string &SerialNumber, const GWObjects::HealthCheck &H);
GWObjects::ConnectionState * Register(const std::string & SerialNumber, WSConnection *);
std::shared_ptr<ConnectionEntry> Register(const std::string & SerialNumber, WSConnection *);
void UnRegister(const std::string & SerialNumber, WSConnection *);
bool SendCommand(GWObjects::CommandDetails & Command);
bool Connected(const std::string & SerialNumber);
@@ -51,9 +51,11 @@ namespace OpenWifi {
bool AnalyzeRegistry(GWObjects::Dashboard &D);
private:
std::map<std::string,std::unique_ptr<ConnectionEntry>> Devices_;
std::map<std::string,std::shared_ptr<ConnectionEntry>> Devices_;
DeviceRegistry() noexcept;
DeviceRegistry() noexcept:
SubSystemServer("DeviceRegistry", "DevStatus", "devicestatus") {
}
};
inline DeviceRegistry * DeviceRegistry() { return DeviceRegistry::instance(); }

View File

@@ -30,8 +30,8 @@ namespace OpenWifi {
const std::string & Path() { return Path_; };
static FileUploader *instance() {
static FileUploader instance;
return &instance;
static FileUploader * instance_ = new FileUploader;
return instance_;
}
[[nodiscard]] inline uint64_t MaxSize() const { return MaxSize_; }

View File

@@ -15,8 +15,8 @@ namespace OpenWifi {
typedef std::map<uint64_t,std::string> OUIMap;
static OUIServer *instance() {
static OUIServer instance;
return &instance;
static OUIServer *instance_ = new OUIServer;
return instance_;
}
int Start() override;

View File

@@ -15,7 +15,10 @@ namespace OpenWifi {
Poco::JSON::Array Arr;
Poco::JSON::Object Answer;
if(StorageService()->GetBlackListDevices(QB_.Offset, QB_.Limit, Devices)) {
if(QB_.CountOnly) {
auto Count = StorageService()->GetBlackListDeviceCount();
return ReturnCountOnly(Count);
} else if(StorageService()->GetBlackListDevices(QB_.Offset, QB_.Limit, Devices)) {
for(const auto &i:Devices) {
Poco::JSON::Object O;
i.to_json(O);

View File

@@ -15,6 +15,12 @@
namespace OpenWifi {
void RESTAPI_default_configurations::DoGet() {
if(QB_.CountOnly) {
auto Count = StorageService()->GetDefaultConfigurationsCount();
return ReturnCountOnly(Count);
}
std::vector<GWObjects::DefaultConfiguration> DefConfigs;
StorageService()->GetDefaultConfigurations(QB_.Offset, QB_.Limit, DefConfigs);

View File

@@ -204,7 +204,7 @@ void RESTAPI_device_commandHandler::Configure() {
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.SubmittedBy = UserInfo_.webtoken.username_;
Cmd.Command = uCentralProtocol::CONFIGURE;
Cmd.RunAt = When;
@@ -246,7 +246,7 @@ void RESTAPI_device_commandHandler::Upgrade() {
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.SubmittedBy = UserInfo_.webtoken.username_;
Cmd.Command = uCentralProtocol::UPGRADE;
Cmd.RunAt = When;
@@ -357,7 +357,7 @@ void RESTAPI_device_commandHandler::ExecuteCommand() {
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.SubmittedBy = UserInfo_.webtoken.username_;
Cmd.Command = Command;
Cmd.Custom = 1;
@@ -397,7 +397,7 @@ void RESTAPI_device_commandHandler::Reboot() {
uint64_t When = GetWhen(Obj);
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.SubmittedBy = UserInfo_.webtoken.username_;
Cmd.Command = uCentralProtocol::REBOOT;
Cmd.RunAt = When;
@@ -434,7 +434,7 @@ void RESTAPI_device_commandHandler::Factory() {
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.SubmittedBy = UserInfo_.webtoken.username_;
Cmd.Command = uCentralProtocol::FACTORY;
Cmd.RunAt = When;
@@ -480,7 +480,7 @@ void RESTAPI_device_commandHandler::LEDs() {
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.SubmittedBy = UserInfo_.webtoken.username_;
Cmd.Command = uCentralProtocol::LEDS;
Cmd.RunAt = When;
@@ -519,7 +519,7 @@ void RESTAPI_device_commandHandler::Trace() {
auto Network = GetS(RESTAPI::Protocol::NETWORK, Obj);
auto Interface = GetS(RESTAPI::Protocol::INTERFACE, Obj);
auto UUID = MicroService::instance().CreateUUID();
auto UUID = MicroService::CreateUUID();
auto URI = FileUploader()->FullName() + UUID;
GWObjects::CommandDetails Cmd;
@@ -567,7 +567,7 @@ void RESTAPI_device_commandHandler::WifiScan() {
(!Obj->has(RESTAPI::Protocol::BANDS) &&
!Obj->has(RESTAPI::Protocol::CHANNELS)))) {
bool Verbose = GetB(RESTAPI::Protocol::VERBOSE, Obj);
auto UUID = MicroService::instance().CreateUUID();
auto UUID = MicroService::CreateUUID();
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
@@ -618,7 +618,7 @@ void RESTAPI_device_commandHandler::EventQueue() {
auto Types = Obj->getArray(RESTAPI::Protocol::TYPES);
auto UUID = MicroService::instance().CreateUUID();
auto UUID = MicroService::CreateUUID();
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
@@ -664,7 +664,7 @@ void RESTAPI_device_commandHandler::MakeRequest() {
Cmd.SerialNumber = SerialNumber_;
Cmd.SubmittedBy = UserInfo_.webtoken.username_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.Command = uCentralProtocol::REQUEST;
Cmd.RunAt = When;
@@ -689,7 +689,7 @@ void RESTAPI_device_commandHandler::MakeRequest() {
if (MicroService::instance().ConfigGetString("rtty.enabled", "false") == "true") {
GWObjects::Device Device;
if (StorageService()->GetDevice(SerialNumber_, Device)) {
auto CommandUUID = MicroService::instance().CreateUUID();
auto CommandUUID = MicroService::CreateUUID();
GWObjects::RttySessionDetails Rtty{
.SerialNumber = SerialNumber_,

View File

@@ -46,7 +46,8 @@ namespace OpenWifi {
if(!Authenticated) {
std::string Frame{IncomingFrame.begin()};
auto Tokens = Utils::Split(Frame,':');
if(Tokens.size()==2 && AuthClient()->IsTokenAuthorized(Tokens[1], UserInfo_)) {
bool Expired=false;
if(Tokens.size()==2 && AuthClient()->IsAuthorized(Tokens[1], UserInfo_, Expired)) {
Authenticated=true;
std::string S{"Welcome! Bienvenue! Bienvenidos!"};
WS.sendFrame(S.c_str(),S.size());

View File

@@ -12,6 +12,7 @@
#include "Daemon.h"
#ifdef TIP_GATEWAY_SERVICE
#include "DeviceRegistry.h"
#include "CapabilitiesCache.h"
#endif
#include "RESTAPI_GWobjects.h"
@@ -26,7 +27,7 @@ namespace OpenWifi::GWObjects {
void Device::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"serialNumber", SerialNumber);
#ifdef TIP_GATEWAY_SERVICE
field_to_json(Obj,"deviceType", Daemon::instance()->IdentifyDevice(Compatible));
field_to_json(Obj,"deviceType", CapabilitiesCache::instance()->Get(Compatible));
#endif
field_to_json(Obj,"macAddress", MACAddress);
field_to_json(Obj,"manufacturer", Manufacturer);

View File

@@ -10,27 +10,30 @@
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
using OpenWifi::RESTAPI_utils::field_to_json;
using OpenWifi::RESTAPI_utils::field_from_json;
namespace OpenWifi::ProvObjects {
void ObjectInfo::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json(Obj,"id",id);
RESTAPI_utils::field_to_json(Obj,"name",name);
RESTAPI_utils::field_to_json(Obj,"description",description);
RESTAPI_utils::field_to_json(Obj,"created",created);
RESTAPI_utils::field_to_json(Obj,"modified",modified);
RESTAPI_utils::field_to_json(Obj,"notes",notes);
RESTAPI_utils::field_to_json(Obj,"tags",tags);
field_to_json(Obj,"id",id);
field_to_json(Obj,"name",name);
field_to_json(Obj,"description",description);
field_to_json(Obj,"created",created);
field_to_json(Obj,"modified",modified);
field_to_json(Obj,"notes",notes);
field_to_json(Obj,"tags",tags);
}
bool ObjectInfo::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json(Obj,"id",id);
RESTAPI_utils::field_from_json(Obj,"name",name);
RESTAPI_utils::field_from_json(Obj,"description",description);
RESTAPI_utils::field_from_json(Obj,"created",created);
RESTAPI_utils::field_from_json(Obj,"modified",modified);
RESTAPI_utils::field_from_json(Obj,"notes",notes);
RESTAPI_utils::field_from_json(Obj,"tags",tags);
field_from_json(Obj,"id",id);
field_from_json(Obj,"name",name);
field_from_json(Obj,"description",description);
field_from_json(Obj,"created",created);
field_from_json(Obj,"modified",modified);
field_from_json(Obj,"notes",notes);
field_from_json(Obj,"tags",tags);
return true;
} catch(...) {
@@ -39,18 +42,18 @@ namespace OpenWifi::ProvObjects {
}
void ManagementPolicyEntry::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json( Obj,"users",users);
RESTAPI_utils::field_to_json( Obj,"resources",resources);
RESTAPI_utils::field_to_json( Obj,"access",access);
RESTAPI_utils::field_to_json( Obj,"policy",policy);
field_to_json( Obj,"users",users);
field_to_json( Obj,"resources",resources);
field_to_json( Obj,"access",access);
field_to_json( Obj,"policy",policy);
}
bool ManagementPolicyEntry::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json( Obj,"users",users);
RESTAPI_utils::field_from_json( Obj,"resources",resources);
RESTAPI_utils::field_from_json( Obj,"access",access);
RESTAPI_utils::field_from_json( Obj,"policy",policy);
field_from_json( Obj,"users",users);
field_from_json( Obj,"resources",resources);
field_from_json( Obj,"access",access);
field_from_json( Obj,"policy",policy);
return true;
} catch(...) {
@@ -60,17 +63,17 @@ namespace OpenWifi::ProvObjects {
void ManagementPolicy::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json(Obj, "entries", entries);
RESTAPI_utils::field_to_json(Obj, "inUse", inUse);
RESTAPI_utils::field_to_json(Obj, "entity", entity);
field_to_json(Obj, "entries", entries);
field_to_json(Obj, "inUse", inUse);
field_to_json(Obj, "entity", entity);
}
bool ManagementPolicy::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
RESTAPI_utils::field_from_json(Obj, "entries", entries);
RESTAPI_utils::field_from_json(Obj, "inUse", inUse);
RESTAPI_utils::field_from_json(Obj, "entity", entity);
field_from_json(Obj, "entries", entries);
field_from_json(Obj, "inUse", inUse);
field_from_json(Obj, "entity", entity);
return true;
} catch(...) {
@@ -80,31 +83,31 @@ namespace OpenWifi::ProvObjects {
void Entity::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json( Obj,"parent",parent);
RESTAPI_utils::field_to_json( Obj,"venues",venues);
RESTAPI_utils::field_to_json( Obj,"children",children);
RESTAPI_utils::field_to_json( Obj,"contacts",contacts);
RESTAPI_utils::field_to_json( Obj,"locations",locations);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
RESTAPI_utils::field_to_json( Obj,"deviceConfiguration",deviceConfiguration);
RESTAPI_utils::field_to_json( Obj,"devices",devices);
RESTAPI_utils::field_to_json( Obj,"rrm",rrm);
RESTAPI_utils::field_to_json( Obj,"sourceIP",sourceIP);
field_to_json( Obj,"parent",parent);
field_to_json( Obj,"venues",venues);
field_to_json( Obj,"children",children);
field_to_json( Obj,"contacts",contacts);
field_to_json( Obj,"locations",locations);
field_to_json( Obj,"managementPolicy",managementPolicy);
field_to_json( Obj,"deviceConfiguration",deviceConfiguration);
field_to_json( Obj,"devices",devices);
field_to_json( Obj,"rrm",rrm);
field_to_json( Obj,"sourceIP",sourceIP);
}
bool Entity::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
RESTAPI_utils::field_from_json( Obj,"parent",parent);
RESTAPI_utils::field_from_json( Obj,"venues",venues);
RESTAPI_utils::field_from_json( Obj,"children",children);
RESTAPI_utils::field_from_json( Obj,"contacts",contacts);
RESTAPI_utils::field_from_json( Obj,"locations",locations);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
RESTAPI_utils::field_from_json( Obj,"deviceConfiguration",deviceConfiguration);
RESTAPI_utils::field_from_json( Obj,"devices",devices);
RESTAPI_utils::field_from_json( Obj,"rrm",rrm);
RESTAPI_utils::field_from_json( Obj,"sourceIP",sourceIP);
field_from_json( Obj,"parent",parent);
field_from_json( Obj,"venues",venues);
field_from_json( Obj,"children",children);
field_from_json( Obj,"contacts",contacts);
field_from_json( Obj,"locations",locations);
field_from_json( Obj,"managementPolicy",managementPolicy);
field_from_json( Obj,"deviceConfiguration",deviceConfiguration);
field_from_json( Obj,"devices",devices);
field_from_json( Obj,"rrm",rrm);
field_from_json( Obj,"sourceIP",sourceIP);
return true;
} catch(...) {
@@ -113,14 +116,14 @@ namespace OpenWifi::ProvObjects {
}
void DiGraphEntry::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json( Obj,"parent",parent);
RESTAPI_utils::field_to_json( Obj,"child",child);
field_to_json( Obj,"parent",parent);
field_to_json( Obj,"child",child);
}
bool DiGraphEntry::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json( Obj,"parent",parent);
RESTAPI_utils::field_from_json( Obj,"child",child);
field_from_json( Obj,"parent",parent);
field_from_json( Obj,"child",child);
return true;
} catch (...) {
@@ -130,37 +133,37 @@ namespace OpenWifi::ProvObjects {
void Venue::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json( Obj,"parent",parent);
RESTAPI_utils::field_to_json( Obj,"entity",entity);
RESTAPI_utils::field_to_json( Obj,"children",children);
RESTAPI_utils::field_to_json( Obj,"devices",devices);
RESTAPI_utils::field_to_json( Obj,"topology",topology);
RESTAPI_utils::field_to_json( Obj,"parent",parent);
RESTAPI_utils::field_to_json( Obj,"design",design);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
RESTAPI_utils::field_to_json( Obj,"deviceConfiguration",deviceConfiguration);
RESTAPI_utils::field_to_json( Obj,"contact",contact);
RESTAPI_utils::field_to_json( Obj,"location",location);
RESTAPI_utils::field_to_json( Obj,"rrm",rrm);
RESTAPI_utils::field_to_json( Obj,"sourceIP",sourceIP);
field_to_json( Obj,"parent",parent);
field_to_json( Obj,"entity",entity);
field_to_json( Obj,"children",children);
field_to_json( Obj,"devices",devices);
field_to_json( Obj,"topology",topology);
field_to_json( Obj,"parent",parent);
field_to_json( Obj,"design",design);
field_to_json( Obj,"managementPolicy",managementPolicy);
field_to_json( Obj,"deviceConfiguration",deviceConfiguration);
field_to_json( Obj,"contact",contact);
field_to_json( Obj,"location",location);
field_to_json( Obj,"rrm",rrm);
field_to_json( Obj,"sourceIP",sourceIP);
}
bool Venue::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
RESTAPI_utils::field_from_json( Obj,"parent",parent);
RESTAPI_utils::field_from_json( Obj,"entity",entity);
RESTAPI_utils::field_from_json( Obj,"children",children);
RESTAPI_utils::field_from_json( Obj,"devices",devices);
RESTAPI_utils::field_from_json( Obj,"topology",topology);
RESTAPI_utils::field_from_json( Obj,"parent",parent);
RESTAPI_utils::field_from_json( Obj,"design",design);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
RESTAPI_utils::field_from_json( Obj,"deviceConfiguration",deviceConfiguration);
RESTAPI_utils::field_from_json( Obj,"contact",contact);
RESTAPI_utils::field_from_json( Obj,"location",location);
RESTAPI_utils::field_from_json( Obj,"rrm",rrm);
RESTAPI_utils::field_from_json( Obj,"sourceIP",sourceIP);
field_from_json( Obj,"parent",parent);
field_from_json( Obj,"entity",entity);
field_from_json( Obj,"children",children);
field_from_json( Obj,"devices",devices);
field_from_json( Obj,"topology",topology);
field_from_json( Obj,"parent",parent);
field_from_json( Obj,"design",design);
field_from_json( Obj,"managementPolicy",managementPolicy);
field_from_json( Obj,"deviceConfiguration",deviceConfiguration);
field_from_json( Obj,"contact",contact);
field_from_json( Obj,"location",location);
field_from_json( Obj,"rrm",rrm);
field_from_json( Obj,"sourceIP",sourceIP);
return true;
} catch (...) {
@@ -169,16 +172,16 @@ namespace OpenWifi::ProvObjects {
}
void UserInfoDigest::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json( Obj,"id",id);
RESTAPI_utils::field_to_json( Obj,"entity",loginId);
RESTAPI_utils::field_to_json( Obj,"children",userType);
field_to_json( Obj,"id",id);
field_to_json( Obj,"entity",loginId);
field_to_json( Obj,"children",userType);
}
bool UserInfoDigest::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json( Obj,"id",id);
RESTAPI_utils::field_from_json( Obj,"entity",loginId);
RESTAPI_utils::field_from_json( Obj,"children",userType);
field_from_json( Obj,"id",id);
field_from_json( Obj,"entity",loginId);
field_from_json( Obj,"children",userType);
return true;
} catch(...) {
}
@@ -187,17 +190,17 @@ namespace OpenWifi::ProvObjects {
void ManagementRole::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
RESTAPI_utils::field_to_json( Obj,"users",users);
RESTAPI_utils::field_to_json( Obj,"entity",entity);
field_to_json( Obj,"managementPolicy",managementPolicy);
field_to_json( Obj,"users",users);
field_to_json( Obj,"entity",entity);
}
bool ManagementRole::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
RESTAPI_utils::field_from_json( Obj,"users",users);
RESTAPI_utils::field_from_json( Obj,"entity",entity);
field_from_json( Obj,"managementPolicy",managementPolicy);
field_from_json( Obj,"users",users);
field_from_json( Obj,"entity",entity);
return true;
} catch(...) {
}
@@ -206,39 +209,39 @@ namespace OpenWifi::ProvObjects {
void Location::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json( Obj,"type",OpenWifi::ProvObjects::to_string(type));
RESTAPI_utils::field_to_json( Obj,"buildingName",buildingName);
RESTAPI_utils::field_to_json( Obj,"addressLines",addressLines);
RESTAPI_utils::field_to_json( Obj,"city",city);
RESTAPI_utils::field_to_json( Obj,"state",state);
RESTAPI_utils::field_to_json( Obj,"postal",postal);
RESTAPI_utils::field_to_json( Obj,"country",country);
RESTAPI_utils::field_to_json( Obj,"phones",phones);
RESTAPI_utils::field_to_json( Obj,"mobiles",mobiles);
RESTAPI_utils::field_to_json( Obj,"geoCode",geoCode);
RESTAPI_utils::field_to_json( Obj,"inUse",inUse);
RESTAPI_utils::field_to_json( Obj,"entity",entity);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
field_to_json( Obj,"type",OpenWifi::ProvObjects::to_string(type));
field_to_json( Obj,"buildingName",buildingName);
field_to_json( Obj,"addressLines",addressLines);
field_to_json( Obj,"city",city);
field_to_json( Obj,"state",state);
field_to_json( Obj,"postal",postal);
field_to_json( Obj,"country",country);
field_to_json( Obj,"phones",phones);
field_to_json( Obj,"mobiles",mobiles);
field_to_json( Obj,"geoCode",geoCode);
field_to_json( Obj,"inUse",inUse);
field_to_json( Obj,"entity",entity);
field_to_json( Obj,"managementPolicy",managementPolicy);
}
bool Location::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
std::string tmp_type;
RESTAPI_utils::field_from_json( Obj,"type", tmp_type);
field_from_json( Obj,"type", tmp_type);
type = location_from_string(tmp_type);
RESTAPI_utils::field_from_json( Obj,"buildingName",buildingName);
RESTAPI_utils::field_from_json( Obj,"addressLines",addressLines);
RESTAPI_utils::field_from_json( Obj,"city",city);
RESTAPI_utils::field_from_json( Obj,"state",state);
RESTAPI_utils::field_from_json( Obj,"postal",postal);
RESTAPI_utils::field_from_json( Obj,"country",country);
RESTAPI_utils::field_from_json( Obj,"phones",phones);
RESTAPI_utils::field_from_json( Obj,"mobiles",mobiles);
RESTAPI_utils::field_from_json( Obj,"geoCode",geoCode);
RESTAPI_utils::field_from_json( Obj,"inUse",inUse);
RESTAPI_utils::field_from_json( Obj,"entity",entity);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
field_from_json( Obj,"buildingName",buildingName);
field_from_json( Obj,"addressLines",addressLines);
field_from_json( Obj,"city",city);
field_from_json( Obj,"state",state);
field_from_json( Obj,"postal",postal);
field_from_json( Obj,"country",country);
field_from_json( Obj,"phones",phones);
field_from_json( Obj,"mobiles",mobiles);
field_from_json( Obj,"geoCode",geoCode);
field_from_json( Obj,"inUse",inUse);
field_from_json( Obj,"entity",entity);
field_from_json( Obj,"managementPolicy",managementPolicy);
return true;
} catch (...) {
@@ -248,43 +251,43 @@ namespace OpenWifi::ProvObjects {
void Contact::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json( Obj,"type", to_string(type));
RESTAPI_utils::field_to_json( Obj,"title",title);
RESTAPI_utils::field_to_json( Obj,"salutation",salutation);
RESTAPI_utils::field_to_json( Obj,"firstname",firstname);
RESTAPI_utils::field_to_json( Obj,"lastname",lastname);
RESTAPI_utils::field_to_json( Obj,"initials",initials);
RESTAPI_utils::field_to_json( Obj,"visual",visual);
RESTAPI_utils::field_to_json( Obj,"mobiles",mobiles);
RESTAPI_utils::field_to_json( Obj,"phones",phones);
RESTAPI_utils::field_to_json( Obj,"primaryEmail",primaryEmail);
RESTAPI_utils::field_to_json( Obj,"secondaryEmail",secondaryEmail);
RESTAPI_utils::field_to_json( Obj,"accessPIN",accessPIN);
RESTAPI_utils::field_to_json( Obj,"inUse",inUse);
RESTAPI_utils::field_to_json( Obj,"entity",entity);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
field_to_json( Obj,"type", to_string(type));
field_to_json( Obj,"title",title);
field_to_json( Obj,"salutation",salutation);
field_to_json( Obj,"firstname",firstname);
field_to_json( Obj,"lastname",lastname);
field_to_json( Obj,"initials",initials);
field_to_json( Obj,"visual",visual);
field_to_json( Obj,"mobiles",mobiles);
field_to_json( Obj,"phones",phones);
field_to_json( Obj,"primaryEmail",primaryEmail);
field_to_json( Obj,"secondaryEmail",secondaryEmail);
field_to_json( Obj,"accessPIN",accessPIN);
field_to_json( Obj,"inUse",inUse);
field_to_json( Obj,"entity",entity);
field_to_json( Obj,"managementPolicy",managementPolicy);
}
bool Contact::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
std::string tmp_type;
RESTAPI_utils::field_from_json( Obj,"type", tmp_type);
field_from_json( Obj,"type", tmp_type);
type = contact_from_string(tmp_type);
RESTAPI_utils::field_from_json( Obj,"title",title);
RESTAPI_utils::field_from_json( Obj,"salutation",salutation);
RESTAPI_utils::field_from_json( Obj,"firstname",firstname);
RESTAPI_utils::field_from_json( Obj,"lastname",lastname);
RESTAPI_utils::field_from_json( Obj,"initials",initials);
RESTAPI_utils::field_from_json( Obj,"visual",visual);
RESTAPI_utils::field_from_json( Obj,"mobiles",mobiles);
RESTAPI_utils::field_from_json( Obj,"phones",phones);
RESTAPI_utils::field_from_json( Obj,"primaryEmail",primaryEmail);
RESTAPI_utils::field_from_json( Obj,"secondaryEmail",secondaryEmail);
RESTAPI_utils::field_from_json( Obj,"accessPIN",accessPIN);
RESTAPI_utils::field_from_json( Obj,"inUse",inUse);
RESTAPI_utils::field_from_json( Obj,"entity",entity);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
field_from_json( Obj,"title",title);
field_from_json( Obj,"salutation",salutation);
field_from_json( Obj,"firstname",firstname);
field_from_json( Obj,"lastname",lastname);
field_from_json( Obj,"initials",initials);
field_from_json( Obj,"visual",visual);
field_from_json( Obj,"mobiles",mobiles);
field_from_json( Obj,"phones",phones);
field_from_json( Obj,"primaryEmail",primaryEmail);
field_from_json( Obj,"secondaryEmail",secondaryEmail);
field_from_json( Obj,"accessPIN",accessPIN);
field_from_json( Obj,"inUse",inUse);
field_from_json( Obj,"entity",entity);
field_from_json( Obj,"managementPolicy",managementPolicy);
return true;
} catch (...) {
@@ -294,35 +297,35 @@ namespace OpenWifi::ProvObjects {
void InventoryTag::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json(Obj, "serialNumber", serialNumber);
RESTAPI_utils::field_to_json(Obj, "venue", venue);
RESTAPI_utils::field_to_json(Obj, "entity", entity);
RESTAPI_utils::field_to_json(Obj, "subscriber", subscriber);
RESTAPI_utils::field_to_json(Obj, "deviceType", deviceType);
RESTAPI_utils::field_to_json(Obj, "qrCode", qrCode);
RESTAPI_utils::field_to_json(Obj, "geoCode", geoCode);
RESTAPI_utils::field_to_json(Obj, "location", location);
RESTAPI_utils::field_to_json(Obj, "contact", contact);
RESTAPI_utils::field_to_json( Obj,"deviceConfiguration",deviceConfiguration);
RESTAPI_utils::field_to_json( Obj,"rrm",rrm);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
field_to_json(Obj, "serialNumber", serialNumber);
field_to_json(Obj, "venue", venue);
field_to_json(Obj, "entity", entity);
field_to_json(Obj, "subscriber", subscriber);
field_to_json(Obj, "deviceType", deviceType);
field_to_json(Obj, "qrCode", qrCode);
field_to_json(Obj, "geoCode", geoCode);
field_to_json(Obj, "location", location);
field_to_json(Obj, "contact", contact);
field_to_json( Obj,"deviceConfiguration",deviceConfiguration);
field_to_json( Obj,"rrm",rrm);
field_to_json( Obj,"managementPolicy",managementPolicy);
}
bool InventoryTag::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
RESTAPI_utils::field_from_json( Obj,"serialNumber",serialNumber);
RESTAPI_utils::field_from_json( Obj,"venue",venue);
RESTAPI_utils::field_from_json( Obj,"entity",entity);
RESTAPI_utils::field_from_json( Obj,"subscriber",subscriber);
RESTAPI_utils::field_from_json( Obj,"deviceType",deviceType);
RESTAPI_utils::field_from_json(Obj, "qrCode", qrCode);
RESTAPI_utils::field_from_json( Obj,"geoCode",geoCode);
RESTAPI_utils::field_from_json( Obj,"location",location);
RESTAPI_utils::field_from_json( Obj,"contact",contact);
RESTAPI_utils::field_from_json( Obj,"deviceConfiguration",deviceConfiguration);
RESTAPI_utils::field_from_json( Obj,"rrm",rrm);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
field_from_json( Obj,"serialNumber",serialNumber);
field_from_json( Obj,"venue",venue);
field_from_json( Obj,"entity",entity);
field_from_json( Obj,"subscriber",subscriber);
field_from_json( Obj,"deviceType",deviceType);
field_from_json(Obj, "qrCode", qrCode);
field_from_json( Obj,"geoCode",geoCode);
field_from_json( Obj,"location",location);
field_from_json( Obj,"contact",contact);
field_from_json( Obj,"deviceConfiguration",deviceConfiguration);
field_from_json( Obj,"rrm",rrm);
field_from_json( Obj,"managementPolicy",managementPolicy);
return true;
} catch(...) {
@@ -331,18 +334,18 @@ namespace OpenWifi::ProvObjects {
}
void DeviceConfigurationElement::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json( Obj,"name", name);
RESTAPI_utils::field_to_json( Obj,"description", description);
RESTAPI_utils::field_to_json( Obj,"weight", weight);
RESTAPI_utils::field_to_json( Obj,"configuration", configuration);
field_to_json( Obj,"name", name);
field_to_json( Obj,"description", description);
field_to_json( Obj,"weight", weight);
field_to_json( Obj,"configuration", configuration);
}
bool DeviceConfigurationElement::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json( Obj,"name",name);
RESTAPI_utils::field_from_json( Obj,"description",description);
RESTAPI_utils::field_from_json( Obj,"weight",weight);
RESTAPI_utils::field_from_json( Obj,"configuration",configuration);
field_from_json( Obj,"name",name);
field_from_json( Obj,"description",description);
field_from_json( Obj,"weight",weight);
field_from_json( Obj,"configuration",configuration);
return true;
} catch(...) {
@@ -352,27 +355,27 @@ namespace OpenWifi::ProvObjects {
void DeviceConfiguration::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
RESTAPI_utils::field_to_json( Obj,"deviceTypes",deviceTypes);
RESTAPI_utils::field_to_json( Obj,"configuration",configuration);
RESTAPI_utils::field_to_json( Obj,"inUse",inUse);
RESTAPI_utils::field_to_json( Obj,"variables",variables);
RESTAPI_utils::field_to_json( Obj,"rrm",rrm);
RESTAPI_utils::field_to_json( Obj,"firmwareUpgrade",firmwareUpgrade);
RESTAPI_utils::field_to_json( Obj,"firmwareRCOnly",firmwareRCOnly);
field_to_json( Obj,"managementPolicy",managementPolicy);
field_to_json( Obj,"deviceTypes",deviceTypes);
field_to_json( Obj,"configuration",configuration);
field_to_json( Obj,"inUse",inUse);
field_to_json( Obj,"variables",variables);
field_to_json( Obj,"rrm",rrm);
field_to_json( Obj,"firmwareUpgrade",firmwareUpgrade);
field_to_json( Obj,"firmwareRCOnly",firmwareRCOnly);
}
bool DeviceConfiguration::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
RESTAPI_utils::field_from_json( Obj,"deviceTypes",deviceTypes);
RESTAPI_utils::field_from_json( Obj,"configuration",configuration);
RESTAPI_utils::field_from_json( Obj,"inUse",inUse);
RESTAPI_utils::field_from_json( Obj,"variables",variables);
RESTAPI_utils::field_from_json( Obj,"rrm",rrm);
RESTAPI_utils::field_from_json( Obj,"firmwareUpgrade",firmwareUpgrade);
RESTAPI_utils::field_from_json( Obj,"firmwareRCOnly",firmwareRCOnly);
field_from_json( Obj,"managementPolicy",managementPolicy);
field_from_json( Obj,"deviceTypes",deviceTypes);
field_from_json( Obj,"configuration",configuration);
field_from_json( Obj,"inUse",inUse);
field_from_json( Obj,"variables",variables);
field_from_json( Obj,"rrm",rrm);
field_from_json( Obj,"firmwareUpgrade",firmwareUpgrade);
field_from_json( Obj,"firmwareRCOnly",firmwareRCOnly);
return true;
} catch(...) {
@@ -381,8 +384,8 @@ namespace OpenWifi::ProvObjects {
}
void Report::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json(Obj, "snapshot", snapShot);
RESTAPI_utils::field_to_json(Obj, "devices", tenants);
field_to_json(Obj, "snapshot", snapShot);
field_to_json(Obj, "devices", tenants);
};
void Report::reset() {
@@ -390,16 +393,16 @@ namespace OpenWifi::ProvObjects {
}
void ExpandedUseEntry::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json(Obj, "uuid", uuid);
RESTAPI_utils::field_to_json(Obj, "name", name);
RESTAPI_utils::field_to_json(Obj, "description", description);
field_to_json(Obj, "uuid", uuid);
field_to_json(Obj, "name", name);
field_to_json(Obj, "description", description);
}
bool ExpandedUseEntry::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json( Obj,"uuid",uuid);
RESTAPI_utils::field_from_json( Obj,"name",name);
RESTAPI_utils::field_from_json( Obj,"description",description);
field_from_json( Obj,"uuid",uuid);
field_from_json( Obj,"name",name);
field_from_json( Obj,"description",description);
return true;
} catch(...) {
@@ -408,14 +411,14 @@ namespace OpenWifi::ProvObjects {
}
void ExpandedUseEntryList::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json(Obj, "type", type);
RESTAPI_utils::field_to_json(Obj, "entries", entries);
field_to_json(Obj, "type", type);
field_to_json(Obj, "entries", entries);
}
bool ExpandedUseEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json( Obj,"type",type);
RESTAPI_utils::field_from_json( Obj,"entries",entries);
field_from_json( Obj,"type",type);
field_from_json( Obj,"entries",entries);
return true;
} catch(...) {
@@ -424,12 +427,157 @@ namespace OpenWifi::ProvObjects {
}
void ExpandedUseEntryMapList::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json(Obj, "entries", entries);
field_to_json(Obj, "entries", entries);
}
bool ExpandedUseEntryMapList::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json( Obj,"entries",entries);
field_from_json( Obj,"entries",entries);
return true;
} catch(...) {
}
return false;
}
void UuidList::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj, "list", list);
}
bool UuidList::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
field_from_json(Obj, "list", list);
return true;
} catch(...) {
}
return false;
}
void field_to_json(Poco::JSON::Object &Obj, const char * FieldName, ACLACCESS A) {
switch(A) {
case READ: Obj.set(FieldName,"read"); break;
case MODIFY: Obj.set(FieldName,"modify"); break;
case CREATE: Obj.set(FieldName,"create"); break;
case DELETE: Obj.set(FieldName,"delete"); break;
case NONE:
default:
Obj.set(FieldName,"none");
}
}
void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char * FieldName, ACLACCESS &A) {
if(Obj->has(FieldName)) {
auto V = Obj->getValue<std::string>(FieldName);
if(V=="read")
A = READ;
else if(V=="modify")
A = MODIFY;
else if(V=="create")
A = CREATE;
else if(V=="delete")
A = DELETE;
else if(V=="none")
A = NONE;
else
throw Poco::Exception("invalid JSON");
}
}
void ObjectACL::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json(Obj, "users", users);
RESTAPI_utils::field_to_json(Obj, "roles", roles);
field_to_json(Obj, "access", access);
}
bool ObjectACL::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json(Obj, "users", users);
RESTAPI_utils::field_from_json(Obj, "roles", roles);
field_from_json(Obj, "access", access);
return true;
} catch(...) {
}
return false;
}
void ObjectACLList::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json(Obj, "list", list);
}
bool ObjectACLList::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json(Obj, "list", list);
return true;
} catch(...) {
}
return false;
}
std::string to_string(VISIBILITY A) {
switch(A) {
case PUBLIC: return "public";
case SELECT: return "select";
case PRIVATE:
default:
return "private";
}
}
void field_to_json(Poco::JSON::Object &Obj, const char * FieldName, VISIBILITY A) {
Obj.set(FieldName,to_string(A));
}
VISIBILITY visibility_from_string(const std::string &V) {
if(V=="public")
return PUBLIC;
else if(V=="select")
return SELECT;
else if(V=="private")
return PRIVATE;
throw Poco::Exception("invalid json");
}
void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char * FieldName, VISIBILITY &A) {
if(Obj->has(FieldName)) {
auto V = Obj->getValue<std::string>(FieldName);
A = visibility_from_string(V);
}
}
void Map::to_json(Poco::JSON::Object &Obj) const {
info.to_json(Obj);
RESTAPI_utils::field_to_json( Obj,"data",data);
RESTAPI_utils::field_to_json( Obj,"entity",entity);
RESTAPI_utils::field_to_json( Obj,"creator",creator);
field_to_json( Obj,"visibility",visibility);
RESTAPI_utils::field_to_json( Obj,"access",access);
}
bool Map::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
info.from_json(Obj);
RESTAPI_utils::field_from_json( Obj,"data",data);
RESTAPI_utils::field_from_json( Obj,"entity",entity);
RESTAPI_utils::field_from_json( Obj,"creator",creator);
field_from_json( Obj,"visibility",visibility);
RESTAPI_utils::field_from_json( Obj,"access",access);
return true;
} catch(...) {
}
return false;
}
void MapList::to_json(Poco::JSON::Object &Obj) const {
RESTAPI_utils::field_to_json( Obj,"list",list);
}
bool MapList::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
RESTAPI_utils::field_from_json( Obj,"list",list);
return true;
} catch(...) {
@@ -438,13 +586,48 @@ namespace OpenWifi::ProvObjects {
}
bool UpdateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I) {
uint64_t Now = std::time(nullptr);
if(O->has("name"))
I.name = O->get("name").toString();
if(I.name.empty())
return false;
if(O->has("description"))
I.description = O->get("description").toString();
SecurityObjects::MergeNotes(O,U,I.notes);
I.modified = std::time(nullptr);
SecurityObjects::NoteInfoVec N;
for(auto &i:I.notes) {
if(i.note.empty())
continue;
N.push_back(SecurityObjects::NoteInfo{.created=Now,.createdBy=U.email,.note=i.note});
}
I.modified = Now;
return true;
}
};
bool CreateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I) {
uint64_t Now = std::time(nullptr);
if(O->has("name"))
I.name = O->get("name").toString();
if(I.name.empty())
return false;
if(O->has("description"))
I.description = O->get("description").toString();
SecurityObjects::NoteInfoVec N;
for(auto &i:I.notes) {
if(i.note.empty())
continue;
N.push_back(SecurityObjects::NoteInfo{.created=Now,.createdBy=U.email,.note=i.note});
}
I.notes = N;
I.modified = I.created = Now;
I.id = MicroService::CreateUUID();
return true;
}
}

View File

@@ -15,6 +15,13 @@
namespace OpenWifi::ProvObjects {
enum FIRMWARE_UPGRADE_RULES {
dont_upgrade,
upgrade_inherit,
upgrade_release_only,
upgrade_latest
};
struct ObjectInfo {
Types::UUID_t id;
std::string name;
@@ -317,7 +324,61 @@ namespace OpenWifi::ProvObjects {
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
struct UuidList {
std::vector<std::string> list;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
enum ACLACCESS {
NONE, READ, MODIFY, CREATE, DELETE
};
struct ObjectACL {
UuidList users;
UuidList roles;
ACLACCESS access = NONE;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
struct ObjectACLList {
std::vector<ObjectACL> list;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
enum VISIBILITY {
PUBLIC, PRIVATE, SELECT
};
std::string to_string(VISIBILITY A);
VISIBILITY visibility_from_string(const std::string &V);
struct Map {
ObjectInfo info;
std::string data;
std::string entity;
std::string creator;
VISIBILITY visibility = PRIVATE;
ObjectACLList access;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
struct MapList {
std::vector<Map> list;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
bool UpdateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I);
bool CreateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I);
};

View File

@@ -491,7 +491,6 @@ namespace OpenWifi::SecurityObjects {
field_to_json(Obj,"expires",expires);
field_to_json(Obj,"completed",completed);
field_to_json(Obj,"canceled",canceled);
}
bool ActionLink::from_json(Poco::JSON::Object::Ptr &Obj) {
@@ -511,6 +510,24 @@ namespace OpenWifi::SecurityObjects {
return true;
} catch(...) {
}
return false;
}
void Preferences::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"id",id);
field_to_json(Obj,"modified",modified);
field_to_json(Obj,"data",data);
}
bool Preferences::from_json(Poco::JSON::Object::Ptr &Obj) {
try {
field_from_json(Obj,"id",id);
field_from_json(Obj,"modified",modified);
field_from_json(Obj,"data",data);
return true;
} catch(...) {
}
return false;
}

View File

@@ -9,8 +9,8 @@
#ifndef UCENTRAL_RESTAPI_SECURITYOBJECTS_H
#define UCENTRAL_RESTAPI_SECURITYOBJECTS_H
#include "Poco/JSON/Object.h"
#include "framework/OpenWifiTypes.h"
#include "Poco/JSON/Object.h"
namespace OpenWifi::SecurityObjects {
@@ -59,15 +59,15 @@ namespace OpenWifi::SecurityObjects {
struct MobilePhoneNumber {
std::string number;
bool verified;
bool primary;
bool verified = false;
bool primary = false;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr &Obj);
};
struct MfaAuthInfo {
bool enabled;
bool enabled = false;
std::string method;
void to_json(Poco::JSON::Object &Obj) const;
@@ -86,7 +86,7 @@ namespace OpenWifi::SecurityObjects {
std::string uuid;
std::string question;
std::string method;
uint64_t created;
uint64_t created = std::time(nullptr);
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr &Obj);
@@ -244,6 +244,14 @@ namespace OpenWifi::SecurityObjects {
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr &Obj);
};
struct Preferences {
std::string id;
uint64_t modified;
Types::StringPairVec data;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr &Obj);
};
}
#endif //UCENTRAL_RESTAPI_SECURITYOBJECTS_H

View File

@@ -11,9 +11,9 @@ namespace OpenWifi {
class SerialNumberCache : public SubSystemServer {
public:
static SerialNumberCache *instance() {
static SerialNumberCache instance;
return &instance;
static SerialNumberCache * instance() {
static SerialNumberCache * instance_ = new SerialNumberCache;
return instance_;
}
int Start() override;

View File

@@ -40,7 +40,7 @@ namespace OpenWifi {
}
if(Conn_)
GetAssociations(O,Conn_->Associations_2G,Conn_->Associations_5G);
GetAssociations(O,Conn_->Conn_.Associations_2G,Conn_->Conn_.Associations_5G);
if(UpdatesSinceLastWrite_>10)
Save();

View File

@@ -9,11 +9,12 @@
#include "Poco/JSON/Object.h"
#include "Poco/Logger.h"
#include "RESTObjects//RESTAPI_GWobjects.h"
#include "DeviceRegistry.h"
namespace OpenWifi {
class StateProcessor {
public:
explicit StateProcessor(GWObjects::ConnectionState * Conn, Poco::Logger & L):
explicit StateProcessor(std::shared_ptr<DeviceRegistry::ConnectionEntry> Conn, Poco::Logger & L):
Conn_(Conn), Logger_(L) {}
~StateProcessor() {
@@ -37,7 +38,7 @@ namespace OpenWifi {
// interface name is the key, each entry is a map with the stats name as key and then the value
std::map<std::string,std::map<std::string,uint64_t>> Stats_;
uint64_t UpdatesSinceLastWrite_ = 0 ;
GWObjects::ConnectionState * Conn_ = nullptr;
std::shared_ptr<DeviceRegistry::ConnectionEntry> Conn_;
};
}

View File

@@ -10,90 +10,66 @@
namespace OpenWifi {
void StorageArchiver::run() {
Running_ = true ;
GetLastRun();
while(Running_) {
Poco::Thread::trySleep(2000);
Poco::DateTime Now;
if(!Running_)
break;
if(LastRun_!=Now.day()) {
if(Now.hour()>=RunAtHour_ && Now.minute()>=RunAtMin_) {
std::lock_guard G(Mutex_);
void Archiver::onTimer(Poco::Timer &timer){
auto Now = std::time(nullptr);
for(const auto &i:DBs_) {
if (!Poco::icompare(i.DBName, "healthchecks")) {
Logger_.information("Archiving HealthChecks...");
StorageService()->RemoveHealthChecksRecordsOlderThan(
std::time(nullptr) - (i.HowManyDays * 24 * 60 * 60));
Now - (i.HowManyDays * 24 * 60 * 60));
} else if (!Poco::icompare(i.DBName, "statistics")) {
Logger_.information("Archiving Statistics...");
StorageService()->RemoveStatisticsRecordsOlderThan(
std::time(nullptr) - (i.HowManyDays * 24 * 60 * 60));
Now - (i.HowManyDays * 24 * 60 * 60));
} else if (!Poco::icompare(i.DBName, "devicelogs")) {
Logger_.information("Archiving Device Logs...");
StorageService()->RemoveDeviceLogsRecordsOlderThan(
std::time(nullptr) - (i.HowManyDays * 24 * 60 * 60));
Now - (i.HowManyDays * 24 * 60 * 60));
} else if (!Poco::icompare(i.DBName, "commandlist")) {
Logger_.information("Archiving Command History...");
StorageService()->RemoveCommandListRecordsOlderThan(
std::time(nullptr) - (i.HowManyDays * 24 * 60 * 60));
Now - (i.HowManyDays * 24 * 60 * 60));
} else {
Logger_.information(Poco::format("Cannot archive DB '%s'", i.DBName));
}
}
LastRun_ = Now.day();
SetLastRun();
}
}
}
AppServiceRegistry().Set("lastStorageArchiverRun", (uint64_t) Now);
}
void StorageArchiver::GetLastRun() {
try {
Poco::File LR(LastRunFileName_);
try {
if (LR.exists()) {
std::ifstream I;
I.open(LR.path());
I >> LastRun_;
I.close();
} else {
LastRun_ = 0;
}
} catch(...) {
LastRun_=0;
}
} catch(...) {
LastRun_=0;
}
}
static auto CalculateDelta(int H, int M) {
Poco::LocalDateTime dt;
Poco::LocalDateTime scheduled(dt.year(), dt.month(), dt.day(), H, M, 0);
void StorageArchiver::SetLastRun() {
try {
std::ofstream O;
O.open(LastRunFileName_,std::ios::binary | std::ios::trunc);
O << LastRun_;
O.close();
} catch(const Poco::Exception &E) {
Logger_.log(E);
size_t delta = 0;
if ((dt.hour() < H) || (dt.hour()==H && dt.minute()<M)) {
delta = scheduled.timestamp().epochTime() - dt.timestamp().epochTime();
} else {
delta = (24*60*60) - (dt.timestamp().epochTime() - scheduled.timestamp().epochTime());
}
return delta;
}
int StorageArchiver::Start() {
Enabled_ = MicroService::instance().ConfigGetBool("archiver.enabled",false);
if(!Enabled_) {
Logger_.information("Archiver is disabled.");
return 0;
}
ArchiverCallback_ = std::make_unique<Poco::TimerCallback<Archiver>>(Archiver_,&Archiver::onTimer);
auto Schedule = MicroService::instance().ConfigGetString("archiver.schedule","03:00");
Types::StringVec S = Utils::Split(Schedule,':');
auto S = Poco::StringTokenizer(Schedule,":");
int RunAtHour_, RunAtMin_;
if(S.count()!=2) {
RunAtHour_ = 3 ;
RunAtMin_ = 0;
} else {
RunAtHour_ = std::atoi(S[0].c_str());
RunAtMin_ = std::atoi(S[1].c_str());
}
for(int i=0;i<20;i++) {
std::string key = "archiver.db." + std::to_string(i) + ".name";
@@ -103,7 +79,7 @@ namespace OpenWifi {
if(Poco::icompare(DBName,DB)==0) {
std::string Key = "archiver.db." + std::to_string(i) + ".keep";
auto Keep = MicroService::instance().ConfigGetInt(Key,7);
DBs_.push_back(ArchiverDBEntry{
Archiver_.AddDb(Archiver::ArchiverDBEntry{
.DBName = DB,
.HowManyDays = Keep
});
@@ -111,17 +87,21 @@ namespace OpenWifi {
}
}
}
LastRunFileName_ = MicroService::instance().DataDir() + "/archiver_lastrun.txt";
Janitor_.start(*this);
int NextRun = CalculateDelta(RunAtHour_,RunAtMin_);
Logger_.information(Poco::format("Next run in %d seconds.",NextRun));
Timer_.setStartInterval( NextRun * 1000);
Timer_.setPeriodicInterval(24 * 60 * 60 * 1000); // 1 hours
Timer_.start(*ArchiverCallback_);
return 0;
}
void StorageArchiver::Stop() {
if(Running_) {
Running_=false;
Janitor_.wakeUp();
Janitor_.join();
if(Enabled_) {
Timer_.stop();
}
}

View File

@@ -8,51 +8,58 @@
#include <functional>
#include "framework/MicroService.h"
#include "Poco/Timer.h"
namespace OpenWifi {
struct ArchiverDBEntry {
static const std::list<std::string> AllInternalDBNames{"healthchecks", "statistics", "devicelogs" , "commandlist" };
class Archiver {
public:
struct ArchiverDBEntry {
std::string DBName;
uint64_t HowManyDays=7;
};
typedef std::vector<ArchiverDBEntry> ArchiverDBEntryVec;
};
typedef std::vector<ArchiverDBEntry> ArchiverDBEntryVec;
static const std::list<std::string> AllInternalDBNames{"healthchecks", "statistics", "devicelogs" , "commandlist" };
explicit Archiver(Poco::Logger &Logger):
Logger_(Logger) {
}
class StorageArchiver : public SubSystemServer, Poco::Runnable {
void onTimer(Poco::Timer & timer);
inline void AddDb(const ArchiverDBEntry &E ) {
DBs_.push_back(E);
}
private:
Poco::Logger &Logger_;
ArchiverDBEntryVec DBs_;
};
class StorageArchiver : public SubSystemServer {
public:
static StorageArchiver *instance() {
static StorageArchiver instance;
return &instance;
static StorageArchiver * instance_ = new StorageArchiver;
return instance_;
}
void run() override;
int Start() override;
void Stop() override;
void GetLastRun();
void SetLastRun();
inline bool Enabled() const { return Enabled_; }
private:
std::atomic_bool Running_ = false;
std::atomic_bool Enabled_ = false;
Poco::Thread Janitor_;
ArchiverDBEntryVec DBs_;
uint64_t RunAtHour_=0;
uint64_t RunAtMin_=0;
uint64_t LastRun_=0;
std::string LastRunFileName_;
Poco::Timer Timer_;
Archiver Archiver_{Logger_};
std::unique_ptr<Poco::TimerCallback<Archiver>> ArchiverCallback_;
StorageArchiver() noexcept:
SubSystemServer("StorageArchiver", "STORAGE-ARCHIVE", "archiver")
{
}
};
};
inline StorageArchiver * StorageArchiver() { return StorageArchiver::instance(); }
inline StorageArchiver * StorageArchiver() { return StorageArchiver::instance(); }
} // namespace

View File

@@ -28,8 +28,8 @@ namespace OpenWifi {
};
static Storage *instance() {
static Storage instance;
return &instance;
static Storage * instance_ = new Storage;
return instance_;
}
typedef std::map<std::string,std::string> DeviceCapabilitiesCache;
@@ -89,6 +89,7 @@ namespace OpenWifi {
bool GetDefaultConfiguration(std::string &name, GWObjects::DefaultConfiguration & DefConfig);
bool GetDefaultConfigurations(uint64_t From, uint64_t HowMany, std::vector<GWObjects::DefaultConfiguration> &Devices);
bool FindDefaultConfigurationForModel(const std::string & Model, GWObjects::DefaultConfiguration & DefConfig );
uint64_t GetDefaultConfigurationsCount();
bool AddCommand(std::string & SerialNumber, GWObjects::CommandDetails & Command,CommandExecutionType Type);
bool GetCommands(std::string &SerialNumber, uint64_t FromDate, uint64_t ToDate, uint64_t Offset, uint64_t HowMany, std::vector<GWObjects::CommandDetails> & Commands);
@@ -115,6 +116,8 @@ namespace OpenWifi {
bool IsBlackListed(std::string & SerialNumber);
bool GetBlackListDevices(uint64_t Offset, uint64_t HowMany, std::vector<GWObjects::BlackListedDevice> & Devices );
bool UpdateBlackListDevice(std::string & SerialNumber, GWObjects::BlackListedDevice & Device);
uint64_t GetBlackListDeviceCount();
bool SetLifetimeStats(std::string & SerialNumber, std::string & Stats);
bool GetLifetimeStats(std::string & SerialNumber, std::string & Stats);
bool ResetLifetimeStats(std::string & SerialNumber);

View File

@@ -27,18 +27,18 @@ namespace OpenWifi {
int TelemetryStream::Start() {
ReactorPool_.Start();
Runner.start(*this);
Runner_.start(*this);
return 0;
}
void TelemetryStream::Stop() {
if(Running_) {
Running_ = false;
Runner.join();
}
Logger_.notice("Stopping reactors...");
ReactorPool_.Stop();
if(Running_) {
Running_ = false;
Runner_.join();
}
}
bool TelemetryStream::CreateEndpoint(const std::string &SerialNumber, std::string &EndPoint, std::string &UUID) {
@@ -46,7 +46,7 @@ namespace OpenWifi {
Poco::URI Public(MicroService::instance().ConfigGetString("openwifi.system.uri.public"));
Poco::URI U;
UUID = MicroService::instance().CreateUUID();
UUID = MicroService::CreateUUID();
U.setScheme("wss");
U.setHost(Public.getHost());
U.setPort(Public.getPort());
@@ -69,7 +69,7 @@ namespace OpenWifi {
void TelemetryStream::UpdateEndPoint(const std::string &SerialNumber, const std::string &PayLoad) {
std::lock_guard G(QueueMutex_);
Queue_.push(QueueUpdate{.SerialNumber=SerialNumber,.Payload=PayLoad});
Runner.wakeUp();
Runner_.wakeUp();
}
void TelemetryStream::run() {

View File

@@ -101,8 +101,8 @@ namespace OpenWifi {
};
static TelemetryStream *instance() {
static TelemetryStream instance;
return &instance;
static TelemetryStream * instance_ = new TelemetryStream;
return instance_;
}
int Start() override;
@@ -120,7 +120,7 @@ namespace OpenWifi {
std::map<std::string, std::set<std::string>> SerialNumbers_; // serialNumber -> uuid
TelemetryReactorPool ReactorPool_;
std::mutex QueueMutex_;
Poco::Thread Runner;
Poco::Thread Runner_;
std::queue<QueueUpdate> Queue_;
TelemetryStream() noexcept:
SubSystemServer("TelemetryServer", "TELEMETRY-SVR", "openwifi.telemetry")

View File

@@ -28,12 +28,6 @@
namespace OpenWifi {
WebSocketServer::WebSocketServer() noexcept:
SubSystemServer("WebSocketServer", "WS-SVR", "ucentral.websocket")
{
}
bool WebSocketServer::ValidateCertificate(const std::string & ConnectionId, const Poco::Crypto::X509Certificate & Certificate) {
if(IsCertOk()) {
Logger_.debug(Poco::format("CERTIFICATE(%s): issuer='%s' cn='%s'", ConnectionId, Certificate.issuerName(),Certificate.commonName()));
@@ -218,7 +212,7 @@ namespace OpenWifi {
return false;
uint64_t GoodConfig=ConfigurationCache().CurrentConfig(SerialNumber_);
if(GoodConfig && (GoodConfig==UUID || GoodConfig==Conn_->PendingUUID))
if(GoodConfig && (GoodConfig==UUID || GoodConfig==Conn_->Conn_.PendingUUID))
return false;
GWObjects::Device D;
@@ -231,10 +225,10 @@ namespace OpenWifi {
return false;
}
Conn_->PendingUUID = D.UUID;
Conn_->Conn_.PendingUUID = D.UUID;
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.SubmittedBy = uCentralProtocol::SUBMITTED_BY_SYSTEM;
Cmd.Status = uCentralProtocol::PENDING;
Cmd.Command = uCentralProtocol::CONFIGURE;
@@ -334,7 +328,7 @@ namespace OpenWifi {
}
if(Conn_!= nullptr)
Conn_->LastContact = std::time(nullptr);
Conn_->Conn_.LastContact = std::time(nullptr);
switch(EventType) {
case uCentralProtocol::ET_CONNECT: {
@@ -347,12 +341,12 @@ namespace OpenWifi {
Conn_ = DeviceRegistry()->Register(Serial, this);
SerialNumber_ = Serial;
Conn_->SerialNumber = Serial;
Conn_->UUID = UUID;
Conn_->Firmware = Firmware;
Conn_->PendingUUID = 0;
Conn_->LastContact = std::time(nullptr);
Conn_->Address = Utils::FormatIPv6(WS_->peerAddress().toString());
Conn_->Conn_.SerialNumber = Serial;
Conn_->Conn_.UUID = UUID;
Conn_->Conn_.Firmware = Firmware;
Conn_->Conn_.PendingUUID = 0;
Conn_->Conn_.LastContact = std::time(nullptr);
Conn_->Conn_.Address = Utils::FormatIPv6(WS_->peerAddress().toString());
CId_ = SerialNumber_ + "@" + CId_ ;
// We need to verify the certificate if we have one
@@ -365,7 +359,7 @@ namespace OpenWifi {
else
Logger_.information(Poco::format("CONNECT(%s): Authenticated but not validated. Serial='%s' CN='%s'", CId_, Serial, CN_));
}
Conn_->VerifiedCertificate = CertValidation_;
Conn_->Conn_.VerifiedCertificate = CertValidation_;
if (Daemon()->AutoProvisioning() && !StorageService()->DeviceExists(SerialNumber_)) {
StorageService()->CreateDefaultDevice(SerialNumber_, Capabilities, Firmware, Compatible_);
@@ -375,12 +369,14 @@ namespace OpenWifi {
StorageService()->SetConnectInfo(SerialNumber_, Firmware );
}
}
Conn_->Compatible = Compatible_;
Conn_->Conn_.Compatible = Compatible_;
StatsProcessor_ = std::make_unique<StateProcessor>(Conn_, Logger_);
StatsProcessor_->Initialize(Serial);
LookForUpgrade(UUID);
if(KafkaManager()->Enabled()) {
Poco::JSON::Stringifier Stringify;
ParamsObj->set(uCentralProtocol::CONNECTIONIP,CId_);
@@ -416,7 +412,7 @@ namespace OpenWifi {
else
Logger_.debug(Poco::format("STATE(%s): UUID=%Lu Updating for CMD=%s.", CId_,
UUID, request_uuid));
Conn_->UUID = UUID;
Conn_->Conn_.UUID = UUID;
LookForUpgrade(UUID);
GWObjects::Statistics Stats{ .SerialNumber = SerialNumber_, .UUID = UUID, .Data = State};
Stats.Recorded = std::time(nullptr);
@@ -425,7 +421,6 @@ namespace OpenWifi {
StorageService()->SetCommandResult(request_uuid, State);
}
if (StatsProcessor_)
StatsProcessor_->Add(State);
if(KafkaManager()->Enabled()) {
@@ -465,7 +460,7 @@ namespace OpenWifi {
Logger_.debug(Poco::format("HEALTHCHECK(%s): UUID=%Lu Updating for CMD=%s.",
CId_, UUID, request_uuid));
Conn_->UUID = UUID;
Conn_->Conn_.UUID = UUID;
LookForUpgrade(UUID);
GWObjects::HealthCheck Check;
@@ -520,7 +515,7 @@ namespace OpenWifi {
.Severity = Severity,
.Recorded = (uint64_t)time(nullptr),
.LogType = 0,
.UUID = Conn_->UUID};
.UUID = Conn_->Conn_.UUID};
StorageService()->AddLog(DeviceLog);
} else {
Logger_.warning(Poco::format("LOG(%s): Missing parameters.", CId_));
@@ -616,7 +611,7 @@ namespace OpenWifi {
if(ParamsObj->get(uCentralProtocol::REBOOT).toString()=="true") {
GWObjects::CommandDetails Cmd;
Cmd.SerialNumber = SerialNumber_;
Cmd.UUID = MicroService::instance().CreateUUID();
Cmd.UUID = MicroService::CreateUUID();
Cmd.SubmittedBy = uCentralProtocol::SUBMITTED_BY_SYSTEM;
Cmd.Status = uCentralProtocol::PENDING;
Cmd.Command = uCentralProtocol::REBOOT;
@@ -736,16 +731,26 @@ namespace OpenWifi {
Logger_.information(Poco::format("DISCONNECT(%s): device has disconnected.", CId_));
return delete this;
} else {
if (Conn_ != nullptr) {
Conn_->Conn_.RX += IncomingSize;
Conn_->Conn_.MessageCount++;
}
switch (Op) {
case Poco::Net::WebSocket::FRAME_OP_PING: {
Logger_.debug(Poco::format("WS-PING(%s): received. PONG sent back.", CId_));
WS_->sendFrame("", 0,
(int)Poco::Net::WebSocket::FRAME_OP_PONG |
(int)Poco::Net::WebSocket::FRAME_FLAG_FIN);
if (Conn_ != nullptr) {
Conn_->Conn_.MessageCount++;
}
if (KafkaManager()->Enabled() && Conn_) {
Poco::JSON::Object PingObject;
Poco::JSON::Object PingDetails;
PingDetails.set(uCentralProtocol::FIRMWARE, Conn_->Firmware);
PingDetails.set(uCentralProtocol::FIRMWARE, Conn_->Conn_.Firmware);
PingDetails.set(uCentralProtocol::SERIALNUMBER, SerialNumber_);
PingDetails.set(uCentralProtocol::COMPATIBLE, Compatible_);
PingDetails.set(uCentralProtocol::CONNECTIONIP, CId_);
@@ -756,11 +761,13 @@ namespace OpenWifi {
KafkaManager()->PostMessage(KafkaTopics::CONNECTION, SerialNumber_,
OS.str());
}
return;
}
break;
case Poco::Net::WebSocket::FRAME_OP_PONG: {
Logger_.debug(Poco::format("PONG(%s): received and ignored.",CId_));
return;
}
break;
@@ -790,6 +797,7 @@ namespace OpenWifi {
Logger_.error(Poco::format("FRAME(%s): illegal transaction header, missing 'jsonrpc'",CId_));
Errors_++;
}
return;
}
break;
@@ -804,11 +812,6 @@ namespace OpenWifi {
}
break;
}
if (Conn_ != nullptr) {
Conn_->RX += IncomingSize;
Conn_->MessageCount++;
}
}
}
catch (const Poco::Net::ConnectionResetException & E)
@@ -885,7 +888,7 @@ namespace OpenWifi {
auto BytesSent = WS_->sendFrame(Payload.c_str(),(int)Payload.size());
if(Conn_)
Conn_->TX += BytesSent;
Conn_->Conn_.TX += BytesSent;
return BytesSent == Payload.size();
}

View File

@@ -102,7 +102,7 @@ namespace OpenWifi {
std::unique_ptr<Poco::Net::WebSocket> WS_;
std::string SerialNumber_;
std::string Compatible_;
GWObjects::ConnectionState * Conn_ = nullptr;
std::shared_ptr<DeviceRegistry::ConnectionEntry> Conn_;
bool Registered_ = false ;
std::string CId_;
std::string CN_;
@@ -117,8 +117,8 @@ namespace OpenWifi {
class WebSocketServer : public SubSystemServer {
public:
static WebSocketServer *instance() {
static WebSocketServer instance;
return &instance;
static WebSocketServer *instance_ = new WebSocketServer;
return instance_;
}
int Start() override;
@@ -134,7 +134,11 @@ namespace OpenWifi {
Poco::Net::SocketReactor Reactor_;
Poco::Thread ReactorThread_;
ReactorPool ReactorPool_;
WebSocketServer() noexcept;
WebSocketServer() noexcept:
SubSystemServer("WebSocketServer", "WS-SVR", "ucentral.websocket") {
}
};
inline WebSocketServer * WebSocketServer() { return WebSocketServer::instance(); }

View File

@@ -2,8 +2,7 @@
// Created by stephane bourque on 2021-09-14.
//
#ifndef OWPROV_CONFIGURATIONVALIDATOR_H
#define OWPROV_CONFIGURATIONVALIDATOR_H
#pragma once
#include <nlohmann/json-schema.hpp>
#include "framework/MicroService.h"
@@ -43,4 +42,3 @@ namespace OpenWifi {
inline bool ValidateUCentralConfiguration(const std::string &C, std::string &Error) { return ConfigurationValidator::instance()->Validate(C, Error); }
}
#endif //OWPROV_CONFIGURATIONVALIDATOR_H

View File

@@ -2,8 +2,7 @@
// Created by stephane bourque on 2021-10-08.
//
#ifndef OWPROV_COUNTRYCODES_H
#define OWPROV_COUNTRYCODES_H
#pragma once
#include <vector>
#include <string>
@@ -270,4 +269,3 @@ namespace OpenWifi {
}
#endif //OWPROV_COUNTRYCODES_H

View File

@@ -5,8 +5,8 @@
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALGW_KAFKA_TOPICS_H
#define UCENTRALGW_KAFKA_TOPICS_H
#pragma once
namespace OpenWifi::KafkaTopics {
static const std::string HEALTHCHECK{"healthcheck"};
@@ -37,4 +37,3 @@ namespace OpenWifi::KafkaTopics {
}
}
#endif // UCENTRALGW_KAFKA_TOPICS_H

View File

@@ -6,8 +6,7 @@
// Arilia Wireless Inc.
//
#ifndef OPENWIFI_MICROSERVICE_H
#define OPENWIFI_MICROSERVICE_H
#pragma once
#include <array>
#include <iostream>
@@ -20,6 +19,7 @@
#include <regex>
#include <random>
#include <iomanip>
#include <queue>
using namespace std::chrono_literals;
@@ -58,15 +58,99 @@ using namespace std::chrono_literals;
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/NetworkInterface.h"
#include "Poco/ExpireLRUCache.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Parser.h"
#include "Poco/StringTokenizer.h"
#include "cppkafka/cppkafka.h"
#include "framework/OpenWifiTypes.h"
#include "framework/KafkaTopics.h"
#include "framework/RESTAPI_protocol.h"
#include "framework/RESTAPI_errors.h"
#include "framework/uCentral_Protocol.h"
#include "RESTObjects/RESTAPI_SecurityObjects.h"
#include "nlohmann/json.hpp"
namespace OpenWifi {
enum UNAUTHORIZED_REASON {
SUCCESS=0,
PASSWORD_CHANGE_REQUIRED,
INVALID_CREDENTIALS,
PASSWORD_ALREADY_USED,
USERNAME_PENDING_VERIFICATION,
PASSWORD_INVALID,
INTERNAL_ERROR,
ACCESS_DENIED,
INVALID_TOKEN,
EXPIRED_TOKEN,
RATE_LIMIT_EXCEEDED
};
class AppServiceRegistry {
public:
inline AppServiceRegistry();
static AppServiceRegistry & instance() {
static AppServiceRegistry *instance_= new AppServiceRegistry;
return *instance_;
}
inline ~AppServiceRegistry() {
Save();
}
inline void Save() {
std::istringstream IS( to_string(Registry_));
std::ofstream OF;
OF.open(FileName,std::ios::binary | std::ios::trunc);
Poco::StreamCopier::copyStream(IS, OF);
}
inline void Set(const char *Key, uint64_t Value ) {
Registry_[Key] = Value;
Save();
}
inline void Set(const char *Key, const std::string &Value ) {
Registry_[Key] = Value;
Save();
}
inline void Set(const char *Key, bool Value ) {
Registry_[Key] = Value;
Save();
}
inline bool Get(const char *Key, bool & Value ) {
if(Registry_[Key].is_boolean()) {
Value = Registry_[Key].get<bool>();
return true;
}
return false;
}
inline bool Get(const char *Key, uint64_t & Value ) {
if(Registry_[Key].is_number_unsigned()) {
Value = Registry_[Key].get<uint64_t>();
return true;
}
return false;
}
inline bool Get(const char *Key, std::string & Value ) {
if(Registry_[Key].is_string()) {
Value = Registry_[Key].get<std::string>();
return true;
}
return false;
}
private:
std::string FileName;
nlohmann::json Registry_;
};
}
namespace OpenWifi::RESTAPI_utils {
@@ -86,12 +170,13 @@ namespace OpenWifi::RESTAPI_utils {
Obj.set(Field,S);
}
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::vector<Types::StringPair> & S) {
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::StringPairVec & S) {
Poco::JSON::Array Array;
for(const auto &i:S) {
Poco::JSON::Object O;
O.set("tag",i.first);
O.set("value", i.second);
Array.add(O);
}
Obj.set(Field,Array);
}
@@ -129,7 +214,6 @@ namespace OpenWifi::RESTAPI_utils {
Obj.set(Field,A);
}
template<typename T> void field_to_json(Poco::JSON::Object &Obj,
const char *Field,
const T &V,
@@ -159,6 +243,7 @@ namespace OpenWifi::RESTAPI_utils {
V = (Obj->get(Field).toString() == "true");
}
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, Types::StringPairVec &Vec) {
if(Obj->isArray(Field)) {
auto O = Obj->getArray(Field);
@@ -368,11 +453,12 @@ namespace OpenWifi::RESTAPI_utils {
try {
Poco::JSON::Parser P;
auto Object = P.parse(S).template extract<Poco::JSON::Array::Ptr>();
for (auto const &i : *Object) {
for (const auto &i : *Object) {
auto InnerObject = i.template extract<Poco::JSON::Array::Ptr>();
if(InnerObject->size()==2) {
Types::StringPair P{InnerObject->get(0).toString(), InnerObject->get(1).toString()};
R.push_back(P);
auto S1 = InnerObject->getElement<std::string>(0);
auto S2 = InnerObject->getElement<std::string>(1);
R.push_back(std::make_pair(S1,S2));
}
}
} catch (...) {
@@ -717,6 +803,20 @@ namespace OpenWifi::Utils {
return R;
}
[[nodiscard]] inline std::string IntToSerialNumber(uint64_t S) {
char b[16];
for(int i=0;i<12;++i) {
int B = (S & 0x0f);
if(B<10)
b[11-i] = B+'0';
else
b[11-i] = B - 10 + 'a';
S >>= 4 ;
}
b[12]=0;
return b;
}
[[nodiscard]] inline bool SerialNumberMatch(const std::string &S1, const std::string &S2, int Bits=2) {
auto S1_i = SerialNumberToInt(S1);
@@ -766,16 +866,13 @@ namespace OpenWifi::Utils {
return Result;
}
inline void SaveSystemId(uint64_t Id);
[[nodiscard]] inline uint64_t InitializeSystemId() {
std::random_device RDev;
std::srand(RDev());
std::chrono::high_resolution_clock Clock;
auto Now = Clock.now().time_since_epoch().count();
auto S = (GetDefaultMacAsInt64() + std::rand() + Now) ;
SaveSystemId(S);
std::cout << "ID: " << S << std::endl;
OpenWifi::AppServiceRegistry().Set("systemid",S);
return S;
}
@@ -924,6 +1021,7 @@ namespace OpenWifi {
static const std::string uSERVICE_SUBCRIBER{ "owsub"};
static const std::string uSERVICE_INSTALLER{ "owinst"};
class MyErrorHandler : public Poco::ErrorHandler {
public:
explicit MyErrorHandler(Poco::Util::Application &App) : App_(App) {}
@@ -1355,8 +1453,8 @@ namespace OpenWifi {
};
static RESTAPI_RateLimiter *instance() {
static RESTAPI_RateLimiter instance;
return &instance;
static RESTAPI_RateLimiter * instance_ = new RESTAPI_RateLimiter;
return instance_;
}
inline int Start() final { return 0;};
@@ -1366,18 +1464,18 @@ namespace OpenWifi {
Poco::URI uri(R.getURI());
auto H = str_hash(uri.getPath() + R.clientAddress().host().toString());
auto E = Cache_.get(H);
const auto p1 = std::chrono::system_clock::now();
auto Now = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
auto Now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
if(E.isNull()) {
Cache_.add(H,ClientCacheEntry{.Start=Now, .Count=1});
Logger_.warning(Poco::format("RATE-LIMIT-EXCEEDED: from '%s'", R.clientAddress().toString()));
return false;
}
if((Now-E->Start)<Period) {
E->Count++;
Cache_.update(H,E);
if(E->Count > MaxCalls)
if(E->Count > MaxCalls) {
Logger_.warning(Poco::format("RATE-LIMIT-EXCEEDED: from '%s'", R.clientAddress().toString()));
return true;
}
return false;
}
E->Start = Now;
@@ -1445,20 +1543,23 @@ namespace OpenWifi {
Request = &RequestIn;
Response = &ResponseIn;
if(RateLimited_ && RESTAPI_RateLimiter()->IsRateLimited(RequestIn,MyRates_.Interval, MyRates_.MaxCalls))
return;
if(RateLimited_ && RESTAPI_RateLimiter()->IsRateLimited(RequestIn,MyRates_.Interval, MyRates_.MaxCalls)) {
return UnAuthorized("Rate limit exceeded.",RATE_LIMIT_EXCEEDED);
}
if (!ContinueProcessing())
return;
if (AlwaysAuthorize_ && !IsAuthorized()) {
return;
bool Expired=false;
if (AlwaysAuthorize_ && !IsAuthorized(Expired)) {
if(Expired)
return UnAuthorized(RESTAPI::Errors::ExpiredToken, EXPIRED_TOKEN);
return UnAuthorized(RESTAPI::Errors::InvalidCredentials, ACCESS_DENIED);
}
std::string Reason;
if(!RoleIsAuthorized(RequestIn.getURI(), Request->getMethod(), Reason)) {
UnAuthorized(Reason);
return;
return UnAuthorized(Reason, ACCESS_DENIED);
}
ParseParameters();
@@ -1678,10 +1779,10 @@ namespace OpenWifi {
Poco::JSON::Stringifier::stringify(ErrorObject, Answer);
}
inline void UnAuthorized(const std::string & Reason = "") {
inline void UnAuthorized(const std::string & Reason = "", int Code = INVALID_CREDENTIALS ) {
PrepareResponse(Poco::Net::HTTPResponse::HTTP_FORBIDDEN);
Poco::JSON::Object ErrorObject;
ErrorObject.set("ErrorCode",403);
ErrorObject.set("ErrorCode",Code);
ErrorObject.set("ErrorDetails",Request->getMethod());
ErrorObject.set("ErrorDescription",Reason.empty() ? "No access allowed." : Reason) ;
std::ostream &Answer = Response->send();
@@ -1796,7 +1897,7 @@ namespace OpenWifi {
return true;
}
inline bool IsAuthorized();
inline bool IsAuthorized(bool & Expired);
inline void ReturnObject(Poco::JSON::Object &Object) {
PrepareResponse();
@@ -1817,7 +1918,7 @@ namespace OpenWifi {
QB_.SerialNumber = GetParameter(RESTAPI::Protocol::SERIALNUMBER, "");
QB_.StartDate = GetParameter(RESTAPI::Protocol::STARTDATE, 0);
QB_.EndDate = GetParameter(RESTAPI::Protocol::ENDDATE, 0);
QB_.Offset = GetParameter(RESTAPI::Protocol::OFFSET, 1);
QB_.Offset = GetParameter(RESTAPI::Protocol::OFFSET, 0);
QB_.Limit = GetParameter(RESTAPI::Protocol::LIMIT, 100);
QB_.Filter = GetParameter(RESTAPI::Protocol::FILTER, "");
QB_.Select = GetParameter(RESTAPI::Protocol::SELECT, "");
@@ -1829,7 +1930,7 @@ namespace OpenWifi {
QB_.AdditionalInfo = GetBoolParameter(RESTAPI::Protocol::WITHEXTENDEDINFO,false);
if(QB_.Offset<1)
QB_.Offset=1;
QB_.Offset=0;
return true;
}
@@ -1998,6 +2099,50 @@ namespace OpenWifi {
Poco::JSON::Object Body_;
};
class KafkaProducer : public Poco::Runnable {
public:
inline void run();
void Start() {
if(!Running_) {
Running_=true;
Worker_.start(*this);
}
}
void Stop() {
if(Running_) {
Running_=false;
Worker_.wakeUp();
Worker_.join();
}
}
private:
std::mutex Mutex_;
Poco::Thread Worker_;
std::atomic_bool Running_=false;
};
class KafkaConsumer : public Poco::Runnable {
public:
inline void run();
void Start() {
if(!Running_) {
Running_=true;
Worker_.start(*this);
}
}
void Stop() {
if(Running_) {
Running_=false;
Worker_.wakeUp();
Worker_.join();
}
}
private:
std::mutex Mutex_;
Poco::Thread Worker_;
std::atomic_bool Running_=false;
};
class KafkaManager : public SubSystemServer {
public:
struct KMessage {
@@ -2006,33 +2151,32 @@ namespace OpenWifi {
PayLoad;
};
friend class KafkaConsumer;
friend class KafkaProducer;
inline void initialize(Poco::Util::Application & self) override;
static KafkaManager *instance() {
static KafkaManager instance;
return &instance;
static KafkaManager * instance_ = new KafkaManager;
return instance_;
}
inline int Start() override {
if(!KafkaEnabled_)
return 0;
ProducerThr_ = std::make_unique<std::thread>([this]() { this->ProducerThr(); });
ConsumerThr_ = std::make_unique<std::thread>([this]() { this->ConsumerThr(); });
ConsumerThr_.Start();
ProducerThr_.Start();
return 0;
}
inline void Stop() override {
if(KafkaEnabled_) {
ProducerRunning_ = ConsumerRunning_ = false;
ProducerThr_->join();
ConsumerThr_->join();
ProducerThr_.Stop();
ConsumerThr_.Stop();
return;
}
}
inline void ProducerThr();
inline void ConsumerThr();
inline void PostMessage(const std::string &topic, const std::string & key, const std::string &PayLoad, bool WrapMessage = true ) {
if(KafkaEnabled_) {
std::lock_guard G(Mutex_);
@@ -2085,18 +2229,13 @@ namespace OpenWifi {
// void WakeUp();
private:
std::mutex ProducerMutex_;
std::mutex ConsumerMutex_;
bool KafkaEnabled_ = false;
std::atomic_bool ProducerRunning_ = false;
std::atomic_bool ConsumerRunning_ = false;
std::queue<KMessage> Queue_;
std::string SystemInfoWrapper_;
std::unique_ptr<std::thread> ConsumerThr_;
std::unique_ptr<std::thread> ProducerThr_;
int FunctionId_=1;
Types::NotifyTable Notifiers_;
std::unique_ptr<cppkafka::Configuration> Config_;
KafkaProducer ProducerThr_;
KafkaConsumer ConsumerThr_;
inline void PartitionAssignment(const cppkafka::TopicPartitionList& partitions) {
Logger_.information(Poco::format("Partition assigned: %Lu...",(uint64_t )partitions.front().get_partition()));
@@ -2121,8 +2260,8 @@ namespace OpenWifi {
}
static AuthClient *instance() {
static AuthClient instance;
return &instance;
static AuthClient * instance_ = new AuthClient;
return instance_;
}
inline int Start() override {
@@ -2130,25 +2269,20 @@ namespace OpenWifi {
}
inline void Stop() override {
Cache_.clear();
}
inline void RemovedCachedToken(const std::string &Token) {
std::lock_guard G(Mutex_);
UserCache_.erase(Token);
Cache_.remove(Token);
}
inline static bool IsTokenExpired(const SecurityObjects::WebToken &T) {
return ((T.expires_in_+T.created_)<std::time(nullptr));
}
inline bool IsAuthorized(Poco::Net::HTTPServerRequest & Request, std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo ) {
std::lock_guard G(Mutex_);
auto User = UserCache_.find(SessionToken);
if(User != UserCache_.end() && !IsTokenExpired(User->second.webtoken)) {
UInfo = User->second;
return true;
} else {
inline bool RetrieveTokenInformation(const std::string & SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo, bool & Expired) {
try {
Types::StringPairVec QueryData;
QueryData.push_back(std::make_pair("token",SessionToken));
OpenAPIRequestGet Req( uSERVICE_SECURITY,
@@ -2158,49 +2292,39 @@ namespace OpenWifi {
Poco::JSON::Object::Ptr Response;
if(Req.Do(Response)==Poco::Net::HTTPResponse::HTTP_OK) {
if(Response->has("tokenInfo") && Response->has("userInfo")) {
SecurityObjects::UserInfoAndPolicy P;
P.from_json(Response);
UserCache_[SessionToken] = P;
UInfo = P;
UInfo.from_json(Response);
if(IsTokenExpired(UInfo.webtoken)) {
Expired = true;
return false;
}
Expired = false;
Cache_.update(SessionToken, UInfo);
return true;
}
}
} catch (...) {
}
Expired = false;
return false;
}
inline bool IsTokenAuthorized(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo) {
std::lock_guard G(Mutex_);
auto User = UserCache_.find(SessionToken);
if(User != UserCache_.end() && !IsTokenExpired(User->second.webtoken)) {
UInfo = User->second;
return true;
} else {
Types::StringPairVec QueryData;
QueryData.push_back(std::make_pair("token",SessionToken));
OpenAPIRequestGet Req(uSERVICE_SECURITY,
"/api/v1/validateToken",
QueryData,
5000);
Poco::JSON::Object::Ptr Response;
if(Req.Do(Response)==Poco::Net::HTTPResponse::HTTP_OK) {
if(Response->has("tokenInfo") && Response->has("userInfo")) {
SecurityObjects::UserInfoAndPolicy P;
P.from_json(Response);
UserCache_[SessionToken] = P;
UInfo = P;
}
return true;
}
}
inline bool IsAuthorized(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo, bool & Expired) {
auto User = Cache_.get(SessionToken);
if(!User.isNull()) {
if(IsTokenExpired(User->webtoken)) {
Expired = true;
return false;
}
Expired = false;
UInfo = *User;
return true;
}
return RetrieveTokenInformation(SessionToken, UInfo, Expired);
}
private:
OpenWifi::SecurityObjects::UserInfoCache UserCache_;
Poco::ExpireLRUCache<std::string,OpenWifi::SecurityObjects::UserInfoAndPolicy> Cache_{1024,1200000 };
};
inline AuthClient * AuthClient() { return AuthClient::instance(); }
@@ -2260,14 +2384,14 @@ namespace OpenWifi {
}
static ALBHealthCheckServer *instance() {
static ALBHealthCheckServer instance;
return &instance;
static ALBHealthCheckServer * instance = new ALBHealthCheckServer;
return instance;
}
inline int Start() override;
inline void Stop() override {
if(Server_)
if(Running_)
Server_->stop();
}
@@ -2275,6 +2399,7 @@ namespace OpenWifi {
std::unique_ptr<Poco::Net::HTTPServer> Server_;
std::unique_ptr<Poco::Net::ServerSocket> Socket_;
int Port_ = 0;
std::atomic_bool Running_=false;
};
inline ALBHealthCheckServer * ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); }
@@ -2289,17 +2414,18 @@ namespace OpenWifi {
class RESTAPI_server : public SubSystemServer {
public:
static RESTAPI_server *instance() {
static RESTAPI_server instance;
return &instance;
static RESTAPI_server *instance_ = new RESTAPI_server;
return instance_;
}
int Start() override;
inline void Stop() override {
Logger_.information("Stopping ");
for( const auto & svr : RESTServers_ )
svr->stop();
Pool_.joinAll();
RESTServers_.clear();
}
inline void reinitialize(Poco::Util::Application &self) override;
inline Poco::Net::HTTPRequestHandler *CallServer(const char *Path) {
@@ -2354,7 +2480,7 @@ namespace OpenWifi {
if(!Svr.RootCA().empty())
Svr.LogCas(Logger_);
auto Params = new Poco::Net::HTTPServerParams;
Poco::Net::HTTPServerParams::Ptr Params = new Poco::Net::HTTPServerParams;
Params->setMaxThreads(50);
Params->setMaxQueued(200);
Params->setKeepAlive(true);
@@ -2371,8 +2497,8 @@ namespace OpenWifi {
public:
static RESTAPI_InternalServer *instance() {
static RESTAPI_InternalServer instance;
return &instance;
static RESTAPI_InternalServer *instance_ = new RESTAPI_InternalServer;
return instance_;
}
inline int Start() override;
@@ -2380,7 +2506,7 @@ namespace OpenWifi {
Logger_.information("Stopping ");
for( const auto & svr : RESTServers_ )
svr->stop();
RESTServers_.clear();
Pool_.stopAll();
}
inline void reinitialize(Poco::Util::Application &self) override;
@@ -2397,7 +2523,6 @@ namespace OpenWifi {
RESTAPI_InternalServer() noexcept: SubSystemServer("RESTAPIInternalServer", "REST-ISRV", "openwifi.internal.restapi")
{
}
};
inline RESTAPI_InternalServer * RESTAPI_InternalServer() { return RESTAPI_InternalServer::instance(); };
@@ -2415,7 +2540,7 @@ namespace OpenWifi {
}
private:
Poco::Logger & Logger_;
RESTAPI_GenericServer &Server_;
RESTAPI_GenericServer & Server_;
};
inline int RESTAPI_InternalServer::Start() {
@@ -2454,6 +2579,8 @@ namespace OpenWifi {
uint64_t LastUpdate=0;
};
class SubSystemServer;
typedef std::map<uint64_t, MicroServiceMeta> MicroServiceMetaMap;
typedef std::vector<MicroServiceMeta> MicroServiceMetaVec;
@@ -2474,11 +2601,13 @@ namespace OpenWifi {
DAEMON_BUS_TIMER(BusTimer),
SubSystems_(std::move(Subsystems)) {
instance_ = this;
RandomEngine_.seed(std::chrono::steady_clock::now().time_since_epoch().count());
}
[[nodiscard]] std::string Version() { return Version_; }
[[nodiscard]] const Poco::SharedPtr<Poco::Crypto::RSAKey> & Key() { return AppKey_; }
[[nodiscard]] inline const std::string & DataDir() { return DataDir_; }
[[nodiscard]] inline const std::string & WWWAssetsDir() { return WWWAssetsDir_; }
[[nodiscard]] bool Debug() const { return DebugMode_; }
[[nodiscard]] uint64_t ID() const { return ID_; }
[[nodiscard]] std::string Hash() const { return MyHash_; };
@@ -2491,6 +2620,13 @@ namespace OpenWifi {
static inline uint64_t GetPID() { return Poco::Process::id(); };
[[nodiscard]] inline const std::string GetPublicAPIEndPoint() { return MyPublicEndPoint_ + "/api/v1"; };
[[nodiscard]] inline const std::string & GetUIURI() const { return UIURI_;};
[[nodiscard]] inline uint64_t Random(uint64_t ceiling) {
return (RandomEngine_() % ceiling);
}
[[nodiscard]] inline uint64_t Random(uint64_t min, uint64_t max) {
return ((RandomEngine_() % (max-min)) + min);
}
inline void Exit(int Reason);
inline void BusMessageReceived(const std::string &Key, const std::string & Message);
@@ -2512,7 +2648,7 @@ namespace OpenWifi {
inline void InitializeSubSystemServers();
inline void StartSubSystemServers();
inline void StopSubSystemServers();
[[nodiscard]] inline std::string CreateUUID();
[[nodiscard]] static inline std::string CreateUUID();
inline bool SetSubsystemLogLevel(const std::string &SubSystem, const std::string &Level);
inline void Reload(const std::string &Sub);
inline Types::StringVec GetSubSystems() const;
@@ -2542,9 +2678,10 @@ namespace OpenWifi {
std::string ConfigFileName_;
Poco::UUIDGenerator UUIDGenerator_;
uint64_t ID_ = 1;
Poco::SharedPtr<Poco::Crypto::RSAKey> AppKey_ = nullptr;
Poco::SharedPtr<Poco::Crypto::RSAKey> AppKey_;
bool DebugMode_ = false;
std::string DataDir_;
std::string WWWAssetsDir_;
SubSystemVec SubSystems_;
Poco::Crypto::CipherFactory & CipherFactory_ = Poco::Crypto::CipherFactory::defaultFactory();
Poco::Crypto::Cipher * Cipher_ = nullptr;
@@ -2557,6 +2694,7 @@ namespace OpenWifi {
std::string Version_{std::string(APP_VERSION) + "("+ BUILD_NUMBER + ")"};
BusEventManager BusEventManager_;
std::mutex InfraMutex_;
std::default_random_engine RandomEngine_;
std::string DAEMON_PROPERTIES_FILENAME;
std::string DAEMON_ROOT_ENV_VAR;
@@ -2733,6 +2871,9 @@ namespace OpenWifi {
logger().log(E);
}
}
WWWAssetsDir_ = ConfigPath("openwifi.restapi.wwwassets","");
if(WWWAssetsDir_.empty())
WWWAssetsDir_ = DataDir_;
LoadMyConfig();
@@ -2839,12 +2980,41 @@ namespace OpenWifi {
inline void MicroService::StopSubSystemServers() {
BusEventManager_.Stop();
for(auto i=SubSystems_.rbegin(); i!=SubSystems_.rend(); ++i)
for(auto i=SubSystems_.rbegin(); i!=SubSystems_.rend(); ++i) {
(*i)->Stop();
}
}
[[nodiscard]] inline std::string MicroService::CreateUUID() {
return UUIDGenerator_.create().toString();
static std::random_device rd;
static std::mt19937_64 gen(rd());
static std::uniform_int_distribution<> dis(0, 15);
static std::uniform_int_distribution<> dis2(8, 11);
std::stringstream ss;
int i;
ss << std::hex;
for (i = 0; i < 8; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 4; i++) {
ss << dis(gen);
}
ss << "-4";
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
ss << dis2(gen);
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 12; i++) {
ss << dis(gen);
};
return ss.str();
}
inline bool MicroService::SetSubsystemLogLevel(const std::string &SubSystem, const std::string &Level) {
@@ -2858,7 +3028,6 @@ namespace OpenWifi {
}
return true;
} else {
// std::cout << "Sub:" << SubSystem << " Level:" << Level << std::endl;
for (auto i : SubSystems_) {
if (Sub == Poco::toLower(i->Name())) {
i->Logger().setLevel(P);
@@ -3008,13 +3177,28 @@ namespace OpenWifi {
StartSubSystemServers();
waitForTerminationRequest();
StopSubSystemServers();
logger.notice(Poco::format("Stopped %s...",DAEMON_APP_NAME));
}
return Application::EXIT_OK;
}
AppServiceRegistry::AppServiceRegistry() {
FileName = MicroService::instance().DataDir() + "/registry.json";
Poco::File F(FileName);
try {
if(F.exists()) {
std::ostringstream OS;
std::ifstream IF(FileName);
Poco::StreamCopier::copyStream(IF, OS);
Registry_ = nlohmann::json::parse(OS.str());
}
} catch (...) {
Registry_ = nlohmann::json::parse("{}");
}
}
inline void SubSystemServer::initialize(Poco::Util::Application &self) {
Logger_.notice("Initializing...");
auto i = 0;
@@ -3072,6 +3256,7 @@ namespace OpenWifi {
inline int ALBHealthCheckServer::Start() {
if(MicroService::instance().ConfigGetBool("alb.enable",false)) {
Running_=true;
Port_ = (int)MicroService::instance().ConfigGetInt("alb.port",15015);
Socket_ = std::make_unique<Poco::Net::ServerSocket>(Port_);
auto Params = new Poco::Net::HTTPServerParams;
@@ -3116,41 +3301,42 @@ namespace OpenWifi {
KafkaEnabled_ = MicroService::instance().ConfigGetBool("openwifi.kafka.enable",false);
}
inline void KafkaManager::ProducerThr() {
inline void KafkaProducer::run() {
cppkafka::Configuration Config({
{ "client.id", MicroService::instance().ConfigGetString("openwifi.kafka.client.id") },
{ "metadata.broker.list", MicroService::instance().ConfigGetString("openwifi.kafka.brokerlist") }
});
SystemInfoWrapper_ = R"lit({ "system" : { "id" : )lit" +
KafkaManager()->SystemInfoWrapper_ = R"lit({ "system" : { "id" : )lit" +
std::to_string(MicroService::instance().ID()) +
R"lit( , "host" : ")lit" + MicroService::instance().PrivateEndPoint() +
R"lit(" } , "payload" : )lit" ;
cppkafka::Producer Producer(Config);
ProducerRunning_ = true;
while(ProducerRunning_) {
Running_ = true;
while(Running_) {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
try
{
std::lock_guard G(ProducerMutex_);
std::lock_guard G(Mutex_);
auto Num=0;
while (!Queue_.empty()) {
const auto M = Queue_.front();
while (!KafkaManager()->Queue_.empty()) {
const auto M = KafkaManager()->Queue_.front();
Producer.produce(
cppkafka::MessageBuilder(M.Topic).key(M.Key).payload(M.PayLoad));
Queue_.pop();
KafkaManager()->Queue_.pop();
Num++;
}
if(Num)
Producer.flush();
} catch (const cppkafka::HandleException &E ) {
Logger_.warning(Poco::format("Caught a Kafka exception (producer): %s",std::string{E.what()}));
KafkaManager()->Logger_.warning(Poco::format("Caught a Kafka exception (producer): %s",std::string{E.what()}));
} catch (const Poco::Exception &E) {
Logger_.log(E);
KafkaManager()->Logger_.log(E);
}
}
Producer.flush();
}
inline void KafkaManager::ConsumerThr() {
inline void KafkaConsumer::run() {
cppkafka::Configuration Config({
{ "client.id", MicroService::instance().ConfigGetString("openwifi.kafka.client.id") },
{ "metadata.broker.list", MicroService::instance().ConfigGetString("openwifi.kafka.brokerlist") },
@@ -3170,13 +3356,13 @@ namespace OpenWifi {
cppkafka::Consumer Consumer(Config);
Consumer.set_assignment_callback([this](cppkafka::TopicPartitionList& partitions) {
if(!partitions.empty()) {
Logger_.information(Poco::format("Partition assigned: %Lu...",
KafkaManager()->Logger_.information(Poco::format("Partition assigned: %Lu...",
(uint64_t)partitions.front().get_partition()));
}
});
Consumer.set_revocation_callback([this](const cppkafka::TopicPartitionList& partitions) {
if(!partitions.empty()) {
Logger_.information(Poco::format("Partition revocation: %Lu...",
KafkaManager()->Logger_.information(Poco::format("Partition revocation: %Lu...",
(uint64_t)partitions.front().get_partition()));
}
});
@@ -3185,13 +3371,13 @@ namespace OpenWifi {
auto BatchSize = MicroService::instance().ConfigGetInt("openwifi.kafka.consumer.batchsize",20);
Types::StringVec Topics;
for(const auto &i:Notifiers_)
for(const auto &i:KafkaManager()->Notifiers_)
Topics.push_back(i.first);
Consumer.subscribe(Topics);
ConsumerRunning_ = true;
while(ConsumerRunning_) {
Running_ = true;
while(Running_) {
try {
std::vector<cppkafka::Message> MsgVec = Consumer.poll_batch(BatchSize, std::chrono::milliseconds(200));
for(auto const &Msg:MsgVec) {
@@ -3199,14 +3385,14 @@ namespace OpenWifi {
continue;
if (Msg.get_error()) {
if (!Msg.is_eof()) {
Logger_.error(Poco::format("Error: %s", Msg.get_error().to_string()));
KafkaManager()->Logger_.error(Poco::format("Error: %s", Msg.get_error().to_string()));
}if(!AutoCommit)
Consumer.async_commit(Msg);
continue;
}
std::lock_guard G(ConsumerMutex_);
auto It = Notifiers_.find(Msg.get_topic());
if (It != Notifiers_.end()) {
std::lock_guard G(Mutex_);
auto It = KafkaManager()->Notifiers_.find(Msg.get_topic());
if (It != KafkaManager()->Notifiers_.end()) {
Types::TopicNotifyFunctionList &FL = It->second;
std::string Key{Msg.get_key()};
std::string Payload{Msg.get_payload()};
@@ -3219,11 +3405,12 @@ namespace OpenWifi {
Consumer.async_commit(Msg);
}
} catch (const cppkafka::HandleException &E) {
Logger_.warning(Poco::format("Caught a Kafka exception (consumer): %s",std::string{E.what()}));
KafkaManager()->Logger_.warning(Poco::format("Caught a Kafka exception (consumer): %s",std::string{E.what()}));
} catch (const Poco::Exception &E) {
Logger_.log(E);
KafkaManager()->Logger_.log(E);
}
}
Consumer.unsubscribe();
}
inline void RESTAPI_server::reinitialize(Poco::Util::Application &self) {
@@ -3444,11 +3631,9 @@ namespace OpenWifi {
if(Response.getStatus()==Poco::Net::HTTPResponse::HTTP_OK) {
Poco::JSON::Parser P;
ResponseObject = P.parse(is).extract<Poco::JSON::Object::Ptr>();
// std::cout << "Response OK" << std::endl;
} else {
Poco::JSON::Parser P;
ResponseObject = P.parse(is).extract<Poco::JSON::Object::Ptr>();
// std::cout << "Response: " << Response.getStatus() << std::endl;
}
return Response.getStatus();
}
@@ -3494,11 +3679,9 @@ namespace OpenWifi {
if(Response.getStatus()==Poco::Net::HTTPResponse::HTTP_OK) {
Poco::JSON::Parser P;
ResponseObject = P.parse(is).extract<Poco::JSON::Object::Ptr>();
// std::cout << "Response OK" << std::endl;
} else {
Poco::JSON::Parser P;
ResponseObject = P.parse(is).extract<Poco::JSON::Object::Ptr>();
// std::cout << "Response: " << Response.getStatus() << std::endl;
}
return Response.getStatus();
}
@@ -3524,9 +3707,9 @@ namespace OpenWifi {
}
#ifdef TIP_SECURITY_SERVICE
[[nodiscard]] bool AuthServiceIsAuthorized(Poco::Net::HTTPServerRequest & Request,std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo );
[[nodiscard]] bool AuthServiceIsAuthorized(Poco::Net::HTTPServerRequest & Request,std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo, bool & Expired );
#endif
inline bool RESTAPIHandler::IsAuthorized() {
inline bool RESTAPIHandler::IsAuthorized( bool & Expired ) {
if(Internal_) {
auto Allowed = MicroService::instance().IsValidAPIKEY(*Request);
if(!Allowed) {
@@ -3556,9 +3739,9 @@ namespace OpenWifi {
}
}
#ifdef TIP_SECURITY_SERVICE
if (AuthServiceIsAuthorized(*Request, SessionToken_, UserInfo_)) {
if (AuthServiceIsAuthorized(*Request, SessionToken_, UserInfo_, Expired)) {
#else
if (AuthClient()->IsAuthorized(*Request, SessionToken_, UserInfo_)) {
if (AuthClient()->IsAuthorized( SessionToken_, UserInfo_, Expired)) {
#endif
if(Server_.LogIt(Request->getMethod(),true)) {
Logger_.debug(Poco::format("X-REQ-ALLOWED(%s): User='%s@%s' Method='%s' Path='%s",
@@ -3575,7 +3758,6 @@ namespace OpenWifi {
Utils::FormatIPv6(Request->clientAddress().toString()),
Request->getMethod(), Request->getURI()));
}
UnAuthorized();
}
return false;
}
@@ -3585,40 +3767,14 @@ namespace OpenWifi {
}
namespace OpenWifi::Utils {
inline void SaveSystemId(uint64_t Id) {
try {
std::ofstream O;
O.open(MicroService::instance().DataDir() + "/system.id",std::ios::binary | std::ios::trunc);
O << Id;
O.close();
} catch (...)
{
std::cout << "Could not save system ID" << std::endl;
}
}
[[nodiscard]] inline uint64_t GetSystemId() {
uint64_t ID=0;
// if the system ID file exists, open and read it.
Poco::File SID( MicroService::instance().DataDir() + "/system.id");
try {
if (SID.exists()) {
std::ifstream I;
I.open(SID.path());
I >> ID;
I.close();
if (ID == 0)
if(!AppServiceRegistry().Get("systemid",ID)) {
return InitializeSystemId();
}
return ID;
} else {
return InitializeSystemId();
}
} catch (...) {
return InitializeSystemId();
}
}
}
}
namespace OpenWifi::CIDR {
@@ -3759,5 +3915,3 @@ namespace OpenWifi::CIDR {
return std::all_of(cbegin(Ranges), cend(Ranges), ValidateRange);
}
}
#endif // UCENTRALGW_MICROSERVICE_H

View File

@@ -1,25 +1,17 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
// Created by stephane bourque on 2021-11-16.
//
#ifndef UCENTRALGW_UCENTRALTYPES_H
#define UCENTRALGW_UCENTRALTYPES_H
#pragma once
#include <vector>
#include <string>
#include <map>
#include <functional>
#include <list>
#include <utility>
#include <vector>
#include <functional>
#include <string>
#include <queue>
#include "Poco/StringTokenizer.h"
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Stringifier.h"
#include <list>
#include <set>
namespace OpenWifi::Types {
typedef std::pair<std::string,std::string> StringPair;
@@ -35,69 +27,14 @@ namespace OpenWifi::Types {
typedef std::vector<uint64_t> TagList;
typedef std::string UUID_t;
typedef std::vector<UUID_t> UUIDvec_t;
}
inline void UpdateCountedMap(CountedMap &M, const std::string &S, uint64_t Increment=1) {
namespace OpenWifi {
inline void UpdateCountedMap(OpenWifi::Types::CountedMap &M, const std::string &S, uint64_t Increment=1) {
auto it = M.find(S);
if(it==M.end())
M[S] = Increment;
else
it->second += Increment;
}
inline std::string to_string( const StringVec &V) {
Poco::JSON::Array O;
for(const auto &i:V) {
O.add(i);
}
std::stringstream SS;
Poco::JSON::Stringifier::stringify(O,SS);
return SS.str();
}
inline std::string to_string( const StringPairVec &V) {
Poco::JSON::Array O;
for(const auto &i:V) {
Poco::JSON::Array OO;
OO.add(i.first);
OO.add(i.second);
O.add(OO);
}
std::stringstream SS;
Poco::JSON::Stringifier::stringify(O,SS);
return SS.str();
}
inline void from_string(const std::string &S, StringPairVec &V) {
try {
Poco::JSON::Parser P;
auto O = P.parse(S).extract<Poco::JSON::Array::Ptr>();
for(const auto &i:*O) {
auto Inner = i.extract<Poco::JSON::Array::Ptr>();
for(const auto &j:*Inner) {
auto S1 = i[0].toString();
auto S2 = i[1].toString();
V.push_back(std::make_pair(S1,S2));
}
}
} catch (...) {
}
}
inline void from_string(const std::string &S, StringVec &V) {
try {
Poco::JSON::Parser P;
auto O = P.parse(S).extract<Poco::JSON::Array::Ptr>();
for(auto const &i:*O) {
V.push_back(i.toString());
}
} catch (...) {
}
}
};
#endif // UCENTRALGW_UCENTRALTYPES_H
}

View File

@@ -2,8 +2,7 @@
// Created by stephane bourque on 2021-09-12.
//
#ifndef OWPROV_RESTAPI_ERRORS_H
#define OWPROV_RESTAPI_ERRORS_H
#pragma once
namespace OpenWifi::RESTAPI::Errors {
static const std::string MissingUUID{"Missing UUID."};
@@ -47,13 +46,18 @@ namespace OpenWifi::RESTAPI::Errors {
static const std::string IdMustBe0{"To create a user, you must set the ID to 0"};
static const std::string InvalidUserRole{"Invalid userRole."};
static const std::string InvalidEmailAddress{"Invalid email address."};
static const std::string InvalidPassword{"Invalid password."};
static const std::string PasswordRejected{"Password was rejected. This maybe an old password."};
static const std::string InvalidIPRanges{"Invalid IP range specifications."};
static const std::string InvalidLOrderBy{"Invalid orderBy specification."};
static const std::string NeedMobileNumber{"You must provide at least one validated phone number."};
static const std::string BadMFAMethod{"MFA only supports sms or email."};
static const std::string InvalidCredentials{"Invalid credentials (username/password)."};
static const std::string InvalidPassword{"Password does not conform to basic password rules."};
static const std::string UserPendingVerification{"User access denied pending email verification."};
static const std::string PasswordMustBeChanged{"Password must be changed."};
static const std::string UnrecognizedRequest{"Ill-formed request. Please consult documentation."};
static const std::string MissingAuthenticationInformation{"Missing authentication information."};
static const std::string InsufficientAccessRights{"Insufficient access rights to complete the operation."};
static const std::string ExpiredToken{"Token has expired, user must login."};
}
#endif //OWPROV_RESTAPI_ERRORS_H

View File

@@ -6,8 +6,7 @@
// Arilia Wireless Inc.
//
#ifndef UCENTRALGW_RESTAPI_PROTOCOL_H
#define UCENTRALGW_RESTAPI_PROTOCOL_H
#pragma once
namespace OpenWifi::RESTAPI::Protocol {
static const char * CAPABILITIES = "capabilities";
@@ -136,5 +135,3 @@ namespace OpenWifi::RESTAPI::Protocol {
static const char * UI = "UI";
}
#endif // UCENTRALGW_RESTAPI_PROTOCOL_H

View File

@@ -2,8 +2,7 @@
// Created by stephane bourque on 2021-10-06.
//
#ifndef OPENWIFI_STORAGE_H
#define OPENWIFI_STORAGE_H
#pragma once
#include "Poco/Data/Session.h"
#include "Poco/Data/SessionPool.h"
@@ -26,13 +25,6 @@ namespace OpenWifi {
class StorageClass : public SubSystemServer {
public:
/* static StorageClass *instance() {
if (instance_ == nullptr) {
instance_ = new StorageClass;
}
return instance_;
}
*/
StorageClass() noexcept:
SubSystemServer("StorageClass", "STORAGE-SVR", "storage")
{
@@ -56,18 +48,18 @@ namespace OpenWifi {
}
void Stop() override {
Pool_->shutdown();
}
[[nodiscard]] inline std::string ComputeRange(uint64_t From, uint64_t HowMany) {
if(dbType_==sqlite) {
return " LIMIT " + std::to_string(From-1) + ", " + std::to_string(HowMany) + " ";
return " LIMIT " + std::to_string(From) + ", " + std::to_string(HowMany) + " ";
} else if(dbType_==pgsql) {
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " ";
} else if(dbType_==mysql) {
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " ";
}
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " ";
}
inline std::string ConvertParams(const std::string & S) const {
@@ -96,15 +88,13 @@ namespace OpenWifi {
inline int Setup_PostgreSQL();
protected:
std::unique_ptr<Poco::Data::SessionPool> Pool_;
std::unique_ptr<Poco::Data::SQLite::Connector> SQLiteConn_;
std::unique_ptr<Poco::Data::PostgreSQL::Connector> PostgresConn_;
std::unique_ptr<Poco::Data::MySQL::Connector> MySQLConn_;
Poco::SharedPtr<Poco::Data::SessionPool> Pool_;
Poco::Data::SQLite::Connector SQLiteConn_;
Poco::Data::PostgreSQL::Connector PostgresConn_;
Poco::Data::MySQL::Connector MySQLConn_;
DBType dbType_ = sqlite;
};
// inline StorageClass * Storage() { return StorageClass::instance(); }
#ifdef SMALL_BUILD
int Service::Setup_MySQL() { Daemon()->exit(Poco::Util::Application::EXIT_CONFIG); return 0; }
int Service::Setup_PostgreSQL() { Daemon()->exit(Poco::Util::Application::EXIT_CONFIG); return 0; }
@@ -116,9 +106,8 @@ namespace OpenWifi {
auto DBName = MicroService::instance().DataDir() + "/" + MicroService::instance().ConfigGetString("storage.type.sqlite.db");
auto NumSessions = MicroService::instance().ConfigGetInt("storage.type.sqlite.maxsessions", 64);
auto IdleTime = MicroService::instance().ConfigGetInt("storage.type.sqlite.idletime", 60);
SQLiteConn_ = std::make_unique<Poco::Data::SQLite::Connector>();
SQLiteConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(SQLiteConn_->name(), DBName, 4, NumSessions, IdleTime);
SQLiteConn_.registerConnector();
Pool_ = Poco::SharedPtr<Poco::Data::SessionPool>(new Poco::Data::SessionPool(SQLiteConn_.name(), DBName, 4, NumSessions, IdleTime));
return 0;
}
@@ -141,9 +130,8 @@ namespace OpenWifi {
";port=" + Port +
";compress=true;auto-reconnect=true";
MySQLConn_ = std::make_unique<Poco::Data::MySQL::Connector>();
MySQLConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(MySQLConn_->name(), ConnectionStr, 4, NumSessions, IdleTime);
MySQLConn_.registerConnector();
Pool_ = Poco::SharedPtr<Poco::Data::SessionPool>(new Poco::Data::SessionPool(MySQLConn_.name(), ConnectionStr, 4, NumSessions, IdleTime));
return 0;
}
@@ -168,14 +156,11 @@ namespace OpenWifi {
" port=" + Port +
" connect_timeout=" + ConnectionTimeout;
PostgresConn_ = std::make_unique<Poco::Data::PostgreSQL::Connector>();
PostgresConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(PostgresConn_->name(), ConnectionStr, 4, NumSessions, IdleTime);
PostgresConn_.registerConnector();
Pool_ = Poco::SharedPtr<Poco::Data::SessionPool>(new Poco::Data::SessionPool(PostgresConn_.name(), ConnectionStr, 4, NumSessions, IdleTime));
return 0;
}
#endif
}
#endif //OPENWIFI_STORAGE_H

View File

@@ -6,8 +6,7 @@
// Arilia Wireless Inc.
//
#ifndef __OPENWIFI_ORM_H__
#define __OPENWIFI_ORM_H__
#pragma once
#include <string>
#include <memory>
@@ -755,4 +754,3 @@ namespace ORM {
};
}
#endif

View File

@@ -5,9 +5,7 @@
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALGW_UCENTRALPROTOCOL_H
#define UCENTRALGW_UCENTRALPROTOCOL_H
#pragma once
#include "Poco/String.h"
@@ -130,5 +128,3 @@ namespace OpenWifi::uCentralProtocol {
return ET_UNKNOWN;
};
}
#endif // UCENTRALGW_UCENTRALPROTOCOL_H

View File

@@ -175,6 +175,25 @@ namespace OpenWifi {
return false;
}
uint64_t Storage::GetBlackListDeviceCount() {
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Select(Sess);
std::string St{"SELECT Count(*) FROM BlackList"};
uint64_t Count = 0 ;
Select << ConvertParams(St),
Poco::Data::Keywords::into(Count);
Select.execute();
return Count;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return 0;
}
bool Storage::IsBlackListed(std::string &SerialNumber) {
try {
Poco::Data::Session Sess = Pool_->get();

View File

@@ -7,10 +7,11 @@
//
#include "StorageService.h"
#include "CentralConfig.h"
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Object.h"
#include "Poco/Data/RecordSet.h"
#include "CapabilitiesCache.h"
namespace OpenWifi {
@@ -25,23 +26,11 @@ namespace OpenWifi {
uint64_t Now = std::time(nullptr);
// Find compatible in the capabilities...
std::string Compatible;
OpenWifi::Config::Capabilities Caps(Capabilities);
Poco::JSON::Parser P;
Poco::JSON::Parser IncomingParser;
Poco::JSON::Object::Ptr Obj = IncomingParser.parse(Capabilities).extract<Poco::JSON::Object::Ptr>();
Poco::DynamicStruct ds = *Obj;
if(ds.contains("compatible")) {
Compat = Compatible = ds["compatible"].toString();
} else {
// Maybe this is an old firmware
auto TmpCompatible = ds["model"]["id"].toString();
for(const auto &i:TmpCompatible)
Compatible += (char) ( i==',' ? '_' : i);
}
Compat = Caps.Compatible();
if(!Caps.Compatible().empty() && !Caps.Platform().empty())
CapabilitiesCache::instance()->Add(Caps.Compatible(),Caps.Platform());
std::string St{"SELECT SerialNumber FROM Capabilities WHERE SerialNumber=?"};
Select << ConvertParams(St),
@@ -49,7 +38,10 @@ namespace OpenWifi {
Poco::Data::Keywords::use(SerialNumber);
Select.execute();
{ std::lock_guard G(Mutex_); CapsCache_[Compatible] = Capabilities; }
{
std::lock_guard G(Mutex_);
CapsCache_[Compat] = Capabilities;
}
if (SS.empty()) {
Logger_.information("Adding capabilities for " + SerialNumber);
@@ -78,7 +70,7 @@ namespace OpenWifi {
Update.execute();
}
Storage::SetDeviceCompatibility(SerialNumber, Compatible);
Storage::SetDeviceCompatibility(SerialNumber, Compat);
return true;
}
catch (const Poco::Exception &E) {

View File

@@ -157,9 +157,8 @@ typedef Poco::Tuple<
bool DatesIncluded = (FromDate != 0 || ToDate != 0);
std::string Fields{
"SELECT " +
DB_Command_SelectFields +
" FROM CommandList ORDER BY UUID ASC "};
"SELECT " + DB_Command_SelectFields + " FROM CommandList " };
std::string IntroStatement = SerialNumber.empty()
? Fields + std::string(DatesIncluded ? "WHERE " : "")
: Fields + "WHERE SerialNumber='" + SerialNumber + "'" +
@@ -177,7 +176,12 @@ typedef Poco::Tuple<
Poco::Data::Statement Select(Sess);
Select << IntroStatement + DateSelector + ComputeRange(Offset, HowMany),
std::string FullQuery = IntroStatement + DateSelector +
" ORDER BY Submitted ASC " + ComputeRange(Offset, HowMany);
// std::cout << "Offset: " << Offset << " >> " << FullQuery << std::endl;
Select << FullQuery,
Poco::Data::Keywords::into(Records);
Select.execute();
for (const auto &i : Records) {
@@ -355,12 +359,10 @@ typedef Poco::Tuple<
std::string st{"SELECT " +
DB_Command_SelectFields +
" FROM CommandList WHERE SerialNumber=? ORDER BY Submitted DESC"};
" FROM CommandList WHERE SerialNumber=? ORDER BY Submitted DESC " + ComputeRange(0, HowMany)};
Select << ConvertParams(st),
Poco::Data::Keywords::into(Records),
Poco::Data::Keywords::use(SerialNumber),
Poco::Data::Keywords::limit(HowMany);
Poco::Data::Keywords::use(SerialNumber);
Select.execute();
for (auto i : Records) {
@@ -677,7 +679,7 @@ typedef Poco::Tuple<
while(More) {
auto Command = RSet[0].convert<std::string>();
if(!Command.empty())
Types::UpdateCountedMap(R,Command);
UpdateCountedMap(R,Command);
More = RSet.moveNext();
}
return true;

View File

@@ -12,18 +12,51 @@
namespace OpenWifi {
/*
* Data model for DefaultConfigurations:
Table name: DefaultConfigs
const static std::string DB_DefConfig_SelectFields_ForCreation{
"Name VARCHAR(30) PRIMARY KEY, "
"Configuration TEXT, "
"Models TEXT, "
"Description TEXT, "
"Created BIGINT , "
"LastModified BIGINT)", now;
*/
"LastModified BIGINT)"};
const static std::string DB_DefConfig_SelectFields{
"Name, "
"Configuration, "
"Models, "
"Description, "
"Created, "
"LastModified "};
const static std::string DB_DefConfig_InsertValues{"?,?,?,?,?,?"};
typedef Poco::Tuple<
std::string,
std::string,
std::string,
std::string,
uint64_t,
uint64_t
> DefConfigRecordTuple;
typedef std::vector<DefConfigRecordTuple> DefConfigRecordList;
void Convert(const DefConfigRecordTuple &R, GWObjects::DefaultConfiguration & T) {
T.Name = R.get<0>();
T.Configuration = R.get<1>();
T.Models = R.get<2>();
T.Description = R.get<3>();
T.Created = R.get<4>();
T.LastModified = R.get<5>();
}
void Convert(const GWObjects::DefaultConfiguration & R, DefConfigRecordTuple &T) {
T.set<0>(R.Name);
T.set<1>(R.Configuration);
T.set<2>(R.Models);
T.set<3>(R.Description);
T.set<4>(R.Created);
T.set<5>(R.LastModified);
}
bool Storage::CreateDefaultConfiguration(std::string &Name, GWObjects::DefaultConfiguration &DefConfig) {
try {
@@ -38,44 +71,28 @@ namespace OpenWifi {
Name;
Select.execute();
if (TmpName.empty()) {
if (!TmpName.empty())
return false;
Config::Config Cfg(DefConfig.Configuration);
/*
"Name VARCHAR(30) PRIMARY KEY, "
"Configuration TEXT, "
"Models TEXT, "
"Description TEXT, "
"Created BIGINT , "
"LastModified BIGINT)", now;
*/
if (Cfg.Valid()) {
uint64_t Now = std::time(nullptr);
Poco::Data::Statement Insert(Sess);
std::string St{"INSERT INTO DefaultConfigs (Name, Configuration, Models, Description, Created, LastModified) "
"VALUES(?,?,?,?,?,?)"};
std::string St{"INSERT INTO DefaultConfigs " + DB_DefConfig_SelectFields + ") "
"VALUES(" + DB_DefConfig_InsertValues + ")"};
DefConfigRecordTuple R;
Convert(DefConfig,R);
Insert << ConvertParams(St),
Poco::Data::Keywords::use(Name),
Poco::Data::Keywords::use(DefConfig.Configuration),
Poco::Data::Keywords::use(DefConfig.Models),
Poco::Data::Keywords::use(DefConfig.Description),
Poco::Data::Keywords::use(Now),
Poco::Data::Keywords::use(Now);
Poco::Data::Keywords::use(R);
Insert.execute();
return true;
} else {
Logger_.warning("Cannot create device: invalid configuration.");
return false;
}
} else {
Logger_.warning("Default configuration already exists.");
}
}
catch (const Poco::Exception &E) {
Logger_.warning(Poco::format("%s(%s): Failed with: %s", std::string(__func__), Name, E.displayText()));
@@ -115,16 +132,16 @@ namespace OpenWifi {
uint64_t Now = time(nullptr);
Poco::Data::Statement Update(Sess);
std::string St{"UPDATE DefaultConfigs SET Configuration=?, Models=?, Description=?, LastModified=? WHERE Name=?"};
std::string St{"UPDATE DefaultConfigs SET Name=?, Configuration=?, Models=?, Description=?, Created=? , LastModified=? WHERE Name=?"};
DefConfigRecordTuple R;
Convert(DefConfig, R);
Update << ConvertParams(St),
Poco::Data::Keywords::use(DefConfig.Configuration),
Poco::Data::Keywords::use(DefConfig.Models),
Poco::Data::Keywords::use(DefConfig.Description),
Poco::Data::Keywords::use(Now),
Poco::Data::Keywords::use(R),
Poco::Data::Keywords::use(Name);
Update.execute();
return true;
} else {
Logger_.warning(
@@ -144,31 +161,21 @@ namespace OpenWifi {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Select(Sess);
std::string St{"SELECT "
"Name, "
"Configuration, "
"Models, "
"Description, "
"Created, "
"LastModified "
"FROM DefaultConfigs WHERE Name=?"};
std::string St{"SELECT " + DB_DefConfig_SelectFields +
" FROM DefaultConfigs WHERE Name=?"};
DefConfigRecordTuple R;
Select << ConvertParams(St),
Poco::Data::Keywords::into(DefConfig.Name),
Poco::Data::Keywords::into(DefConfig.Configuration),
Poco::Data::Keywords::into(DefConfig.Models),
Poco::Data::Keywords::into(DefConfig.Description),
Poco::Data::Keywords::into(DefConfig.Created),
Poco::Data::Keywords::into(DefConfig.LastModified),
Poco::Data::Keywords::into(R),
Poco::Data::Keywords::use(Name);
Select.execute();
if (DefConfig.Name.empty())
return false;
if (!DefConfig.Name.empty()) {
Convert(R,DefConfig);
return true;
}
return false;
}
catch (const Poco::Exception &E) {
Logger_.warning(Poco::format("%s(%s): Failed with: %s", std::string(__func__), Name, E.displayText()));
}
@@ -177,43 +184,19 @@ namespace OpenWifi {
bool Storage::GetDefaultConfigurations(uint64_t From, uint64_t HowMany,
std::vector<GWObjects::DefaultConfiguration> &DefConfigs) {
typedef Poco::Tuple<
std::string,
std::string,
std::string,
std::string,
uint64_t,
uint64_t> DeviceRecord;
typedef std::vector<DeviceRecord> RecordList;
RecordList Records;
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Select(Sess);
Select << "SELECT "
"Name, "
"Configuration, "
"Models, "
"Description, "
"Created, "
"LastModified "
"FROM DefaultConfigs ORDER BY NAME ASC " +
ComputeRange(From, HowMany),
DefConfigRecordList Records;
Select << "SELECT " + DB_DefConfig_SelectFields +
" FROM DefaultConfigs ORDER BY NAME ASC " + ComputeRange(From, HowMany) ,
Poco::Data::Keywords::into(Records);
Select.execute();
for (auto i: Records) {
GWObjects::DefaultConfiguration R{
.Name = i.get<0>(),
.Configuration = i.get<1>(),
.Models = i.get<2>(),
.Description = i.get<3>(),
.Created = i.get<4>(),
.LastModified = i.get<5>()};
GWObjects::DefaultConfiguration R;
Convert(i,R);
DefConfigs.push_back(R);
}
return true;
@@ -224,69 +207,43 @@ namespace OpenWifi {
return false;
}
bool FindInList(const std::string &Model, const std::string &List) {
unsigned long P = 0;
std::string Token;
while (P < List.size()) {
auto P2 = List.find_first_of(',', P);
if (P2 == std::string::npos) {
Token = List.substr(P);
if (Model.find(Token) != std::string::npos)
return true;
return false;
} else {
Token = List.substr(P, P2);
if (Model.find(Token) != std::string::npos)
return true;
}
P = P2 + 1;
}
return false;
}
bool Storage::FindDefaultConfigurationForModel(const std::string &Model, GWObjects::DefaultConfiguration &DefConfig) {
try {
typedef Poco::Tuple<
std::string,
std::string,
std::string,
std::string,
uint64_t,
uint64_t> DeviceRecord;
typedef std::vector<DeviceRecord> RecordList;
RecordList Records;
DefConfigRecordList Records;
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Select(Sess);
Select << "SELECT "
"Name, "
"Configuration, "
"Models, "
"Description, "
"Created, "
"LastModified "
"FROM DefaultConfigs ORDER BY NAME ASC " +
ComputeRange(1,1),
Select << "SELECT " + DB_DefConfig_SelectFields + " FROM DefaultConfigs",
Poco::Data::Keywords::into(Records);
Select.execute();
for (auto i: Records) {
DefConfig.Models = i.get<2>();
if (FindInList(Model, DefConfig.Models)) {
DefConfig.Name = i.get<0>();
Logger_.information(Poco::format("AUTO-PROVISIONING: found default configuration '%s' for model:%s",
DefConfig.Name, Model));
DefConfig.Name = i.get<0>();
DefConfig.Configuration = i.get<1>();
DefConfig.Models = i.get<2>();
DefConfig.Description = i.get<3>();
DefConfig.Created = i.get<4>();
DefConfig.LastModified = i.get<5>();
GWObjects::DefaultConfiguration Config;
Convert(i,Config);
// We need to account for possible old values in config...
if(Config.Models[0]!='[') {
if(Config.Models.empty())
Config.Models = "[]";
else
Config.Models = "[" + Config.Models + "]";
}
try {
Poco::JSON::Parser P;
auto List = P.parse(Config.Models).extract<Poco::JSON::Array::Ptr>();
for(const auto &j:*List) {
if(j.toString()=="*" || j.toString()==Model) {
DefConfig = Config;
return true;
}
}
} catch(...) {
// if we had problems parsing this entry, just skip it.
}
}
Logger_.information(Poco::format("AUTO-PROVISIONING: no default configuration for model:%s", Model));
return false;
}
@@ -296,5 +253,20 @@ namespace OpenWifi {
return false;
}
uint64_t Storage::GetDefaultConfigurationsCount() {
uint64_t Count = 0;
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Select(Sess);
Select << "SELECT Count(*) from DefaultConfigs" ,
Poco::Data::Keywords::into(Count);
Select.execute();
return Count;
}
catch (const Poco::Exception &E) {
Logger_.warning(Poco::format("%s: Failed with: %s", std::string(__func__), E.displayText()));
}
return Count;
}
}

View File

@@ -16,6 +16,7 @@
#include "StateProcessor.h"
#include "StorageService.h"
#include "framework/MicroService.h"
#include "CapabilitiesCache.h"
namespace OpenWifi {
@@ -265,7 +266,10 @@ namespace OpenWifi {
Config::Capabilities Caps(Capabilities);
GWObjects::DefaultConfiguration DefConfig;
if (FindDefaultConfigurationForModel(Caps.Model(), DefConfig)) {
if(!Caps.Platform().empty() && !Caps.Compatible().empty())
CapabilitiesCache::instance()->Add(Caps.Compatible(),Caps.Platform());
if (FindDefaultConfigurationForModel(Compat, DefConfig)) {
Config::Config NewConfig(DefConfig.Configuration);
NewConfig.SetUUID(Now);
D.Configuration = NewConfig.get();
@@ -282,6 +286,7 @@ namespace OpenWifi {
D.Manufacturer = Caps.Model();
D.Firmware = Firmware;
D.Notes = SecurityObjects::NoteInfoVec { SecurityObjects::NoteInfo{ (uint64_t)std::time(nullptr), "", "Auto-provisioned."}};
return CreateDevice(D);
}
@@ -722,19 +727,19 @@ namespace OpenWifi {
auto SerialNumber = RSet[0].convert<std::string>();
auto DeviceType = RSet[1].convert<std::string>();
auto Revision = RSet[2].convert<std::string>();
Types::UpdateCountedMap(Dashboard.vendors, OUIServer()->GetManufacturer(SerialNumber));
Types::UpdateCountedMap(Dashboard.deviceType, DeviceType);
UpdateCountedMap(Dashboard.vendors, OUIServer()->GetManufacturer(SerialNumber));
UpdateCountedMap(Dashboard.deviceType, DeviceType);
GWObjects::ConnectionState ConnState;
if(DeviceRegistry()->GetState(SerialNumber, ConnState)) {
Types::UpdateCountedMap(Dashboard.status, ConnState.Connected ? "connected" : "not connected");
Types::UpdateCountedMap(Dashboard.certificates, ComputeCertificateTag(ConnState.VerifiedCertificate));
Types::UpdateCountedMap(Dashboard.lastContact, ComputeUpLastContactTag(ConnState.LastContact));
UpdateCountedMap(Dashboard.status, ConnState.Connected ? "connected" : "not connected");
UpdateCountedMap(Dashboard.certificates, ComputeCertificateTag(ConnState.VerifiedCertificate));
UpdateCountedMap(Dashboard.lastContact, ComputeUpLastContactTag(ConnState.LastContact));
GWObjects::HealthCheck HC;
if(DeviceRegistry()->GetHealthcheck(SerialNumber,HC))
Types::UpdateCountedMap(Dashboard.healths, ComputeSanityTag(HC.Sanity));
UpdateCountedMap(Dashboard.healths, ComputeSanityTag(HC.Sanity));
else
Types::UpdateCountedMap(Dashboard.healths, ComputeSanityTag(100));
UpdateCountedMap(Dashboard.healths, ComputeSanityTag(100));
std::string LastStats;
if(DeviceRegistry()->GetStatistics(SerialNumber, LastStats) && !LastStats.empty()) {
Poco::JSON::Parser P;
@@ -744,34 +749,33 @@ namespace OpenWifi {
if(RawObject->has("unit")) {
auto Unit = RawObject->getObject("unit");
if (Unit->has("uptime")) {
Types::UpdateCountedMap(Dashboard.upTimes, ComputeUpTimeTag(Unit->get("uptime")));
UpdateCountedMap(Dashboard.upTimes, ComputeUpTimeTag(Unit->get("uptime")));
}
if (Unit->has("memory")) {
auto Memory = Unit->getObject("memory");
uint64_t Free = Memory->get("free");
uint64_t Total = Memory->get("total");
Types::UpdateCountedMap(Dashboard.memoryUsed, ComputeFreeMemoryTag(Free, Total));
UpdateCountedMap(Dashboard.memoryUsed, ComputeFreeMemoryTag(Free, Total));
}
if (Unit->has("load")) {
auto Load = Unit->getArray("load");
Types::UpdateCountedMap(Dashboard.load1,
UpdateCountedMap(Dashboard.load1,
ComputeLoadTag(Load->getElement<uint64_t>(0)));
Types::UpdateCountedMap(Dashboard.load5,
UpdateCountedMap(Dashboard.load5,
ComputeLoadTag(Load->getElement<uint64_t>(1)));
Types::UpdateCountedMap(Dashboard.load15,
UpdateCountedMap(Dashboard.load15,
ComputeLoadTag(Load->getElement<uint64_t>(2)));
}
}
uint64_t Associations_2G, Associations_5G;
StateProcessor::GetAssociations(RawObject, Associations_2G, Associations_5G);
Types::UpdateCountedMap(Dashboard.associations, "2G", Associations_2G);
Types::UpdateCountedMap(Dashboard.associations, "5G", Associations_5G);
UpdateCountedMap(Dashboard.associations, "2G", Associations_2G);
UpdateCountedMap(Dashboard.associations, "5G", Associations_5G);
}
Types::UpdateCountedMap(Dashboard.status, ConnState.Connected ? "connected" : "not connected");
UpdateCountedMap(Dashboard.status, ConnState.Connected ? "connected" : "not connected");
} else {
Types::UpdateCountedMap(Dashboard.status, "not connected");
UpdateCountedMap(Dashboard.status, "not connected");
}
More = RSet.moveNext();
}

View File

@@ -114,7 +114,7 @@ namespace OpenWifi {
std::string st{"SELECT " + DB_HealthCheckSelectFields + " FROM HealthChecks WHERE SerialNumber=? ORDER BY Recorded DESC "};
Select << ConvertParams(st) + ComputeRange(1,HowMany),
Select << ConvertParams(st) + ComputeRange(0,HowMany),
Poco::Data::Keywords::into(Records),
Poco::Data::Keywords::use(SerialNumber);
Select.execute();

View File

@@ -161,7 +161,7 @@ namespace OpenWifi {
Poco::Data::Statement Select(Sess);
std::string st{"SELECT " + DB_LogsSelectFields + " FROM DeviceLogs WHERE SerialNumber=? AND LogType=? ORDER BY Recorded DESC " + ComputeRange(1, HowMany)};
std::string st{"SELECT " + DB_LogsSelectFields + " FROM DeviceLogs WHERE SerialNumber=? AND LogType=? ORDER BY Recorded DESC " + ComputeRange(0, HowMany)};
Select << ConvertParams(st),
Poco::Data::Keywords::into(Records),
Poco::Data::Keywords::use(SerialNumber),

View File

@@ -112,7 +112,7 @@ namespace OpenWifi {
std::string St{"SELECT " +
DB_StatsSelectFields +
" FROM Statistics WHERE SerialNumber=? ORDER BY Recorded DESC "};
Select << ConvertParams(St) + ComputeRange(1, HowMany),
Select << ConvertParams(St) + ComputeRange(0, HowMany),
Poco::Data::Keywords::into(Records),
Poco::Data::Keywords::use(SerialNumber);
Select.execute();

View File

@@ -35,7 +35,7 @@ fi
token=""
result_file=result.json
username="tip@ucentral.com"
password="openwifi"
password="Snoopy99!!!"
browser_list=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl)
browser=""
@@ -137,7 +137,7 @@ deletecommand() {
}
listcommands() {
curl ${FLAGS} -X GET "https://${OWGW}/api/v1/commands?serialNumber=$1&limit=3000" \
curl ${FLAGS} -X GET "https://${OWGW}/api/v1/commands?serialNumber=$1&limit=300" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}