2023-08-16 13:48:11 +10:00
|
|
|
# API server
|
|
|
|
|
|
|
|
from flask import Flask, request, jsonify
|
|
|
|
import dotenv
|
|
|
|
import os
|
2023-08-17 13:36:57 +10:00
|
|
|
import threading
|
2023-08-16 13:48:11 +10:00
|
|
|
|
|
|
|
dotenv.load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
# API route for POST requests for new-site (takes variable 'domain')
|
|
|
|
@app.route('/new-site', methods=['POST'])
|
|
|
|
def new_site():
|
|
|
|
# Get the URL from the request
|
|
|
|
domain = request.args.get('domain')
|
|
|
|
count = get_sites_count()
|
|
|
|
|
|
|
|
if site_exists(domain):
|
|
|
|
return jsonify({'error': 'Site already exists', 'success': 'false'})
|
|
|
|
|
|
|
|
# Add site to file
|
|
|
|
sites_file = open('sites.txt', 'a')
|
|
|
|
sites_file.write(domain + '\n')
|
2023-08-17 13:12:51 +10:00
|
|
|
sites_file.close()
|
2023-08-16 13:48:11 +10:00
|
|
|
|
2023-08-17 13:27:57 +10:00
|
|
|
# New site in background
|
2023-08-17 13:36:57 +10:00
|
|
|
thread = threading.Thread(target=new_site, args=(domain, 5000 + count))
|
|
|
|
thread.start()
|
|
|
|
|
2023-08-17 13:10:12 +10:00
|
|
|
|
2023-08-16 13:48:11 +10:00
|
|
|
# Return the domain and the number of sites
|
|
|
|
return jsonify({'domain': domain, 'count': count})
|
|
|
|
|
2023-08-17 13:27:57 +10:00
|
|
|
@app.route('/tlsa', methods=['GET'])
|
|
|
|
def tlsa():
|
|
|
|
domain = request.args.get('domain')
|
|
|
|
if domain == None:
|
|
|
|
return jsonify({'error': 'Invalid domain', 'success': 'false'})
|
2023-08-17 13:48:08 +10:00
|
|
|
|
|
|
|
tlsa = None
|
|
|
|
try:
|
|
|
|
tlsa_file = open('wordpress-'+domain+'/tlsa.txt', 'r')
|
|
|
|
tlsa = tlsa_file.readlines()
|
|
|
|
tlsa_file.close()
|
|
|
|
except FileNotFoundError:
|
|
|
|
return jsonify({'error': 'TLSA record not found', 'success': 'false'})
|
2023-08-17 13:27:57 +10:00
|
|
|
|
2023-08-17 13:52:23 +10:00
|
|
|
# Remove newlines
|
|
|
|
tlsa = tlsa.replace('\n', '')
|
2023-08-17 13:27:57 +10:00
|
|
|
return jsonify({'domain': domain, 'tlsa': tlsa})
|
|
|
|
|
|
|
|
|
2023-08-16 16:59:57 +10:00
|
|
|
# Return status
|
|
|
|
@app.route('/status', methods=['GET'])
|
|
|
|
def status():
|
|
|
|
num_Sites = get_sites_count()
|
|
|
|
|
|
|
|
availability=(num_Sites < int(os.getenv('MAX_SITES')))
|
|
|
|
return jsonify({'availability': availability, 'num_sites': num_Sites})
|
|
|
|
|
|
|
|
|
|
|
|
# Ping status
|
|
|
|
@app.route('/ping')
|
|
|
|
def ping():
|
|
|
|
return 'pong'
|
|
|
|
|
2023-08-16 13:48:11 +10:00
|
|
|
def get_sites_count():
|
|
|
|
# If file doesn't exist, create it
|
|
|
|
try:
|
|
|
|
sites_file = open('sites.txt', 'r')
|
|
|
|
except FileNotFoundError:
|
|
|
|
sites_file = open('sites.txt', 'w')
|
|
|
|
sites_file.close()
|
|
|
|
sites_file = open('sites.txt', 'r')
|
2023-08-16 16:59:57 +10:00
|
|
|
print(sites_file.readlines())
|
2023-08-16 13:48:11 +10:00
|
|
|
# Return number of lines in file
|
|
|
|
return len(sites_file.readlines())
|
|
|
|
|
|
|
|
def site_exists(domain):
|
|
|
|
# If file doesn't exist, create it
|
|
|
|
try:
|
|
|
|
sites_file = open('sites.txt', 'r')
|
|
|
|
except FileNotFoundError:
|
|
|
|
sites_file = open('sites.txt', 'w')
|
|
|
|
sites_file.close()
|
|
|
|
sites_file = open('sites.txt', 'r')
|
|
|
|
|
|
|
|
# Check if domain is in file
|
|
|
|
if domain in sites_file.read():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2023-08-17 13:27:57 +10:00
|
|
|
|
2023-08-17 13:50:09 +10:00
|
|
|
def new_site(domain,port):
|
2023-08-17 13:27:57 +10:00
|
|
|
script = 'bash wp.sh ' + domain + ' '+ str(port)
|
|
|
|
os.system(script)
|
2023-08-16 13:48:11 +10:00
|
|
|
|
|
|
|
# Start the server
|
|
|
|
if __name__ == '__main__':
|
2023-08-16 17:05:15 +10:00
|
|
|
app.run(debug=False, port=5000,host='0.0.0.0')
|