add haproxy config generator for proxying

pull/22/head
lza_menace 2 years ago
parent 33a6a9b8a2
commit 0d12e4d30a

@ -7,7 +7,7 @@ import geoip2.database
import arrow import arrow
import requests import requests
from flask import Flask, request, redirect, jsonify from flask import Flask, request, redirect, jsonify
from flask import render_template, flash from flask import render_template, flash, Response
from urllib.parse import urlparse, urlencode from urllib.parse import urlparse, urlencode
from xmrnodes.helpers import determine_crypto, is_onion, make_request from xmrnodes.helpers import determine_crypto, is_onion, make_request
@ -99,6 +99,25 @@ def nodes_json():
} }
}) })
@app.route("/haproxy.cfg")
def haproxy():
crypto = request.args.get('chain') or 'monero'
nettype = request.args.get('network') or 'mainnet'
cors = request.args.get('cors') or False
tor = request.args.get('onion') or False
nodes = Node.select().where(
Node.validated == True,
Node.nettype == nettype,
Node.crypto == crypto,
Node.is_tor == tor,
Node.web_compatible == cors
)
tpl = render_template("haproxy.html", nodes=nodes)
print(tpl)
res = Response(tpl)
res.headers['Content-Disposition'] = f'attachment; filename="haproxy-{crypto}-{nettype}-cors_{cors}-tor_{tor}.cfg"'
return res
@app.route("/wow_nodes.json") @app.route("/wow_nodes.json")
def wow_nodes_json(): def wow_nodes_json():
nodes = Node.select().where( nodes = Node.select().where(
@ -314,7 +333,7 @@ def validate():
@app.cli.command("export") @app.cli.command("export")
def export(): def export():
all_nodes = [] all_nodes = []
ts = int(arrow.get().timestamp) ts = int(arrow.get().timestamp())
export_dir = f"{config.DATA_DIR}/export.txt" export_dir = f"{config.DATA_DIR}/export.txt"
export_dir_stamped = f"{config.DATA_DIR}/export-{ts}.txt" export_dir_stamped = f"{config.DATA_DIR}/export-{ts}.txt"
nodes = Node.select().where(Node.validated == True) nodes = Node.select().where(Node.validated == True)

@ -24,6 +24,10 @@ class Node(Model):
datetime_failed = DateTimeField(default=None, null=True) datetime_failed = DateTimeField(default=None, null=True)
fail_reason = CharField(null=True) fail_reason = CharField(null=True)
def get_netloc(self):
_url = urlparse(self.url)
return _url.netloc
def get_failed_checks(self): def get_failed_checks(self):
hcs = HealthCheck.select().where(HealthCheck.node == self, HealthCheck.health == False) hcs = HealthCheck.select().where(HealthCheck.node == self, HealthCheck.health == False)
return hcs return hcs

@ -0,0 +1,18 @@
defaults
mode http
timeout client 10s
timeout connect 5s
timeout server 10s
timeout http-request 10s
frontend frontend
bind :8080
default_backend nodes
backend nodes
balance roundrobin
option httpchk GET /get_info
{% for node in nodes -%}
server backend-{{ node.id }} {{ node.get_netloc() }}
{% endfor %}

@ -69,7 +69,6 @@
</span> </span>
</form> </form>
</div> </div>
<br>
{% if nodes %} {% if nodes %}
<div class="xmrnodes"> <div class="xmrnodes">
<p class="center"> <p class="center">
@ -81,6 +80,7 @@
</strong> </strong>
<br><br> <br><br>
{% endif %} {% endif %}
<a href="{{ url_for('haproxy', chain=request.args.get('chain'), network=request.args.get('network'), cors=request.args.get('cors'), onion=request.args.get('onion')) }}">Download HAProxy config</a><br /><br />
Tracking {{ nodes_all }} {{ nettype }} {{ crypto | capitalize }} nodes in the database. Tracking {{ nodes_all }} {{ nettype }} {{ crypto | capitalize }} nodes in the database.
<br> <br>
Of those, {{ nodes_unhealthy }} nodes failed their last check-in (unresponsive to ping or over 500 blocks away from highest reported block). Of those, {{ nodes_unhealthy }} nodes failed their last check-in (unresponsive to ping or over 500 blocks away from highest reported block).

Loading…
Cancel
Save