Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

dream-alpha / enigma2-plugin-extensions-vpnmanager   deb

Repository URL to install this package:

Version: 1.2.1 

/ usr / lib / enigma2 / python / Plugins / Extensions / VpnManager / infoHelper.py

# -*- coding: utf-8 -*-
from Components.MenuList import MenuList
from Components.config import config
from enigma import gFont, getDesktop, eListboxPythonMultiContent, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER, \
    RT_VALIGN_CENTER, eTimer
from Tools.LoadPixmap import LoadPixmap
import os
import re

STATUS = "/etc/openvpn/openvpn.stat"

sizes = [(1 << 50L, 'PB'),
         (1 << 40L, 'TB'),
         (1 << 30L, 'GB'),
         (1 << 20L, 'MB'),
         (1 << 10L, 'KB'),
         (1, 'B')
         ]

# Desktop
DESKTOPSIZE = getDesktop(0).size()
if DESKTOPSIZE.width() >= 1920:
    png_true = "/usr/lib/enigma2/python/Plugins/Extensions/VpnManager/image/true_1920.png"
    png_false = "/usr/lib/enigma2/python/Plugins/Extensions/VpnManager/image/false_1920.png"
else:
    png_true = "/usr/lib/enigma2/python/Plugins/Extensions/VpnManager/image/true_1280.png"
    png_false = "/usr/lib/enigma2/python/Plugins/Extensions/VpnManager/image/false_1280.png"


class infoHelper():
    def __init__(self):
        self.infoLabel = MenuList([], enableWrapAround=True, content=eListboxPythonMultiContent)
        self.infoLabel.l.setFont(0, gFont('Vpn', skinValueCalculate(27)))
        self.infoLabel.l.setItemHeight(skinValueCalculate(42))
        self['myInfoLabel'] = self.infoLabel

        self.updateTimer = eTimer()
        self.updateTimer_conn = self.updateTimer.timeout.connect(self.load_info)

        self.onLayoutFinish.append(self.load_info)

    def load_info(self):
        info = []
        if config.vpnmanager.type.value == "vpn":
            version = checkVpnVersion()
            if version:
                info.append((_("OpenVPN-Version:"), version, False))
        info.append((_("Autostart:"), config.vpnmanager.autostart.value, True))
        if config.vpnmanager.type.value == "vpn":
            info.append((_("Resolv conf:"), config.vpnmanager.resolv.value, True))

        if config.vpnmanager.type.value == "wireguard":
            if os.path.isfile("/var/run/resolvconf/interfaces/Wg0"):
                with open("/var/run/resolvconf/interfaces/Wg0", "r") as resolv_conf:
                    x = 1
                    for line in resolv_conf.readlines():
                        if "nameserver" in line:
                            dns = "DNS %s:" % str(x)
                            text = re.sub("nameserver", "", line).strip()
                            info.append((dns, text, False))
                            x = x + 1

        elif config.vpnmanager.type.value == "vpn":
            if os.path.isfile("/etc/resolv.conf"):
                with open("/etc/resolv.conf", "r") as resolv_conf:
                    x = 1
                    for line in resolv_conf.readlines():
                        if "nameserver" in line:
                            dns = "DNS %s:" % str(x)
                            text = re.sub("nameserver", "", line).strip()
                            info.append((dns, text, False))
                            x = x + 1

        if os.path.isfile(STATUS) and config.vpnmanager.type.value == "vpn":
            stat = open(STATUS, "r")
            for line in stat.readlines():
                cols = line.split(',')
                if len(cols) == 2:
                    if is_number(cols[1].replace("\n", "")):
                        item = cols[0].replace("bytes", "").strip()
                        item_info = byte2str(int(cols[1].replace("\n", "")))
                        info.append((item, item_info, False))

        self.infoLabel.setList(list(map(set_info_label, info)))
        self['myInfoLabel'].selectionEnabled(0)
        if not self.StatusSpinner:
            self.updateTimer.start(12000, True)


def byte2str(size):
    f = suf = 0
    for f, suf in sizes:
        if size >= f:
            break
    return "%.2f %s" % (size / float(f), suf)


def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False


def set_info_label(entry):
    res = [entry]
    if entry[2]:
        res.append(
            (eListboxPythonMultiContent.TYPE_TEXT, skinValueCalculate(8), 0, skinValueCalculate(280), skinValueCalculate(42), 0, RT_HALIGN_LEFT | RT_VALIGN_CENTER, entry[0]))
        if entry[1]:
            png = LoadPixmap(png_true)
        else:
            png = LoadPixmap(png_false)
        res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHABLEND, skinValueCalculate(300), skinValueCalculate(5), skinValueCalculate(45), skinValueCalculate(32), png))
    else:
        res.append(
            (eListboxPythonMultiContent.TYPE_TEXT, skinValueCalculate(8), 0, skinValueCalculate(280), skinValueCalculate(42), 0, RT_HALIGN_LEFT | RT_VALIGN_CENTER, entry[0]))
        res.append(
            (eListboxPythonMultiContent.TYPE_TEXT, skinValueCalculate(300), 0, skinValueCalculate(200), skinValueCalculate(42), 0, RT_HALIGN_LEFT | RT_VALIGN_CENTER, entry[1]))
    return res


def checkVpnVersion():
    version = ""
    command = os.popen("apt list --installed *openvpn*")
    command = command.read()
    v = re.findall("(\d.\d.\d)", command, re.M)
    if v:
        version = v[0]
    return version


def skinValueCalculate(value):
    if DESKTOPSIZE.width() > 1920:
        # 2560x1440
        skinFactor = 1.33333333333
        skinMultiply = True
    elif DESKTOPSIZE.width() == 1920:
        # 1920x1080
        skinFactor = 1
        skinMultiply = False
    else:
        # 1280x720
        skinFactor = 1.5
        skinMultiply = False
    if skinMultiply:
        return int(float(round(value * skinFactor)))
    else:
        return int(value / skinFactor)