Skip to content

Commit

Permalink
DNS cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuken committed Jan 27, 2024
1 parent 137d148 commit e0ecfe9
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 101 deletions.
4 changes: 4 additions & 0 deletions arc-core/src/arc/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import arc.struct.*;
import arc.util.*;

import java.net.*;

public interface Application extends Disposable{

/** Returns a list of all the application listeners used. */
Expand Down Expand Up @@ -96,6 +98,8 @@ default boolean openURI(String URI){
return false;
}

default void getDnsServers(Seq<InetSocketAddress> out){}

/** Posts a runnable on the main loop thread.*/
void post(Runnable runnable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import arc.struct.*;
import arc.util.*;

import java.net.*;

/**
* An implementation of the {@link Application} interface for Android. Create an {@link Activity} that derives from this class. In
* the {@link Activity#onCreate(Bundle)} method call the {@link #initialize(ApplicationListener)} method specifying the
Expand Down Expand Up @@ -166,6 +168,31 @@ protected void hideStatusBar(boolean hide){
getWindow().getDecorView().setSystemUiVisibility(0x1);
}

@Override
public void getDnsServers(Seq<InetSocketAddress> out){
if(getVersion() < 21) return; //needs API level 21

try{
ConnectivityManager cm = getSystemService(ConnectivityManager.class);
Network network = cm.getActiveNetwork();
if(network == null){
// if the device is offline, there's no active network
return;
}

LinkProperties lp = cm.getLinkProperties(network);
if(lp == null){
// can be null for an unknown network, which may happen if networks change
return;
}

for(InetAddress address : lp.getDnsServers()){
out.add(new InetSocketAddress(address, 53));
}
}catch(Throwable ignored){
}
}

@Override
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
Expand Down
34 changes: 0 additions & 34 deletions extensions/arcnet/src/arc/net/dns/AbstractNameserverProvider.java

This file was deleted.

19 changes: 13 additions & 6 deletions extensions/arcnet/src/arc/net/dns/ArcDns.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package arc.net.dns;

import arc.*;
import arc.math.*;
import arc.struct.*;
import arc.util.*;
import arc.util.io.Streams.*;

import java.io.*;
import java.net.*;
import java.nio.charset.*;

public final class ArcDns{

Expand All @@ -26,7 +26,7 @@ public final class ArcDns{
}

public static Seq<NameserverProvider> getNameserverProviders(){
return nameserverProviders.copy();
return nameserverProviders;
}

/** Set a new ordered list of resolver config providers. */
Expand All @@ -38,25 +38,32 @@ public static void setNameserverProviders(Seq<NameserverProvider> providers){

/** Returns all located servers */
public static Seq<InetSocketAddress> getNameservers(){
return nameservers.copy();
return nameservers;
}

public static void refreshNameservers(){
nameservers.clear();

if(Core.app != null){
Core.app.getDnsServers(nameservers);
}

for(NameserverProvider provider : nameserverProviders){
if(provider.isEnabled()){
try{
provider.initialize();
nameservers.addAll(provider.getNameservers());
// Stop when a name server is found
if(!nameservers.isEmpty()) return;
}catch(InitializationException e){
}catch(Exception e){
Log.warn("[DNS] Failed to initialize provider: @", e);
}
}
}

for(InetSocketAddress server : nameservers){
Log.debug("[DNS] Added @ to nameservers", server);
}

// Add localhost as nameserver if no suitable nameserver provider found
nameservers.add(new InetSocketAddress(InetAddress.getLoopbackAddress(), dnsResolverPort));
}
Expand Down Expand Up @@ -101,7 +108,7 @@ public static Seq<SRVRecord> getSrvRecords(String domain, InetSocketAddress name
// Domain
for(String part : domain.split("\\.")){
out.writeByte(part.length());
out.write(part.getBytes(StandardCharsets.UTF_8));
out.write(part.getBytes(Strings.utf8));
}
out.writeByte(0);

Expand Down
11 changes: 0 additions & 11 deletions extensions/arcnet/src/arc/net/dns/InitializationException.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,12 @@
* <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/jndi/jndi-dns.html">JNDI DNS Service Provider</a>.
*/
public final class JndiContextNameserverProvider implements NameserverProvider{
private InnerJndiContextNameserverProvider inner;

public JndiContextNameserverProvider(){
if(!OS.isAndroid){
try{
inner = new InnerJndiContextNameserverProvider();
}catch(Throwable e){
Log.debug("[DNS] JNDI DNS not available");
}
}
}

@Override
public void initialize(){
inner.initialize();
}

@Override
public Seq<InetSocketAddress> getNameservers(){
return inner.getNameservers();
}

@Override
public boolean isEnabled(){
return inner != null;
}

private static final class InnerJndiContextNameserverProvider extends AbstractNameserverProvider{
static{
Log.debug("[DNS] JNDI class: @", DirContext.class.getName());
}
try{
Seq<InetSocketAddress> result = new Seq<>();

@Override
public void initialize(){
reset();
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
// http://mail.openjdk.java.net/pipermail/net-dev/2017-March/010695.html
Expand Down Expand Up @@ -84,12 +55,21 @@ public void initialize(){
port = dnsResolverPort;
}

addNameServer(new InetSocketAddress(host, port));
result.add(new InetSocketAddress(host, port));
}catch(URISyntaxException e){
Log.debug("[DNS] Could not parse @ as a dns server, ignoring: @", server, e);
}
}
}

return result;
}catch(Throwable t){
return new Seq<>();
}
}

@Override
public boolean isEnabled(){
return !OS.isAndroid && !OS.isIos;
}
}
3 changes: 0 additions & 3 deletions extensions/arcnet/src/arc/net/dns/NameserverProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import java.net.*;

public interface NameserverProvider{
/** Initializes the servers. */
void initialize() throws InitializationException;

/** Returns all located servers, which may be empty. */
Seq<InetSocketAddress> getNameservers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@
package arc.net.dns;

import arc.files.*;
import arc.struct.*;
import arc.util.*;

import java.io.*;
import java.net.*;
import java.util.*;

import static arc.net.dns.ArcDns.dnsResolverPort;
import static arc.net.dns.ArcDns.*;

public final class ResolvConfNameserverProvider extends AbstractNameserverProvider{
public final class ResolvConfNameserverProvider implements NameserverProvider{

@Override
public void initialize(){
reset();
public Seq<InetSocketAddress> getNameservers(){
Seq<InetSocketAddress> out = new Seq<>();
// first try the default unix config path
if(!tryParseResolveConf("/etc/resolv.conf")){
if(!tryParseResolveConf("/etc/resolv.conf", out)){
// then fallback to netware
tryParseResolveConf("sys:/etc/resolv.cfg");
tryParseResolveConf("sys:/etc/resolv.cfg", out);
}
return out;
}

private boolean tryParseResolveConf(String path){
private boolean tryParseResolveConf(String path, Seq<InetSocketAddress> out){
Fi conf = new Fi(path);

if(conf.exists()){
Expand All @@ -36,11 +38,11 @@ private boolean tryParseResolveConf(String path){
if(!tokenizer.hasMoreTokens()) continue;

if(tokenizer.nextToken().equals("nameserver")){
addNameServer(new InetSocketAddress(tokenizer.nextToken(), dnsResolverPort));
out.add(new InetSocketAddress(tokenizer.nextToken(), dnsResolverPort));
}
}
return true;
}catch(IOException ignored){
}catch(Exception ignored){
}
}

Expand Down
11 changes: 11 additions & 0 deletions extensions/arcnet/src/arc/net/dns/SRVRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ public SRVRecord(long ttl, int priority, int weight, int port, String target){
this.target = target;
}

@Override
public String toString(){
return "SRVRecord{" +
"ttl=" + ttl +
", priority=" + priority +
", weight=" + weight +
", port=" + port +
", target='" + target + '\'' +
'}';
}

@Override
public int compareTo(SRVRecord o){
if(this.priority != o.priority){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
import static arc.net.dns.ArcDns.dnsResolverPort;

public final class WellKnownNameserverProvider implements NameserverProvider{

private final Seq<InetSocketAddress> nameservers = Seq.with(
new InetSocketAddress("1.1.1.1", dnsResolverPort), // Cloudflare
new InetSocketAddress("8.8.8.8", dnsResolverPort) // Google
);

@Override
public void initialize(){
}

@Override
public Seq<InetSocketAddress> getNameservers(){
return nameservers.copy();
return nameservers;
}
}

0 comments on commit e0ecfe9

Please sign in to comment.