Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new proxy instance #4

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions routes/misc/add_proxy.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import cloudscraper
from flask import render_template, request, jsonify
from flask import render_template, request, jsonify, redirect, url_for
from bs4 import BeautifulSoup

scraper = cloudscraper.create_scraper()


def proxy_add_new(csrf_access_token):
error = None
if request.method == 'POST':
return check_nginx_validity(csrf_access_token)
if not 'username' in request.form:
return check_nginx_validity(csrf_access_token)
else:
return authenticate_user_in_nginx(request.form['target'], request.form['username'], request.form['password'], csrf_access_token, request.form['csrf_token'])
return render_template('misc/add_proxy.html', error=error)
elif request.method == 'GET':
return render_template('misc/add_proxy.html', error=error)

Expand Down Expand Up @@ -37,14 +42,16 @@ def timeout_checker(url, timeout=5):

def check_nginx_validity(csrf_access_token):
if request.method == 'POST':
if csrf_access_token != request.form['csrf_token']:
csrf_token = request.form['csrf_token']

if csrf_access_token != csrf_token:
return jsonify({'error': 'Invalid CSRF token'}), 400
else:
url_form = request.form['target']
url = url_form.strip()
if not url:
error = 'URL is required.'
return render_template('misc/add_proxy.html', error=error)
return render_template('misc/add_proxy.html', target_error=error)
else:
if validate(url):
pass
Expand All @@ -53,20 +60,60 @@ def check_nginx_validity(csrf_access_token):
try:
response = timeout_checker(url)
if response == 'host_up':
return render_template('misc/add_proxy.html', success='Host is up and running !')
return render_template('misc/add_proxy.html', target_success='Host is up and running !')
elif response == "Timeout Error":
error = 'Timeout Error'
return render_template('misc/add_proxy.html', error=error)
return render_template('misc/add_proxy.html', target_error=error)
elif response == 'not_nginx_proxy_manager':
error = 'Error... This is not a Nginx Proxy Manager URL !'
return render_template('misc/add_proxy.html', error=error)
error = 'This is not a Nginx Proxy Manager URL !'
return render_template('misc/add_proxy.html', target_error=error)
else:
error = 'Error fetching the URL'
return render_template('misc/add_proxy.html', error=error)
return render_template('misc/add_proxy.html', target_error=error)
except:
return render_template('misc/add_proxy.html', error='Error fetching the URL')
error = ''
return render_template('misc/add_proxy.html', target_error='Error fetching the URL')



def authenticate_user_in_nginx(url, user, password, csrf_access_token, csrf_sent_token):
csrf_access_token = csrf_access_token
csrf_sent_token = csrf_sent_token
if csrf_access_token != csrf_sent_token:
return jsonify({'error': 'Invalid CSRF token'}), 400
if not verify_credentials_validity(url, user, password):
return render_template('misc/add_proxy.html', error='Please fill all the fields')
else:

nginx_url = url+'/api/tokens'
nginx_user = user
nginx_password = password

def authenticate_user_in_nginx(user, password):
pass
try:
form = {
'identity': nginx_user,
'secret': nginx_password
}
response = scraper.post(nginx_url, data=form)
jsonified = response.json()
if 'error' in jsonified:
return render_template('misc/add_proxy.html', error=jsonified['error']['message'])
else:
token = jsonified['token']

headers = {
'Authorization': f'Bearer {token}',
'accept': 'application/json'
}
hosts = scraper.get(url+'/api/nginx/proxy-hosts', headers=headers)
print(hosts.json()[1])
return render_template('misc/add_proxy.html', success='Successfully authenticated !')
#return redirect(url_for('dashboard'))
except Exception as e:
print(e)


def verify_credentials_validity(url, user, password):
if not url or not user or not password:
return False
else:
return True
10 changes: 10 additions & 0 deletions static/misc/add_proxy.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ body {
font-size: 15px;
}

.target_error {
color: red;
font-size: 15px;
}

.success {
color: green;
font-size: 15px;
}

.target_success {
color: green;
font-size: 15px;
}
23 changes: 19 additions & 4 deletions templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<!-- Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="static/dashboard/style.css" rel="stylesheet">
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Dashboard</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<form action="/logout" method="post">
<button type="submit" class="btn btn-link nav-link">Déconnexion</button>
</form>
</li>
<li class="nav-item">
<a class="nav-link" href="/add_proxy">Ajouter un proxy</a>
</li>
</ul>
</div>
</nav>

<div class="container">
<h1>Dashboard</h1>
<div class="row">
Expand Down Expand Up @@ -38,11 +55,9 @@ <h5 class="card-title">Card 3</h5>
</div>
</div>
</div>

</div>
</div>

<!-- Bootstrap JS and dependencies (not required for styling) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
Expand Down
24 changes: 17 additions & 7 deletions templates/misc/add_proxy.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,32 @@
</div>
<button type="submit" class="btn btn-primary" id="pingBtn">Check host</button>
</form>
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }}</p>
{% if target_error %}
<p class="target_error"><strong>Error:</strong> {{ target_error }}</p>
{% endif %}
{% if success %}
<p class="success"><strong>Success ! :</strong> {{ success }}</p>
{% if target_success %}
<p class="target_success"><strong>Success ! :</strong> {{ target_success }}</p>
{% endif %}
<div id="status" class="mt-3"></div>
<form action="/add_proxy" method="post">
<div class="form-group">
<input type="hidden" name="target" value="{{ request.form.target }}">
<input type="hidden" name="csrf_token" value="{{ request.cookies.get('csrf_access_token') }}">
<label for="username">Username:</label>
<input type="text" class="form-control bg-dark text-white" placeholder="Username" id="username" value="{{ request.form.username }}">
<input type="text" class="form-control bg-dark text-white" placeholder="Username" id="username" name="username" value="{{ request.form.username }}">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control bg-dark text-white" placeholder="Password" id="password" value="{{ request.form.password }}">
<input type="password" class="form-control bg-dark text-white" placeholder="Password" id="password" name="password" value="{{ request.form.password }}">
</div>
<button class="btn btn-success" id="loginBtn">Login</button>
<button type="submit" class="btn btn-primary" id="loginBtn">Login</button>
</form>
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }}</p>
{% endif %}
{% if success %}
<p class="success"><strong>Success ! :</strong> {{ success }}</p>
{% endif %}
</div>
</div>
</div>
Expand Down