From 841ea530f45ec8ea9ed2941ee3d31bfb755491af Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Thu, 14 Mar 2019 01:10:29 -0400 Subject: [PATCH] Re-factor Resolver to provide async/inline default impls so only 1 needs implemented, implement simple DirectProxy mode that takes a listener and resolver only --- .../main/java/com/moparisthebest/dns/DnsProxy.java | 9 ++++++++- .../com/moparisthebest/dns/listen/Listener.java | 9 ++++----- .../moparisthebest/dns/resolve/CacheResolver.java | 5 ----- .../com/moparisthebest/dns/resolve/Resolver.java | 13 +++++++++++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/jDnsProxy/src/main/java/com/moparisthebest/dns/DnsProxy.java b/jDnsProxy/src/main/java/com/moparisthebest/dns/DnsProxy.java index 29a26fe..353112a 100644 --- a/jDnsProxy/src/main/java/com/moparisthebest/dns/DnsProxy.java +++ b/jDnsProxy/src/main/java/com/moparisthebest/dns/DnsProxy.java @@ -4,6 +4,7 @@ import com.moparisthebest.dns.listen.Listener; import com.moparisthebest.dns.net.ParsedUrl; import com.moparisthebest.dns.resolve.CacheResolver; import com.moparisthebest.dns.resolve.QueueProcessingResolver; +import com.moparisthebest.dns.resolve.Resolver; import java.io.File; import java.io.FileInputStream; @@ -17,6 +18,12 @@ public class DnsProxy { public static void main(String[] args) throws Throwable { + if (args.length == 2) { + // quick hack to run direct proxy todo: make this shut down clean + Listener.of(ParsedUrl.of(args[0]), Resolver.of(ParsedUrl.of(args[1])), ForkJoinPool.commonPool()).run(); + return; + } + final Map config; final File propsFile = new File(args.length > 0 ? args[0] : "jdnsproxy.properties"); if(propsFile.canRead()) { @@ -62,7 +69,7 @@ public class DnsProxy { .startQueueProcessingResolvers(queueProcessingResolvers); final List listeners = Arrays.stream(config.getOrDefault("listeners", "tcp://127.0.0.1:5353 udp://127.0.0.1:5353").split("\\s+")) - .map(url -> Listener.ofAndStart(url, resolver, executor)).collect(Collectors.toList()); + .map(url -> Listener.ofAndStart(ParsedUrl.of(url), resolver, executor)).collect(Collectors.toList()); //final List listeners = new ArrayList<>(); //listeners.add(Listener.ofAndStart("tcp://127.0.0.1:5556", resolver, executor)); //listeners.add(Listener.ofAndStart("udp://127.0.0.1:5556", resolver, executor)); diff --git a/jDnsProxy/src/main/java/com/moparisthebest/dns/listen/Listener.java b/jDnsProxy/src/main/java/com/moparisthebest/dns/listen/Listener.java index ad392a4..67255e3 100644 --- a/jDnsProxy/src/main/java/com/moparisthebest/dns/listen/Listener.java +++ b/jDnsProxy/src/main/java/com/moparisthebest/dns/listen/Listener.java @@ -13,18 +13,17 @@ public interface Listener extends Runnable, AutoCloseable { ServiceLoader services = ServiceLoader.load(Services.class); - static Listener of(final String listener, final Resolver resolver, final ExecutorService executor) { - final ParsedUrl parsedUrl = ParsedUrl.of(listener); + static Listener of(final ParsedUrl parsedUrl, final Resolver resolver, final ExecutorService executor) { for (final Services s : services) { final Listener ret = s.getListener(parsedUrl, resolver, executor); if (ret != null) return ret; } - throw new IllegalArgumentException("unhandled listener format: " + listener); + throw new IllegalArgumentException("unhandled listener format: " + parsedUrl.getUrlStr()); } - public static Listener ofAndStart(final String listener, final Resolver resolver, final ExecutorService executor) { - final Listener ret = of(listener, resolver, executor); + public static Listener ofAndStart(final ParsedUrl parsedUrl, final Resolver resolver, final ExecutorService executor) { + final Listener ret = of(parsedUrl, resolver, executor); executor.execute(ret); return ret; } diff --git a/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/CacheResolver.java b/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/CacheResolver.java index f32ae18..8158106 100644 --- a/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/CacheResolver.java +++ b/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/CacheResolver.java @@ -164,11 +164,6 @@ public class CacheResolver implements Resolver, AutoCloseable { return request; } - @Override - public Packet resolve(final Packet request) throws Exception { - return resolveAsync(new BaseRequestResponse(request)).get().getResponse(); - } - public void persistCache(final File file, final Map cache) throws IOException { final File tmpFile = new File(file.getAbsolutePath() + ".tmp"); try(FileOutputStream fos = new FileOutputStream(tmpFile); diff --git a/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/Resolver.java b/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/Resolver.java index 187db9b..531bca1 100644 --- a/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/Resolver.java +++ b/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/Resolver.java @@ -8,7 +8,14 @@ import java.util.concurrent.CompletableFuture; public interface Resolver { default CompletableFuture resolveAsync(E requestResponse) { - return null; + try { + requestResponse.setResponse(resolve(requestResponse.getRequest())); + return CompletableFuture.completedFuture(requestResponse); + } catch (Exception e) { + final CompletableFuture ret = new CompletableFuture<>(); + ret.completeExceptionally(e); + return ret; + } /* return CompletableFuture.supplyAsync(() -> { requestResponse.setResponse(resolve(requestResponse.getRequest())); @@ -17,7 +24,9 @@ public interface Resolver { */ } - Packet resolve(Packet request) throws Exception; + default Packet resolve(Packet request) throws Exception { + return resolveAsync(new BaseRequestResponse(request)).get().getResponse(); + } ServiceLoader services = ServiceLoader.load(Services.class);