new file: client/clientconf.yaml

modified:   client/main.py
	new file:   node/nodeconf.yaml
	modified:   server/main.py
	new file:   server/serverconf.yaml
	modified:   server/xiaomiandns.py
This commit is contained in:
Smart-SangGe 2023-04-07 19:52:19 +08:00
parent 36f8324677
commit f05335c44e
6 changed files with 94 additions and 47 deletions

0
client/clientconf.yaml Normal file
View File

View File

@ -39,6 +39,7 @@ def generate_key():
return private_key_base64,public_key_base64
# # Encrypt a message using the public key
# message = b"Hello World"
# encrypted_message = public_key.encrypt(

0
node/nodeconf.yaml Normal file
View File

View File

@ -1,10 +1,15 @@
import xiaomiandns
import yaml
if __name__ == '__main__':
db_file = '../database/dns.db'
DNS_port = 53
listen_host= "0.0.0.0"
with open('serverconf.yaml', 'r') as f:
config = yaml.safe_load(f)
db_file = config['database']['db_file']
DNS_port = config['DNS']['port']
DNS_listen_host = config['DNS']['listen_host']
API_port = config['API']['port']
API_listen_host = config['API']['listen_host']
DNSServer = xiaomiandns.DNSServer(listen_host, DNS_port, db_file)
DNSServer.run()

8
server/serverconf.yaml Normal file
View File

@ -0,0 +1,8 @@
database:
db_file : '../database/dns.db'
DNS:
port : 53
listen_host : "0.0.0.0"
API:
port : 81
listen_host : "0.0.0.0"

View File

@ -11,6 +11,9 @@ import time
import sqlite3
import re
import base64
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
class DNSServer:
@ -93,7 +96,6 @@ class DNSAPI:
# /delete
# data: domian=xxxx&ip=xx.xx.xx.xx&prikey=xxxxx&nodetype=xxxx
def __init__(self, hostname, port, db_file):
self.hostname = hostname
self.port = port
@ -190,12 +192,12 @@ class DNSAPI:
response = 'HTTP/1.1 {} {}\r\n'.format(status_code, reason_phrase)
return response.encode("utf-8")
def add_data(self, url):
def add_data(self, data):
# parse and check validation
domain, ip, pubkey, nodetype = parse_data(url)
domain, ip, pubkey, nodetype = self.parse_data(data)
if not check_data(url):
if not self.check_data(domain,ip,nodetype):
return 400
# connect db
@ -218,42 +220,61 @@ class DNSAPI:
"INSERT INTO xiaomiandns (domain,ip,pubkey,nodetype,timestamp) VALUES (?,?,?,?,DATETIME('now'))", (domain, ip, pubkey, nodetype))
return 200
def delete_data(self, url):
def delete_data(self, data):
# parse and check validation
domain, ip, privkey, nodetype = parse_data(url)
domain, ip, private_key_base64, nodetype = self.parse_data(data)
if not check_data(url):
if not self.check_data(domain, ip ,nodetype):
return 400
# connect db
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(
"SELECT pubkey FROM xiaomiandns WHERE domain = ? AND ip = ? AND nodetype = ?", (domain, ip, pubkey, nodetype))
pubkey = c.fetchone()[0]
pubkey = pubkey
"SELECT pubkey FROM xiaomiandns WHERE domain = ? AND ip = ? AND nodetype = ?", (domain, ip, nodetype))
public_key_base64 = c.fetchone()
cursor.close()
conn.close()
if existing_data:
return 400
if public_key_base64 != None:
public_key_base64 = public_key_base64[0]
else:
# Insert the new data
return 400
private_key_bytes = base64.b64decode(
private_key_base64).decode("utf-8")
private_key = serialization.load_pem_private_key(
private_key_bytes,
password=None
)
gen_public_key = private_key.public_key()
gen_public_key_bytes = gen_public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
gen_public_key_base64 = base64.b64encode(gen_public_key_bytes).decode('utf-8')
if gen_public_key_base64 == public_key_base64:
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(
"INSERT INTO xiaomiandns (domain,ip,pubkey,nodetype,timestamp) VALUES (?,?,?,?,DATETIME('now'))", (domain, ip, pubkey, nodetype))
"DELETE FROM xiaomiandns WHERE domain = ? AND ip = ? AND nodetype = ?", (domain, ip, nodetype))
cursor.close()
conn.close()
return 200
else:
return 400
def parse_data(self, url):
def parse_data(self, data):
domain = re.search(r'domain=([^&]+)', url)
ip = re.search(r'ip=([^&]+)', url)
pubkey = re.search(r'pubkey=([^&]+)', url)
privkey = re.search(r'privkey=([^&]+)', url)
nodetype = re.search(r'nodetype=([^]+)', url)
domain = re.search(r'domain=([^&]+)', data)
ip = re.search(r'ip=([^&]+)', data)
pubkey = re.search(r'pubkey=([^&]+)', data)
privkey = re.search(r'privkey=([^&]+)', data)
nodetype = re.search(r'nodetype=([^]+)', data)
if domain and ip and nodetype:
domain = domain.group(1)
@ -292,17 +313,29 @@ class DNSAPI:
if __name__ == '__main__':
with open('serverconf.yaml', 'r') as f:
config = yaml.safe_load(f)
db_file = config['database']['db_file']
DNS_port = config['DNS']['port']
DNS_listen_host = config['DNS']['listen_host']
API_port = config['API']['port']
API_listen_host = config['API']['listen_host']
# some config
db_file = '../database/dns.db'
DNS_port = 53
listen_host = "0.0.0.0"
API_port = 81
# start dns server
server = DNSServer(listen_host, DNS_port, db_file)
server = DNSServer(API_listen_host, DNS_port, db_file)
server.run()
# start dns api server
APIserver = DNSAPI(listen_host, API_port, db_file)
APIserver = DNSAPI(API_listen_host, API_port, db_file)
APIserver.run()