2022-01-03 10:33:02 +00:00
|
|
|
import datetime
|
|
|
|
import ipaddress
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import flask
|
|
|
|
import flask_login
|
|
|
|
|
|
|
|
from . import db
|
2024-08-14 09:25:07 +00:00
|
|
|
from . import ipsets
|
2023-01-26 15:22:05 +00:00
|
|
|
from . import system
|
2022-01-03 10:33:02 +00:00
|
|
|
|
2023-12-04 08:46:37 +00:00
|
|
|
blueprint = flask.Blueprint('vpn', __name__)
|
2022-01-03 10:33:02 +00:00
|
|
|
wgkey_regex = re.compile(r'^[A-Za-z0-9/+=]{44}$')
|
|
|
|
|
|
|
|
@blueprint.route('/')
|
|
|
|
@flask_login.login_required
|
|
|
|
def index():
|
|
|
|
return flask.render_template('vpn/index.html')
|
|
|
|
|
2024-07-30 08:53:57 +00:00
|
|
|
@blueprint.route('/custom')
|
|
|
|
@flask_login.login_required
|
|
|
|
def custom():
|
|
|
|
if not flask_login.current_user.is_admin:
|
|
|
|
return flask.Response('forbidden', status=403, mimetype='text/plain')
|
|
|
|
with db.locked():
|
|
|
|
keys = {ip: data for ip, data in db.read('wireguard').items() if data.get('networks') and not data.get('user')}
|
2024-08-14 09:25:07 +00:00
|
|
|
return flask.render_template('vpn/custom.html', keys=keys, ipsets=ipsets.read().keys())
|
2024-07-30 08:53:57 +00:00
|
|
|
|
2022-01-03 10:33:02 +00:00
|
|
|
@blueprint.route('/list')
|
|
|
|
@flask_login.login_required
|
|
|
|
def list():
|
2024-07-27 11:40:02 +00:00
|
|
|
# Return logged-in user’s keys, marking the key used for current connection (if any).
|
2023-07-12 12:18:31 +00:00
|
|
|
user = flask_login.current_user.get_id()
|
2024-07-30 08:53:57 +00:00
|
|
|
return flask.jsonify([
|
|
|
|
data | {'ip': ip, 'active': flask.request.remote_addr in (ip, data.get('ip6'))}
|
2024-07-27 11:40:02 +00:00
|
|
|
for ip, data in db.load('wireguard').items() if data.get('user') == user
|
2024-07-30 08:53:57 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
@blueprint.route('/list-custom')
|
|
|
|
@flask_login.login_required
|
|
|
|
def list_custom():
|
|
|
|
# Return all custom keys.
|
|
|
|
if not flask_login.current_user.is_admin:
|
|
|
|
return flask.Response('forbidden', status=403, mimetype='text/plain')
|
|
|
|
return flask.jsonify([
|
|
|
|
data | {'ip': ip, 'active': flask.request.remote_addr in (ip, data.get('ip6'))}
|
|
|
|
for ip, data in db.load('wireguard').items() if data.get('networks') and not data.get('user')
|
|
|
|
])
|
2022-01-03 10:33:02 +00:00
|
|
|
|
|
|
|
@blueprint.route('/new', methods=('POST',))
|
|
|
|
@flask_login.login_required
|
|
|
|
def new():
|
2024-07-27 11:40:02 +00:00
|
|
|
# Each key is assigned a new IPv4 address from the pool settings['wg_net'].
|
|
|
|
# Each key is then assigned a corresponding IPv6 subnet, depending on the amount of surplus addresses available.
|
2023-12-12 18:26:55 +00:00
|
|
|
# For wg_net 10.10.0.0/18 and wg_net6 1234:5678:90ab:cdef::/64,
|
|
|
|
# the key for 10.10.0.10/32 would get 1234:5678:90ab:cdef:a::/80.
|
|
|
|
def ipv4to6(net4, ip4, net6):
|
2024-07-27 11:40:02 +00:00
|
|
|
# Calculate the address and prefix length for the IPv6 network that can be assigned to this key.
|
2023-12-12 18:26:55 +00:00
|
|
|
len4 = (net4.max_prefixlen - net4.prefixlen)
|
|
|
|
len6 = (net6.max_prefixlen - net6.prefixlen)
|
|
|
|
# Make sure the network address ends at a colon. Wastes some addresses but IPv6.
|
|
|
|
assigned = (len6 - len4) - (len6 - len4) % 16
|
|
|
|
ip6 = (net6.network_address + (index<<assigned)).compressed
|
|
|
|
return ip6 + '/' + str(net6.max_prefixlen - assigned)
|
|
|
|
|
2022-01-03 10:33:02 +00:00
|
|
|
pubkey = flask.request.json.get('pubkey', '')
|
2024-07-27 11:40:02 +00:00
|
|
|
options = flask.request.json.get('options', {})
|
2022-01-03 10:33:02 +00:00
|
|
|
if not re.match(wgkey_regex, pubkey):
|
|
|
|
return flask.Response('invalid key', status=400, mimetype='text/plain')
|
|
|
|
|
2024-07-27 11:40:02 +00:00
|
|
|
# Read server’s private key and find the corresponding public key.
|
2023-07-12 12:18:31 +00:00
|
|
|
settings = db.load('settings')
|
|
|
|
server_pubkey = subprocess.run([f'wg pubkey'], input=settings.get('wg_key'),
|
|
|
|
text=True, capture_output=True, shell=True).stdout.strip()
|
2022-01-03 10:33:02 +00:00
|
|
|
|
2024-07-27 11:40:02 +00:00
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
key = {
|
|
|
|
'key': pubkey,
|
|
|
|
'time': now.timestamp(),
|
|
|
|
'name': re.sub('[^-._A-Za-z0-9]', '', options.get('name', '')),
|
|
|
|
}
|
|
|
|
|
|
|
|
# Determine IPv4 and IPv6 addresses for the new key.
|
2024-04-30 07:38:14 +00:00
|
|
|
host = ipaddress.ip_interface(settings.get('wg_net') or '10.0.0.1/24')
|
2024-07-27 11:40:02 +00:00
|
|
|
key['ip6'] = None
|
2023-07-12 12:18:31 +00:00
|
|
|
with db.locked():
|
|
|
|
# Find a free address for the new key.
|
2023-09-15 11:58:21 +00:00
|
|
|
keys = db.read('wireguard')
|
2023-12-04 09:23:41 +00:00
|
|
|
for index, ip in enumerate(host.network.hosts(), start=1):
|
2023-09-15 11:58:21 +00:00
|
|
|
if ip != host.ip and str(ip) not in keys:
|
2023-12-04 09:23:41 +00:00
|
|
|
if wg_net6 := settings.get('wg_net6'):
|
2024-07-27 11:40:02 +00:00
|
|
|
key['ip6'] = ipv4to6(host.network, ip, ipaddress.ip_interface(wg_net6).network)
|
2023-07-12 12:18:31 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
return flask.Response('no more available IP addresses', status=500, mimetype='text/plain')
|
2024-07-27 11:40:02 +00:00
|
|
|
|
|
|
|
# Add remaining attributes to new key and update key database.
|
2024-07-30 08:53:57 +00:00
|
|
|
if flask_login.current_user.is_admin and flask.request.json.get('networks'):
|
|
|
|
key['networks'] = flask.request.json.get('networks')
|
|
|
|
else:
|
|
|
|
key['user'] = flask_login.current_user.get_id()
|
2024-07-27 11:40:02 +00:00
|
|
|
keys[str(ip)] = key
|
2023-09-15 11:58:21 +00:00
|
|
|
db.write('wireguard', keys)
|
2023-07-12 12:18:31 +00:00
|
|
|
|
|
|
|
# Generate a new config archive for firewall nodes.
|
|
|
|
system.run(system.save_config)
|
|
|
|
|
|
|
|
# Template arguments.
|
|
|
|
args = {
|
2023-09-15 12:26:11 +00:00
|
|
|
'server': settings.get('wg_endpoint'),
|
2024-04-30 07:38:14 +00:00
|
|
|
'port': settings.get('wg_port') or '51820',
|
2023-07-12 12:18:31 +00:00
|
|
|
'server_key': server_pubkey,
|
|
|
|
'pubkey': pubkey,
|
|
|
|
'timestamp': now,
|
2024-07-27 11:40:02 +00:00
|
|
|
'ip': str(ip),
|
|
|
|
'ip6': key['ip6'],
|
|
|
|
'name': key['name'],
|
2024-07-30 08:53:57 +00:00
|
|
|
'user': key.get('user', 'custom'),
|
2024-07-27 11:40:02 +00:00
|
|
|
'dns': settings.get('wg_dns', '') if options.get('use-dns', True) else False,
|
2024-04-30 07:38:14 +00:00
|
|
|
'allowed_nets': settings.get('wg_allowed_nets', ''),
|
2024-07-27 11:40:02 +00:00
|
|
|
'add_default': options.get('add-default', False),
|
2023-07-12 12:18:31 +00:00
|
|
|
}
|
|
|
|
return flask.render_template('vpn/wg-fri.conf', **args)
|
2022-01-03 10:33:02 +00:00
|
|
|
|
|
|
|
@blueprint.route('/del', methods=('POST',))
|
|
|
|
@flask_login.login_required
|
|
|
|
def delete():
|
|
|
|
pubkey = flask.request.json.get('pubkey', '')
|
|
|
|
if not wgkey_regex.match(pubkey):
|
|
|
|
return flask.Response('invalid key', status=400, mimetype='text/plain')
|
|
|
|
|
2023-07-12 12:18:31 +00:00
|
|
|
with db.locked():
|
|
|
|
user = flask_login.current_user.get_id()
|
2024-07-27 11:40:02 +00:00
|
|
|
keys = {
|
|
|
|
ip: data for ip, data in db.read('wireguard').items()
|
|
|
|
if not (data.get('key') == pubkey and (data.get('user') == user or flask_login.current_user.is_admin))
|
|
|
|
}
|
2023-09-15 11:58:21 +00:00
|
|
|
db.write('wireguard', keys)
|
2022-01-03 10:33:02 +00:00
|
|
|
|
2023-07-12 12:18:31 +00:00
|
|
|
system.run(system.save_config)
|
2022-01-03 10:33:02 +00:00
|
|
|
|
2023-07-12 12:18:31 +00:00
|
|
|
return flask.Response(f'deleted key {pubkey}', status=200, mimetype='text/plain')
|