2018-03-23 11:46:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2021-07-14 03:17:48 +00:00
|
|
|
import socket
|
2018-03-23 11:46:46 +00:00
|
|
|
import uuid
|
2018-03-25 13:47:29 +00:00
|
|
|
import random
|
|
|
|
|
2021-07-26 08:32:01 +00:00
|
|
|
from django.core.cache import cache
|
2018-12-10 05:03:02 +00:00
|
|
|
import paramiko
|
2018-03-23 11:46:46 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2022-08-16 03:09:30 +00:00
|
|
|
from .base import BaseAccount
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2021-07-26 08:32:01 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
__all__ = ['Domain', 'Gateway']
|
|
|
|
|
|
|
|
|
2018-07-12 16:00:35 +00:00
|
|
|
class Domain(OrgModelMixin):
|
2018-03-23 11:46:46 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2020-07-02 10:51:16 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2018-03-23 11:46:46 +00:00
|
|
|
comment = models.TextField(blank=True, verbose_name=_('Comment'))
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, null=True,
|
|
|
|
verbose_name=_('Date created'))
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Domain")
|
2020-07-02 10:51:16 +00:00
|
|
|
unique_together = [('org_id', 'name')]
|
2021-01-17 04:44:12 +00:00
|
|
|
ordering = ('name',)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-03-25 13:47:29 +00:00
|
|
|
def has_gateway(self):
|
|
|
|
return self.gateway_set.filter(is_active=True).exists()
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
@lazyproperty
|
2018-03-25 13:47:29 +00:00
|
|
|
def gateways(self):
|
|
|
|
return self.gateway_set.filter(is_active=True)
|
|
|
|
|
|
|
|
def random_gateway(self):
|
2021-07-26 08:32:01 +00:00
|
|
|
gateways = [gw for gw in self.gateways if gw.is_connective]
|
|
|
|
if gateways:
|
|
|
|
return random.choice(gateways)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
logger.warn(f'Gateway all bad. domain={self}, gateway_num={len(self.gateways)}.')
|
|
|
|
if self.gateways:
|
2021-07-26 08:32:01 +00:00
|
|
|
return random.choice(self.gateways)
|
2018-03-25 13:47:29 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2022-08-16 03:09:30 +00:00
|
|
|
class Gateway(BaseAccount):
|
2021-07-26 08:32:01 +00:00
|
|
|
UNCONNECTIVE_KEY_TMPL = 'asset_unconnective_gateway_{}'
|
|
|
|
UNCONNECTIVE_SILENCE_PERIOD_KEY_TMPL = 'asset_unconnective_gateway_silence_period_{}'
|
|
|
|
UNCONNECTIVE_SILENCE_PERIOD_BEGIN_VALUE = 60 * 5
|
|
|
|
|
2022-02-21 08:24:03 +00:00
|
|
|
class Protocol(models.TextChoices):
|
2021-07-08 06:23:18 +00:00
|
|
|
ssh = 'ssh', 'SSH'
|
|
|
|
|
2020-10-28 08:54:54 +00:00
|
|
|
ip = models.CharField(max_length=128, verbose_name=_('IP'), db_index=True)
|
2018-03-23 11:46:46 +00:00
|
|
|
port = models.IntegerField(default=22, verbose_name=_('Port'))
|
2021-07-08 06:23:18 +00:00
|
|
|
protocol = models.CharField(choices=Protocol.choices, max_length=16, default=Protocol.ssh, verbose_name=_("Protocol"))
|
2018-07-25 03:21:12 +00:00
|
|
|
domain = models.ForeignKey(Domain, on_delete=models.CASCADE, verbose_name=_("Domain"))
|
2018-03-23 11:46:46 +00:00
|
|
|
comment = models.CharField(max_length=128, blank=True, null=True, verbose_name=_("Comment"))
|
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_("Is active"))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2018-03-25 13:47:29 +00:00
|
|
|
|
2018-07-17 09:09:36 +00:00
|
|
|
class Meta:
|
2018-07-20 02:54:16 +00:00
|
|
|
unique_together = [('name', 'org_id')]
|
2018-09-03 03:24:25 +00:00
|
|
|
verbose_name = _("Gateway")
|
2022-03-10 03:25:33 +00:00
|
|
|
permissions = [
|
|
|
|
('test_gateway', _('Test gateway'))
|
|
|
|
]
|
2018-12-10 05:03:02 +00:00
|
|
|
|
2021-07-26 08:32:01 +00:00
|
|
|
def set_unconnective(self):
|
|
|
|
unconnective_key = self.UNCONNECTIVE_KEY_TMPL.format(self.id)
|
|
|
|
unconnective_silence_period_key = self.UNCONNECTIVE_SILENCE_PERIOD_KEY_TMPL.format(self.id)
|
|
|
|
|
|
|
|
unconnective_silence_period = cache.get(unconnective_silence_period_key,
|
|
|
|
self.UNCONNECTIVE_SILENCE_PERIOD_BEGIN_VALUE)
|
|
|
|
cache.set(unconnective_silence_period_key, unconnective_silence_period * 2)
|
|
|
|
cache.set(unconnective_key, unconnective_silence_period, unconnective_silence_period)
|
|
|
|
|
|
|
|
def set_connective(self):
|
|
|
|
unconnective_key = self.UNCONNECTIVE_KEY_TMPL.format(self.id)
|
|
|
|
unconnective_silence_period_key = self.UNCONNECTIVE_SILENCE_PERIOD_KEY_TMPL.format(self.id)
|
|
|
|
|
|
|
|
cache.delete(unconnective_key)
|
|
|
|
cache.delete(unconnective_silence_period_key)
|
|
|
|
|
|
|
|
def get_is_unconnective(self):
|
|
|
|
unconnective_key = self.UNCONNECTIVE_KEY_TMPL.format(self.id)
|
|
|
|
return cache.get(unconnective_key, False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_connective(self):
|
|
|
|
return not self.get_is_unconnective()
|
|
|
|
|
|
|
|
@is_connective.setter
|
|
|
|
def is_connective(self, value):
|
|
|
|
if value:
|
|
|
|
self.set_connective()
|
|
|
|
else:
|
|
|
|
self.set_unconnective()
|
|
|
|
|
2019-03-19 11:09:09 +00:00
|
|
|
def test_connective(self, local_port=None):
|
|
|
|
if local_port is None:
|
|
|
|
local_port = self.port
|
2019-11-13 08:40:34 +00:00
|
|
|
|
2018-12-10 05:03:02 +00:00
|
|
|
client = paramiko.SSHClient()
|
|
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
proxy = paramiko.SSHClient()
|
|
|
|
proxy.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
|
|
|
|
try:
|
|
|
|
proxy.connect(self.ip, port=self.port,
|
|
|
|
username=self.username,
|
|
|
|
password=self.password,
|
|
|
|
pkey=self.private_key_obj)
|
|
|
|
except(paramiko.AuthenticationException,
|
|
|
|
paramiko.BadAuthenticationType,
|
2019-07-18 03:13:58 +00:00
|
|
|
paramiko.SSHException,
|
2021-09-13 10:47:20 +00:00
|
|
|
paramiko.ChannelException,
|
2021-07-14 03:17:48 +00:00
|
|
|
paramiko.ssh_exception.NoValidConnectionsError,
|
|
|
|
socket.gaierror) as e:
|
2021-07-26 07:50:09 +00:00
|
|
|
err = str(e)
|
|
|
|
if err.startswith('[Errno None] Unable to connect to port'):
|
|
|
|
err = _('Unable to connect to port {port} on {ip}')
|
|
|
|
err = err.format(port=self.port, ip=self.ip)
|
|
|
|
elif err == 'Authentication failed.':
|
|
|
|
err = _('Authentication failed')
|
2021-09-13 10:47:20 +00:00
|
|
|
elif err == 'Connect failed':
|
|
|
|
err = _('Connect failed')
|
2021-07-26 08:32:01 +00:00
|
|
|
self.is_connective = False
|
2021-07-26 07:50:09 +00:00
|
|
|
return False, err
|
2018-12-10 05:03:02 +00:00
|
|
|
|
|
|
|
try:
|
2019-03-19 11:09:09 +00:00
|
|
|
sock = proxy.get_transport().open_channel(
|
|
|
|
'direct-tcpip', ('127.0.0.1', local_port), ('127.0.0.1', 0)
|
|
|
|
)
|
|
|
|
client.connect("127.0.0.1", port=local_port,
|
2018-12-10 05:03:02 +00:00
|
|
|
username=self.username,
|
|
|
|
password=self.password,
|
|
|
|
key_filename=self.private_key_file,
|
|
|
|
sock=sock,
|
|
|
|
timeout=5)
|
2021-09-16 02:59:01 +00:00
|
|
|
except (paramiko.SSHException,
|
|
|
|
paramiko.ssh_exception.SSHException,
|
|
|
|
paramiko.ChannelException,
|
|
|
|
paramiko.AuthenticationException,
|
|
|
|
TimeoutError) as e:
|
|
|
|
|
|
|
|
err = getattr(e, 'text', str(e))
|
|
|
|
if err == 'Connect failed':
|
|
|
|
err = _('Connect failed')
|
2021-07-26 08:32:01 +00:00
|
|
|
self.is_connective = False
|
2021-09-16 02:59:01 +00:00
|
|
|
return False, err
|
2018-12-10 05:03:02 +00:00
|
|
|
finally:
|
|
|
|
client.close()
|
2021-07-26 08:32:01 +00:00
|
|
|
self.is_connective = True
|
2018-12-10 05:03:02 +00:00
|
|
|
return True, None
|