Ejemplo n.º 1
0
    def __init__(
            self, system=None, arch=None,
            node=None, external_ip=None,
            internet=False, boottime=None
        ):
        self.system = system or platform.system()
        self.arch = arch or platform.machine().lower()
        self.arch = self.well_known_machines_equality.get(self.arch, self.arch)

        self.node = node or uuid.getnode()
        try:
            self.boottime = boottime or datetime.datetime.fromtimestamp(
                psutil.boot_time()
            )
        except:
            self.boottime = datetime.datetime.fromtimestamp(0)

        self.internet = bool(internet)
        self.external_ip = external_ip
        if external_ip is not None:
            if external_ip in ['0.0.0.0', u'0.0.0.0', 0, netaddr.IPAddress('0.0.0.0')]:
                self.external_ip = None
            else:
                self.external_ip = netaddr.IPAddress(external_ip)
                if self.external_ip.version == 6:
                    self.external_ip = None
        elif online:
            self.external_ip = online.external_ip(force_ipv4=True)
            if self.external_ip:
                self.internet = True
            else:
                self.internet = online.online()
Ejemplo n.º 2
0
def get_listener_ip_with_local(cache=True,
                               external=False,
                               config=None,
                               igd=None):
    '''
    Returns connectable external IP address
    '''

    global LISTENER_IP_EXTERNAL, LISTENER_IP_LOCAL

    if LISTENER_IP_LOCAL and cache and not external:
        return LISTENER_IP_LOCAL, True

    if LISTENER_IP_EXTERNAL and cache and external:
        return LISTENER_IP_EXTERNAL, False

    if not LISTENER_IP_EXTERNAL and config:
        try:
            LISTENER_IP_EXTERNAL = config.getip('pupyd', 'external')
        except AddrFormatError:
            LISTENER_IP_EXTERNAL = None

        if not LISTENER_IP_EXTERNAL and config.getboolean(
                'pupyd', 'allow_requests_to_external_services'):
            if igd and igd.available:
                try:
                    LISTENER_IP_EXTERNAL = str(
                        IPAddress(igd.GetExternalIP()['NewExternalIPAddress']))
                except Exception, e:
                    logging.warning('IGD Exception: %s', e)

            if not LISTENER_IP_EXTERNAL:
                ipv6 = config.getboolean('pupyd', 'ipv6')

                LISTENER_IP_EXTERNAL = external_ip(force_ipv4=not ipv6)

        if not LISTENER_IP_EXTERNAL:
            logging.warning('Failed to find out external IP')