<commit_before>from platform import system, release
from sys import exit, stdout
from os import getuid
from os.path import dirname, abspath
from pwd import getpwuid
from netifaces import interfaces
from random import choice
from string import lowercase, uppercase, digits
from netifaces import ifaddresses, gateways, AF_LINK, AF_INET, AF_INET6
from scapy.all import srp, Ether, ARP
from netaddr import IPNetwork, IPAddress
from struct import pack, error

class Base:
    cINFO = None
    cERROR = None
    cSUCCESS = None
    cWARNING = None
    cEND = None

    c_info = None
    c_error = None
    c_success = None
    c_warning = None

    def __init__(self):
        self.cINFO = '\033[1;34m'
        self.cERROR = '\033[1;31m'
        self.cSUCCESS = '\033[1;32m'
        self.cWARNING = '\033[1;33m'
        self.cEND = '\033[0m'

        self.c_info = self.cINFO + '[*]' + self.cEND + ' '
        self.c_error = self.cERROR + '[-]' + self.cEND + ' '
        self.c_success = self.cSUCCESS + '[+]' + self.cEND + ' '
        self.c_warning = self.cWARNING + '[!]' + self.cEND + ' '

    @staticmethod
    def print_banner():
        with open(dirname(abspath(__file__)) + '/version.txt', 'r') as version_file:
            current_version = version_file.read()
        greenc = '\033[1;32m'
        yellowc = '\033[1;33m'
        endc = '\033[0m'
        print greenc + "                                          _        _   " + endc
        print greenc + " _ __ __ ___      __     _ __   __ _  ___| | _____| |_ " + endc
        print greenc + "| '__/ _` \ \ /\ / /___ | '_ \ / _` |/ __| |/ / _ \ __|" + endc
        print greenc + "| | | (_| |\ V  V /|___|| |_) | (_| | (__|   <  __/ |_ " + endc
        print greenc + "|_|  \__,_| \_/\_/      | .__/ \__,_|\___|_|\_\___|\__|" + endc
        print greenc + "                        |_|                      v" + current_version + endc
        print yellowc + "\r\nhttps://github.com/Vladimir-Ivanov-Git/raw-packet\r\n" + endc

    def color_print(self, color, *strings):
        if color == "blue":
            stdout.write(self.c_info)
        if color == "red":
            stdout.write(self.c_error)
        if color == "orange":
            stdout.write(self.c_warning)
        if color == "green":
            stdout.write(self.c_success)
        for index in range(len(strings)):
            if index % 2 == 0:
                stdout.write(strings[index])
            else:
                if color == "blue":
                    stdout.write(self.cINFO)
                if color == "red":
                    stdout.write(self.cERROR)
                if color == "orange":
                    stdout.write(self.cWARNING)
                if color == "green":
                    stdout.write(self.cSUCCESS)
                stdout.write(strings[index] + self.cEND)
        stdout.write("\n")

    def print_info(self, *strings):
        self.color_print("blue", *strings)

    def print_error(self, *strings):
        self.color_print("red", *strings)

    def print_warning(self, *strings):
        self.color_print("orange", *strings)

    def print_success(self, *strings):
        self.color_print("green", *strings)

    @staticmethod
    def check_platform():
        if system() != "Linux":
            print "This script can run only in Linux platform!"
            print "Your platform: " + str(system()) + " " + str(release()) + " not supported!"
            exit(1)

    @staticmethod
    def check_user():
        if getuid() != 0:
            print "Only root can run this script!"
            print "You: " + str(getpwuid(getuid())[0]) + " can not run this script!"
            exit(1)

    @staticmethod
    def pack8(data):
        try:
            return pack("B", data)
        except error:
            print "Bad value for 8 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack16(data):
        try:
            return pack("!H", data)
        except error:
            print "Bad value for 16 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack32(data):
        try:
            return pack("<I", data)
        except error:
            print "Bad value for 32 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack64(data):
        try:
            return pack("<Q", data)
        except error:
            print "Bad value for 64 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def netiface_selection():
        netiface_index = 1
        current_netifaces = interfaces()

        print "Your interface list:"
        for netiface in current_netifaces:
            print " " + str(netiface_index) + ") " + netiface
            netiface_index += 1

        netiface_index -= 1
        current_netiface_index = raw_input('Set network interface from range (1-' + str(netiface_index) + '): ')

        if not current_netiface_index.isdigit():
            print "Your input data is not digit!"
            exit(1)

        if any([int(current_netiface_index) < 1, int(current_netiface_index) > netiface_index]):
            print "Your number is not within range (1-" + str(netiface_index) + ")"
            exit(1)

        current_network_interface = ""
        try:
            current_network_interface = str(current_netifaces[int(current_netiface_index) - 1])
        except:
            print "This network interface has some problem"
            exit(1)
        return current_network_interface

    @staticmethod
    def get_netiface_mac_address(interface_name):
        try:
            mac_address = str(ifaddresses(interface_name)[AF_LINK][0]['addr'])
        except:
            mac_address = None
        return mac_address

    @staticmethod
    def get_netiface_ip_address(interface_name):
        try:
            ip_address = str(ifaddresses(interface_name)[AF_INET][0]['addr'])
        except:
            ip_address = None
        return ip_address

    @staticmethod
    def get_netiface_ipv6_address(interface_name, address_number=0):
        try:
            ipv6_address = str(ifaddresses(interface_name)[AF_INET6][address_number]['addr'])
            ipv6_address = ipv6_address.replace("%" + interface_name, "", 1)
        except:
            ipv6_address = None
        return ipv6_address

    def get_netiface_ipv6_link_address(self, interface_name):
        for index in range(10):
            ipv6_address = self.get_netiface_ipv6_address(interface_name, index)
            if ipv6_address.startswith("fe80::"):
                return ipv6_address
        return None

    def get_netiface_ipv6_glob_address(self, interface_name):
        for index in range(10):
            ipv6_address = self.get_netiface_ipv6_address(interface_name, index)
            if ipv6_address.startswith("fe80::"):
                pass
            else:
                return ipv6_address
        return None

    @staticmethod
    def create_ipv6_link_address(mac_address):
        try:
            parts = mac_address.split(":")
            parts.insert(3, "ff")
            parts.insert(4, "fe")
            parts[0] = "%x" % (int(parts[0], 16) ^ 2)
            ipv6Parts = []
            for i in range(0, len(parts), 2):
                ipv6Parts.append("".join(parts[i:i + 2]))
            ipv6_address = "fe80::%s" % (":".join(ipv6Parts))
        except:
            ipv6_address = None
        return ipv6_address

    @staticmethod
    def get_netiface_netmask(interface_name):
        try:
            netmask = str(ifaddresses(interface_name)[AF_INET][0]['netmask'])
        except:
            netmask = None
        return netmask

    def get_netiface_first_ip(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            first_ip = str(ip[2])
        except:
            first_ip = None
        return first_ip

    def get_netiface_last_ip(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            first_ip = str(ip[-3])
        except:
            first_ip = None
        return first_ip

    def get_netiface_net(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            network = str(ip[0]) + "/" + str(IPAddress(netmask).netmask_bits())
        except:
            network = None
        return network

    @staticmethod
    def get_netiface_broadcast(interface_name):
        try:
            broadcast = str(ifaddresses(interface_name)[AF_INET][0]['broadcast'])
        except:
            broadcast = None
        return broadcast

    @staticmethod
    def make_random_string(length):
        return ''.join(choice(lowercase + uppercase + digits) for _ in range(length))

    @staticmethod
    def get_mac(iface, ip):
        gw_ip = ""
        gws = gateways()
        for gw in gws.keys():
            try:
                if str(gws[gw][AF_INET][1]) == iface:
                    gw_ip = str(gws[gw][AF_INET][0])
            except IndexError:
                if str(gws[gw][0][1]) == iface:
                    gw_ip = str(gws[gw][0][0])
        try:
            alive, dead = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip), iface=iface, timeout=10, verbose=0)
            return str(alive[0][1].hwsrc)
        except IndexError:
            try:
                alive, dead = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=gw_ip), iface=iface, timeout=10, verbose=0)
                return str(alive[0][1].hwsrc)
            except:
                return "ff:ff:ff:ff:ff:ff"
        except:
            return "ff:ff:ff:ff:ff:ff"
<commit_msg>Add function check installed OS software.<commit_after>from platform import system, release, dist
from sys import exit, stdout
from os import getuid
from os.path import dirname, abspath
from pwd import getpwuid
from netifaces import interfaces
from random import choice
from string import lowercase, uppercase, digits
from netifaces import ifaddresses, gateways, AF_LINK, AF_INET, AF_INET6
from scapy.all import srp, Ether, ARP
from netaddr import IPNetwork, IPAddress
from struct import pack, error
from os import errno
import subprocess as sub

class Base:
    cINFO = None
    cERROR = None
    cSUCCESS = None
    cWARNING = None
    cEND = None

    c_info = None
    c_error = None
    c_success = None
    c_warning = None

    os_installed_packages_list = None

    def __init__(self):
        self.cINFO = '\033[1;34m'
        self.cERROR = '\033[1;31m'
        self.cSUCCESS = '\033[1;32m'
        self.cWARNING = '\033[1;33m'
        self.cEND = '\033[0m'

        self.c_info = self.cINFO + '[*]' + self.cEND + ' '
        self.c_error = self.cERROR + '[-]' + self.cEND + ' '
        self.c_success = self.cSUCCESS + '[+]' + self.cEND + ' '
        self.c_warning = self.cWARNING + '[!]' + self.cEND + ' '

    @staticmethod
    def print_banner():
        with open(dirname(abspath(__file__)) + '/version.txt', 'r') as version_file:
            current_version = version_file.read()
        greenc = '\033[1;32m'
        yellowc = '\033[1;33m'
        endc = '\033[0m'
        print greenc + "                                          _        _   " + endc
        print greenc + " _ __ __ ___      __     _ __   __ _  ___| | _____| |_ " + endc
        print greenc + "| '__/ _` \ \ /\ / /___ | '_ \ / _` |/ __| |/ / _ \ __|" + endc
        print greenc + "| | | (_| |\ V  V /|___|| |_) | (_| | (__|   <  __/ |_ " + endc
        print greenc + "|_|  \__,_| \_/\_/      | .__/ \__,_|\___|_|\_\___|\__|" + endc
        print greenc + "                        |_|                      v" + current_version + endc
        print yellowc + "\r\nhttps://github.com/Vladimir-Ivanov-Git/raw-packet\r\n" + endc

    def color_print(self, color, *strings):
        if color == "blue":
            stdout.write(self.c_info)
        if color == "red":
            stdout.write(self.c_error)
        if color == "orange":
            stdout.write(self.c_warning)
        if color == "green":
            stdout.write(self.c_success)
        for index in range(len(strings)):
            if index % 2 == 0:
                stdout.write(strings[index])
            else:
                if color == "blue":
                    stdout.write(self.cINFO)
                if color == "red":
                    stdout.write(self.cERROR)
                if color == "orange":
                    stdout.write(self.cWARNING)
                if color == "green":
                    stdout.write(self.cSUCCESS)
                stdout.write(strings[index] + self.cEND)
        stdout.write("\n")

    def print_info(self, *strings):
        self.color_print("blue", *strings)

    def print_error(self, *strings):
        self.color_print("red", *strings)

    def print_warning(self, *strings):
        self.color_print("orange", *strings)

    def print_success(self, *strings):
        self.color_print("green", *strings)

    @staticmethod
    def check_platform():
        if system() != "Linux":
            print "This script can run only in Linux platform!"
            print "Your platform: " + str(system()) + " " + str(release()) + " not supported!"
            exit(1)

    @staticmethod
    def check_user():
        if getuid() != 0:
            print "Only root can run this script!"
            print "You: " + str(getpwuid(getuid())[0]) + " can not run this script!"
            exit(1)

    @staticmethod
    def pack8(data):
        try:
            return pack("B", data)
        except error:
            print "Bad value for 8 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack16(data):
        try:
            return pack("!H", data)
        except error:
            print "Bad value for 16 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack32(data):
        try:
            return pack("<I", data)
        except error:
            print "Bad value for 32 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack64(data):
        try:
            return pack("<Q", data)
        except error:
            print "Bad value for 64 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def netiface_selection():
        netiface_index = 1
        current_netifaces = interfaces()

        print "Your interface list:"
        for netiface in current_netifaces:
            print " " + str(netiface_index) + ") " + netiface
            netiface_index += 1

        netiface_index -= 1
        current_netiface_index = raw_input('Set network interface from range (1-' + str(netiface_index) + '): ')

        if not current_netiface_index.isdigit():
            print "Your input data is not digit!"
            exit(1)

        if any([int(current_netiface_index) < 1, int(current_netiface_index) > netiface_index]):
            print "Your number is not within range (1-" + str(netiface_index) + ")"
            exit(1)

        current_network_interface = ""
        try:
            current_network_interface = str(current_netifaces[int(current_netiface_index) - 1])
        except:
            print "This network interface has some problem"
            exit(1)
        return current_network_interface

    @staticmethod
    def get_netiface_mac_address(interface_name):
        try:
            mac_address = str(ifaddresses(interface_name)[AF_LINK][0]['addr'])
        except:
            mac_address = None
        return mac_address

    @staticmethod
    def get_netiface_ip_address(interface_name):
        try:
            ip_address = str(ifaddresses(interface_name)[AF_INET][0]['addr'])
        except:
            ip_address = None
        return ip_address

    @staticmethod
    def get_netiface_ipv6_address(interface_name, address_number=0):
        try:
            ipv6_address = str(ifaddresses(interface_name)[AF_INET6][address_number]['addr'])
            ipv6_address = ipv6_address.replace("%" + interface_name, "", 1)
        except:
            ipv6_address = None
        return ipv6_address

    def get_netiface_ipv6_link_address(self, interface_name):
        for index in range(10):
            ipv6_address = self.get_netiface_ipv6_address(interface_name, index)
            if ipv6_address.startswith("fe80::"):
                return ipv6_address
        return None

    def get_netiface_ipv6_glob_address(self, interface_name):
        for index in range(10):
            ipv6_address = self.get_netiface_ipv6_address(interface_name, index)
            if ipv6_address.startswith("fe80::"):
                pass
            else:
                return ipv6_address
        return None

    @staticmethod
    def create_ipv6_link_address(mac_address):
        try:
            parts = mac_address.split(":")
            parts.insert(3, "ff")
            parts.insert(4, "fe")
            parts[0] = "%x" % (int(parts[0], 16) ^ 2)
            ipv6Parts = []
            for i in range(0, len(parts), 2):
                ipv6Parts.append("".join(parts[i:i + 2]))
            ipv6_address = "fe80::%s" % (":".join(ipv6Parts))
        except:
            ipv6_address = None
        return ipv6_address

    @staticmethod
    def get_netiface_netmask(interface_name):
        try:
            netmask = str(ifaddresses(interface_name)[AF_INET][0]['netmask'])
        except:
            netmask = None
        return netmask

    def get_netiface_first_ip(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            first_ip = str(ip[2])
        except:
            first_ip = None
        return first_ip

    def get_netiface_last_ip(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            first_ip = str(ip[-3])
        except:
            first_ip = None
        return first_ip

    def get_netiface_net(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            network = str(ip[0]) + "/" + str(IPAddress(netmask).netmask_bits())
        except:
            network = None
        return network

    @staticmethod
    def get_netiface_broadcast(interface_name):
        try:
            broadcast = str(ifaddresses(interface_name)[AF_INET][0]['broadcast'])
        except:
            broadcast = None
        return broadcast

    @staticmethod
    def make_random_string(length):
        return ''.join(choice(lowercase + uppercase + digits) for _ in range(length))

    @staticmethod
    def get_mac(iface, ip):
        gw_ip = ""
        gws = gateways()
        for gw in gws.keys():
            try:
                if str(gws[gw][AF_INET][1]) == iface:
                    gw_ip = str(gws[gw][AF_INET][0])
            except IndexError:
                if str(gws[gw][0][1]) == iface:
                    gw_ip = str(gws[gw][0][0])
        try:
            alive, dead = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip), iface=iface, timeout=10, verbose=0)
            return str(alive[0][1].hwsrc)
        except IndexError:
            try:
                alive, dead = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=gw_ip), iface=iface, timeout=10, verbose=0)
                return str(alive[0][1].hwsrc)
            except:
                return "ff:ff:ff:ff:ff:ff"
        except:
            return "ff:ff:ff:ff:ff:ff"

    def ubuntu_list_installed_packages(self):
        apt_list_out = None
        try:
            apt_list = sub.Popen(['apt list --installed'], shell=True, stdout=sub.PIPE, stderr=sub.PIPE)
            apt_list_out, apt_list_err = apt_list.communicate()
        except OSError as e:
            if e.errno == errno.ENOENT:
                self.print_error("Program: ", "apt", " is not installed!")
                exit(1)
            else:
                self.print_error("Something else went wrong while trying to run ", "`apt list --installed`")
                exit(2)
        if apt_list_out is not None:
            self.os_installed_packages_list = apt_list_out
        return apt_list_out

    def check_installed_software(self, software_name):
        self.check_platform()
        if "Kali" in dist() or "Ubuntu" in dist():
            if self.os_installed_packages_list is None:
                self.ubuntu_list_installed_packages()
            if self.os_installed_packages_list is None:
                self.print_warning("Unable to verify OS installed software.")
                return True
            else:
                if software_name in self.os_installed_packages_list:
                    return True
                else:
                    self.print_error("Software: " + software_name + " is not installed!")
                    return False
        else:
            self.print_warning("Unable to verify OS installed software. This function works only in Kali or Ubuntu")
            return True<|endoftext|>


<commit_before>from platform import system, release, dist
from sys import exit, stdout
from os import getuid
from os.path import dirname, abspath
from pwd import getpwuid
from netifaces import interfaces
from random import choice
from string import lowercase, uppercase, digits
from netifaces import ifaddresses, gateways, AF_LINK, AF_INET, AF_INET6
from scapy.all import srp, Ether, ARP
from netaddr import IPNetwork, IPAddress
from struct import pack, error
from os import errno
import subprocess as sub

class Base:
    cINFO = None
    cERROR = None
    cSUCCESS = None
    cWARNING = None
    cEND = None

    c_info = None
    c_error = None
    c_success = None
    c_warning = None

    os_installed_packages_list = None

    def __init__(self):
        self.cINFO = '\033[1;34m'
        self.cERROR = '\033[1;31m'
        self.cSUCCESS = '\033[1;32m'
        self.cWARNING = '\033[1;33m'
        self.cEND = '\033[0m'

        self.c_info = self.cINFO + '[*]' + self.cEND + ' '
        self.c_error = self.cERROR + '[-]' + self.cEND + ' '
        self.c_success = self.cSUCCESS + '[+]' + self.cEND + ' '
        self.c_warning = self.cWARNING + '[!]' + self.cEND + ' '

    @staticmethod
    def print_banner():
        with open(dirname(abspath(__file__)) + '/version.txt', 'r') as version_file:
            current_version = version_file.read()
        greenc = '\033[1;32m'
        yellowc = '\033[1;33m'
        endc = '\033[0m'
        print greenc + "                                          _        _   " + endc
        print greenc + " _ __ __ ___      __     _ __   __ _  ___| | _____| |_ " + endc
        print greenc + "| '__/ _` \ \ /\ / /___ | '_ \ / _` |/ __| |/ / _ \ __|" + endc
        print greenc + "| | | (_| |\ V  V /|___|| |_) | (_| | (__|   <  __/ |_ " + endc
        print greenc + "|_|  \__,_| \_/\_/      | .__/ \__,_|\___|_|\_\___|\__|" + endc
        print greenc + "                        |_|                      v" + current_version + endc
        print yellowc + "\r\nhttps://github.com/Vladimir-Ivanov-Git/raw-packet\r\n" + endc

    def color_print(self, color, *strings):
        if color == "blue":
            stdout.write(self.c_info)
        if color == "red":
            stdout.write(self.c_error)
        if color == "orange":
            stdout.write(self.c_warning)
        if color == "green":
            stdout.write(self.c_success)
        for index in range(len(strings)):
            if index % 2 == 0:
                stdout.write(strings[index])
            else:
                if color == "blue":
                    stdout.write(self.cINFO)
                if color == "red":
                    stdout.write(self.cERROR)
                if color == "orange":
                    stdout.write(self.cWARNING)
                if color == "green":
                    stdout.write(self.cSUCCESS)
                stdout.write(strings[index] + self.cEND)
        stdout.write("\n")

    def print_info(self, *strings):
        self.color_print("blue", *strings)

    def print_error(self, *strings):
        self.color_print("red", *strings)

    def print_warning(self, *strings):
        self.color_print("orange", *strings)

    def print_success(self, *strings):
        self.color_print("green", *strings)

    @staticmethod
    def check_platform():
        if system() != "Linux":
            print "This script can run only in Linux platform!"
            print "Your platform: " + str(system()) + " " + str(release()) + " not supported!"
            exit(1)

    @staticmethod
    def check_user():
        if getuid() != 0:
            print "Only root can run this script!"
            print "You: " + str(getpwuid(getuid())[0]) + " can not run this script!"
            exit(1)

    @staticmethod
    def pack8(data):
        try:
            return pack("B", data)
        except error:
            print "Bad value for 8 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack16(data):
        try:
            return pack("!H", data)
        except error:
            print "Bad value for 16 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack32(data):
        try:
            return pack("<I", data)
        except error:
            print "Bad value for 32 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack64(data):
        try:
            return pack("<Q", data)
        except error:
            print "Bad value for 64 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def netiface_selection():
        netiface_index = 1
        current_netifaces = interfaces()

        print "Your interface list:"
        for netiface in current_netifaces:
            print " " + str(netiface_index) + ") " + netiface
            netiface_index += 1

        netiface_index -= 1
        current_netiface_index = raw_input('Set network interface from range (1-' + str(netiface_index) + '): ')

        if not current_netiface_index.isdigit():
            print "Your input data is not digit!"
            exit(1)

        if any([int(current_netiface_index) < 1, int(current_netiface_index) > netiface_index]):
            print "Your number is not within range (1-" + str(netiface_index) + ")"
            exit(1)

        current_network_interface = ""
        try:
            current_network_interface = str(current_netifaces[int(current_netiface_index) - 1])
        except:
            print "This network interface has some problem"
            exit(1)
        return current_network_interface

    @staticmethod
    def get_netiface_mac_address(interface_name):
        try:
            mac_address = str(ifaddresses(interface_name)[AF_LINK][0]['addr'])
        except:
            mac_address = None
        return mac_address

    @staticmethod
    def get_netiface_ip_address(interface_name):
        try:
            ip_address = str(ifaddresses(interface_name)[AF_INET][0]['addr'])
        except:
            ip_address = None
        return ip_address

    @staticmethod
    def get_netiface_ipv6_address(interface_name, address_number=0):
        try:
            ipv6_address = str(ifaddresses(interface_name)[AF_INET6][address_number]['addr'])
            ipv6_address = ipv6_address.replace("%" + interface_name, "", 1)
        except:
            ipv6_address = None
        return ipv6_address

    def get_netiface_ipv6_link_address(self, interface_name):
        for index in range(10):
            ipv6_address = self.get_netiface_ipv6_address(interface_name, index)
            if ipv6_address.startswith("fe80::"):
                return ipv6_address
        return None

    def get_netiface_ipv6_glob_address(self, interface_name):
        for index in range(10):
            ipv6_address = self.get_netiface_ipv6_address(interface_name, index)
            if ipv6_address.startswith("fe80::"):
                pass
            else:
                return ipv6_address
        return None

    @staticmethod
    def create_ipv6_link_address(mac_address):
        try:
            parts = mac_address.split(":")
            parts.insert(3, "ff")
            parts.insert(4, "fe")
            parts[0] = "%x" % (int(parts[0], 16) ^ 2)
            ipv6Parts = []
            for i in range(0, len(parts), 2):
                ipv6Parts.append("".join(parts[i:i + 2]))
            ipv6_address = "fe80::%s" % (":".join(ipv6Parts))
        except:
            ipv6_address = None
        return ipv6_address

    @staticmethod
    def get_netiface_netmask(interface_name):
        try:
            netmask = str(ifaddresses(interface_name)[AF_INET][0]['netmask'])
        except:
            netmask = None
        return netmask

    def get_netiface_first_ip(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            first_ip = str(ip[2])
        except:
            first_ip = None
        return first_ip

    def get_netiface_last_ip(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            first_ip = str(ip[-3])
        except:
            first_ip = None
        return first_ip

    def get_netiface_net(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            network = str(ip[0]) + "/" + str(IPAddress(netmask).netmask_bits())
        except:
            network = None
        return network

    @staticmethod
    def get_netiface_broadcast(interface_name):
        try:
            broadcast = str(ifaddresses(interface_name)[AF_INET][0]['broadcast'])
        except:
            broadcast = None
        return broadcast

    @staticmethod
    def make_random_string(length):
        return ''.join(choice(lowercase + uppercase + digits) for _ in range(length))

    @staticmethod
    def get_mac(iface, ip):
        gw_ip = ""
        gws = gateways()
        for gw in gws.keys():
            try:
                if str(gws[gw][AF_INET][1]) == iface:
                    gw_ip = str(gws[gw][AF_INET][0])
            except IndexError:
                if str(gws[gw][0][1]) == iface:
                    gw_ip = str(gws[gw][0][0])
        try:
            alive, dead = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip), iface=iface, timeout=10, verbose=0)
            return str(alive[0][1].hwsrc)
        except IndexError:
            try:
                alive, dead = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=gw_ip), iface=iface, timeout=10, verbose=0)
                return str(alive[0][1].hwsrc)
            except:
                return "ff:ff:ff:ff:ff:ff"
        except:
            return "ff:ff:ff:ff:ff:ff"

    def ubuntu_list_installed_packages(self):
        apt_list_out = None
        try:
            apt_list = sub.Popen(['apt list --installed'], shell=True, stdout=sub.PIPE, stderr=sub.PIPE)
            apt_list_out, apt_list_err = apt_list.communicate()
        except OSError as e:
            if e.errno == errno.ENOENT:
                self.print_error("Program: ", "apt", " is not installed!")
                exit(1)
            else:
                self.print_error("Something else went wrong while trying to run ", "`apt list --installed`")
                exit(2)
        if apt_list_out is not None:
            self.os_installed_packages_list = apt_list_out
        return apt_list_out

    def check_installed_software(self, software_name):
        self.check_platform()
        if "Kali" in dist() or "Ubuntu" in dist():
            if self.os_installed_packages_list is None:
                self.ubuntu_list_installed_packages()
            if self.os_installed_packages_list is None:
                self.print_warning("Unable to verify OS installed software.")
                return True
            else:
                if software_name in self.os_installed_packages_list:
                    return True
                else:
                    self.print_error("Software: " + software_name + " is not installed!")
                    return False
        else:
            self.print_warning("Unable to verify OS installed software. This function works only in Kali or Ubuntu")
            return True<commit_msg>Added function check process.<commit_after>from platform import system, release, dist
from sys import exit, stdout
from os import getuid
from os.path import dirname, abspath
from pwd import getpwuid
from netifaces import interfaces
from random import choice
from string import lowercase, uppercase, digits
from netifaces import ifaddresses, gateways, AF_LINK, AF_INET, AF_INET6
from scapy.all import srp, Ether, ARP
from netaddr import IPNetwork, IPAddress
from struct import pack, error
from os import errno
import subprocess as sub
import psutil as ps

class Base:
    cINFO = None
    cERROR = None
    cSUCCESS = None
    cWARNING = None
    cEND = None

    c_info = None
    c_error = None
    c_success = None
    c_warning = None

    os_installed_packages_list = None

    def __init__(self):
        self.cINFO = '\033[1;34m'
        self.cERROR = '\033[1;31m'
        self.cSUCCESS = '\033[1;32m'
        self.cWARNING = '\033[1;33m'
        self.cEND = '\033[0m'

        self.c_info = self.cINFO + '[*]' + self.cEND + ' '
        self.c_error = self.cERROR + '[-]' + self.cEND + ' '
        self.c_success = self.cSUCCESS + '[+]' + self.cEND + ' '
        self.c_warning = self.cWARNING + '[!]' + self.cEND + ' '

    @staticmethod
    def print_banner():
        with open(dirname(abspath(__file__)) + '/version.txt', 'r') as version_file:
            current_version = version_file.read()
        greenc = '\033[1;32m'
        yellowc = '\033[1;33m'
        endc = '\033[0m'
        print greenc + "                                          _        _   " + endc
        print greenc + " _ __ __ ___      __     _ __   __ _  ___| | _____| |_ " + endc
        print greenc + "| '__/ _` \ \ /\ / /___ | '_ \ / _` |/ __| |/ / _ \ __|" + endc
        print greenc + "| | | (_| |\ V  V /|___|| |_) | (_| | (__|   <  __/ |_ " + endc
        print greenc + "|_|  \__,_| \_/\_/      | .__/ \__,_|\___|_|\_\___|\__|" + endc
        print greenc + "                        |_|                      v" + current_version + endc
        print yellowc + "\r\nhttps://github.com/Vladimir-Ivanov-Git/raw-packet\r\n" + endc

    def color_print(self, color, *strings):
        if color == "blue":
            stdout.write(self.c_info)
        if color == "red":
            stdout.write(self.c_error)
        if color == "orange":
            stdout.write(self.c_warning)
        if color == "green":
            stdout.write(self.c_success)
        for index in range(len(strings)):
            if index % 2 == 0:
                stdout.write(strings[index])
            else:
                if color == "blue":
                    stdout.write(self.cINFO)
                if color == "red":
                    stdout.write(self.cERROR)
                if color == "orange":
                    stdout.write(self.cWARNING)
                if color == "green":
                    stdout.write(self.cSUCCESS)
                stdout.write(strings[index] + self.cEND)
        stdout.write("\n")

    def print_info(self, *strings):
        self.color_print("blue", *strings)

    def print_error(self, *strings):
        self.color_print("red", *strings)

    def print_warning(self, *strings):
        self.color_print("orange", *strings)

    def print_success(self, *strings):
        self.color_print("green", *strings)

    @staticmethod
    def check_platform():
        if system() != "Linux":
            print "This script can run only in Linux platform!"
            print "Your platform: " + str(system()) + " " + str(release()) + " not supported!"
            exit(1)

    @staticmethod
    def check_user():
        if getuid() != 0:
            print "Only root can run this script!"
            print "You: " + str(getpwuid(getuid())[0]) + " can not run this script!"
            exit(1)

    @staticmethod
    def pack8(data):
        try:
            return pack("B", data)
        except error:
            print "Bad value for 8 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack16(data):
        try:
            return pack("!H", data)
        except error:
            print "Bad value for 16 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack32(data):
        try:
            return pack("<I", data)
        except error:
            print "Bad value for 32 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def pack64(data):
        try:
            return pack("<Q", data)
        except error:
            print "Bad value for 64 bit pack: " + str(data)
            exit(1)

    @staticmethod
    def netiface_selection():
        netiface_index = 1
        current_netifaces = interfaces()

        print "Your interface list:"
        for netiface in current_netifaces:
            print " " + str(netiface_index) + ") " + netiface
            netiface_index += 1

        netiface_index -= 1
        current_netiface_index = raw_input('Set network interface from range (1-' + str(netiface_index) + '): ')

        if not current_netiface_index.isdigit():
            print "Your input data is not digit!"
            exit(1)

        if any([int(current_netiface_index) < 1, int(current_netiface_index) > netiface_index]):
            print "Your number is not within range (1-" + str(netiface_index) + ")"
            exit(1)

        current_network_interface = ""
        try:
            current_network_interface = str(current_netifaces[int(current_netiface_index) - 1])
        except:
            print "This network interface has some problem"
            exit(1)
        return current_network_interface

    @staticmethod
    def get_netiface_mac_address(interface_name):
        try:
            mac_address = str(ifaddresses(interface_name)[AF_LINK][0]['addr'])
        except:
            mac_address = None
        return mac_address

    @staticmethod
    def get_netiface_ip_address(interface_name):
        try:
            ip_address = str(ifaddresses(interface_name)[AF_INET][0]['addr'])
        except:
            ip_address = None
        return ip_address

    @staticmethod
    def get_netiface_ipv6_address(interface_name, address_number=0):
        try:
            ipv6_address = str(ifaddresses(interface_name)[AF_INET6][address_number]['addr'])
            ipv6_address = ipv6_address.replace("%" + interface_name, "", 1)
        except:
            ipv6_address = None
        return ipv6_address

    def get_netiface_ipv6_link_address(self, interface_name):
        for index in range(10):
            ipv6_address = self.get_netiface_ipv6_address(interface_name, index)
            if ipv6_address.startswith("fe80::"):
                return ipv6_address
        return None

    def get_netiface_ipv6_glob_address(self, interface_name):
        for index in range(10):
            ipv6_address = self.get_netiface_ipv6_address(interface_name, index)
            if ipv6_address.startswith("fe80::"):
                pass
            else:
                return ipv6_address
        return None

    @staticmethod
    def create_ipv6_link_address(mac_address):
        try:
            parts = mac_address.split(":")
            parts.insert(3, "ff")
            parts.insert(4, "fe")
            parts[0] = "%x" % (int(parts[0], 16) ^ 2)
            ipv6Parts = []
            for i in range(0, len(parts), 2):
                ipv6Parts.append("".join(parts[i:i + 2]))
            ipv6_address = "fe80::%s" % (":".join(ipv6Parts))
        except:
            ipv6_address = None
        return ipv6_address

    @staticmethod
    def get_netiface_netmask(interface_name):
        try:
            netmask = str(ifaddresses(interface_name)[AF_INET][0]['netmask'])
        except:
            netmask = None
        return netmask

    def get_netiface_first_ip(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            first_ip = str(ip[2])
        except:
            first_ip = None
        return first_ip

    def get_netiface_last_ip(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            first_ip = str(ip[-3])
        except:
            first_ip = None
        return first_ip

    def get_netiface_net(self, interface_name):
        try:
            netmask = self.get_netiface_netmask(interface_name)
            ip_address = self.get_netiface_ip_address(interface_name)
            ip = IPNetwork(ip_address + '/' + netmask)
            network = str(ip[0]) + "/" + str(IPAddress(netmask).netmask_bits())
        except:
            network = None
        return network

    @staticmethod
    def get_netiface_broadcast(interface_name):
        try:
            broadcast = str(ifaddresses(interface_name)[AF_INET][0]['broadcast'])
        except:
            broadcast = None
        return broadcast

    @staticmethod
    def make_random_string(length):
        return ''.join(choice(lowercase + uppercase + digits) for _ in range(length))

    @staticmethod
    def get_mac(iface, ip):
        gw_ip = ""
        gws = gateways()
        for gw in gws.keys():
            try:
                if str(gws[gw][AF_INET][1]) == iface:
                    gw_ip = str(gws[gw][AF_INET][0])
            except IndexError:
                if str(gws[gw][0][1]) == iface:
                    gw_ip = str(gws[gw][0][0])
        try:
            alive, dead = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip), iface=iface, timeout=10, verbose=0)
            return str(alive[0][1].hwsrc)
        except IndexError:
            try:
                alive, dead = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=gw_ip), iface=iface, timeout=10, verbose=0)
                return str(alive[0][1].hwsrc)
            except:
                return "ff:ff:ff:ff:ff:ff"
        except:
            return "ff:ff:ff:ff:ff:ff"

    def ubuntu_list_installed_packages(self):
        apt_list_out = None
        try:
            apt_list = sub.Popen(['apt list --installed'], shell=True, stdout=sub.PIPE, stderr=sub.PIPE)
            apt_list_out, apt_list_err = apt_list.communicate()
        except OSError as e:
            if e.errno == errno.ENOENT:
                self.print_error("Program: ", "apt", " is not installed!")
                exit(1)
            else:
                self.print_error("Something else went wrong while trying to run ", "`apt list --installed`")
                exit(2)
        if apt_list_out is not None:
            self.os_installed_packages_list = apt_list_out
        return apt_list_out

    def check_installed_software(self, software_name):
        self.check_platform()
        if "Kali" in dist() or "Ubuntu" in dist():
            if self.os_installed_packages_list is None:
                self.ubuntu_list_installed_packages()
            if self.os_installed_packages_list is None:
                self.print_warning("Unable to verify OS installed software.")
                return True
            else:
                if software_name in self.os_installed_packages_list:
                    return True
                else:
                    self.print_error("Software: " + software_name + " is not installed!")
                    return False
        else:
            self.print_warning("Unable to verify OS installed software. This function works only in Kali or Ubuntu")
            return True

    @staticmethod
    def check_process(process_name):
        for process in ps.process_iter():
            if 'python' in process.name():
                for argument in process.cmdline():
                    if process_name in argument:
                        return process.pid
            if process.name() == process_name:
                return process.pid
        return 0<|endoftext|>


<commit_before>from keras.models import Sequential, model_from_json
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import *
from keras.optimizers import SGD
from tetris_game import POSSIBLE_MOVES, TIME_PER_TICK, MOVES_POOL
from collections import deque
import os
import glob
import numpy as np
import queue
import threading
import time
import random
import math

N_REPLAYS_PER_ROUND = 5

# SARS - [State_0, Action, Reward, State_1]
STATE0_INDEX = 0
ACTION_INDEX = 1
REWARD_INDEX = 2
STATE1_INDEX = 3

BUFFER_SIZE = 1000000

class Agent():
    def __init__(self):
        self.init_model()
        self.save_requests = 0
        # Treat as a ring buffer
        self.max_pos = 0
        self.current_pos = 0
        self.n_plays = 0
        self.states_t0 = np.zeros((BUFFER_SIZE,1,22,10), dtype=np.int8)
        self.actions = np.zeros([BUFFER_SIZE])
        self.states_t1 = np.zeros((BUFFER_SIZE,1,22,10), dtype=np.int8)
        self.rewards = np.zeros([BUFFER_SIZE])

    def exploit(self):
        # Simple linear exploit, max at 90% exploitations
        return random.random() < 0.80

    def choose_action(self, state):
        if self.exploit():
            print(np.array(state, ndmin=4))
            vals = self.model.predict(np.array(state, ndmin=4), verbose=0)
            print(vals)
            return np.argmax( vals)
        return random.choice(MOVES_POOL)

    def handle(self, state0, action, reward, state1):
        self.states_t0[self.current_pos] = state0
        self.actions[self.current_pos] = action
        self.rewards[self.current_pos] = reward
        self.states_t1[self.current_pos] = state1
        self.current_pos = (self.current_pos + 1) % BUFFER_SIZE
        self.n_plays += 1
        self.max_pos = min(self.max_pos + 1, BUFFER_SIZE)
        if reward > 0 and self.n_plays > 4:
            # Prioritized Sweeping
            indexes = [x % BUFFER_SIZE for x in range(self.current_pos - 1, self.current_pos - 3, -1)]
            for i, index in enumerate(indexes):
                self.rewards[index] += reward * (1/(2+index))
            self.train_on_indexes(indexes)
        self.experience_replay()
        if reward == -10000:
           self.save()

    def save(self):
        self.save_requests += 1
        if self.save_requests == 100:
            self.save_requests = 0
            name = time.strftime("%m-%dT%H%M%S%Z")
            model_file = 'output/model_{}.json'.format(name)
            weights_file = 'output/weights_{}.h5'.format(name)
            open(model_file, 'w').write(self.model.to_json())
            self.model.save_weights(weights_file, overwrite=True)
            print('Saved: {} and {}'.format(model_file, weights_file))

    def experience_replay(self):
        indexes = np.random.randint(0, self.max_pos, N_REPLAYS_PER_ROUND)
        self.train_on_indexes(indexes)

    def train_on_indexes(self, indexes):
        y = self.model.predict(self.states_t0[indexes], verbose=0)
        future_rewards = np.amax(self.model.predict(self.states_t1[indexes], verbose=0), axis=1)
        for i, a in enumerate(self.actions[indexes]):
            y[i][a] = self.rewards[indexes][i] + future_rewards[i]
        self.model.train_on_batch(self.states_t0[indexes], y)

    def init_model(self):
    #     self.model = model_from_json(open(max(glob.iglob('output/model_*.json'), key=os.path.getctime)).read())
    #     self.model.load_weights(max(glob.iglob('output/weights_*.h5'), key=os.path.getctime))
        self.model = Sequential()
        # 32 Convolution filters of size 2x2 each
        self.model.add(Convolution2D(32, 2, 2,
                                     input_shape=(1,22,10),
                                     activation='relu'))
        # Second convolution layer
        self.model.add(Convolution2D(64, 4, 4,
                                     input_shape=(32,21,9),
                                     activation='relu'))
        # Flatten to a single vector of inputs
        self.model.add(Flatten())
        # Dense hidden layer
        self.model.add(Dense(64, activation='relu', init='uniform'))
        self.model.add(Dense(64, activation='relu', init='uniform'))
        self.model.add(Dense(len(POSSIBLE_MOVES), activation='linear', init='uniform'))
        self.model.compile(loss='mse', optimizer='rmsprop')

class GreedyAgent(Agent):
    def __init__(self, model_path=None, weights_path=None):
        threading.Thread.__init__(self)
        self.model_path = model_path
        self.weights_path = weights_path
        self.init_model()
        self.replay_memory = []

    def init_model(self):
        if self.model_path:
            self.model = model_from_json(open(self.model_path).read())
        else:
            self.model = model_from_json(open(max(glob.iglob('output/model_*.json'), key=os.path.getctime)).read())
        if self.weights_path:
            self.model.load_weights(self.weights_path)
        else:
            self.model.load_weights(max(glob.iglob('output/weights_*.h5'), key=os.path.getctime))

    def choose_action(self, state):
        time.sleep(0.05)
        return random.choice(MOVES_POOL)
        # return np.argmax(self.model.predict(np.array(state, ndmin=4), batch_size=1, verbose=0))

    def handle(self, s0, a, r, s1):
        pass
<commit_msg>debugging<commit_after>from keras.models import Sequential, model_from_json
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import *
from keras.optimizers import SGD
from tetris_game import POSSIBLE_MOVES, TIME_PER_TICK, MOVES_POOL
from collections import deque
import os
import glob
import numpy as np
import queue
import threading
import time
import random
import math

N_REPLAYS_PER_ROUND = 5

# SARS - [State_0, Action, Reward, State_1]
STATE0_INDEX = 0
ACTION_INDEX = 1
REWARD_INDEX = 2
STATE1_INDEX = 3

BUFFER_SIZE = 1000000

class Agent():
    def __init__(self):
        self.init_model()
        self.save_requests = 0
        # Treat as a ring buffer
        self.max_pos = 0
        self.current_pos = 0
        self.n_plays = 0
        self.states_t0 = np.zeros((BUFFER_SIZE,1,22,10), dtype=np.int8)
        self.actions = np.zeros([BUFFER_SIZE])
        self.states_t1 = np.zeros((BUFFER_SIZE,1,22,10), dtype=np.int8)
        self.rewards = np.zeros([BUFFER_SIZE])

    def exploit(self):
        # Simple linear exploit, max at 90% exploitations
        return random.random() < 0.80

    def choose_action(self, state):
        if self.exploit():
            vals = self.model.predict(np.array(state, ndmin=4), verbose=0)
            if random.random() < 0.001:
                print('Some predicted values for a board:')
                print(np.array(state, ndmin=4))
                print('[ROTATE_LEFT, ROTATE_RIGHT, MOVE_RIGHT, MOVE_LEFT, MOVE_DOWN, DO_NOTHING]')
                print(vals)
            return np.argmax(vals)
        return random.choice(MOVES_POOL)

    def handle(self, state0, action, reward, state1):
        self.states_t0[self.current_pos] = state0
        self.actions[self.current_pos] = action
        self.rewards[self.current_pos] = reward
        self.states_t1[self.current_pos] = state1
        self.current_pos = (self.current_pos + 1) % BUFFER_SIZE
        self.n_plays += 1
        self.max_pos = min(self.max_pos + 1, BUFFER_SIZE)
        if reward > 0 and self.n_plays > 4:
            # Prioritized Sweeping
            indexes = [x % BUFFER_SIZE for x in range(self.current_pos - 1, self.current_pos - 3, -1)]
            for i, index in enumerate(indexes):
                self.rewards[index] += reward * (1/(2+index))
            self.train_on_indexes(indexes)
        self.experience_replay()
        if reward == -10000:
           self.save()

    def save(self):
        self.save_requests += 1
        if self.save_requests == 100:
            self.save_requests = 0
            name = time.strftime("%m-%dT%H%M%S%Z")
            model_file = 'output/model_{}.json'.format(name)
            weights_file = 'output/weights_{}.h5'.format(name)
            open(model_file, 'w').write(self.model.to_json())
            self.model.save_weights(weights_file, overwrite=True)
            print('Saved: {} and {}'.format(model_file, weights_file))

    def experience_replay(self):
        indexes = np.random.randint(0, self.max_pos, N_REPLAYS_PER_ROUND)
        self.train_on_indexes(indexes)

    def train_on_indexes(self, indexes):
        y = self.model.predict(self.states_t0[indexes], verbose=0)
        future_rewards = np.amax(self.model.predict(self.states_t1[indexes], verbose=0), axis=1)
        for i, a in enumerate(self.actions[indexes]):
            y[i][a] = self.rewards[indexes][i] + future_rewards[i]
        self.model.train_on_batch(self.states_t0[indexes], y)

    def init_model(self):
    #     self.model = model_from_json(open(max(glob.iglob('output/model_*.json'), key=os.path.getctime)).read())
    #     self.model.load_weights(max(glob.iglob('output/weights_*.h5'), key=os.path.getctime))
        self.model = Sequential()
        # 32 Convolution filters of size 2x2 each
        self.model.add(Convolution2D(32, 2, 2,
                                     input_shape=(1,22,10),
                                     activation='relu'))
        # Second convolution layer
        self.model.add(Convolution2D(64, 4, 4,
                                     input_shape=(32,21,9),
                                     activation='relu'))
        # Flatten to a single vector of inputs
        self.model.add(Flatten())
        # Dense hidden layer
        self.model.add(Dense(64, activation='relu', init='uniform'))
        self.model.add(Dense(64, activation='relu', init='uniform'))
        self.model.add(Dense(len(POSSIBLE_MOVES), activation='linear', init='uniform'))
        self.model.compile(loss='mse', optimizer='rmsprop')

class GreedyAgent(Agent):
    def __init__(self, model_path=None, weights_path=None):
        threading.Thread.__init__(self)
        self.model_path = model_path
        self.weights_path = weights_path
        self.init_model()
        self.replay_memory = []

    def init_model(self):
        if self.model_path:
            self.model = model_from_json(open(self.model_path).read())
        else:
            self.model = model_from_json(open(max(glob.iglob('output/model_*.json'), key=os.path.getctime)).read())
        if self.weights_path:
            self.model.load_weights(self.weights_path)
        else:
            self.model.load_weights(max(glob.iglob('output/weights_*.h5'), key=os.path.getctime))

    def choose_action(self, state):
        time.sleep(0.05)
        return random.choice(MOVES_POOL)
        # return np.argmax(self.model.predict(np.array(state, ndmin=4), batch_size=1, verbose=0))

    def handle(self, s0, a, r, s1):
        pass
<|endoftext|>


<commit_before>from datetime import datetime
import json
import urlparse
from requests import Session
from lxml import html

from properties import *

__author__ = '4ikist'

log = logger.getChild("vk_api")

class APIException(Exception):
    pass

class VK_API():
    def __init__(self, login, pwd):
        self.session = Session()
        self.session.verify = certs_path
        self.base_url = 'https://api.vk.com/method/'
        self.login = login
        self.pwd = pwd
        self.__get_access_token(login, pwd)

    def __form_lp_server_address(self, ts=None):
        if ts is None:
            self.lp_server_params = self.get('messages.getLongPollServer')

        self.lp_server_connection = 'http://%s?act=a_check&key=%s&ts=%s&wait=25&mode=0' % (
            self.lp_server_params.get('server'),
            self.lp_server_params.get('key'),
            self.lp_server_params.get('ts') if ts is None else ts)

    def __get_access_token(self, login, pwd):
        if (not hasattr(self, 'access_expires') and not hasattr(self, 'last_auth')) or (
                    self.last_auth - datetime.now()).total_seconds() > self.access_expires:
            auth = self.__auth__(login,pwd)
            self.access_token = auth['access_token']
            self.user_id = auth['user_id']
            self.access_expires = auth['expires_in']
            self.last_auth = datetime.now()

        return self.access_token

    def __auth__(self, vk_login, vk_pass):
        log.info('auth...')
        result = self.session.get('https://oauth.vk.com/authorize', params=vk_access_credentials)
        doc = html.document_fromstring(result.content)
        inputs = doc.xpath('//input')
        form_params = {}
        for el in inputs:
            form_params[el.attrib.get('name')] = el.value
        form_params['email'] = vk_login
        form_params['pass'] = vk_pass
        form_url = doc.xpath('//form')[0].attrib.get('action')
        # process second page
        result = self.session.post(form_url, form_params)
        doc = html.document_fromstring(result.content)
        # if already login
        if 'OAuth Blank' not in doc.xpath('//title')[0].text:
            submit_url = doc.xpath('//form')[0].attrib.get('action')
            result = self.session.post(submit_url, cookies=result.cookies)

        # retrieving access token from url
        parsed_url = urlparse.urlparse(result.url)
        if 'error' in parsed_url.query:
            log.error('error in authenticate \n%s' % parsed_url.query)
            raise APIException(dict([el.split('=') for el in parsed_url.query.split('&')]))

        fragment = parsed_url.fragment
        access_token = dict([el.split('=') for el in fragment.split('&')])
        log.info('auth was successful')
        return access_token

    def get(self, method_name, **kwargs):
        params = dict({'access_token': self.__get_access_token(self.login, self.pwd)}, **kwargs)
        result = self.session.get('%s%s' % (self.base_url, method_name), params=params)
        result_object = json.loads(result.content)
        if 'error' in result_object:
            if result_object['error']['error_code'] == 14:
                kwargs['captcha_key'] = raw_input(
                    "please input what you see at [%s]" % result_object['error']['captcha_img'])
                kwargs['captcha_sid'] = result_object['error']['captcha_sid']
                return self.get(method_name, **kwargs)
            raise APIException(result_object)
        return result_object['response']

    def mark_as_read(self, messages):
        messages_str = ','.join([str(m) for m in messages])
        result = self.get('messages.markAsRead', **{'message_ids': messages_str})
        if result != 1:
            log.error('can not mark as read messages %s' % messages_str)

    def get_messages(self):
        self.__form_lp_server_address()
        log.info('will retrieve messages')
        while True:
            result = self.session.get(self.lp_server_connection)
            result = json.loads(result.content)
            if result.get('failed') == 2:
                self.__form_lp_server_address()
            else:
                self.__form_lp_server_address(ts=result.get('ts'))
            read_messages = []
            for update in result['updates']:
                if update[0] == 4 and update[2] in (1, 17, 33, 49) and update[2] != 3:
                    message_id = update[1]
                    from_id = update[3]
                    text = update[-1]
                    tstamp = update[4]
                    read_messages.append(message_id)
                    yield {'from': from_id, 'text': text.lower(), 'timestamp': tstamp}

            if read_messages:
                self.mark_as_read(read_messages)

    def send_message(self, user_id, text):
        params = {'user_id': user_id, 'message': text}
        result = self.get('messages.send', **params)
        return result

    def add_followers_to_friends(self):
        params = {'count': 100, 'filters': 'followers'}
        result = self.get('notifications.get', **params)
        followers = []
        for follower_meta in result['items'][1:]:
            if follower_meta['type'] == 'follow':
                for follower_id in follower_meta['feedback']:
                    followers.append(follower_id['owner_id'])
        for el in followers:
            result = self.get('friends.add', **{'user_id': el})
            if result != 2:
                log.error('error in add to friend for user: %s' % el)
<commit_msg>fix time to show<commit_after>from datetime import datetime
import json
import urlparse
from requests import Session
from lxml import html

from properties import *

__author__ = '4ikist'

log = logger.getChild("vk_api")

class APIException(Exception):
    pass

class VK_API():
    def __init__(self, login, pwd):
        self.session = Session()
        self.session.verify = certs_path
        self.base_url = 'https://api.vk.com/method/'
        self.login = login
        self.pwd = pwd
        self.__get_access_token(login, pwd)

    def __form_lp_server_address(self, ts=None):
        if ts is None:
            self.lp_server_params = self.get('messages.getLongPollServer')

        self.lp_server_connection = 'http://%s?act=a_check&key=%s&ts=%s&wait=25&mode=0' % (
            self.lp_server_params.get('server'),
            self.lp_server_params.get('key'),
            self.lp_server_params.get('ts') if ts is None else ts)

    def __get_access_token(self, login, pwd):
        if (not hasattr(self, 'access_expires') and not hasattr(self, 'last_auth')) or (
                    self.last_auth - datetime.now()).total_seconds() > self.access_expires:
            auth = self.__auth__(login, pwd)
            self.access_token = auth['access_token']
            self.user_id = auth['user_id']
            self.access_expires = auth['expires_in']
            self.last_auth = datetime.now()

        return self.access_token

    def __auth__(self, vk_login, vk_pass):
        log.info('auth...')
        result = self.session.get('https://oauth.vk.com/authorize', params=vk_access_credentials)
        doc = html.document_fromstring(result.content)
        inputs = doc.xpath('//input')
        form_params = {}
        for el in inputs:
            form_params[el.attrib.get('name')] = el.value
        form_params['email'] = vk_login
        form_params['pass'] = vk_pass
        form_url = doc.xpath('//form')[0].attrib.get('action')
        # process second page
        result = self.session.post(form_url, form_params)
        doc = html.document_fromstring(result.content)
        # if already login
        if 'OAuth Blank' not in doc.xpath('//title')[0].text:
            submit_url = doc.xpath('//form')[0].attrib.get('action')
            result = self.session.post(submit_url, cookies=result.cookies)

        # retrieving access token from url
        parsed_url = urlparse.urlparse(result.url)
        if 'error' in parsed_url.query:
            log.error('error in authenticate \n%s' % parsed_url.query)
            raise APIException(dict([el.split('=') for el in parsed_url.query.split('&')]))

        fragment = parsed_url.fragment
        access_token = dict([el.split('=') for el in fragment.split('&')])
        log.info('auth was successful')
        return access_token

    def get(self, method_name, **kwargs):
        params = dict({'access_token': self.__get_access_token(self.login, self.pwd)}, **kwargs)
        result = self.session.get('%s%s' % (self.base_url, method_name), params=params)
        result_object = json.loads(result.content)
        if 'error' in result_object:
            if result_object['error']['error_code'] == 14:
                kwargs['captcha_key'] = raw_input(
                    "please input what you see at [%s]" % result_object['error']['captcha_img'])
                kwargs['captcha_sid'] = result_object['error']['captcha_sid']
                return self.get(method_name, **kwargs)
            raise APIException(result_object)
        return result_object['response']

    def mark_as_read(self, messages):
        messages_str = ','.join([str(m) for m in messages])
        result = self.get('messages.markAsRead', **{'message_ids': messages_str})
        if result != 1:
            log.error('can not mark as read messages %s' % messages_str)

    def get_messages(self):
        self.__form_lp_server_address()
        log.info('will retrieve messages')
        while True:
            result = self.session.get(self.lp_server_connection)
            result = json.loads(result.content)
            if result.get('failed') == 2:
                self.__form_lp_server_address()
            else:
                self.__form_lp_server_address(ts=result.get('ts'))
            read_messages = []
            for update in result['updates']:
                if update[0] == 4 and update[2] in (1, 17, 33, 49) and update[2] != 3:
                    message_id = update[1]
                    from_id = update[3]
                    text = update[-1]
                    tstamp = update[4]
                    read_messages.append(message_id)
                    log.info('timestamp inboxed message: %s' % datetime.fromtimestamp(tstamp))
                    yield {'from': from_id, 'text': text.lower(), 'timestamp': tstamp}
                elif update[0] == 4 and update[2] in (3, 19, 51):
                    log.info('timestamp outboxed message: %s' % datetime.fromtimestamp(update[4]))

            if read_messages:
                self.mark_as_read(read_messages)

    def send_message(self, user_id, text):
        params = {'user_id': user_id, 'message': text}
        result = self.get('messages.send', **params)
        return result

    def add_followers_to_friends(self):
        params = {'count': 100, 'filters': 'followers'}
        result = self.get('notifications.get', **params)
        followers = []
        for follower_meta in result['items'][1:]:
            if follower_meta['type'] == 'follow':
                for follower_id in follower_meta['feedback']:
                    followers.append(follower_id['owner_id'])
        for el in followers:
            result = self.get('friends.add', **{'user_id': el})
            if result != 2:
                log.error('error in add to friend for user: %s' % el)
<|endoftext|>


<commit_before>__author__ = 'chris'

import ast
import json
import os
import time
from constants import DATA_FOLDER, SEED
from market.profile import Profile
from keyutils.keys import KeyChain
from random import shuffle
from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol
from protos.countries import CountryCode
from protos.objects import Plaintext_Message, Value, Listings
from protos import objects
from binascii import unhexlify
from dht.node import Node

class WSProtocol(WebSocketServerProtocol):
    """
    Handles new incoming requests coming from a websocket.
    """

    def onOpen(self):
        self.factory.register(self)

    def get_vendors(self, message_id):
        if message_id in self.factory.outstanding_vendors:
            queried = self.factory.outstanding_vendors[message_id]
        else:
            queried = []
            self.factory.outstanding_vendors = {}
            self.factory.outstanding_vendors[message_id] = queried

        vendors = self.factory.db.VendorStore().get_vendors()

        shuffle(vendors)
        to_query = []
        for vendor in vendors:
            if vendor.id not in queried:
                to_query.append(vendor)

        def handle_response(metadata, node):
            to_query.remove(node)
            if len(to_query) == 0:
                self.factory.mserver.querySeed(SEED)
            if metadata is not None:
                vendor = {
                    "id": message_id,
                    "vendor":
                        {
                            "guid": node.id.encode("hex"),
                            "name": metadata.name,
                            "short_description": metadata.short_description,
                            "handle": metadata.handle,
                            "avatar_hash": metadata.avatar_hash.encode("hex"),
                            "nsfw": metadata.nsfw
                        }
                }
                self.sendMessage(json.dumps(vendor, indent=4), False)
                queried.append(node.id)
                return True
            else:
                self.factory.db.VendorStore().delete_vendor(node.id.encode("hex"))
                return False

        for node in to_query[:30]:
            self.factory.mserver.get_user_metadata(node).addCallback(handle_response, node)

    def get_moderators(self, message_id):
        m = self.factory.db.ModeratorStore()

        def parse_response(moderators):
            if moderators is not None:
                m.clear_all()

                def parse_profile(profile, node):
                    if profile is not None:
                        m.save_moderator(node.id.encode("hex"), node.signed_pubkey,
                                         profile.encryption_key.public_key,
                                         profile.encryption_key.signature, profile.bitcoin_key.public_key,
                                         profile.bitcoin_key.signature, profile.name, profile.avatar_hash,
                                         profile.moderation_fee, profile.handle, profile.short_description)
                        moderator = {
                            "id": message_id,
                            "moderator":
                                {
                                    "guid": node.id.encode("hex"),
                                    "name": profile.name,
                                    "handle": profile.handle,
                                    "short_description": profile.short_description,
                                    "avatar_hash": profile.avatar_hash.encode("hex"),
                                    "about": profile.about,
                                    "fee": profile.moderation_fee
                                }
                        }
                        self.sendMessage(json.dumps(moderator, indent=4), False)
                    else:
                        m.delete_moderator(node.id)
                for mod in moderators:
                    try:
                        val = objects.Value()
                        val.ParseFromString(mod)
                        n = objects.Node()
                        n.ParseFromString(val.serializedData)
                        node_to_ask = Node(n.guid, n.ip, n.port, n.signedPublicKey)
                        if n.guid == KeyChain(self.factory.db).guid:
                            parse_profile(Profile(self.factory.db).get(), node_to_ask)
                        else:
                            self.factory.mserver.get_profile(node_to_ask)\
                                .addCallback(parse_profile, node_to_ask)
                    except Exception:
                        pass
        self.factory.kserver.get("moderators").addCallback(parse_response)

    def get_homepage_listings(self, message_id):
        if message_id not in self.factory.outstanding_listings:
            self.factory.outstanding_listings = {}
            self.factory.outstanding_listings[message_id] = []
        vendors = self.factory.db.VendorStore().get_vendors()

        if len(vendors) == 0:
            self.factory.mserver.querySeed(SEED)
            vendors = self.factory.db.VendorStore().get_vendors()

        shuffle(vendors)

        def handle_response(listings, node):
            count = 0
            if listings is not None:
                for l in listings.listing:
                    if l.contract_hash not in self.factory.outstanding_listings[message_id]:
                        listing_json = {
                            "id": message_id,
                            "listing":
                                {
                                    "guid": node.id.encode("hex"),
                                    "handle": listings.handle,
                                    "avatar_hash": listings.avatar_hash.encode("hex"),
                                    "title": l.title,
                                    "contract_hash": l.contract_hash.encode("hex"),
                                    "thumbnail_hash": l.thumbnail_hash.encode("hex"),
                                    "category": l.category,
                                    "price": l.price,
                                    "currency_code": l.currency_code,
                                    "nsfw": l.nsfw,
                                    "origin": str(CountryCode.Name(l.origin)),
                                    "ships_to": []
                                }
                        }
                        for country in l.ships_to:
                            listing_json["listing"]["ships_to"].append(str(CountryCode.Name(country)))
                        if not os.path.isfile(DATA_FOLDER + 'cache/' + l.thumbnail_hash.encode("hex")):
                            self.factory.mserver.get_image(node, l.thumbnail_hash)
                        if not os.path.isfile(DATA_FOLDER + 'cache/' + listings.avatar_hash.encode("hex")):
                            self.factory.mserver.get_image(node, listings.avatar_hash)
                        self.sendMessage(json.dumps(listing_json, indent=4), False)
                        count += 1
                        self.factory.outstanding_listings[message_id].append(l.contract_hash)
                        if count == 3:
                            break
                vendors.remove(node)
            else:
                self.factory.db.VendorStore().delete_vendor(node.id.encode("hex"))
                vendors.remove(node)

        for vendor in vendors[:15]:
            self.factory.mserver.get_listings(vendor).addCallback(handle_response, vendor)

    def send_message(self, guid, handle, message, subject, message_type, recipient_encryption_key):
        self.factory.db.MessageStore().save_message(guid, handle, "", unhexlify(recipient_encryption_key), subject,
                                                    message_type.upper(), message, time.time(), "", "", True)

        def send(node_to_send):
            n = node_to_send if node_to_send is not None else Node(unhexlify(guid), "123.4.5.6", 1234)
            self.factory.mserver.send_message(n, recipient_encryption_key,
                                              Plaintext_Message.Type.Value(message_type.upper()),
                                              message, subject)
        self.factory.kserver.resolve(unhexlify(guid)).addCallback(send)

    def search(self, message_id, keyword):
        def respond(l, node):
            if l is not None:
                listing_json = {
                    "id": message_id,
                    "listing":
                        {
                            "guid": node.id.encode("hex"),
                            "title": l.title,
                            "contract_hash": l.contract_hash.encode("hex"),
                            "thumbnail_hash": l.thumbnail_hash.encode("hex"),
                            "category": l.category,
                            "price": l.price,
                            "currency_code": l.currency_code,
                            "nsfw": l.nsfw,
                            "origin": str(CountryCode.Name(l.origin)),
                            "ships_to": [],
                            "avatar_hash": l.avatar_hash,
                            "handle": l.handle
                        }
                }
                for country in l.ships_to:
                    listing_json["listing"]["ships_to"].append(str(CountryCode.Name(country)))
                self.sendMessage(json.dumps(listing_json, indent=4), False)

        def parse_results(values):
            if values is not None:
                for v in values:
                    try:
                        val = Value()
                        val.ParseFromString(v)
                        n = objects.Node()
                        n.ParseFromString(val.serializedData)
                        node_to_ask = Node(n.guid, n.ip, n.port, n.signedPublicKey, True)
                        if n.guid == KeyChain(self.factory.db).guid:
                            proto = self.factory.db.ListingsStore().get_proto()
                            l = Listings()
                            l.ParseFromString(proto)
                            for listing in l.listing:
                                if listing.contract_hash == val.valueKey:
                                    respond(listing, node_to_ask)
                        else:
                            self.factory.mserver.get_contract_metadata(node_to_ask, val.valueKey)\
                                .addCallback(respond, node_to_ask)
                    except Exception:
                        pass
        self.factory.kserver.get(keyword.lower()).addCallback(parse_results)

    def onMessage(self, payload, isBinary):
        try:
            request_json = json.loads(payload)
            if isinstance(request_json, unicode):
                payload = ast.literal_eval(payload)
                request_json = json.loads(payload)

            message_id = str(request_json["request"]["id"])

            if request_json["request"]["command"] == "get_vendors":
                self.get_vendors(message_id)

            if request_json["request"]["command"] == "get_moderators":
                self.get_moderators(message_id)

            elif request_json["request"]["command"] == "get_homepage_listings":
                self.get_homepage_listings(message_id)

            elif request_json["request"]["command"] == "search":
                self.search(message_id, request_json["request"]["keyword"].lower())

            elif request_json["request"]["command"] == "send_message":
                self.send_message(request_json["request"]["guid"],
                                  request_json["request"]["handle"],
                                  request_json["request"]["message"],
                                  request_json["request"]["subject"],
                                  request_json["request"]["message_type"],
                                  request_json["request"]["recipient_key"])

        except Exception as e:
            print 'Exception occurred: %s' % e

    def connectionLost(self, reason):
        WebSocketServerProtocol.connectionLost(self, reason)
        self.factory.unregister(self)

class WSFactory(WebSocketServerFactory):

    """
    Simple broadcast server broadcasting any message it receives to all
    currently connected clients.
    """

    def __init__(self, url, mserver, kserver, only_ip="127.0.0.1", debug=False, debugCodePaths=False):
        WebSocketServerFactory.__init__(self, url, debug=debug, debugCodePaths=debugCodePaths)
        self.mserver = mserver
        self.kserver = kserver
        self.db = mserver.db
        self.outstanding_listings = {}
        self.outstanding_vendors = {}
        self.clients = []
        self.only_ip = only_ip

    def register(self, client):
        if client.transport.getPeer().host != self.only_ip and self.only_ip != "0.0.0.0":
            client.transport.loseConnection()
        elif client not in self.clients:
            self.clients.append(client)

    def unregister(self, client):
        if client in self.clients:
            self.clients.remove(client)

    def push(self, msg):
        for c in self.clients:
            c.sendMessage(msg)
<commit_msg>Fix avatar hash encoding in search results<commit_after>__author__ = 'chris'

import ast
import json
import os
import time
from constants import DATA_FOLDER, SEED
from market.profile import Profile
from keyutils.keys import KeyChain
from random import shuffle
from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol
from protos.countries import CountryCode
from protos.objects import Plaintext_Message, Value, Listings
from protos import objects
from binascii import unhexlify
from dht.node import Node

class WSProtocol(WebSocketServerProtocol):
    """
    Handles new incoming requests coming from a websocket.
    """

    def onOpen(self):
        self.factory.register(self)

    def get_vendors(self, message_id):
        if message_id in self.factory.outstanding_vendors:
            queried = self.factory.outstanding_vendors[message_id]
        else:
            queried = []
            self.factory.outstanding_vendors = {}
            self.factory.outstanding_vendors[message_id] = queried

        vendors = self.factory.db.VendorStore().get_vendors()

        shuffle(vendors)
        to_query = []
        for vendor in vendors:
            if vendor.id not in queried:
                to_query.append(vendor)

        def handle_response(metadata, node):
            to_query.remove(node)
            if len(to_query) == 0:
                self.factory.mserver.querySeed(SEED)
            if metadata is not None:
                vendor = {
                    "id": message_id,
                    "vendor":
                        {
                            "guid": node.id.encode("hex"),
                            "name": metadata.name,
                            "short_description": metadata.short_description,
                            "handle": metadata.handle,
                            "avatar_hash": metadata.avatar_hash.encode("hex"),
                            "nsfw": metadata.nsfw
                        }
                }
                self.sendMessage(json.dumps(vendor, indent=4), False)
                queried.append(node.id)
                return True
            else:
                self.factory.db.VendorStore().delete_vendor(node.id.encode("hex"))
                return False

        for node in to_query[:30]:
            self.factory.mserver.get_user_metadata(node).addCallback(handle_response, node)

    def get_moderators(self, message_id):
        m = self.factory.db.ModeratorStore()

        def parse_response(moderators):
            if moderators is not None:
                m.clear_all()

                def parse_profile(profile, node):
                    if profile is not None:
                        m.save_moderator(node.id.encode("hex"), node.signed_pubkey,
                                         profile.encryption_key.public_key,
                                         profile.encryption_key.signature, profile.bitcoin_key.public_key,
                                         profile.bitcoin_key.signature, profile.name, profile.avatar_hash,
                                         profile.moderation_fee, profile.handle, profile.short_description)
                        moderator = {
                            "id": message_id,
                            "moderator":
                                {
                                    "guid": node.id.encode("hex"),
                                    "name": profile.name,
                                    "handle": profile.handle,
                                    "short_description": profile.short_description,
                                    "avatar_hash": profile.avatar_hash.encode("hex"),
                                    "about": profile.about,
                                    "fee": profile.moderation_fee
                                }
                        }
                        self.sendMessage(json.dumps(moderator, indent=4), False)
                    else:
                        m.delete_moderator(node.id)
                for mod in moderators:
                    try:
                        val = objects.Value()
                        val.ParseFromString(mod)
                        n = objects.Node()
                        n.ParseFromString(val.serializedData)
                        node_to_ask = Node(n.guid, n.ip, n.port, n.signedPublicKey)
                        if n.guid == KeyChain(self.factory.db).guid:
                            parse_profile(Profile(self.factory.db).get(), node_to_ask)
                        else:
                            self.factory.mserver.get_profile(node_to_ask)\
                                .addCallback(parse_profile, node_to_ask)
                    except Exception:
                        pass
        self.factory.kserver.get("moderators").addCallback(parse_response)

    def get_homepage_listings(self, message_id):
        if message_id not in self.factory.outstanding_listings:
            self.factory.outstanding_listings = {}
            self.factory.outstanding_listings[message_id] = []
        vendors = self.factory.db.VendorStore().get_vendors()

        if len(vendors) == 0:
            self.factory.mserver.querySeed(SEED)
            vendors = self.factory.db.VendorStore().get_vendors()

        shuffle(vendors)

        def handle_response(listings, node):
            count = 0
            if listings is not None:
                for l in listings.listing:
                    if l.contract_hash not in self.factory.outstanding_listings[message_id]:
                        listing_json = {
                            "id": message_id,
                            "listing":
                                {
                                    "guid": node.id.encode("hex"),
                                    "handle": listings.handle,
                                    "avatar_hash": listings.avatar_hash.encode("hex"),
                                    "title": l.title,
                                    "contract_hash": l.contract_hash.encode("hex"),
                                    "thumbnail_hash": l.thumbnail_hash.encode("hex"),
                                    "category": l.category,
                                    "price": l.price,
                                    "currency_code": l.currency_code,
                                    "nsfw": l.nsfw,
                                    "origin": str(CountryCode.Name(l.origin)),
                                    "ships_to": []
                                }
                        }
                        for country in l.ships_to:
                            listing_json["listing"]["ships_to"].append(str(CountryCode.Name(country)))
                        if not os.path.isfile(DATA_FOLDER + 'cache/' + l.thumbnail_hash.encode("hex")):
                            self.factory.mserver.get_image(node, l.thumbnail_hash)
                        if not os.path.isfile(DATA_FOLDER + 'cache/' + listings.avatar_hash.encode("hex")):
                            self.factory.mserver.get_image(node, listings.avatar_hash)
                        self.sendMessage(json.dumps(listing_json, indent=4), False)
                        count += 1
                        self.factory.outstanding_listings[message_id].append(l.contract_hash)
                        if count == 3:
                            break
                vendors.remove(node)
            else:
                self.factory.db.VendorStore().delete_vendor(node.id.encode("hex"))
                vendors.remove(node)

        for vendor in vendors[:15]:
            self.factory.mserver.get_listings(vendor).addCallback(handle_response, vendor)

    def send_message(self, guid, handle, message, subject, message_type, recipient_encryption_key):
        self.factory.db.MessageStore().save_message(guid, handle, "", unhexlify(recipient_encryption_key), subject,
                                                    message_type.upper(), message, time.time(), "", "", True)

        def send(node_to_send):
            n = node_to_send if node_to_send is not None else Node(unhexlify(guid), "123.4.5.6", 1234)
            self.factory.mserver.send_message(n, recipient_encryption_key,
                                              Plaintext_Message.Type.Value(message_type.upper()),
                                              message, subject)
        self.factory.kserver.resolve(unhexlify(guid)).addCallback(send)

    def search(self, message_id, keyword):
        def respond(l, node):
            if l is not None:
                listing_json = {
                    "id": message_id,
                    "listing":
                        {
                            "guid": node.id.encode("hex"),
                            "title": l.title,
                            "contract_hash": l.contract_hash.encode("hex"),
                            "thumbnail_hash": l.thumbnail_hash.encode("hex"),
                            "category": l.category,
                            "price": l.price,
                            "currency_code": l.currency_code,
                            "nsfw": l.nsfw,
                            "origin": str(CountryCode.Name(l.origin)),
                            "ships_to": [],
                            "avatar_hash": l.avatar_hash.encode("hex"),
                            "handle": l.handle
                        }
                }
                for country in l.ships_to:
                    listing_json["listing"]["ships_to"].append(str(CountryCode.Name(country)))
                self.sendMessage(json.dumps(listing_json, indent=4), False)

        def parse_results(values):
            if values is not None:
                for v in values:
                    try:
                        val = Value()
                        val.ParseFromString(v)
                        n = objects.Node()
                        n.ParseFromString(val.serializedData)
                        node_to_ask = Node(n.guid, n.ip, n.port, n.signedPublicKey, True)
                        if n.guid == KeyChain(self.factory.db).guid:
                            proto = self.factory.db.ListingsStore().get_proto()
                            l = Listings()
                            l.ParseFromString(proto)
                            for listing in l.listing:
                                if listing.contract_hash == val.valueKey:
                                    respond(listing, node_to_ask)
                        else:
                            self.factory.mserver.get_contract_metadata(node_to_ask, val.valueKey)\
                                .addCallback(respond, node_to_ask)
                    except Exception:
                        pass
        self.factory.kserver.get(keyword.lower()).addCallback(parse_results)

    def onMessage(self, payload, isBinary):
        try:
            request_json = json.loads(payload)
            if isinstance(request_json, unicode):
                payload = ast.literal_eval(payload)
                request_json = json.loads(payload)

            message_id = str(request_json["request"]["id"])

            if request_json["request"]["command"] == "get_vendors":
                self.get_vendors(message_id)

            if request_json["request"]["command"] == "get_moderators":
                self.get_moderators(message_id)

            elif request_json["request"]["command"] == "get_homepage_listings":
                self.get_homepage_listings(message_id)

            elif request_json["request"]["command"] == "search":
                self.search(message_id, request_json["request"]["keyword"].lower())

            elif request_json["request"]["command"] == "send_message":
                self.send_message(request_json["request"]["guid"],
                                  request_json["request"]["handle"],
                                  request_json["request"]["message"],
                                  request_json["request"]["subject"],
                                  request_json["request"]["message_type"],
                                  request_json["request"]["recipient_key"])

        except Exception as e:
            print 'Exception occurred: %s' % e

    def connectionLost(self, reason):
        WebSocketServerProtocol.connectionLost(self, reason)
        self.factory.unregister(self)

class WSFactory(WebSocketServerFactory):

    """
    Simple broadcast server broadcasting any message it receives to all
    currently connected clients.
    """

    def __init__(self, url, mserver, kserver, only_ip="127.0.0.1", debug=False, debugCodePaths=False):
        WebSocketServerFactory.__init__(self, url, debug=debug, debugCodePaths=debugCodePaths)
        self.mserver = mserver
        self.kserver = kserver
        self.db = mserver.db
        self.outstanding_listings = {}
        self.outstanding_vendors = {}
        self.clients = []
        self.only_ip = only_ip

    def register(self, client):
        if client.transport.getPeer().host != self.only_ip and self.only_ip != "0.0.0.0":
            client.transport.loseConnection()
        elif client not in self.clients:
            self.clients.append(client)

    def unregister(self, client):
        if client in self.clients:
            self.clients.remove(client)

    def push(self, msg):
        for c in self.clients:
            c.sendMessage(msg)
<|endoftext|>