10 Commits
1.2.0 ... 1.2.3

Author SHA1 Message Date
beba1cad05 про ридми опять забыли( 2025-08-14 21:31:58 +00:00
36a76c4af7 added ability to change banlist file 2025-08-15 00:27:32 +03:00
4c8f908f18 added banlist 2025-08-15 00:25:49 +03:00
54c9ccef80 merge 1.2.2 2025-08-13 00:45:59 +03:00
0c1766d5d9 Merge branch 'testing'
penis
2025-08-13 00:44:38 +03:00
5527ef90e6 забыл про ридми 2025-08-13 00:40:52 +03:00
8c07d87efa какашки 2025-08-13 00:38:56 +03:00
96fd850b91 gfg 2025-08-11 12:03:01 +00:00
35353d63c8 cool patch with 5 lines of code 2025-08-11 11:55:14 +00:00
1d0ef1535f cool patch with 5 lines of code 2025-08-11 13:58:54 +03:00
6 changed files with 31 additions and 9 deletions

View File

@@ -26,6 +26,7 @@ pip install aiofiles
- `log_file` - файл логов (по умолчанию вывод в консоль) - `log_file` - файл логов (по умолчанию вывод в консоль)
- `preset_file` - файл пресета - `preset_file` - файл пресета
- `banlist_file` - файл с айпишниками, которые будут получать 403
- `directory` - рабочая директория - `directory` - рабочая директория
## буферы ## буферы
@@ -43,6 +44,7 @@ pip install aiofiles
- `<ADDR>` - адрес клиента - `<ADDR>` - адрес клиента
- `<FILE>` - файл / директория, к которой запрашивается доступ - `<FILE>` - файл / директория, к которой запрашивается доступ
- `<TIME>` - время, когда был выполнен запрос
## шаблоны ## шаблоны
@@ -63,7 +65,7 @@ pip install aiofiles
## preset.html ## preset.html
обычный html документ, являющийся шаблоном для листинга каталога обычный html документ, являющийся шаблоном для листинга каталога. если в директории будет находиться preset.html, сервер будет использовать именно его. в противном случае - тот, который указан в конфиге.
### теги пресета ### теги пресета

0
banlist.lol Normal file
View File

View File

@@ -1,4 +1,4 @@
name="debweb 1.2.0" name="debweb 1.2.3"
proxied=False proxied=False
addr="localhost" addr="localhost"
@@ -6,6 +6,7 @@ port=7856
log_file=None log_file=None
preset_file="preset.html" preset_file="preset.html"
banlist_file="banlist.lol"
directory="files/" directory="files/"
read_buffer=16384 read_buffer=16384

View File

@@ -1,7 +1,7 @@
<html> <html>
<head><title>404 Forbidden</title></head> <head><title>403 Forbidden</title></head>
<body> <body>
<center><h1>404 Forbidden</h1></center> <center><h1>403 Forbidden</h1></center>
<hr><center>debweb</center> <hr><center>debweb</center>
</body> </body>
</html> </html>

23
main.py
View File

@@ -21,6 +21,7 @@ class WebServer:
async def log(self, text: str, addr: tuple=None, file: str=None) -> None: async def log(self, text: str, addr: tuple=None, file: str=None) -> None:
text = text.replace("<ADDR>", f"{addr[0]}:{addr[1]}" if addr else "") text = text.replace("<ADDR>", f"{addr[0]}:{addr[1]}" if addr else "")
text = text.replace("<FILE>", file if file else "") text = text.replace("<FILE>", file if file else "")
text = text.replace("<TIME>", datetime.datetime.now().strftime(config.time_format))
if config.log_file: if config.log_file:
async with aiofiles.open(config.log_file, mode="a") as file: async with aiofiles.open(config.log_file, mode="a") as file:
@@ -76,7 +77,6 @@ class WebServer:
if real_addr and config.proxied: if real_addr and config.proxied:
addr = (real_addr, addr[1]) addr = (real_addr, addr[1])
request = data.split("\n")[0] request = data.split("\n")[0]
parts = request.split() parts = request.split()
if len(parts) < 2: return if len(parts) < 2: return
@@ -85,7 +85,7 @@ class WebServer:
file_name = path[1:] if path.startswith('/') else path file_name = path[1:] if path.startswith('/') else path
file_path = os.path.abspath(os.path.join(config.directory, file_name)) file_path = os.path.abspath(os.path.join(config.directory, file_name))
if not file_path.startswith(os.path.abspath(config.directory)): if not file_path.startswith(os.path.abspath(config.directory)): # directory traversal
await self.log(config.err_msgs[418], addr, file_path) await self.log(config.err_msgs[418], addr, file_path)
file_size = os.path.getsize(config.err_files[418]) file_size = os.path.getsize(config.err_files[418])
await self.send_headers(writer, 418, file_size) await self.send_headers(writer, 418, file_size)
@@ -94,6 +94,17 @@ class WebServer:
writer.close() writer.close()
await writer.wait_closed() await writer.wait_closed()
return return
if addr[0] in utils.get_banlist(config.banlist_file): # banlist for pidors
await self.log(config.err_msgs[403], addr, file_path)
file_size = os.path.getsize(config.err_files[403])
await self.send_headers(writer, 403, file_size)
await self.send_file(writer, config.err_files[403], file_size)
writer.close()
await writer.wait_closed()
return
if os.path.isfile(file_path): if os.path.isfile(file_path):
@@ -108,7 +119,11 @@ class WebServer:
elif os.path.isdir(file_path): elif os.path.isdir(file_path):
resp = "" resp = ""
async with aiofiles.open(config.preset_file, "r", encoding="utf-8") as f: if os.path.isfile(os.path.join(file_path, "preset.html")):
preset_file = os.path.join(file_path, "preset.html")
else:
preset_file = config.preset_file
async with aiofiles.open(preset_file, "r", encoding="utf-8") as f:
resp = await f.read() resp = await f.read()
files = "" files = ""
@@ -159,7 +174,7 @@ class WebServer:
else: else:
await self.log(config.err_msgs[404], addr, config.err_files[404]) await self.log(config.err_msgs[404], addr, file_path)
file_size = os.path.getsize(config.err_files[404]) file_size = os.path.getsize(config.err_files[404])
await self.send_headers(writer, 404, file_size) await self.send_headers(writer, 404, file_size)
await self.send_file(writer, config.err_files[404], file_size) await self.send_file(writer, config.err_files[404], file_size)

View File

@@ -10,4 +10,8 @@ def get_mod_time(path: str, format: str="%a %b %e %H:%M:%S %Z %Y") -> str:
def get_create_time(path: str, format: str="%a %b %e %H:%M:%S %Z %Y") -> str: def get_create_time(path: str, format: str="%a %b %e %H:%M:%S %Z %Y") -> str:
create_time = os.path.getctime(path) create_time = os.path.getctime(path)
create_datetime = datetime.datetime.fromtimestamp(create_time) create_datetime = datetime.datetime.fromtimestamp(create_time)
return create_datetime.strftime(format) return create_datetime.strftime(format)
def get_banlist(path: str) -> list:
return list(set(open("banlist.lol").readlines()))