def anonConnect(ip='127.0.0.1', port=11234): from cjdnsadmin import connect path = os.path.expanduser('~/.cjdnsadmin') try: with open(path, 'r') as adminInfo: data = json.load(adminInfo) return connect(data['addr'], data['port'], '') except IOError: return connect(ip, int(port), '')
def connect(ip='',port=0,password=''): from cjdnsadmin import connect, connectWithAdminInfo try: cjdns = connect(ip,int(port), password) except: cjdns = connectWithAdminInfo() return cjdns
def anonConnect(ip='127.0.0.1', port=11234): from cjdnsadmin import connect return connect(ip, int(port), '')
#!/usr/bin/env python import os import sys import json import re from hashlib import sha512 try: from cjdnsadmin import connect,connectWithAdminInfo except ImportError: sys.path.insert(0, os.getenv("cjdnsadmin","/opt/cjdns/contrib/python/cjdnsadmin")) from cjdnsadmin import connect,connectWithAdminInfo if os.getenv("cjdns_password") is not None: cjdns = connect(os.getenv("cjdns_ip", "127.0.0.1"), int(os.getenv("cjdns_port", "11234")), os.getenv("cjdns_password")) else: cjdns = connectWithAdminInfo() try: cjdns.InterfaceController_peerStats() except AttributeError: print "InterfaceController_peerStats() not a function" print "Do you have an old version of cjdns?" print "possibly the stable-0.4 branch." sys.exit(1) ## Stolen from contrib/python/cjdnsadmin/publicToIp6.py ## # see util/Base32.h def Base32_decode(input):
#!/usr/bin/env python import sys, os try: from cjdnsadmin import connect, connectWithAdminInfo except ImportError: sys.path.append( os.getenv("cjdnsadmin", "/opt/cjdns/contrib/python/cjdnsadmin")) from cjdnsadmin import connect, connectWithAdminInfo if os.getenv("cjdns_password") is not None: cjdns = connect(os.getenv("cjdns_ip", "127.0.0.1"), int(os.getenv("cjdns_port", "11234")), os.getenv("cjdns_password")) else: cjdns = connectWithAdminInfo() config = False if len(sys.argv) > 1: if sys.argv[1] == "config": config = True allPeers = {} i = 0 while True: ps = cjdns.InterfaceController_peerStats(i) peers = ps['peers'] for p in peers: name = p['publicKey'][-10:] if "user" in p: name = p['user']
def main(): module = AnsibleModule( # not checking because of daisy chain to file module argument_spec=dict( authorizedPassword=dict(required=False), cjdroute=dict(required=False, default='/etc/cjdroute.conf'), autoclean=dict(requird=False, type='bool', default=True), udppeer=dict(required=False), state=dict(default='present', choices=['present', 'absent']), ) ) changed = False try: import cjdnsadmin params = module.params cjdroute = json.load(open(params['cjdroute'])) cjdns = cjdnsadmin.connect('127.0.0.1', 11234, cjdroute['admin']['password']) if params['authorizedPassword'] is not None: if not 'user' in params['authorizedPassword']: module.fail_json(msg='No user specified') elif not 'password' in params['authorizedPassword'] and params['state'] == 'present': module.fail_json(msg='No password specified') else: position = None for i in range(0, len(cjdroute['authorizedPasswords'])): passwordmatches = cjdroute['authorizedPasswords'][i]['password'] == params['authorizedPassword']['password'] usermatches = False if 'user' in cjdroute['authorizedPasswords'][i]: usermatches = cjdroute['authorizedPasswords'][i]['user'] == params['authorizedPassword']['user'] if passwordmatches or usermatches: position = i if params['state'] == 'present': if position is not None: if cjdroute['authorizedPasswords'][position]['user'] != params['authorizedPassword']['user']: cjdroute['authorizedPasswords'][position]['user'] = params['authorizedPassword']['user'] changed = True if cjdroute['authorizedPasswords'][position]['password'] != params['authorizedPassword']['password']: cjdroute['authorizedPasswords'][position]['password'] = params['authorizedPassword']['password'] changed = True else: cjdroute['authorizedPasswords'].append(params['authorizedPassword']) ipv6 = 0 if 'ipv6' in params['authorizedPassword']: ipv6 = params['authorizedPassword']['ipv6'] cjdns.AuthorizedPasswords_add(params['authorizedPassword']['password'], params['authorizedPassword']['user'], ipv6=ipv6) changed = True if params['state'] == 'absent' and position is not None: cjdroute['authorizedPasswords'].pop(position) cjdns.AuthorizedPasswords_remove(params['authorizedPassword']['user']) changed = True if params['udppeer'] is not None: if params['state'] == 'absent': if params['udppeer']['address'] in cjdroute['interfaces']['UDPInterface'][0]['connectTo']: del cjdroute['interfaces']['UDPInterface'][0]['connectTo'][params['udppeer']['address']] changed = True elif params['state'] == 'present': if params['udppeer']['address'] in cjdroute['interfaces']['UDPInterface'][0]['connectTo']: for key in params['udppeer']['data']: if key in cjdroute['interfaces']['UDPInterface'][0]['connectTo'][params['udppeer']['address']]: if params['udppeer']['data'][key] != cjdroute['interfaces']['UDPInterface'][0]['connectTo'][params['udppeer']['address']][key]: cjdroute['interfaces']['UDPInterface'][0]['connectTo'][params['udppeer']['address']][key] = params['udppeer']['data'][key] changed = True else: cjdroute['interfaces']['UDPInterface'][0]['connectTo'][params['udppeer']['address']][key] = params['udppeer']['data'][key] changed = True else: cjdroute['interfaces']['UDPInterface'][0]['connectTo'][params['udppeer']['address']] = params['udppeer']['data'] cjdns.UDPInterface_beginConnection( params['udppeer']['data']['publicKey'], params['udppeer']['address'], password=params['udppeer']['data']['password']) changed = True facts = {} if "ipv6" in cjdroute: facts['ip'] = cjdroute['ipv6'] if "publicKey" in cjdroute: facts['public_key'] = cjdroute['publicKey'] facts['UDPInterface'] = [] for interface in cjdroute['interfaces']['UDPInterface']: bind = interface['bind'].split(":") # This is gonna break on IPv6 udpiffact = { "port": bind[-1], "host": ":".join(bind[0:-1]) # This should handle IPv6 shit } facts['UDPInterface'].append(udpiffact) if changed: with open(params['cjdroute'], 'w') as cjdroutefile: json.dump(cjdroute, cjdroutefile, indent=4, separators=(',', ': '), sort_keys=True) module.exit_json(changed=changed, ansible_facts={"cjdns": facts}) except ImportError: module.fail_json(msg='Please install the cjdnsadmin python library') except IOError as e: module.fail_json(msg='Failed to open cjdroute.conf: %s' % e)
def cjdns_connect(process=0): if cjdns_use_default: return cjdnsadmin.connectWithAdminInfo() else: return cjdnsadmin.connect(cjdns_ip, cjdns_first_port + process, cjdns_password)