pyCraft icon indicating copy to clipboard operation
pyCraft copied to clipboard

Connect to server protected by cloudflare?

Open VeryBigSad opened this issue 4 years ago • 3 comments

Trying to connect to a server which is protected by Cloudflare, so direct IP connect won't work. I am being timed out every time, though other servers are working (example: mc.hypixel.net). The IP if I'm actually stupid and this works is poggop.org (down as of September 2020, probably will never be up again).

VeryBigSad avatar Jul 10 '20 23:07 VeryBigSad

We will likely need to add support for querying SRV records to fetch a server IP and port from a DNS SRV record and also add a way to specify the "vhost" we are connecting to in the login packet.

starcraft66 avatar Nov 24 '20 21:11 starcraft66

I edited the end of get_options function, to check for SRV records and connect accordingly.

	match = re.match(r"((?P<host>[^\[\]:]+)|\[(?P<addr>[^\[\]]+)\])"
					 r"(:(?P<port>\d+))?$", options.server)
	if match is None:
		raise ValueError("Invalid server address: '%s'." % options.server)
	options.address = match.group("host") or match.group("addr")
	try:
		srvInfo = {}
		srv_records=dns.resolver.resolve('_minecraft._tcp.'+options.address, 'SRV')
		for srv in srv_records:
			srvInfo['weight']   = srv.weight
			srvInfo['host']     = str(srv.target).rstrip('.')
			srvInfo['priority'] = srv.priority
			srvInfo['port']     = srv.port
		options.address = srvInfo['host']
		options.port = srvInfo['port']
	except:
		options.port = int(match.group("port") or 25565)

NotTahaAli avatar May 06 '21 14:05 NotTahaAli

That method will work for connecting to the "default" virtual host of a minecraft server. Many servers are behind reverse proxies that serve multiple virtual hosts behind the same address and pyCraft would likely not be able to connect to the right backend on those servers. Some work would need to be done in addition to the SRV querying code written by @TacticQuestions to fully implement this feature.

See: https://github.com/ammaraskar/pyCraft/blob/master/minecraft/networking/connection.py#L478

When combined with the code above, this line would set the "Server Address" in the handshake packet to the address returned by the SRV record and not the the correct value of the actual hostname that is initially queried for an SRV record.

starcraft66 avatar May 06 '21 15:05 starcraft66