<commit_before>package net.plan99.bitcoin.cartographer

import kotlin.concurrent.thread
import org.slf4j.LoggerFactory
import org.xbill.DNS.*
import java.net.*

// A small, simple DNS server.
class DnsServer(private val dnsName: String, private val port: Int, private val crawler: Crawler) {
    private val log = LoggerFactory.getLogger("cartographer.dnsserver")

    public fun start() {
        thread(start = true, daemon = true, name = "DNS UDP", block = {
            val socket = DatagramSocket(port.toInt())
            val inBits = ByteArray(512)
            val inPacket = DatagramPacket(inBits, inBits.size())
            while (true) {
                try {
                    inPacket.setLength(inBits.size())
                    socket.receive(inPacket)
                    val outBits = processMessage(Message(inBits))
                    val outPacket = DatagramPacket(outBits, outBits.size(), inPacket.getSocketAddress())
                    socket.send(outPacket)
                } catch (e: Exception) {
                    log.error("Error handling DNS request", e)
                }
            }
        })
    }

    fun processMessage(message: Message): ByteArray {
        if (message.getHeader().getOpcode() != Opcode.QUERY)
            return errorMessage(message, Rcode.NOTIMP)
        val response = Message(message.getHeader().getID())
        response.getHeader().setFlag(Flags.QR.toInt());
        val ips = crawler.getSomePeers(30, -1)
        for (ip in ips) {
            val ipaddr = ip.first.getAddress()
            try {
                if (ipaddr is Inet4Address)
                    response.addRecord(ARecord(Name(dnsName), 1, 10, ipaddr), Section.ANSWER)
                else if (ipaddr is Inet6Address)
                    response.addRecord(AAAARecord(Name(dnsName), 1, 10, ipaddr), Section.ANSWER)
            } catch(e: Exception) {
                log.error("Failed to add record for ${ipaddr}: ${e}")
            }
        }
        return response.toWire()
    }

    fun errorMessage(query: Message, code: Int): ByteArray {
        val msg = Message()
        val hdr = query.getHeader()
        hdr.setRcode(code)<commit_msg>Set TTL to 60 seconds<commit_after>package net.plan99.bitcoin.cartographer

import kotlin.concurrent.thread
import org.slf4j.LoggerFactory
import org.xbill.DNS.*
import java.net.*

// A small, simple DNS server.
class DnsServer(private val dnsName: String, private val port: Int, private val crawler: Crawler) {
    private val log = LoggerFactory.getLogger("cartographer.dnsserver")

    public fun start() {
        thread(start = true, daemon = true, name = "DNS UDP", block = {
            val socket = DatagramSocket(port.toInt())
            val inBits = ByteArray(512)
            val inPacket = DatagramPacket(inBits, inBits.size())
            while (true) {
                try {
                    inPacket.setLength(inBits.size())
                    socket.receive(inPacket)
                    val outBits = processMessage(Message(inBits))
                    val outPacket = DatagramPacket(outBits, outBits.size(), inPacket.getSocketAddress())
                    socket.send(outPacket)
                } catch (e: Exception) {
                    log.error("Error handling DNS request", e)
                }
            }
        })
    }

    fun processMessage(message: Message): ByteArray {
        if (message.getHeader().getOpcode() != Opcode.QUERY)
            return errorMessage(message, Rcode.NOTIMP)
        val response = Message(message.getHeader().getID())
        response.getHeader().setFlag(Flags.QR.toInt());
        val ips = crawler.getSomePeers(30, -1)
        for (ip in ips) {
            val ipaddr = ip.first.getAddress()
            val TTL = 60L  // seconds
            try {
                if (ipaddr is Inet4Address)
                    response.addRecord(ARecord(Name(dnsName), 1, TTL, ipaddr), Section.ANSWER)
                else if (ipaddr is Inet6Address)
                    response.addRecord(AAAARecord(Name(dnsName), 1, TTL, ipaddr), Section.ANSWER)
            } catch(e: Exception) {
                log.error("Failed to add record for ${ipaddr}: ${e}")
            }
        }
        return response.toWire()
    }

    fun errorMessage(query: Message, code: Int): ByteArray {
        val msg = Message()
        val hdr = query.getHeader()
        hdr.setRcode(code)
{'fraction_non_alphanumeric': 0.10831062670299728, 'fraction_numerical': 0.007720254314259764, 'mean_word_length': 1.9683288409703503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4001447', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1223, 'n_words': 295}