GeyserMC / MCProtocolLib

A library for communication with a Minecraft client/server.
MIT License
724 stars 201 forks source link

Android: Connection timed out when connecting to external Server #486

Closed H4xX0r1337 closed 4 years ago

H4xX0r1337 commented 5 years ago

MCProtocolLib works fine on Android when connecting to a server running in the local wifi network. However, when connecting to external server ips the connection always times out. I tried multiple servers, all are working fine in the original client. Those servers are all compatible with 1.14.4. I can even use PickaxeChat to connect to that server on the same phone without any issues. I added the internet connection to manifest.

H4xX0r1337 commented 5 years ago

@Steveice10 Yes, I did. It works perfectly fine on my PC with MCProtocolLib. I'm using the same servers, the same SessionAdapter and the same account.

H4xX0r1337 commented 5 years ago

@Steveice10 The only thing that might interfere with my connection in any way is my Adblocker, but it works fine with PickaxeChat. I disabled the adblocker and still get the same issue.
It gets even stranger. One of the servers I tried yesterday now kicks me for "io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: SB-Hub.com/104.128.57.82:25565".
My guess is that there is either A) something is horribly wrong with my phone. I'll check it on another and keep you updated or B) I'm missing some permission or system restriction preventing me from accessing external servers.

H4xX0r1337 commented 5 years ago

@Steveice10 Update: Same issue on my other phone. Both run pie tho.

H4xX0r1337 commented 5 years ago

@Steveice10 Update 2: I found a single cracked server that works fine on my phone: "playmc.games".

0-x-2-2 commented 5 years ago

sounds like a protocol encryption issue 🤔

H4xX0r1337 commented 5 years ago

@Steveice10 Any update? Have you tried it on Android yet?

H4xX0r1337 commented 5 years ago

@Steveice10 I think I found the root cause. It wasn't because premium, cracked, remote or local. The server(s) I'm trying to connect to uses IPv6. Now I could find that most servers are actually working, because IPv4 is way more common.

Edit: The server domain (skyblock.net) is also a webpage. I guess that it might be important for the timeout.

Edit 2: I FOUND THE ISSUE! My PC was able to resolve the correct IP address for minecraft, which also worked on my phone. However MCProtocolLib on Android resolves an incorrect IPv6 address (probably the website)

H4xX0r1337 commented 5 years ago

@Steveice10 I forgot to tell you: The actual server IP is IPv4 (the website was IPv6). I thought the server was IPv6 because McProtocolLib looked up the wrong address (but only on my phone).

Sorry if I'm repeating myself, but I want to get of any misunderstanding.

0-x-2-2 commented 5 years ago

Add this to your entry point.

System.setProperty("java.net.preferIPv4Stack", "true");

Afaik the official Minecraft client also does this.

H4xX0r1337 commented 5 years ago

@o-x-2-2 wouldnt that also affect the rest of the application?

0-x-2-2 commented 5 years ago

@o-x-2-2 wouldnt that also affect the rest of the application?

Correct but it should not be an issue at all.

H4xX0r1337 commented 5 years ago

@0-x-2-2 Bad practice tho. Better explicitly specify when making the request. Also, when I use cmd to get the IPv4 from skyblock.net it returns an invalid IPv4 address. Minecraft must do something else different.

0-x-2-2 commented 5 years ago

Anything that requires IPv6 will still work in fact as I said before the official Minecraft client and old Java Launcher also does this. You only get this issue because it is trying to use the IPv6 IP that they don't have Minecraft on because Java Edition does not support IPv6 natively.

H4xX0r1337 commented 5 years ago

@0-x-2-2 Still, it is a bad idea for a library to affect the rest of the application.

0-x-2-2 commented 5 years ago

@0-x-2-2 Still, it is a bad idea for a library to affect the rest of the application.

I'm not saying the library should do this I am saying you should do this. This is also the functionality of the regular Minecraft client.

H4xX0r1337 commented 5 years ago

@0-x-2-2 Oh, thats what you mean. I'll try.

H4xX0r1337 commented 5 years ago

@0-x-2-2 Sorry for letting you wait that long, but I wanted to let you know that preferIPv4Stack doesn't make any difference.

H4xX0r1337 commented 5 years ago

@Steveice10 The issue is still there with the latest commit. A combination with preferIPv4stack also doesn't work.

H4xX0r1337 commented 4 years ago

@Steveice10 Sorry I completely forgot about this issue. I was using a numeric IP as a workaround. I just checked the latest version (1.15-1) and the issue is still there. The phone resolves the wrong IP, probably from the website.

H4xX0r1337 commented 4 years ago

@Steveice10 Now I'm being disconnected because of this: "io.netty.handler.codec.DecoderException: java.lang.UnsupportedOperationException" Edit: Exact stacktrace: https://pastebin.com/6iWwURST

H4xX0r1337 commented 4 years ago

@Steveice10 It's working now. HUGE thanks for fixing this.

H4xX0r1337 commented 4 years ago

@Steveice10 This bug reappeared after you added the packetError event. The connections always times out, the remote address of the session is null. By using the direct ip it works. Also other domains that only have a minecraft server and not a web server in addition seem to work too.

H4xX0r1337 commented 4 years ago

Cannot reproduce. Using the latest commits of both PacketLib and MCProtocolLib, I can connect to the server tested on, "sb-hub.com", just fine.

@Steveice10 tested on android? I only tested "skyblock.net"

H4xX0r1337 commented 4 years ago

I just pushed a commit to PacketLib to add a flag to print DNS resolution errors, so give that a try and let me know what stack trace you get.

@Steveice10 I did session.setFlag(BuiltinFlags.PRINT_DNS_ERRORS, true) but nothing was printed. It just times out and it's the remote address is null at the point i check it

H4xX0r1337 commented 4 years ago

@Steveice10 05-30 23:59:49.028 11512 11702 I System.out: [PacketLib] No SRV records found.

H4xX0r1337 commented 4 years ago

@Steveice10 It still resolves to null on my phone. (Latest MCProtocolLib + PacketLib)

H4xX0r1337 commented 4 years ago

@Steveice10 It just doesn't work on my phone :/ Have you tried mobile data too? Do I maybe have to update MCAuthLib manually?

H4xX0r1337 commented 4 years ago

@Steveice10 this might be helpful https://stackoverflow.com/questions/56635898/dnsjava-library-return-no-records-found

H4xX0r1337 commented 4 years ago

@Steveice10 This workaround works on my phone and dns-java. Not sure how you would do that with netty. System.setProperty("dns.server", "8.8.8.8")

H4xX0r1337 commented 4 years ago

@Steveice10 maybe provide a flag to set the dns server? The property thing only works for dns-java

H4xX0r1337 commented 4 years ago

@Steveice10 This is everything your apk outputs on my phone: https://pastebin.com/n64xu1SF. The only thing which makes sense is 06-17 22:35:34.672 19796 19843 W erAddressStreamProvider: Default DNS servers: [/2001:4860:4860::8888:53, /2001:4860:4860::8844:53] (Google Public DNS as a fallback). It doesn't output anything else.

My DNS settings are on the default and I also tried to manually change them with no success.

H4xX0r1337 commented 4 years ago

@Steveice10 https://pastebin.com/kkjJ2MMf now there seems to be a stacktrace.

H4xX0r1337 commented 4 years ago

@Steveice10 That changed nothing for me, which android version do you use? Because apparantly the issue was introduced with 8.0 (https://developer.android.com/about/versions/oreo/android-8.0-changes#o-pri)

H4xX0r1337 commented 4 years ago

@Steveice10 I really don't know what's happening here. I have this issue too with another phone on Android 9.0 (my main one is 10.0). I only use mobile data but I doubt it would have an affect.

H4xX0r1337 commented 4 years ago

@Steveice10 could you try to add this line in your example? System.setProperty("dns.server", "2001:4860:4860::8888")
Wondering if your device can resolve an explicit IPv6 DNS or not

H4xX0r1337 commented 4 years ago

@Steveice10 I thought the 1.15 release of MCProtocolLib would use dns-java instead of Netty's lookup

H4xX0r1337 commented 4 years ago

@Steveice10 ah i thought it was replaced in a later commit. I made a test which sets the DNS via dns-java (for testing, idk how to do that with netty). Here's the apk if you want to test it. https://workupload.com/file/3x7HbJPps9N
Since 2 of my devices have this issue I suspect it might be my network. I'll test it with a real wifi connection later.

H4xX0r1337 commented 4 years ago

@Steveice10 It just won't work on my devices here. And for some reason I can't use the number IP because that will kick me and demand to join via the official domain. I also tried to use reflection to change the domain but that didn't work out either.