-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
247 lines (200 loc) · 9.09 KB
/
server.py
File metadata and controls
247 lines (200 loc) · 9.09 KB
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
from flask import (
Flask,
render_template,
request,
jsonify,
session,
flash,
redirect,
url_for,
)
import subprocess
import threading
import os
import json
from time import strftime
import logging
from logging.handlers import RotatingFileHandler
from flask_session import Session
from pymongo import MongoClient
from flask import Response
import time
from flask_pymongo import PyMongo
app = Flask(__name__)
app.secret_key = """
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⠛⠛⠋⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⠛⠿⠻⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀⠀⠀⡀⠠⠤⠒⢂⣉⣉⣉⣑⣒⣒⠒⠒⠒⠒⠒⠒⠒⠀⠀⠐⠒⠚⠻⠿⠿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⡠⠔⠉⣀⠔⠒⠉⣀⣀⠀⠀⠀⣀⡀⠈⠉⠑⠒⠒⠒⠒⠒⠈⠉⠉⠉⠁⠂⠀⠈⠙⢿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⠔⠁⠠⠖⠡⠔⠊⠀⠀⠀⠀⠀⠀⠀⠐⡄⠀⠀⠀⠀⠀⠀⡄⠀⠀⠀⠀⠉⠲⢄⠀⠀⠀⠈⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⠋⠀⠀⠀⠀⠀⠀⠀⠊⠀⢀⣀⣤⣤⣤⣤⣀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠜⠀⠀⠀⠀⣀⡀⠀⠈⠃⠀⠀⠀⠸⣿⣿⣿⣿
⣿⣿⣿⣿⡿⠥⠐⠂⠀⠀⠀⠀⡄⠀⠰⢺⣿⣿⣿⣿⣿⣟⠀⠈⠐⢤⠀⠀⠀⠀⠀⠀⢀⣠⣶⣾⣯⠀⠀⠉⠂⠀⠠⠤⢄⣀⠙⢿⣿⣿
⣿⡿⠋⠡⠐⠈⣉⠭⠤⠤⢄⡀⠈⠀⠈⠁⠉⠁⡠⠀⠀⠀⠉⠐⠠⠔⠀⠀⠀⠀⠀⠲⣿⠿⠛⠛⠓⠒⠂⠀⠀⠀⠀⠀⠀⠠⡉⢢⠙⣿
⣿⠀⢀⠁⠀⠊⠀⠀⠀⠀⠀⠈⠁⠒⠂⠀⠒⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⢀⣀⡠⠔⠒⠒⠂⠀⠈⠀⡇⣿
⣿⠀⢸⠀⠀⠀⢀⣀⡠⠋⠓⠤⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠀⠀⠀⠀⠀⠀⠈⠢⠤⡀⠀⠀⠀⠀⠀⠀⢠⠀⠀⠀⡠⠀⡇⣿
⣿⡀⠘⠀⠀⠀⠀⠀⠘⡄⠀⠀⠀⠈⠑⡦⢄⣀⠀⠀⠐⠒⠁⢸⠀⠀⠠⠒⠄⠀⠀⠀⠀⠀⢀⠇⠀⣀⡀⠀⠀⢀⢾⡆⠀⠈⡀⠎⣸⣿
⣿⣿⣄⡈⠢⠀⠀⠀⠀⠘⣶⣄⡀⠀⠀⡇⠀⠀⠈⠉⠒⠢⡤⣀⡀⠀⠀⠀⠀⠀⠐⠦⠤⠒⠁⠀⠀⠀⠀⣀⢴⠁⠀⢷⠀⠀⠀⢰⣿⣿
⣿⣿⣿⣿⣇⠂⠀⠀⠀⠀⠈⢂⠀⠈⠹⡧⣀⠀⠀⠀⠀⠀⡇⠀⠀⠉⠉⠉⢱⠒⠒⠒⠒⢖⠒⠒⠂⠙⠏⠀⠘⡀⠀⢸⠀⠀⠀⣿⣿⣿
⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠑⠄⠰⠀⠀⠁⠐⠲⣤⣴⣄⡀⠀⠀⠀⠀⢸⠀⠀⠀⠀⢸⠀⠀⠀⠀⢠⠀⣠⣷⣶⣿⠀⠀⢰⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠁⢀⠀⠀⠀⠀⠀⡙⠋⠙⠓⠲⢤⣤⣷⣤⣤⣤⣤⣾⣦⣤⣤⣶⣿⣿⣿⣿⡟⢹⠀⠀⢸⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠑⠀⢄⠀⡰⠁⠀⠀⠀⠀⠀⠈⠉⠁⠈⠉⠻⠋⠉⠛⢛⠉⠉⢹⠁⢀⢇⠎⠀⠀⢸⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⠈⠢⢄⡉⠂⠄⡀⠀⠈⠒⠢⠄⠀⢀⣀⣀⣰⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⢀⣎⠀⠼⠊⠀⠀⠀⠘⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⡀⠉⠢⢄⡈⠑⠢⢄⡀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠁⠀⠀⢀⠀⠀⠀⠀⠀⢻⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣀⡈⠑⠢⢄⡀⠈⠑⠒⠤⠄⣀⣀⠀⠉⠉⠉⠉⠀⠀⠀⣀⡀⠤⠂⠁⠀⢀⠆⠀⠀⢸⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣄⡀⠁⠉⠒⠂⠤⠤⣀⣀⣉⡉⠉⠉⠉⠉⢀⣀⣀⡠⠤⠒⠈⠀⠀⠀⠀⣸⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣶⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣶⣶⣶⣤⣤⣤⣤⣀⣀⣤⣤⣤⣶⣾⣿⣿⣿⣿⣿
"""
app.config["SESSION_TYPE"] = "mongodb"
app.config["SESSION_MONGODB"] = MongoClient(
"mongodb+srv://test:pSvJpf5T51CJJWk2@mc-web-server.ags8p.mongodb.net/"
)
app.config["SESSION_MONGODB_DB"] = "web-server"
app.config["SESSION_MONGODB_COLLECT"] = "sessions"
Session(app)
app.config["MONGO_URI"] = (
"mongodb+srv://test:pSvJpf5T51CJJWk2@mc-web-server.ags8p.mongodb.net/web-server"
)
mongo = PyMongo(app)
admin_pass = "lol, lmao"
minecraft_server_process = None
output_log = []
log_lock = threading.Lock()
logger = logging.getLogger(__name__)
handler = RotatingFileHandler(
"G:/MC servers/Divine Journey/web server/MC-web-server/logs/app.log",
maxBytes=100000,
backupCount=3,
)
logger.setLevel(logging.INFO)
logger.addHandler(handler)
@app.route("/")
def index():
if "logged_in" in session and session["logged_in"]:
return redirect(url_for("control_panel"))
return render_template("login.html")
@app.route("/login", methods=["POST"])
def login():
password = request.form["password"]
if password == admin_pass:
session["logged_in"] = True
return redirect(url_for("control_panel"))
else:
flash("Incorrect password dumbfuck. Try again or get out")
return redirect(url_for("index"))
@app.route("/logout")
def logout():
session.pop("logged_in", None)
return redirect(url_for("index"))
@app.route("/control_panel", methods=["GET"])
def control_panel():
if "logged_in" in session and session["logged_in"]:
return render_template("index.html")
return redirect(url_for("index"))
@app.route("/start_server", methods=["POST"])
def start_server():
global minecraft_server_process
if minecraft_server_process is None:
minecraft_server_process = subprocess.Popen(
["launch.bat"],
cwd="G:\\MC servers\\Divine Journey\\",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE,
shell=True,
text=True,
)
threading.Thread(target=read_output).start()
return jsonify(status="started", server_running=True)
else:
return jsonify(status="already_running", server_running=True)
@app.route("/stop_server", methods=["POST"])
def stop_server():
global minecraft_server_process
if minecraft_server_process is not None:
os.system("taskkill /f /pid " + str(minecraft_server_process.pid))
minecraft_server_process = None
return jsonify(status="stopped", server_running=False)
else:
return jsonify(status="not_running", server_running=False)
@app.route("/send_command", methods=["POST"])
def send_command():
global minecraft_server_process
command = request.form["command"]
if minecraft_server_process is not None:
minecraft_server_process.stdin.write(command + "\n")
minecraft_server_process.stdin.flush()
if command.lower().find("stop") != -1 and command.lower().find("debug") == -1:
minecraft_server_process = None
return jsonify(status="command_sent", server_running=True)
else:
return jsonify(status="not_running", server_running=False)
@app.route("/get_output", methods=["GET"])
def get_output():
global output_log
with log_lock:
output_copy = output_log[-100:]
return jsonify(
output=output_copy, server_running=(minecraft_server_process is not None)
)
@app.after_request
def after_request(response):
timestamp = strftime("[%Y-%b-%d %H:%M]")
logger.info(
"%s %s %s %s %s %s",
timestamp,
request.remote_addr,
request.method,
request.scheme,
request.full_path,
response.status,
)
return response
def read_output():
global minecraft_server_process, output_log
try:
while minecraft_server_process is not None:
output = minecraft_server_process.stdout.readline()
if output:
with log_lock:
if "get_output" not in output:
output_log.append(output.strip())
if len(output_log) > 100:
output_log = output_log[-100:]
if minecraft_server_process.poll() is not None:
break
minecraft_server_process = None
except:
output_log.append("Server closed")
@app.route("/stream_output")
def stream_output():
def event_stream():
while True:
time.sleep(1)
with log_lock:
output_copy = output_log[-100:]
yield f"data: {json.dumps({'output': output_copy, 'server_running': minecraft_server_process is not None})}\n\n"
return Response(event_stream(), content_type="text/event-stream")
@app.route("/get_player_data", methods=["GET"])
def get_player_data():
player_collection = mongo.db.players
players = list(player_collection.find())
totalGames = sum(player["wins"] for player in players)
for player in players:
player["_id"] = str(player["_id"])
if totalGames > 0:
player["winrate"] = (player["wins"] / totalGames) * 100
else:
player["winrate"] = 0
return jsonify(players)
@app.route("/save_player_data", methods=["POST"])
def save_player_data():
player_collection = mongo.db.players
data = request.json
for player in data:
player_collection.update_one(
{"name": player["name"]}, {"$set": {"wins": player["wins"]}}
)
return jsonify({"status": "success"})
@app.route("/stats")
def stats():
return render_template("stats.html")