2020-05-26 17:24:29 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
########################################################
|
|
|
|
# SSH Dialogs to manage Subscriptions on the RaspiBlitz
|
|
|
|
########################################################
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import math
|
|
|
|
import time
|
|
|
|
import toml
|
2020-05-26 20:08:32 +02:00
|
|
|
import os
|
2020-05-26 19:58:27 +02:00
|
|
|
import subprocess
|
2020-05-26 17:24:29 +02:00
|
|
|
|
2020-05-26 17:27:47 +02:00
|
|
|
from dialog import Dialog
|
|
|
|
|
2020-05-26 17:24:29 +02:00
|
|
|
from blitzpy import RaspiBlitzConfig
|
|
|
|
|
|
|
|
# load config
|
|
|
|
cfg = RaspiBlitzConfig()
|
2020-05-26 17:27:47 +02:00
|
|
|
cfg.reload()
|
2020-05-26 17:24:29 +02:00
|
|
|
|
2020-05-26 19:15:03 +02:00
|
|
|
# basic values
|
|
|
|
SUBSCRIPTIONS_FILE="/mnt/hdd/app-data/subscriptions/subscriptions.toml"
|
2020-05-26 17:24:29 +02:00
|
|
|
|
|
|
|
####### HELPER FUNCTIONS #########
|
|
|
|
|
|
|
|
def eprint(*args, **kwargs):
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
|
|
|
def parseDateIP2TORSERVER(datestr):
|
|
|
|
return datetime.datetime.strptime(datestr,"%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
|
|
|
|
|
def secondsLeft(dateObj):
|
|
|
|
return round((dateObj - datetime.datetime.utcnow()).total_seconds())
|
|
|
|
|
|
|
|
####### SSH MENU FUNCTIONS #########
|
|
|
|
|
|
|
|
def mySubscriptions():
|
|
|
|
|
2020-05-26 17:40:35 +02:00
|
|
|
# check if any subscriptions are available
|
|
|
|
countSubscriptions=0
|
|
|
|
try:
|
|
|
|
subs = toml.load(SUBSCRIPTIONS_FILE)
|
|
|
|
countSubscriptions += len(subs['subscriptions_ip2tor'])
|
|
|
|
except Exception as e: pass
|
|
|
|
if countSubscriptions == 0:
|
|
|
|
Dialog(dialog="dialog",autowidgetsize=True).msgbox('''
|
2020-05-26 20:14:58 +02:00
|
|
|
You have no active or inactive subscriptions.
|
2020-05-26 17:40:35 +02:00
|
|
|
''',title="Info")
|
|
|
|
return
|
|
|
|
|
2020-05-26 17:43:59 +02:00
|
|
|
# load subscriptions and make dialog choices out of it
|
|
|
|
choices = []
|
|
|
|
lookup = {}
|
|
|
|
lookupIndex=0
|
|
|
|
subs = toml.load(SUBSCRIPTIONS_FILE)
|
2020-05-26 17:24:29 +02:00
|
|
|
|
2020-05-26 17:43:59 +02:00
|
|
|
# list ip2tor subscriptions
|
|
|
|
for sub in subs['subscriptions_ip2tor']:
|
|
|
|
# remember subscription under lookupindex
|
|
|
|
lookupIndex += 1
|
|
|
|
lookup[str(lookupIndex)]=sub
|
|
|
|
# add to dialog choices
|
|
|
|
if sub['active']:
|
2020-05-26 19:43:31 +02:00
|
|
|
activeState="active"
|
2020-05-26 17:43:59 +02:00
|
|
|
else:
|
2020-05-26 19:43:31 +02:00
|
|
|
activeState="in-active"
|
2020-05-26 19:58:27 +02:00
|
|
|
name="IP2TOR Bridge for {0}".format(sub['blitz_service'])
|
2020-05-26 19:43:31 +02:00
|
|
|
choices.append( ("{0}".format(lookupIndex), "{0} ({1})".format(name.ljust(30), activeState)) )
|
2020-05-26 17:24:29 +02:00
|
|
|
|
2020-05-26 17:43:59 +02:00
|
|
|
# show menu with options
|
|
|
|
d = Dialog(dialog="dialog",autowidgetsize=True)
|
|
|
|
d.set_background_title("RaspiBlitz Subscriptions")
|
|
|
|
code, tag = d.menu(
|
2020-05-26 19:43:31 +02:00
|
|
|
"\nYou have the following subscriptions - select for details:",
|
|
|
|
choices=choices, width=65, height=15, title="My Subscriptions")
|
2020-05-26 17:24:29 +02:00
|
|
|
|
2020-05-26 17:43:59 +02:00
|
|
|
# if user chosses CANCEL
|
|
|
|
if code != d.OK: return
|
|
|
|
|
|
|
|
# get data of selected subscrption
|
|
|
|
selectedSub = lookup[str(tag)]
|
|
|
|
|
|
|
|
# show details of selected
|
|
|
|
d = Dialog(dialog="dialog",autowidgetsize=True)
|
|
|
|
d.set_background_title("My Subscriptions")
|
|
|
|
if selectedSub['type'] == "ip2tor-v1":
|
|
|
|
if len(selectedSub['warning']) > 0:
|
|
|
|
selectedSub['warning'] = "\n{0}".formart(selectedSub['warning'])
|
|
|
|
text='''
|
2020-05-26 17:24:29 +02:00
|
|
|
This is a IP2TOR subscription bought on {initdate} at
|
|
|
|
{shop}
|
|
|
|
|
|
|
|
It forwards from the public address {publicaddress} to
|
|
|
|
{toraddress}
|
|
|
|
for the RaspiBlitz service: {service}
|
|
|
|
|
|
|
|
It will renew every {renewhours} hours for {renewsats} sats.
|
|
|
|
Total payed so far: {totalsats} sats
|
|
|
|
|
|
|
|
The state of the subscription is: {active} {warning}
|
|
|
|
|
|
|
|
The following additional information is available:
|
|
|
|
{description}
|
2020-05-26 17:27:47 +02:00
|
|
|
'''.format( initdate=selectedSub['time_created'],
|
2020-05-26 17:24:29 +02:00
|
|
|
shop=selectedSub['shop'],
|
|
|
|
publicaddress="{0}:{1}".format(selectedSub['ip'],selectedSub['port']),
|
|
|
|
toraddress=selectedSub['tor'],
|
|
|
|
renewhours=(round(int(selectedSub['duration'])/3600)),
|
|
|
|
renewsats=(round(int(selectedSub['price_extension'])/1000)),
|
|
|
|
totalsats=(round(int(selectedSub['price_extension'])/1000)),
|
2020-05-26 20:41:01 +02:00
|
|
|
active= "ACTIVE" if selectedSub['active'] else "NOT ACTIVE",
|
2020-05-26 17:24:29 +02:00
|
|
|
warning=selectedSub['warning'],
|
|
|
|
description=selectedSub['description'],
|
|
|
|
service=selectedSub['blitz_service']
|
2020-05-26 17:43:59 +02:00
|
|
|
)
|
2020-05-26 17:24:29 +02:00
|
|
|
|
2020-05-26 17:43:59 +02:00
|
|
|
if selectedSub['active']:
|
2020-05-26 20:13:40 +02:00
|
|
|
extraLable = "CANCEL SUBSCRIPTION"
|
2020-05-26 17:43:59 +02:00
|
|
|
else:
|
2020-05-26 20:13:40 +02:00
|
|
|
extraLable = "DELETE SUBSCRIPTION"
|
2020-05-26 20:28:49 +02:00
|
|
|
code = d.msgbox(text, title="Subscription Detail", ok_label="Back", extra_button=True, extra_label=extraLable ,width=75, height=30)
|
2020-05-26 17:24:29 +02:00
|
|
|
|
2020-05-26 17:43:59 +02:00
|
|
|
# user wants to delete this subscription
|
2020-05-26 20:09:35 +02:00
|
|
|
# call the responsible sub script for deletion just in case any subscription needs to do some extra api calls when canceling
|
2020-05-26 17:43:59 +02:00
|
|
|
if code == "extra":
|
2020-05-26 20:10:34 +02:00
|
|
|
os.system("clear")
|
2020-05-26 17:43:59 +02:00
|
|
|
if selectedSub['type'] == "ip2tor-v1":
|
2020-05-26 20:02:37 +02:00
|
|
|
cmd="python /home/admin/config.scripts/blitz.subscriptions.ip2tor.py subscription-cancel {0}".format(selectedSub['id'])
|
2020-05-26 19:58:27 +02:00
|
|
|
print("# running: {0}".format(cmd))
|
2020-05-26 20:08:32 +02:00
|
|
|
os.system(cmd)
|
|
|
|
time.sleep(2)
|
2020-05-26 20:10:34 +02:00
|
|
|
else:
|
|
|
|
print("# FAIL: unknown subscription type")
|
|
|
|
time.sleep(3)
|
2020-05-26 20:08:32 +02:00
|
|
|
|
2020-05-26 17:43:59 +02:00
|
|
|
# loop until no more subscriptions or user chooses CANCEL on subscription list
|
|
|
|
mySubscriptions()
|
2020-05-26 17:24:29 +02:00
|
|
|
|
|
|
|
####### SSH MENU #########
|
|
|
|
|
2020-05-26 21:05:56 +02:00
|
|
|
choices = []
|
|
|
|
choices.append( ("LIST","My Subscriptions") )
|
|
|
|
choices.append( ("NEW1","+ new IP2TOR Bridge") )
|
2020-05-26 20:41:01 +02:00
|
|
|
|
2020-05-26 21:05:56 +02:00
|
|
|
d = Dialog(dialog="dialog",autowidgetsize=True)
|
|
|
|
d.set_background_title("RaspiBlitz Subscriptions")
|
|
|
|
code, tag = d.menu(
|
|
|
|
"\nCheck your existing subscriptions or create new:",
|
|
|
|
choices=choices, width=40, height=10, title="Subscription Management")
|
2020-05-26 20:42:07 +02:00
|
|
|
|
2020-05-26 21:05:56 +02:00
|
|
|
# if user chosses CANCEL
|
2020-05-26 21:07:28 +02:00
|
|
|
if code != d.OK:
|
|
|
|
sys.exit(0)
|
2020-05-26 20:41:01 +02:00
|
|
|
|
2020-05-26 21:05:56 +02:00
|
|
|
if tag == "LIST":
|
|
|
|
mySubscriptions()
|
|
|
|
sys.exit(0)
|
2020-05-26 20:41:01 +02:00
|
|
|
|
2020-05-26 21:05:56 +02:00
|
|
|
if tag == "NEW1":
|
|
|
|
cmd="python /home/admin/config.scripts/blitz.subscriptions.ip2tor.py blitz.subscriptions.ip2tor.py create-ssh-dialog {0} {1} {2}".format("RTL","s7foqiwcstnxmlesfsjt7nlhwb2o6w44hc7glv474n7sbyckf76wn6id.onion","80")
|
|
|
|
print("# running: {0}".format(cmd))
|
2020-05-26 21:07:28 +02:00
|
|
|
os.system(cmd)
|
|
|
|
sys.exit(0)
|