diff --git a/http-doh-listener/pom.xml b/http-doh-listener/pom.xml new file mode 100644 index 0000000..4d0387b --- /dev/null +++ b/http-doh-listener/pom.xml @@ -0,0 +1,58 @@ + + + + com.moparisthebest.dns + jDnsProxy-parent + 1.0-SNAPSHOT + + 4.0.0 + http-doh-listener + jar + 1.0-SNAPSHOT + ${project.artifactId} + + 2.1.3.RELEASE + 5.1.5.RELEASE + + + + ${project.groupId} + jDnsProxy + ${project.version} + + + org.springframework.boot + spring-boot-starter + ${spring.boot.version} + compile + + + org.springframework.boot + spring-boot-starter-tomcat + ${spring.boot.version} + compile + + + org.springframework + spring-web + ${spring.version} + compile + + + org.springframework + spring-webmvc + ${spring.version} + compile + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + ${spring.boot.version} + + + + diff --git a/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/DohController.java b/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/DohController.java new file mode 100644 index 0000000..0bf28f0 --- /dev/null +++ b/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/DohController.java @@ -0,0 +1,54 @@ +package com.moparisthebest.dns.listen; + +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.Base64; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; + +import com.moparisthebest.dns.dto.Packet; +import com.moparisthebest.dns.resolve.BaseRequestResponse; +import com.moparisthebest.dns.resolve.Resolver; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; + +@RestController +public class DohController { + + // probably a much better way to do this... later... + static Resolver resolver = null; + static ExecutorService executor = null; + + private static final HttpHeaders headers = new HttpHeaders(); + private static final Base64.Decoder decoder = Base64.getUrlDecoder(); + + static { + headers.setContentType(MediaType.parseMediaType("application/dns-message")); + } + + @RequestMapping(value = "/dns-query", method = {RequestMethod.POST, RequestMethod.PUT}) + public HttpEntity dnsQueryPost(final HttpEntity request) throws Exception { + return dnsQuery(request.getBody()); + } + + @RequestMapping(value = "/dns-query", method = {RequestMethod.GET}) + public HttpEntity dnsQueryGet(@RequestParam("dns") final String dns) throws Exception { + return dnsQuery(decoder.decode(dns)); + } + + private static HttpEntity dnsQuery(final byte[] request) throws Exception { + //return request; + //return new HttpEntity<>(new byte[]{1}); + //final BaseRequestResponse requestResponse = new BaseRequestResponse(new Packet(ByteBuffer.wrap(request))); + //System.out.println(requestResponse); + //debugPacket(requestResponse.getRequest().getBuf()); + + //System.out.println(resolver); + //final byte[] response = resolver.resolveAsync(requestResponse).get().getResponse().getBuf().array(); + final byte[] response = resolver.resolve(new Packet(ByteBuffer.wrap(request))).getBuf().array(); + + return new HttpEntity<>(response, headers); + } +} diff --git a/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/HttpListener.java b/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/HttpListener.java new file mode 100644 index 0000000..ac40f70 --- /dev/null +++ b/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/HttpListener.java @@ -0,0 +1,39 @@ +package com.moparisthebest.dns.listen; + +import com.moparisthebest.dns.resolve.Resolver; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; + +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutorService; + +public class HttpListener implements Listener { + + private final InetSocketAddress isa; + + private ConfigurableApplicationContext ctx = null; + + public HttpListener(final InetSocketAddress isa, final Resolver resolver, final ExecutorService executor) { + this.isa = isa; + DohController.resolver = resolver; + DohController.executor = executor; + } + + @Override + public void run() { + final Map props = new HashMap<>(); + props.put("server.address", isa.getAddress().getHostAddress()); + props.put("server.port", isa.getPort()); + ctx = new SpringApplicationBuilder() + .sources(HttpServices.class) + .properties(props) + .run(); + } + + @Override + public void close() { + ctx.stop(); + } +} diff --git a/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/HttpServices.java b/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/HttpServices.java new file mode 100644 index 0000000..33326d7 --- /dev/null +++ b/http-doh-listener/src/main/java/com/moparisthebest/dns/listen/HttpServices.java @@ -0,0 +1,23 @@ +package com.moparisthebest.dns.listen; + +import com.moparisthebest.dns.net.ParsedUrl; +import com.moparisthebest.dns.resolve.Resolver; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.net.InetSocketAddress; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ForkJoinPool; + +@SpringBootApplication +public class HttpServices implements Services { + + @Override + public Listener getListener(final ParsedUrl parsedUrl, final Resolver resolver, final ExecutorService executor) { + if (!"http".equals(parsedUrl.getProtocol())) { + return null; + } + final InetSocketAddress isa = ((InetSocketAddress) parsedUrl.getAddr()); + return new HttpListener(isa, resolver, executor); + } + +} diff --git a/http-doh-listener/src/main/resources/META-INF/services/com.moparisthebest.dns.listen.Services b/http-doh-listener/src/main/resources/META-INF/services/com.moparisthebest.dns.listen.Services new file mode 100644 index 0000000..c288214 --- /dev/null +++ b/http-doh-listener/src/main/resources/META-INF/services/com.moparisthebest.dns.listen.Services @@ -0,0 +1 @@ +com.moparisthebest.dns.listen.HttpServices \ No newline at end of file diff --git a/pom.xml b/pom.xml index 92fd48e..297e8b7 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,7 @@ jDnsProxy xmpp-dox + http-doh-listener ${project.artifactId}