Controller now listens locally to UDP port 4242 for API commands, added always-minimal api2 request type.
This commit is contained in:
		
							parent
							
								
									3980b8c8c1
								
							
						
					
					
						commit
						d0912c9d00
					
				
					 3 changed files with 526 additions and 489 deletions
				
			
		|  | @ -362,6 +362,8 @@ int main_loop(int argc, char ** argv){ | |||
|   tthread::thread uplinkThread(Controller::uplinkConnection, 0);/*LTS*/ | ||||
|   //start push checking thread
 | ||||
|   tthread::thread pushThread(Controller::pushCheckLoop, 0); | ||||
|   //start UDP API thread
 | ||||
|   tthread::thread UDPAPIThread(Controller::handleUDPAPI, 0); | ||||
| 
 | ||||
|    | ||||
|   //start main loop
 | ||||
|  | @ -410,6 +412,8 @@ int main_loop(int argc, char ** argv){ | |||
|   uplinkThread.join(); | ||||
|   HIGH_MSG("Joining push thread..."); | ||||
|   pushThread.join(); | ||||
|   HIGH_MSG("Joining UDP API thread..."); | ||||
|   UDPAPIThread.join(); | ||||
|   #ifdef LICENSING | ||||
|   HIGH_MSG("Joining license thread..."); | ||||
|   licenseThread.join(); | ||||
|  |  | |||
|  | @ -187,7 +187,7 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ | |||
|       JSON::Value Response; | ||||
|       JSON::Value Request = JSON::fromString(H.GetVar("command")); | ||||
|       //invalid request? send the web interface, unless requested as "/api"
 | ||||
|       if ( !Request.isObject() && H.url != "/api"){ | ||||
|       if ( !Request.isObject() && H.url != "/api" && H.url != "/api2"){ | ||||
|         #include "server.html.h" | ||||
|         H.Clean(); | ||||
|         H.SetHeader("Content-Type", "text/html"); | ||||
|  | @ -200,6 +200,9 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ | |||
|         H.Clean(); | ||||
|         break; | ||||
|       } | ||||
|       if (H.url == "/api2"){ | ||||
|         Request["minimal"] = true; | ||||
|       } | ||||
|       {//lock the config mutex here - do not unlock until done processing
 | ||||
|         tthread::lock_guard<tthread::mutex> guard(configMutex); | ||||
|         //Are we local and not forwarded? Instant-authorized.
 | ||||
|  | @ -214,6 +217,61 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ | |||
|           authorized |= authorize(Request, Response, conn); | ||||
|         } | ||||
|         if (authorized){ | ||||
|           handleAPICommands(Request, Response); | ||||
|         }else{//unauthorized
 | ||||
|           Util::sleep(1000);//sleep a second to prevent bruteforcing 
 | ||||
|           logins++; | ||||
|         } | ||||
|         Controller::checkServerLimits(); /*LTS*/ | ||||
|       }//config mutex lock
 | ||||
|       //send the response, either normally or through JSONP callback.
 | ||||
|       std::string jsonp = ""; | ||||
|       if (H.GetVar("callback") != ""){ | ||||
|         jsonp = H.GetVar("callback"); | ||||
|       } | ||||
|       if (H.GetVar("jsonp") != ""){ | ||||
|         jsonp = H.GetVar("jsonp"); | ||||
|       } | ||||
|       H.Clean(); | ||||
|       H.SetHeader("Content-Type", "text/javascript"); | ||||
|       H.setCORSHeaders(); | ||||
|       if (jsonp == ""){ | ||||
|         H.SetBody(Response.toString() + "\n\n"); | ||||
|       }else{ | ||||
|         H.SetBody(jsonp + "(" + Response.toString() + ");\n\n"); | ||||
|       } | ||||
|       H.SendResponse("200", "OK", conn); | ||||
|       H.Clean(); | ||||
|     }//if HTTP request received
 | ||||
|   }//while connected
 | ||||
|   return 0; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void Controller::handleUDPAPI(void * np){ | ||||
|   Socket::UDPConnection uSock(true); | ||||
|   if (!uSock.bind(4242, "localhost")){ | ||||
|     FAIL_MSG("Could not open local API UDP socket - not all functionality will be available"); | ||||
|     return; | ||||
|   } | ||||
|   while (Controller::conf.is_active){ | ||||
|     if (uSock.Receive()){ | ||||
|       INFO_MSG("UDP API: %s", uSock.data); | ||||
|       JSON::Value Request = JSON::fromString(uSock.data, uSock.data_len); | ||||
|       Request["minimal"] = true; | ||||
|       JSON::Value Response; | ||||
|       if (Request.isObject()){ | ||||
|         handleAPICommands(Request, Response); | ||||
|       }else{ | ||||
|         WARN_MSG("Invalid API command received over UDP: %s", uSock.data); | ||||
|       } | ||||
|     }else{ | ||||
|       Util::sleep(500); | ||||
|     } | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void Controller::handleAPICommands(JSON::Value & Request, JSON::Value & Response){ | ||||
|   //Parse config and streams from the request.
 | ||||
|   if (Request.isMember("config")){ | ||||
|     Controller::checkConfig(Request["config"], Controller::Storage["config"]); | ||||
|  | @ -701,32 +759,5 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ | |||
| 
 | ||||
| 
 | ||||
|   Controller::configChanged = true; | ||||
|            | ||||
|         }else{//unauthorized
 | ||||
|           Util::sleep(1000);//sleep a second to prevent bruteforcing 
 | ||||
|           logins++; | ||||
|         } | ||||
|         Controller::checkServerLimits(); /*LTS*/ | ||||
|       }//config mutex lock
 | ||||
|       //send the response, either normally or through JSONP callback.
 | ||||
|       std::string jsonp = ""; | ||||
|       if (H.GetVar("callback") != ""){ | ||||
|         jsonp = H.GetVar("callback"); | ||||
|       } | ||||
|       if (H.GetVar("jsonp") != ""){ | ||||
|         jsonp = H.GetVar("jsonp"); | ||||
|       } | ||||
|       H.Clean(); | ||||
|       H.SetHeader("Content-Type", "text/javascript"); | ||||
|       H.setCORSHeaders(); | ||||
|       if (jsonp == ""){ | ||||
|         H.SetBody(Response.toString() + "\n\n"); | ||||
|       }else{ | ||||
|         H.SetBody(jsonp + "(" + Response.toString() + ");\n\n"); | ||||
|       } | ||||
|       H.SendResponse("200", "OK", conn); | ||||
|       H.Clean(); | ||||
|     }//if HTTP request received
 | ||||
|   }//while connected
 | ||||
|   return 0; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -5,4 +5,6 @@ namespace Controller { | |||
|   void checkConfig(JSON::Value & in, JSON::Value & out); | ||||
|   bool authorize(JSON::Value & Request, JSON::Value & Response, Socket::Connection & conn); | ||||
|   int handleAPIConnection(Socket::Connection & conn); | ||||
|   void handleAPICommands(JSON::Value & Request, JSON::Value & Response); | ||||
|   void handleUDPAPI(void * np); | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Thulinma
						Thulinma