|
|
@ -1,19 +1,28 @@
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from os import makedirs
|
|
|
|
from os import makedirs
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from flask import Flask, request, redirect
|
|
|
|
from flask import Flask, request, redirect
|
|
|
|
from flask import render_template, flash, url_for
|
|
|
|
from flask import render_template, flash, url_for
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from xmrnodes.forms import SubmitNode
|
|
|
|
from xmrnodes.models import Node
|
|
|
|
from xmrnodes.models import Node
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
|
|
|
level=logging.INFO,
|
|
|
|
|
|
|
|
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_envvar("FLASK_SECRETS")
|
|
|
|
app.config.from_envvar("FLASK_SECRETS")
|
|
|
|
app.secret_key = app.config["SECRET_KEY"]
|
|
|
|
app.secret_key = app.config["SECRET_KEY"]
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
|
|
def index():
|
|
|
|
def index():
|
|
|
|
|
|
|
|
form = SubmitNode()
|
|
|
|
itp = 20
|
|
|
|
itp = 20
|
|
|
|
page = request.args.get("page", 1)
|
|
|
|
page = request.args.get("page", 1)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
@ -22,61 +31,85 @@ def index():
|
|
|
|
flash("Wow, wtf hackerman. Cool it.")
|
|
|
|
flash("Wow, wtf hackerman. Cool it.")
|
|
|
|
page = 1
|
|
|
|
page = 1
|
|
|
|
|
|
|
|
|
|
|
|
nodes = Node.select().where(Node.available==True).order_by(Node.datetime_entered.desc()).paginate(page, itp)
|
|
|
|
nodes = Node.select().where(Node.available==True).order_by(
|
|
|
|
|
|
|
|
Node.datetime_entered.desc()
|
|
|
|
|
|
|
|
).paginate(page, itp)
|
|
|
|
total_pages = Node.select().count() / itp
|
|
|
|
total_pages = Node.select().count() / itp
|
|
|
|
return render_template("index.html", nodes=nodes, page=page, total_pages=total_pages)
|
|
|
|
return render_template(
|
|
|
|
|
|
|
|
"index.html",
|
|
|
|
|
|
|
|
nodes=nodes,
|
|
|
|
|
|
|
|
page=page,
|
|
|
|
|
|
|
|
total_pages=total_pages,
|
|
|
|
|
|
|
|
form=form
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/add", methods=["GET", "POST"])
|
|
|
|
@app.route("/add", methods=["GET", "POST"])
|
|
|
|
def add():
|
|
|
|
def add():
|
|
|
|
if request.method == "POST":
|
|
|
|
if request.method == "POST":
|
|
|
|
url = request.form.get("url")
|
|
|
|
url = request.form.get("node_url")
|
|
|
|
regex = re.compile(
|
|
|
|
regex = re.compile(
|
|
|
|
r'^(?:http)s?://' # http:// or https://
|
|
|
|
r'^(?:http)s?://' # http:// or https://
|
|
|
|
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
|
|
|
|
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
|
|
|
|
r'localhost|' #localhost...
|
|
|
|
r'localhost|' #localhost...
|
|
|
|
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
|
|
|
|
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
|
|
|
|
r'(?::\d+)?' # optional port
|
|
|
|
r'(?::\d+)?' # optional port
|
|
|
|
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
|
|
|
r'(?:/?|[/?]\S+)$', re.IGNORECASE
|
|
|
|
|
|
|
|
)
|
|
|
|
re_match = re.match(regex, url)
|
|
|
|
re_match = re.match(regex, url)
|
|
|
|
|
|
|
|
if re_match is None:
|
|
|
|
if re_match is not None:
|
|
|
|
flash("This doesn't look like a valid URL")
|
|
|
|
|
|
|
|
else:
|
|
|
|
_url = urlparse(url)
|
|
|
|
_url = urlparse(url)
|
|
|
|
|
|
|
|
url = f"{_url.scheme}://{_url.netloc}"
|
|
|
|
|
|
|
|
if Node.select().where(Node.url == url).exists():
|
|
|
|
|
|
|
|
flash("This node is already in the database.")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
flash("Seems like a valid node. Added to the database and will check soon.")
|
|
|
|
|
|
|
|
node = Node(url=url)
|
|
|
|
|
|
|
|
node.save()
|
|
|
|
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.cli.command("validate")
|
|
|
|
|
|
|
|
def validate():
|
|
|
|
|
|
|
|
nodes = Node.select().where(Node.validated == False)
|
|
|
|
|
|
|
|
for node in nodes:
|
|
|
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
|
|
|
is_onion = node.url.split(":")[1].endswith(".onion")
|
|
|
|
|
|
|
|
logging.info(f"Attempting to validate {node.url}")
|
|
|
|
|
|
|
|
if is_onion:
|
|
|
|
|
|
|
|
logging.info("onion address found")
|
|
|
|
|
|
|
|
node.tor = True
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
endpoint = f"{_url.scheme}://{_url.netloc}"
|
|
|
|
r = requests.get(node.url + "/get_info", timeout=3)
|
|
|
|
r = requests.get(endpoint + "/get_info", timeout=3)
|
|
|
|
|
|
|
|
r.raise_for_status()
|
|
|
|
r.raise_for_status()
|
|
|
|
# print(r.json())
|
|
|
|
assert "height" in r.json()
|
|
|
|
return {"status": "success"}
|
|
|
|
assert "nettype" in r.json()
|
|
|
|
|
|
|
|
nettype = r.json()["nettype"]
|
|
|
|
|
|
|
|
logging.info("success")
|
|
|
|
|
|
|
|
if nettype in ["mainnet", "stagenet", "testnet"]:
|
|
|
|
|
|
|
|
node.nettype = nettype
|
|
|
|
|
|
|
|
node.available = True
|
|
|
|
|
|
|
|
node.validated = True
|
|
|
|
|
|
|
|
node.datetime_checked = now
|
|
|
|
|
|
|
|
node.save()
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
logging.info("unexpected nettype")
|
|
|
|
except requests.exceptions.ConnectTimeout:
|
|
|
|
except requests.exceptions.ConnectTimeout:
|
|
|
|
flash("connection timed out. double-check the port")
|
|
|
|
logging.info("connection timed out")
|
|
|
|
return {"status": "fail", "reason": "timeout"}
|
|
|
|
node.delete_instance()
|
|
|
|
except requests.exceptions.SSLError:
|
|
|
|
except requests.exceptions.SSLError:
|
|
|
|
flash("invalid certificate")
|
|
|
|
logging.info("invalid certificate")
|
|
|
|
return {"status": "fail", "reason": "invalid cert"}
|
|
|
|
node.delete_instance()
|
|
|
|
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
|
|
|
|
logging.info("connection error")
|
|
|
|
|
|
|
|
node.delete_instance()
|
|
|
|
|
|
|
|
except requests.exceptions.HTTPError:
|
|
|
|
|
|
|
|
logging.info("http error, 4xx or 5xx")
|
|
|
|
|
|
|
|
node.delete_instance()
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
flash("failed to send req", str(e))
|
|
|
|
logging.info("failed for reasons unknown")
|
|
|
|
print(e)
|
|
|
|
node.delete_instance()
|
|
|
|
return {"status": "fail"}
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
flash("invalid url provided")
|
|
|
|
|
|
|
|
return {"status": "fail"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return "ok"
|
|
|
|
|
|
|
|
node = Node(
|
|
|
|
|
|
|
|
scheme=proto,
|
|
|
|
|
|
|
|
address=addr,
|
|
|
|
|
|
|
|
port=port,
|
|
|
|
|
|
|
|
version=r.json()["version"],
|
|
|
|
|
|
|
|
tor=addr.endswith(".onion"),
|
|
|
|
|
|
|
|
available=r.json()["status"] == "OK",
|
|
|
|
|
|
|
|
mainnet=r.json()["mainnet"],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
node.save()
|
|
|
|
|
|
|
|
return {"status": "success"}
|
|
|
|
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/about")
|
|
|
|
@app.route("/about")
|
|
|
|
def about():
|
|
|
|
def about():
|
|
|
|