예제 #1
0
파일: MainService.py 프로젝트: tsnik/SkyNet
 def __init__(self):
     SNPService.__init__(self)
     self.config = Config.getconf()
     self.name = self.config.name
     self.port = int(self.config.server.Port)
     self.ip = self.config.server.IP
     self.tg = TelegramService(self.config.token)
     self.cert_manager = CertManager("keys", "skynet", self.name)
예제 #2
0
class DeviceService(SNPService):

    def __init__(self, config):
        SNPService.__init__(self)
        self.config = config
        self.connecting_servers = {}
        self.cert_manager = CertManager("keys", "device_servers", self.config.name)

    def startService(self):
        def callb(res):
            for dev in res:
                fact = SNProtocolClientFactory(self)
                self.cert_manager.connect_to_server(dev[1], dev[2], fact)
        DB.get_device_servers().addCallback(callb)

    def type_fch(self, request, reqid, protocol):
        ip = protocol.transport.getPeer().host

        def callb(res):
            protocol.sendResponse(request, reqid)
            self.parent.field_updated(res, request["Field"])
        DB.get_local_devid_from_remote(ip, request["DevId"]).addCallback(callb)

    def type_aur(self, request, reqid, protocol):
        #  TODO: request to control server for device server pass
        pass

    def type_wel(self, request, reqid, protocol):
        def callb(res, ip):
            protocol.sendResponse({"Name": self.config.name}, reqid)
            if ip in self.connecting_servers:
                self.connecting_servers[ip].callback(res)
        ip = protocol.transport.getPeer().host
        port = protocol.transport.getPeer().port
        self.peers[ip] = protocol
        DB.update_devices(ip, port, request["Name"], request["Devices"]).addCallback(callb, ip)

    def get_device_fields(self, device_server, devid):
        return self.peers[device_server].sendRequest({"Type": "GDF", "DevId": devid})

    def update_device_field(self, device_server, devid, field, value):
        return self.peers[device_server].sendRequest({"Type": "UDF", "DevId": devid,
                                                      "Field": field, "Value": value})

    def add_server(self, ip, port, pin):
        fact = SNProtocolClientFactory(self)
        self.cert_manager.connect_to_server(ip, port, fact)
        d = defer.Deferred()
        self.connecting_servers[ip] = d
        return d

    def clientConnectionFailed(self, connector, reason):
        ip = connector.getDestination().host
        if ip in self.connecting_servers:
            self.connecting_servers[ip].errback(Exception(ip))
예제 #3
0
    def __init__(self, config):
        SNPService.__init__(self)
        self.factory = SNProtocolServerFactory(self)
        self.config = config
        self.port = int(config.port)
        self.iface = config.iface
        self.db = DB.get_db()
        self.cert_manager = CertManager("keys", "control_servers",
                                        self.config.name)

        sslcontext = ssl.DefaultOpenSSLContextFactory(
            'keys/' + self.config.name + '.key',
            'keys/' + self.config.name + '.crt')
        self.ControlServer = self.cert_manager.create_server(
            self.port, self.factory, self.iface)
예제 #4
0
파일: MainService.py 프로젝트: tsnik/SkyNet
 def __init__(self):
     SNPService.__init__(self)
     self.config = Config.getconf()
     self.port = int(self.config.port)
     self.iface = self.config.iface
     self.factory = SNProtocolServerFactory(self)
     self.cert_manager = CertManager("keys", "skynet", self.config.name)
     self.server = self.cert_manager.create_server(self.port, self.factory,
                                                   self.iface)
     self.devices = {}
     self.num = 0
     for device in self.config.devices:
         driver = getattr(
             __import__('drivers.{0}'.format(device.Driver),
                        fromlist=[device.Driver]), device.Driver)
         self.devices[self.num] = driver(self.num, device.Name,
                                         self.field_updated)
         self.num += 1
예제 #5
0
파일: MainService.py 프로젝트: tsnik/SkyNet
 def __init__(self):
     SNPService.__init__(self)
     self.config = Config.getconf()
     self.port = int(self.config.port)
     self.iface = self.config.iface
     self.factory = SNProtocolServerFactory(self)
     self.cert_manager = CertManager("keys", "skynet", self.config.name)
     self.server = self.cert_manager.create_server(self.port, self.factory, self.iface)
     self.devices = {}
     self.num = 0
     for device in self.config.devices:
         driver = getattr(__import__('drivers.{0}'.format(device.Driver), fromlist=[device.Driver]), device.Driver)
         self.devices[self.num] = driver(self.num, device.Name, self.field_updated)
         self.num += 1
예제 #6
0
 def __init__(self, config):
     SNPService.__init__(self)
     self.config = config
     self.connecting_servers = {}
     self.cert_manager = CertManager("keys", "device_servers",
                                     self.config.name)
예제 #7
0
class DeviceService(SNPService):
    def __init__(self, config):
        SNPService.__init__(self)
        self.config = config
        self.connecting_servers = {}
        self.cert_manager = CertManager("keys", "device_servers",
                                        self.config.name)

    def startService(self):
        def callb(res):
            for dev in res:
                fact = SNProtocolClientFactory(self)
                self.cert_manager.connect_to_server(dev[1], dev[2], fact)

        DB.get_device_servers().addCallback(callb)

    def type_fch(self, request, reqid, protocol):
        ip = protocol.transport.getPeer().host

        def callb(res):
            protocol.sendResponse(request, reqid)
            self.parent.field_updated(res, request["Field"])

        DB.get_local_devid_from_remote(ip, request["DevId"]).addCallback(callb)

    def type_aur(self, request, reqid, protocol):
        #  TODO: request to control server for device server pass
        pass

    def type_wel(self, request, reqid, protocol):
        def callb(res, ip):
            protocol.sendResponse({"Name": self.config.name}, reqid)
            if ip in self.connecting_servers:
                self.connecting_servers[ip].callback(res)

        ip = protocol.transport.getPeer().host
        port = protocol.transport.getPeer().port
        self.peers[ip] = protocol
        DB.update_devices(ip, port, request["Name"],
                          request["Devices"]).addCallback(callb, ip)

    def get_device_fields(self, device_server, devid):
        return self.peers[device_server].sendRequest({
            "Type": "GDF",
            "DevId": devid
        })

    def update_device_field(self, device_server, devid, field, value):
        return self.peers[device_server].sendRequest({
            "Type": "UDF",
            "DevId": devid,
            "Field": field,
            "Value": value
        })

    def add_server(self, ip, port, pin):
        fact = SNProtocolClientFactory(self)
        self.cert_manager.connect_to_server(ip, port, fact)
        d = defer.Deferred()
        self.connecting_servers[ip] = d
        return d

    def clientConnectionFailed(self, connector, reason):
        ip = connector.getDestination().host
        if ip in self.connecting_servers:
            self.connecting_servers[ip].errback(Exception(ip))
예제 #8
0
파일: MainService.py 프로젝트: tsnik/SkyNet
class MainService(SNPService):
    def __init__(self):
        SNPService.__init__(self)
        self.config = Config.getconf()
        self.port = int(self.config.port)
        self.iface = self.config.iface
        self.factory = SNProtocolServerFactory(self)
        self.cert_manager = CertManager("keys", "skynet", self.config.name)
        self.server = self.cert_manager.create_server(self.port, self.factory,
                                                      self.iface)
        self.devices = {}
        self.num = 0
        for device in self.config.devices:
            driver = getattr(
                __import__('drivers.{0}'.format(device.Driver),
                           fromlist=[device.Driver]), device.Driver)
            self.devices[self.num] = driver(self.num, device.Name,
                                            self.field_updated)
            self.num += 1

    def startService(self):
        from snp import create_self_signed_cert
        create_self_signed_cert("keys", self.config.name)
        self.server.startService()

    def field_updated(self, device, field_name, value):
        if len(list(self.peers.values())) > 0:
            list(self.peers.values())[0].sendRequest({
                "Type": "FCH",
                "DevId": device.did,
                "Field": {
                    "Name": field_name,
                    "Value": value
                }
            })

    def connectionMade(self, protocol):
        def callb(res):
            protocol.sendRequest({
                "Type": "WEL",
                "Name": self.config.name,
                "Devices": res
            })
            ip = protocol.transport.getPeer().host
            self.peers[ip] = protocol
            print("Welcome message sent")

        d = self.getDevices()
        d.addCallback(callb)

    def getDevices(self):
        def callb(res):
            devices = [{
                "Name": device.name,
                "DevId": device.did,
                "Fields": res[device.did]
            } for device in self.devices.values()]
            return devices

        d = defer.gatherResults([
            device.get_device_fields().addCallback(
                lambda res: [field.to_dict() for field in res.values()])
            for device in self.devices.values()
        ])
        d.addCallback(callb)
        return d

    def type_gdf(self, request, reqid, protocol):
        def callb(res):
            protocol.sendResponse(
                {
                    "Type": "GDF",
                    "Device": {
                        "Name": device.name,
                        "DevId": device.did,
                        "Fields": res
                    }
                }, reqid)

        device = self.devices[request["DevId"]]
        d = device.get_device_fields().addCallback(
            lambda res: [field.to_dict() for field in res.values()])
        d.addCallback(callb)

    def type_udf(self, request, reqid, protocol):
        def callb(res):
            protocol.sendResponse(
                {
                    "Type": "UDF",
                    "DevId": request["DevId"],
                    "Field": res.to_dict()
                }, reqid)

        device = self.devices[request["DevId"]]
        d = device.update_device_field(request["Field"], request["Value"])
        d.addCallback(callb)
예제 #9
0
파일: MainService.py 프로젝트: tsnik/SkyNet
class MainService(SNPService):
    def __init__(self):
        SNPService.__init__(self)
        self.config = Config.getconf()
        self.name = self.config.name
        self.port = int(self.config.server.Port)
        self.ip = self.config.server.IP
        self.tg = TelegramService(self.config.token)
        self.cert_manager = CertManager("keys", "skynet", self.name)

    def type_wel(self, request, reqid, protocol):
        protocol.sendResponse({"Type": "WEL", "Name": self.name, "Methods": []}, reqid)

    def type_cmt(self, request, reqid, protocol):
        pass

    def startService(self):
        from snp import create_self_signed_cert
        create_self_signed_cert("keys", self.config.name)
        fact = SNProtocolClientFactory(self)
        self.cert_manager.connect_to_server(self.ip, self.port, fact)

        self.tg.parent = self
        self.tg.startService()

    def connectionMade(self, protocol):
        ip = protocol.transport.getPeer().host
        self.peers[ip] = protocol

    def get_devices(self):
        def callb(res):
            ret = {}
            devices = res["Devices"]
            for device in devices:
                ret[int(device["ID"])] = device["Name"]
            return ret
        d = list(self.peers.values())[0].sendRequest({"Type": "GDL"})
        d.addCallback(callb)
        return d

    def get_device_info(self, id):
        def callb(res):
            return res["Device"]
        d = list(self.peers.values())[0].sendRequest({"Type": "GDF", "DevId": id})
        d.addCallback(callb)
        return d

    def get_scripts(self, password):
        def callb(res):
            scripts = {int(script["Id"]): Script.create_from_dict(script)for script in res["Scripts"]}
            return scripts
        d = list(self.peers.values())[0].sendRequest({"Type": "GSC", "Password": password})
        d.addCallback(callb)
        return d

    def remove_script(self, id, password):
        d = list(self.peers.values())[0].sendRequest({"Type": "DSC", "ScriptId": id, "Password": password})
        return d

    def update_field(self, dev_id, field, value):
        d = list(self.peers.values())[0].sendRequest({"Type": "UDF",
                                                      "DevId": dev_id, "Field": field, "Value": value})
        return d

    def create_script(self, script, password):
        d = list(self.peers.values())[0].sendRequest({"Type": "CSC", "Script": script.to_dict(), "Password": password})
        return d

    def get_servers(self, password):
        def callb(res):
            return res["Servers"]
        d = list(self.peers.values())[0].sendRequest({"Type": "GSD", "Password": password})
        d.addCallback(callb)
        return d

    def add_server(self, ip, port, pin, password):
        def callb(res):
            return res["Server"]
        d = list(self.peers.values())[0].sendRequest({"Type": "RSD", "Password": password,
                                                      "IP": ip, "Port": port, "Pin": pin})
        d.addCallback(callb)
        return d
예제 #10
0
 def __init__(self, config):
     SNPService.__init__(self)
     self.config = config
     self.connecting_servers = {}
     self.cert_manager = CertManager("keys", "device_servers", self.config.name)
예제 #11
0
파일: MainService.py 프로젝트: tsnik/SkyNet
class MainService(SNPService):
    def __init__(self):
        SNPService.__init__(self)
        self.config = Config.getconf()
        self.port = int(self.config.port)
        self.iface = self.config.iface
        self.factory = SNProtocolServerFactory(self)
        self.cert_manager = CertManager("keys", "skynet", self.config.name)
        self.server = self.cert_manager.create_server(self.port, self.factory, self.iface)
        self.devices = {}
        self.num = 0
        for device in self.config.devices:
            driver = getattr(__import__('drivers.{0}'.format(device.Driver), fromlist=[device.Driver]), device.Driver)
            self.devices[self.num] = driver(self.num, device.Name, self.field_updated)
            self.num += 1

    def startService(self):
        from snp import create_self_signed_cert
        create_self_signed_cert("keys", self.config.name)
        self.server.startService()

    def field_updated(self, device, field_name, value):
        if len(list(self.peers.values())) > 0:
            list(self.peers.values())[0].sendRequest({"Type": "FCH", "DevId": device.did,
                                                      "Field": {"Name": field_name, "Value": value}})

    def connectionMade(self, protocol):
        def callb(res):
            protocol.sendRequest({"Type": "WEL", "Name": self.config.name, "Devices": res})
            ip = protocol.transport.getPeer().host
            self.peers[ip] = protocol
            print("Welcome message sent")

        d = self.getDevices()
        d.addCallback(callb)

    def getDevices(self):
        def callb(res):
            devices = [{"Name": device.name, "DevId": device.did, "Fields": res[device.did]}
                       for device in self.devices.values()]
            return devices

        d = defer.gatherResults([device.get_device_fields().addCallback(
            lambda res: [field.to_dict() for field in res.values()]) for device in self.devices.values()])
        d.addCallback(callb)
        return d

    def type_gdf(self, request, reqid, protocol):
        def callb(res):
            protocol.sendResponse({"Type": "GDF", "Device": {"Name": device.name, "DevId": device.did, "Fields": res}},
                                  reqid)

        device = self.devices[request["DevId"]]
        d = device.get_device_fields().addCallback(lambda res: [field.to_dict() for field in res.values()])
        d.addCallback(callb)

    def type_udf(self, request, reqid, protocol):
        def callb(res):
            protocol.sendResponse({"Type": "UDF", "DevId": request["DevId"], "Field": res.to_dict()}, reqid)

        device = self.devices[request["DevId"]]
        d = device.update_device_field(request["Field"], request["Value"])
        d.addCallback(callb)
예제 #12
0
class ControlService(SNPService):
    def __init__(self, config):
        SNPService.__init__(self)
        self.factory = SNProtocolServerFactory(self)
        self.config = config
        self.port = int(config.port)
        self.iface = config.iface
        self.db = DB.get_db()
        self.cert_manager = CertManager("keys", "control_servers",
                                        self.config.name)

        sslcontext = ssl.DefaultOpenSSLContextFactory(
            'keys/' + self.config.name + '.key',
            'keys/' + self.config.name + '.crt')
        self.ControlServer = self.cert_manager.create_server(
            self.port, self.factory, self.iface)

    def connectionMade(self, protocol):
        ip = protocol.transport.getPeer().host

        def callb(res):
            DB.update_methods(ip, res["Name"], res["Methods"])
            self.peers[ip] = protocol

        #  TODO: Request to BD
        protocol.sendRequest({
            "Type": "WEL",
            "Name": self.config.name
        }).addCallback(callb)

    def startService(self):
        self.ControlServer.startService()

    def stopService(self):
        self.ControlServer.stopService()

    def type_gdl(self, request, reqid, protocol):
        def callb(res):
            response = {"Devices": res}
            protocol.sendResponse(response, reqid)

        DB.get_devices().addCallback(callb)

    def type_gdf(self, request, reqid, protocol):
        def callb(res):
            protocol.sendResponse({"Type": "GDF", "Device": res}, reqid)

        d = self.parent.get_device_data(request["DevId"])
        d.addCallback(callb)

    def type_udf(self, request, reqid, protocol):
        def callb(res):
            protocol.sendResponse(res, reqid)

        self.parent.update_device_field(request["DevId"], request["Field"],
                                        request["Value"]).addCallback(callb)

    def check_pass(self, request, protocol):
        if "Password" in request:
            if request["Password"] == self.config.adminpass:
                return True
        protocol.sendError(400, request)
        return False

    def type_ssd(self, request, reqid, protocol):
        if self.check_pass(request, protocol):
            pass

    def type_rsd(self, request, reqid, protocol):
        if self.check_pass(request, protocol):

            def callb(res):
                protocol.sendResponse({"Type": "RSD", "Server": res}, reqid)

            def errb(err):
                protocol.sendError(404, request)

            d = self.parent.add_server(request["IP"], request["Port"],
                                       request["Pin"])
            d.addCallbacks(callb, errb)

    def type_rnd(self, request, reqid, protocol):
        if self.check_pass(request, protocol):
            pass

    def type_gmt(self, request, reqid, protocol):
        if self.check_pass(request, protocol):

            def callb(res):
                protocol.sendResponse({"Type": "GMT", "Methods": res}, reqid)

            d = self.parent.get_methods()
            d.addCallback(callb)

    def type_gsc(self, request, reqid, protocol):
        if self.check_pass(request, protocol):

            def callb(res):
                jscripts = [script.to_dict() for script in res]
                protocol.sendResponse({
                    "Type": "GSC",
                    "Scripts": jscripts
                }, reqid)

            d = self.parent.get_scripts()
            d.addCallback(callb)

    def type_csc(self, request, reqid, protocol):
        if self.check_pass(request, protocol):

            def callb(res):
                protocol.sendResponse({
                    "Type": "CSC",
                    "Script": res.to_dict()
                }, reqid)

            script = Script.create_from_dict(request["Script"])
            d = self.parent.create_script(script)
            d.addCallback(callb)

    def type_esc(self, request, reqid, protocol):
        if self.check_pass(request, protocol):

            def callb(res):
                protocol.sendResponse({
                    "Type": "ESC",
                    "Script": res.to_dict()
                }, reqid)

            script = Script.create_from_dict(request["Script"])
            d = self.parent.edit_script(script)
            d.addCallback(callb)

    def type_dsc(self, request, reqid, protocol):
        if self.check_pass(request, protocol):

            def callb(res):
                protocol.sendResponse({"Type": "DSC", "ScriptId": res}, reqid)

            d = self.parent.delete_script(request["ScriptId"])
            d.addCallback(callb)

    def type_gsd(self, request, reqid, protocol):
        if self.check_pass(request, protocol):

            def callb(res):
                protocol.sendResponse({"Type": "GSD", "Servers": res}, reqid)

            d = self.parent.get_servers()
            d.addCallback(callb)

    def callMethod(self, control_server, name, **args):
        method = {"Name": name, "Fields": args}
        return self.peers[control_server].sendRequest({
            "Type": "CMT",
            "Method": method
        })