UDP API port is now stored in and read from global config

This commit is contained in:
Thulinma 2023-05-01 02:09:21 +02:00
parent 40df03439c
commit 0af85de22d
13 changed files with 61 additions and 39 deletions

View file

@ -441,12 +441,22 @@ int Controller::handleAPIConnection(Socket::Connection &conn){
void Controller::handleUDPAPI(void *np){
Socket::UDPConnection uSock(true);
if (!uSock.bind(UDP_API_PORT, UDP_API_HOST)){
uint16_t boundPort = uSock.bind(UDP_API_PORT, UDP_API_HOST);
if (!boundPort){
FAIL_MSG("Could not open local API UDP socket - not all functionality will be available");
return;
}
HTTP::URL boundAddr;
boundAddr.protocol = "udp";
boundAddr.setPort(boundPort);
boundAddr.host = uSock.getBoundAddress();
{
tthread::lock_guard<tthread::mutex> guard(configMutex);
udpApiBindAddr = boundAddr.getUrl();
Controller::writeConfig();
}
Util::Procs::socketList.insert(uSock.getSock());
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.allocateDestination();
while (Controller::conf.is_active){
if (uSock.Receive()){
MEDIUM_MSG("UDP API: %s", (const char*)uSock.data);

View file

@ -16,6 +16,7 @@ namespace Controller{
std::string instanceId; /// instanceId (previously uniqId) is set in controller.cpp
std::string prometheus;
std::string accesslog;
std::string udpApiBindAddr;
Util::Config conf;
JSON::Value Storage; ///< Global storage of data.
tthread::mutex configMutex;
@ -444,7 +445,9 @@ namespace Controller{
|| !globAccX.getFieldAccX("sessionOutputMode")
|| !globAccX.getFieldAccX("sessionUnspecifiedMode")
|| !globAccX.getFieldAccX("sessionStreamInfoMode")
|| !globAccX.getFieldAccX("tknMode")){
|| !globAccX.getFieldAccX("tknMode")
|| !globAccX.getFieldAccX("udpApi")
){
globAccX.setReload();
globCfg.master = true;
globCfg.close();
@ -461,6 +464,7 @@ namespace Controller{
globAccX.addField("sessionUnspecifiedMode", RAX_64UINT);
globAccX.addField("sessionStreamInfoMode", RAX_64UINT);
globAccX.addField("tknMode", RAX_64UINT);
globAccX.addField("udpApi", RAX_128STRING);
globAccX.setRCount(1);
globAccX.setEndPos(1);
globAccX.setReady();
@ -472,6 +476,7 @@ namespace Controller{
globAccX.setInt("sessionUnspecifiedMode", Storage["config"]["sessionUnspecifiedMode"].asInt());
globAccX.setInt("sessionStreamInfoMode", Storage["config"]["sessionStreamInfoMode"].asInt());
globAccX.setInt("tknMode", Storage["config"]["tknMode"].asInt());
globAccX.setString("udpApi", udpApiBindAddr);
globAccX.setInt("systemBoot", systemBoot);
globCfg.master = false; // leave the page after closing
}

View file

@ -8,6 +8,7 @@ namespace Controller{
extern std::string instanceId; ///< global storage of instanceId (previously uniqID) is set in controller.cpp
extern std::string prometheus; ///< Prometheus access string
extern std::string accesslog; ///< Where to write the access log
extern std::string udpApiBindAddr; ///< Bound address where the UDP API listens
extern Util::Config conf; ///< Global storage of configuration.
extern JSON::Value Storage; ///< Global storage of data.
extern tthread::mutex logMutex; ///< Mutex for log thread.

View file

@ -2199,9 +2199,7 @@ namespace Mist{
prevLosCount = pktLosNow;
}
pData["active_seconds"] = statComm.getTime();
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(pStat.toString());
Util::sendUDPApi(pStat);
lastPushUpdate = now;
}
}
@ -2240,11 +2238,9 @@ namespace Mist{
/*LTS-START*/
// Tag the session with the user agent
if (newUA && ((now - myConn.connTime()) >= uaDelay || !myConn) && UA.size()){
std::string APIcall =
"{\"tag_sessid\":{\"" + statComm.sessionId + "\":" + JSON::string_escape("UA:" + UA) + "}}";
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(APIcall);
JSON::Value APIcall;
APIcall["tag_sessid"][statComm.sessionId] = "UA:"+UA;
Util::sendUDPApi(APIcall);
newUA = false;
}
/*LTS-END*/

View file

@ -226,9 +226,7 @@ namespace Mist{
pData["active_seconds"] = (Util::bootSecs() - startTime);
pData["ainfo"]["sourceTime"] = statSourceMs;
pData["ainfo"]["sinkTime"] = statSinkMs;
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(pStat.toString());
Util::sendUDPApi(pStat);
lastProcUpdate = Util::bootSecs();
}
}

View file

@ -948,9 +948,7 @@ int main(int argc, char *argv[]){
tthread::lock_guard<tthread::mutex> guard(broadcasterMutex);
pData["ainfo"]["bc"] = Mist::currBroadAddr;
}
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(pStat.toString());
Util::sendUDPApi(pStat);
lastProcUpdate = Util::bootSecs();
}
}

View file

@ -59,8 +59,6 @@ int main(int argc, char **argv){
std::string cbPath = getenv("RENEWED_LINEAGE");
std::string cbCert = cbPath + "/fullchain.pem";
std::string cbKey = cbPath + "/privkey.pem";
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
Util::DTSCShmReader rProto(SHM_PROTO);
DTSC::Scan prtcls = rProto.getScan();
unsigned int pro_cnt = prtcls.getSize();
@ -76,11 +74,11 @@ int main(int argc, char **argv){
cmd["updateprotocol"][1u]["cert"] = cbCert;
cmd["updateprotocol"][1u]["key"] = cbKey;
INFO_MSG("Executing: %s", cmd.toString().c_str());
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
Util::wait(500);
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
Util::wait(500);
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
}
}
if (!found){
@ -91,11 +89,11 @@ int main(int argc, char **argv){
cmd["addprotocol"]["cert"] = cbCert;
cmd["addprotocol"]["key"] = cbKey;
INFO_MSG("Executing: %s", cmd.toString().c_str());
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
Util::wait(500);
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
Util::wait(500);
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
}
Util::wait(5000);
return 0;
@ -119,15 +117,13 @@ int main(int argc, char **argv){
cmd["addprotocol"]["connector"] = "HTTP";
cmd["addprotocol"]["port"] = 80;
cmd["addprotocol"]["certbot"] = cbCombo;
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
Util::wait(1000);
int counter = 10;
while (--counter && ((foundHTTP80 = checkPort80(currConf)) == -1 ||
currConf["certbot"].asStringRef() != cbCombo)){
INFO_MSG("Waiting for Controller to pick up new config...");
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
Util::wait(1000);
}
if (!counter){
@ -146,15 +142,13 @@ int main(int argc, char **argv){
cmd["updateprotocol"].append(currConf);
cmd["updateprotocol"].append(currConf);
cmd["updateprotocol"][1u]["certbot"] = cbCombo;
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
Util::wait(1000);
int counter = 10;
while (--counter && ((foundHTTP80 = checkPort80(currConf)) == -1 ||
currConf["certbot"].asStringRef() != cbCombo)){
INFO_MSG("Waiting for Controller to pick up new config...");
uSock.SendNow(cmd.toString());
Util::sendUDPApi(cmd);
Util::wait(1000);
}
if (!counter){