aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaotisk <kaotisk@arching-kaos.com>2021-03-08 16:55:57 +0200
committerkaotisk <kaotisk@arching-kaos.com>2021-03-08 16:55:57 +0200
commitb320459e483b2232f33e1bc28876bcb8419ee6bc (patch)
treeb192e26aab2a077211f78b2ff38b01fe7177f456
parentfa0751c25054edde3b627b93ab97b4f3620735e8 (diff)
downloadpython-cjdns-peering-tools-b320459e483b2232f33e1bc28876bcb8419ee6bc.tar.gz
python-cjdns-peering-tools-b320459e483b2232f33e1bc28876bcb8419ee6bc.tar.bz2
python-cjdns-peering-tools-b320459e483b2232f33e1bc28876bcb8419ee6bc.zip
Ready, I guess
-rw-r--r--README.md21
-rw-r--r--appendPeers.py16
-rwxr-xr-xgen.sh12
-rw-r--r--peer_info_generate.py49
4 files changed, 98 insertions, 0 deletions
diff --git a/README.md b/README.md
index 0881e1d..239f82d 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,22 @@
# python-cjdns-peering-tools
+
+A small collection of tools for generating and importing peering information through python scripts.
+
+## `appendPeers.py`
+It's the script that gets a `cjdroute.conf` and `peers.json` to append IP4 peers to configuration file.
+
+## `gen.sh`
+Different ways to generate a `cjdroute.conf` with no comments so we can work with it as JSON in Python.
+
+## `peer_info_generate.py`
+Script that creates a user and a password in `authorizedPasswords` and generates `peer_info_generated.json` file.
+
+## `peer_info_generated.json`
+This is a generated file by `peer_info_generate.py` which contains the peering information for someone to connect to your node.
+
+You should manually change `0.0.0.0` to your public IP4. #TODO Script for that
+
+## `peers.json`
+File with peering information.
+
+Can be used with https://github.com/hyperboria/peers
diff --git a/appendPeers.py b/appendPeers.py
new file mode 100644
index 0000000..cc75b9a
--- /dev/null
+++ b/appendPeers.py
@@ -0,0 +1,16 @@
+import json
+# function to add to JSON
+def write_json(data, filename='cjdroute.conf'):
+ with open(filename,'w') as f:
+ json.dump(data, f, indent=4)
+with open('cjdroute.conf') as json_file:
+ data = json.load(json_file)
+ temp = data['interfaces']
+ temp2 = temp['UDPInterface']
+ temp3 = temp2[0]
+ temp4 = temp3['connectTo']
+ with open('peers.json') as peers:
+ peers = json.load(peers)
+ temp4.update(peers)
+
+write_json(data)
diff --git a/gen.sh b/gen.sh
new file mode 100755
index 0000000..89140df
--- /dev/null
+++ b/gen.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+# Previous implementation
+#cjdroute --genconf > cjdroute.json
+#grep -v '^\s*//\|^\s*$' cjdroute.json > cjdroute-nc.json
+#perl -p0e 's!/\*.*?\*/!!sg' cjdroute-nc.json > cjdroute.conf
+
+# Nowadays :)
+#cjdroute --genconf | sed -n '/^\/\*.*\*\//!p' | sed -n '/ \/\/.*/!p' | sed 's|/\*|\n&|g;s|*/|&\n|g' | sed '/\/\*/,/*\//d' > cjdroute.conf
+
+# Futuristic way B-)
+# cjdroute --genconf | cjdroute --cleanconf > cjdroute.conf
+python appendPeers.py
diff --git a/peer_info_generate.py b/peer_info_generate.py
new file mode 100644
index 0000000..9e97cae
--- /dev/null
+++ b/peer_info_generate.py
@@ -0,0 +1,49 @@
+import json
+import random
+import string
+# function to add to JSON
+def write_json(data, filename='cjdroute.conf'):
+ with open(filename,'w') as f:
+ json.dump(data, f, indent=4)
+
+# function to output peer_info
+def write_info(data2, filename='peer_info_generated.json'):
+ with open(filename,'w') as g:
+ json.dump(data2, g, indent=4)
+
+# function to generate passwords
+def get_random_string(length):
+ # choose from all lowercase letter
+ letters = string.ascii_lowercase
+ return ''.join(random.choice(letters) for i in range(length))
+# print("Random string of length", length, "is:", result_str)
+
+with open('cjdroute.conf') as json_file:
+ data = json.load(json_file)
+# Store publicKey
+ publicKey = data['publicKey']
+# Store aythorized passwords
+ authorizedPasswords = data['authorizedPasswords']
+# Ask for a username
+ username = input("Enter username:")
+# Create a password
+ password = get_random_string(24)
+# Get block ready
+ adduser = {"password":password,"user":username}
+# Update JSON
+ authorizedPasswords.append(adduser)
+# Print dict
+ print(json.dumps(data))
+# Do write please
+ write_json(data)
+# Prepare the variables for peer_info_generation
+ temp = data['interfaces']
+ temp2 = temp['UDPInterface']
+ temp3 = temp2[0]
+ public_ip4 = temp3['bind']
+ peerOutput = {public_ip4: {"login":username,"password":password,"publicKey":publicKey}}
+# data2.append(peerOutput)
+# with open('peers.json') as peers:
+# peers = json.load(peers)
+# temp4.update(peers)
+ write_info(peerOutput)