A simple web portal for Linux iptables
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

web.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. from flask import Flask, render_template, request, url_for
  3. import logic
  4. app = Flask(__name__)
  5. @app.route('/remove', methods=['GET'])
  6. def remove():
  7. macs = logic.get_known_macs()
  8. return render_template('remove.html', macs=macs)
  9. @app.route('/removed', methods=['POST'])
  10. def removed():
  11. macs = request.form.getlist('mac')
  12. print('Macs: {}'.format(macs))
  13. for mac in macs:
  14. logic.remove_mac_address(mac)
  15. print('Removed {}'.format(mac))
  16. return 'Success!'
  17. @app.route('/', methods=['GET'])
  18. def form():
  19. macs = logic.get_unknown_macs()
  20. return render_template('portal.html', macs=macs)
  21. @app.route('/submit', methods=['POST'])
  22. def submit():
  23. macs = request.form.getlist('mac')
  24. print('Macs: {}'.format(macs))
  25. duration_type = request.form['duration_type']
  26. print('Duration Type: {}'.format(duration_type))
  27. comments = request.form.getlist('comment')
  28. comments = [ c for c in comments if c ]
  29. if len(macs) != len(comments):
  30. return 'Some macs doesn\'t have their owners written!'
  31. if duration_type == 'permanent':
  32. expire = 'never'
  33. else:
  34. h = request.form['duration_h']
  35. if not h:
  36. h = 0
  37. m = request.form['duration_m']
  38. if not m:
  39. m = 0
  40. expire = 3600 * int(h) + 60 * int(m)
  41. print('Duration: {}'.format(expire))
  42. mac_comm = list(zip(macs, comments))
  43. for mac in mac_comm:
  44. logic.allow_mac_address(mac[0], expire, mac[1])
  45. if expire == 'never':
  46. print('Allowed {0} forever'.format(mac[0], expire))
  47. else:
  48. print('Allowed {0} for {1}h {2}m'.format(mac[0], h, m))
  49. return 'Success!'
  50. if __name__ == '__main__':
  51. app.run(host="172.16.88.25", port="81")