bool Client::Register() { Json::Value data(Json::objectValue); data["ip"] = Config::getIPAddr(); data["port"] = Config::getListenPort(); data["name"] = Config::getServerName(); data["terrain-name"] = Config::getTerrainName(); data["max-clients"] = Config::getMaxClients(); data["version"] = RORNET_VERSION; data["use-password"] = Config::isPublic(); m_server_path = "/" + Config::GetServerlistPath() + "/server-list"; Logger::Log(LOG_INFO, "Attempting to register on serverlist (%s)", m_server_path.c_str()); Http::Response response; int result_code = this->HttpRequest(Http::METHOD_POST, data.toStyledString().c_str(), &response); if (result_code < 0) { Logger::Log(LOG_ERROR, "Registration failed, result code: %d", result_code); return false; } else if (result_code != 200) { Logger::Log(LOG_INFO, "Registration failed, response code: HTTP %d, body: %s", result_code, response.GetBody().c_str()); return false; } Json::Value root; Json::Reader reader; if (!reader.parse(response.GetBody().c_str(), root)) { Logger::Log(LOG_ERROR, "Registration failed, invalid server response (JSON parsing failed)"); Logger::Log(LOG_DEBUG, "Raw response: %s", response.GetBody().c_str()); return false; } Json::Value trust_level = root["verified-level"]; Json::Value challenge = root["challenge"]; if (!root.isObject() || !trust_level.isNumeric() || !challenge.isString()) { Logger::Log(LOG_ERROR, "Registration failed, incorrect response from server"); Logger::Log(LOG_DEBUG, "Raw response: %s", response.GetBody().c_str()); return false; } m_token = challenge.asString(); m_trust_level = trust_level.asInt(); m_is_registered = true; return true; }
bool RetrievePublicIp() { char url[300] = ""; sprintf(url, "/%s/get-public-ip", Config::GetServerlistPath().c_str()); Http::Response response; int result_code = Http::Request(Http::METHOD_GET, Config::GetServerlistHostC(), url, "application/json", "", &response); if (result_code < 0) { Logger::Log(LOG_ERROR, "Failed to retrieve public IP address"); return false; } Config::setIPAddr(response.GetBody()); return true; }
IpAddress IpAddress::GetPublicAddress(Time timeout) { // The trick here is more complicated, because the only way // to get our public IP address is to get it from a distant computer. // Here we get the web page from http://www.sfml-dev.org/ip-provider.php // and parse the result to extract our IP address // (not very hard: the web page contains only our IP address). Http server("www.sfml-dev.org"); Http::Request request("/ip-provider.php", Http::Request::Get); Http::Response page = server.SendRequest(request, timeout); if (page.GetStatus() == Http::Response::Ok) return IpAddress(page.GetBody()); // Something failed: return an invalid address return IpAddress(); }