Add support for SRV records (#6982)

* SRV record support

* respect the formatting II

* avoid blocking call

* Add android support

* switch to stripped down version of dnsjava

* move srv lookup from `Net` to `NetProvider`

* random optimizations

* bump arc and restart the thing

* reimplement using ArcDns

* Update core/src/mindustry/net/ArcNetProvider.java

Co-authored-by: buthed010203 <naguiar010203@gmail.com>

* Update ArcNetProvider.java

Co-authored-by: buthed010203 <naguiar010203@gmail.com>
This commit is contained in:
Phinner
2022-07-03 14:37:58 +00:00
committed by GitHub
parent 4d5f16e75d
commit 685e3cac83
3 changed files with 31 additions and 9 deletions

View File

@@ -5,10 +5,12 @@ import arc.func.*;
import arc.math.*;
import arc.net.*;
import arc.net.FrameworkMessage.*;
import arc.net.dns.*;
import arc.struct.*;
import arc.util.*;
import arc.util.Log.*;
import arc.util.io.*;
import mindustry.*;
import mindustry.net.Net.*;
import mindustry.net.Packets.*;
import net.jpountz.lz4.*;
@@ -17,6 +19,7 @@ import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
import java.util.concurrent.*;
import static mindustry.Vars.*;
@@ -189,8 +192,27 @@ public class ArcNetProvider implements NetProvider{
@Override
public void pingHost(String address, int port, Cons<Host> valid, Cons<Exception> invalid){
try{
DatagramSocket socket = new DatagramSocket();
var host = pingHostImpl(address, port);
Core.app.post(() -> valid.get(host));
}catch(IOException e){
if(port == Vars.port){
for(var record : ArcDns.getSrvRecords("_mindustry._tcp." + address)){
try{
var host = pingHostImpl(record.target, record.port);
Core.app.post(() -> valid.get(host));
return;
}catch(IOException ignored){
}
}
}
Core.app.post(() -> invalid.get(e));
}
}
private Host pingHostImpl(String address, int port) throws IOException{
try(DatagramSocket socket = new DatagramSocket()){
long time = Time.millis();
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), port));
socket.setSoTimeout(2000);
@@ -199,10 +221,8 @@ public class ArcNetProvider implements NetProvider{
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
Host host = NetworkIO.readServerData((int)Time.timeSinceMillis(time), packet.getAddress().getHostAddress(), buffer);
Core.app.post(() -> valid.get(host));
}catch(Exception e){
Core.app.post(() -> invalid.get(e));
host.port = port;
return host;
}
}

View File

@@ -325,6 +325,7 @@ public class Net{
/**
* Pings a host in a pooled thread. If an error occurred, failed() should be called with the exception.
* If the port is the default mindustry port, SRV records are checked too.
*/
public void pingHost(String address, int port, Cons<Host> valid, Cons<Exception> failed){
pingExecutor.submit(() -> provider.pingHost(address, port, valid, failed));
@@ -375,7 +376,10 @@ public class Net{
*/
void discoverServers(Cons<Host> callback, Runnable done);
/** Ping a host. If an error occurred, failed() should be called with the exception. This method should block. */
/**
* Ping a host. If an error occurred, failed() should be called with the exception. This method should block.
* If the port is the default mindustry port (6567), SRV records are checked too.
*/
void pingHost(String address, int port, Cons<Host> valid, Cons<Exception> failed);
/** Host a server at specified port. */
@@ -393,5 +397,4 @@ public class Net{
closeServer();
}
}
}

View File

@@ -133,7 +133,7 @@ public class JoinDialog extends BaseDialog{
if(!buttons[0].childrenPressed()){
if(server.lastHost != null){
Events.fire(new ClientPreConnectEvent(server.lastHost));
safeConnect(server.ip, server.port, server.lastHost.version);
safeConnect(server.lastHost.address, server.lastHost.port, server.lastHost.version);
}else{
connect(server.ip, server.port);
}
@@ -410,7 +410,6 @@ public class JoinDialog extends BaseDialog{
int resport = address.contains(":") ? Strings.parseInt(address.split(":")[1]) : port;
net.pingHost(resaddress, resport, res -> {
if(refreshes != cur) return;
res.port = resport;
//add header
if(groupTable[0] == null){