001/*
002 * Copyright 2015-2020 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns.minidnsrepl;
012
013import java.io.FileOutputStream;
014import java.io.IOException;
015import java.lang.reflect.Field;
016
017import org.minidns.AbstractDnsClient;
018import org.minidns.DnsClient;
019import org.minidns.cache.LruCache;
020import org.minidns.dnsmessage.DnsMessage;
021import org.minidns.dnssec.DnssecClient;
022import org.minidns.hla.DnssecResolverApi;
023import org.minidns.hla.ResolverResult;
024import org.minidns.iterative.IterativeDnsClient;
025import org.minidns.jul.MiniDnsJul;
026import org.minidns.record.A;
027
028public class MiniDnsRepl {
029
030    public static final DnsClient DNSCLIENT = new DnsClient();
031    public static final IterativeDnsClient ITERATIVEDNSCLIENT = new IterativeDnsClient();
032    public static final DnssecClient DNSSECCLIENT = new DnssecClient();
033
034    static {
035        LruCache cache = null;
036        try {
037            Field defaultCacheField = AbstractDnsClient.class.getDeclaredField("DEFAULT_CACHE");
038            defaultCacheField.setAccessible(true);
039            cache = (LruCache) defaultCacheField.get(null);
040        } catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
041            throw new IllegalStateException(e);
042        }
043        DEFAULT_CACHE = cache;
044    }
045
046    public static final LruCache DEFAULT_CACHE;
047
048    public static void init() {
049        // CHECKSTYLE:OFF
050        System.out.println("MiniDNS REPL");
051        // CHECKSTYLE:ON
052    }
053
054    public static void clearCache() throws SecurityException, IllegalArgumentException {
055        DEFAULT_CACHE.clear();
056    }
057
058    public static void main(String[] args) throws IOException, SecurityException, IllegalArgumentException {
059        MiniDnsJul.enableMiniDnsTrace();
060
061        ResolverResult<A> res = DnssecResolverApi.INSTANCE.resolveDnssecReliable("verteiltesysteme.net", A.class);
062        /*
063        DnssecStats.iterativeDnssecLookupNormalVsExtendedCache();
064        DnssecClient client = new DNSSECClient(new LRUCache(1024));
065        DnssecMessage secRes = client.queryDnssec("verteiltesysteme.net", TYPE.A);
066        */
067
068        /*
069        DnssecStats.iterativeDnssecLookupNormalVsExtendedCache();
070        Nsid nsid = NSIDTest.testNsidLRoot();
071        DnsMessage res = RECURSIVEDNSCLIENT.query("mate.geekplace.eu", TYPE.A);
072        */
073        // CHECKSTYLE:OFF
074        System.out.println(res);
075//        System.out.println(nsid);
076//      System.out.println(secRes);
077//        System.out.println(res);
078        // CHCECKSTYLE:ON
079    }
080
081    public static void writeToFile(DnsMessage dnsMessage, String path) throws IOException {
082        try (FileOutputStream fos = new FileOutputStream(path)) {
083            dnsMessage.writeTo(fos, true);
084        }
085    }
086}