Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Commit

Permalink
[Feature] Alternate configuration format from JSON to TOML.
Browse files Browse the repository at this point in the history
1. Alternate configuration format from JSON to TOML.
2. Support both ipv4 and ipv6 in configuration file 'static.toml'.
3. If file 'static.toml' not exist, regard it as a empty file.
  • Loading branch information
zengxs committed Apr 30, 2017
1 parent 4345734 commit 127059e
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 42 deletions.
15 changes: 0 additions & 15 deletions config.json

This file was deleted.

27 changes: 27 additions & 0 deletions gdns.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: dnsjava:dnsjava:2.1.8" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.moandjiezana.toml:toml4j:0.7.1" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.7.0" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.12.0" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.9.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.9.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.9.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.9.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.9.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.9.Final" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<properties>
<dnsjava.version>2.1.8</dnsjava.version>
<gson.version>2.8.0</gson.version>
<toml4j.version>0.7.1</toml4j.version>
<okhttp3.version>3.7.0</okhttp3.version>
<netty.version>4.1.9.Final</netty.version>
<junit.version>4.12</junit.version>
Expand All @@ -30,14 +31,19 @@
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.moandjiezana.toml</groupId>
<artifactId>toml4j</artifactId>
<version>${toml4j.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp3.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<artifactId>netty-handler</artifactId>
<version>${netty.version}</version>
</dependency>

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/matsuz/GoogleDNSLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import com.matsuz.gdns.Configuration;
import com.matsuz.gdns.UDPServer;

import java.net.URISyntaxException;
import java.nio.file.Paths;


public class GoogleDNSLauncher {
public static void main(String[] args) throws InterruptedException, URISyntaxException {
// 设置Static Map配置文件
Configuration.getInstance().setConfigPath(Paths.get("config.json"));
public static void main(String[] args) throws InterruptedException {
// 设置Static配置文件
Configuration.getInstance().setConfigPath(Paths.get("static.toml"));

UDPServer.run();

Expand Down
47 changes: 37 additions & 10 deletions src/main/java/com/matsuz/gdns/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.matsuz.gdns;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.moandjiezana.toml.Toml;

import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class Configuration {
private static final Configuration instance = new Configuration();
private static final Gson gson = new GsonBuilder().setLenient().create();
private static final Toml toml = new Toml();

private Path configPath;

Expand All @@ -29,11 +29,38 @@ public void setConfigPath(Path path) {
this.configPath = path;
}

List<Map<String, String>> getConfigList() throws IOException {
StringBuilder sb = new StringBuilder();
Files.lines(configPath, StandardCharsets.UTF_8).forEach(e -> {
if (!e.trim().startsWith("//")) sb.append(e);
});
return (List<Map<String, String>>) gson.fromJson(sb.toString(), List.class);
private String getStaticFileContent() throws IOException {
InputStream in = Files.newInputStream(configPath);
ByteArrayOutputStream out = new ByteArrayOutputStream();

// bytes copy
final int BLOCK_SIZE = 1024;
byte[] bytes = new byte[BLOCK_SIZE];
int len;
while ((len = in.read(bytes)) != -1) out.write(bytes, 0, len);

return new String(out.toByteArray(), StandardCharsets.UTF_8);
}

List<Map<String, Object>> getIPv4ConfigList() {
try {
List<Toml> ipv4Entries = toml.read(getStaticFileContent()).getTables("ipv4");
List<Map<String, Object>> result = new ArrayList<>();
ipv4Entries.forEach(e -> result.add(e.toMap()));
return result;
} catch (IOException e) {
return null;
}
}

List<Map<String, Object>> getIPv6ConfigList() {
try {
List<Toml> ipv4Entries = toml.read(getStaticFileContent()).getTables("ipv6");
List<Map<String, Object>> result = new ArrayList<>();
ipv4Entries.forEach(e -> result.add(e.toMap()));
return result;
} catch (IOException e) {
return null;
}
}
}
29 changes: 17 additions & 12 deletions src/main/java/com/matsuz/gdns/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@ private Resolver() {

public static Message resolve(Name name, int rrType, String edns) throws IOException {
// compare with static map
if (rrType == Type.A) {
List<Map<String, String>> configList = Configuration.getInstance().getConfigList();
for (Map<String, String> entry : configList) {
String regex = entry.get("pattern") + "\\.?";
if (Pattern.matches(regex, name.toString())) {
Message response = new Message();
response.addRecord(Record.fromString(
name, rrType, DClass.IN, 0,
entry.get("address"), Name.root
), Section.ANSWER);
return response;
if (rrType == Type.A || rrType == Type.AAAA) {
List<Map<String, Object>> configList = null;
if (rrType == Type.A)
configList = Configuration.getInstance().getIPv4ConfigList();
else if (rrType == Type.AAAA)
configList = Configuration.getInstance().getIPv6ConfigList();
if (configList != null)
for (Map<String, Object> entry : configList) {
String regex = entry.get("pattern") + "\\.?";
if (Pattern.matches(regex, name.toString())) {
Message response = new Message();
response.addRecord(Record.fromString(
name, rrType, DClass.IN, 0,
(String) entry.get("address"), Name.root
), Section.ANSWER);
return response;
}
}
}
}

// request google public dns
Expand Down
18 changes: 18 additions & 0 deletions static.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[[ipv4]]
# example ipv4
pattern = '''www\.example\.com'''
address = '127.0.0.1'

[[ipv4]]
pattern = '''www\.google\.com'''
address = '172.217.27.132'


[[ipv6]]
# example ipv6
pattern = '''www\.example\.com'''
address = '::1'

[[ipv6]]
pattern = '''www\.google\.com'''
address = '2404:6800:4008:800::2004'

0 comments on commit 127059e

Please sign in to comment.