Skip to content

Commit 6cf00dd

Browse files
committed
Port customizability function added.
1 parent a7e629a commit 6cf00dd

File tree

7 files changed

+133
-34
lines changed

7 files changed

+133
-34
lines changed

main.py

+36-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Description: Main file for the LockDown project
2+
import os
3+
import io
24
import webbrowser
35
from flask import Flask, render_template, request, redirect, url_for
46
from modules.config import readAppConfig, updateAppConfig, loadLanguageFiles, tryLoad
57

8+
import sys
9+
import subprocess
610

711
app = Flask(__name__)
812
appcfg = readAppConfig()
@@ -13,6 +17,17 @@
1317
port = tryLoad("app_port")
1418

1519

20+
def restart_server():
21+
"""Restarts the current Python script."""
22+
print("Restarting server...")
23+
python = sys.executable
24+
# Close stdout and stderr to prevent inheritance
25+
with open(os.devnull, 'wb') as devnull:
26+
os.dup2(devnull.fileno(), sys.stdout.fileno())
27+
os.dup2(devnull.fileno(), sys.stderr.fileno())
28+
os.execl(python, python, *sys.argv)
29+
30+
1631
@app.route("/set_language", methods=["POST"])
1732
def set_language():
1833
import time
@@ -45,11 +60,26 @@ def reset_everything():
4560
return redirect(url_for("index"))
4661

4762

63+
@app.route("/set_port", methods=["POST"])
64+
def set_port():
65+
global port
66+
67+
port = request.form.get("port")
68+
if port == "" or not port.isdigit() or int(port) < 1024 or int(port) > 65535:
69+
return redirect(url_for("index"))
70+
appcfg["app_port"] = port
71+
updateAppConfig("app_port", port)
72+
73+
restart_server() # Restart instead of shutdown
74+
return 'Server restarting...' # Indicate restart in progress
75+
76+
4877
@app.route("/")
4978
def index():
5079
if language == "":
5180
return render_template("language.html", version=version, language=language, author=author)
52-
return render_template("index.html", version=version, language=language, author=author, language_files=language_files)
81+
return render_template("index.html", version=version, language=language, author=author,
82+
language_files=language_files, port=port)
5383

5484

5585
@app.route("/status")
@@ -58,8 +88,8 @@ def status():
5888

5989

6090
if __name__ == "__main__":
61-
appcfg = readAppConfig()
62-
app_port = tryLoad("app_port")
63-
webbrowser.open("http://localhost:" + str(app_port))
64-
app.run(debug=True, port=app_port, host="0.0.0.0")
65-
91+
while True:
92+
appcfg = readAppConfig()
93+
app_port = tryLoad("app_port")
94+
# webbrowser.open("http://localhost:" + str(app_port))
95+
app.run(debug=True, port=app_port, host="0.0.0.0")

modules/config.py

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def tryLoad(key):
88
data = appcfg[key]
99
except:
1010
data = getDefaultConfig()[key]
11+
# Fix the missing key on the config file and update it.
12+
updateAppConfig(key, data)
1113
return data
1214

1315

templates/index.html

+88-25
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
}
4343
.main-screen {
4444
transition: opacity 1s ease-in-out;
45+
height: 70vh;
4546
}
4647
html {
4748
height: 100%;
@@ -50,6 +51,8 @@
5051
body{
5152
background-color: #212121; /* Bold and Energetic background */
5253
margin: 0;
54+
display: flex;
55+
flex-direction: column;
5356
}
5457
h1 {
5558
transition: opacity 1s;
@@ -85,9 +88,10 @@
8588
.footer-div {
8689
position: fixed;
8790
bottom: 0;
91+
height: 10vh;
8892
width: 100%;
8993
text-align: center;
90-
background-color: rgba(21, 102, 192, 0.09); /* Match header for consistency */
94+
background-color: rgb(32, 50, 70); /* Match header for consistency */
9195
padding: 10px;
9296
}
9397

@@ -206,7 +210,8 @@
206210
height: 100px;
207211
}
208212

209-
.language-card select {
213+
.port-number,
214+
.language-card select{
210215
width: 100%;
211216
padding: 5px;
212217
border: 1px solid #ddd;
@@ -258,21 +263,56 @@
258263
color: #000000; /* Bold and Energetic text */
259264
}
260265

261-
cardwidget {
266+
.cardwidget {
262267
display: flex;
263268
flex-wrap: wrap;
264269
justify-content: center;
265270
width: 80%;
271+
overflow-y: scroll;
272+
height: 100%;
273+
max-height: calc(70vh - 80px); /* Limit height based on viewport and spacing */
266274
}
267275

268276
@media (max-width: 768px) { /* Adjust the breakpoint as needed */
269277
.tabs {
270278
width: 90%; /* Set the width to 90% on mobile devices */
271279
}
272280
.settings-card{
273-
width: 90%;
281+
width: 100%;
282+
height: 120px;
274283
}
275284
}
285+
input[type=number]::-webkit-inner-spin-button,
286+
input[type=number]::-webkit-outer-spin-button {
287+
-webkit-appearance: none;
288+
margin: 0;
289+
}
290+
291+
/* Scrollbar Styling for Webkit browsers (Chrome, Safari) */
292+
::-webkit-scrollbar {
293+
width: 10px; /* Width of the scrollbar thumb */
294+
}
295+
296+
::-webkit-scrollbar-track {
297+
background: rgba(33, 33, 33, 0.8); /* Track background with some transparency */
298+
border-radius: 8px; /* Rounded corners for the track */
299+
}
300+
301+
::-webkit-scrollbar-thumb {
302+
background: #F25D5D; /* Thumb background color */
303+
border-radius: 8px; /* Rounded corners for the thumb */
304+
}
305+
306+
::-webkit-scrollbar-thumb:hover {
307+
background: #993d3d; /* Slightly darker shade for hover */
308+
}
309+
310+
311+
/* Scrollbar Styling for Firefox */
312+
* {
313+
scrollbar-width: thin;
314+
scrollbar-color: #F25D5D rgba(33, 33, 33, 0.8);
315+
}
276316
</style>
277317
</head>
278318
<body>
@@ -303,7 +343,7 @@ <h1>{{ language_files.home }}</h1>
303343
</div>
304344

305345
<div class="tab-content" id="settings">
306-
<cardwidget>
346+
<div class="cardwidget" >
307347
<div class="settings-card language-card">
308348
<h3>{{ language_files.language }}</h3>
309349
<label>
@@ -316,35 +356,35 @@ <h3>{{ language_files.language }}</h3>
316356
</label>
317357
<button class="click-buttons" id="save-language">{{ language_files.save }}</button>
318358
</div>
359+
<div class="settings-card" style="align-items: center;">
360+
<h3>{{ language_files.port }}</h3>
361+
<input style="width: 98%; margin-bottom: 10px" class="port-number" inputmode="numeric" type="number" name="port" value="{{ port }}" min="1" max="65535" placeholder="{{ language_files.portinfo }}">
362+
<button class="click-buttons" id="save-port" style="width: 100%">{{ language_files.save }}</button>
363+
</div>
319364
<div class="settings-card reset-card">
320365
<h3>{{ language_files.reset }}</h3>
321366
<p>{{ language_files.resetinfo }}</p>
322367
<button class="click-buttons" id="confirm-reset">{{ language_files.reset }}</button>
323368
</div>
324-
<div class="settings-card">
325-
<h3>{{ language_files.port }}</h3>
326-
<p>{{ language_files.portinfo }}</p>
327-
<input type="number" name="port" value="{{ port }}" min="1" max="65535" step="1">
328-
</div>
329-
</cardwidget>
330-
</div>
331369

332-
</div>
333-
</center>
334-
<center>
335-
<div class="footer-div" style="right: -10px; left:-10px;">
336-
<p class="version-text">{{ language_files.version }} {{ version }}</p>
337-
<div class="footer-tabs">
338-
<div style="display:none;" class="footer-tab" onclick="window.location.href='/about'">{{ language_files.about }}</div>
339-
<div class="footer-tab" style="padding: 0; height: 38px; border-radius: 20px; background-color: transparent"><a href="https://github.com/Eta06" target="_blank">
340-
<img src="{{ url_for('static', filename='images/github.png') }}" alt="GitHub" width="38" height="38" style="filter: invert(100%)">
341-
</a>
342370
</div>
343-
<div style="display:none;" class="footer-tab" onclick="window.location.href='/contact'">{{ language_files.contact }}</div>
344371
</div>
372+
345373
</div>
346374
</center>
347-
<script>
375+
<div class="footer-div" style="text-align: center;">
376+
<p class="version-text">{{ language_files.version }} {{ version }}</p>
377+
<div class="footer-tabs">
378+
<div style="display:none;" class="footer-tab" onclick="window.location.href='/about'">{{ language_files.about }}</div>
379+
<div class="footer-tab" style="padding: 0; height: 38px; border-radius: 20px; background-color: transparent"><a href="https://github.com/Eta06/LockDown" target="_blank">
380+
<img src="{{ url_for('static', filename='images/github.png') }}" alt="GitHub" width="38" height="38" style="filter: invert(100%)">
381+
</a>
382+
</div>
383+
<div style="display:none;" class="footer-tab" onclick="window.location.href='/contact'">{{ language_files.contact }}</div>
384+
</div>
385+
</div>
386+
387+
<script>
348388

349389
// on language save add listener to the save button
350390
document.getElementById("save-language").addEventListener("click", function () {
@@ -398,6 +438,29 @@ <h3>{{ language_files.port }}</h3>
398438
});
399439
});
400440

441+
// save the port number
442+
document.getElementById("save-port").addEventListener("click", function (){
443+
const port = document.querySelector('input[name="port"]').value;
444+
// save the port number
445+
fetch('/set_port', {
446+
method: 'POST',
447+
headers: {
448+
'Content-Type': 'application/x-www-form-urlencoded'
449+
},
450+
body: 'port=' + port
451+
})
452+
const domain = window.location.hostname;
453+
const url = `http://${domain}:${port}`;
454+
Swal.fire({
455+
title: "{{ language_files.portchanged }}",
456+
timeout: 3000,
457+
icon: "success",
458+
showCancelButton: false,
459+
})
460+
setTimeout(function () {
461+
window.location.href = url;
462+
}, 2000)
463+
})
401464

402465
function clearActiveTabs() {
403466
tabs.forEach(tab => tab.classList.remove('active'));
@@ -435,6 +498,6 @@ <h3>{{ language_files.port }}</h3>
435498
// document.getElementById(footerTabId).classList.add('active');
436499
});
437500
});
438-
</script>
501+
</script>
439502
</body>
440503
</html>

translations/en.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"yesreset": "Yes, reset!",
1717
"areyousure": "Are you sure?",
1818
"port": "Server Port",
19-
"portinfo": "Port number to use for the application",
19+
"portinfo": "Port number to use for the application. Default: 4900",
20+
"portchanged": "Port number changed!",
2021
"irreversibleAction": "This action cannot be undone!",
2122
"resetinfo": "Are you sure you want to reset all settings?",
2223
"infotext1": "This is the web UI for LockDown App.",

translations/es.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"yesreset": "Si, restablecer!",
1717
"areyousure": "¿Está seguro?",
1818
"port": "Puerto del servidor",
19-
"portinfo": "Número de puerto para usar la aplicación",
19+
"portinfo": "Número de puerto para utilizar la aplicación. Por defecto: 4900",
20+
"portchanged": "Número de puerto cambiado!",
2021
"irreversibleAction": "Esta acción no se puede deshacer!",
2122
"resetinfo": "¿Está seguro de que desea restablecer todas las configuraciones?",
2223
"infotext1": "Esta es la interfaz web de la aplicación LockDown.",

translations/ru.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"yesreset": "Да, сбросить!",
1717
"areyousure": "Вы уверены?",
1818
"port": "Порт сервера",
19-
"portinfo": "",
19+
"portinfo": "Номер порта для использования приложения. По умолчанию: 4900",
20+
"portchanged": "Номер порта изменен!",
2021
"irreversibleAction": "Эта операция не может быть отменена!",
2122
"resetinfo": "Вы уверены, что хотите сбросить все настройки?",
2223
"infotext1": "Это веб-интерфейс приложения LockDown.",

translations/tr.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"areyousure": "Emin misiniz?",
1818
"port": "Sunucu Portu",
1919
"portinfo": "Uygulama için kullanılacak port numarasıdır. Varsayılan: 4900",
20+
"portchanged": "Port numarası değiştirildi!",
2021
"irreversibleAction": "Bu işlem geri alınamaz!",
2122
"resetinfo": "Tüm ayarları sıfırlamak istediğinizden emin misiniz?",
2223
"infotext1": "Bu, LockDown uygulamasının web arayüzüdür.",

0 commit comments

Comments
 (0)