Added API call for shutting down MistServer from local API connections
This commit is contained in:
parent
b057698018
commit
a05dba0ce8
1 changed files with 21 additions and 0 deletions
|
@ -257,6 +257,7 @@ int Controller::handleAPIConnection(Socket::Connection & conn){
|
||||||
//set up defaults
|
//set up defaults
|
||||||
unsigned int logins = 0;
|
unsigned int logins = 0;
|
||||||
bool authorized = false;
|
bool authorized = false;
|
||||||
|
bool isLocal = false;
|
||||||
HTTP::Parser H;
|
HTTP::Parser H;
|
||||||
//while connected and not past login attempt limit
|
//while connected and not past login attempt limit
|
||||||
while (conn && logins < 4){
|
while (conn && logins < 4){
|
||||||
|
@ -264,6 +265,7 @@ int Controller::handleAPIConnection(Socket::Connection & conn){
|
||||||
//Are we local and not forwarded? Instant-authorized.
|
//Are we local and not forwarded? Instant-authorized.
|
||||||
if (!authorized && !H.hasHeader("X-Real-IP") && conn.isLocal()){
|
if (!authorized && !H.hasHeader("X-Real-IP") && conn.isLocal()){
|
||||||
MEDIUM_MSG("Local API access automatically authorized");
|
MEDIUM_MSG("Local API access automatically authorized");
|
||||||
|
isLocal = true;
|
||||||
authorized = true;
|
authorized = true;
|
||||||
}
|
}
|
||||||
#ifdef NOAUTH
|
#ifdef NOAUTH
|
||||||
|
@ -329,6 +331,7 @@ int Controller::handleAPIConnection(Socket::Connection & conn){
|
||||||
//if already authorized, do not re-check for authorization
|
//if already authorized, do not re-check for authorization
|
||||||
if (authorized && Storage["account"]){
|
if (authorized && Storage["account"]){
|
||||||
Response["authorize"]["status"] = "OK";
|
Response["authorize"]["status"] = "OK";
|
||||||
|
if (isLocal){Response["authorize"]["local"] = true;}
|
||||||
}else{
|
}else{
|
||||||
authorized |= authorize(Request, Response, conn);
|
authorized |= authorize(Request, Response, conn);
|
||||||
}
|
}
|
||||||
|
@ -378,7 +381,9 @@ void Controller::handleUDPAPI(void * np){
|
||||||
JSON::Value Response;
|
JSON::Value Response;
|
||||||
if (Request.isObject()){
|
if (Request.isObject()){
|
||||||
tthread::lock_guard<tthread::mutex> guard(configMutex);
|
tthread::lock_guard<tthread::mutex> guard(configMutex);
|
||||||
|
Response["authorize"]["local"] = true;
|
||||||
handleAPICommands(Request, Response);
|
handleAPICommands(Request, Response);
|
||||||
|
Response.removeMember("authorize");
|
||||||
uSock.SendNow(Response.toString());
|
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);
|
||||||
|
@ -652,5 +657,21 @@ void Controller::handleAPICommands(JSON::Value & Request, JSON::Value & Response
|
||||||
Response["api_endpoint"] = url.getUrl();
|
Response["api_endpoint"] = url.getUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Request.isMember("shutdown")){
|
||||||
|
if (Response.isMember("authorize") && Response["authorize"].isMember("local")){
|
||||||
|
std::string reason;
|
||||||
|
if (Request["shutdown"].isObject() || Request["shutdown"].isArray()){
|
||||||
|
reason = Request["shutdown"].toString();
|
||||||
|
}else{
|
||||||
|
reason = Request["shutdown"].asString();
|
||||||
|
}
|
||||||
|
WARN_MSG("Shutdown requested through local API: %s", reason.c_str());
|
||||||
|
Controller::conf.is_active = false;
|
||||||
|
Response["shutdown"] = "Shutting down";
|
||||||
|
}else{
|
||||||
|
Response["shutdown"] = "Ignored - only local users may request shutdown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Controller::configChanged = true;
|
Controller::configChanged = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue