void HttpInterface::evReset(HttpRequest* req, strings& args) { for (NodesMap::iterator descIt = nodes.begin(); descIt != nodes.end(); ++descIt) { bool ok; nodeId = getNodeId(descIt->second.name, 0, &ok); if (!ok) continue; string nodeName = WStringToUTF8(descIt->second.name); Reset(nodeId).serialize(asebaStream); // reset node asebaStream->flush(); Run(nodeId).serialize(asebaStream); // re-run node asebaStream->flush(); if (nodeName.find("thymio-II") == 0) { strings args; args.push_back("motor.left.target"); args.push_back("0"); sendSetVariable(nodeName, args); args[0] = "motor.right.target"; sendSetVariable(nodeName, args); } size_t eventPos; if (commonDefinitions.events.contains(UTF8ToWString("reset"), &eventPos)) { strings data; data.push_back("reset"); sendEvent(nodeName,data); } finishResponse(req, 200, ""); } }
void SessionInfo::SetHomepage(const char* homepage) { tstring newHomepage = UTF8ToWString(homepage); if (m_homepages.end() == std::find(m_homepages.begin(), m_homepages.end(), newHomepage)) { m_homepages.push_back(newHomepage); } }
// Utility: find variable address bool HttpInterface::getNodeAndVarPos(const string& nodeName, const string& variableName, unsigned& nodeId, unsigned& pos) { // make sure the node exists bool ok; nodeId = getNodeId(UTF8ToWString(nodeName), 0, &ok); if (!ok) { if (verbose) wcerr << "invalid node name " << UTF8ToWString(nodeName) << endl; return false; } pos = unsigned(-1); // check whether variable is known from a compilation, if so, get position const NodeNameVariablesMap::const_iterator allVarMapIt(allVariables.find(nodeName)); if (allVarMapIt != allVariables.end()) { const VariablesMap& varMap(allVarMapIt->second); const VariablesMap::const_iterator varIt(varMap.find(UTF8ToWString(variableName))); if (varIt != varMap.end()) pos = varIt->second.first; } // if variable is not user-defined, check whether it is provided by this node if (pos == unsigned(-1)) { bool ok; pos = getVariablePos(nodeId, UTF8ToWString(variableName), &ok); if (!ok) { if (verbose) wcerr << "no variable " << UTF8ToWString(variableName) << " in node " << UTF8ToWString(nodeName); return false; } } return true; }
void HttpInterface::sendEvent(const std::string nodeName, const strings& args) { size_t eventPos; if (commonDefinitions.events.contains(UTF8ToWString(args[0]), &eventPos)) { // build event and emit UserMessage::DataVector data; for (size_t i=1; i<args.size(); ++i) data.push_back(atoi(args[i].c_str())); UserMessage userMessage(eventPos, data); userMessage.serialize(asebaStream); asebaStream->flush(); } else if (verbose) cerr << "sendEvent " << nodeName << ": no event " << args[0] << endl; }
std::pair<unsigned,unsigned> HttpInterface::sendGetVariables(const std::string nodeName, const strings& args) { unsigned nodePos, varPos; for (strings::const_iterator it(args.begin()); it != args.end(); ++it) { // get node id, variable position and length if (verbose) cerr << "getVariables " << nodeName << " " << *it; const bool exists(getNodeAndVarPos(nodeName, *it, nodePos, varPos)); if (!exists) continue; VariablesMap vm = allVariables[nodeName]; const unsigned length(vm[UTF8ToWString(*it)].second); if (verbose) cerr << " (" << nodePos << "," << varPos << "):" << length << "\n"; // send the message GetVariables getVariables(nodePos, varPos, length); getVariables.serialize(asebaStream); } asebaStream->flush(); return std::pair<unsigned,unsigned>(nodePos,varPos); // just last one }
bool SessionInfo::ProcessConfig(const string& config_json) { m_upgradeVersion.clear(); m_psk.clear(); m_sshPort = 0; m_sshUsername.clear(); m_sshPassword.clear(); m_sshHostKey.clear(); m_sshSessionID.clear(); m_sshObfuscatedPort = 0; m_sshObfuscatedKey.clear(); m_homepages.clear(); m_servers.clear(); m_pageViewRegexes.clear(); m_httpsRequestRegexes.clear(); m_preemptiveReconnectLifetimeMilliseconds = PREEMPTIVE_RECONNECT_LIFETIME_MILLISECONDS_DEFAULT; m_localHttpProxyPort = 0; m_localHttpsProxyPort = 0; m_localSocksProxyPort = 0; Json::Value config; Json::Reader reader; bool parsingSuccessful = reader.parse(config_json, config); if (!parsingSuccessful) { string fail = reader.getFormattedErrorMessages(); my_print(NOT_SENSITIVE, false, _T("%s:%d: Page view regex parse failed: %S"), __TFUNCTION__, __LINE__, fail.c_str()); return false; } try { // Homepages Json::Value homepages = config["homepages"]; for (Json::Value::ArrayIndex i = 0; i < homepages.size(); i++) { m_homepages.push_back(UTF8ToWString(homepages[i].asString())); } // Upgrade m_upgradeVersion = config.get("upgrade_client_version", "").asString(); // Servers Json::Value servers = config["encoded_server_list"]; for (Json::Value::ArrayIndex i = 0; i < servers.size(); i++) { m_servers.push_back(servers[i].asString()); } // SSH and OSSH values m_sshPort = config.get("ssh_port", 0).asInt(); m_sshUsername = config.get("ssh_username", "").asString(); m_sshPassword = config.get("ssh_password", "").asString(); m_sshHostKey = config.get("ssh_host_key", "").asString(); m_sshSessionID = config.get("ssh_session_id", "").asString(); m_sshObfuscatedPort = config.get("ssh_obfuscated_port", 0).asInt(); m_sshObfuscatedKey = config.get("ssh_obfuscated_key", "").asString(); // VPN PSK m_psk = config.get("l2tp_ipsec_psk", "").asString(); // Page view regexes Json::Value regexes = config["page_view_regexes"]; for (Json::Value::ArrayIndex i = 0; i < regexes.size(); i++) { RegexReplace rx_re; rx_re.regex = regex( regexes[i].get("regex", "").asString(), regex::ECMAScript | regex::icase | regex::optimize); rx_re.replace = regexes[i].get("replace", "").asString(); m_pageViewRegexes.push_back(rx_re); } // HTTPS request regexes regexes = config["https_request_regexes"]; for (Json::Value::ArrayIndex i = 0; i < regexes.size(); i++) { RegexReplace rx_re; rx_re.regex = regex( regexes[i].get("regex", "").asString(), regex::ECMAScript | regex::icase | regex::optimize); rx_re.replace = regexes[i].get("replace", "").asString(); m_httpsRequestRegexes.push_back(rx_re); } // Preemptive Reconnect Lifetime Milliseconds m_preemptiveReconnectLifetimeMilliseconds = (DWORD)config.get("preemptive_reconnect_lifetime_milliseconds", 0).asUInt(); // A zero value indicates that it should be disabled. } catch (exception& e) { my_print(NOT_SENSITIVE, false, _T("%s:%d: Config parse exception: %S"), __TFUNCTION__, __LINE__, e.what()); return false; } return true; }
void HttpInterface::evVariableOrEvent(HttpRequest* req, strings& args) { string nodeName(args[0]); size_t eventPos; if ( ! commonDefinitions.events.contains(UTF8ToWString(args[1]), &eventPos)) { // this is a variable if (req->method.find("POST") == 0 || args.size() >= 3) { // set variable value strings values; if (args.size() >= 3) values.assign(args.begin()+1, args.end()); else { // Parse POST form data values.push_back(args[1]); parse_json_form(req->content, values); } if (values.size() == 0) { finishResponse(req, 404, ""); if (verbose) cerr << req << " evVariableOrEevent 404 can't set variable " << args[0] << ", no values" << endl; return; } sendSetVariable(nodeName, values); finishResponse(req, 200, ""); if (verbose) cerr << req << " evVariableOrEevent 200 set variable " << values[0] << endl; } else { // get variable value strings values; values.assign(args.begin()+1, args.begin()+2); unsigned source, start; if ( ! getNodeAndVarPos(nodeName, values[0], source, start)) { finishResponse(req, 404, ""); if (verbose) cerr << req << " evVariableOrEevent 404 no such variable " << values[0] << endl; return; } sendGetVariables(nodeName, values); pendingVariables[std::make_pair(source,start)].insert(req); if (verbose) cerr << req << " evVariableOrEevent schedule var " << values[0] << "(" << source << "," << start << ") add " << req << " to subscribers" << endl; return; } } else { // this is an event // arguments are args 1..N strings data; data.push_back(args[1]); if (args.size() >= 3) for (size_t i=2; i<args.size(); ++i) data.push_back((args[i].c_str())); else if (req->method.find("POST") == 0) { // Parse POST form data parse_json_form(std::string(req->content, req->content.size()), data); } sendEvent(nodeName, data); finishResponse(req, 200, ""); // or perhaps {"return_value":null,"cmd":"sendEvent","name":nodeName}? return; } }