peerlink/peerlink/settings.py

56 lines
1.5 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
import ed25519
from .pdict import pdict
base_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
config_path = os.path.normpath(os.path.join(base_dir, 'config'))
if not os.path.exists(config_path):
os.makedirs(config_path)
nodes_path = os.path.join(config_path, 'nodes.db')
key_path = os.path.join(config_path, 'node.key')
tls_cert_path = os.path.join(config_path, 'node.tls.crt')
tls_key_path = os.path.join(config_path, 'node.tls.key')
defaults = {
"address": "127.0.0.1",
"port": 8842,
"node_address": "",
"node_port": 8851,
"cert": "",
"directory_service": "http://[2a01:4f8:120:3201::3]:25519",
"localnode_discovery": True
}
server = pdict(os.path.join(config_path, 'settings.json'), defaults)
services = pdict(os.path.join(config_path, 'services.json'), {})
if os.path.exists(key_path):
with open(key_path, 'rb') as fd:
sk = ed25519.SigningKey(fd.read())
vk = sk.get_verifying_key()
else:
sk, vk = ed25519.create_keypair()
with open(key_path, 'wb') as fd:
os.chmod(key_path, 0o600)
fd.write(sk.to_bytes())
os.chmod(key_path, 0o400)
ENCODING='base64'
USER_ID = vk.to_ascii(encoding=ENCODING).decode()
if not os.path.exists(tls_cert_path):
from .tls import create_certificate
server['cert'] = create_certificate(tls_key_path, tls_cert_path, USER_ID)
VERSION="0.0"
USER_AGENT = 'PeerLink/%s' % VERSION
TIMEOUT = 5