From 2f497ac0eeee7b72ce24f6342f30694f5bebb353 Mon Sep 17 00:00:00 2001 From: iWarpBTC Date: Tue, 21 Jun 2022 22:04:43 +0200 Subject: [PATCH] retreiving hits --- lnbits/extensions/boltcards/crud.py | 11 +++--- lnbits/extensions/boltcards/models.py | 9 ----- .../boltcards/templates/boltcards/index.html | 1 - lnbits/extensions/boltcards/views_api.py | 36 ++++++++++++++++--- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/lnbits/extensions/boltcards/crud.py b/lnbits/extensions/boltcards/crud.py index 62aad356..f34ce659 100644 --- a/lnbits/extensions/boltcards/crud.py +++ b/lnbits/extensions/boltcards/crud.py @@ -101,16 +101,13 @@ async def get_hit(hit_id: str) -> Optional[Hit]: return Hit.parse_obj(hit) -async def get_hits(wallet_ids: Union[str, List[str]]) -> List[Hit]: - - cards = get_cards(wallet_ids) - - q = ",".join(["?"] * len(cards)) +async def get_hits(cards_ids: Union[str, List[str]]) -> List[Hit]: + q = ",".join(["?"] * len(cards_ids)) rows = await db.fetchall( - f"SELECT * FROM boltcards.hits WHERE wallet IN ({q})", (*(card.card_id for card in cards),) + f"SELECT * FROM boltcards.hits WHERE card_id IN ({q})", (*cards_ids,) ) - return [Card(**row) for row in rows] + return [Hit(**row) for row in rows] async def create_hit( card_id, ip, useragent, old_ctr, new_ctr diff --git a/lnbits/extensions/boltcards/models.py b/lnbits/extensions/boltcards/models.py index 75621269..728aa2bb 100644 --- a/lnbits/extensions/boltcards/models.py +++ b/lnbits/extensions/boltcards/models.py @@ -28,12 +28,3 @@ class Hit(BaseModel): old_ctr: int new_ctr: int time: int - -''' -class CreateHitData(BaseModel): - card_id: str = Query(...) - ip: str = Query(...) - useragent: str = Query(...) - old_ctr: int = Query(...) - new_ctr: int = Query(...) -''' diff --git a/lnbits/extensions/boltcards/templates/boltcards/index.html b/lnbits/extensions/boltcards/templates/boltcards/index.html index a6997a5d..4910cb66 100644 --- a/lnbits/extensions/boltcards/templates/boltcards/index.html +++ b/lnbits/extensions/boltcards/templates/boltcards/index.html @@ -368,7 +368,6 @@ if (this.g.user.wallets.length) { this.getCards() this.getWithdraws() - this.getHits() } } }) diff --git a/lnbits/extensions/boltcards/views_api.py b/lnbits/extensions/boltcards/views_api.py index 75cfe129..8a8e33a2 100644 --- a/lnbits/extensions/boltcards/views_api.py +++ b/lnbits/extensions/boltcards/views_api.py @@ -24,6 +24,7 @@ from .crud import ( get_cards, get_card, create_card, + get_hits, update_card, delete_card, update_card_counter @@ -102,6 +103,22 @@ async def api_link_delete(card_id, wallet: WalletTypeInfo = Depends(require_admi await delete_card(card_id) raise HTTPException(status_code=HTTPStatus.NO_CONTENT) +@boltcards_ext.get("/api/v1/hits") +async def api_hits( + g: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False) +): + wallet_ids = [g.wallet.id] + + if all_wallets: + wallet_ids = (await get_user(g.wallet.user)).wallet_ids + + cards = await get_cards(wallet_ids) + cards_ids = [] + for card in cards: + cards_ids.append(card.id) + + return [hit.dict() for hit in await get_hits(cards_ids)] + @boltcards_ext.get("/api/v1/scan/") # pay.btcslovnik.cz/boltcards/api/v1/scan/?uid=00000000000000&ctr=000000&c=0000000000000000 async def api_scan( uid, ctr, c, @@ -124,8 +141,17 @@ async def api_scan( await update_card_counter(ctr_int, card.id) - link = await get_withdraw_link(card.withdraw, 0) + ip = request.client.host + if request.headers['x-real-ip']: + ip = request.headers['x-real-ip'] + elif request.headers['x-forwarded-for']: + ip = request.headers['x-forwarded-for'] + agent = request.headers['user-agent'] if 'user-agent' in request.headers else '' + + await create_hit(card.id, ip, agent, card.counter, ctr_int) + + link = await get_withdraw_link(card.withdraw, 0) return link.lnurl_response(request) @boltcards_ext.get("/api/v1/scane/") @@ -152,8 +178,8 @@ async def api_scane( print(getSunMAC(card_uid, counter, bytes.fromhex(card.file_key)).hex().upper()) return {"status": "ERROR", "reason": "CMAC does not check."} - counter_int = int.from_bytes(counter, "little") - if counter_int <= card.counter: + ctr_int = int.from_bytes(counter, "little") + if ctr_int <= card.counter: return {"status": "ERROR", "reason": "This link is already used."} await update_card_counter(counter_int, card.id) @@ -164,7 +190,9 @@ async def api_scane( elif request.headers['x-forwarded-for']: ip = request.headers['x-forwarded-for'] - await create_hit(card.id, ip, request.headers['user-agent'], card.counter, counter_int) + agent = request.headers['user-agent'] if 'user-agent' in request.headers else '' + + await create_hit(card.id, ip, agent, card.counter, ctr_int) link = await get_withdraw_link(card.withdraw, 0) return link.lnurl_response(request)