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:
@@ -5,10 +5,12 @@ import arc.func.*;
|
|||||||
import arc.math.*;
|
import arc.math.*;
|
||||||
import arc.net.*;
|
import arc.net.*;
|
||||||
import arc.net.FrameworkMessage.*;
|
import arc.net.FrameworkMessage.*;
|
||||||
|
import arc.net.dns.*;
|
||||||
import arc.struct.*;
|
import arc.struct.*;
|
||||||
import arc.util.*;
|
import arc.util.*;
|
||||||
import arc.util.Log.*;
|
import arc.util.Log.*;
|
||||||
import arc.util.io.*;
|
import arc.util.io.*;
|
||||||
|
import mindustry.*;
|
||||||
import mindustry.net.Net.*;
|
import mindustry.net.Net.*;
|
||||||
import mindustry.net.Packets.*;
|
import mindustry.net.Packets.*;
|
||||||
import net.jpountz.lz4.*;
|
import net.jpountz.lz4.*;
|
||||||
@@ -17,6 +19,7 @@ import java.io.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
import java.nio.channels.*;
|
import java.nio.channels.*;
|
||||||
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
import static mindustry.Vars.*;
|
import static mindustry.Vars.*;
|
||||||
@@ -189,8 +192,27 @@ public class ArcNetProvider implements NetProvider{
|
|||||||
@Override
|
@Override
|
||||||
public void pingHost(String address, int port, Cons<Host> valid, Cons<Exception> invalid){
|
public void pingHost(String address, int port, Cons<Host> valid, Cons<Exception> invalid){
|
||||||
try{
|
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();
|
long time = Time.millis();
|
||||||
|
|
||||||
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), port));
|
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), port));
|
||||||
socket.setSoTimeout(2000);
|
socket.setSoTimeout(2000);
|
||||||
|
|
||||||
@@ -199,10 +221,8 @@ public class ArcNetProvider implements NetProvider{
|
|||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
|
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
|
||||||
Host host = NetworkIO.readServerData((int)Time.timeSinceMillis(time), packet.getAddress().getHostAddress(), buffer);
|
Host host = NetworkIO.readServerData((int)Time.timeSinceMillis(time), packet.getAddress().getHostAddress(), buffer);
|
||||||
|
host.port = port;
|
||||||
Core.app.post(() -> valid.get(host));
|
return host;
|
||||||
}catch(Exception e){
|
|
||||||
Core.app.post(() -> invalid.get(e));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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.
|
* 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){
|
public void pingHost(String address, int port, Cons<Host> valid, Cons<Exception> failed){
|
||||||
pingExecutor.submit(() -> provider.pingHost(address, port, valid, failed));
|
pingExecutor.submit(() -> provider.pingHost(address, port, valid, failed));
|
||||||
@@ -375,7 +376,10 @@ public class Net{
|
|||||||
*/
|
*/
|
||||||
void discoverServers(Cons<Host> callback, Runnable done);
|
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);
|
void pingHost(String address, int port, Cons<Host> valid, Cons<Exception> failed);
|
||||||
|
|
||||||
/** Host a server at specified port. */
|
/** Host a server at specified port. */
|
||||||
@@ -393,5 +397,4 @@ public class Net{
|
|||||||
closeServer();
|
closeServer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ public class JoinDialog extends BaseDialog{
|
|||||||
if(!buttons[0].childrenPressed()){
|
if(!buttons[0].childrenPressed()){
|
||||||
if(server.lastHost != null){
|
if(server.lastHost != null){
|
||||||
Events.fire(new ClientPreConnectEvent(server.lastHost));
|
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{
|
}else{
|
||||||
connect(server.ip, server.port);
|
connect(server.ip, server.port);
|
||||||
}
|
}
|
||||||
@@ -410,7 +410,6 @@ public class JoinDialog extends BaseDialog{
|
|||||||
int resport = address.contains(":") ? Strings.parseInt(address.split(":")[1]) : port;
|
int resport = address.contains(":") ? Strings.parseInt(address.split(":")[1]) : port;
|
||||||
net.pingHost(resaddress, resport, res -> {
|
net.pingHost(resaddress, resport, res -> {
|
||||||
if(refreshes != cur) return;
|
if(refreshes != cur) return;
|
||||||
res.port = resport;
|
|
||||||
|
|
||||||
//add header
|
//add header
|
||||||
if(groupTable[0] == null){
|
if(groupTable[0] == null){
|
||||||
|
|||||||
Reference in New Issue
Block a user