<commit_before>#!/usr/bin/env python3

import time
from lib.tuntap import TunThread
from lib import dns
from lib import query
from lib.packet import Packet, PacketPool

hostname = b'vpn.bgpat.net'
query.Field.HostName.default = hostname
query.Field.HostName.pattern = hostname
Packet.hostname = hostname

txpool = PacketPool()
rxpool = PacketPool()


class VPNServer(TunThread):
    daemon = True
    name = 'tun0'
    addr = '192.168.200.1'
    gateway = '192.168.200.1'

    def receive(self, data):
        global rxpool
        pkt = Packet(data)
        rxpool.push(pkt)


tun = VPNServer()
tun.start()


class DNSServer(dns.ServerThread):
    daemon = True

    def receive(self, req, addr, port, data):
        txInit = query.TxInitialize(req)
        txSend = query.TxSend(req)
        rxRecv = query.Receive(req)
        rxPoll = query.Polling(req)
        if txInit.params is not None:
            params = txInit.params
            res = self.txinit(params['id'], params['count'])
            print('txinit', params)
        elif txSend.params is not None:
            params = txSend.params
            res = self.txsend(params['id'], params['sequence'], params['data'])
            print('txsend', params)
        elif rxRecv.params is not None:
            params = rxRecv.params
            res = self.rxrecv(params['id'], params['sequence'])
            print('rxrecv', params)
        elif rxPoll.params is not None:
            res = self.rxpoll()
            print('rxpoll')
        else:
            res = query.Error()
        return {
            'value': bytes(res),
            'type': res.type,
        }

    def txinit(self, id, count):
        global txpool
        txpool[id] = Packet(count)
        return query.Ok(count=count, sequence=count)

    def txsend(self, id, seq, data):
        global txpool
        pkt = txpool[id]
        pkt[seq] = data
        remain = pkt.count - len(pkt)
        if not remain:
            tun.send(pkt.unpack())
            del txpool[id]
        return query.Ok(count=count, sequence=seq)

    def rxrecv(self, id, seq):
        global rxpool
        pkt = rxpool[id]
        if seq in pkt:<commit_msg>Fix count<commit_after>#!/usr/bin/env python3

import time
from lib.tuntap import TunThread
from lib import dns
from lib import query
from lib.packet import Packet, PacketPool

hostname = b'vpn.bgpat.net'
query.Field.HostName.default = hostname
query.Field.HostName.pattern = hostname
Packet.hostname = hostname

txpool = PacketPool()
rxpool = PacketPool()


class VPNServer(TunThread):
    daemon = True
    name = 'tun0'
    addr = '192.168.200.1'
    gateway = '192.168.200.1'

    def receive(self, data):
        global rxpool
        pkt = Packet(data)
        rxpool.push(pkt)


tun = VPNServer()
tun.start()


class DNSServer(dns.ServerThread):
    daemon = True

    def receive(self, req, addr, port, data):
        txInit = query.TxInitialize(req)
        txSend = query.TxSend(req)
        rxRecv = query.Receive(req)
        rxPoll = query.Polling(req)
        if txInit.params is not None:
            params = txInit.params
            res = self.txinit(params['id'], params['count'])
            print('txinit', params)
        elif txSend.params is not None:
            params = txSend.params
            res = self.txsend(params['id'], params['sequence'], params['data'])
            print('txsend', params)
        elif rxRecv.params is not None:
            params = rxRecv.params
            res = self.rxrecv(params['id'], params['sequence'])
            print('rxrecv', params)
        elif rxPoll.params is not None:
            res = self.rxpoll()
            print('rxpoll')
        else:
            res = query.Error()
        return {
            'value': bytes(res),
            'type': res.type,
        }

    def txinit(self, id, count):
        global txpool
        txpool[id] = Packet(count)
        return query.Ok(count=count, sequence=count)

    def txsend(self, id, seq, data):
        global txpool
        pkt = txpool[id]
        pkt[seq] = data
        remain = pkt.count - len(pkt)
        if not remain:
            tun.send(pkt.unpack())
            del txpool[id]
        return query.Ok(count=remain, sequence=seq)

    def rxrecv(self, id, seq):
        global rxpool
        pkt = rxpool[id]
        if seq in pkt:
{'fraction_non_alphanumeric': 0.11574821305049574, 'fraction_numerical': 0.010145261701637076, 'mean_word_length': 2.080965909090909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '1935441', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1360, 'n_words': 342}