Skip to content
Snippets Groups Projects
Commit 8129f7e1 authored by Ferenc Schulcz's avatar Ferenc Schulcz
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
__pycache__
import services
import db
import os
import requests
import datetime
plugin_dir = os.path.dirname(__file__) # parent folder name of the absolute path of this file
def register(add_endpoint, add_postable_endpoint, add_menu):
add_endpoint('dyndns', dyndns)
add_menu('dyndns', 'DynDNS', 'dyndns')
add_postable_endpoint('dyndns-register', dyndnsRegister)
add_endpoint('dyndns-update', dyndnsUpdate)
def dyndns(**kwargs):
session = kwargs['session']
request = kwargs['request']
rqtools = kwargs['rqtools']
if 'username' not in session.keys():
return rqtools.redirect(rqtools.url_for('login', next='dyndns'))
if not services.authorize_user(session['username'], 'dyndns'):
return rqtools.get_403(None)
ownRecords = db.db['dyndns-records'].find({'username': session['username']})
recordsCount = db.db['dyndns-records'].count_documents({'username': session['username']})
otherRecords = None
if services.authorize_user(session['username'], 'dyndnsadmin'):
otherRecords = db.db['dyndns-records'].find({})
return rqtools.render_template(plugin_dir + '/templates/dyndns.html', records=ownRecords, recordsCount=recordsCount, otherRecords=otherRecords)
def dyndnsRegister(**kwargs):
session = kwargs['session']
request = kwargs['request']
rqtools = kwargs['rqtools']
if 'username' not in session.keys():
return rqtools.get_403(None)
if not services.authorize_user(session['username'], 'dyndns'):
return rqtools.get_403(None)
if not 'domainname' in request.form.keys():
return rqtools.get_400(None)
x = db.db['dyndns-records'].find_one(filter={'domain': request.form['domainname']})
if not x is None:
db.sendMessage(session['username'], 'This domain is already registered by someone.')
return rqtools.redirect(rqtools.url_for('service', servicename='dyndns'))
r = requests.get(url = 'http://127.0.0.1:3002/register', params = {'domain': request.form['domainname']})
reply = r.json()
if r.status_code > 299:
db.sendMessage(session['username'], reply['message'])
return rqtools.redirect(rqtools.url_for('service', servicename='dyndns'))
x = db.db['dyndns-records'].insert_one({'username': session['username'], 'domain': reply['domainName'], 'token': reply['token'], 'ip': "null", 'lastupdate': "never"})
db.sendMessage(session['username'], reply['message'])
return rqtools.redirect(rqtools.url_for('service', servicename='dyndns'))
def dyndnsUpdate(**kwargs):
# No authentication + authorization here on purpose
# Auth is handled by DynDNS server using a token
request = kwargs['request']
rqtools = kwargs['rqtools']
ip = request.remote_addr
if not 'token' in request.args.keys():
return rqtools.get_400("You need to provide your token.")
x = db.db['dyndns-records'].find_one({'token': request.args['token']})
if x['ip'] == ip:
return {'message': 'Already set.'}, 200
r = requests.get(url = 'http://127.0.0.1:3002/update', params = {'token': request.args['token'], 'ip': ip})
reply = r.json()
if r.status_code < 300:
print("Updated " + x['domain'] + " from " + x['ip'] + " to " + ip)
db.db['dyndns-records'].update_one(filter={'token': request.args['token']}, update={'$set': {'ip': ip, 'lastupdate': datetime.datetime.now()}})
return reply, r.status_code
{% extends 'base.html' %}
{% block title %}DynDNS @ SFeri{% endblock %}
{% block head_extensions %}
{% endblock %}
{% block content %}
<div class="slim_container">
<h1>Dynamic DNS</h1>
<p style="text-align: justify;">
<bold>What is DynDNS?</bold> Unless you are at Schönherz, your IP address probably updates from time to time.
With dynamic DNS, you can always point <code>your-domain.dyndns.sfsrv.hu</code> to your device.
</p>
<p style="text-align: justify;">
<bold>How to use?</bold> Register a new domain here and you will get a unique token. To update your IP address
on the DNS server, just visit <code>https://sferi.hu/dyndns-update?token=[your token]</code> manually or via a
script: <code>curl -4 'https://sferi.hu/dyndns-update?token=[your token]'</code>.
</p>
<p style="text-align: justify;">
Currently, only IPv4 is supported.
</p>
<p style="text-align: justify;">
<bold>Warning:</bold> You should keep your token a secret.
</p>
{% if recordsCount > 0 %}
<h2>Your DynDNS domains</h2>
<div style="overflow-x: auto;">
<table>
<tr>
<th>Domain</th>
<th>Token</th>
<th>Update link</th>
<th>Current IP</th>
</tr>
{% for r in records %}
<tr>
<th>{{ r['domain'] }}</th>
<th>{{ r['token'] }}</th>
<th><a href="https://sferi.hu/dyndns-update?token={{ r['token'] }}">link</a></th>
<th>{{ r['ip'] }}</th>
</tr>
{% endfor %}
</table>
</div>
{% endif %}
<h2>Register a new domain</h2>
<form method="POST" action="{{ url_for('service', servicename='dyndns-register') }}" class="slim_container">
<section class="inputfield">
<label for="domainname">Domain name</label>
<input name="domainname" id="domainname" required>
<label>.dyndns.sfsrv.hu</label>
</section>
<section class="inputfield">
<input class="button" type="submit" value="Register">
</section>
</form>
{% if otherRecords != None %}
<h2>Other records</h2>
<div style="overflow-x: auto;">
<table>
<tr>
<th>User</th>
<th>Domain</th>
<th>Current IP</th>
<th>Last update</th>
</tr>
{% for r in otherRecords %}
<tr>
<th>{{ r['username'] }}</th>
<th>{{ r['domain'] }}</th>
<th>{{ r['ip'] }}</th>
<th>{{ r['lastupdate'] }}</th>
</tr>
{% endfor %}
</table>
</div>
{% endif %}
</div>
<div class="h-expander-3"></div>
{% endblock %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment