Compare commits

...

8 Commits
0.5 ... main

Author SHA1 Message Date
40c4476081 Quick fix 2023-04-17 11:02:08 +02:00
890dfea5ab Quick fix 2023-04-17 10:55:01 +02:00
f39ca9c997 Merge branch 'main' of https://gitlab.pm/rune/ddns 2023-04-17 10:35:04 +02:00
099499f4c6 Fixed longer domain names for local add of existing domains 2023-04-17 10:34:57 +02:00
c6bdca6562 Type fix 2023-04-16 18:45:19 +02:00
b436703b29 Typo fix 2023-04-16 18:34:20 +02:00
6beea36361 Update version .1 2023-04-15 17:12:43 +02:00
024211f0f3 Fix issue #1 2023-04-15 17:11:35 +02:00
2 changed files with 50 additions and 24 deletions

View File

@ -4,7 +4,7 @@ _DDNS_ is a dynamic DNS helper for Digital Ocean users to utilize their DO accou
## Installation ## Installation
Download the latest relase from https://gitlab.pm/rune/ddns/releases. Unzip and move to a folder in you path (ease of use). You can alos rename the file ```ddns.py``` to just ```ddns``` and make the file executable with ```chmod +x ddns```. To install required python modules run ```pip3 install -r requirements.txt``` Download the latest relase from https://gitlab.pm/rune/ddns/releases. Unzip and move to a folder in you path (ease of use). You can also rename the file ```ddns.py``` to just ```ddns``` and make the file executable with ```chmod +x ddns```. To install required python modules run ```pip3 install -r requirements.txt```
## Usage ## Usage
@ -27,7 +27,7 @@ to discuss what you would like to change.
## Support ## Support
If you found a bug or you have sugestion for new features create an issue. If you found a bug or you have suggestion for new features create an issue.
## Future development ## Future development

70
ddns.py
View File

@ -19,7 +19,7 @@ filepath = homefilepath.joinpath('.config/ddns')
database = filepath.joinpath('ddns.db') database = filepath.joinpath('ddns.db')
logfile = filepath.joinpath('ddns.log') logfile = filepath.joinpath('ddns.log')
logging.basicConfig(filename=logfile,level=logging.INFO,format='%(message)s') logging.basicConfig(filename=logfile,level=logging.INFO,format='%(message)s')
app_version = '0.5' app_version = '0.5.2'
def get_ip(): def get_ip():
@ -129,8 +129,12 @@ def add_subdomain(domain):
print('[red]Error:[/red] Give the domain name in simple form e.g. [b]test.domain.com[/b]') print('[red]Error:[/red] Give the domain name in simple form e.g. [b]test.domain.com[/b]')
else: else:
parts = domain.split('.') parts = domain.split('.')
sub = parts[0] if len(parts) > 3:
top = parts[1] + '.' + parts[2] top = parts[1] + '.' + parts[2] + '.' + parts[3]
sub = parts[0]
else:
sub = parts[0]
top = parts[1] + '.' + parts[2]
apikey = get_api() apikey = get_api()
if apikey == None: if apikey == None:
print("[red]Error:[/red] Missing APIkey. Please add one!") print("[red]Error:[/red] Missing APIkey. Please add one!")
@ -145,9 +149,10 @@ def add_subdomain(domain):
if count == 0: if count == 0:
print('[red]Error:[/red] Top domain [bold]%s[/bold] does not exist in the DB. Please add it with [i]ddns -t %s[/i].' % (top,top)) print('[red]Error:[/red] Top domain [bold]%s[/bold] does not exist in the DB. Please add it with [i]ddns -t %s[/i].' % (top,top))
else: else:
cursor.execute('SELECT id FROM domains WHERE name LIKE ?',(top,)) cursor.execute('SELECT id,name FROM domains WHERE name LIKE ?',(top,))
topdomain_id = cursor.fetchone() topdomain = cursor.fetchone()
topdomain_id = topdomain_id[0] topdomain_id = topdomain[0]
topdomain_name = topdomain[1]
cursor.execute('SELECT count(*) FROM subdomains WHERE main_id LIKE ? AND name like ?',(topdomain_id,sub,)) cursor.execute('SELECT count(*) FROM subdomains WHERE main_id LIKE ? AND name like ?',(topdomain_id,sub,))
count = cursor.fetchone()[0] count = cursor.fetchone()[0]
if count != 0: if count != 0:
@ -174,15 +179,20 @@ def remove_subdomain(domain):
print('[red]Error:[/red] Give the domain name in simple form e.g. [b]test.domain.com[/b]') print('[red]Error:[/red] Give the domain name in simple form e.g. [b]test.domain.com[/b]')
else: else:
parts = domain.split('.') parts = domain.split('.')
sub = parts[0] if len(parts) > 3:
top = parts[1] + '.' + parts[2] top = parts[1] + '.' + parts[2] + '.' + parts[3]
sub = parts[0]
else:
sub = parts[0]
top = parts[1] + '.' + parts[2]
longtop=sub+'.'+top
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM domains WHERE name like ?',(top,)) cursor.execute('SELECT COUNT(*) FROM domains WHERE name like ? or name like ?',(top,longtop,))
count = cursor.fetchone()[0] count = cursor.fetchone()[0]
if count == 0: if count == 0:
print('[red]Error:[/red] Top domain [bold]%s[/bold] does not exist in the DB. So I\'m giving up!.' % (top)) print('[red]Error:[/red] Top domain [bold]%s[/bold] does not exist in the DB. So I\'m giving up!.' % (top))
else: else:
cursor.execute('SELECT COUNT(*) FROM subdomains WHERE name like ? and main_id=(SELECT id from domains WHERE name=?)',(sub,top,)) cursor.execute('SELECT COUNT(*) FROM subdomains WHERE name like ? and main_id=(SELECT id from domains WHERE name like ? or name like ?)',(sub,top,longtop,))
count = cursor.fetchone()[0] count = cursor.fetchone()[0]
if count == 0: if count == 0:
print('[red]Error:[/red] Domain [bold]%s[/bold] does not exist in the DB. So I\'m giving up!.' % (domain)) print('[red]Error:[/red] Domain [bold]%s[/bold] does not exist in the DB. So I\'m giving up!.' % (domain))
@ -191,7 +201,7 @@ def remove_subdomain(domain):
if apikey == None: if apikey == None:
print("[red]Error:[/red] Missing APIkey. Please add one!") print("[red]Error:[/red] Missing APIkey. Please add one!")
else: else:
cursor.execute('SELECT id FROM subdomains WHERE name like ? and main_id=(SELECT id from domains WHERE name=?)',(sub,top,)) cursor.execute('SELECT id FROM subdomains WHERE name like ? and main_id=(SELECT id from domains WHERE name like ? or name like ?)',(sub,top,longtop,))
subdomain_id = str(cursor.fetchone()[0]) subdomain_id = str(cursor.fetchone()[0])
headers = {'Authorization': 'Bearer ' + apikey, "Content-Type": "application/json"} headers = {'Authorization': 'Bearer ' + apikey, "Content-Type": "application/json"}
response = requests.delete('https://api.digitalocean.com/v2/domains/'+top+'/records/' + subdomain_id, headers=headers) response = requests.delete('https://api.digitalocean.com/v2/domains/'+top+'/records/' + subdomain_id, headers=headers)
@ -209,15 +219,20 @@ def edit_subdomain(domain):
print('[red]Error:[/red] Give the domain name in simple form e.g. [b]test.domain.com[/b]') print('[red]Error:[/red] Give the domain name in simple form e.g. [b]test.domain.com[/b]')
else: else:
parts = domain.split('.') parts = domain.split('.')
sub = parts[0] if len(parts) > 3:
top = parts[1] + '.' + parts[2] top = parts[1] + '.' + parts[2] + '.' + parts[3]
sub = parts[0]
else:
sub = parts[0]
top = parts[1] + '.' + parts[2]
longtop=sub+'.'+top
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM domains WHERE name like ?',(top,)) cursor.execute('SELECT COUNT(*) FROM domains WHERE name like ? or name like ?',(top,longtop,))
count = cursor.fetchone()[0] count = cursor.fetchone()[0]
if count == 0: if count == 0:
print('[red]Error:[/red] Top domain [bold]%s[/bold] does not exist in the DB. So I\'m giving up!.' % (top)) print('[red]Error:[/red] Top domain [bold]%s[/bold] does not exist in the DB. So I\'m giving up!.' % (top))
else: else:
cursor.execute('SELECT COUNT(*) FROM subdomains WHERE name like ? and main_id=(SELECT id from domains WHERE name=?)',(sub,top,)) cursor.execute('SELECT COUNT(*) FROM subdomains WHERE name like ? and main_id=(SELECT id from domains WHERE name like ? or name like ?)',(sub,top,longtop))
count = cursor.fetchone()[0] count = cursor.fetchone()[0]
if count == 0: if count == 0:
print('[red]Error:[/red] Domain [bold]%s[/bold] does not exist in the DB. So I\'m giving up!.' % (domain)) print('[red]Error:[/red] Domain [bold]%s[/bold] does not exist in the DB. So I\'m giving up!.' % (domain))
@ -226,7 +241,7 @@ def edit_subdomain(domain):
if apikey == None: if apikey == None:
print("[red]Error:[/red] Missing APIkey. Please add one!") print("[red]Error:[/red] Missing APIkey. Please add one!")
else: else:
cursor.execute('SELECT id,active FROM subdomains WHERE name like ? and main_id=(SELECT id from domains WHERE name=?)',(sub,top,)) cursor.execute('SELECT id,active FROM subdomains WHERE name like ? and main_id=(SELECT id from domains WHERE name like ? or name like ?)',(sub,top,longtop))
domain_info = cursor.fetchone() domain_info = cursor.fetchone()
subdomain_id = str(domain_info[0]) subdomain_id = str(domain_info[0])
status = domain_info[1] status = domain_info[1]
@ -333,8 +348,13 @@ def domaininfo(domain):
print('[red]Error:[/red]. Give the domain name in simple form e.g. [bold]test.domain.com[/bold]') print('[red]Error:[/red]. Give the domain name in simple form e.g. [bold]test.domain.com[/bold]')
else: else:
parts = domain.split('.') parts = domain.split('.')
topdomain = parts[1]+'.'+parts[2] if len(parts) > 3:
cursor.execute('SELECT id FROM domains WHERE name like ?', (topdomain,)) top = parts[1] + '.' + parts[2] + '.' + parts[3]
sub = parts[0]
else:
sub = parts[0]
top = parts[1] + '.' + parts[2]
cursor.execute('SELECT id FROM domains WHERE name like ?', (top,))
domainid = cursor.fetchone()[0] domainid = cursor.fetchone()[0]
cursor.execute('SELECT * FROM subdomains WHERE main_id like ?', (domainid,)) cursor.execute('SELECT * FROM subdomains WHERE main_id like ?', (domainid,))
domains = cursor.fetchall() domains = cursor.fetchall()
@ -471,9 +491,15 @@ def local_add_subdomain(domain,domainid):
print('[red]Error:[/red] Give the domain name in simple form e.g. [b]test.domain.com[/b]') print('[red]Error:[/red] Give the domain name in simple form e.g. [b]test.domain.com[/b]')
else: else:
parts = domain.split('.') parts = domain.split('.')
sub = parts[0] if len(parts) > 3:
top = parts[1] + '.' + parts[2] top = parts[1] + '.' + parts[2] + '.' + parts[3]
sub = parts[0]
else:
sub = parts[0]
top = parts[1] + '.' + parts[2]
apikey = get_api() apikey = get_api()
longtop=sub+'.'+top
if apikey == None: if apikey == None:
print("[red]Error:[/red] Missing APIkey. Please add one!") print("[red]Error:[/red] Missing APIkey. Please add one!")
else: else:
@ -482,12 +508,12 @@ def local_add_subdomain(domain,domainid):
print('[red]Error:[/red] Failed to get public IP. Do you have a typo in your URI? [red]Error %s.[/red]' % (ip)) print('[red]Error:[/red] Failed to get public IP. Do you have a typo in your URI? [red]Error %s.[/red]' % (ip))
else: else:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM domains WHERE name like ?',(top,)) cursor.execute('SELECT COUNT(*) FROM domains WHERE name like ? or name like ?',(top,longtop,))
count = cursor.fetchone()[0] count = cursor.fetchone()[0]
if count == 0: if count == 0:
print('[red]Error:[/red] Top domain [bold]%s[/bold] does not exist in the DB. Please add it with [i]ddns -t %s[/i].' % (top,top)) print('[red]Error:[/red] Top domain [bold]%s[/bold] does not exist in the DB. Please add it with [i]ddns -t %s[/i].' % (top,top))
else: else:
cursor.execute('SELECT id FROM domains WHERE name LIKE ?',(top,)) cursor.execute('SELECT id FROM domains WHERE name LIKE ? or name like ?',(top,longtop,))
topdomain_id = cursor.fetchone() topdomain_id = cursor.fetchone()
topdomain_id = topdomain_id[0] topdomain_id = topdomain_id[0]
cursor.execute('SELECT count(*) FROM subdomains WHERE main_id LIKE ? AND name like ?',(topdomain_id,sub,)) cursor.execute('SELECT count(*) FROM subdomains WHERE main_id LIKE ? AND name like ?',(topdomain_id,sub,))