Skip to content

Commit

Permalink
Namecoin: Add support for SRV DNS records
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Dec 12, 2019
1 parent f1d6ad1 commit 29a7aea
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions electrum_nmc/electrum/gui/qt/configure_dns_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __init__(self, value, parent):
self.ui.btnTLSCreate.clicked.connect(self.create_tls_record)
self.ui.btnSSHFPCreate.clicked.connect(self.create_sshfp_record)
self.ui.btnTXTCreate.clicked.connect(self.create_txt_record)
self.ui.btnSRVCreate.clicked.connect(self.create_srv_record)

self.ui.dialogButtons.accepted.connect(self.accept)
self.ui.dialogButtons.rejected.connect(self.reject)
Expand Down Expand Up @@ -258,6 +259,26 @@ def create_txt_record(self):

self.insert_record(idx, record)

def create_srv_record(self):
model = self.ui.listDNSRecords.model()
idx = model.rowCount()

domain = self.get_selected_domain()
try:
data = [
int(self.ui.editSRVPriority.text()),
int(self.ui.editSRVWeight.text()),
int(self.ui.editSRVPort.text()),
self.ui.editSRVHost.text(),
]
except ValueError:
self.show_error(_("The Priority, Weight, and Port must be integers."))
return

record = [domain, "srv", data]

self.insert_record(idx, record)

def has_freenet_record(self, domain):
for record in self.get_records():
record_domain, record_type, data = record
Expand Down Expand Up @@ -324,6 +345,9 @@ def insert_record(self, idx, record):
elif record_type == "txt":
formatted_record_type = "TXT"
formatted_data = json.dumps(data)
elif record_type == "srv":
formatted_record_type = "SRV"
formatted_data = json.dumps(data)
else:
raise Exception("Unknown record type")

Expand Down
48 changes: 48 additions & 0 deletions electrum_nmc/electrum/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,12 @@ def get_domain_records(domain, value):
if value["txt"] == []:
del value["txt"]

if "srv" in value:
new_records, value["srv"] = get_domain_records_srv(domain, value["srv"])
records.extend(new_records)
if value["srv"] == []:
del value["srv"]

if "map" in value:
new_records, value["map"] = get_domain_records_map(domain, value["map"])
records.extend(new_records)
Expand Down Expand Up @@ -908,6 +914,38 @@ def get_domain_records_txt_single(domain, value):

return [domain, "txt", value], None

def get_domain_records_srv(domain, value):
# Must be array
if type(value) != list:
return [], value

# Parse each array item
records = []
remaining = []
for raw_address in value:
single_record, single_remaining = get_domain_records_srv_single(domain, raw_address)
if single_record is not None:
records.append(single_record)
if single_remaining is not None:
remaining.append(single_remaining)

return records, remaining

def get_domain_records_srv_single(domain, value):
# Must be array
if type(value) != list:
return None, value

# Must be length 4
if len(value) != 4:
return None, value

# Check value types
if type(value[0]) != int or type(value[1]) != int or type(value[2]) != int or type(value[3]) != str:
return None, value

return [domain, "srv", value], None

def get_domain_records_map(domain, value):
# Must be dict
if type(value) != dict:
Expand Down Expand Up @@ -984,6 +1022,8 @@ def add_domain_record(base_domain, value, record):
add_domain_record_sshfp(subdomain_value, data)
elif record_type == "txt":
add_domain_record_txt(subdomain_value, data)
elif record_type == "srv":
add_domain_record_srv(subdomain_value, data)

def add_domain_record_map(value, map_labels):
if len(map_labels) == 0:
Expand Down Expand Up @@ -1107,6 +1147,14 @@ def add_domain_record_txt(value, data):
# Add the record
value["txt"].append(data)

def add_domain_record_srv(value, data):
# Make sure the field exists
if "srv" not in value:
value["srv"] = []

# Add the record
value["srv"].append(data)


import binascii
from datetime import datetime, timedelta
Expand Down

0 comments on commit 29a7aea

Please sign in to comment.