Skip to content

Instantly share code, notes, and snippets.

@campadrenalin
Created February 25, 2012 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save campadrenalin/1906867 to your computer and use it in GitHub Desktop.
Save campadrenalin/1906867 to your computer and use it in GitHub Desktop.
CJDNS config scripts
#!/usr/bin/python
# Removes comments and pretty-prints config
# Can be used as a JSON validator, also
import json
import re
import sys
confpath = "/etc/cjdroute.conf"
if len(sys.argv) > 1:
confpath = sys.argv[1]
print >> sys.stderr, "Editing "+confpath
def load_conf(confpath):
try:
f = open(confpath)
except:
print >> sys.stderr, confpath, "does not exist, run cjconf-make first"
quit(1)
raw = f.read()
del f
# Filter out comments
raw = re.sub("(\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+)|(//.*)", "", raw)
raw = re.sub("//[^\n]*", "", raw)
# Convert to Python obj
try:
config = json.loads(raw, strict=False)
except:
print >> sys.stderr, confpath, "does not contain valid JSON."
quit(1)
return config
config = load_conf(confpath)
def save(obj):
with open(confpath, 'w') as f:
json.dump(obj, f, indent=4)
def add(config):
ip = raw_input("IP address: ")
port = raw_input("Port: ")
password = raw_input("Password: ")
key = raw_input("Public Key: ")
trust = raw_input("Trust (leave blank for default): ")
confirm = raw_input("Add this peer? [Y|N]: ")
if confirm in ("Y", "y"):
new = {"password":password,
"authType":1,
"publicKey":key,
"trust":trust
}
addr = ip+":"+port
config['interfaces']['UDPInterface']['connectTo'][addr] = new
save(config)
return config
def remove(config):
print "Remove a peer:"
peers = ["Leave 'em all alone"]
for p in config['interfaces']['UDPInterface']['connectTo']:
peers.append(p)
for p in range(len(peers)):
print "["+str(p)+"] "+peers[p]
try:
response = int(raw_input("Enter the number of the peer to remove: "))
except ValueError:
print "Ha ha, very funny."
return config
if (response >0 and response < len(peers)):
del config['interfaces']['UDPInterface']['connectTo'][peers[response]]
save(config)
else:
print "And no peers are decapitated today."
return config
if len(sys.argv) == 3:
print >> sys.stderr, "Attempting to merge with", sys.argv[2]
source = load_conf(sys.argv[2])
for interface in source['interfaces']:
if not interface in config['interfaces']:
config['interfaces'][interface] = {}
config['interfaces'][interface]['bind'] = \
source['interfaces'][interface]['bind']
config['interfaces'][interface]['connectTo'].update(
source['interfaces'][interface]['connectTo'])
print json.dumps(config, indent = 4)
quit() # Done, successfully
suicidal = False
while not suicidal:
print "Current Peers:"
print json.dumps(config['interfaces']['UDPInterface']['connectTo'],
indent = 4)
response = raw_input("[A]dd a peer, [R]emove a peer, or [Q]uit? ")
if response == "Q":
suicidal = True
elif response == "A":
config = add(config)
elif response == "R":
config = remove(config)
else:
print "Unrecognized option"
#!/bin/sh
# Use a temp file, so if something goes wrong, old file is not clobbered
cjconf-add /etc/cjdroute.conf /etc/cjdroute.conf.default > /tmp/cjdroute.conf
mv /tmp/cjdroute.conf /etc/cjdroute.conf
#!/bin/sh
set -e
if [ -f /etc/cjdroute.conf ] && [ -s /etc/cjdroute.conf ]
then
echo "/etc/cjdroute.conf already exists"
else
echo "/etc/cjdroute.conf does not exist, making one"
cjdroute --genconf > /etc/cjdroute.conf
fi
#!/usr/bin/python
# Removes comments and pretty-prints config
# Can be used as a JSON validator, also
import json
import re
import sys
confpath = "/etc/cjdroute.conf"
if len(sys.argv) > 1:
confpath = sys.argv[1]
def load_conf(confpath):
try:
f = open(confpath)
except:
print >> sys.stderr, confpath, "does not exist, run cjconf-make first"
quit(1)
raw = f.read()
del f
# Filter out comments
raw = re.sub("(\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+)|(//.*)", "", raw)
raw = re.sub("//[^\n]*", "", raw)
# Convert to Python obj
try:
config = json.loads(raw, strict=False)
except:
print >> sys.stderr, confpath, "does not contain valid JSON."
quit(1)
return config
config = load_conf(confpath)
# Print out the result, prettily
print json.dumps(config, indent=4)
#!/bin/sh
set -e
if [ -e /etc/cjdroute.conf ]
then
rm /etc/cjdroute.conf
fi

Installation

Download all the other files in this gist to /usr/bin. You also want to either copy or symlink cjdroute into /usr/bin as well, with EITHER of the following commands, but not both:

sudo cp <full path to cjdroute> /usr/bin

sudo ln -s <full path to cjdroute> /usr/bin/cjdroute

Replace the bracketed sections with the actual path, of course. There should be no brackets in the commands you actually use. Unless you have really weird folder names.

To create an initial configuration with default peers, run:

sudo cjconf-make
sudo cjconf-add-default

cjconf-make

Creates a new config file at /etc/cjdroute.conf if one does not exist yet.

cjconf-remove

Removes the file /etc/cjdroute.conf if it exists. Full wipe, nothing left.

cjconf-print

See your current configuration as pretty-printed JSON. Strips out comments.

cjconf-add

Interactive peer list configuration. Called with no arguments, it interactively edits /etc/cjdroute.conf. Called with one, it uses that as a filename for a conf file to mess with, again, interactively.

Call it with two arguments, and it creates a merge of all the peers in both of them, and pipes the JSON result to stdout. See source code of cjconf-add-default for an example of how this is used.

cjconf-add-default

Merges /etc/cjdroute.conf with /etc/cjdroute.conf.default with cjconf-add, and save the result back into /etc/cjdroute.conf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment