-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
326 lines (255 loc) · 11.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import requests
from flask import Flask, flash, g, redirect, render_template, request, session, url_for
from admins import *
#from cache import Cache
from cloudflare import *
from routes.authentication import *
from data_sql import *
from discord import get_github_username, send_discord_message
from concurrency import *
from threadedreturn import ThreadWithReturnValue
#Sql Wrapper
database = dataSQL(dbfile="database.db")
#Flask App
app = Flask(__name__)
app.secret_key = SECRET_KEY
app.config["GITHUB_CLIENT_ID"] = CLIENT_ID
app.config["GITHUB_CLIENT_SECRET"] = CLIENT_SECRET
#Cloudflare global variables are in cloudflare.py since a lot of .py files uses them
#USER SIDE
#===============================
#/
@app.route("/")
def indexnormal():
return render_template("home.html")
#/edit
@app.route("/edit", methods=["GET","POST"])
def edit():
if "id" not in session:
return redirect("/")
args = request.args.to_dict()
INPUT = args["dom"]
if INPUT not in database.subdomains_from_token(session=session["id"]):
return redirect("dashboard")
DOMAIN = INPUT.split(".")[1]+"."+INPUT.split(".")[2]
DOM = CLOUDFLARE[DOMAIN].find(INPUT)
if ("dom" in args and args["dom"] is not None) == False:
return redirect("dashboard")
if request.method == "POST":
NAME = request.form["name"]
TYPE = request.form["type"]
CONTENT = request.form["content"]
id = CLOUDFLARE[DOMAIN].find(name=NAME)["id"]
if CLOUDFLARE[DOMAIN].update(DNS_RECORD_NAME=str(NAME),DNS_RECORD_CONTENT=CONTENT,type=TYPE, id=id).status_code == 200:
target = session["id"]
send_discord_message(f"SESSION ID ``{target}`` as ``{get_github_username(github_id=target)}`` has **update** the domain: ``{INPUT}`` to following: ```TYPE -> {TYPE} \nNAME -> {NAME} \nCONTENT -> {CONTENT}```")
return redirect("dashboard")
else:
return render_template("edit.html", domain=DOM, error="FAILED TO UPDATE ON CLOUDFLARE")
return render_template("edit.html", domain=DOM, error="")
#/claim
@app.route("/claim", methods=["GET", "POST"])
def claim():
if "id" not in session:
return redirect("/")
target = session["id"]
if request.method == "POST":
domains_thread = ThreadWithReturnValue(target=database.subdomains_from_token, args = (target,))
domains_thread.start()
max_domains_thread = ThreadWithReturnValue(target=dataSQL(dbfile="database.db").get_from_token, args=("max", target,))
max_domains_thread.start()
INPUT = request.form["dns_submission"]
DOMAIN = request.form["domain"]
if len(INPUT.split(".")) > 1:
return render_template("claim.html", error="Inappropriate Choice", domains=DOMAINS)
if DOMAIN not in CLOUDFLARE:
return render_template("claim.html", error="We Don't Offer That Domain", domains=DOMAINS)
SUBDOMAIN = f"{INPUT}.{DOMAIN}"
if CLOUDFLARE[DOMAIN].find(name=SUBDOMAIN):
return render_template("claim.html", error="Domain already taken", domains=DOMAINS)
max_domains = max_domains_thread.join()
if max_domains <= len(domains_thread.join()):
return render_template("claim.html", error="You already have the maximum number of domains.",domains=DOMAINS)
# Give the user the subdomain
response = CLOUDFLARE[DOMAIN].insert_CNAME_record(
DNS_RECORD_NAME=SUBDOMAIN,
DNS_RECORD_CONTENT="github.com",
comment=f"OWNER RESPONSIBLE IS {target} as {get_github_username(github_id=target)}"
)
if response.status_code != 200:
Thread(target=send_discord_message, args = (response.text,)).start()
return render_template("claim.html", error="Domain already exist on Cloudflare.", domains=DOMAINS)
database.new_subdomain(token=session["id"], subdomain=SUBDOMAIN)
t = ThreadWithReturnValue(target=CACHE_INSTANCE.get_subdomains, args=(True,))
t.start()
Thread(target=send_discord_message, args = (f"SESSION ID ``{target}`` as ``{get_github_username(github_id=target)}`` has **claimed** the domain: ``{SUBDOMAIN}``",)).start()
t.join()
return redirect("dashboard") #successful process
else:
return render_template("claim.html", error="", domains=DOMAINS)
#/dashboard
@app.route("/dashboard", methods=["GET", "POST"])
def dashboard(response: str = ""):
if "id" not in session:
return redirect("login")
domains_thread = ThreadWithReturnValue(target=database.subdomains_from_token, args=(session["id"],))
domains_thread.start()
all_sub_domains_thread = ThreadWithReturnValue(target=CACHE_INSTANCE.get_subdomains)
all_sub_domains_thread.start()
target = session["id"]
user_info_thread = ThreadWithReturnValue(target=requests.get,
kwargs={'url': f"https://api.github.com/user/{target}", "headers": {'Authorization': 'token ' + GITHUB_TOKEN}}
)
user_info_thread.start()
args = request.args.to_dict()
if "delete" in args and args["delete"] is not None:
INPUT = args["delete"]
insert = INPUT.split(".")
if len(insert) != 3: #subdomain | domain | com (3)
return redirect("dashboard")
DOMAIN = insert[1] + "." + insert[2]
if (INPUT in domains_thread.join()) == False: #if user doesn't own domain, return them back
return redirect("dashboard")
if CLOUDFLARE[DOMAIN].find_and_delete(INPUT):
database.delete(subdomain=INPUT)
send_discord_message(f"SESSION ID ``{target}`` as ``{get_github_username(github_id=target)}`` has **deleted** the domain: ``{INPUT}``.")
domains_thread = ThreadWithReturnValue(target=CACHE_INSTANCE.get_subdomains, args=(True,))
domains_thread.start()
#return redirect("dashboard")
user_info = user_info_thread.join().json()
print(user_info)
user_profile_picture = user_info["avatar_url"]
user_company = user_info["company"]
domains = domains_thread.join()
if domains == []:
return render_template(
"dashboard.html",
subdomains=[],
github_username=session["username"],
github_profile=user_profile_picture,
github_company=user_company,
response=response
)
all_sub_domains = all_sub_domains_thread.join()
try:
user_subdomains = [
possible_domain
for possible_domain in all_sub_domains
if possible_domain["name"] in domains
]
except Exception as e:
print("ERROR ------------------------------\n"+e)
user_subdomains = []
return render_template(
"dashboard.html",
subdomains=user_subdomains,
github_username=session["username"],
github_profile=user_profile_picture,
github_company=user_company,
response=response
)
#ADMIN CODE
#===============================
#/loginadmin
@app.route("/loginadmin", methods=["GET", "POST"])
def loginadmin():
if request.method == "POST":
session.pop("user_id", None)
try:
email = request.form["email"]
pw = request.form["password"]
user = [x for x in ADMIN_ACCTS if x.username == email][0]
if user and user.password == pw:
session["user_id"] = user.id
session["admin_email"] = user.username
send_discord_message(f":safety_vest: ADMIN ``{user.username}`` has logged in. :safety_vest: ")
return redirect(url_for("admin"))
send_discord_message(f":octagonal_sign: ADMIN ``{user.username}`` has failed login attempt. :octagonal_sign: ")
return redirect(url_for("loginadmin"))
except Exception as e:
send_discord_message(f":octagonal_sign: __Attempted Login Failed...__ Error: ```{e}``` :octagonal_sign: ")
return render_template("login.html")
#/admin
@app.route("/admin", methods=["GET", "POST"]) # admin site soon
def admin():
if not g.user:
return redirect(url_for("adminlogin"))
subdomains = ThreadWithReturnValue(target=cloudf_doms, args = (DOMAINS,CLOUDFLARE,))
subdomains.start()
owners = database.admin_fetchall()
args = request.args.to_dict()
if "delete" in args and args["delete"] is not None:
INPUT = args["delete"]
insert = INPUT.split(".")
DOMAIN = insert[1] + "." + insert[2]
if CLOUDFLARE[DOMAIN].find_and_delete(INPUT):
database.delete(subdomain=INPUT)
target = session["admin_email"]
send_discord_message(f":safety_vest: ADMIN ``{target}`` has deleted the domain ``{INPUT}``. :safety_vest: ")
#re-run the code
subdomains.join()
subdomains = ThreadWithReturnValue(target=cloudf_doms, args = (DOMAINS,CLOUDFLARE,)) #re-run it
subdomains.start()
elif "disassociate" in args and args["disassociate"] is not None:
DOMAIN_TO_REMOVE = args["disassociate"]
database.delete(subdomain=DOMAIN_TO_REMOVE)
target = session["admin_email"]
send_discord_message(f":safety_vest: ADMIN ``{target}`` has modified the SQL; Disassociated the domain ``{DOMAIN_TO_REMOVE}``. :safety_vest: ")
owners = database.admin_fetchall()
x = subdomains.join()
return render_template(
"admin.html", subdomains=x, len_sd = len(x), account_id=CLOUDFLARE_ACCOUNT_ID, owners=owners, len_owners=len(owners)
)
#Before a Website is Accessed
#BEFORE_REQUEST
#Only /admin and /loginadmin really uses this
#======================================
@app.before_request
def before_request():
g.user = None
if "user_id" in session:
user = [x for x in ADMIN_ACCTS if x.id == session["user_id"]][0]
g.user = user
#After A Website is Accessed
#AFTER_REQUEST
#=======================================
@app.after_request
def after_request_func(response):
if session.get("id") != None:
try:
target = session.get("id")
if request.path.count('/') == 1:
send_discord_message(f"Session ``{target}`` as ``{get_github_username(github_id=target)}`` accessed the subdirectory ``{request.path}``")
except:
pass
return response
# ERROR HANDLING
# Goes to another website and prints issue
#=======================================
#@app.errorhandler(Exception)
#def handle_error(error):
# exc_type, exc_value, exc_traceback = sys.exc_info()
# error_message = f"An error occurred: {exc_type} - {exc_value}"
# target = session["id"]
# Thread(target=send_discord_message, args = (f"SESSION ID ``{target}`` as ``{get_github_username(github_id=target)}`` has encountered an error: ``{error_message}``",)).start()
# return render_template("error.html",error=error_message)
#STARTUP
#Sets up Cache ahead of time
def startup():
t = Thread(target=CACHE_INSTANCE.get_subdomains, args=(False,))
t.start()
print("SERVER STARTED...")
send_discord_message("SERVER STARTING!")
t.join()
print("Cache is up to date!")
send_discord_message("Cache up to date.")
if __name__ == "__main__":
# from waitress import serve
# serve(app, host="0.0.0.0", port=8080)
startup()
app.register_blueprint(authentication)
try:
from waitress import serve
serve(app, host="0.0.0.0", port=PORT)
except:
app.run(host="0.0.0.0", port=PORT, debug=False, threaded=True)