2018-03-23 11:46:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
import uuid
|
2018-03-25 13:47:29 +00:00
|
|
|
import random
|
2019-11-13 08:40:34 +00:00
|
|
|
import re
|
2018-03-25 13:47:29 +00:00
|
|
|
|
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 _
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2020-03-12 08:24:38 +00:00
|
|
|
from .base import BaseUser
|
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)
|
|
|
|
name = models.CharField(max_length=128, unique=True, verbose_name=_('Name'))
|
|
|
|
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")
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def gateways(self):
|
|
|
|
return self.gateway_set.filter(is_active=True)
|
|
|
|
|
|
|
|
def random_gateway(self):
|
|
|
|
return random.choice(self.gateways)
|
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class Gateway(BaseUser):
|
2018-12-19 11:40:58 +00:00
|
|
|
PROTOCOL_SSH = 'ssh'
|
|
|
|
PROTOCOL_RDP = 'rdp'
|
2018-03-23 11:46:46 +00:00
|
|
|
PROTOCOL_CHOICES = (
|
2018-12-19 11:40:58 +00:00
|
|
|
(PROTOCOL_SSH, 'ssh'),
|
|
|
|
(PROTOCOL_RDP, 'rdp'),
|
2018-03-23 11:46:46 +00:00
|
|
|
)
|
|
|
|
ip = models.GenericIPAddressField(max_length=32, verbose_name=_('IP'), db_index=True)
|
|
|
|
port = models.IntegerField(default=22, verbose_name=_('Port'))
|
2018-12-19 11:40:58 +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")
|
2018-12-10 05:03:02 +00:00
|
|
|
|
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-12-03 04:03:18 +00:00
|
|
|
if self.password and not re.match(r'\w+$', self.password):
|
|
|
|
return False, _("Password should not contain special characters")
|
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,
|
|
|
|
paramiko.ssh_exception.NoValidConnectionsError) as e:
|
2018-12-10 05:03:02 +00:00
|
|
|
return False, str(e)
|
|
|
|
|
|
|
|
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)
|
|
|
|
except (paramiko.SSHException, paramiko.ssh_exception.SSHException,
|
|
|
|
paramiko.AuthenticationException, TimeoutError) as e:
|
|
|
|
return False, str(e)
|
|
|
|
finally:
|
|
|
|
client.close()
|
|
|
|
return True, None
|