2020-04-29 03:08:09 +00:00
|
|
|
# coding: utf-8
|
2021-10-22 12:06:16 +00:00
|
|
|
from jumpserver.context_processor import default_interface
|
|
|
|
from django.conf import settings
|
2023-08-07 01:37:24 +00:00
|
|
|
from IPy import IP
|
2020-04-29 03:08:09 +00:00
|
|
|
|
2023-08-29 09:02:51 +00:00
|
|
|
from common.utils import lookup_domain
|
|
|
|
|
2020-04-29 03:08:09 +00:00
|
|
|
|
2022-07-05 06:43:56 +00:00
|
|
|
def get_interface_setting_or_default():
|
2021-10-22 12:06:16 +00:00
|
|
|
if not settings.XPACK_ENABLED:
|
|
|
|
return default_interface
|
|
|
|
from xpack.plugins.interface.models import Interface
|
|
|
|
return Interface.get_interface_setting()
|
|
|
|
|
|
|
|
|
|
|
|
def get_login_title():
|
2022-07-05 06:43:56 +00:00
|
|
|
return get_interface_setting_or_default()['login_title']
|
2023-08-07 01:37:24 +00:00
|
|
|
|
|
|
|
|
2023-08-29 09:02:51 +00:00
|
|
|
def generate_ips(address_string):
|
2023-09-06 02:30:55 +00:00
|
|
|
def transform(_ip):
|
|
|
|
real_ip, err_msg = lookup_domain(_ip)
|
|
|
|
return _ip if err_msg else real_ip
|
2023-08-07 01:37:24 +00:00
|
|
|
# 支持的格式
|
|
|
|
# 192.168.1.1,192.168.1.2
|
|
|
|
# 192.168.1.1-12 | 192.168.1.1-192.168.1.12 | 192.168.1.0/30 | 192.168.1.1
|
|
|
|
ips = []
|
2023-08-29 09:02:51 +00:00
|
|
|
ip_list = address_string.split(',')
|
2023-09-06 02:30:55 +00:00
|
|
|
if len(ip_list) >= 1:
|
2023-08-07 01:37:24 +00:00
|
|
|
for ip in ip_list:
|
|
|
|
try:
|
2023-09-06 02:30:55 +00:00
|
|
|
ips.append(str(IP(transform(ip))))
|
2023-08-07 01:37:24 +00:00
|
|
|
except ValueError:
|
2023-09-06 02:30:55 +00:00
|
|
|
pass
|
|
|
|
if ips:
|
|
|
|
return ips
|
2023-08-07 01:37:24 +00:00
|
|
|
|
2023-08-29 09:02:51 +00:00
|
|
|
ip_list = address_string.split('-')
|
2023-08-07 01:37:24 +00:00
|
|
|
try:
|
|
|
|
if len(ip_list) == 2:
|
|
|
|
start_ip, end_ip = ip_list
|
|
|
|
if ip_list[1].find('.') == -1:
|
|
|
|
end_ip = start_ip[:start_ip.rindex('.') + 1] + end_ip
|
|
|
|
for ip in range(IP(start_ip).int(), IP(end_ip).int() + 1):
|
|
|
|
ips.extend((str(ip) for ip in IP(ip)))
|
|
|
|
else:
|
|
|
|
ips.extend((str(ip) for ip in IP(ip_list[0])))
|
|
|
|
except ValueError:
|
|
|
|
ips = []
|
|
|
|
return ips
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_port(port):
|
|
|
|
valid = True
|
|
|
|
try:
|
|
|
|
port = int(port)
|
|
|
|
if port > 65535 or port < 1:
|
|
|
|
valid = False
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
valid = False
|
|
|
|
return valid
|
|
|
|
|
2023-09-06 02:30:55 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
def generate_ports(ports):
|
|
|
|
port_list = []
|
|
|
|
if isinstance(ports, int):
|
|
|
|
port_list.append(ports)
|
|
|
|
elif isinstance(ports, str):
|
|
|
|
port_list.extend(
|
|
|
|
[int(p) for p in ports.split(',') if p.isdigit()]
|
|
|
|
)
|
|
|
|
elif isinstance(ports, list):
|
|
|
|
port_list = ports
|
|
|
|
port_list = list(map(int, filter(is_valid_port, port_list)))
|
|
|
|
return port_list
|