2022-04-01 09:18:18 -06:00
|
|
|
# MIT License
|
|
|
|
#
|
|
|
|
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
2016-06-03 11:02:02 -06:00
|
|
|
import os
|
2018-04-16 14:04:28 -06:00
|
|
|
import sys
|
2016-06-03 11:02:02 -06:00
|
|
|
import glob
|
2018-03-19 09:39:08 -06:00
|
|
|
import time
|
2019-11-10 05:56:04 -07:00
|
|
|
import random
|
2021-05-03 12:24:44 -06:00
|
|
|
import threading
|
2016-06-03 11:02:02 -06:00
|
|
|
|
2021-08-19 06:10:37 -06:00
|
|
|
from ._version import __version__
|
|
|
|
|
2018-04-04 06:14:22 -06:00
|
|
|
from .Reticulum import Reticulum
|
2018-03-16 03:50:37 -06:00
|
|
|
from .Identity import Identity
|
2021-09-02 12:35:42 -06:00
|
|
|
from .Link import Link, RequestReceipt
|
2018-03-16 03:50:37 -06:00
|
|
|
from .Transport import Transport
|
2018-03-19 09:39:08 -06:00
|
|
|
from .Destination import Destination
|
|
|
|
from .Packet import Packet
|
2018-04-17 09:46:48 -06:00
|
|
|
from .Packet import PacketReceipt
|
2021-08-21 06:52:31 -06:00
|
|
|
from .Resource import Resource, ResourceAdvertisement
|
2022-06-07 07:48:23 -06:00
|
|
|
from .Cryptography import HKDF
|
|
|
|
from .Cryptography import Hashes
|
2018-03-16 03:50:37 -06:00
|
|
|
|
2016-06-03 11:02:02 -06:00
|
|
|
modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
2018-03-19 09:39:08 -06:00
|
|
|
__all__ = [ os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')]
|
|
|
|
|
|
|
|
LOG_CRITICAL = 0
|
|
|
|
LOG_ERROR = 1
|
|
|
|
LOG_WARNING = 2
|
|
|
|
LOG_NOTICE = 3
|
|
|
|
LOG_INFO = 4
|
|
|
|
LOG_VERBOSE = 5
|
|
|
|
LOG_DEBUG = 6
|
2018-04-23 15:42:16 -06:00
|
|
|
LOG_EXTREME = 7
|
2018-03-19 09:39:08 -06:00
|
|
|
|
2020-08-13 04:15:56 -06:00
|
|
|
LOG_STDOUT = 0x91
|
2018-03-19 09:39:08 -06:00
|
|
|
LOG_FILE = 0x92
|
|
|
|
|
2021-10-08 11:23:10 -06:00
|
|
|
LOG_MAXSIZE = 5*1024*1024
|
|
|
|
|
2020-08-13 04:15:56 -06:00
|
|
|
loglevel = LOG_NOTICE
|
2018-03-19 09:39:08 -06:00
|
|
|
logfile = None
|
|
|
|
logdest = LOG_STDOUT
|
|
|
|
logtimefmt = "%Y-%m-%d %H:%M:%S"
|
|
|
|
|
2022-04-27 11:00:09 -06:00
|
|
|
instance_random = random.Random()
|
|
|
|
instance_random.seed(os.urandom(10))
|
2019-11-10 05:56:04 -07:00
|
|
|
|
2021-05-03 12:24:44 -06:00
|
|
|
_always_override_destination = False
|
|
|
|
|
|
|
|
logging_lock = threading.Lock()
|
|
|
|
|
2018-03-19 09:39:08 -06:00
|
|
|
def loglevelname(level):
|
2020-08-13 04:15:56 -06:00
|
|
|
if (level == LOG_CRITICAL):
|
|
|
|
return "Critical"
|
|
|
|
if (level == LOG_ERROR):
|
|
|
|
return "Error"
|
|
|
|
if (level == LOG_WARNING):
|
|
|
|
return "Warning"
|
|
|
|
if (level == LOG_NOTICE):
|
|
|
|
return "Notice"
|
|
|
|
if (level == LOG_INFO):
|
|
|
|
return "Info"
|
|
|
|
if (level == LOG_VERBOSE):
|
|
|
|
return "Verbose"
|
|
|
|
if (level == LOG_DEBUG):
|
|
|
|
return "Debug"
|
|
|
|
if (level == LOG_EXTREME):
|
|
|
|
return "Extra"
|
|
|
|
|
|
|
|
return "Unknown"
|
2018-03-19 09:39:08 -06:00
|
|
|
|
2021-08-19 06:10:37 -06:00
|
|
|
def version():
|
|
|
|
return __version__
|
|
|
|
|
2021-12-01 03:40:44 -07:00
|
|
|
def host_os():
|
|
|
|
from .vendor.platformutils import get_platform
|
|
|
|
return get_platform()
|
|
|
|
|
2022-04-20 02:40:51 -06:00
|
|
|
def timestamp_str(time_s):
|
|
|
|
timestamp = time.localtime(time_s)
|
|
|
|
return time.strftime(logtimefmt, timestamp)
|
|
|
|
|
2021-05-03 12:24:44 -06:00
|
|
|
def log(msg, level=3, _override_destination = False):
|
|
|
|
global _always_override_destination
|
|
|
|
|
2020-08-13 04:15:56 -06:00
|
|
|
if loglevel >= level:
|
2022-04-20 02:40:51 -06:00
|
|
|
logstring = "["+timestamp_str(time.time())+"] ["+loglevelname(level)+"] "+msg
|
2021-05-03 12:24:44 -06:00
|
|
|
logging_lock.acquire()
|
2018-03-19 09:39:08 -06:00
|
|
|
|
2021-08-19 06:10:37 -06:00
|
|
|
if (logdest == LOG_STDOUT or _always_override_destination or _override_destination):
|
2020-08-13 04:15:56 -06:00
|
|
|
print(logstring)
|
2021-05-03 12:24:44 -06:00
|
|
|
logging_lock.release()
|
2018-03-19 09:39:08 -06:00
|
|
|
|
2021-05-03 12:24:44 -06:00
|
|
|
elif (logdest == LOG_FILE and logfile != None):
|
|
|
|
try:
|
|
|
|
file = open(logfile, "a")
|
|
|
|
file.write(logstring+"\n")
|
|
|
|
file.close()
|
2021-10-08 11:23:10 -06:00
|
|
|
|
|
|
|
if os.path.getsize(logfile) > LOG_MAXSIZE:
|
|
|
|
prevfile = logfile+".1"
|
|
|
|
if os.path.isfile(prevfile):
|
|
|
|
os.unlink(prevfile)
|
|
|
|
os.rename(logfile, prevfile)
|
|
|
|
|
2021-05-03 12:24:44 -06:00
|
|
|
logging_lock.release()
|
|
|
|
except Exception as e:
|
|
|
|
logging_lock.release()
|
|
|
|
_always_override_destination = True
|
|
|
|
log("Exception occurred while writing log message to log file: "+str(e), LOG_CRITICAL)
|
|
|
|
log("Dumping future log events to console!", LOG_CRITICAL)
|
|
|
|
log(msg, level)
|
|
|
|
|
2018-03-19 11:11:50 -06:00
|
|
|
|
2019-11-10 05:56:04 -07:00
|
|
|
def rand():
|
2022-04-27 11:00:09 -06:00
|
|
|
result = instance_random.random()
|
2020-08-13 04:15:56 -06:00
|
|
|
return result
|
2019-11-10 05:56:04 -07:00
|
|
|
|
2018-03-19 11:11:50 -06:00
|
|
|
def hexrep(data, delimit=True):
|
2022-01-22 13:36:49 -07:00
|
|
|
try:
|
|
|
|
iter(data)
|
|
|
|
except TypeError:
|
|
|
|
data = [data]
|
|
|
|
|
2020-08-13 04:15:56 -06:00
|
|
|
delimiter = ":"
|
|
|
|
if not delimit:
|
|
|
|
delimiter = ""
|
|
|
|
hexrep = delimiter.join("{:02x}".format(c) for c in data)
|
|
|
|
return hexrep
|
2018-03-20 05:32:41 -06:00
|
|
|
|
|
|
|
def prettyhexrep(data):
|
2020-08-13 04:15:56 -06:00
|
|
|
delimiter = ""
|
|
|
|
hexrep = "<"+delimiter.join("{:02x}".format(c) for c in data)+">"
|
|
|
|
return hexrep
|
2018-04-16 14:04:28 -06:00
|
|
|
|
2022-06-09 06:46:36 -06:00
|
|
|
def prettysize(num, suffix='B'):
|
|
|
|
units = ['','K','M','G','T','P','E','Z']
|
|
|
|
last_unit = 'Y'
|
|
|
|
|
|
|
|
if suffix == 'b':
|
|
|
|
num *= 8
|
|
|
|
units = ['','K','M','G','T','P','E','Z']
|
|
|
|
last_unit = 'Y'
|
|
|
|
|
|
|
|
for unit in units:
|
|
|
|
if abs(num) < 1000.0:
|
|
|
|
if unit == "":
|
|
|
|
return "%.0f %s%s" % (num, unit, suffix)
|
|
|
|
else:
|
|
|
|
return "%.2f %s%s" % (num, unit, suffix)
|
|
|
|
num /= 1000.0
|
|
|
|
|
|
|
|
return "%.2f%s%s" % (num, last_unit, suffix)
|
|
|
|
|
2018-04-16 14:04:28 -06:00
|
|
|
def panic():
|
2021-09-24 08:09:07 -06:00
|
|
|
os._exit(255)
|
|
|
|
|
|
|
|
def exit():
|
2021-11-04 10:14:58 -06:00
|
|
|
print("")
|
2021-09-24 08:09:07 -06:00
|
|
|
sys.exit(0)
|