Merge branch 'development' into LTS_development

# Conflicts:
#	src/controller/controller_api.cpp
This commit is contained in:
Thulinma 2020-07-15 19:30:40 +02:00
commit a15b3c20f1
5 changed files with 52 additions and 28 deletions

View file

@ -40,6 +40,9 @@ static Socket::Server *serv_sock_pointer = 0;
uint32_t Util::Config::printDebugLevel = DEBUG; // uint32_t Util::Config::printDebugLevel = DEBUG; //
std::string Util::Config::streamName; std::string Util::Config::streamName;
std::string Util::listenInterface;
uint32_t Util::listenPort = 0;
Util::Config::Config(){ Util::Config::Config(){
// global options here // global options here
vals["debug"]["long"] = "debug"; vals["debug"]["long"] = "debug";
@ -370,6 +373,7 @@ int Util::Config::serveThreadedSocket(int (*callback)(Socket::Connection &)){
DEVEL_MSG("Failure to open socket"); DEVEL_MSG("Failure to open socket");
return 1; return 1;
} }
Socket::getSocketName(server_socket.getSocket(), Util::listenInterface, Util::listenPort);
serv_sock_pointer = &server_socket; serv_sock_pointer = &server_socket;
activate(); activate();
if (server_socket.getSocket()){ if (server_socket.getSocket()){
@ -397,6 +401,7 @@ int Util::Config::serveForkedSocket(int (*callback)(Socket::Connection &S)){
DEVEL_MSG("Failure to open socket"); DEVEL_MSG("Failure to open socket");
return 1; return 1;
} }
Socket::getSocketName(server_socket.getSocket(), Util::listenInterface, Util::listenPort);
serv_sock_pointer = &server_socket; serv_sock_pointer = &server_socket;
activate(); activate();
if (server_socket.getSocket()){ if (server_socket.getSocket()){

View file

@ -49,6 +49,11 @@ namespace Util{
void addConnectorOptions(int port, JSON::Value &capabilities); void addConnectorOptions(int port, JSON::Value &capabilities);
}; };
/// The interface address the current serveSocket function is listening on
extern std::string listenInterface;
/// The port the current serveSocket function is listening on
extern uint32_t listenPort;
/// Gets directory the current executable is stored in. /// Gets directory the current executable is stored in.
std::string getMyPath(); std::string getMyPath();

View file

@ -270,6 +270,31 @@ std::string Socket::resolveHostToBestExternalAddrGuess(const std::string &host,
return newaddr; return newaddr;
} }
/// Gets bound host and port for a socket and returns them by reference.
/// Returns true on success and false on failure.
bool Socket::getSocketName(int fd, std::string & host, uint32_t & port){
struct sockaddr_in6 tmpaddr;
socklen_t len = sizeof(tmpaddr);
if (getsockname(fd, (sockaddr *)&tmpaddr, &len)){
return false;
}
static char addrconv[INET6_ADDRSTRLEN];
if (tmpaddr.sin6_family == AF_INET6){
host = inet_ntop(AF_INET6, &(tmpaddr.sin6_addr), addrconv, INET6_ADDRSTRLEN);
if (host.substr(0, 7) == "::ffff:"){host = host.substr(7);}
port = ntohs(tmpaddr.sin6_port);
HIGH_MSG("Local IPv6 addr [%s:%" PRIu32 "]", host.c_str(), port);
return true;
}
if (tmpaddr.sin6_family == AF_INET){
host = inet_ntop(AF_INET, &(((sockaddr_in *)&tmpaddr)->sin_addr), addrconv, INET6_ADDRSTRLEN);
port = ntohs(((sockaddr_in *)&tmpaddr)->sin_port);
HIGH_MSG("Local IPv4 addr [%s:%" PRIu32 "]", host.c_str(), port);
return true;
}
return false;
}
std::string uint2string(unsigned int i){ std::string uint2string(unsigned int i){
std::stringstream st; std::stringstream st;
st << i; st << i;
@ -457,20 +482,8 @@ void Socket::Connection::setBoundAddr(){
return; return;
} }
//Otherwise, read from socket pointer. Works for both SSL and non-SSL sockets, and real sockets passed as fd's, but not for non-sockets (duh) //Otherwise, read from socket pointer. Works for both SSL and non-SSL sockets, and real sockets passed as fd's, but not for non-sockets (duh)
struct sockaddr_in6 tmpaddr; uint32_t boundport = 0;
socklen_t len = sizeof(tmpaddr); getSocketName(getSocket(), boundaddr, boundport);
if (!getsockname(getSocket(), (sockaddr *)&tmpaddr, &len)){
static char addrconv[INET6_ADDRSTRLEN];
if (tmpaddr.sin6_family == AF_INET6){
boundaddr = inet_ntop(AF_INET6, &(tmpaddr.sin6_addr), addrconv, INET6_ADDRSTRLEN);
if (boundaddr.substr(0, 7) == "::ffff:"){boundaddr = boundaddr.substr(7);}
HIGH_MSG("Local IPv6 addr [%s]", boundaddr.c_str());
}
if (tmpaddr.sin6_family == AF_INET){
boundaddr = inet_ntop(AF_INET, &(((sockaddr_in *)&tmpaddr)->sin_addr), addrconv, INET6_ADDRSTRLEN);
HIGH_MSG("Local IPv4 addr [%s]", boundaddr.c_str());
}
}
} }
// Cleans up the socket by dropping the connection. // Cleans up the socket by dropping the connection.
@ -1741,21 +1754,9 @@ void Socket::UDPConnection::SendNow(const char *sdata, size_t len){
} }
std::string Socket::UDPConnection::getBoundAddress(){ std::string Socket::UDPConnection::getBoundAddress(){
struct sockaddr_in6 tmpaddr;
socklen_t len = sizeof(tmpaddr);
std::string boundaddr; std::string boundaddr;
if (!getsockname(sock, (sockaddr *)&tmpaddr, &len)){ uint32_t boundport;
static char addrconv[INET6_ADDRSTRLEN]; Socket::getSocketName(sock, boundaddr, boundport);
if (tmpaddr.sin6_family == AF_INET6){
boundaddr = inet_ntop(AF_INET6, &(tmpaddr.sin6_addr), addrconv, INET6_ADDRSTRLEN);
if (boundaddr.substr(0, 7) == "::ffff:"){boundaddr = boundaddr.substr(7);}
HIGH_MSG("Local IPv6 addr [%s]", boundaddr.c_str());
}
if (tmpaddr.sin6_family == AF_INET){
boundaddr = inet_ntop(AF_INET, &(((sockaddr_in *)&tmpaddr)->sin_addr), addrconv, INET6_ADDRSTRLEN);
HIGH_MSG("Local IPv4 addr [%s]", boundaddr.c_str());
}
}
return boundaddr; return boundaddr;
} }

View file

@ -44,6 +44,7 @@ namespace Socket{
bool checkTrueSocket(int sock); bool checkTrueSocket(int sock);
std::string resolveHostToBestExternalAddrGuess(const std::string &host, int family = AF_UNSPEC, std::string resolveHostToBestExternalAddrGuess(const std::string &host, int family = AF_UNSPEC,
const std::string &hint = ""); const std::string &hint = "");
bool getSocketName(int fd, std::string & host, uint32_t & port);
/// A buffer made out of std::string objects that can be efficiently read from and written to. /// A buffer made out of std::string objects that can be efficiently read from and written to.
class Buffer{ class Buffer{

View file

@ -2,6 +2,7 @@
#include <sys/stat.h> //for browse API call #include <sys/stat.h> //for browse API call
#include <fstream> #include <fstream>
#include <mist/http_parser.h> #include <mist/http_parser.h>
#include <mist/url.h>
#include <mist/auth.h> #include <mist/auth.h>
#include <mist/stream.h> #include <mist/stream.h>
#include <mist/config.h> #include <mist/config.h>
@ -444,6 +445,7 @@ void Controller::handleUDPAPI(void * np){
return; return;
} }
Util::Procs::socketList.insert(uSock.getSock()); Util::Procs::socketList.insert(uSock.getSock());
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
while (Controller::conf.is_active){ while (Controller::conf.is_active){
if (uSock.Receive()){ if (uSock.Receive()){
MEDIUM_MSG("UDP API: %s", uSock.data); MEDIUM_MSG("UDP API: %s", uSock.data);
@ -453,6 +455,7 @@ void Controller::handleUDPAPI(void * np){
if (Request.isObject()){ if (Request.isObject()){
tthread::lock_guard<tthread::mutex> guard(configMutex); tthread::lock_guard<tthread::mutex> guard(configMutex);
handleAPICommands(Request, Response); handleAPICommands(Request, Response);
uSock.SendNow(Response.toString());
}else{ }else{
WARN_MSG("Invalid API command received over UDP: %s", uSock.data); WARN_MSG("Invalid API command received over UDP: %s", uSock.data);
} }
@ -941,6 +944,15 @@ void Controller::handleAPICommands(JSON::Value & Request, JSON::Value & Response
Controller::fillActive(Request["stats_streams"], Response["stats_streams"]); Controller::fillActive(Request["stats_streams"], Response["stats_streams"]);
} }
if (Request.isMember("api_endpoint")){
HTTP::URL url("http://localhost:4242");
url.host = Util::listenInterface;
if (url.host == "::"){url.host = "::1";}
if (url.host == "0.0.0.0"){url.host = "127.0.0.1";}
url.port = JSON::Value(Util::listenPort).asString();
Response["api_endpoint"] = url.getUrl();
}
if (Request.isMember("invalidate_sessions")){ if (Request.isMember("invalidate_sessions")){
if (Request["invalidate_sessions"].isArray()){ if (Request["invalidate_sessions"].isArray()){
for (unsigned int i = 0; i < Request["invalidate_sessions"].size(); ++i){ for (unsigned int i = 0; i < Request["invalidate_sessions"].size(); ++i){