Browse Source

integration with cloudflare, working prototype

master
Kristjan 4 years ago
parent
commit
493ab5464e
  1. 45
      lnbits/extensions/subdomains/crud.py
  2. 8
      lnbits/extensions/subdomains/templates/subdomains/index.html
  3. 4
      lnbits/extensions/subdomains/views_api.py

45
lnbits/extensions/subdomains/crud.py

@ -32,7 +32,7 @@ async def create_subdomain(
async def set_subdomain_paid(payment_hash: str) -> Subdomains:
row = await db.fetchone("SELECT * FROM subdomain WHERE id = ?", (payment_hash,))
row = await db.fetchone("SELECT s.*, d.domain as domain_name FROM subdomain s INNER JOIN domain d ON (s.domain = d.id) WHERE s.id = ?", (payment_hash,))
if row[8] == False:
await db.execute(
"""
@ -55,30 +55,59 @@ async def set_subdomain_paid(payment_hash: str) -> Subdomains:
""",
(amount, row[1]),
)
subdomain = await get_subdomain(payment_hash)
### SEND REQUEST TO CLOUDFLARE
url="https://api.cloudflare.com/client/v4/zones/" + domaindata.cf_zone_id + "/dns_records"
header= {'Authorization': 'Bearer ' + domaindata.cf_token, 'Content-Type': 'application/json'}
aRecord=subdomain.subdomain + '.' + subdomain.domain_name
cf_response = ""
async with httpx.AsyncClient() as client:
try:
r = await client.post(
url,
headers=header,
json={
"type": "A",
"name": aRecord,
"content": subdomain.ip,
"ttl": 0,
"proxed": False
},
timeout=40,
)
cf_response = r.text
except AssertionError:
cf_response = "Error occured"
### Use webhook to notify about cloudflare registration
if domaindata.webhook:
async with httpx.AsyncClient() as client:
try:
r = await client.post(
domaindata.webhook,
json={
"domain": subdomain.domain,
"domain": subdomain.domain_name,
"subdomain": subdomain.subdomain,
"email": subdomain.email,
"ip": subdomain.ip
"ip": subdomain.ip,
"cost:": str(subdomain.sats) + " sats",
"duration": str(subdomain.duration) + " days",
"cf_response": cf_response
},
timeout=40,
)
except AssertionError:
webhook = None
return subdomain
subdomain = await get_subdomain(payment_hash)
return
async def get_subdomain(subdomain_id: str) -> Optional[Subdomains]:
row = await db.fetchone("SELECT * FROM subdomain s INNER JOIN domain d ON (s.domain = d.id) WHERE s.id = ?", (subdomain_id,))
row = await db.fetchone("SELECT s.*, d.domain as domain_name FROM subdomain s INNER JOIN domain d ON (s.domain = d.id) WHERE s.id = ?", (subdomain_id,))
print(row)
return Subdomains(**row) if row else None
@ -99,14 +128,14 @@ async def delete_subdomain(subdomain_id: str) -> None:
# Domains
async def create_domain(*, wallet: str, domain: str, cfToken: str, cfZoneId: str, webhook: Optional[str] = None, description: str, cost: int) -> Domains:
async def create_domain(*, wallet: str, domain: str, cf_token: str, cf_zone_id: str, webhook: Optional[str] = None, description: str, cost: int) -> Domains:
domain_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO domain (id, wallet, domain, webhook, cf_token, cf_zone_id, description, cost, amountmade)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(domain_id, wallet, domain, webhook, cfToken, cfZoneId, description, cost, 0),
(domain_id, wallet, domain, webhook, cf_token, cf_zone_id, description, cost, 0),
)
domain = await get_domain(domain_id)

8
lnbits/extensions/subdomains/templates/subdomains/index.html

@ -106,9 +106,9 @@
label="Wallet *">
</q-select>
<q-input filled dense v-model.trim="domainDialog.data.domain" type="text" label="Domain name "></q-input>
<q-input filled dense v-model.trim="domainDialog.data.cfToken" type="text" label="Cloudflare API token">
<q-input filled dense v-model.trim="domainDialog.data.cf_token" type="text" label="Cloudflare API token">
</q-input>
<q-input filled dense v-model.trim="domainDialog.data.cfZoneId" type="text" label="Cloudflare Zone Id">
<q-input filled dense v-model.trim="domainDialog.data.cf_zone_id" type="text" label="Cloudflare Zone Id">
</q-input>
<q-input filled dense v-model.trim="domainDialog.data.webhook" type="text" label="Webhook (optional)"
hint="A URL to be called whenever this link receives a payment."></q-input>
@ -306,8 +306,8 @@
this.domainDialog.data.wallet = link.wallet
this.domainDialog.data.domain = link.domain
this.domainDialog.data.description = link.description
this.domainDialog.data.cfToken = link.cf_token
this.domainDialog.data.cfZoneId = link.cf_zone_id
this.domainDialog.data.cf_token = link.cf_token
this.domainDialog.data.cf_zone_id = link.cf_zone_id
this.domainDialog.data.webhook = link.webhook
this.domainDialog.data.cost = link.cost
this.domainDialog.show = true

4
lnbits/extensions/subdomains/views_api.py

@ -42,8 +42,8 @@ async def api_domains():
schema={
"wallet": {"type": "string", "empty": False, "required": True},
"domain": {"type": "string", "empty": False, "required": True},
"cfToken": {"type": "string", "empty": False, "required": True},
"cfZoneId": {"type": "string", "empty": False, "required": True},
"cf_token": {"type": "string", "empty": False, "required": True},
"cf_zone_id": {"type": "string", "empty": False, "required": True},
"webhook": {"type": "string", "empty": False, "required": False},
"description": {"type": "string", "min": 0, "required": True},
"cost": {"type": "integer", "min": 0, "required": True},

Loading…
Cancel
Save