diff --git a/README.md b/README.md index 0af65ce..2282314 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,17 @@ # meile-plan-rest Meile Subscription Plan REST API +## Dependencies +* uWSGI, `sudo apt install uwsgi-plugin-python3` +* Flask, +* FlaskMySQL, +* pexepct, +* PyJWT, +* date-util +* Werkzeug, +* SQLAlchemy, + +`pip install -r requirements.txt` + # Documentation -https://petstore.swagger.io/?url=https://raw.githubusercontent.com/MathNodes/meile-plan-rest/main/doc/meile-api.yaml +https://petstore.swagger.io/?url=https://path/to/file.yaml diff --git a/helpers/add-nodes-to-plan.sh b/helpers/add-nodes-to-plan.sh new file mode 100644 index 0000000..170f870 --- /dev/null +++ b/helpers/add-nodes-to-plan.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +PLANID=$1 +FILE=$2 + +# Edit These +WALLETNAME="BTCPay2" +KEYRINGDIR="/home/sentinel" + +if [[ $# -lt 2 ]]; then + echo " " + echo "Add Nodes from file to Plan, v0.1 - freQniK" + echo " " + echo "Usage: $0 " + echo " " + exit +fi + +mapfile -t plan_nodes < <(cat $FILE) + +for n in ${plan_nodes[@]}; do + echo "Adding: $n, to plan: $PLANID" + echo " " + ./sentinelhub tx vpn plan add-node \ + --from "$WALLETNAME" \ + --gas-prices "0.3udvpn" \ + --node "https://rpc.mathnodes.com:443" \ + --keyring-dir "$KEYRINGDIR" \ + --keyring-backend "file" \ + --chain-id "sentinelhub-2" \ + --yes \ + $PLANID $n + sleep 5 +done diff --git a/helpers/insert-nodes.py b/helpers/insert-nodes.py new file mode 100644 index 0000000..25e545a --- /dev/null +++ b/helpers/insert-nodes.py @@ -0,0 +1,74 @@ +#!/bin/env python3 + +import pymysql +import scrtxxs +import uuid +import argparse +import sys + + +def connDB(): + db = pymysql.connect(host=scrtxxs.MySQLHost, + port=scrtxxs.MySQLPort, + user=scrtxxs.MySQLUsername, + passwd=scrtxxs.MySQLPassword, + db=scrtxxs.MySQLDB, + charset='utf8mb4', + cursorclass=pymysql.cursors.DictCursor) + return db + + + + +if __name__ == "__main__": + + parser = argparse.ArgumentParser(description="Plan Node Inserter v0.1 - freQniK") + parser.add_argument('-f', '--file', help="file to read nodes from", metavar="node_file") + + args = parser.parse_args() + + if args.file: + NODE_FILE = str(args.file) + else: + print("Please specify a nodes file") + sys.exit(1) + + db = connDB() + c = db.cursor() + + + puuid = input("Enter the Plan UUID: ") + address = "start" + + node_addresses = [] + + + + with open(NODE_FILE, 'r') as nfile: + data = nfile.readlines() + k = 0 + for d in data: + d = d.rstrip() + node_addresses.append(d) + k += 1 + + + print(f"Inserting: {k}, nodes into plan: {puuid}...") + queries = [] + for a in node_addresses: + q =''' + INSERT IGNORE INTO plan_nodes (uuid, node_address) + VALUES ("%s", "%s"); + ''' % (puuid, a) + queries.append(q) + k = 0 + for q in queries: + try: + print(q) + c.execute(q) + db.commit() + k += 1 + except Exception as e: + print(str(e)) + + print(f"Inserted: {k}, nodes into plan: {puuid}.") \ No newline at end of file diff --git a/helpers/insert-plans.py b/helpers/insert-plans.py new file mode 100644 index 0000000..dbd84d7 --- /dev/null +++ b/helpers/insert-plans.py @@ -0,0 +1,44 @@ +#!/bin/env python3 + +import pymysql +import scrtxxs +import uuid +from datetime import datetime +from dateutil.relativedelta import relativedelta + +def connDB(): + db = pymysql.connect(host=scrtxxs.MySQLHost, + port=scrtxxs.MySQLPort, + user=scrtxxs.MySQLUsername, + passwd=scrtxxs.MySQLPassword, + db=scrtxxs.MySQLDB, + charset='utf8mb4', + cursorclass=pymysql.cursors.DictCursor) + return db + + +db = connDB() +c = db.cursor() + +puuid = uuid.uuid4() + +sub_id = int(input("Enter the Subscription ID: ")) +plan_id = int(input("Enter the Plan ID: ")) +plan_name = input("Enter the name of the plan: ") +au_plan_price = int(input("Enter the Plan price in a.u.: ")) +au_plan_denom = input("Enter the denom: ") + +now = datetime.now() +expires = now + relativedelta(months=+1) + +q =''' + INSERT IGNORE INTO meile_plans (uuid, subscription_id, plan_id, plan_name, plan_price, plan_denom, expiration_date) + VALUES ("%s", %d, %d, "%s", %d, "%s", "%s"); + ''' % (puuid, sub_id, plan_id, plan_name, au_plan_price, au_plan_denom, str(expires)) +try: + print(q) + c.execute(q) + db.commit() + print("Plan committed to database") +except Exception as e: + print(str(e)) \ No newline at end of file diff --git a/helpers/subscribe-to-nodes.sh b/helpers/subscribe-to-nodes.sh new file mode 100644 index 0000000..3f99ea6 --- /dev/null +++ b/helpers/subscribe-to-nodes.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +FILE="$1" + +# Edit These +DIR="/home/sentinel/go/bin/subscriptions" #Log dir of sub hashes and results +WALLETNAME="BTCPay2" +KEYRINGDIR="/home/sentinel" +HOURS=720 + +if [[ $# -lt 1 ]]; then + echo " " + echo "Subscribe To Nodes - v0.1 - freQniK" + echo " " + echo "Usage: $0 > $DIR/$n.resp + + sleep 7 + hash=`cat $DIR/$n.resp | grep "txhash" | sed 's/txhash\: //g'` + ./sentinelhub query tx --type=hash $hash --node https://rpc.mathnodes.com:443 | grep "raw_log" | sed 's/raw_log\: //g' | sed 's/^.//;s/.$//' | jq >> $DIR/$n-$hash.json + cat $DIR/$n-$hash.json + +done diff --git a/meile_plan_api.py b/meile_plan_api.py index 5e40048..ba36057 100644 --- a/meile_plan_api.py +++ b/meile_plan_api.py @@ -1,8 +1,6 @@ -import pwd import os import jwt import time -import stripe import pexpect from datetime import datetime diff --git a/run-meile-rest.sh b/run-meile-rest.sh new file mode 100644 index 0000000..65f299b --- /dev/null +++ b/run-meile-rest.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +IP=$1 + +FULLCHAIN="/home/sentinel/api/certs/fullchain.pem" +PRIVKEY="/home/sentinel/api/certs/privkey.pem" + +uwsgi --plugin python3 --https-socket $IP:5000,$FULLCHAIN,$PRIVKEY --wsgi-file uWSGI.py --callable app --processes 6 --threads 8 --stats $IP:9191 diff --git a/scrtxxs.py b/scrtxxs.py index 0780669..51455bf 100644 --- a/scrtxxs.py +++ b/scrtxxs.py @@ -5,6 +5,8 @@ WalletName = "Name" HotWalletPW = "password" SQLAlchemyScrtKey = 'sql_scrt_key' +MySQLHost = "127.0.0.1" +MySQLPort = 3306 MySQLUsername = 'username' MySQLPassword = 'password' MySQLDB = 'db_name' diff --git a/sql/plan_db.sql b/sql/plan_db.sql index 31e46ad..d20f0b8 100644 --- a/sql/plan_db.sql +++ b/sql/plan_db.sql @@ -1,6 +1,9 @@ --- Create Meile Plan Table -CREATE TABLE meile_plans (uuid VARCHAR(100), subscription_id UNSIGNED BIGINT, plan_id UNSIGNED BIGINT, plan_name VARACHAR(256), plan_price UNSIGNED BIGINT, plan_denom VARCHAR(100), PRIMARY KEY(uuid)) +CREATE TABLE meile_plans (uuid VARCHAR(100), subscription_id BIGINT UNSIGNED, plan_id BIGINT UNSIGNED, plan_name VARCHAR(256), plan_price BIGINT UNSIGNED, plan_denom VARCHAR(100), expiration_date TIMESTAMP PRIMARY KEY(uuid)); --- Create Meile Subscriber Table -CREATE TABLE meile_subscriptions (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, uuid VARCHAR(100), wallet VARCHAR(100), subscription_id UNSIGNED BIGINT, plan_id UNSIGNED BIGINT, amt_paid DECIMAL(24,12), amt_denom VARCHAR(100), subscribe_date TIMSTAMP, subscription_duration UNSIGNED SMALLINT, expires TIMESTAMP, PRIMARY KEY(id)) \ No newline at end of file +CREATE TABLE meile_subscriptions (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, uuid VARCHAR(100), wallet VARCHAR(100), subscription_id BIGINT UNSIGNED, plan_id BIGINT UNSIGNED, amt_paid DECIMAL(24,12), amt_denom VARCHAR(10), subscribe_date TIMESTAMP, subscription_duration SMALLINT UNSIGNED, expires TIMESTAMP, PRIMARY KEY(id)); + +--- Create Plan Nodes Table +CREATE TABLE plan_nodes (uuid VARCHAR(100), node_address VARCHAR(255), PRIMARY KEY(uuid, node_address)); \ No newline at end of file diff --git a/uWSGI.py b/uWSGI.py new file mode 100644 index 0000000..557f9d0 --- /dev/null +++ b/uWSGI.py @@ -0,0 +1,4 @@ +from meile_plan_api import app + +if __name__ == "__main__": + app.run()