SmartlyDressedGames / Unturned-3.x-Community

Community portion of the Unturned-3.x repo. If you have access to the source code you can find it here:
https://github.com/SmartlyDressedGames/Unturned-3.x/
83 stars 17 forks source link

Parse input IP as address before using DNS lookup #4413

Closed sunnamed434 closed 1 week ago

sunnamed434 commented 1 month ago

The video below shows the problem, sometimes Unturned is just laggy but a few times it lead me to the whole game crash. Idk what causes this crash, IP: 156.236.84.205​:27070 https://streamable.com/y82oph

sunnamed434 commented 1 month ago

Client logs:

[2024-04-04 10:22:51] Caught exception while resolving "156.236.84.205​" for connect:
[2024-04-04 10:22:51] Could not resolve host '156.236.84.205​'
[2024-04-04 10:22:51] at System.Net.Dns.Error_11001 (System.String hostName) [0x00015] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostByName (System.String hostName) [0x00021] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00052] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00056] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at SDG.Unturned.MenuPlayConnectUI.onClickedConnectButton (SDG.Unturned.ISleekElement button) [0x00091] in <b71e6c009f434644860b1b465ded4e87>:0
[2024-04-04 10:22:51] Unable to parse IP "156.236.84.205​"
[2024-04-04 10:22:51] Long delay between Updates: 11.46236s
[2024-04-04 10:23:06] Caught exception while resolving "156.236.84.205​" for connect:
[2024-04-04 10:23:06] Could not resolve host '156.236.84.205​'
[2024-04-04 10:23:06] at System.Net.Dns.Error_11001 (System.String hostName) [0x00015] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostByName (System.String hostName) [0x00021] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00052] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00056] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at SDG.Unturned.MenuPlayConnectUI.onClickedConnectButton (SDG.Unturned.ISleekElement button) [0x00091] in <b71e6c009f434644860b1b465ded4e87>:0
[2024-04-04 10:23:06] Unable to parse IP "156.236.84.205​"
[2024-04-04 10:23:06] Long delay between Updates: 11.06443s
[2024-04-04 10:29:26] Caught exception while resolving "156.236.84.205​" for connect:
[2024-04-04 10:29:26] Could not resolve host '156.236.84.205​'
[2024-04-04 10:29:26] at System.Net.Dns.Error_11001 (System.String hostName) [0x00015] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostByName (System.String hostName) [0x00021] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00052] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00056] in <647615f3c1e6423a897a1a8f71c1fd3c>:0 
  at SDG.Unturned.MenuPlayConnectUI.onClickedConnectButton (SDG.Unturned.ISleekElement button) [0x00091] in <b71e6c009f434644860b1b465ded4e87>:0
[2024-04-04 10:29:26] Unable to parse IP "156.236.84.205​"
[2024-04-04 10:29:26] Long delay between Updates: 11.07846s
sunnamed434 commented 1 month ago

IP from the discord server where I copied this IP, seems like I copied the also, but it doesn't show in the menu IP/Port inputfield 🪨 P9nda's Unturned KOTH [Rust] (Wiped) IP: 156.236.84.205​ ​ ─ ​ ​Port: 27070​ ​ ─ ​ ​Players: 3

SDGNelson commented 1 month ago

On my PC resolving that address is immediate, but the underlying issue is that the game calls Dns.GetHostAddresses on the main thread and it blocks until it gets a result. It should probably try parsing as IPv4 before calling GetHostAddresses instead of just afterward. I suspect the reason the game closed is if the Windows "application is not responding" popup appears and gets clicked to close instead of waiting - looks like the timeout was around 11 seconds in your case.

As a solution for the meantime, I would suggest investigating why your DNS response was so slow.

sunnamed434 commented 1 month ago

After I copied IP again all went just fine, without lags, but I guess the problem is in invalid character inputted into the ip/port

CyberAndrii commented 1 month ago

Uhh I don't think you know what DNS is... It is used to resolve IP addresses from domain names. If you make the game to parse as IPv4 before calling GetHostAddresses, then calling it will make no sense because you already have the IP address. But in that case it won't be possible to connect via domain names anymore because it won't be able to parse the IPv4 address from the domain.

On my PC resolving that address is immediate

That's likely because you copied a valid IP address so it doesn't need to resolve anything. In sunnamed's case there's \u200b (zero-width space) character at the end. You can copy it from the logs by selecting both the IP and " after the IP then remove the ".

SDGNelson commented 1 month ago

Uhh I don't think you know what DNS is... It is used to resolve IP addresses from domain names. If you make the game to parse as IPv4 before calling GetHostAddresses, then calling it will make no sense because you already have the IP address. But in that case it won't be possible to connect via domain names anymore because it won't be able to parse the IPv4 address from the domain.

Currently, the game checks whether the "Connect Address" text input is a server code or "localhost", and if not it proceeds to call Dns.GetHostAddresses. While some players do connect by domain name, a lot of the time this could be skipped by calling IPv4Address.TryParse first. If it IS a valid IPv4 address then it can proceed with that, and if not it can try resolving the name. I am well familiar with what DNS is.

That's likely because you copied a valid IP address so it doesn't need to resolve anything. In sunnamed's case there's \u200b (zero-width space) character at the end. You can copy it from the logs by selecting both the IP and " after the IP then remove the ".

Good catch about the zero-width character. I copied it without the quotation mark so I didn't get the space. The game does call string.Trim before parsing, but unfortunately doesn't remove that. It seems like an unlikely/infrequent case that players would be pasting IPs with zero-width characters, so maybe this post is a non-issue. The reason I mentioned that it was immediate in my case was to clarify that Dns.GetHostAddresses wasn't stalling for 10 seconds on my end the way it was on OP's, and I wondered if internally Windows was forwarding the perfectly valid address to the DNS server rather than parsing it - which is why I proposed bypassing it when the input is already valid.