This commit is contained in:
benarc
2021-11-12 04:14:55 +00:00
parent 860709f48c
commit 267dea4f75
61 changed files with 23219 additions and 13297 deletions
+2 -5
View File
@@ -8,10 +8,8 @@ from lnbits.tasks import catch_everything_and_restart
db = Database("ext_subdomains")
subdomains_ext: APIRouter = APIRouter(
prefix="/subdomains",
tags=["subdomains"]
)
subdomains_ext: APIRouter = APIRouter(prefix="/subdomains", tags=["subdomains"])
def subdomains_renderer():
return template_renderer(["lnbits/extensions/subdomains/templates"])
@@ -25,4 +23,3 @@ from .views_api import * # noqa
def subdomains_start():
loop = asyncio.get_event_loop()
loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
+1 -5
View File
@@ -50,11 +50,7 @@ async def cloudflare_deletesubdomain(domain: Domains, domain_id: str):
}
async with httpx.AsyncClient() as client:
try:
r = await client.delete(
url + "/" + domain_id,
headers=header,
timeout=40,
)
r = await client.delete(url + "/" + domain_id, headers=header, timeout=40)
cf_response = r.text
except AssertionError:
cf_response = "Error occured"
+2 -8
View File
@@ -6,11 +6,7 @@ from . import db
from .models import CreateDomain, Domains, Subdomains
async def create_subdomain(
payment_hash,
wallet,
data: CreateDomain
) -> Subdomains:
async def create_subdomain(payment_hash, wallet, data: CreateDomain) -> Subdomains:
await db.execute(
"""
INSERT INTO subdomains.subdomain (id, domain, email, subdomain, ip, wallet, sats, duration, paid, record_type)
@@ -105,9 +101,7 @@ async def delete_subdomain(subdomain_id: str) -> None:
# Domains
async def create_domain(
data: CreateDomain
) -> Domains:
async def create_domain(data: CreateDomain) -> Domains:
domain_id = urlsafe_short_hash()
await db.execute(
"""
+3
View File
@@ -12,6 +12,7 @@ class CreateDomain(BaseModel):
cost: int = Query(..., ge=0)
allowed_record_types: str = Query(...)
class CreateSubdomain(BaseModel):
domain: str = Query(...)
subdomain: str = Query(...)
@@ -21,6 +22,7 @@ class CreateSubdomain(BaseModel):
duration: int = Query(...)
record_type: str = Query(...)
class Domains(BaseModel):
id: str
wallet: str
@@ -34,6 +36,7 @@ class Domains(BaseModel):
time: int
allowed_record_types: str
class Subdomains(BaseModel):
id: str
wallet: str
+1
View File
@@ -17,6 +17,7 @@ async def wait_for_paid_invoices():
payment = await invoice_queue.get()
await on_invoice_paid(payment)
# async def register_listeners():
# invoice_paid_chan_send, invoice_paid_chan_recv = trio.open_memory_channel(2)
# register_invoice_listener(invoice_paid_chan_send)
+9 -6
View File
@@ -14,9 +14,12 @@ from .crud import get_domain
templates = Jinja2Templates(directory="templates")
@subdomains_ext.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)):
return subdomains_renderer().TemplateResponse("subdomains/index.html", {"request": request, "user": user.dict()})
return subdomains_renderer().TemplateResponse(
"subdomains/index.html", {"request": request, "user": user.dict()}
)
@subdomains_ext.get("/{domain_id}")
@@ -24,20 +27,20 @@ async def display(request: Request, domain_id):
domain = await get_domain(domain_id)
if not domain:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Domain does not exist."
status_code=HTTPStatus.NOT_FOUND, detail="Domain does not exist."
)
allowed_records = (
domain.allowed_record_types.replace('"', "").replace(" ", "").split(",")
)
return subdomains_renderer().TemplateResponse(
"subdomains/display.html",{
"subdomains/display.html",
{
"request": request,
"domain_id": domain.id,
"domain_domain": domain.domain,
"domain_desc": domain.description,
"domain_cost": domain.cost,
"domain_allowed_record_types": allowed_records,
}
},
)
+35 -40
View File
@@ -28,7 +28,9 @@ from .crud import (
@subdomains_ext.get("/api/v1/domains")
async def api_domains(g: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
async def api_domains(
g: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
):
wallet_ids = [g.wallet.id]
if all_wallets:
@@ -39,19 +41,19 @@ async def api_domains(g: WalletTypeInfo = Depends(get_key_type), all_wallets: bo
@subdomains_ext.post("/api/v1/domains")
@subdomains_ext.put("/api/v1/domains/{domain_id}")
async def api_domain_create(data: CreateDomain, domain_id=None, g: WalletTypeInfo = Depends(get_key_type)):
async def api_domain_create(
data: CreateDomain, domain_id=None, g: WalletTypeInfo = Depends(get_key_type)
):
if domain_id:
domain = await get_domain(domain_id)
if not domain:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Domain does not exist."
status_code=HTTPStatus.NOT_FOUND, detail="Domain does not exist."
)
if domain.wallet != g.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Not your domain."
status_code=HTTPStatus.FORBIDDEN, detail="Not your domain."
)
domain = await update_domain(domain_id, **data.dict())
@@ -66,14 +68,10 @@ async def api_domain_delete(domain_id, g: WalletTypeInfo = Depends(get_key_type)
if not domain:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Domain does not exist."
)
status_code=HTTPStatus.NOT_FOUND, detail="Domain does not exist."
)
if domain.wallet != g.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Not your domain."
)
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your domain.")
await delete_domain(domain_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
@@ -83,7 +81,9 @@ async def api_domain_delete(domain_id, g: WalletTypeInfo = Depends(get_key_type)
@subdomains_ext.get("/api/v1/subdomains")
async def api_subdomains(all_wallets: bool = Query(False), g: WalletTypeInfo = Depends(get_key_type)):
async def api_subdomains(
all_wallets: bool = Query(False), g: WalletTypeInfo = Depends(get_key_type)
):
wallet_ids = [g.wallet.id]
if all_wallets:
@@ -99,22 +99,21 @@ async def api_subdomain_make_subdomain(domain_id, data: CreateSubdomain):
# If the request is coming for the non-existant domain
if not domain:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="LNsubdomain does not exist."
)
status_code=HTTPStatus.NOT_FOUND, detail="LNsubdomain does not exist."
)
## If record_type is not one of the allowed ones reject the request
if data.record_type not in domain.allowed_record_types:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"{data.record_type} not a valid record."
)
status_code=HTTPStatus.BAD_REQUEST,
detail=f"{data.record_type} not a valid record.",
)
## If domain already exist in our database reject it
if await get_subdomainBySubdomain(data.subdomain) is not None:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"{data.subdomain}.{domain.domain} domain already taken."
)
status_code=HTTPStatus.BAD_REQUEST,
detail=f"{data.subdomain}.{domain.domain} domain already taken.",
)
## Dry run cloudflare... (create and if create is sucessful delete it)
cf_response = await cloudflare_create_subdomain(
@@ -124,12 +123,14 @@ async def api_subdomain_make_subdomain(domain_id, data: CreateSubdomain):
ip=data.ip,
)
if cf_response["success"] == True:
await cloudflare_deletesubdomain(domain=domain, domain_id=cf_response["result"]["id"])
await cloudflare_deletesubdomain(
domain=domain, domain_id=cf_response["result"]["id"]
)
else:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f'Problem with cloudflare: {cf_response["errors"][0]["message"]}'
)
status_code=HTTPStatus.BAD_REQUEST,
detail=f'Problem with cloudflare: {cf_response["errors"][0]["message"]}',
)
## ALL OK - create an invoice and return it to the user
sats = data.sats
@@ -142,10 +143,7 @@ async def api_subdomain_make_subdomain(domain_id, data: CreateSubdomain):
extra={"tag": "lnsubdomain"},
)
except Exception as e:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail=str(e)
)
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
subdomain = await create_subdomain(
payment_hash=payment_hash, wallet=domain.wallet, data=data
@@ -153,9 +151,8 @@ async def api_subdomain_make_subdomain(domain_id, data: CreateSubdomain):
if not subdomain:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="LNsubdomain could not be fetched."
)
status_code=HTTPStatus.NOT_FOUND, detail="LNsubdomain could not be fetched."
)
return {"payment_hash": payment_hash, "payment_request": payment_request}
@@ -181,15 +178,13 @@ async def api_subdomain_delete(subdomain_id, g: WalletTypeInfo = Depends(get_key
if not subdomain:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="LNsubdomain does not exist."
)
status_code=HTTPStatus.NOT_FOUND, detail="LNsubdomain does not exist."
)
if subdomain.wallet != g.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Not your subdomain."
)
status_code=HTTPStatus.FORBIDDEN, detail="Not your subdomain."
)
await delete_subdomain(subdomain_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)