Permalink
Browse files

added compression

git-svn-id: svn://forre.st/undns@1193 470744a7-cac9-478e-843e-5ec1b25c69e8
  • Loading branch information...
1 parent 1ae7539 commit b8f647806446daf42b9b2d4e0d24d0211b22a3dd forrest committed Mar 15, 2011
Showing with 46 additions and 48 deletions.
  1. +10 −9 packet.py
  2. +7 −6 server.py
  3. +6 −7 test.sh
  4. +10 −7 tool.py
  5. +13 −19 util.py
View
19 packet.py
@@ -1,5 +1,6 @@
import json
import hashlib
+import zlib
from Crypto.PublicKey import RSA
import twisted.names.common, twisted.names.client, twisted.names.dns, twisted.names.server, twisted.names.error, twisted.names.authority
@@ -20,8 +21,8 @@ def __init__(self, contents, origin):
class Packet(object):
@classmethod
- def from_json(cls, x, address=None, address_hash=None):
- d = json.loads(x)
+ def from_binary(cls, x, address=None, address_hash=None):
+ d = json.loads(zlib.decompress(x))
return cls(util.tuple_to_key(d['public_key']), d['zone_file'], d['signature'], address, address_hash)
def __init__(self, public_key, zone_file, signature, address=None, address_hash=None):
@@ -41,10 +42,10 @@ def __init__(self, public_key, zone_file, signature, address=None, address_hash=
if address is not None and self.get_address() != address:
raise ValueError("address not correct")
if address_hash is not None and self.get_address_hash() != address_hash:
- raise ValueError("address hash hot correct")
+ raise ValueError("address hash not correct")
- def to_json(self):
- return json.dumps(dict(public_key=util.key_to_tuple(self._public_key), zone_file=self._zone_file, signature=self._signature))
+ def to_binary(self):
+ return zlib.compress(json.dumps(dict(public_key=util.key_to_tuple(self._public_key), zone_file=self._zone_file, signature=self._signature)))
def get_address(self):
return self._address
@@ -64,17 +65,17 @@ def generate(cls, rng):
return cls(RSA.generate(1024, rng))
@classmethod
- def from_json(cls, x):
- return cls(util.tuple_to_key(json.loads(x)))
+ def from_binary(cls, x):
+ return cls(util.tuple_to_key(json.loads(zlib.decompress(x))))
def __init__(self, private_key):
if not private_key.has_private():
raise ValueError("key not private")
self._private_key = private_key
- def to_json(self):
- return json.dumps(util.key_to_tuple(self._private_key))
+ def to_binary(self):
+ return zlib.compress(json.dumps(util.key_to_tuple(self._private_key)))
def get_address(self):
return util.key_to_address(self._private_key.publickey())
View
13 server.py
@@ -5,7 +5,6 @@
import random
import hashlib
import optparse
-import json
import subprocess
import twisted.names.common, twisted.names.client, twisted.names.dns, twisted.names.server, twisted.names.error, twisted.names.authority
@@ -15,7 +14,6 @@
from twisted.python import failure
from entangled.kademlia import node, datastore
-import util
import packet
try:
@@ -38,6 +36,9 @@
parser.add_option("-d", "--dht-port", metavar="PORT",
help="use UDP port PORT to connect to other DHT nodes and listen for connections (if not specified a random high port is chosen)",
type="int", action="store", default=random.randrange(49152, 65536), dest="dht_port")
+#parser.add_option("-n", "--node", metavar="ADDR:PORT",
+# help="connect to existing DHT node at ADDR listening on UDP port PORT",
+# type="int", action="append", default=random.randrange(49152, 65536), dest="dht_port")
(options, args) = parser.parse_args()
print name
@@ -50,7 +51,7 @@ def parse(x):
return ip, int(port)
knownNodes = map(parse, args)
-packets = [packet.Packet.from_json(open(filename).read()) for filename in options.packet_filenames]
+packets = [packet.Packet.from_binary(open(filename).read()) for filename in options.packet_filenames]
# DHT
@@ -64,7 +65,7 @@ class UnDNSNode(node.Node):
def store(self, key, value, originalPublisherID=None, age=0, **kwargs):
print repr((self, key, value, originalPublisherID, age, kwargs))
- packet.Packet.from_json(value, address_hash=key) # will throw an exception if not valid
+ packet.Packet.from_binary(value, address_hash=key) # will throw an exception if not valid
node.Node.store(self, key, value, originalPublisherID, age, **kwargs)
@@ -76,7 +77,7 @@ def store(self, key, value, originalPublisherID=None, age=0, **kwargs):
def store(*args):
for packet in packets:
print "publishing", packet.get_address()
- n.iterativeStore(packet.get_address_hash(), packet.to_json())
+ n.iterativeStore(packet.get_address_hash(), packet.to_binary())
reactor.callLater(13.23324141, store)
n._joinDeferred.addCallback(store)
@@ -108,7 +109,7 @@ def callback(result):
return defer.fail(failure.Failure(names.dns.AuthoritativeDomainError(name)))
assert isinstance(result, dict), result
- packet = packet.Packet.from_json(result[name_hash])
+ packet = packet.Packet.from_binary(result[name_hash])
if packet.get_address() != name_alone:
return defer.fail(failure.Failure(names.dns.AuthoritativeDomainError(name)))
View
13 test.sh
@@ -1,22 +1,21 @@
#!/bin/bash
-python tool.py generate > $1.key
+time python tool.py generate > $1.key
echo key:
-cat $1.key
+time python tool.py view $1.key
echo
-python tool.py info $1.key > $1.url
echo url:
-cat $1.url
+time python tool.py info $1.key
echo
-python tool.py encode $1.key $1.zone > $1.packet
+time python tool.py encode $1.key $1.zone > $1.packet
echo packet:
-cat $1.packet
+time python tool.py view $1.packet
echo
echo decode:
-python tool.py decode $1.packet
+time python tool.py decode $1.packet
View
17 tool.py
@@ -1,31 +1,34 @@
import sys
import hashlib
-import json
from Crypto import Random
-import util
import packet
rng = Random.new().read
if sys.argv[1] == "generate":
- print packet.PrivateKey.generate(rng).to_json()
+ print packet.PrivateKey.generate(rng).to_binary()
elif sys.argv[1] == "info":
- private_key = packet.PrivateKey.from_json(open(sys.argv[2]).read())
+ private_key = packet.PrivateKey.from_binary(open(sys.argv[2]).read())
print private_key.get_address()
elif sys.argv[1] == "encode":
- private_key = packet.PrivateKey.from_json(open(sys.argv[2]).read())
+ private_key = packet.PrivateKey.from_binary(open(sys.argv[2]).read())
zone_file = open(sys.argv[3]).read()
- print private_key.encode(zone_file, rng).to_json()
+ print private_key.encode(zone_file, rng).to_binary()
elif sys.argv[1] == "decode":
- pkt = packet.Packet.from_json(open(sys.argv[2]).read())
+ pkt = packet.Packet.from_binary(open(sys.argv[2]).read())
print pkt.get_address()
print
print pkt.get_zone_file(),
+
+elif sys.argv[1] == "view":
+ import json
+ import zlib
+ print json.loads(zlib.decompress(open(sys.argv[2]).read()))
View
32 util.py
@@ -2,34 +2,28 @@
from Crypto.PublicKey import RSA
-def int_to_string(i, alphabet):
- res = []
- while True:
- res.append(i % len(alphabet))
- i //= len(alphabet)
- if not i:
- break
- return ''.join(alphabet[x] for x in reversed(res))
-
-def string_to_int(s, alphabet):
- acc = 0
- place_value = 1
- for char in s[::-1]:
- acc += place_value * alphabet.index(char)
- place_value *= len(alphabet)
- return acc
+alphabet = '0123456789abcdefghijklmnopqrstuvwxyz'
+
+def natural_to_string(n, alphabet):
+ if n < 0:
+ raise ValueError()
+ a, b = divmod(n, len(alphabet))
+ return (natural_to_string(a, alphabet) if a else "") + alphabet[b]
+
+def string_to_natural(s, alphabet):
+ if not s or (s != alphabet[0] and s.startswith(alphabet[0])):
+ raise ValueError()
+ return sum(alphabet.index(char) * len(alphabet)**i for i, char in enumerate(reversed(s)))
def key_to_name(k):
hash = int(hashlib.sha1(k.exportKey()).hexdigest(), 16)
- return int_to_string(hash, alphabet)
+ return natural_to_string(hash, alphabet)
def key_to_address(k):
return key_to_name(k) + ".undns.forre.st"
-alphabet = '0123456789abcdefghijklmnopqrstuvwxyz'
def key_to_tuple(k):
- # todo: store bitwidth(actually not needed, i think) and type in tuple
if k.has_private():
k = k.key
return (k.n, k.e, k.d, k.p, k.q, k.u)

0 comments on commit b8f6478

Please sign in to comment.