Exemplo n.º 1
0
    def load_config(self):

        root_folder_path = os.path.dirname(os.path.realpath(__file__))
        default_config_path = root_folder_path + '/default-config.hjson'
        user_config_path = root_folder_path + '/user-config.hjson'

        self.config = ConfigLoader()
        self.config.load(default_config_path=default_config_path, user_config_path=user_config_path)
Exemplo n.º 2
0
def test_default_config(gateway):

    gateway_root_path = os.path.dirname(
        os.path.realpath(__file__)) + '/../../magic/gateway'

    config = ConfigLoader()
    config.load(default_config_path=gateway_root_path +
                '/default-config.hjson',
                user_config_path=gateway_root_path + '/user-config.hjson')

    assert gateway.config == config
Exemplo n.º 3
0
def test_default_config(payment_enabler):

    payment_root_path = os.path.dirname(
        os.path.realpath(__file__)) + '/../../magic/payment'

    config = ConfigLoader()
    config.load(default_config_path=payment_root_path +
                '/default-config.hjson',
                user_config_path=payment_root_path + '/user-config.hjson')

    assert payment_enabler.config == config
Exemplo n.º 4
0
 def __init__(self):
     self.logger = logging.getLogger('MagicGateway')
     self.config = ConfigLoader()
     self.config.load()
     self.loop = asyncio.get_event_loop()
     self.radius_daemon = RadiusDaemon(self)
     self.flask_daemon = FlaskDaemon(self)
     self.payment_type = PaymentTypeFactory.createPaymentType(self.config)
     self.radius_requester = RadiusReq(self.config)
     self.web3_provider = Web3.HTTPProvider("https://rinkeby.infura.io/%s" % self.config['admin']['infura_api_key'])
     self.web3 = Web3(self.web3_provider)
     self.load_eth_contracts()
     self.users = {}
Exemplo n.º 5
0
class MagicPayment():
    def __init__(self):
        self.load_config()
        self.logger = logging.getLogger('MagicPayment')
        self.loop = asyncio.get_event_loop()
        self.flask_daemon = FlaskDaemon(self)
        self.web3_provider = Web3.HTTPProvider(
            "https://rinkeby.infura.io/%s" %
            self.config['admin']['infura_api_key'])
        self.web3 = Web3(self.web3_provider)
        self.load_eth_contracts()

    def load_config(self):

        root_folder_path = os.path.dirname(os.path.realpath(__file__))
        default_config_path = root_folder_path + '/default-config.hjson'
        user_config_path = root_folder_path + '/user-config.hjson'

        self.config = ConfigLoader()
        self.config.load(default_config_path=default_config_path,
                         user_config_path=user_config_path)

    def load_eth_contracts(self):

        self.mgc_contract_address = parse_address(
            self.config['dev']['mgc_address'])
        self.address = parse_address(
            self.config['admin']['eth_address'].lower())

        root_folder_path = os.path.dirname(os.path.realpath(__file__)) + "/.."
        mgc_abi_file = root_folder_path + '/abi/MagicToken.json'

        with open(mgc_abi_file) as f:
            info_json = json.load(f)

        self.MgcTokenContract = self.web3.eth.contract(
            address=self.mgc_contract_address, abi=info_json["abi"])

    def sighandler(self, signum, frame):
        """
        Handle signals so that the program shuts down appropriately
        :param signum: the signal to handle
        :param frame: unused for this implementation
        """
        if (signum == signal.SIGTERM):
            self.logger.warning('Got SIGTERM. Shutting down.')
            self.shutdown = True
        elif (signum == signal.SIGINT):
            self.logger.warning('Got SIGINT. Shutting down.')
            self.shutdown = True
        else:
            self.logger.warning('Signal %d not handled', signum)

    def run(self):

        self.logger.warning("Magic Payment Enabler started.")

        self.flask_daemon.daemon = True
        self.flask_daemon.start()

        try:
            self.loop.run_until_complete(self.heartbeat())
        except KeyboardInterrupt:
            self.logger.warning('Shutting down')

    async def heartbeat(self):
        # TODO: make this work in batches using asyncio.gather
        await asyncio.sleep(1)
        await self.heartbeat()
Exemplo n.º 6
0
class MagicGateway():

    def __init__(self):
        self.load_config()
        self.logger = logging.getLogger('MagicGateway')
        self.loop = asyncio.get_event_loop()
        self.radius_daemon = RadiusDaemon(self)
        self.flask_daemon = FlaskDaemon(self)
        self.payment_type = PaymentTypeFactory.createPaymentType(self.config)
        self.radius_requester = RadiusReq(self.config)
        self.web3_provider = Web3.HTTPProvider("https://rinkeby.infura.io/%s" % self.config['admin']['infura_api_key'])
        self.web3 = Web3(self.web3_provider)
        self.load_eth_contracts()
        self.users = {}

    def load_config(self):

        root_folder_path = os.path.dirname(os.path.realpath(__file__))
        default_config_path = root_folder_path + '/default-config.hjson'
        user_config_path = root_folder_path + '/user-config.hjson'

        self.config = ConfigLoader()
        self.config.load(default_config_path=default_config_path, user_config_path=user_config_path)


    def load_eth_contracts(self):

        self.mgc_contract_address = parse_address(self.config['dev']['mgc_address'])
        self.address = parse_address(self.config['admin']['eth_address'].lower())

        root_folder_path = os.path.dirname(os.path.realpath(__file__)) + "/.."
        mgc_abi_file = root_folder_path + '/abi/MagicToken.json'

        with open(mgc_abi_file) as f:
            info_json = json.load(f)

        self.MgcTokenContract = self.web3.eth.contract(
            address=self.mgc_contract_address,
            abi=info_json["abi"]
        )

    def sighandler(self, signum, frame):
        """
        Handle signals so that the program shuts down appropriately
        :param signum: the signal to handle
        :param frame: unused for this implementation
        """
        if (signum == signal.SIGTERM):
            self.logger.warning('Got SIGTERM. Shutting down.')
            self.shutdown = True
        elif (signum == signal.SIGINT):
            self.logger.warning('Got SIGINT. Shutting down.')
            self.shutdown = True
        else:
            self.logger.warning('Signal %d not handled', signum)

    def run(self):

        # may use these in the future if we want to go to a non-blocking socket
        # signal.signal(signal.SIGTERM, self.sighandler)
        # signal.signal(signal.SIGINT, self.sighandler)

        self.logger.warning("Magic App Service started using eth address %s" % self.config['admin']['eth_address'])

        self.radius_daemon.daemon = True
        self.radius_daemon.start()
        self.flask_daemon.daemon = True
        self.flask_daemon.start()

        try:
            self.loop.run_until_complete(self.heartbeat())
        except KeyboardInterrupt:
            self.logger.warning('Shutting down')

    async def heartbeat(self):

        # TODO: make this work in batches using asyncio.gather
        for key in self.users:
            await self.users[key].on_heartbeat()

        await asyncio.sleep(1)
        await self.heartbeat()

    async def on_user_auth(self, auth_object):
        """
        Called from the magicradiusd daemon thread upon a successful verification of user's identity.
        :param auth_object: the authorization object for this user.
        """

        if auth_object.address not in self.users.keys():
            self.users[auth_object.address] = \
                User(self, auth_object, auth_object.address, auth_object.sessionId)
            return await self.users[auth_object.address].on_auth(True)
        else:
            user = self.users[auth_object.address]
            user.radiusSessionId = auth_object.sessionId
            return await user.on_auth()

    async def on_keepalive(self, address, signed_message):
        """
        Called from the flask server when a user reports in with a GET request to /keepalive
        :param address: address name
        :param signed_message: message indicating session details sent from client
        """
        try:
            user = self.users[address]
            user.on_keepalive(address, signed_message)
        except KeyError:
            self.logger.warning("Unknown user attempting keepalive. (%s)" % address)