Add http-doh-listener implementing DoH listener with spring boot

This commit is contained in:
Travis Burtrum 2019-03-14 01:21:17 -04:00
parent 841ea530f4
commit 964521f0cd
6 changed files with 176 additions and 0 deletions

58
http-doh-listener/pom.xml Normal file
View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.moparisthebest.dns</groupId>
<artifactId>jDnsProxy-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>http-doh-listener</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}</name>
<properties>
<spring.boot.version>2.1.3.RELEASE</spring.boot.version>
<spring.version>5.1.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jDnsProxy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring.boot.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -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<byte[]> dnsQueryPost(final HttpEntity<byte[]> request) throws Exception {
return dnsQuery(request.getBody());
}
@RequestMapping(value = "/dns-query", method = {RequestMethod.GET})
public HttpEntity<byte[]> dnsQueryGet(@RequestParam("dns") final String dns) throws Exception {
return dnsQuery(decoder.decode(dns));
}
private static HttpEntity<byte[]> 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);
}
}

View File

@ -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<String, Object> 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();
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1 @@
com.moparisthebest.dns.listen.HttpServices

View File

@ -50,6 +50,7 @@
<modules>
<module>jDnsProxy</module>
<module>xmpp-dox</module>
<module>http-doh-listener</module>
</modules>
<build>
<finalName>${project.artifactId}</finalName>