Refactoring project layout

This commit is contained in:
stephb9959
2021-10-19 19:35:42 -07:00
parent 670e61640f
commit 117e820d1e
76 changed files with 1337 additions and 133 deletions

View File

@@ -48,38 +48,48 @@ find_package(PostgreSQL REQUIRED)
find_package(MySQL REQUIRED)
find_package(Poco REQUIRED COMPONENTS JSON Crypto JWT Net Util NetSSL Data DataSQLite DataPostgreSQL DataMySQL)
include_directories(/usr/local/include /usr/local/opt/openssl/include src include/kafka /usr/local/opt/mysql-client/include)
add_executable( owsec
build
src/Daemon.h src/Daemon.cpp
src/MicroService.cpp src/MicroService.h
src/SubSystemServer.cpp src/SubSystemServer.h
src/RESTAPI_oauth2Handler.h src/RESTAPI_oauth2Handler.cpp
src/RESTAPI_handler.h src/RESTAPI_handler.cpp
src/RESTAPI_server.cpp src/RESTAPI_server.h
src/RESTAPI_SecurityObjects.cpp src/RESTAPI_SecurityObjects.h
src/RESTAPI_system_command.h src/RESTAPI_system_command.cpp
src/RESTAPI_protocol.h
src/AuthService.h src/AuthService.cpp
src/KafkaManager.h src/KafkaManager.cpp
src/StorageService.cpp src/StorageService.h
src/Utils.cpp src/Utils.h
src/storage_setup.cpp
src/storage_tables.cpp src/SMTPMailerService.cpp src/SMTPMailerService.h
src/RESTAPI_users_handler.cpp src/RESTAPI_users_handler.h
src/RESTAPI_user_handler.cpp src/RESTAPI_user_handler.h
src/RESTAPI_action_links.cpp src/RESTAPI_action_links.h src/storage_users.cpp
src/RESTAPI_InternalServer.cpp src/RESTAPI_InternalServer.h
src/RESTAPI_validateToken_handler.cpp src/RESTAPI_validateToken_handler.h
src/RESTAPI_systemEndpoints_handler.cpp src/RESTAPI_systemEndpoints_handler.h
src/RESTAPI_AssetServer.cpp src/RESTAPI_AssetServer.h
src/RESTAPI_avatarHandler.cpp src/RESTAPI_avatarHandler.h
src/storage_avatar.cpp src/storage_avatar.h src/storage_users.h
src/OpenWifiTypes.h src/RESTAPI_email_handler.cpp src/RESTAPI_email_handler.h
src/storage_tokens.cpp
src/RESTAPI_GenericServer.h src/RESTAPI_GenericServer.cpp
src/RESTAPI_errors.h
src/Storage.h
src/SMSSender.cpp src/SMSSender.h src/RESTAPI_sms_handler.cpp src/RESTAPI_sms_handler.h src/MFAServer.cpp src/MFAServer.h src/SMS_provider_aws.cpp src/SMS_provider_aws.h src/SMS_provider.cpp src/SMS_provider.h src/SMS_provider_twilio.cpp src/SMS_provider_twilio.h)
build
src/Daemon.h src/Daemon.cpp
src/framework/MicroService.cpp src/framework/MicroService.h
src/framework/SubSystemServer.cpp src/framework/SubSystemServer.h
src/RESTAPI/RESTAPI_oauth2Handler.h src/RESTAPI/RESTAPI_oauth2Handler.cpp
src/framework/RESTAPI_handler.h src/framework/RESTAPI_handler.cpp
src/RESTAPI/RESTAPI_server.cpp src/RESTAPI/RESTAPI_server.h
src/RESTAPI/RESTAPI_SecurityObjects.cpp src/RESTAPI/RESTAPI_SecurityObjects.h
src/framework/RESTAPI_system_command.h src/framework/RESTAPI_system_command.cpp
src/AuthService.h src/AuthService.cpp
src/framework/KafkaManager.h src/framework/KafkaManager.cpp
src/StorageService.cpp src/StorageService.h
src/framework/Utils.cpp src/framework/Utils.h
src/SMTPMailerService.cpp src/SMTPMailerService.h
src/RESTAPI/RESTAPI_users_handler.cpp src/RESTAPI/RESTAPI_users_handler.h
src/RESTAPI/RESTAPI_user_handler.cpp src/RESTAPI/RESTAPI_user_handler.h
src/RESTAPI/RESTAPI_action_links.cpp src/RESTAPI/RESTAPI_action_links.h
src/RESTAPI/RESTAPI_InternalServer.cpp src/RESTAPI/RESTAPI_InternalServer.h
src/RESTAPI/RESTAPI_validateToken_handler.cpp src/RESTAPI/RESTAPI_validateToken_handler.h
src/RESTAPI/RESTAPI_systemEndpoints_handler.cpp src/RESTAPI/RESTAPI_systemEndpoints_handler.h
src/RESTAPI/RESTAPI_AssetServer.cpp src/RESTAPI/RESTAPI_AssetServer.h
src/RESTAPI/RESTAPI_avatarHandler.cpp src/RESTAPI/RESTAPI_avatarHandler.h
src/storage/storage_avatar.cpp src/storage/storage_avatar.h src/storage/storage_users.h
src/RESTAPI/RESTAPI_email_handler.cpp src/RESTAPI/RESTAPI_email_handler.h
src/framework/RESTAPI_GenericServer.h src/framework/RESTAPI_GenericServer.cpp
src/framework/storage_setup.cpp
src/storage/storage_tables.cpp
src/framework/RESTAPI_protocol.h
src/framework/OpenWifiTypes.h
src/storage/storage_users.cpp
src/storage/storage_tokens.cpp
src/framework/RESTAPI_errors.h
src/framework/Storage.h
src/SMSSender.cpp src/SMSSender.h
src/RESTAPI/RESTAPI_sms_handler.cpp src/RESTAPI/RESTAPI_sms_handler.h
src/MFAServer.cpp src/MFAServer.h
src/SMS_provider_aws.cpp src/SMS_provider_aws.h
src/SMS_provider.cpp src/SMS_provider.h
src/SMS_provider_twilio.cpp src/SMS_provider_twilio.h)
if(NOT SMALL_BUILD)
target_link_libraries(owsec PUBLIC

2
build
View File

@@ -1 +1 @@
56
59

View File

@@ -13,12 +13,12 @@
#include "Poco/JWT/Signer.h"
#include "Daemon.h"
#include "RESTAPI_handler.h"
#include "framework/RESTAPI_handler.h"
#include "StorageService.h"
#include "AuthService.h"
#include "Utils.h"
#include "KafkaManager.h"
#include "Kafka_topics.h"
#include "framework/Utils.h"
#include "framework/KafkaManager.h"
#include "framework/Kafka_topics.h"
#include "SMTPMailerService.h"
#include "MFAServer.h"

View File

@@ -11,7 +11,7 @@
#include <regex>
#include "SubSystemServer.h"
#include "framework/SubSystemServer.h"
#include "Poco/JSON/Object.h"
#include "Poco/Net/HTTPServerRequest.h"
@@ -19,7 +19,7 @@
#include "Poco/JWT/Signer.h"
#include "Poco/SHA2Engine.h"
#include "RESTAPI_SecurityObjects.h"
#include "RESTAPI/RESTAPI_SecurityObjects.h"
namespace OpenWifi{

View File

@@ -26,12 +26,12 @@
#include <aws/s3/model/PutBucketAclRequest.h>
#include <aws/s3/model/GetBucketAclRequest.h>
#include "ALBHealthCheckServer.h"
#include "KafkaManager.h"
#include "framework/ALBHealthCheckServer.h"
#include "framework/KafkaManager.h"
#include "StorageService.h"
#include "RESTAPI_server.h"
#include "RESTAPI/RESTAPI_server.h"
#include "SMTPMailerService.h"
#include "RESTAPI_InternalServer.h"
#include "RESTAPI/RESTAPI_InternalServer.h"
#include "AuthService.h"
#include "SMSSender.h"

View File

@@ -21,8 +21,8 @@
#include "Poco/Crypto/Cipher.h"
#include "OpenWifiTypes.h"
#include "MicroService.h"
#include "framework/OpenWifiTypes.h"
#include "framework/MicroService.h"
namespace OpenWifi {

View File

@@ -5,9 +5,9 @@
#ifndef OWSEC_MFASERVER_H
#define OWSEC_MFASERVER_H
#include "SubSystemServer.h"
#include "framework/SubSystemServer.h"
#include "Poco/JSON/Object.h"
#include "RESTAPI_SecurityObjects.h"
#include "RESTAPI/RESTAPI_SecurityObjects.h"
namespace OpenWifi {
struct MFACacheEntry {

View File

@@ -4,10 +4,10 @@
#include "RESTAPI_AssetServer.h"
#include "Poco/File.h"
#include "Daemon.h"
#include "../Daemon.h"
#include "RESTAPI_server.h"
#include "Utils.h"
#include "RESTAPI_protocol.h"
#include "../framework/Utils.h"
#include "../framework/RESTAPI_protocol.h"
namespace OpenWifi {
void RESTAPI_AssetServer::DoGet() {

View File

@@ -5,7 +5,7 @@
#ifndef UCENTRALSEC_RESTAPI_ASSETSERVER_H
#define UCENTRALSEC_RESTAPI_ASSETSERVER_H
#include "RESTAPI_handler.h"
#include "../framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_AssetServer : public RESTAPIHandler {

View File

@@ -5,7 +5,7 @@
#include "Poco/URI.h"
#include "RESTAPI_system_command.h"
#include "../framework/RESTAPI_system_command.h"
#include "RESTAPI_user_handler.h"
#include "RESTAPI_users_handler.h"
#include "RESTAPI_action_links.h"
@@ -13,7 +13,7 @@
#include "RESTAPI_InternalServer.h"
#include "RESTAPI_sms_handler.h"
#include "Utils.h"
#include "../framework/Utils.h"
namespace OpenWifi {

View File

@@ -5,13 +5,13 @@
#ifndef UCENTRALSEC_RESTAPI_INTERNALSERVER_H
#define UCENTRALSEC_RESTAPI_INTERNALSERVER_H
#include "SubSystemServer.h"
#include "../framework/SubSystemServer.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/NetException.h"
#include "RESTAPI_GenericServer.h"
#include "../framework/RESTAPI_GenericServer.h"
namespace OpenWifi {

View File

@@ -10,7 +10,7 @@
#include "Poco/JSON/Stringifier.h"
#include "RESTAPI_SecurityObjects.h"
#include "RESTAPI_utils.h"
#include "../framework/RESTAPI_utils.h"
using OpenWifi::RESTAPI_utils::field_to_json;
using OpenWifi::RESTAPI_utils::field_from_json;

View File

@@ -10,7 +10,7 @@
#define UCENTRAL_RESTAPI_SECURITYOBJECTS_H
#include "Poco/JSON/Object.h"
#include "OpenWifiTypes.h"
#include "../framework/OpenWifiTypes.h"
namespace OpenWifi::SecurityObjects {

View File

@@ -3,14 +3,14 @@
//
#include "RESTAPI_action_links.h"
#include "StorageService.h"
#include "Utils.h"
#include "RESTAPI_utils.h"
#include "../StorageService.h"
#include "../framework/Utils.h"
#include "../framework/RESTAPI_utils.h"
#include "Poco/JSON/Parser.h"
#include "Poco/Net/HTMLForm.h"
#include "RESTAPI_server.h"
#include "Daemon.h"
#include "../Daemon.h"
namespace OpenWifi {
void RESTAPI_action_links::DoGet() {

View File

@@ -6,7 +6,7 @@
#define UCENTRALSEC_RESTAPI_ACTION_LINKS_H
#include "RESTAPI_handler.h"
#include "../framework/RESTAPI_handler.h"
#include "Poco/Net/PartHandler.h"
#include "Poco/Message.h"
#include "Poco/Net/MessageHeader.h"

View File

@@ -6,11 +6,11 @@
#include <iostream>
#include "RESTAPI_avatarHandler.h"
#include "StorageService.h"
#include "Daemon.h"
#include "../StorageService.h"
#include "../Daemon.h"
#include "Poco/Net/HTMLForm.h"
#include "Utils.h"
#include "RESTAPI_protocol.h"
#include "../framework/Utils.h"
#include "../framework/RESTAPI_protocol.h"
namespace OpenWifi {

View File

@@ -6,7 +6,7 @@
#define UCENTRALSEC_RESTAPI_AVATARHANDLER_H
#include "RESTAPI_handler.h"
#include "framework/RESTAPI_handler.h"
namespace OpenWifi {

View File

@@ -8,9 +8,9 @@
#include "Poco/Exception.h"
#include "Poco/JSON/Parser.h"
#include "Daemon.h"
#include "SMTPMailerService.h"
#include "RESTAPI_errors.h"
#include "../Daemon.h"
#include "../SMTPMailerService.h"
#include "../framework/RESTAPI_errors.h"
namespace OpenWifi {
void RESTAPI_email_handler::DoPost() {

View File

@@ -6,7 +6,7 @@
#define OWSEC_RESTAPI_EMAIL_HANDLER_H
#include "RESTAPI_handler.h"
#include "framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_email_handler : public RESTAPIHandler {

View File

@@ -8,13 +8,13 @@
#include "Poco/JSON/Parser.h"
#include "AuthService.h"
#include "../AuthService.h"
#include "RESTAPI_oauth2Handler.h"
#include "RESTAPI_protocol.h"
#include "../framework/RESTAPI_protocol.h"
#include "RESTAPI_server.h"
#include "MFAServer.h"
#include "../MFAServer.h"
#include "Utils.h"
#include "../framework/Utils.h"
namespace OpenWifi {
void RESTAPI_oauth2Handler::DoGet() {

View File

@@ -9,7 +9,7 @@
#ifndef UCENTRAL_RESTAPI_OAUTH2HANDLER_H
#define UCENTRAL_RESTAPI_OAUTH2HANDLER_H
#include "RESTAPI_handler.h"
#include "../framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_oauth2Handler : public RESTAPIHandler {

View File

@@ -12,7 +12,7 @@
#include "RESTAPI_server.h"
#include "RESTAPI_oauth2Handler.h"
#include "RESTAPI_system_command.h"
#include "../framework/RESTAPI_system_command.h"
#include "RESTAPI_user_handler.h"
#include "RESTAPI_users_handler.h"
#include "RESTAPI_action_links.h"
@@ -22,8 +22,8 @@
#include "RESTAPI_email_handler.h"
#include "RESTAPI_sms_handler.h"
#include "Daemon.h"
#include "Utils.h"
#include "../Daemon.h"
#include "../framework/Utils.h"
namespace OpenWifi {

View File

@@ -9,13 +9,13 @@
#ifndef UCENTRAL_UCENTRALRESTAPISERVER_H
#define UCENTRAL_UCENTRALRESTAPISERVER_H
#include "SubSystemServer.h"
#include "../framework/SubSystemServer.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/NetException.h"
#include "RESTAPI_GenericServer.h"
#include "../framework/RESTAPI_GenericServer.h"
namespace OpenWifi {

View File

@@ -3,9 +3,9 @@
//
#include "RESTAPI_sms_handler.h"
#include "SMSSender.h"
#include "Utils.h"
#include "RESTAPI_errors.h"
#include "../SMSSender.h"
#include "../framework/Utils.h"
#include "../framework/RESTAPI_errors.h"
namespace OpenWifi {

View File

@@ -6,7 +6,7 @@
#define OWSEC_RESTAPI_SMS_HANDLER_H
#include "RESTAPI_handler.h"
#include "../framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_sms_handler : public RESTAPIHandler {

View File

@@ -3,7 +3,7 @@
//
#include "RESTAPI_systemEndpoints_handler.h"
#include "Daemon.h"
#include "../Daemon.h"
#include "RESTAPI_SecurityObjects.h"
namespace OpenWifi {

View File

@@ -5,7 +5,7 @@
#ifndef UCENTRALSEC_RESTAPI_SYSTEMENDPOINTS_HANDLER_H
#define UCENTRALSEC_RESTAPI_SYSTEMENDPOINTS_HANDLER_H
#include "RESTAPI_handler.h"
#include "../framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_systemEndpoints_handler : public RESTAPIHandler {
public:

View File

@@ -3,12 +3,12 @@
//
#include "RESTAPI_user_handler.h"
#include "StorageService.h"
#include "../StorageService.h"
#include "Poco/JSON/Parser.h"
#include "Utils.h"
#include "RESTAPI_utils.h"
#include "RESTAPI_errors.h"
#include "SMSSender.h"
#include "../framework/Utils.h"
#include "../framework/RESTAPI_utils.h"
#include "../framework/RESTAPI_errors.h"
#include "../SMSSender.h"
namespace OpenWifi {
void RESTAPI_user_handler::DoGet() {

View File

@@ -5,7 +5,7 @@
#ifndef UCENTRALSEC_RESTAPI_USER_HANDLER_H
#define UCENTRALSEC_RESTAPI_USER_HANDLER_H
#include "RESTAPI_handler.h"
#include "framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_user_handler : public RESTAPIHandler {

View File

@@ -3,9 +3,9 @@
//
#include "RESTAPI_users_handler.h"
#include "StorageService.h"
#include "RESTAPI_protocol.h"
#include "Utils.h"
#include "../StorageService.h"
#include "../framework/RESTAPI_protocol.h"
#include "../framework/Utils.h"
namespace OpenWifi {
void RESTAPI_users_handler::DoGet() {

View File

@@ -5,7 +5,7 @@
#ifndef UCENTRALSEC_RESTAPI_USERS_HANDLER_H
#define UCENTRALSEC_RESTAPI_USERS_HANDLER_H
#include "RESTAPI_handler.h"
#include "framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_users_handler : public RESTAPIHandler {

View File

@@ -3,9 +3,9 @@
//
#include "RESTAPI_validateToken_handler.h"
#include "Daemon.h"
#include "AuthService.h"
#include "Utils.h"
#include "../Daemon.h"
#include "../AuthService.h"
#include "../framework/Utils.h"
namespace OpenWifi {
void RESTAPI_validateToken_handler::DoGet() {

View File

@@ -5,7 +5,7 @@
#ifndef UCENTRALSEC_RESTAPI_VALIDATETOKEN_HANDLER_H
#define UCENTRALSEC_RESTAPI_VALIDATETOKEN_HANDLER_H
#include "RESTAPI_handler.h"
#include "../framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_validateToken_handler : public RESTAPIHandler {

View File

@@ -5,7 +5,7 @@
#ifndef OWSEC_SMSSENDER_H
#define OWSEC_SMSSENDER_H
#include "SubSystemServer.h"
#include "framework/SubSystemServer.h"
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/core/auth/AWSCredentials.h>

View File

@@ -17,7 +17,7 @@
#include "Poco/Net/AcceptCertificateHandler.h"
#include "SMTPMailerService.h"
#include "Utils.h"
#include "framework/Utils.h"
#include "Daemon.h"
namespace OpenWifi {

View File

@@ -5,7 +5,7 @@
#ifndef UCENTRALSEC_SMTPMAILERSERVICE_H
#define UCENTRALSEC_SMTPMAILERSERVICE_H
#include "SubSystemServer.h"
#include "framework/SubSystemServer.h"
#include "Poco/File.h"
#include "Poco/Net/InvalidCertificateHandler.h"

View File

@@ -9,7 +9,7 @@
#include "StorageService.h"
#include "Daemon.h"
#include "Poco/Util/Application.h"
#include "Utils.h"
#include "framework/Utils.h"
namespace OpenWifi {

View File

@@ -21,9 +21,9 @@
#endif
#include "AuthService.h"
#include "RESTAPI_SecurityObjects.h"
#include "SubSystemServer.h"
#include "Storage.h"
#include "RESTAPI/RESTAPI_SecurityObjects.h"
#include "framework/SubSystemServer.h"
#include "framework/Storage.h"
namespace OpenWifi {

View File

@@ -21,7 +21,7 @@
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Logger.h"
#include "Daemon.h"
#include "../Daemon.h"
#include "SubSystemServer.h"
namespace OpenWifi {

View File

@@ -9,8 +9,8 @@
#include <utility>
#include "AuthClient.h"
#include "RESTAPI_SecurityObjects.h"
#include "Daemon.h"
#include "../RESTAPI/RESTAPI_SecurityObjects.h"
#include "../Daemon.h"
#include "OpenAPIRequest.h"
namespace OpenWifi {

View File

@@ -10,7 +10,7 @@
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/JWT/Signer.h"
#include "Poco/SHA2Engine.h"
#include "RESTAPI_SecurityObjects.h"
#include "../RESTAPI/RESTAPI_SecurityObjects.h"
#include "SubSystemServer.h"
namespace OpenWifi {

147
src/framework/CIDRUtils.cpp Normal file
View File

@@ -0,0 +1,147 @@
//
// Created by stephane bourque on 2021-09-29.
//
#include "CIDRUtils.h"
#include "framework/OpenWifiTypes.h"
namespace OpenWifi::CIDR {
static bool cidr_match(const in_addr &addr, const in_addr &net, uint8_t bits) {
if (bits == 0) {
return true;
}
return !((addr.s_addr ^ net.s_addr) & htonl(0xFFFFFFFFu << (32 - bits)));
}
static bool cidr6_match(const in6_addr &address, const in6_addr &network, uint8_t bits) {
#ifdef __linux__
const uint32_t *a = address.s6_addr32;
const uint32_t *n = network.s6_addr32;
#else
const uint32_t *a = address.__u6_addr.__u6_addr32;
const uint32_t *n = network.__u6_addr.__u6_addr32;
#endif
int bits_whole, bits_incomplete;
bits_whole = bits >> 5; // number of whole u32
bits_incomplete = bits & 0x1F; // number of bits in incomplete u32
if (bits_whole) {
if (memcmp(a, n, bits_whole << 2)!=0) {
return false;
}
}
if (bits_incomplete) {
uint32_t mask = htonl((0xFFFFFFFFu) << (32 - bits_incomplete));
if ((a[bits_whole] ^ n[bits_whole]) & mask) {
return false;
}
}
return true;
}
static bool ConvertStringToLong(const char *S, unsigned long &L) {
char *end;
L = std::strtol(S,&end,10);
return end != S;
}
static bool CidrIPinRange(const Poco::Net::IPAddress &IP, const std::string &Range) {
Poco::StringTokenizer TimeTokens(Range,"/",Poco::StringTokenizer::TOK_TRIM);
Poco::Net::IPAddress RangeIP;
if(Poco::Net::IPAddress::tryParse(TimeTokens[0],RangeIP)) {
if(TimeTokens.count()==2) {
if (RangeIP.family() == Poco::Net::IPAddress::IPv4) {
unsigned long MaskLength;
if (ConvertStringToLong(TimeTokens[1].c_str(), MaskLength)) {
return cidr_match(*static_cast<const in_addr *>(RangeIP.addr()),
*static_cast<const in_addr *>(IP.addr()), MaskLength);
}
} else if (RangeIP.family() == Poco::Net::IPAddress::IPv6) {
unsigned long MaskLength;
if (ConvertStringToLong(TimeTokens[1].c_str(), MaskLength)) {
return cidr6_match(*static_cast<const in6_addr *>(RangeIP.addr()),
*static_cast<const in6_addr *>(IP.addr()), MaskLength);
}
}
}
return false;
}
return false;
}
//
// Ranges can be a single IP, of IP1-IP2, of A set of IPs: IP1,IP2,IP3, or a cidr IP/24
// These can work for IPv6 too...
//
static bool ValidateRange(const std::string &R) {
auto Tokens = Poco::StringTokenizer(R,"-");
if(Tokens.count()==2) {
Poco::Net::IPAddress a,b;
if(!Poco::Net::IPAddress::tryParse(Tokens[0],a) && Poco::Net::IPAddress::tryParse(Tokens[1],b))
return false;
return a.family() == b.family();
}
Tokens = Poco::StringTokenizer(R,",");
if(Tokens.count()>1) {
return std::all_of(Tokens.begin(), Tokens.end(), [](const std::string &A) {
Poco::Net::IPAddress a;
return Poco::Net::IPAddress::tryParse(A,a);
} );
}
Tokens = Poco::StringTokenizer(R,"/");
if(Tokens.count()==2) {
Poco::Net::IPAddress a;
if(!Poco::Net::IPAddress::tryParse(Tokens[0],a))
return false;
if(std::atoi(Tokens[1].c_str())==0)
return false;
return true;
}
Poco::Net::IPAddress a;
return Poco::Net::IPAddress::tryParse(R,a);
}
static bool IpInRange(const Poco::Net::IPAddress & target, const std::string & R) {
auto Tokens = Poco::StringTokenizer(R,"-");
if(Tokens.count()==2) {
auto a = Poco::Net::IPAddress::parse(Tokens[0]);
auto b = Poco::Net::IPAddress::parse(Tokens[1]);
if(target.family() != a.family())
return false;
return (a<=target && b>=target);
}
Tokens = Poco::StringTokenizer(R,",");
if(Tokens.count()>1) {
return std::any_of(Tokens.begin(), Tokens.end(), [target](const std::string &Element) {
return Poco::Net::IPAddress::parse(Element) == target ; });
}
Tokens = Poco::StringTokenizer(R,"/");
if(Tokens.count()==2) {
return CidrIPinRange(target,R);
}
return Poco::Net::IPAddress::parse(R)==target;
}
bool IpInRanges(const std::string &IP, const Types::StringVec &R) {
Poco::Net::IPAddress Target;
if(!Poco::Net::IPAddress::tryParse(IP,Target))
return false;
return std::any_of(cbegin(R),cend(R),[Target](const std::string &i) { return IpInRange(Target,i); });
}
bool ValidateIpRanges(const Types::StringVec & Ranges) {
return std::all_of(cbegin(Ranges), cend(Ranges), ValidateRange);
}
}

16
src/framework/CIDRUtils.h Normal file
View File

@@ -0,0 +1,16 @@
//
// Created by stephane bourque on 2021-09-29.
//
#ifndef OWPROV_CIDRUTILS_H
#define OWPROV_CIDRUTILS_H
#include "framework/OpenWifiTypes.h"
namespace OpenWifi::CIDR {
[[nodiscard]] bool IpInRanges(const std::string &IP, const Types::StringVec &R);
[[nodiscard]] bool ValidateIpRanges(const Types::StringVec & Ranges);
}
#endif //OWPROV_CIDRUTILS_H

View File

@@ -0,0 +1,273 @@
//
// Created by stephane bourque on 2021-10-08.
//
#ifndef OWPROV_COUNTRYCODES_H
#define OWPROV_COUNTRYCODES_H
#include <vector>
#include <string>
#include <utility>
namespace OpenWifi {
struct CountryInfo {
std::string code;
std::string name;
};
static const std::vector<CountryInfo> CountryCodes {
{ .code= "US", .name= "United States" },
{ .code= "GB", .name= "United Kingdom" },
{ .code= "CA", .name= "Canada" },
{ .code= "AF", .name= "Afghanistan" },
{ .code= "AX", .name= "Aland Islands" },
{ .code= "AL", .name= "Albania" },
{ .code= "DZ", .name= "Algeria" },
{ .code= "AS", .name= "American Samoa" },
{ .code= "AD", .name= "Andorra" },
{ .code= "AO", .name= "Angola" },
{ .code= "AI", .name= "Anguilla" },
{ .code= "AQ", .name= "Antarctica" },
{ .code= "AG", .name= "Antigua And Barbuda" },
{ .code= "AR", .name= "Argentina" },
{ .code= "AM", .name= "Armenia" },
{ .code= "AN", .name= "Netherlands Antilles" },
{ .code= "AW", .name= "Aruba" },
{ .code= "AU", .name= "Australia" },
{ .code= "AT", .name= "Austria" },
{ .code= "AZ", .name= "Azerbaijan" },
{ .code= "BS", .name= "Bahamas" },
{ .code= "BH", .name= "Bahrain" },
{ .code= "BD", .name= "Bangladesh" },
{ .code= "BB", .name= "Barbados" },
{ .code= "BY", .name= "Belarus" },
{ .code= "BE", .name= "Belgium" },
{ .code= "BZ", .name= "Belize" },
{ .code= "BJ", .name= "Benin" },
{ .code= "BM", .name= "Bermuda" },
{ .code= "BT", .name= "Bhutan" },
{ .code= "BO", .name= "Bolivia" },
{ .code= "BA", .name= "Bosnia And Herzegovina" },
{ .code= "BW", .name= "Botswana" },
{ .code= "BV", .name= "Bouvet Island" },
{ .code= "BR", .name= "Brazil" },
{ .code= "IO", .name= "British Indian Ocean Territory" },
{ .code= "BN", .name= "Brunei Darussalam" },
{ .code= "BG", .name= "Bulgaria" },
{ .code= "BF", .name= "Burkina Faso" },
{ .code= "BI", .name= "Burundi" },
{ .code= "KH", .name= "Cambodia" },
{ .code= "CM", .name= "Cameroon" },
{ .code= "CA", .name= "Canada" },
{ .code= "CV", .name= "Cape Verde" },
{ .code= "KY", .name= "Cayman Islands" },
{ .code= "CF", .name= "Central African Republic" },
{ .code= "TD", .name= "Chad" },
{ .code= "CL", .name= "Chile" },
{ .code= "CN", .name= "China" },
{ .code= "CX", .name= "Christmas Island" },
{ .code= "CC", .name= "Cocos (Keeling) Islands" },
{ .code= "CO", .name= "Colombia" },
{ .code= "KM", .name= "Comoros" },
{ .code= "CG", .name= "Congo" },
{ .code= "CD", .name= "Congo, Democratic Republic" },
{ .code= "CK", .name= "Cook Islands" },
{ .code= "CR", .name= "Costa Rica" },
{ .code= "CI", .name= "Cote D\"Ivoire" },
{ .code= "HR", .name= "Croatia" },
{ .code= "CU", .name= "Cuba" },
{ .code= "CY", .name= "Cyprus" },
{ .code= "CZ", .name= "Czech Republic" },
{ .code= "DK", .name= "Denmark" },
{ .code= "DJ", .name= "Djibouti" },
{ .code= "DM", .name= "Dominica" },
{ .code= "DO", .name= "Dominican Republic" },
{ .code= "EC", .name= "Ecuador" },
{ .code= "EG", .name= "Egypt" },
{ .code= "SV", .name= "El Salvador" },
{ .code= "GQ", .name= "Equatorial Guinea" },
{ .code= "ER", .name= "Eritrea" },
{ .code= "EE", .name= "Estonia" },
{ .code= "ET", .name= "Ethiopia" },
{ .code= "FK", .name= "Falkland Islands (Malvinas)" },
{ .code= "FO", .name= "Faroe Islands" },
{ .code= "FJ", .name= "Fiji" },
{ .code= "FI", .name= "Finland" },
{ .code= "FR", .name= "France" },
{ .code= "GF", .name= "French Guiana" },
{ .code= "PF", .name= "French Polynesia" },
{ .code= "TF", .name= "French Southern Territories" },
{ .code= "GA", .name= "Gabon" },
{ .code= "GM", .name= "Gambia" },
{ .code= "GE", .name= "Georgia" },
{ .code= "DE", .name= "Germany" },
{ .code= "GH", .name= "Ghana" },
{ .code= "GI", .name= "Gibraltar" },
{ .code= "GR", .name= "Greece" },
{ .code= "GL", .name= "Greenland" },
{ .code= "GD", .name= "Grenada" },
{ .code= "GP", .name= "Guadeloupe" },
{ .code= "GU", .name= "Guam" },
{ .code= "GT", .name= "Guatemala" },
{ .code= "GG", .name= "Guernsey" },
{ .code= "GN", .name= "Guinea" },
{ .code= "GW", .name= "Guinea-Bissau" },
{ .code= "GY", .name= "Guyana" },
{ .code= "HT", .name= "Haiti" },
{ .code= "HM", .name= "Heard Island & Mcdonald Islands" },
{ .code= "VA", .name= "Holy See (Vatican City State)" },
{ .code= "HN", .name= "Honduras" },
{ .code= "HK", .name= "Hong Kong" },
{ .code= "HU", .name= "Hungary" },
{ .code= "IS", .name= "Iceland" },
{ .code= "IN", .name= "India" },
{ .code= "ID", .name= "Indonesia" },
{ .code= "IR", .name= "Iran, Islamic Republic Of" },
{ .code= "IQ", .name= "Iraq" },
{ .code= "IE", .name= "Ireland" },
{ .code= "IM", .name= "Isle Of Man" },
{ .code= "IL", .name= "Israel" },
{ .code= "IT", .name= "Italy" },
{ .code= "JM", .name= "Jamaica" },
{ .code= "JP", .name= "Japan" },
{ .code= "JE", .name= "Jersey" },
{ .code= "JO", .name= "Jordan" },
{ .code= "KZ", .name= "Kazakhstan" },
{ .code= "KE", .name= "Kenya" },
{ .code= "KI", .name= "Kiribati" },
{ .code= "KR", .name= "Korea" },
{ .code= "KW", .name= "Kuwait" },
{ .code= "KG", .name= "Kyrgyzstan" },
{ .code= "LA", .name= "Lao People\"s Democratic Republic" },
{ .code= "LV", .name= "Latvia" },
{ .code= "LB", .name= "Lebanon" },
{ .code= "LS", .name= "Lesotho" },
{ .code= "LR", .name= "Liberia" },
{ .code= "LY", .name= "Libyan Arab Jamahiriya" },
{ .code= "LI", .name= "Liechtenstein" },
{ .code= "LT", .name= "Lithuania" },
{ .code= "LU", .name= "Luxembourg" },
{ .code= "MO", .name= "Macao" },
{ .code= "MK", .name= "Macedonia" },
{ .code= "MG", .name= "Madagascar" },
{ .code= "MW", .name= "Malawi" },
{ .code= "MY", .name= "Malaysia" },
{ .code= "MV", .name= "Maldives" },
{ .code= "ML", .name= "Mali" },
{ .code= "MT", .name= "Malta" },
{ .code= "MH", .name= "Marshall Islands" },
{ .code= "MQ", .name= "Martinique" },
{ .code= "MR", .name= "Mauritania" },
{ .code= "MU", .name= "Mauritius" },
{ .code= "YT", .name= "Mayotte" },
{ .code= "MX", .name= "Mexico" },
{ .code= "FM", .name= "Micronesia, Federated States Of" },
{ .code= "MD", .name= "Moldova" },
{ .code= "MC", .name= "Monaco" },
{ .code= "MN", .name= "Mongolia" },
{ .code= "ME", .name= "Montenegro" },
{ .code= "MS", .name= "Montserrat" },
{ .code= "MA", .name= "Morocco" },
{ .code= "MZ", .name= "Mozambique" },
{ .code= "MM", .name= "Myanmar" },
{ .code= "NA", .name= "Namibia" },
{ .code= "NR", .name= "Nauru" },
{ .code= "NP", .name= "Nepal" },
{ .code= "NL", .name= "Netherlands" },
{ .code= "AN", .name= "Netherlands Antilles" },
{ .code= "NC", .name= "New Caledonia" },
{ .code= "NZ", .name= "New Zealand" },
{ .code= "NI", .name= "Nicaragua" },
{ .code= "NE", .name= "Niger" },
{ .code= "NG", .name= "Nigeria" },
{ .code= "NU", .name= "Niue" },
{ .code= "NF", .name= "Norfolk Island" },
{ .code= "MP", .name= "Northern Mariana Islands" },
{ .code= "NO", .name= "Norway" },
{ .code= "OM", .name= "Oman" },
{ .code= "PK", .name= "Pakistan" },
{ .code= "PW", .name= "Palau" },
{ .code= "PS", .name= "Palestinian Territory, Occupied" },
{ .code= "PA", .name= "Panama" },
{ .code= "PG", .name= "Papua New Guinea" },
{ .code= "PY", .name= "Paraguay" },
{ .code= "PE", .name= "Peru" },
{ .code= "PH", .name= "Philippines" },
{ .code= "PN", .name= "Pitcairn" },
{ .code= "PL", .name= "Poland" },
{ .code= "PT", .name= "Portugal" },
{ .code= "PR", .name= "Puerto Rico" },
{ .code= "QA", .name= "Qatar" },
{ .code= "RE", .name= "Reunion" },
{ .code= "RO", .name= "Romania" },
{ .code= "RU", .name= "Russian Federation" },
{ .code= "RW", .name= "Rwanda" },
{ .code= "BL", .name= "Saint Barthelemy" },
{ .code= "SH", .name= "Saint Helena" },
{ .code= "KN", .name= "Saint Kitts And Nevis" },
{ .code= "LC", .name= "Saint Lucia" },
{ .code= "MF", .name= "Saint Martin" },
{ .code= "PM", .name= "Saint Pierre And Miquelon" },
{ .code= "VC", .name= "Saint Vincent And Grenadines" },
{ .code= "WS", .name= "Samoa" },
{ .code= "SM", .name= "San Marino" },
{ .code= "ST", .name= "Sao Tome And Principe" },
{ .code= "SA", .name= "Saudi Arabia" },
{ .code= "SN", .name= "Senegal" },
{ .code= "RS", .name= "Serbia" },
{ .code= "SC", .name= "Seychelles" },
{ .code= "SL", .name= "Sierra Leone" },
{ .code= "SG", .name= "Singapore" },
{ .code= "SK", .name= "Slovakia" },
{ .code= "SI", .name= "Slovenia" },
{ .code= "SB", .name= "Solomon Islands" },
{ .code= "SO", .name= "Somalia" },
{ .code= "ZA", .name= "South Africa" },
{ .code= "GS", .name= "South Georgia And Sandwich Isl." },
{ .code= "ES", .name= "Spain" },
{ .code= "LK", .name= "Sri Lanka" },
{ .code= "SD", .name= "Sudan" },
{ .code= "SR", .name= "Suriname" },
{ .code= "SJ", .name= "Svalbard And Jan Mayen" },
{ .code= "SZ", .name= "Swaziland" },
{ .code= "SE", .name= "Sweden" },
{ .code= "CH", .name= "Switzerland" },
{ .code= "SY", .name= "Syrian Arab Republic" },
{ .code= "TW", .name= "Taiwan" },
{ .code= "TJ", .name= "Tajikistan" },
{ .code= "TZ", .name= "Tanzania" },
{ .code= "TH", .name= "Thailand" },
{ .code= "TL", .name= "Timor-Leste" },
{ .code= "TG", .name= "Togo" },
{ .code= "TK", .name= "Tokelau" },
{ .code= "TO", .name= "Tonga" },
{ .code= "TT", .name= "Trinidad And Tobago" },
{ .code= "TN", .name= "Tunisia" },
{ .code= "TR", .name= "Turkey" },
{ .code= "TM", .name= "Turkmenistan" },
{ .code= "TC", .name= "Turks And Caicos Islands" },
{ .code= "TV", .name= "Tuvalu" },
{ .code= "UG", .name= "Uganda" },
{ .code= "UA", .name= "Ukraine" },
{ .code= "AE", .name= "United Arab Emirates" },
{ .code= "GB", .name= "United Kingdom" },
{ .code= "US", .name= "United States" },
{ .code= "UM", .name= "United States Outlying Islands" },
{ .code= "UY", .name= "Uruguay" },
{ .code= "UZ", .name= "Uzbekistan" },
{ .code= "VU", .name= "Vanuatu" },
{ .code= "VE", .name= "Venezuela" },
{ .code= "VN", .name= "Viet Nam" },
{ .code= "VG", .name= "Virgin Islands, British" },
{ .code= "VI", .name= "Virgin Islands, U.S." },
{ .code= "WF", .name= "Wallis And Futuna" },
{ .code= "EH", .name= "Western Sahara" },
{ .code= "YE", .name= "Yemen" },
{ .code= "ZM", .name= "Zambia" },
{ .code= "ZW", .name= "Zimbabwe" }
};
}
#endif //OWPROV_COUNTRYCODES_H

View File

@@ -9,7 +9,7 @@
#include "KafkaManager.h"
#include "Daemon.h"
#include "../Daemon.h"
#include "Utils.h"
namespace OpenWifi {

View File

@@ -13,7 +13,7 @@
#include <thread>
#include "SubSystemServer.h"
#include "OpenWifiTypes.h"
#include "framework/OpenWifiTypes.h"
#include "cppkafka/cppkafka.h"

View File

@@ -18,7 +18,7 @@
#include <Poco/URI.h>
#include <Poco/Exception.h>
#include "Utils.h"
#include "Daemon.h"
#include "../Daemon.h"
namespace OpenWifi {

View File

@@ -8,7 +8,7 @@
#include <vector>
#include <string>
#include "Daemon.h"
#include "../Daemon.h"
#include "Poco/StringTokenizer.h"
#include "Poco/Net/HTTPRequest.h"

View File

@@ -20,7 +20,7 @@
#include "RESTAPI_errors.h"
#ifdef TIP_SECURITY_SERVICE
#include "AuthService.h"
#include "../AuthService.h"
#else
#include "AuthClient.h"
#endif
@@ -28,7 +28,7 @@
#include "RESTAPI_handler.h"
#include "RESTAPI_protocol.h"
#include "Utils.h"
#include "Daemon.h"
#include "../Daemon.h"
namespace OpenWifi {

View File

@@ -24,7 +24,7 @@
#include "Poco/CountingStream.h"
#include "Poco/NullStream.h"
#include "RESTAPI_SecurityObjects.h"
#include "../RESTAPI/RESTAPI_SecurityObjects.h"
#include "RESTAPI_utils.h"
#include "RESTAPI_GenericServer.h"

View File

@@ -12,7 +12,7 @@
#include "Poco/DateTime.h"
#include "Poco/DateTimeFormat.h"
#include "Daemon.h"
#include "../Daemon.h"
#include "RESTAPI_protocol.h"
#include "RESTAPI_errors.h"
#include <thread>

View File

@@ -7,7 +7,7 @@
//
#include "SubSystemServer.h"
#include "Daemon.h"
#include "../Daemon.h"
#include "Poco/Net/X509Certificate.h"
#include "Poco/DateTimeFormatter.h"
@@ -17,7 +17,7 @@
#include "openssl/ssl.h"
#include "Daemon.h"
#include "../Daemon.h"
namespace OpenWifi {
SubSystemServer::SubSystemServer(std::string Name, const std::string &LoggingPrefix,

View File

@@ -29,7 +29,7 @@
#include "Poco/Net/HTTPResponse.h"
#include "uCentralProtocol.h"
#include "Daemon.h"
#include "../Daemon.h"
namespace OpenWifi::Utils {

View File

@@ -18,7 +18,7 @@
#include "Poco/Net/IPAddress.h"
#include "Poco/String.h"
#include "Poco/File.h"
#include "OpenWifiTypes.h"
#include "framework/OpenWifiTypes.h"
#define DBGLINE { std::cout << __FILE__ << ":" << __func__ << ":" << __LINE__ << std::endl; };

758
src/framework/orm.h Normal file
View File

@@ -0,0 +1,758 @@
//
// 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.
//
#ifndef __OPENWIFI_ORM_H__
#define __OPENWIFI_ORM_H__
#include <string>
#include <memory>
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <array>
#include "Poco/Tuple.h"
#include "Poco/Data/SessionPool.h"
#include "Poco/Data/Statement.h"
#include "Poco/Data/RecordSet.h"
#include "Poco/Data/SQLite/Connector.h"
#include "Poco/Logger.h"
#include "Poco/StringTokenizer.h"
#include "Storage.h"
namespace ORM {
enum FieldType {
FT_INT,
FT_BIGINT,
FT_TEXT,
FT_VARCHAR,
FT_BLOB
};
enum Indextype {
ASC,
DESC
};
struct Field {
std::string Name;
FieldType Type;
int Size=0;
bool Index=false;
Field(std::string N, FieldType T, int S=0, bool Index=false) :
Name(std::move(N)),
Type(T),
Size(S),
Index(Index) {}
explicit Field(std::string N) :
Name(std::move(N))
{
Type = FT_TEXT;
}
Field(std::string N, int S) :
Name(std::move(N)), Size(S)
{
if(Size>0 && Size<255)
Type = FT_VARCHAR;
else
Type = FT_TEXT;
}
Field(std::string N, int S, bool I):
Name(std::move(N)), Size(S), Index(I)
{
if(Size>0 && Size<255)
Type = FT_VARCHAR;
else
Type = FT_TEXT;
}
};
typedef std::vector<Field> FieldVec;
struct IndexEntry {
std::string FieldName;
Indextype Type;
};
typedef std::vector<IndexEntry> IndexEntryVec;
struct Index {
std::string Name;
IndexEntryVec Entries;
};
typedef std::vector<Index> IndexVec;
inline std::string FieldTypeToChar(OpenWifi::DBType Type, FieldType T, int Size=0) {
switch(T) {
case FT_INT: return "INT";
case FT_BIGINT: return "BIGINT";
case FT_TEXT: return "TEXT";
case FT_VARCHAR:
if(Size)
return std::string("VARCHAR(") + std::to_string(Size) + std::string(")");
else
return "TEXT";
case FT_BLOB:
if(Type==OpenWifi::DBType::mysql)
return "LONGBLOB";
else if(Type==OpenWifi::DBType::pgsql)
return "BYTEA";
else if(Type==OpenWifi::DBType::sqlite)
return "BLOB";
default:
assert(false);
return "";
}
assert(false);
return "";
}
inline std::string Escape(const std::string &S) {
std::string R;
for(const auto &i:S)
if(i=='\'')
R += "''";
else
R += i;
return R;
}
enum SqlComparison { EQ = 0, NEQ, LT, LTE, GT, GTE };
enum SqlBinaryOp { AND = 0 , OR };
static const std::vector<std::string> BOPS{" and ", " or "};
static const std::vector<std::string> SQLCOMPS{"=","!=","<","<=",">",">="};
inline std::string to_string(uint64_t V) {
return std::to_string(V);
}
inline std::string to_string(int V) {
return std::to_string(V);
}
inline std::string to_string(bool V) {
return std::to_string(V);
}
inline std::string to_string(const std::string &S) {
return S;
}
inline std::string to_string(const char * S) {
return S;
}
template <typename RecordTuple, typename RecordType> class DB {
public:
DB( OpenWifi::DBType dbtype,
const char *TableName,
const FieldVec & Fields,
const IndexVec & Indexes,
Poco::Data::SessionPool & Pool,
Poco::Logger &L,
const char *Prefix):
Type(dbtype),
DBName(TableName),
Pool_(Pool),
Logger_(L),
Prefix_(Prefix)
{
bool first = true;
int Place=0;
assert( RecordTuple::length == Fields.size());
for(const auto &i:Fields) {
FieldNames_[i.Name] = Place;
if(!first) {
CreateFields_ += ", ";
SelectFields_ += ", ";
UpdateFields_ += ", ";
SelectList_ += ", ";
} else {
SelectList_ += "(";
}
CreateFields_ += i.Name + " " + FieldTypeToChar(Type, i.Type,i.Size) + (i.Index ? " unique primary key" : "");
SelectFields_ += i.Name ;
UpdateFields_ += i.Name + "=?";
SelectList_ += "?";
first = false;
Place++;
}
SelectList_ += ")";
if(!Indexes.empty()) {
if(Type==OpenWifi::DBType::sqlite || Type==OpenWifi::DBType::pgsql) {
for(const auto &j:Indexes) {
std::string IndexLine;
IndexLine = std::string("CREATE INDEX IF NOT EXISTS ") + j.Name + std::string(" ON ") + DBName + " (";
bool first_entry=true;
for(const auto &k:j.Entries) {
assert(FieldNames_.find(k.FieldName) != FieldNames_.end());
if(!first_entry) {
IndexLine += " , ";
}
first_entry = false;
IndexLine += k.FieldName + std::string(" ") + std::string(k.Type == Indextype::ASC ? "ASC" : "DESC") ;
}
IndexLine += " );";
IndexCreation += IndexLine;
}
} else if(Type==OpenWifi::DBType::mysql) {
bool firstIndex = true;
std::string IndexLine;
for(const auto &j:Indexes) {
if(!firstIndex)
IndexLine += ", ";
firstIndex = false;
IndexLine += " INDEX " + j.Name + " ( " ;
bool first_entry=true;
for(const auto &k:j.Entries) {
assert(FieldNames_.find(k.FieldName) != FieldNames_.end());
if(!first_entry) {
IndexLine += " ,";
}
first_entry = false;
IndexLine += k.FieldName + std::string(k.Type == Indextype::ASC ? " ASC" : " DESC");
}
IndexLine += " ) ";
}
IndexCreation = IndexLine;
}
}
}
[[nodiscard]] const std::string & CreateFields() const { return CreateFields_; };
[[nodiscard]] const std::string & SelectFields() const { return SelectFields_; };
[[nodiscard]] const std::string & SelectList() const { return SelectList_; };
[[nodiscard]] const std::string & UpdateFields() const { return UpdateFields_; };
inline std::string OP(const char *F, SqlComparison O , int V) {
assert( FieldNames_.find(F) != FieldNames_.end() );
return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")" ;
}
inline std::string OP(const char *F, SqlComparison O , uint64_t V) {
assert( FieldNames_.find(F) != FieldNames_.end() );
return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")" ;
}
std::string OP(const char *F, SqlComparison O , const std::string & V) {
assert( FieldNames_.find(F) != FieldNames_.end() );
return std::string{"("} + F + SQLCOMPS[O] + "'" + Escape(V) + "')" ;
}
std::string OP(const char *F, SqlComparison O , const char * V) {
assert( FieldNames_.find(F) != FieldNames_.end() );
return std::string{"("} + F + SQLCOMPS[O] + "'" + Escape(V) + "')" ;
}
static std::string OP( const std::string &P1, SqlBinaryOp BOP , const std::string &P2) {
return std::string("(")+P1 + BOPS[BOP] + P2 +")";
}
std::string OP( bool Paran, const std::string &P1, SqlBinaryOp BOP , const std::string &P2) {
return P1 + BOPS[BOP] + P2 +")";
}
template <typename... Others> std::string OP( bool ParanOpen, const std::string &P1, SqlBinaryOp BOP , const std::string &P2, Others... More) {
return P1 + BOPS[BOP] + OP(ParanOpen, P2, More...) + ")";
}
template <typename... Others> std::string OP( const std::string &P1, SqlBinaryOp BOP , const std::string &P2, Others... More) {
return std::string{"("} + P1 + BOPS[BOP] + OP(true, P2, More...);
}
inline bool Create() {
std::string S;
if(Type==OpenWifi::DBType::mysql) {
if(IndexCreation.empty())
S = "create table if not exists " + DBName +" ( " + CreateFields_ + " )" ;
else
S = "create table if not exists " + DBName +" ( " + CreateFields_ + " ), " + IndexCreation + " )";
} else if (Type==OpenWifi::DBType::pgsql || Type==OpenWifi::DBType::sqlite) {
S = "create table if not exists " + DBName + " ( " + CreateFields_ + " ); " + IndexCreation ;
}
// std::cout << "CREATE-DB: " << S << std::endl;
try {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement CreateStatement(Session);
CreateStatement << S;
CreateStatement.execute();
return true;
} catch (const Poco::Exception &E) {
std::cout << "Exception while creating DB: " << E.name() << std::endl;
}
return false;
}
[[nodiscard]] std::string ConvertParams(const std::string & S) const {
if(Type!=OpenWifi::DBType::pgsql)
return S;
std::string R;
R.reserve(S.size()*2+1);
auto Idx=1;
for(auto const & i:S)
{
if(i=='?') {
R += '$';
R.append(std::to_string(Idx++));
} else {
R += i;
}
}
return R;
}
void Convert( RecordTuple &in , RecordType &out);
void Convert( RecordType &in , RecordTuple &out);
inline const std::string & Prefix() { return Prefix_; };
bool CreateRecord( RecordType & R) {
try {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Insert(Session);
RecordTuple RT;
Convert(R, RT);
std::string St = "insert into " + DBName + " ( " + SelectFields_ + " ) values " + SelectList_;
Insert << ConvertParams(St) ,
Poco::Data::Keywords::use(RT);
Insert.execute();
return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
template<typename T> bool GetRecord( const char * FieldName, T Value, RecordType & R) {
try {
assert( FieldNames_.find(FieldName) != FieldNames_.end() );
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Select(Session);
RecordTuple RT;
std::string St = "select " + SelectFields_ + " from " + DBName + " where " + FieldName + "=?" ;
Select << ConvertParams(St) ,
Poco::Data::Keywords::into(RT),
Poco::Data::Keywords::use(Value);
if(Select.execute()==1) {
Convert(RT,R);
return true;
}
return false;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
typedef std::vector<std::string> StringVec;
template < typename T,
typename T0, typename T1> bool GR(const char *FieldName, T & R,T0 &V0, T1 &V1) {
try {
assert( FieldNames_.find(FieldName) != FieldNames_.end() );
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Select(Session);
RecordTuple RT;
std::string St = "select " + SelectFields_ + " from " + DBName
+ " where " + FieldName[0] + "=? and " + FieldName[1] + "=?" ;
Select << ConvertParams(St) ,
Poco::Data::Keywords::into(RT),
Poco::Data::Keywords::use(V0),
Poco::Data::Keywords::use(V1);
if(Select.execute()==1) {
Convert(RT,R);
return true;
}
return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
typedef std::vector<RecordTuple> RecordList;
bool GetRecords( uint64_t Offset, uint64_t HowMany, std::vector<RecordType> & Records, const std::string & Where = "", const std::string & OrderBy = "") {
try {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Select(Session);
RecordList RL;
std::string St = "select " + SelectFields_ + " from " + DBName +
(Where.empty() ? "" : " where " + Where) + OrderBy +
ComputeRange(Offset, HowMany) ;
Select << St ,
Poco::Data::Keywords::into(RL);
if(Select.execute()>0) {
for(auto &i:RL) {
RecordType R;
Convert(i, R);
Records.template emplace_back(R);
}
return true;
}
return false;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
template <typename T> bool UpdateRecord( const char *FieldName, T & Value, RecordType & R) {
try {
assert( FieldNames_.find(FieldName) != FieldNames_.end() );
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Update(Session);
RecordTuple RT;
Convert(R, RT);
std::string St = "update " + DBName + " set " + UpdateFields_ + " where " + FieldName + "=?" ;
Update << ConvertParams(St) ,
Poco::Data::Keywords::use(RT),
Poco::Data::Keywords::use(Value);
Update.execute();
return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
template <typename T> bool GetNameAndDescription(const char *FieldName, T & Value, std::string & Name, std::string & Description ) {
try {
assert( FieldNames_.find(FieldName) != FieldNames_.end() );
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Select(Session);
RecordTuple RT;
std::string St = "select " + SelectFields_ + " from " + DBName + " where " + FieldName + "=?" ;
RecordType R;
Select << ConvertParams(St) ,
Poco::Data::Keywords::into(RT),
Poco::Data::Keywords::use(Value);
if(Select.execute()==1) {
Convert(RT,R);
Name = R.info.name;
Description = R.info.description;
return true;
}
return false;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
template <typename T> bool DeleteRecord( const char *FieldName, T Value) {
try {
assert( FieldNames_.find(FieldName) != FieldNames_.end() );
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Delete(Session);
std::string St = "delete from " + DBName + " where " + FieldName + "=?" ;
Delete << ConvertParams(St) ,
Poco::Data::Keywords::use(Value);
Delete.execute();
return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
bool DeleteRecords( const std::string & WhereClause ) {
try {
assert( !WhereClause.empty());
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Delete(Session);
std::string St = "delete from " + DBName + " where " + WhereClause;
Delete << St;
Delete.execute();
return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
bool Exists(const char *FieldName, std::string & Value) {
try {
assert( FieldNames_.find(FieldName) != FieldNames_.end() );
RecordType R;
if(GetRecord(FieldName,Value,R))
return true;
return false;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
bool Iterate( std::function<bool(const RecordType &R)> F) {
try {
uint64_t Offset=1;
uint64_t Batch=50;
bool Done=false;
while(!Done) {
std::vector<RecordType> Records;
if(GetRecords(Offset,Batch,Records)) {
for(const auto &i:Records) {
if(!F(i))
return true;
}
if(Records.size()<Batch)
return true;
Offset += Batch;
} else {
Done=true;
}
}
return true;
} catch(const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
bool PrepareOrderBy(const std::string &OrderByList, std::string &OrderByString) {
auto items = Poco::StringTokenizer(OrderByList,",");
std::string ItemList;
for(const auto &i:items) {
auto T = Poco::StringTokenizer(i,":");
if(T.count()!=2) {
return false;
}
if(T[1]!="a" && T[1]!="d") {
return false;
}
if(!ItemList.empty())
ItemList += " , ";
auto hint = FieldNames_.find(T[0]);
if(hint==FieldNames_.end()) {
return false;
}
ItemList += T[0] + (T[1]=="a" ? " ASC" : " DESC");
}
if(!ItemList.empty()) {
OrderByString = " ORDER BY " + ItemList;
}
std::cout << OrderByString << std::endl;
return true;
}
uint64_t Count( const std::string & Where="" ) {
try {
uint64_t Cnt=0;
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Select(Session);
std::string st{"SELECT COUNT(*) FROM " + DBName + " " + (Where.empty() ? "" : (" where " + Where)) };
Select << st ,
Poco::Data::Keywords::into(Cnt);
Select.execute();
return Cnt;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return 0;
}
template <typename X> bool ManipulateVectorMember( X T, const char *FieldName, std::string & ParentUUID, std::string & ChildUUID, bool Add) {
try {
assert( FieldNames_.find(FieldName) != FieldNames_.end() );
RecordType R;
if(GetRecord(FieldName, ParentUUID, R)) {
auto it = std::find((R.*T).begin(),(R.*T).end(),ChildUUID);
if(Add) {
if(it!=(R.*T).end() && *it == ChildUUID)
return false;
(R.*T).push_back(ChildUUID);
std::sort((R.*T).begin(),(R.*T).end());
} else {
if(it!=(R.*T).end() && *it == ChildUUID)
(R.*T).erase(it);
else
return false;
}
UpdateRecord(FieldName, ParentUUID, R);
return true;
}
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
inline bool AddChild( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteChild( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddLocation( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteLocation( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddContact( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteContact( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddVenue( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteVenue( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddDevice( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteDevice( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddEntity( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::entities, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteEntity( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::entities, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddUser( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::users, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DelUser( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::users, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddInUse(const char *FieldName, std::string & ParentUUID, const std::string & Prefix, const std::string & ChildUUID) {
std::string FakeUUID{ Prefix + ":" + ChildUUID};
return ManipulateVectorMember(&RecordType::inUse,FieldName, ParentUUID, FakeUUID, true);
}
inline bool DeleteInUse(const char *FieldName, std::string & ParentUUID, const std::string & Prefix, const std::string & ChildUUID) {
std::string FakeUUID{ Prefix + ":" + ChildUUID};
return ManipulateVectorMember(&RecordType::inUse,FieldName, ParentUUID, FakeUUID, false);
}
inline bool DeleteContact(const char *FieldName, std::string & ParentUUID, const std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::contacts,FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddContact(const char *FieldName, std::string & ParentUUID, const std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::contacts,FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteLocation(const char *FieldName, std::string & ParentUUID, const std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::locations,FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddLocation(const char *FieldName, std::string & ParentUUID, const std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::locations,FieldName, ParentUUID, ChildUUID, true);
}
inline bool GetInUse(const char *FieldName, std::string & UUID, std::vector<std::string> & UUIDs ) {
RecordType R;
if(GetRecord(FieldName,UUID,R)) {
UUIDs = R.inUse;
return true;
}
return false;
}
[[nodiscard]] inline std::string ComputeRange(uint64_t From, uint64_t HowMany) {
if(From<1) From=1;
switch(Type) {
case OpenWifi::DBType::sqlite:
return " LIMIT " + std::to_string(From-1) + ", " + std::to_string(HowMany) + " ";
case OpenWifi::DBType::pgsql:
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
case OpenWifi::DBType::mysql:
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
default:
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
}
}
Poco::Logger & Logger() { return Logger_; }
private:
OpenWifi::DBType Type;
std::string DBName;
std::string CreateFields_;
std::string SelectFields_;
std::string SelectList_;
std::string UpdateFields_;
std::string IndexCreation;
std::map<std::string,int> FieldNames_;
Poco::Data::SessionPool &Pool_;
Poco::Logger &Logger_;
std::string Prefix_;
};
}
#endif

View File

@@ -2,8 +2,8 @@
// Created by stephane bourque on 2021-08-22.
//
#include "StorageService.h"
#include "Daemon.h"
#include "../StorageService.h"
#include "../Daemon.h"
namespace OpenWifi {

View File

@@ -6,11 +6,11 @@
#include <fstream>
#include "storage_avatar.h"
#include "StorageService.h"
#include "../StorageService.h"
#include "Poco/File.h"
#include "Poco/Data/LOBStream.h"
#include "Daemon.h"
#include "../Daemon.h"
namespace OpenWifi {

View File

@@ -2,8 +2,8 @@
// Created by stephane bourque on 2021-06-13.
//
#include "StorageService.h"
#include "Utils.h"
#include "../StorageService.h"
#include "../framework/Utils.h"
#include "storage_users.h"
#include "storage_avatar.h"

View File

@@ -1,5 +1,5 @@
#include "StorageService.h"
#include "../StorageService.h"
namespace OpenWifi {

View File

@@ -2,9 +2,9 @@
// Created by stephane bourque on 2021-06-25.
//
#include "StorageService.h"
#include "RESTAPI_utils.h"
#include "Daemon.h"
#include "../StorageService.h"
#include "../framework/RESTAPI_utils.h"
#include "../Daemon.h"
#include "Poco/Tuple.h"
#include "storage_users.h"