poprawka w logowaniu
This commit is contained in:
46
app.py
46
app.py
@@ -203,43 +203,49 @@ def get_real_ip():
|
|||||||
return request.remote_addr
|
return request.remote_addr
|
||||||
|
|
||||||
|
|
||||||
import os
|
|
||||||
import socket
|
|
||||||
import re
|
|
||||||
|
|
||||||
def is_allowed_ip(remote_ip, allowed_hosts_str):
|
def is_allowed_ip(remote_ip, allowed_hosts_str):
|
||||||
# awaryjny dostęp
|
|
||||||
if os.path.exists("emergency_access.txt"):
|
if os.path.exists("emergency_access.txt"):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if not allowed_hosts_str or not allowed_hosts_str.strip():
|
if not allowed_hosts_str or not allowed_hosts_str.strip():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
allowed_hosts = re.split(r"[\n,]+", allowed_hosts_str.strip())
|
|
||||||
allowed_ips = set()
|
allowed_ips = set()
|
||||||
|
hosts = re.split(r"[\n,]+", allowed_hosts_str.strip())
|
||||||
|
|
||||||
for host in allowed_hosts:
|
for host in hosts:
|
||||||
host = host.strip()
|
host = host.strip()
|
||||||
if not host:
|
if not host:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if re.match(r"^\d{1,3}(\.\d{1,3}){3}$", host):
|
|
||||||
allowed_ips.add(host)
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resolved_ip = socket.gethostbyname(host)
|
ip_obj = ipaddress.ip_address(host)
|
||||||
allowed_ips.add(resolved_ip)
|
allowed_ips.add(ip_obj)
|
||||||
except Exception:
|
continue
|
||||||
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
hostname = socket.gethostbyaddr(remote_ip)[0]
|
infos = socket.getaddrinfo(host, None)
|
||||||
app.logger.info(f"Odwiedzający IP: {remote_ip}, host: {hostname}")
|
for family, _, _, _, sockaddr in infos:
|
||||||
except Exception:
|
ip_str = sockaddr[0]
|
||||||
pass
|
try:
|
||||||
|
ip_obj = ipaddress.ip_address(ip_str)
|
||||||
|
allowed_ips.add(ip_obj)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
except Exception as e:
|
||||||
|
app.logger.warning(f"Nie można rozwiązać hosta {host}: {e}")
|
||||||
|
|
||||||
return remote_ip in allowed_ips
|
try:
|
||||||
|
remote_ip_obj = ipaddress.ip_address(remote_ip)
|
||||||
|
except ValueError:
|
||||||
|
app.logger.warning(f"Nieprawidłowe IP klienta: {remote_ip}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
is_allowed = remote_ip_obj in allowed_ips
|
||||||
|
app.logger.info(f"is_allowed_ip: {remote_ip_obj} -> {is_allowed} (lista: {allowed_ips})")
|
||||||
|
return is_allowed
|
||||||
|
|
||||||
|
|
||||||
def to_local(dt):
|
def to_local(dt):
|
||||||
|
|||||||
Reference in New Issue
Block a user