auth.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from flask import Flask, request, render_template
  2. import paramiko
  3. import time
  4. import re
  5. # MikroTik API credentials
  6. ROUTER_IP = '10.10.10.10'
  7. USERNAME = 'admin'
  8. PASSWORD = 'PASSWORD'
  9. app = Flask(__name__)
  10. # Configure Flask to trust X-Forwarded-For header
  11. app.config['TRUSTED_PROXIES'] = '127.0.0.1'
  12. def remove_port(ip_address_with_port):
  13. return re.split(r'[;,|:]',ip_address_with_port)
  14. # Function to add IP address to the specified list with a timeout
  15. def add_to_list(ip_address_arg):
  16. ip_address = remove_port(ip_address_arg)[0]
  17. ssh = paramiko.SSHClient()
  18. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  19. ssh.connect(ROUTER_IP,port=22, username=USERNAME, password=PASSWORD)
  20. # Send command to add IP address to address list
  21. command = f"/ip firewall address-list add list=port_knocking_stage1 address={ip_address} timeout=12h"
  22. stdin, stdout, stderr = ssh.exec_command(command)
  23. # Wait for the command to execute
  24. time.sleep(1)
  25. # Check for any errors
  26. if stderr.read().decode():
  27. print("Error:", stderr.read().decode())
  28. else:
  29. print("IP address added successfully. "+ip_address)
  30. ssh.close()
  31. # Dummy database for demonstration (replace with your own authentication mechanism)
  32. users = {
  33. 'admin': 'PASS',
  34. 'guest': 'PASS'
  35. }
  36. # Authentication route
  37. @app.route('/', methods=['GET', 'POST'])
  38. def login():
  39. error = None
  40. if request.method == 'POST':
  41. username = request.form['username']
  42. password = request.form['password']
  43. if username in users and users[username] == password:
  44. user_ip = request.headers.get('X-Forwarded-For', request.remote_addr)
  45. add_to_list(user_ip)
  46. add_to_list(user_ip)
  47. success_message = 'Authenticated successfully! Your IP address <strong>{}</strong> has been added to the whitelist for 12 hours.<br> <a href="https://filevista.ardugeek.ovh/public/58/home-server-wan.rdp" download>Download RDP File Here</a>'.format(remove_port(user_ip)[0])
  48. return '<div style="font-family: Arial, sans-serif; text-align: center; margin-top: 50px;"><h2 style="color: #4CAF50;">Success!</h2><p>{}</p></div>'.format(success_message)
  49. else:
  50. error = 'Invalid credentials. Please try again.'
  51. return render_template('login.html', error=error)
  52. if __name__ == '__main__':
  53. app.run(debug=False)