Announcing Server in Lua.

Post Reply
User avatar
Lejo
Member
Posts: 718
Joined: Mon Oct 19, 2015 16:32
GitHub: Lejo1
In-game: Lejo

Announcing Server in Lua.

by Lejo » Post

I tried to announce a Server with a Lua mod.
I always get the Error:

Code: Select all

2018-03-21 17:11:27: ERROR[CurlFetch]: servers.minetest.net/announce not found (HTTP response code said error) (response code 400)
I'm not realy sure how I have to send the clients_names, but without I get the same Error.
My Code I tried to use is:

Code: Select all

local httpapi = minetest.request_http_api()
local started = false
local function sendAnnounce(action, clients_names)
	local aa_names = {"start", "update", "delete"}
	local server = {}
	server["action"] = aa_names[action]
	server["port"] = tonumber(minetest.setting_get("port"))
	server["address"] = minetest.setting_get("server_address")
	if (action ~= 3) then
		server["name"]         = minetest.setting_get("server_name")
		server["description"]  = minetest.setting_get("server_description")
		server["version"]      = minetest.get_version().string
		server["url"]          = minetest.setting_get("server_url")
		server["creative"]     = minetest.setting_getbool("creative_mode")
		server["damage"]       = minetest.setting_getbool("enable_damage")
		server["password"]     = minetest.setting_getbool("disallow_empty_password")
		server["pvp"]          = minetest.setting_getbool("enable_pvp")
		server["uptime"]       = minetest.get_server_uptime()
		server["game_time"]    = minetest.get_gametime()
		server["clients"]      = #clients_names
		server["clients_max"]  = tonumber(minetest.setting_get("max_users"))
		--server["clients_list"] = ""
		for _, name in pairs(clients_names) do
			if server["clients_list"] == "" then
				server["clients_list"] = name
			else server["clients_list"] = server["clients_list"].." "..name
			end
		end
		server["gameid"] = "minetest"
	end

	if (action == 1) then
		server["privs"] = minetest.setting_get("default_privs")
	end
	local fetch_request = {}
	local json = minetest.write_json(server)
	fetch_request.url = minetest.setting_get("serverlist_url").."/announce"
  fetch_request.post_fields = {}
	fetch_request.post_fields["json"] = json
	fetch_request.multipart = true
	httpapi.fetch_async(fetch_request)
end

local function update_serverlist()
	local names = {}
  for _, player in ipairs(minetest.get_connected_players()) do
    local name = player:get_player_name()
    table.insert(names, name)
  end
   if started == true then
  sendAnnounce(2, names)
	minetest.after(180, function() update_serverlist() end)
  else started = true
   sendAnnounce(1, namens)
   minetest.after(180, function() update_serverlist() end)
   end
end
update_serverlist()
Based on:https://github.com/minetest/minetest/bl ... t.cpp#L194
Can anyone help me?

User avatar
crazyR
Member
Posts: 60
Joined: Thu Jun 19, 2014 14:41
Location: uk

Re: Announcing Server in Lua.

by crazyR » Post

Why would you be trying todo this? The only reason I can see you wanting to do this is to spoof the server list?
I would advise against that.

User avatar
Lejo
Member
Posts: 718
Joined: Mon Oct 19, 2015 16:32
GitHub: Lejo1
In-game: Lejo

Re: Announcing Server in Lua.

by Lejo » Post

My server Announcing is often buggy and don't work at all: viewtopic.php?f=6&t=18687
So I tried to fix it with a lua mod.

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: Announcing Server in Lua.

by Krock » Post

1) clients_list is an array, not a string.
2) 5th last line of your pasted code: "sendAnnounce(1, namens)" -> should be "names"

Code: Select all

server["clients_list"] = clients_names
If our JSON API works correctly (function read_json_value in c_content.cpp), this will work just fine.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Lejo
Member
Posts: 718
Joined: Mon Oct 19, 2015 16:32
GitHub: Lejo1
In-game: Lejo

Re: Announcing Server in Lua.

by Lejo » Post

Krock wrote:1) clients_list is an array, not a string.
2) 5th last line of your pasted code: "sendAnnounce(1, namens)" -> should be "names"

Code: Select all

server["clients_list"] = clients_names
If our JSON API works correctly (function read_json_value in c_content.cpp), this will work just fine.
Thanks for your help, but I read this:
You can not mix string and integer keys.
So should I use for client_list the names as key and as value or only as key and true as value?
Edit: you are right.
Last edited by Lejo on Tue May 15, 2018 13:31, edited 1 time in total.

User avatar
Lejo
Member
Posts: 718
Joined: Mon Oct 19, 2015 16:32
GitHub: Lejo1
In-game: Lejo

Re: Announcing Server in Lua.

by Lejo » Post

I also tried it without this clients_list, which should also work:https://github.com/minetest/master-serv ... er.py#L184
But I got the same Error.

User avatar
Lejo
Member
Posts: 718
Joined: Mon Oct 19, 2015 16:32
GitHub: Lejo1
In-game: Lejo

Re: Announcing Server in Lua.

by Lejo » Post

Krock wrote:If our JSON API works correctly (function read_json_value in c_content.cpp), this will work just fine.
Here is my minetest.write_json output:
Image
I compared it with servers.minetest.net/list
It looks correct.

nOOb3167
New member
Posts: 1
Joined: Fri May 18, 2018 01:20
GitHub: nOOb3167

Re: Announcing Server in Lua.

by nOOb3167 » Post

Change was requested to the master-server to allow for this functionality.
https://github.com/minetest/master-server/pull/23

Once merged, such code should start working:

Code: Select all

local httpapi = minetest.request_http_api()

local function itoa(x)
   return tostring(math.floor(x))
end

local function sendAnnounce(clntlist)
   local server = {}

   server["action"] = "start"
   server["port"] = itoa(tonumber(minetest.setting_get("port")))
   server["address"] = minetest.setting_get("server_address")
      server["name"]         = minetest.setting_get("server_name")
      server["description"]  = minetest.setting_get("server_description")
      server["version"]      = minetest.get_version().string
      server["url"]          = minetest.setting_get("server_url")
      server["creative"]     = minetest.setting_getbool("creative_mode")
      server["damage"]       = minetest.setting_getbool("enable_damage")
      server["password"]     = minetest.setting_getbool("disallow_empty_password")
      server["pvp"]          = minetest.setting_getbool("enable_pvp")
      server["uptime"]       = itoa(minetest.get_server_uptime())
      server["game_time"]    = itoa(minetest.get_gametime() or 0)
      server["clients"]      = itoa(#clntlist)
      server["clients_max"]  = itoa(minetest.setting_get("max_users"))
      server["clients_list"] = clntlist
      server["gameid"] = "minetest"

   server["privs"] = minetest.setting_get("default_privs")

   local fetch_request = {}
   local json = minetest.write_json(server)
   fetch_request.url = minetest.setting_get("serverlist_url").."/announce"
   fetch_request.post_data = {}
   fetch_request.post_data["json"] = json
   fetch_request.multipart = true
   httpapi.fetch_async(fetch_request)
end

clntlist = { "hello" }

sendAnnounce(clntlist)
The main change is to send number values as strings (ex. tostring(math.floor(1234))).
You made some other mistakes, such as attempting to set fetch_request.post_fields instead of fetch_request.post_data.

The other problem i see in the output you posted is "address":"192.168.181.47" .
This is a local / LAN ip address.

After you send to server.minetest.net/announce, the master-server has to contact your address (and port) to check if your server is running. Only then will it be added to the list.
This server check happens asynchronously, after server.minetest.net/announce responds with success.

Even after you fix everything else (ie the PR i linked gets merged, and you use code similar to what i posted),
if a local / LAN ip such as 192.168.181.47 is send as address, the master-server response will indicate success, but fail silently before it adds your server to the list.

You will need to send a public ip address (and port), which accepts connections through firewalls / NAT.

User avatar
Lejo
Member
Posts: 718
Joined: Mon Oct 19, 2015 16:32
GitHub: Lejo1
In-game: Lejo

Re: Announcing Server in Lua.

by Lejo » Post

Thanks a lot for your help.
I used post_flields because c++ announce does it so, but i will test both.
The ip is only local, because it does a Screenshot at home, on my server it is public.
I hope the PR will be merged.

Post Reply

Who is online

Users browsing this forum: talamh and 17 guests