2023-02-16 00:06:31 +09:00
|
|
|
import json, renderer, headscale, helper, pytz, os
|
2023-02-15 19:32:27 +09:00
|
|
|
from flask import Flask, render_template, request, url_for, redirect, Markup
|
|
|
|
|
from dateutil import parser
|
2023-02-06 04:58:09 +00:00
|
|
|
from flask_executor import Executor
|
|
|
|
|
|
|
|
|
|
# Global vars
|
|
|
|
|
# Colors: https://materializecss.com/color.html
|
|
|
|
|
COLOR_NAV = "blue-grey darken-1"
|
|
|
|
|
COLOR_BTN = "blue-grey darken-3"
|
2023-02-11 08:44:19 +09:00
|
|
|
BASE_PATH = os.environ["BASE_PATH"].replace('"', '')
|
2023-02-12 15:03:35 +09:00
|
|
|
BUILD_DATE = os.environ["BUILD_DATE"]
|
2023-02-10 22:24:29 +09:00
|
|
|
APP_VERSION = os.environ["APP_VERSION"]
|
2023-02-10 21:32:28 +09:00
|
|
|
GIT_COMMIT = os.environ["GIT_COMMIT"]
|
2023-02-10 21:51:19 +09:00
|
|
|
GIT_BRANCH = os.environ["GIT_BRANCH"]
|
2023-02-10 21:49:56 +09:00
|
|
|
HS_VERSION = "v0.20.0"
|
2023-02-06 04:58:09 +00:00
|
|
|
DEBUG_STATE = False
|
2023-02-16 20:17:36 +09:00
|
|
|
AUTH_TYPE = os.environ["AUTH_TYPE"]
|
2023-02-16 21:30:49 +09:00
|
|
|
BASIC_AUTH = False
|
2023-02-06 04:58:09 +00:00
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
static_url_path = '/static'
|
2023-02-15 19:02:14 +09:00
|
|
|
if BASE_PATH != '': static_url_path = BASE_PATH + static_url_path
|
2023-02-13 20:27:10 +09:00
|
|
|
|
|
|
|
|
app = Flask(__name__, static_url_path=static_url_path)
|
|
|
|
|
executor = Executor(app)
|
|
|
|
|
|
2023-02-15 19:36:59 +09:00
|
|
|
app.logger.warning("Static assets served on: "+static_url_path)
|
|
|
|
|
app.logger.warning("BASE_PATH: "+BASE_PATH)
|
2023-02-13 20:27:10 +09:00
|
|
|
|
2023-02-16 20:17:36 +09:00
|
|
|
# Set Authentication type:
|
|
|
|
|
if AUTH_TYPE.lower() == "oidc":
|
|
|
|
|
# Load OIDC libraries
|
|
|
|
|
app.logger.info("Loading OIDC libraries and configuring app...")
|
|
|
|
|
# https://flask-oidc.readthedocs.io/en/latest/
|
|
|
|
|
|
|
|
|
|
if AUTH_TYPE.lower() == "basic":
|
|
|
|
|
# Load basic auth libraries:
|
|
|
|
|
app.logger.info("Loading basic auth libraries and configuring app...")
|
|
|
|
|
# https://flask-basicauth.readthedocs.io/en/latest/
|
|
|
|
|
|
2023-02-16 20:44:33 +09:00
|
|
|
from flask_basicauth import BasicAuth
|
|
|
|
|
app.config['BASIC_AUTH_USERNAME'] = os.environ["BASIC_AUTH_USER"].replace('"', '')
|
|
|
|
|
app.config['BASIC_AUTH_PASSWORD'] = os.environ["BASIC_AUTH_PASS"]
|
|
|
|
|
app.config['BASIC_AUTH_FORCE'] = True
|
2023-02-16 21:30:49 +09:00
|
|
|
BASIC_AUTH = True
|
2023-02-16 20:44:33 +09:00
|
|
|
|
|
|
|
|
basic_auth = BasicAuth(app)
|
|
|
|
|
|
2023-02-16 20:17:36 +09:00
|
|
|
|
2023-02-13 20:56:31 +09:00
|
|
|
########################################################################################
|
|
|
|
|
# / pages - User-facing pages
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/')
|
|
|
|
|
@app.route(BASE_PATH+'/')
|
|
|
|
|
@app.route('/overview')
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/overview')
|
2023-02-06 04:58:09 +00:00
|
|
|
def overview_page():
|
2023-02-16 20:17:36 +09:00
|
|
|
# Some basic sanity checks:
|
2023-02-16 21:30:49 +09:00
|
|
|
if not helper.load_checks(): return redirect(BASE_PATH+str(url_for(helper.load_checks())))
|
2023-02-06 04:58:09 +00:00
|
|
|
|
|
|
|
|
return render_template('overview.html',
|
|
|
|
|
render_page = renderer.render_overview(),
|
|
|
|
|
COLOR_NAV = COLOR_NAV,
|
|
|
|
|
COLOR_BTN = COLOR_BTN
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/machines', methods=('GET', 'POST'))
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/machines', methods=('GET', 'POST'))
|
|
|
|
|
def machines_page():
|
2023-02-16 20:17:36 +09:00
|
|
|
# Some basic sanity checks:
|
2023-02-16 21:30:49 +09:00
|
|
|
if not helper.load_checks(): return redirect(BASE_PATH+str(url_for(helper.load_checks())))
|
2023-02-15 12:31:22 +09:00
|
|
|
|
2023-02-06 04:58:09 +00:00
|
|
|
cards = renderer.render_machines_cards()
|
|
|
|
|
return render_template('machines.html',
|
|
|
|
|
cards = cards,
|
|
|
|
|
headscale_server = headscale.get_url(),
|
|
|
|
|
COLOR_NAV = COLOR_NAV,
|
|
|
|
|
COLOR_BTN = COLOR_BTN
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/users', methods=('GET', 'POST'))
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/users', methods=('GET', 'POST'))
|
|
|
|
|
def users_page():
|
2023-02-16 20:17:36 +09:00
|
|
|
# Some basic sanity checks:
|
2023-02-16 21:30:49 +09:00
|
|
|
if not helper.load_checks(): return redirect(BASE_PATH+str(url_for(helper.load_checks())))
|
2023-02-06 04:58:09 +00:00
|
|
|
|
|
|
|
|
cards = renderer.render_users_cards()
|
|
|
|
|
return render_template('users.html',
|
|
|
|
|
cards = cards,
|
|
|
|
|
headscale_server = headscale.get_url(),
|
|
|
|
|
COLOR_NAV = COLOR_NAV,
|
|
|
|
|
COLOR_BTN = COLOR_BTN
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/settings', methods=('GET', 'POST'))
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/settings', methods=('GET', 'POST'))
|
|
|
|
|
def settings_page():
|
2023-02-16 20:17:36 +09:00
|
|
|
# Some basic sanity checks:
|
2023-02-16 21:30:49 +09:00
|
|
|
if not helper.load_checks(): return redirect(BASE_PATH+str(url_for(helper.load_checks())))
|
2023-02-06 04:58:09 +00:00
|
|
|
|
2023-02-16 20:17:36 +09:00
|
|
|
return render_template('settings.html',
|
|
|
|
|
url = headscale.get_url(),
|
2023-02-15 12:31:22 +09:00
|
|
|
COLOR_NAV = COLOR_NAV,
|
|
|
|
|
COLOR_BTN = COLOR_BTN,
|
|
|
|
|
HS_VERSION = HS_VERSION,
|
|
|
|
|
APP_VERSION = APP_VERSION,
|
|
|
|
|
GIT_COMMIT = GIT_COMMIT,
|
|
|
|
|
GIT_BRANCH = GIT_BRANCH,
|
|
|
|
|
BUILD_DATE = BUILD_DATE
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-15 12:50:34 +09:00
|
|
|
@app.route(BASE_PATH+'/error')
|
|
|
|
|
@app.route('/error')
|
2023-02-15 12:31:22 +09:00
|
|
|
def error_page():
|
2023-02-15 13:59:45 +09:00
|
|
|
if helper.startup_checks() == "Pass":
|
2023-02-15 14:29:14 +09:00
|
|
|
return redirect(url_for('overview_page'))
|
2023-02-15 13:59:45 +09:00
|
|
|
|
2023-02-15 12:31:22 +09:00
|
|
|
return render_template('error.html',
|
2023-02-15 13:23:25 +09:00
|
|
|
ERROR_MESSAGE = Markup(helper.startup_checks())
|
2023-02-15 12:31:22 +09:00
|
|
|
)
|
2023-02-06 04:58:09 +00:00
|
|
|
|
|
|
|
|
########################################################################################
|
|
|
|
|
# /api pages
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
|
|
########################################################################################
|
|
|
|
|
# Headscale API Key Endpoints
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/test_key', methods=('GET', 'POST'))
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/test_key', methods=('GET', 'POST'))
|
|
|
|
|
def test_key_page():
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
|
|
|
|
|
# Test the API key. If the test fails, return a failure.
|
|
|
|
|
status = headscale.test_api_key(url, api_key)
|
|
|
|
|
if status != 200: return "Unauthenticated"
|
|
|
|
|
|
|
|
|
|
renewed = headscale.renew_api_key(url, api_key)
|
2023-02-15 19:36:59 +09:00
|
|
|
app.logger.warning("The below statement will be TRUE if the key has been renewed or DOES NOT need renewal. False in all other cases")
|
|
|
|
|
app.logger.warning("Renewed: "+str(renewed))
|
2023-02-06 04:58:09 +00:00
|
|
|
# The key works, let's renew it if it needs it. If it does, re-read the api_key from the file:
|
|
|
|
|
if renewed: api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
key_info = headscale.get_api_key_info(url, api_key)
|
|
|
|
|
|
|
|
|
|
# Set the current timezone and local time
|
2023-02-16 00:06:31 +09:00
|
|
|
timezone = pytz.timezone(os.environ["TZ"] if os.environ["TZ"] else "UTC")
|
2023-02-06 04:58:09 +00:00
|
|
|
|
|
|
|
|
# Format the dates for easy readability
|
|
|
|
|
expiration_parse = parser.parse(key_info['expiration'])
|
|
|
|
|
expiration_local = expiration_parse.astimezone(timezone)
|
|
|
|
|
expiration_time = str(expiration_local.strftime('%A %m/%d/%Y, %H:%M:%S'))+" "+str(timezone)
|
|
|
|
|
|
|
|
|
|
creation_parse = parser.parse(key_info['createdAt'])
|
|
|
|
|
creation_local = creation_parse.astimezone(timezone)
|
|
|
|
|
creation_time = str(creation_local.strftime('%A %m/%d/%Y, %H:%M:%S'))+" "+str(timezone)
|
|
|
|
|
|
|
|
|
|
key_info['expiration'] = expiration_time
|
|
|
|
|
key_info['createdAt'] = creation_time
|
|
|
|
|
|
|
|
|
|
message = json.dumps(key_info)
|
|
|
|
|
return message
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/save_key', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/save_key', methods=['POST'])
|
|
|
|
|
def save_key_page():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
api_key = json_response['api_key']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
file_written = headscale.set_api_key(api_key)
|
|
|
|
|
message = ''
|
|
|
|
|
|
|
|
|
|
if file_written:
|
|
|
|
|
# Re-read the file and get the new API key and test it
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
test_status = headscale.test_api_key(url, api_key)
|
|
|
|
|
if test_status == 200:
|
|
|
|
|
key_info = headscale.get_api_key_info(url, api_key)
|
|
|
|
|
expiration = key_info['expiration']
|
|
|
|
|
message = "Key: '"+api_key+"', Expiration: "+expiration
|
|
|
|
|
# If the key was saved successfully, test it:
|
|
|
|
|
return "Key saved and tested: "+message
|
|
|
|
|
else: return "Key failed testing. Check your key"
|
|
|
|
|
else: return "Key did not save properly. Check logs"
|
|
|
|
|
|
|
|
|
|
########################################################################################
|
|
|
|
|
# Machine API Endpoints
|
|
|
|
|
########################################################################################
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/update_route', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/update_route', methods=['POST'])
|
|
|
|
|
def update_route_page():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
route_id = json_response['route_id']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
current_state = json_response['current_state']
|
|
|
|
|
|
|
|
|
|
return headscale.update_route(url, api_key, route_id, current_state)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/machine_information', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/machine_information', methods=['POST'])
|
|
|
|
|
def machine_information_page():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
machine_id = json_response['id']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.get_machine_info(url, api_key, machine_id)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/delete_machine', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/delete_machine', methods=['POST'])
|
|
|
|
|
def delete_machine_page():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
machine_id = json_response['id']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.delete_machine(url, api_key, machine_id)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/rename_machine', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/rename_machine', methods=['POST'])
|
|
|
|
|
def rename_machine_page():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
machine_id = json_response['id']
|
|
|
|
|
new_name = json_response['new_name']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.rename_machine(url, api_key, machine_id, new_name)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/move_user', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/move_user', methods=['POST'])
|
|
|
|
|
def move_user_page():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
machine_id = json_response['id']
|
|
|
|
|
new_user = json_response['new_user']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.move_user(url, api_key, machine_id, new_user)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/set_machine_tags', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/set_machine_tags', methods=['POST'])
|
|
|
|
|
def set_machine_tags():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
machine_id = json_response['id']
|
|
|
|
|
machine_tags = json_response['tags_list']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.set_machine_tags(url, api_key, machine_id, machine_tags)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/register_machine', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/register_machine', methods=['POST'])
|
|
|
|
|
def register_machine():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
machine_key = json_response['key']
|
|
|
|
|
user = json_response['user']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return str(headscale.register_machine(url, api_key, machine_key, user))
|
|
|
|
|
|
|
|
|
|
########################################################################################
|
|
|
|
|
# User API Endpoints
|
|
|
|
|
########################################################################################
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/rename_user', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/rename_user', methods=['POST'])
|
|
|
|
|
def rename_user_page():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
old_name = json_response['old_name']
|
|
|
|
|
new_name = json_response['new_name']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.rename_user(url, api_key, old_name, new_name)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/add_user', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/add_user', methods=['POST'])
|
|
|
|
|
def add_user():
|
|
|
|
|
json_response = json.dumps(request.get_json())
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.add_user(url, api_key, json_response)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/delete_user', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/delete_user', methods=['POST'])
|
|
|
|
|
def delete_user():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
user_name = json_response['name']
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.delete_user(url, api_key, user_name)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/get_users', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/get_users', methods=['POST'])
|
|
|
|
|
def get_users_page():
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.get_users(url, api_key)
|
|
|
|
|
|
|
|
|
|
########################################################################################
|
|
|
|
|
# Pre-Auth Key API Endpoints
|
|
|
|
|
########################################################################################
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/add_preauth_key', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/add_preauth_key', methods=['POST'])
|
|
|
|
|
def add_preauth_key():
|
|
|
|
|
json_response = json.dumps(request.get_json())
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.add_preauth_key(url, api_key, json_response)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/expire_preauth_key', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/expire_preauth_key', methods=['POST'])
|
|
|
|
|
def expire_preauth_key():
|
|
|
|
|
json_response = json.dumps(request.get_json())
|
|
|
|
|
url = headscale.get_url()
|
|
|
|
|
api_key = headscale.get_api_key()
|
|
|
|
|
|
|
|
|
|
return headscale.expire_preauth_key(url, api_key, json_response)
|
|
|
|
|
|
2023-02-13 20:27:10 +09:00
|
|
|
@app.route(BASE_PATH+'/api/build_preauthkey_table', methods=['POST'])
|
2023-02-06 04:58:09 +00:00
|
|
|
@app.route('/api/build_preauthkey_table', methods=['POST'])
|
|
|
|
|
def build_preauth_key_table():
|
|
|
|
|
json_response = request.get_json()
|
|
|
|
|
user_name = json_response['name']
|
|
|
|
|
|
|
|
|
|
return renderer.build_preauth_key_table(user_name)
|
|
|
|
|
|
|
|
|
|
########################################################################################
|
|
|
|
|
# Main thread
|
|
|
|
|
########################################################################################
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app.run(host="0.0.0.0", debug=DEBUG_STATE)
|