Optimize imports

This commit is contained in:
meemo committed 2022-02-06 23:47:48 -05:00
1 parent fa9c7df4fe
commit 77b4ab8fc7
1 file changed
+21 -18
+21 -18
View File
@@ -1,16 +1,17 @@
# sudomemoDNS # sudomemoDNS
from datetime import datetime from datetime import datetime
from json import loads
from socket import socket, AF_INET, SOCK_DGRAM
from sys import platform
from time import sleep from time import sleep
from dnslib import DNSLabel, QTYPE, RD, RR
from dnslib import A, AAAA, CNAME, MX, NS, SOA, TXT from dnslib import A, AAAA, CNAME, MX, NS, SOA, TXT
from dnslib import DNSLabel, QTYPE, RD, RR
from dnslib.server import DNSServer from dnslib.server import DNSServer
from requests import get
import socket from requests.exceptions import RequestException
import requests from requests.exceptions import Timeout
import json
import sys
def get_platform(): def get_platform():
@@ -20,10 +21,10 @@ def get_platform():
'darwin': 'macOS', 'darwin': 'macOS',
'win32': 'Windows' 'win32': 'Windows'
} }
if sys.platform not in platforms: if platform not in platforms:
return sys.platform return platform
return platforms[sys.platform] return platforms[platform]
SUDOMEMODNS_VERSION = "1.2.1" SUDOMEMODNS_VERSION = "1.2.1"
@@ -37,7 +38,7 @@ def format_ip(address):
def get_ip(): def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s = socket(AF_INET, SOCK_DGRAM)
try: try:
# doesn't even have to be reachable # doesn't even have to be reachable
s.connect(('10.255.255.255', 1)) s.connect(('10.255.255.255', 1))
@@ -164,16 +165,16 @@ class Record:
ZONES = {} ZONES = {}
try: try:
get_zones = requests.get("https://www.sudomemo.net/api/dns_zones.json", get_zones = get("https://www.sudomemo.net/api/dns_zones.json",
headers={'User-Agent': 'SudomemoDNS/' + SUDOMEMODNS_VERSION + ' (' + get_platform() + ')'}) headers={'User-Agent': 'SudomemoDNS/' + SUDOMEMODNS_VERSION + ' (' + get_platform() + ')'})
except requests.exceptions.Timeout: except Timeout:
print("[ERROR] Unable to load DNS data: Connection to Sudomemo timed out. Are you connected to the Internet?") print("[ERROR] Unable to load DNS data: Connection to Sudomemo timed out. Are you connected to the Internet?")
except requests.exceptions.RequestException as e: except RequestException as e:
print("[ERROR] Unable load DNS data.") print("[ERROR] Unable load DNS data.")
print("[ERROR] Exception: ", e) print("[ERROR] Exception: ", e)
sys.exit(1) exit(1)
try: try:
zones = json.loads(get_zones.text) zones = loads(get_zones.text)
except ValueError as e: except ValueError as e:
print("[ERROR] Unable load DNS data: Invalid response from server. Check that you can visit sudomemo.net") print("[ERROR] Unable load DNS data: Invalid response from server. Check that you can visit sudomemo.net")
@@ -227,8 +228,10 @@ elif get_platform() == 'macOS':
print("[INFO] If you aren't seeing any requests, check that this is the case first with lsof -i:53 (requires lsof)") print("[INFO] If you aren't seeing any requests, check that this is the case first with lsof -i:53 (requires lsof)")
print("[INFO] To run as root, prefix the command with 'sudo'") print("[INFO] To run as root, prefix the command with 'sudo'")
elif get_platform() == 'Windows': elif get_platform() == 'Windows':
print("[INFO] Please note that you may have to allow this application through the firewall. If so, a popup will appear in a moment.") print(
print("[INFO] If you are not seeing any requests, make sure you have allowed this application through the firewall. If you have already done so, disregard this message.") "[INFO] Please note that you may have to allow this application through the firewall. If so, a popup will appear in a moment.")
print(
"[INFO] If you are not seeing any requests, make sure you have allowed this application through the firewall. If you have already done so, disregard this message.")
try: try:
servers = [ servers = [
@@ -238,7 +241,7 @@ try:
except PermissionError: except PermissionError:
print("[ERROR] Permission error: Check that you are running this as an administrator or root") print("[ERROR] Permission error: Check that you are running this as an administrator or root")
sys.exit(1) exit(1)
print("[INFO] sudomemoDNS is ready. Now waiting for DNS requests from your console...") print("[INFO] sudomemoDNS is ready. Now waiting for DNS requests from your console...")