-
Notifications
You must be signed in to change notification settings - Fork 116
Tweaking iptables for Captive Portal (Anish's Python approach, thx to Nikos Fotiou) #870
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aeacbe6
tweaking iptables for captive portal
jvonau 74fb198
Initial working copy of the captive portal
m-anish 5674675
captive_portal service must run as root
m-anish 418d941
whitespace
jvonau ef58b91
use lan_ip in place of hardcoding
jvonau cc09adb
replace named-iiab.conf to pickup changes in dns_jail_enabled
jvonau 43b67fb
install dnsmasq by default
jvonau 516daef
use dns_jail_enabled for dnsmasq blackhole
jvonau 2f47e84
use py_captive_portal_enabled and restore captive_portal_enabled
jvonau ca7c291
use py_ for new captive_portal variables
jvonau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| - name: Create directory for captive portal script | ||
| file: path=/opt/iiab/captive-portal state=directory | ||
| when: py_captive_portal_install | ||
|
|
||
| - name: Copy captive portal script | ||
| template: | ||
| src: roles/network/templates/captive_portal/captive_portal.py.j2 | ||
| dest: /opt/iiab/captive-portal/captive_portal.py | ||
| owner: root | ||
| group: root | ||
| mode: 0740 | ||
| when: py_captive_portal_install | ||
|
|
||
| - name: Copy captive portal service file | ||
| template: | ||
| src: roles/network/templates/captive_portal/captive_portal.service.j2 | ||
| dest: /etc/systemd/system/captive_portal.service | ||
| owner: root | ||
| group: root | ||
| mode: 0644 | ||
| when: py_captive_portal_install | ||
|
|
||
| - name: Enable captive_portal after copying files | ||
| service: name=captive_portal.service enabled=yes | ||
| when: py_captive_portal_install and py_captive_portal_enabled | ||
|
|
||
| - name: Start captive_portal after copying files | ||
| service: name=captive_portal.service state=started | ||
| when: py_captive_portal_install and py_captive_portal_enabled | ||
|
|
||
| - name: Disable captive_portal after copying files | ||
| service: name=captive_portal.service enabled=no | ||
| when: py_captive_portal_install and py_captive_portal_enabled | ||
|
|
||
| - name: Stop captive_portal after copying files | ||
| service: name=captive_portal.service state=started | ||
| when: py_captive_portal_install and py_captive_portal_enabled |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
roles/network/templates/captive_portal/captive_portal.py.j2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #!/usr/bin/python | ||
|
|
||
| # Captive portal script adapted from https://github.com/nikosft/captive-portal | ||
|
|
||
| import subprocess | ||
| import BaseHTTPServer | ||
| import cgi | ||
|
|
||
| # These variables are used as settings | ||
| PORT = int("{{ py_captive_portal_port }}") # the port in which the captive portal web server listens | ||
| IFACE = "{{ iiab_lan_iface }}" # the interface that captive portal protects | ||
| IP_ADDRESS = "{{ lan_ip }}" # the ip address of the captive portal (it can be the IP of IFACE) | ||
|
|
||
| ''' | ||
| This it the http server used by the the captive portal | ||
| ''' | ||
| class CaptivePortal(BaseHTTPServer.BaseHTTPRequestHandler): | ||
| #this is the index of the captive portal | ||
| #it simply redirects the user to the to login page | ||
| html_redirect = """ | ||
| <html> | ||
| <head> | ||
| <meta http-equiv="refresh" content="0; url=http://%s:%s/login" /> | ||
| </head> | ||
| <body> | ||
| <b>Redirecting to login page</b> | ||
| </body> | ||
| </html> | ||
| """%(IP_ADDRESS, PORT) | ||
| #the login page | ||
| html_login = """ | ||
| <html> | ||
| <body> | ||
| <b>Login Form</b> | ||
| <form method="POST" action="do_login"> | ||
| Username: <input type="text" name="username"><br> | ||
| Password: <input type="password" name="password"><br> | ||
| <input type="submit" value="Submit"> | ||
| </form> | ||
| </body> | ||
| </html> | ||
| """ | ||
|
|
||
| ''' | ||
| if the user requests the login page show it, else | ||
| use the redirect page | ||
| ''' | ||
| def do_GET(self): | ||
| path = self.path | ||
| self.send_response(200) | ||
| self.send_header("Content-type", "text/html") | ||
| self.end_headers() | ||
| if path == "/login": | ||
| self.wfile.write(self.html_login) | ||
| else: | ||
| self.wfile.write(self.html_redirect) | ||
| ''' | ||
| this is called when the user submits the login form | ||
| ''' | ||
| def do_POST(self): | ||
| self.send_response(200) | ||
| self.send_header("Content-type", "text/html") | ||
| self.end_headers() | ||
| form = cgi.FieldStorage( | ||
| fp=self.rfile, | ||
| headers=self.headers, | ||
| environ={'REQUEST_METHOD':'POST', | ||
| 'CONTENT_TYPE':self.headers['Content-Type'], | ||
| }) | ||
| username = form.getvalue("username") | ||
| password = form.getvalue("password") | ||
| #dummy security check | ||
| if username == '{{ py_captive_portal_username }}' and password == '{{ py_captive_portal_password }}': | ||
| #authorized user | ||
| remote_IP = self.client_address[0] | ||
| print 'New authorization from '+ remote_IP | ||
| print 'Updating IP tables' | ||
| subprocess.call(["iptables","-t", "nat", "-I", "PREROUTING","1", "-s", remote_IP, "-j" ,"ACCEPT"]) | ||
| subprocess.call(["iptables", "-I", "FORWARD", "-s", remote_IP, "-j" ,"ACCEPT"]) | ||
| self.wfile.write("You are now authorized. Navigate to any URL") | ||
| else: | ||
| #show the login form | ||
| self.wfile.write(self.html_login) | ||
|
|
||
| #the following function makes server produce no output | ||
| #comment it out if you want to print diagnostic messages | ||
| #def log_message(self, format, *args): | ||
| # return | ||
|
|
||
| print "Starting captive portal web server" | ||
| httpd = BaseHTTPServer.HTTPServer(('', PORT), CaptivePortal) | ||
|
|
||
| try: | ||
| httpd.serve_forever() | ||
| except KeyboardInterrupt: | ||
| pass | ||
| httpd.server_close() |
15 changes: 15 additions & 0 deletions
15
roles/network/templates/captive_portal/captive_portal.service.j2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [Unit] | ||
| Description=Captive portal | ||
| After=syslog.target | ||
|
|
||
| [Service] | ||
| Type=simple | ||
| User=root | ||
| Group=root | ||
| WorkingDirectory=/opt/iiab/captive-portal | ||
| ExecStart=/opt/iiab/captive-portal/captive_portal.py | ||
| StandardOutput=syslog | ||
| StandardError=syslog | ||
|
|
||
| [Install] | ||
| WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we place these 3 lines into our local_vars.yml files as well?