#-*- coding: utf-8 -*-
#
# :authors: Arturo Filastò, Isis Lovecruft
# :license: see included LICENSE file
import os
import sys
import xmlrpclib

from fabric.api import run, cd, sudo

api_auth = {}
# Set these values 
api_auth['Username'] = "you@example.com"
api_auth['AuthString'] = "your_password"
slice_name = "your_slice_name"

### Do not change this
api_auth['AuthMethod'] = "password"

def search_node(nfilter="*.cert.org.cn"):
    api_server = xmlrpclib.ServerProxy('https://www.planet-lab.org/PLCAPI/')
    if api_server.AuthCheck(api_auth):
        print "We are authenticated"
    else:
        print "We are not authenticated"
    node_filter = {'hostname': nfilter}
    return_fields = ['hostname', 'site_id']
    all_nodes = api_server.GetNodes(api_auth, node_filter, return_fields)
    print all_nodes

def add_node(nodeid):
    node_id = int(nodeid)
    api_server = xmlrpclib.ServerProxy('https://www.planet-lab.org/PLCAPI/')
    node_filter = {'node_id': node_id}
    return_fields = ['hostname', 'site_id']
    nodes = api_server.GetNodes(api_auth, node_filter, return_fields)
    print 'Adding nodes %s' % nodes
    api_server.AddNode(api_auth, node_id, slice_name)

def deployooniprobe(distro="debian"):
    """
    This is used to deploy ooni-probe on debian based systems.
    """
    run("git clone https://git.torproject.org/ooni-probe.git ooni-probe")
    cd("ooni-probe")
    if distro == "debian":
        sudo("apt-get install git-core python python-pip python-dev")
    else:
        print "The selected distro is not supported"
        print "The following commands may fail"
    run("virtualenv env")
    run("source env/bin/activate")
    run("pip install https://hg.secdev.org/scapy/archive/tip.zip")
    run("pip install -r requirements.txt")


@@ -131,18 +131,94 @@
 ort 
-xmlrpclib%0A
+yaml%0Aimport xmlrpclib%0Afrom StringIO import StringIO%0A%0Afrom fabric.operations import get
 %0Afro
@@ -250,16 +250,21 @@
 cd, sudo
+, env
 %0A%0Aapi_au
@@ -465,16 +465,150 @@
 sword%22%0A%0A
+env.user = 'root'%0Adef set_hosts(host_file):%0A    with open(host_file) as f:%0A        for host in f:%0A            env.hosts.append(host)%0A%0A
 def sear
@@ -1985,8 +1985,1396 @@
 .txt%22)%0A%0A
+def generate_bouncer_file(install_directory='/data/oonib/', bouncer_file=%22bouncer.yaml%22):%0A    output = StringIO()%0A    get(os.path.join(install_directory, 'oonib.conf'), output)%0A    output.seek(0)%0A    oonib_configuration = yaml.safe_load(output)%0A    %0A    output.truncate(0)%0A    get(os.path.join(oonib_configuration%5B'main'%5D%5B'tor_datadir'%5D, 'collector', 'hostname'),%0A        output)%0A    output.seek(0)%0A    collector_hidden_service = output.readlines()%5B0%5D.strip()%0A%0A    address = env.host%0A    test_helpers = %7B%0A            'dns': address + ':' + str(oonib_configuration%5B'helpers'%5D%5B'dns'%5D%5B'tcp_port'%5D),%0A            'ssl': 'https://' + address,%0A            'traceroute': address,%0A    %7D%0A    if oonib_configuration%5B'helpers'%5D%5B'tcp-echo'%5D%5B'port'%5D == 80:%0A        test_helpers%5B'tcp-echo'%5D = address%0A    else:%0A        test_helpers%5B'http-return-json-headers'%5D = 'http://' + address%0A%0A    bouncer_data = %7B%0A            'collector': %0A                %7B%0A                    'httpo://'+collector_hidden_service: %7B'test-helper': test_helpers%7D%0A                %7D%0A    %7D%0A    with open(bouncer_file) as f:%0A        old_bouncer_data = yaml.safe_load(f)%0A%0A    with open(bouncer_file, 'w+') as f:%0A        old_bouncer_data%5B'collector'%5D%5B'httpo://'+collector_hidden_service%5D = %7B%7D%0A        old_bouncer_data%5B'collector'%5D%5B'httpo://'+collector_hidden_service%5D%5B'test-helper'%5D = test_helpers%0A        yaml.dump(old_bouncer_data, f)%0A

Add script for harvesting collector and test helper addresses