示例#1
0
文件: views.py 项目: visionp/memority
async def view_user_info(request: web.Request):
    attr = request.match_info.get('attr')
    if attr == 'balance':
        if settings.address:
            res = token_contract.get_mmr_balance()
        else:
            res = None
    elif attr == 'role':
        client, host = None, None
        if memo_db_contract.get_host_ip(settings.address):
            host = True
        if settings.client_contract_address:
            client = True
        if client and host:
            res = 'both'
        elif client:
            res = 'client'
        elif host:
            res = 'host'
        else:
            res = None
    else:
        return web.json_response({"status": "error"})
    return web.json_response({
        "status": "success",
        "data": {
            attr: res
        }
    })
示例#2
0
async def check_ip():
    """
    Check if IP is equal to IP in contract and update if not
    :return:  info message [str]
    """
    ip_from_contract = memo_db_contract.get_host_ip(settings.address)
    if not ip_from_contract:
        return 'Not in hoster list.'

    ok, err = check_if_accessible(*ip_from_contract.split(':'))
    if ok:
        return f'Your IP {ip_from_contract} is OK.'

    logger.warning(
        f'Your computer is not accessible by IP from contract! {err}')
    my_ip = await get_ip()
    ok, err = check_if_accessible(my_ip, settings.hoster_app_port)
    if not ok:
        logger.warning(f'Your computer is not accessible by IP! {err}')
        return f'Your computer is not accessible by IP! {err}'

    if ip_from_contract != my_ip:
        logger.warning(f'IP addresses are not equal. Replacing in contract... '
                       f'| IP from contract: {ip_from_contract} '
                       f'| My IP: {my_ip}')
        await memo_db_contract.add_or_update_host(ip=my_ip,
                                                  address=settings.address)
        return f'Replaced {ip_from_contract} with {my_ip}'
示例#3
0
文件: views.py 项目: visionp/memority
async def view_config(request: web.Request, *args, **kwargs):
    name = request.match_info.get('name')
    if name:
        if name in ['private_key', 'encryption_key']:
            return web.json_response({"status": "error", "details": "forbidden"})
        if name == 'host_ip':
            res = memo_db_contract.get_host_ip(settings.address)
        else:
            res = settings.__getattr__(name)
        if res:
            return web.json_response({
                "status": "success",
                "data": {
                    name: res
                }
            })
        else:
            return web.json_response({"status": "error", "details": "not_found"})

    logger.info('View config')
    config = vars(settings)
    with contextlib.suppress(KeyError):
        config['data'].pop('private_key')
    with contextlib.suppress(KeyError):
        config['data'].pop('encryption_key')
    return web.json_response({
        "status": "success",
        "details": "config",
        "data": {
            "config": config
        }
    })
示例#4
0
 def refresh_from_contract(cls):
     hosts = memo_db_contract.get_hosts()
     for host_address in hosts:
         with contextlib.suppress(IntegrityError):
             cls.update_or_create(
                 ip=memo_db_contract.get_host_ip(host_address),
                 address=host_address)
示例#5
0
async def get_miner_ip():
    ip_from_contract = memo_db_contract.get_host_ip(settings.address)
    if ip_from_contract:
        ip = ip_from_contract.split(':')[0]
    else:
        ip = await get_ip()

    return f'{ip}:{settings.mining_port}'
示例#6
0
 def get_or_create(cls, *, ip=None, address):
     try:
         instance = session.query(cls) \
             .filter(func.lower(cls.address) == func.lower(address)) \
             .one()
     except NoResultFound:
         if not ip:
             ip = memo_db_contract.get_host_ip(address)
         instance = cls(ip=ip, address=address)
         instance.save()
     return instance
示例#7
0
async def view_config(request: web.Request, *args, **kwargs):
    name = request.match_info.get('name')
    if name in ['private_key', 'encryption_key']:
        return web.json_response({"status": "error", "details": "forbidden"})
    if name == 'host_ip':
        res = memo_db_contract.get_host_ip(settings.address)
    elif name == 'space_used':
        res = file_size_human_readable(HosterFile.get_total_size())
    else:
        res = settings.__getattr__(name)
    if res:
        return web.json_response({"status": "success", "result": {name: res}})
    else:
        return web.json_response({"status": "error", "details": "not_found"})
示例#8
0
async def get_role():
    res = []
    if memo_db_contract.get_host_ip(settings.address):
        res.append('host')
    if settings.client_contract_address:
        res.append('renter')
    mining_status = {
        'active': 'miner',
        'pending': 'pending_miner',
        'sent': 'pending_miner'
    }.get(
        settings.mining_status
    )
    if mining_status:
        res.append(mining_status)
    return res
示例#9
0
async def get_ip():
    return memo_db_contract.get_host_ip(settings.address)