From 127059ea7366374a503bbc4f2102e3eb422b31aa Mon Sep 17 00:00:00 2001 From: matsuz Date: Sun, 30 Apr 2017 18:53:55 +0800 Subject: [PATCH] [Feature] Alternate configuration format from JSON to TOML. 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. --- config.json | 15 ------ gdns.iml | 27 +++++++++++ pom.xml | 8 +++- .../java/com/matsuz/GoogleDNSLauncher.java | 7 ++- .../java/com/matsuz/gdns/Configuration.java | 47 +++++++++++++++---- src/main/java/com/matsuz/gdns/Resolver.java | 29 +++++++----- static.toml | 18 +++++++ 7 files changed, 109 insertions(+), 42 deletions(-) delete mode 100644 config.json create mode 100644 gdns.iml create mode 100644 static.toml diff --git a/config.json b/config.json deleted file mode 100644 index 7cbb7e8..0000000 --- a/config.json +++ /dev/null @@ -1,15 +0,0 @@ -[ - // 优先级从上至下;使用正则表达式匹配域名。 - { - // example1 - "pattern": "www1\\.example\\.com", - // www1\.example\.com - "address": "127.0.0.1" - }, - { - // example2 - "pattern": "www2\\.example\\.com", - // www1\.example\.com - "address": "127.0.0.2" - } -] diff --git a/gdns.iml b/gdns.iml new file mode 100644 index 0000000..4bb499b --- /dev/null +++ b/gdns.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index fe67f32..4b1da1a 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ 2.1.8 2.8.0 + 0.7.1 3.7.0 4.1.9.Final 4.12 @@ -30,6 +31,11 @@ gson ${gson.version} + + com.moandjiezana.toml + toml4j + ${toml4j.version} + com.squareup.okhttp3 okhttp @@ -37,7 +43,7 @@ io.netty - netty-all + netty-handler ${netty.version} diff --git a/src/main/java/com/matsuz/GoogleDNSLauncher.java b/src/main/java/com/matsuz/GoogleDNSLauncher.java index bd03e02..7e0e354 100644 --- a/src/main/java/com/matsuz/GoogleDNSLauncher.java +++ b/src/main/java/com/matsuz/GoogleDNSLauncher.java @@ -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(); diff --git a/src/main/java/com/matsuz/gdns/Configuration.java b/src/main/java/com/matsuz/gdns/Configuration.java index ec2721e..72f260b 100644 --- a/src/main/java/com/matsuz/gdns/Configuration.java +++ b/src/main/java/com/matsuz/gdns/Configuration.java @@ -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; @@ -29,11 +29,38 @@ public void setConfigPath(Path path) { this.configPath = path; } - List> getConfigList() throws IOException { - StringBuilder sb = new StringBuilder(); - Files.lines(configPath, StandardCharsets.UTF_8).forEach(e -> { - if (!e.trim().startsWith("//")) sb.append(e); - }); - return (List>) 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> getIPv4ConfigList() { + try { + List ipv4Entries = toml.read(getStaticFileContent()).getTables("ipv4"); + List> result = new ArrayList<>(); + ipv4Entries.forEach(e -> result.add(e.toMap())); + return result; + } catch (IOException e) { + return null; + } + } + + List> getIPv6ConfigList() { + try { + List ipv4Entries = toml.read(getStaticFileContent()).getTables("ipv6"); + List> result = new ArrayList<>(); + ipv4Entries.forEach(e -> result.add(e.toMap())); + return result; + } catch (IOException e) { + return null; + } } } diff --git a/src/main/java/com/matsuz/gdns/Resolver.java b/src/main/java/com/matsuz/gdns/Resolver.java index 31892e9..093086d 100644 --- a/src/main/java/com/matsuz/gdns/Resolver.java +++ b/src/main/java/com/matsuz/gdns/Resolver.java @@ -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> configList = Configuration.getInstance().getConfigList(); - for (Map 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> configList = null; + if (rrType == Type.A) + configList = Configuration.getInstance().getIPv4ConfigList(); + else if (rrType == Type.AAAA) + configList = Configuration.getInstance().getIPv6ConfigList(); + if (configList != null) + for (Map 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 diff --git a/static.toml b/static.toml new file mode 100644 index 0000000..1245487 --- /dev/null +++ b/static.toml @@ -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'