Welcome to mirror list, hosted at ThFree Co, Russian Federation.

format.py « dnsviz - github.com/dnsviz/dnsviz.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e5d8366abd240827d15d42b481d50dc03589720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#
# This file is a part of DNSViz, a tool suite for DNS/DNSSEC monitoring,
# analysis, and visualization.
# Created by Casey Deccio (casey@deccio.net)
#
# Copyright 2012-2014 Sandia Corporation. Under the terms of Contract
# DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
# certain rights in this software.
#
# Copyright 2014-2016 VeriSign, Inc.
#
# Copyright 2016-2019 Casey Deccio
#
# DNSViz is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# DNSViz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with DNSViz.  If not, see <http://www.gnu.org/licenses/>.
#

from __future__ import unicode_literals

import calendar
import codecs
import datetime
import re
import time

import dns.name, dns.rdatatype

DNSKEY_FLAGS = {'ZONE': 0x0100, 'SEP': 0x0001, 'revoke': 0x0080}
DNSKEY_PROTOCOLS = { 3: 'DNSSEC' }
DNSKEY_ALGORITHMS = { 1: 'RSA/MD5', 2: 'Diffie-Hellman', 3: 'DSA/SHA1', 5: 'RSA/SHA-1', 6: 'DSA-NSEC3-SHA1', 7: 'RSASHA1-NSEC3-SHA1', \
        8: 'RSA/SHA-256', 10: 'RSA/SHA-512', 12: 'GOST R 34.10-2001', 13: 'ECDSA Curve P-256 with SHA-256', 14: 'ECDSA Curve P-384 with SHA-384',
        15: 'Ed25519', 16: 'Ed448' }
DS_DIGEST_TYPES = { 1: 'SHA-1', 2: 'SHA-256', 3: 'GOST 34.11-94', 4: 'SHA-384' }

NSEC3_FLAGS = {'OPTOUT': 0x01}

DNS_FLAG_DESCRIPTIONS = {
        32768: 'Query Response', 1024: 'Authoritative Answer', 512: 'Truncated Response',
        256: 'Recursion Desired', 128: 'Recursion Available', 32: 'Authentic Data', 16: 'Checking Disabled'
}

EDNS_FLAG_DESCRIPTIONS = { 32768: 'DNSSEC answer OK' }

EDNS_OPT_DESCRIPTIONS = { 3: 'NSID', 8: 'edns-client-subnet', 10: 'COOKIE' }

FMT_MS = '%Y-%m-%d %H:%M:%S.%f %Z'
FMT_NO_MS = '%Y-%m-%d %H:%M:%S %Z'

ZERO = datetime.timedelta(0)
class UTC(datetime.tzinfo):
    '''UTC'''

    def utcoffset(self, dt):
        return ZERO

    def tzname(self, dt):
        # python3/python2 dual compatibility
        if type(b'') is str:
            return b'UTC'
        else:
            return 'UTC'

    def dst(self, dt):
        return ZERO

utc = UTC()

#################
# Timestamp conversions
def timestamp_to_datetime(timestamp, tz=utc):
    return datetime.datetime.fromtimestamp(timestamp, tz)

def datetime_to_timestamp(dt):
    return calendar.timegm(dt.timetuple()) + dt.microsecond/1.0e6

def str_to_datetime(s, tz=utc):
    return timestamp_to_datetime(str_to_timestamp(s), tz)

def str_to_timestamp(s):
    try:
        return calendar.timegm(time.strptime(s, FMT_NO_MS))
    except ValueError:
        return calendar.timegm(time.strptime(s, FMT_MS))

def datetime_to_str(dt):
    if dt.microsecond:
        return dt.strftime(FMT_MS)
    else:
        return dt.strftime(FMT_NO_MS)

def timestamp_to_str(timestamp):
    return datetime_to_str(timestamp_to_datetime(timestamp))

#################
# Human representation of time
def humanize_time(seconds, days=None):
    if days is None:
        days, remainder = divmod(seconds, 86400)
    else:
        remainder = seconds
    hours, remainder = divmod(remainder, 3600)
    minutes, seconds = divmod(remainder, 60)

    output = ''
    if days > 0:
        if days != 1:
            plural = 's'
        else:
            plural = ''
        output += '%d day%s' % (days, plural)
    else:
        if hours > 0:
            if hours != 1:
                plural = 's'
            else:
                plural = ''
            output += '%d hour%s' % (hours, plural)
        if minutes > 0:
            if output:
                output += ', '
            if minutes != 1:
                plural = 's'
            else:
                plural = ''
            output += '%d minute%s' % (minutes, plural)
        if not output:
            if seconds != 1:
                plural = 's'
            else:
                plural = ''
            output += '%d second%s' % (seconds, plural)
    return output

def format_diff(date_now, date_relative):
    if date_now > date_relative:
        diff = date_now - date_relative
        suffix = 'in the past'
    else:
        diff = date_relative - date_now
        suffix = 'in the future'
    return '%s %s' % (humanize_time(diff.seconds, diff.days), suffix)

#################
# Human representation of DNS names
def format_nsec3_name(name):
    return lb2s(dns.name.from_text(name.labels[0].upper(), name.parent().canonicalize()).to_text())

def format_nsec3_rrset_text(nsec3_rrset_text):
    return re.sub(r'^(\d+\s+\d+\s+\d+\s+\S+\s+)([0-9a-zA-Z]+)', lambda x: '%s%s' % (x.group(1), x.group(2).upper()), nsec3_rrset_text).rstrip('.')

def humanize_name(name, idn=False, canonicalize=True):
    if canonicalize:
        name = name.canonicalize()
    if idn:
        try:
            name = name.to_unicode()
        except UnicodeError:
            name = lb2s(name.to_text())
    else:
        name = lb2s(name.to_text())
    if name == '.':
        return name
    return name.rstrip('.')

def latin1_binary_to_string(s):
    # python3/python2 dual compatibility
    #XXX In places where this method wraps calls to dns.name.Name.to_text(),
    # this is no longer needed with dnspython 1.15.0
    if isinstance(s, bytes):
        return codecs.decode(s, 'latin1')
    return s
lb2s = latin1_binary_to_string