Core: Minecraft Server component does not work with SRV records.

Created on 26 Feb 2020  Â·  21Comments  Â·  Source: home-assistant/core

The problem

When using the config flow to define a new Minecraft Server, the component checks if it s a valid IP address, as here However, minecraft servers can also be connected to via SRV records, which do not point to an IP address on its own. Because of this, SRV based minecraft servers cannot be connected to.

Environment

  • Home Assistant release with the issue: 106.0
  • Last working Home Assistant release (if known): N/A
  • Operating environment (Hass.io/Docker/Windows/etc.): Docker
  • Integration causing this issue: Minecraft Server
  • Link to integration documentation on our website: https://www.home-assistant.io/integrations/minecraft_server/
minecraft_server

All 21 comments

Hey there @elmurato, mind taking a look at this issue as its been labeled with a integration (minecraft_server) you are listed as a codeowner for? Thanks!

@Adam-Ant Thanks for sharing the link about SRV records. I didn't know that Minecraft supports it. I'll have a look at this feature request. But in the meantime you should be able to connect with host and port, right?

The validation also does not accept an IP address directly, which seems very odd. Most people would expect to be able to put in an FQDN (hostname) or an IP.

The validation also does not accept an IP address directly, which seems very odd. Most people would expect to be able to put in an FQDN (hostname) or an IP.

IPv4 and IPv6 addresses should be supported. At least with my server it works either with the IPv4 address or with the dynamic DNS URL. The server needs to be turned on and has to be at least version 1.7. If it still doesn‘t work please create a new issue, as this one is a feature request for SRV record support. Thanks!

Edit: It also works with the hostname (server_hostname.local) for me.

@elmurato I tested different interfaces and I suspect the issue with SRV records and my issue is that line 59 stops the validation logic if there is an IP but no MAC resolved for the minecraft server, the else statement will never be reached. Only a server in the SAME subnet as the HA server well ever return a MAC address from the getmac.get_mac_address .

I suspect line 59

if ip_address is not None and mac_address is None:

should read

if ip_address is None and mac_address is None:

https://github.com/home-assistant/home-assistant/blob/df04fe32586d446e645819355c40df55e648f9c4/homeassistant/components/minecraft_server/config_flow.py#L58-L74

@bagobones No, this was done intentionally. If the user enters an IP address, the integration must get the MAC address, as this is required for the unique ID. IP addresses cannot be used for unique IDs, because they can change (actually this was a review finding which I fixed later).

I was not aware that getting the MAC address is not working if the server is in another subnet (not tested by me). But this would be then a limitation. You should be able to enter the local hostname or dynamic DNS URL together with the port though. In that case the MAC address is not required and not checked.

The SRV record support is another story, it‘s simply missing. If I understood correctly the config flow has to extract the URL and the port from the SRV record.

@Adam-Ant I found a way to extract host and port from a SRV record and it looks like working in my dev setup.
But I have one question: Can I expect that the port never changes in a SRV record? Or do I have to extract SRV record data dynamically everytime the integration tries to connect to the server?

As the IP and port both come from the same DNS record, I would consider the port as dynamic as the IP myself.

As the IP and port both come from the same DNS record, I would consider the port as dynamic as the IP myself.

Thanks, then I need to adapt the integration setup, too, and not only the config flow.

I do wonder if this would be a good opportunity to move away from using the MAC as a unique identifier. SRV record or not, at the moment any server outside the default gateway of the HA host won't work, SRV record or not. Similarly - two servers on the same IP, but different ports, would collide when using the MAC. Perhaps a hash of ip, port and server name would work better.?

---- On Fri, 28 Feb 2020 21:14:57 +0000 [email protected] wrote ----

As the IP and port both come from the same DNS record, I would consider the port as dynamic as the IP myself.

Thanks, then I need to adapt the integration setup, too, and not only the config flow.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

two servers on the same IP, but different ports, would collide when using the MAC

Correct and actually something I do with my setup via docker, each docker has the same host name / IP as the docker host but runs on a different port.

I would take the simple approach for the unique ID and ask the user to NAME the server, and do a check if there is already a server named that.. Most users will only have one server, but those with more advanced configs will want them to have separate names if they have more than one anyway.

This approach will not care if the user put in an IP, host and port, or domain and resolved a SRV record.

Similarly - two servers on the same IP, but different ports, would collide when using the MAC.

That's why the unique ID is either <host>-<port> or <mac_address>-<port>. So same IP with different ports should work. But in case of a SRV record, the port can be dynamic, as you said. That's why I have to think about the unique ID with SRV records (probably only the SRV address, as this is fixed).
I would try not to add one more user configuration in the config flow. If possible most of the stuff should be handled automatically.

@Adam-Ant Are you familiar with how custom_components work? If so, could you please test if this branch works now for you? My local tests were fine, but I would like to get your confirmation before I open the pull request. Thanks!

https://github.com/elmurato/home-assistant/tree/minecraft-server-srv-record/homeassistant/components/minecraft_server

No worries, I'll give it a test when I'm back at my computer tonight - hopefully around 9pm gmt

Hi - I am also getting the MAC address error, the server is on a different subnet and I am trying to connect using the IP. I have tested custom components before, but not sure how I can test this update, presuming it will now work without being able to get the MAC. I could manually input the MAC if there was somewhere to do so... Just a thought. Thanks.

Hi - I am also getting the MAC address error, the server is on a different sub-net and I am trying to connect using the IP.

As I said already above, using the local IP address requires the MAC address to be readable. But with a different sub-net this seams not to work. As an alternative you should be able to enter the local host name (like mcserver.local). In my network I can for example use mine-os.fritz.box. Using the host name doesn't require the MAC address, that's why it works that way.

I have tested custom components before, but not sure how I can test this update, presuming it will now work without being able to get the MAC. I could manually input the MAC if there was somewhere to do so... Just a thought. Thanks.

The branch linked above adds support for SRV records. It has nothing to do with MAC address not readable if the server is in a different sub-net.

Just tested the branch, and it is working great for me, with a few exceptions.

The logs output, as below, have a few errors and warnings in them, though I'm unsure if this is related to the SRV change.

2020-02-29 23:27:43 INFO (MainThread) [homeassistant.setup] Setting up minecraft_server
2020-02-29 23:27:43 INFO (MainThread) [homeassistant.setup] Setup of domain minecraft_server took 0.0 seconds.
2020-02-29 23:27:43 INFO (MainThread) [custom_components.minecraft_server] Connection to server (re-)established
2020-02-29 23:27:43 WARNING (MainThread) [custom_components.minecraft_server] Updating the server properties failed - OSError: Server did not respond with any information!
2020-02-29 23:27:43 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.minecraft_server
2020-02-29 23:27:43 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform minecraft_server.sensor: Platform not found (cannot import name 'TIME_MILLISECONDS' from 'homeassistant.const' (/usr/src/homeassistant/homeassistant/const.py)).
2020-02-29 23:27:43 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new binary_sensor.minecraft_server entity: binary_sensor.minecraft_server_status

The other issue is a bit of an edge case - a domain can have an A/CNAME record and an SRV record at the same time. For example, the domain hatest.adam-ant.co.uk (which is set up for this scenario with a running 2 minecraft servers - feel free to use it for testing!) has an SRV record pointing to port 32123, and another server running on 32321. After adding the SRV record server via the integration, adding the other valid server will not work, as HA complains that the host is already configured.

The logs output, as below, have a few errors and warnings in them, though I'm unsure if this is related to the SRV change.

Thanks so much for testing! I think the error with TIME_MILLISECONDS is due to this pull request:

Time units have been standardized throughout all integrations.

These changes are available in my DEV system (0.107.0.dev0), but not in your productive system.

Here are my logs:

2020-03-01 09:59:59 DEBUG (MainThread) [homeassistant.components.minecraft_server] 'hatest.adam-ant.co.uk' is a valid Minecraft SRV record (host=ts3host.cwgaming.co.uk, port=32123)
2020-03-01 09:59:59 DEBUG (MainThread) [homeassistant.components.minecraft_server] Creating server instance for 'Minecraft Server' (host='hatest.adam-ant.co.uk', port=25565)
2020-03-01 09:59:59 DEBUG (MainThread) [homeassistant.components.minecraft_server] 'hatest.adam-ant.co.uk' is a valid Minecraft SRV record (host=ts3host.cwgaming.co.uk, port=32123)
2020-03-01 09:59:59 INFO (MainThread) [homeassistant.components.minecraft_server] Connection to server (re-)established
2020-03-01 10:00:00 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.minecraft_server
2020-03-01 10:00:00 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.minecraft_server
2020-03-01 10:00:00 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new binary_sensor.minecraft_server entity: binary_sensor.minecraft_server_status_5
2020-03-01 10:00:00 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new sensor.minecraft_server entity: sensor.minecraft_server_players_max_5
2020-03-01 10:00:00 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new sensor.minecraft_server entity: sensor.minecraft_server_latency_time_5
2020-03-01 10:00:00 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new sensor.minecraft_server entity: sensor.minecraft_server_protocol_version_5
2020-03-01 10:00:00 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new sensor.minecraft_server entity: sensor.minecraft_server_version_5
2020-03-01 10:00:00 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new sensor.minecraft_server entity: sensor.minecraft_server_players_online_5

So I would open the pull request then. But I think this one will go earliest to 0.107.

The other issue is a bit of an edge case - a domain can have an A/CNAME record and an SRV record at the same time. For example, the domain hatest.adam-ant.co.uk (which is set up for this scenario with a running 2 minecraft servers - feel free to use it for testing!) has an SRV record pointing to port 32123, and another server running on 32321. After adding the SRV record server via the integration, adding the other valid server will not work, as HA complains that the host is already configured.

Hmm... I didn't know that multiple servers are possible with the same domain. How does this even work in Minecraft? How do you choose in the game which server to pick (SRV or A/CNAME) with the same domain? Anyway this would require more changes and probably also a breaking change. Couldn't you just use different domains for the two servers? Like:

  • hatest1.adam-ant.co.uk
  • hatest2.adam-ant.co.uk

How does this even work in Minecraft? How do you choose in the game which server to pick (SRV or A/CNAME) with the same domain?

It follows a simple priority list - explicitly defined port on the end of the address, SRV record, default port (25565). That said, I do agree it's a change not worth the dev time, but I thought I'd make you aware nevertheless.

As I said already above, using the local IP address requires the MAC address to be readable. But with a different sub-net this seams not to work. As an alternative you should be able to enter the local host name (like mcserver.local). In my network I can for example use mine-os.fritz.box. Using the host name doesn't require the MAC address, that's why it works that way.

Thanks for the quick reply - but I have tried mchostname.local and it isn't liking that either (resolves fine). FWIW I am not using the default query port, so manually changing that.

Thanks for the quick reply - but I have tried mchostname.local and it isn't liking that either (resolves fine). FWIW I am not using the default query port, so manually changing that.

As this is another issue, can you please create a new one? Then we can have a look at it there. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Konstigt picture Konstigt  Â·  3Comments

TheZoker picture TheZoker  Â·  3Comments

sogeniusio picture sogeniusio  Â·  3Comments

sibbl picture sibbl  Â·  3Comments

arangates picture arangates  Â·  3Comments