Robustify accesses to server config
This commit is contained in:
		
							parent
							
								
									6032f236d2
								
							
						
					
					
						commit
						98e3940079
					
				
					 15 changed files with 320 additions and 266 deletions
				
			
		|  | @ -125,10 +125,8 @@ namespace Mist { | |||
|     } | ||||
|      | ||||
|     //loop over the connectors
 | ||||
|     IPC::semaphore configLock(SEM_CONF, O_CREAT | O_RDWR, ACCESSPERMS, 1); | ||||
|     configLock.wait(); | ||||
|     IPC::sharedPage serverCfg(SHM_CONF, DEFAULT_CONF_PAGE_SIZE); | ||||
|     DTSC::Scan capa = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("capabilities").getMember("connectors"); | ||||
|     Util::DTSCShmReader rCapa(SHM_CAPA); | ||||
|     DTSC::Scan capa = rCapa.getMember("connectors"); | ||||
|     unsigned int capa_ctr = capa.getSize(); | ||||
|     for (unsigned int i = 0; i < capa_ctr; ++i){ | ||||
|       DTSC::Scan c = capa.getIndice(i); | ||||
|  | @ -161,14 +159,10 @@ namespace Mist { | |||
|             Util::sanitizeName(streamname); | ||||
|             H.SetVar("stream", streamname); | ||||
|           } | ||||
|           configLock.post(); | ||||
|           configLock.close(); | ||||
|           return capa.getIndiceName(i); | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|     configLock.post(); | ||||
|     configLock.close(); | ||||
|     return ""; | ||||
|   } | ||||
|    | ||||
|  | @ -353,32 +347,25 @@ namespace Mist { | |||
|     char * argarr[20]; | ||||
|     for (int i=0; i<20; i++){argarr[i] = 0;} | ||||
|     int id = -1; | ||||
|      | ||||
|     IPC::semaphore configLock(SEM_CONF, O_CREAT | O_RDWR, ACCESSPERMS, 1); | ||||
|     configLock.wait(); | ||||
|     IPC::sharedPage serverCfg(SHM_CONF, DEFAULT_CONF_PAGE_SIZE); | ||||
|     DTSC::Scan prots = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("config").getMember("protocols"); | ||||
|     unsigned int prots_ctr = prots.getSize(); | ||||
|     | ||||
|     JSON::Value pipedCapa; | ||||
|     JSON::Value p;//properties of protocol
 | ||||
|     if (connector == "HTTP" || connector == "HTTP.exe"){ | ||||
|       //restore from values in the environment, regardless of configged settings
 | ||||
|       if (getenv("MIST_HTTP_nostreamtext")){ | ||||
|         p["nostreamtext"] = getenv("MIST_HTTP_nostreamtext"); | ||||
|       } | ||||
|       if (getenv("MIST_HTTP_pubaddr")){ | ||||
|         p["pubaddr"] = getenv("MIST_HTTP_pubaddr"); | ||||
|       } | ||||
|     }else{ | ||||
|       //find connector in config
 | ||||
|       for (unsigned int i=0; i < prots_ctr; ++i){ | ||||
|         if (prots.getIndice(i).getMember("connector").asString() == connector) { | ||||
|           id =  i; | ||||
|           break;    //pick the first protocol in the list that matches the connector 
 | ||||
|      | ||||
| 
 | ||||
|     { | ||||
|       Util::DTSCShmReader rProto(SHM_PROTO); | ||||
|       DTSC::Scan prots = rProto.getScan(); | ||||
|       unsigned int prots_ctr = prots.getSize(); | ||||
|       | ||||
|       if (connector == "HTTP" || connector == "HTTP.exe"){ | ||||
|         //restore from values in the environment, regardless of configged settings
 | ||||
|         if (getenv("MIST_HTTP_nostreamtext")){ | ||||
|           p["nostreamtext"] = getenv("MIST_HTTP_nostreamtext"); | ||||
|         } | ||||
|       } | ||||
|       if (id == -1) { | ||||
|         connector = connector + ".exe"; | ||||
|         if (getenv("MIST_HTTP_pubaddr")){ | ||||
|           p["pubaddr"] = getenv("MIST_HTTP_pubaddr"); | ||||
|         } | ||||
|       }else{ | ||||
|         //find connector in config
 | ||||
|         for (unsigned int i=0; i < prots_ctr; ++i){ | ||||
|           if (prots.getIndice(i).getMember("connector").asString() == connector) { | ||||
|             id =  i; | ||||
|  | @ -386,27 +373,33 @@ namespace Mist { | |||
|           } | ||||
|         } | ||||
|         if (id == -1) { | ||||
|           connector = connector.substr(0, connector.size() - 4); | ||||
|           DEBUG_MSG(DLVL_ERROR, "No connector found for: %s", connector.c_str()); | ||||
|           configLock.post(); | ||||
|           configLock.close(); | ||||
|           return; | ||||
|           connector = connector + ".exe"; | ||||
|           for (unsigned int i=0; i < prots_ctr; ++i){ | ||||
|             if (prots.getIndice(i).getMember("connector").asString() == connector) { | ||||
|               id =  i; | ||||
|               break;    //pick the first protocol in the list that matches the connector 
 | ||||
|             } | ||||
|           } | ||||
|           if (id == -1) { | ||||
|             connector = connector.substr(0, connector.size() - 4); | ||||
|             ERROR_MSG("No connector found for: %s", connector.c_str()); | ||||
|             return; | ||||
|           } | ||||
|         } | ||||
|         //read options from found connector
 | ||||
|         p = prots.getIndice(id).asJSON(); | ||||
|       } | ||||
|       //read options from found connector
 | ||||
|       p = prots.getIndice(id).asJSON(); | ||||
|        | ||||
|       HIGH_MSG("Connector found: %s", connector.c_str()); | ||||
|       Util::DTSCShmReader rCapa(SHM_CAPA); | ||||
|       DTSC::Scan capa = rCapa.getMember("connectors"); | ||||
|       pipedCapa = capa.getMember(connector).asJSON(); | ||||
|     } | ||||
|      | ||||
|     DEBUG_MSG(DLVL_HIGH, "Connector found: %s", connector.c_str()); | ||||
| 
 | ||||
|     //build arguments for starting output process
 | ||||
|      | ||||
|     std::string tmparg = Util::getMyPath() + std::string("MistOut") + connector; | ||||
|      | ||||
|     int argnum = 0; | ||||
|     argarr[argnum++] = (char*)tmparg.c_str(); | ||||
|     JSON::Value pipedCapa = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("capabilities").getMember("connectors").getMember(connector).asJSON(); | ||||
|     configLock.post(); | ||||
|     configLock.close(); | ||||
|     std::string temphost=getConnectedHost(); | ||||
|     std::string debuglevel = JSON::Value((long long)Util::Config::printDebugLevel).asString(); | ||||
|     argarr[argnum++] = (char*)"--ip"; | ||||
|  | @ -465,20 +458,19 @@ namespace Mist { | |||
|       trustedProxies.insert("::1"); | ||||
|       trustedProxies.insert("127.0.0.1"); | ||||
| 
 | ||||
|       IPC::sharedPage serverCfg(SHM_CONF, DEFAULT_CONF_PAGE_SIZE, false, false); ///< Open server config
 | ||||
|       IPC::semaphore configLock(SEM_CONF, O_CREAT | O_RDWR, ACCESSPERMS, 1); | ||||
|       configLock.wait(); | ||||
|       std::string trustedList = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("config").getMember("trustedproxy").asString(); | ||||
|       configLock.post(); | ||||
|       configLock.close(); | ||||
|       size_t pos = 0; | ||||
|       size_t endPos; | ||||
|       while (pos != std::string::npos){ | ||||
|         endPos = trustedList.find(" ", pos); | ||||
|         trustedProxies.insert(trustedList.substr(pos, endPos - pos)); | ||||
|         pos = endPos; | ||||
|         if (pos != std::string::npos){ | ||||
|           pos++; | ||||
|       IPC::sharedPage rPage(SHM_PROXY, 0, false, false); | ||||
|       if (rPage){ | ||||
|         Util::RelAccX rAcc(rPage.mapped); | ||||
|         std::string trustedList(rAcc.getPointer("proxy_data"), rAcc.getSize("proxy_data")); | ||||
|         size_t pos = 0; | ||||
|         size_t endPos; | ||||
|         while (pos != std::string::npos){ | ||||
|           endPos = trustedList.find(" ", pos); | ||||
|           trustedProxies.insert(trustedList.substr(pos, endPos - pos)); | ||||
|           pos = endPos; | ||||
|           if (pos != std::string::npos){ | ||||
|             pos++; | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  |  | |||
|  | @ -11,13 +11,13 @@ namespace Mist { | |||
|   /// Helper function to find the protocol entry for a given port number
 | ||||
|   std::string getProtocolForPort(uint16_t portNo){ | ||||
|     std::string ret; | ||||
|     IPC::semaphore configLock(SEM_CONF, O_CREAT | O_RDWR, ACCESSPERMS, 1); | ||||
|     configLock.wait(); | ||||
|     IPC::sharedPage serverCfg(SHM_CONF, DEFAULT_CONF_PAGE_SIZE); | ||||
|     DTSC::Scan prtcls = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("config").getMember("protocols"); | ||||
|     Util::DTSCShmReader rCapa(SHM_CAPA); | ||||
|     DTSC::Scan conns = rCapa.getMember("connectors"); | ||||
|     Util::DTSCShmReader rProto(SHM_PROTO); | ||||
|     DTSC::Scan prtcls = rProto.getScan(); | ||||
|     unsigned int pro_cnt = prtcls.getSize(); | ||||
|     for (unsigned int i = 0; i < pro_cnt; ++i){ | ||||
|       DTSC::Scan capa = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("capabilities").getMember("connectors").getMember(prtcls.getIndice(i).getMember("connector").asString()); | ||||
|       DTSC::Scan capa = conns.getMember(prtcls.getIndice(i).getMember("connector").asString()); | ||||
|       uint16_t port = prtcls.getIndice(i).getMember("port").asInt(); | ||||
|       //get the default port if none is set
 | ||||
|       if (!port){ | ||||
|  | @ -28,7 +28,6 @@ namespace Mist { | |||
|         break; | ||||
|       } | ||||
|     } | ||||
|     configLock.post(); | ||||
|     if (ret.find(':') != std::string::npos){ | ||||
|       ret.erase(ret.find(':')); | ||||
|     } | ||||
|  | @ -383,16 +382,6 @@ namespace Mist { | |||
|     if (!myConn){ | ||||
|       return json_resp; | ||||
|     } | ||||
|     IPC::semaphore configLock(SEM_CONF, O_CREAT | O_RDWR, ACCESSPERMS, 1); | ||||
|     configLock.wait(); | ||||
|     IPC::sharedPage serverCfg(SHM_CONF, DEFAULT_CONF_PAGE_SIZE); | ||||
|     DTSC::Scan prots = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("config").getMember("protocols"); | ||||
|     if (!prots){ | ||||
|       json_resp["error"] = "The specified stream is not available on this server."; | ||||
|       configLock.post(); | ||||
|       configLock.close(); | ||||
|       return json_resp; | ||||
|     } | ||||
| 
 | ||||
|     bool hasVideo = false; | ||||
|     for (std::map<unsigned int, DTSC::Track>::iterator trit = myMeta.tracks.begin(); trit != myMeta.tracks.end(); trit++){ | ||||
|  | @ -431,7 +420,17 @@ namespace Mist { | |||
|       it->removeMember("ivecs");/*LTS*/ | ||||
|     } | ||||
|     json_resp["meta"].removeMember("source"); | ||||
|      | ||||
| 
 | ||||
|     //Get sources/protocols information
 | ||||
|     Util::DTSCShmReader rCapa(SHM_CAPA); | ||||
|     DTSC::Scan connectors = rCapa.getMember("connectors"); | ||||
|     Util::DTSCShmReader rProto(SHM_PROTO); | ||||
|     DTSC::Scan prots = rProto.getScan(); | ||||
|     if (!prots || !connectors){ | ||||
|       json_resp["error"] = "Server configuration unavailable at this time."; | ||||
|       return json_resp; | ||||
|     } | ||||
|   | ||||
|     //create a set for storing source information
 | ||||
|     std::set<JSON::Value, sourceCompare> sources; | ||||
|      | ||||
|  | @ -444,7 +443,7 @@ namespace Mist { | |||
|     //loop over the connectors.
 | ||||
|     for (unsigned int i = 0; i < prots_ctr; ++i){ | ||||
|       std::string cName = prots.getIndice(i).getMember("connector").asString(); | ||||
|       DTSC::Scan capa = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("capabilities").getMember("connectors").getMember(cName); | ||||
|       DTSC::Scan capa = connectors.getMember(cName); | ||||
|       //if the connector has a port,
 | ||||
|       if (capa.getMember("optional").getMember("port")){ | ||||
|         HTTP::URL outURL(reqHost); | ||||
|  | @ -476,12 +475,11 @@ namespace Mist { | |||
|           std::string cProv = capa.getMember("provides").asString(); | ||||
|           //if this connector can be depended upon by other connectors, loop over the rest
 | ||||
|           //check each enabled protocol separately to see if it depends on this connector
 | ||||
|           DTSC::Scan capa_lst = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("capabilities").getMember("connectors"); | ||||
|           unsigned int capa_lst_ctr = capa_lst.getSize(); | ||||
|           unsigned int capa_lst_ctr = connectors.getSize(); | ||||
|           for (unsigned int j = 0; j < capa_lst_ctr; ++j){ | ||||
|             //if it depends on this connector and has a URL, list it
 | ||||
|             if (conns.count(capa_lst.getIndiceName(j)) && capa_lst.getIndice(j).getMember("deps").asString() == cProv && capa_lst.getIndice(j).getMember("methods")){ | ||||
|               JSON::Value subcapa_json = capa_lst.getIndice(j).asJSON(); | ||||
|             if (conns.count(connectors.getIndiceName(j)) && connectors.getIndice(j).getMember("deps").asString() == cProv && connectors.getIndice(j).getMember("methods")){ | ||||
|               JSON::Value subcapa_json = connectors.getIndice(j).asJSON(); | ||||
|               addSources(streamName, sources, outURL, subcapa_json, json_resp["meta"], useragent); | ||||
|             } | ||||
|           } | ||||
|  | @ -495,8 +493,6 @@ namespace Mist { | |||
|         json_resp["source"].append(*it); | ||||
|       } | ||||
|     } | ||||
|     configLock.post(); | ||||
|     configLock.close(); | ||||
|     return json_resp; | ||||
|   } | ||||
| 
 | ||||
|  | @ -567,41 +563,38 @@ namespace Mist { | |||
|     // send smil MBR index
 | ||||
|     if (H.url.length() > 6 && H.url.substr(H.url.length() - 5, 5) == ".smil"){ | ||||
|       std::string reqHost = HTTP::URL(H.GetHeader("Host")).host; | ||||
| 
 | ||||
|       std::string port, url_rel; | ||||
|        | ||||
|       IPC::semaphore configLock(SEM_CONF, O_CREAT | O_RDWR, ACCESSPERMS, 1); | ||||
|       configLock.wait(); | ||||
|       IPC::sharedPage serverCfg(SHM_CONF, DEFAULT_CONF_PAGE_SIZE); | ||||
|       DTSC::Scan prtcls = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("config").getMember("protocols"); | ||||
|       DTSC::Scan capa = DTSC::Scan(serverCfg.mapped, serverCfg.len).getMember("capabilities").getMember("connectors").getMember("RTMP"); | ||||
|       unsigned int pro_cnt = prtcls.getSize(); | ||||
|       for (unsigned int i = 0; i < pro_cnt; ++i){ | ||||
|         if (prtcls.getIndice(i).getMember("connector").asString() != "RTMP"){ | ||||
|           continue; | ||||
|         } | ||||
|         port = prtcls.getIndice(i).getMember("port").asString(); | ||||
|         //get the default port if none is set
 | ||||
|         if (!port.size()){ | ||||
|           port = capa.getMember("optional").getMember("port").getMember("default").asString(); | ||||
|         } | ||||
|         //extract url
 | ||||
|         url_rel = capa.getMember("url_rel").asString(); | ||||
|         if (url_rel.find('$')){ | ||||
|           url_rel.resize(url_rel.find('$')); | ||||
|         } | ||||
|       } | ||||
|        | ||||
|       std::string trackSources;//this string contains all track sources for MBR smil
 | ||||
|       initialize(); | ||||
|       if (!myConn){return;} | ||||
|       for (std::map<unsigned int, DTSC::Track>::iterator trit = myMeta.tracks.begin(); trit != myMeta.tracks.end(); trit++){ | ||||
|         if (trit->second.type == "video"){ | ||||
|           trackSources += "      <video src='"+ streamName + "?track=" + JSON::Value((long long)trit->first).asString() + "' height='" + JSON::Value((long long)trit->second.height).asString() + "' system-bitrate='" + JSON::Value((long long)trit->second.bps).asString() + "' width='" + JSON::Value((long long)trit->second.width).asString() + "' />\n"; | ||||
|       { | ||||
|         Util::DTSCShmReader rProto(SHM_PROTO); | ||||
|         DTSC::Scan prtcls = rProto.getScan(); | ||||
|         Util::DTSCShmReader rCapa(SHM_CAPA); | ||||
|         DTSC::Scan capa = rCapa.getMember("connectors").getMember("RTMP"); | ||||
|         unsigned int pro_cnt = prtcls.getSize(); | ||||
|         for (unsigned int i = 0; i < pro_cnt; ++i){ | ||||
|           if (prtcls.getIndice(i).getMember("connector").asString() != "RTMP"){ | ||||
|             continue; | ||||
|           } | ||||
|           port = prtcls.getIndice(i).getMember("port").asString(); | ||||
|           //get the default port if none is set
 | ||||
|           if (!port.size()){ | ||||
|             port = capa.getMember("optional").getMember("port").getMember("default").asString(); | ||||
|           } | ||||
|           //extract url
 | ||||
|           url_rel = capa.getMember("url_rel").asString(); | ||||
|           if (url_rel.find('$')){ | ||||
|             url_rel.resize(url_rel.find('$')); | ||||
|           } | ||||
|         } | ||||
|          | ||||
|         initialize(); | ||||
|         if (!myConn){return;} | ||||
|         for (std::map<unsigned int, DTSC::Track>::iterator trit = myMeta.tracks.begin(); trit != myMeta.tracks.end(); trit++){ | ||||
|           if (trit->second.type == "video"){ | ||||
|             trackSources += "      <video src='"+ streamName + "?track=" + JSON::Value((long long)trit->first).asString() + "' height='" + JSON::Value((long long)trit->second.height).asString() + "' system-bitrate='" + JSON::Value((long long)trit->second.bps).asString() + "' width='" + JSON::Value((long long)trit->second.width).asString() + "' />\n"; | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|       configLock.post(); | ||||
|       configLock.close(); | ||||
|        | ||||
|       H.Clean(); | ||||
|       H.SetHeader("Content-Type", "application/smil"); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Thulinma
						Thulinma