Beispiel #1
0
bool Notifier::unregisterServer()
{
    STACKLOG;
	if(!wasregistered || !advertised)
		return false;
	char unregurl[1024];
	sprintf(unregurl, "%s/unregister/?challenge=%s", REPO_URLPREFIX, challenge);
	if (HTTPGET(unregurl) < 0)
		return false;
	advertised = false;

	return getResponse() == "ok";
}
void __fastcall tWWWClientSocket::Execute()
{
  AnsiString s;
  bool hEnd = false;
  char c;
  headercount=0;
  bool ok;

  WS->Clients++;
  while (! hEnd)
  {
     hEnd=recv(socket,&c,1,0)!=1;
     if (headercount>50) hEnd=true;
     if (c!=13 && c!=10) { s=s+c; } else
     {
       if (c==10)
       {
         if (s=="")
         {
           hEnd=true;
         } else {
           HTTPHeaders[headercount++]=s;
         }
         s="";
       }
     }
  }
  if (HTTPHeaders[0].SubString(0,4).UpperCase()=="GET ")
  {
    ok=true;
    HTTPGET();
  }
  if (!ok)
  {
    send(socket, "HTTP/1.1 403 Forbidden\r\n\r\n", 23, 0);
    send(socket, "I DON'T LIKE YOU\r\n\r\n", 20, 0);
  }
  {
    BYTE i = 1;
    setsockopt(socket, AF_INET, SO_LINGER, (char*)&i, 1);
  }
  shutdown(socket,SD_SEND);
  Sleep(100);
  closesocket(socket);
  {
    int i;
    for (i=0;i<100;i++) HTTPHeaders[i]="";
  }
  WS->Clients--;
}
Beispiel #3
0
int UserAuth::sendUserEvent(std::string user_token, std::string type, std::string arg1, std::string arg2)
{
    STACKLOG;
	
	// Only contact the master server if we are allowed to do so
	if(trustlevel<=1) return 0;
	
	char url[1024];
	sprintf(url, "%s/userevent/?v=0&sh=%s&h=%s&t=%s&a1=%s&a2=%s", REPO_URLPREFIX, challenge.c_str(), user_token.c_str(), type.c_str(), arg1.c_str(), arg2.c_str());
	HttpMsg resp;
	if (HTTPGET(url, resp) < 0)
		return -1;

	std::string body = resp.getBody();
	Logger::log(LOG_DEBUG,"UserEvent reply: " + body);

	return (body!="ok");
}
int UserAuth::resolve(std::string user_token, UTFString &user_nick, int clientid)
{
    STACKLOG;

	// There's alot of other info in the user token variable, but we don't need it here.
	// We only need the first 40 characters = the actual (encoded) token.
	user_token = user_token.substr(0,40);
	
	//check cache first
	if(cache.find(user_token) != cache.end())
	{
		// cache hit!
		user_nick = cache[user_token].second;
		return cache[user_token].first;
	}
	
	// initialize the authlevel on none = normal user
	int authlevel = AUTH_NONE;

	// Only contact the master-server if we're allowed to do so
	if(trustlevel>1)
	{
		std::string msg = "";
		UTFString resultNick = L"";
	
		// not found in cache or local_auth, get auth from masterserver
		char url[2048];
		sprintf(url, "%s/authuser_utf8/?c=%s&t=%s&u=%s", REPO_URLPREFIX, challenge.c_str(), user_token.c_str(), user_nick.asUTF8_c_str());
		Logger::log(LOG_DEBUG, "UserAuth query to server: " + std::string(url));
		HttpMsg resp;
		if (HTTPGET(url, resp) < 0)
		{
			Logger::log(LOG_ERROR, "UserAuth resolve query result empty");
			return AUTH_NONE;
		}

		std::string body = resp.getBody();
		Logger::log(LOG_DEBUG,"UserAuth reply: " + body);
		
		std::vector<std::string> args;
		strict_tokenize(body, args, "\t");

		if(args.size() < 2)
		{
			Logger::log(LOG_INFO,"UserAuth: invalid return value from server: " + body);
			return AUTH_NONE;
		}
		
		authlevel = atoi(args[0].c_str());
		resultNick = tryConvertUTF(args[1].c_str());
		if(args.size() > 2)
			msg = args[2];

		// debug output the auth status
		UTFString authst;
		if(authlevel & AUTH_NONE)   authst = authst + "N";
		if(authlevel & AUTH_ADMIN)  authst = authst + "A";
		if(authlevel & AUTH_MOD)    authst = authst + "M";
		if(authlevel & AUTH_RANKED) authst = authst + "R";
		if(authlevel & AUTH_BOT)    authst = authst + "B";
		if(authst.empty()) authst = "(none)";
		Logger::log(LOG_DEBUG, UTFString("User Auth Result: ") + authst + " / " + (resultNick) + " / " + tryConvertUTF(msg.c_str()));

		if(resultNick == L"error" || resultNick == L"reserved" || resultNick == L"notranked")
		{
			resultNick = widen(getNewPlayernameByID(clientid));
			Logger::log(LOG_DEBUG, UTFString("got new random name for player: ") + resultNick);
			authlevel = AUTH_NONE;
		}

		// returned name valid, use it
		user_nick = resultNick;
	}
	
	//then check for overrides in the authorizations file (server admins, etc)
	if(local_auth.find(user_token) != local_auth.end())
	{
		// local auth hit!
		// the stored nickname can be empty if no nickname is specified.
		if(!local_auth[user_token].second.empty())
			user_nick = local_auth[user_token].second;
		authlevel |= local_auth[user_token].first;
	}

	// cache result if ranked or higher
	if(authlevel > AUTH_NONE)
	{
		user_auth_pair_t p;
		p.first = authlevel;
		p.second = user_nick;
		
		Logger::log(LOG_DEBUG, "adding entry to remote auth cache, size: %d",  cache.size());
		cache[user_token] = p;
	}

	return authlevel;
}
Beispiel #5
0
int UserAuth::resolve(std::string user_token, std::string &user_nick)
{
    STACKLOG;

	// There's alot of other info in the user token variable, but we don't need it here.
	// We only need the first 40 characters = the actual (encoded) token.
	user_token = user_token.substr(0,40);
	
	//check cache first
	if(cache.find(user_token) != cache.end())
	{
		// cache hit!
		user_nick = cache[user_token].second;
		return cache[user_token].first;
	}
	
	// initialize the authlevel on none = normal user
	int authlevel = AUTH_NONE;

	// Only contact the master-server if we're allowed to do so
	if(trustlevel>1)
	{
		// not found in cache or local_auth, get auth from masterserver
		char url[1024];
		sprintf(url, "%s/authuser/?c=%s&t=%s", REPO_URLPREFIX, challenge.c_str(), user_token.c_str());
		HttpMsg resp;
		if (HTTPGET(url, resp) < 0)
			return -1;

		std::string body = resp.getBody();
		Logger::log(LOG_DEBUG,"UserAuth reply: " + body);
		
		std::vector<std::string> args;
		strict_tokenize(body, args, "\t");

		if(args.size() != 2)
		{
			Logger::log(LOG_INFO,"UserAuth: invalid return value from server: " + body);
			return AUTH_NONE;
		}
		
		authlevel = atoi(args[0].c_str());
		user_nick = args[1];
	}
	
	//then check for overrides in the authorizations file (server admins, etc)
	if(local_auth.find(user_token) != local_auth.end())
	{
		// local auth hit!
		// the stored nickname can be empty if no nickname is specified.
		if(!local_auth[user_token].second.empty())
			user_nick = local_auth[user_token].second;
		authlevel |= local_auth[user_token].first;
	}

	// debug output the auth status
	char authst[10] = "";
	if(authlevel & AUTH_ADMIN) strcat(authst, "A");
	if(authlevel & AUTH_MOD) strcat(authst, "M");
	if(authlevel & AUTH_RANKED) strcat(authst, "R");
	if(authlevel & AUTH_BOT) strcat(authst, "B");
	if(authlevel != AUTH_NONE) Logger::log(LOG_INFO,"User Auth Flags: " + std::string(authst));

	// cache result
	std::pair< int, std::string > p;
	p.first = authlevel;
	p.second = user_nick;

	Logger::log(LOG_DEBUG, "adding entry to remote auth cache, size: %d",  cache.size());
	cache[user_token] = p;

	return authlevel;
}
Beispiel #6
0
/**
 * @brief
 */
bool Notifier::registerServer()
{
    STACKLOG;
	char regurl[1024];
	int responseformat = 2;
	sprintf(regurl, "%s/register/?name=%s&description=%s&ip=%s&port=%i&"
			"terrainname=%s&maxclients=%i&version=%s&pw=%d&format=%d", 
		REPO_URLPREFIX,
		Config::getServerName().c_str(),
		"",
		Config::getIPAddr().c_str(), 
		Config::getListenPort(),
		Config::getTerrainName().c_str(),
		Config::getMaxClients(),
		RORNET_VERSION,
		Config::isPublic(),
		responseformat);
	
	// format = 2 will result in different response on registration format.
	Logger::log(LOG_DEBUG, "%s%s", "sending registration request: ", regurl);
	Logger::log(LOG_INFO, "Trying to register at Master Server ... (this can take some "
			"seconds as your server is being checked by the Master server)");
	if (HTTPGET(regurl) < 0)
		return false;


	if(responseformat==1)
	{
		// old format
		std::string body = getResponse().getBody();
		if(body.find("error") != std::string::npos || body.length() < 40)
		{
			// print out the error.
			Logger::log(LOG_ERROR, "got that as registration response: %s", body.c_str());
			Logger::log(LOG_ERROR, "!!! Server is NOT registered at the Master server !!!");
			wasregistered=false;
			return false;
		}
		else
		{
			Logger::log(LOG_DEBUG, "got that as registration response: %s", body.c_str());
			
			memset(&challenge, 0, 40);
			strncpy( challenge, body.c_str(), 40 );
			Logger::log(LOG_INFO,"Server is registered at the Master server.");
			wasregistered=true;
			return true;
		}
	} else if (responseformat == 2)
	{
		// new format
		std::vector<std::string> lines = getResponse().getBodyLines();
		if(lines.size() < 2)
		{
			Logger::log(LOG_ERROR, "got wrong server response upon registration: only %d lines instead of minimal 2", lines.size());
			wasregistered=false;
			return false;
		}
		
		// zero line = response to registration, 'ok' or 'error'
		std::string status_short = lines[0];
		// status message - an error message
		std::string status_long = lines[1];
	
		if(status_short == "ok")
		{
		}else if(status_short == "error")
		{
			Logger::log(LOG_ERROR, "error upon registration: %s: %s", status_short.c_str(), status_long.c_str());
			Logger::log(LOG_ERROR, "!!! Server is NOT registered at the Master server !!!");
			wasregistered=false;
			return false;
		}

		// server challenge
		std::string challenge_response = lines[2].c_str();
		// server trustness level
		if(lines.size() > 2)
			trustlevel = atoi(lines[3].c_str());

		Logger::log(LOG_DEBUG, "%s: %s", status_short.c_str(), status_long.c_str());
		Logger::log(LOG_DEBUG, "this server got trustlevel %d", trustlevel);
		
		//copy data
		memset(&challenge, 0, 40);
		strncpy( challenge, challenge_response.c_str(), 40 );
		Logger::log(LOG_INFO,"Server is registered at the Master server.");
		wasregistered=true;
		return true;
	}
	return false;
}