Created
July 12, 2019 13:01
-
-
Save juagargi/2696d0dd19ad189143a494188a9285ce to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import sys | |
import requests | |
import lxml.html | |
import json | |
import base64 | |
COORDINATOR_URL = 'http://localhost:8000' | |
DEDICATED = 'DEDICATED' | |
VM = 'VM' | |
def _extract_csrfmiddlewaretoken(htmltext): | |
h = lxml.html.fromstring(htmltext) | |
if len(h.body.forms) != 1: | |
raise Exception('We need exactly one form in the HTML') | |
return h.body.forms[0].fields['csrfmiddlewaretoken'] | |
class Scraper: | |
""" | |
This class facilitates login in the Coordinator and keeping track of | |
cookies and csrfmiddleware token, as well as the correct calls to create | |
ASes, retrieve information and configuration per AS. | |
""" | |
def __init__(self, username, password): | |
self.username = username | |
self.password = password | |
self.cookies = None | |
self.csrfmiddlewaretoken = None # from the forms; starts empty | |
self.ases = {} # initially empty until refresh is called | |
self._init_cookies() | |
self._login() | |
self.refresh() | |
def _init_cookies(self): | |
""" | |
Visits /registration/register/ and keeps the cookies | |
""" | |
url = '%s/registration/register/' % COORDINATOR_URL | |
r = requests.get(url) | |
r.raise_for_status() | |
if 'csrftoken' not in r.cookies: | |
raise Exception('Cannot find csrftoken in the cookies') | |
self.cookies = r.cookies | |
def _login(self): | |
""" | |
Login into the Coordinator. Updates cookies | |
""" | |
url = '%s/login/' % COORDINATOR_URL | |
data = { | |
'username': self.username, | |
'password': self.password, | |
'csrfmiddlewaretoken': self.cookies['csrftoken'], | |
} | |
r = requests.post(url, data=data, cookies=self.cookies, allow_redirects=False) | |
r.raise_for_status() | |
self.cookies = r.cookies | |
def refresh(self): | |
""" | |
Retrieves the current list of user ASes for this user | |
""" | |
url = '%s/api/user' % COORDINATOR_URL | |
r = requests.get(url, cookies=self.cookies) | |
r.raise_for_status() | |
s = r.content.decode('utf-8') | |
d = json.loads(s) | |
self.ases = d | |
def get_user_as_list(self): | |
return self.ases['ases'].keys() if self.ases else [] | |
def debug_list_user_ases(self): | |
""" | |
Should print the HTML contents of the user AS list page | |
""" | |
url = '%s/user/' % COORDINATOR_URL | |
r = requests.get(url, cookies=self.cookies) | |
print(r.status_code) | |
r.raise_for_status() | |
print(str(r.content)) | |
def create_as(self, ap_index, label, installation_type, use_vpn=True, public_ip=None, | |
public_port=50000, bind_ip=None, bind_port=None): | |
""" | |
Uses the stored credentials to authenticate an AS creation. There is no cookies update | |
:param int ap_index is {1 | 2 | 3 | 4} for APs in ISDs 17, 18, 19 or 20 | |
:param str label | |
:param str installation_type {DEDICATED | VM} | |
:param bool use_vpn | |
:param str public_ip | |
:param int public_port | |
:param str bind_ip | |
:param int bind_port | |
""" | |
url = '%s/user/as/add' % COORDINATOR_URL | |
r = requests.get(url, cookies=self.cookies) | |
r.raise_for_status() | |
self.csrfmiddlewaretoken = _extract_csrfmiddlewaretoken(r.content) | |
data = { | |
'csrfmiddlewaretoken': self.csrfmiddlewaretoken, | |
'attachment_point': ap_index, | |
'label': label, | |
'installation_type': installation_type, | |
'public_port': public_port, | |
'public_ip': public_ip if public_ip else '', | |
'bind_ip': bind_ip if bind_ip else '', | |
'bind_port': bind_port if bind_port else '', | |
} | |
if use_vpn: | |
data['use_vpn'] = 'on', | |
r = requests.post(url, data=data, cookies=self.cookies) | |
r.raise_for_status() | |
self.refresh() | |
def get_config(self, asid): | |
as_ = self.ases['ases'][asid] | |
if len(as_['hosts']) != 1: | |
raise Exception('Expected 1 hosts per AS, failed on %s' % asid) | |
pk, h = next(iter(as_['hosts'].items())) | |
uid = h['uid'] | |
secret = h['secret'] | |
url = '{coord}/api/host/{uid}/config'.format(coord=COORDINATOR_URL, uid=uid) | |
basictoken = base64.b64encode(('%s:%s' % (uid, secret)).encode('ascii')).decode('ascii') | |
r = requests.get(url, headers={'Authorization': 'Basic %s' % basictoken}) | |
r.raise_for_status() | |
return r.content | |
def main(args): | |
# example usage: | |
username = 'juagargi@gmail.com' | |
password = 'blah' | |
s = Scraper(username, password) | |
# s.create_as(1, 'with VPN, dedicated 2', DEDICATED) | |
# s.create_as(1, 'NO VPN, dedicated 2', DEDICATED, use_vpn=False, public_ip='1.2.3.4') | |
print(s.get_user_as_list()) | |
b = s.get_config('ffaa:1:63') | |
with open('/tmp/someas.tgz', 'wb') as f: | |
f.write(b) | |
print('_________________________________-') | |
print(s.ases['ases']['ffaa:1:c4f']) | |
if __name__ == '__main__': | |
main(sys.argv) | |
# steps with curl: | |
# [1] curl -v -c cookies.txt -b cookies.txt http://localhost:8000/registration/register/ | |
# [2] curl -v -c cookies.txt -b cookies.txt -d "username=juagargi@gmail.com&password=blah&csrfmiddlewaretoken=DsfVchpg819DucuahxCt6vdcIAUWRD1OS4cNBZVBoYFP12J7GclKvGb9DQ3bwLIg" http://localhost:8000/login/ | |
# [3] curl -v -b cookies.txt -X GET http://localhost:8000/user/as/add | |
# [4] curl -v -b cookies.txt -X POST -d "csrfmiddlewaretoken=aT5AfZrK367kHdxPNqbwQ9YE0BMFMBMLhVTLbofELKm8FrNwBGyD7umMmkGFTjAQ&attachment_point=1&label=bad+boi&installation_type=DEDICATED&use_vpn=on&public_ip=&public_port=50000&bind_ip=&bind_port=" http://localhost:8000/user/as/add |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment