Commit 4d956fea authored by Guillaume Bury's avatar Guillaume Bury

Small fixes

parent f4e39ebc
#!/usr/bin/env python #!/usr/bin/env python
import argparse, math, random, smtplib, sqlite3, string, time import argparse, math, random, smtplib, sqlite3, string, time
from email.mime.text import MIMEText from email.mime.text import MIMEText
from SimpleXMLRPCServer import SimpleXMLRPCServer from functools import wraps
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from OpenSSL import crypto from OpenSSL import crypto
import netaddr
import traceback import traceback
class RequestHandler(SimpleXMLRPCRequestHandler):
def _dispatch(self, method, params):
return self.server._dispatch(method, (self,) + params)
class main(object): class main(object):
def __init__(self): def __init__(self):
...@@ -60,11 +65,11 @@ class main(object): ...@@ -60,11 +65,11 @@ class main(object):
self.network = bin(self.ca.get_serial_number())[3:] self.network = bin(self.ca.get_serial_number())[3:]
# Starting server # Starting server
server = SimpleXMLRPCServer(("localhost", 8000), allow_none=True) server = SimpleXMLRPCServer(("localhost", 8000), requestHandler=RequestHandler, allow_none=True)
server.register_instance(self) server.register_instance(self)
server.serve_forever() server.serve_forever()
def requestToken(self, email): def requestToken(self, handler, email):
while True: while True:
# Generating token # Generating token
token = ''.join(random.sample(string.ascii_lowercase, 8)) token = ''.join(random.sample(string.ascii_lowercase, 8))
...@@ -96,7 +101,7 @@ class main(object): ...@@ -96,7 +101,7 @@ class main(object):
return prefix return prefix
raise RuntimeError # TODO: raise better exception raise RuntimeError # TODO: raise better exception
def requestCertificate(self, token, cert_req): def requestCertificate(self, handler, token, cert_req):
try: try:
req = crypto.load_certificate_request(crypto.FILETYPE_PEM, cert_req) req = crypto.load_certificate_request(crypto.FILETYPE_PEM, cert_req)
with self.db: with self.db:
...@@ -131,11 +136,19 @@ class main(object): ...@@ -131,11 +136,19 @@ class main(object):
traceback.print_exc() traceback.print_exc()
raise raise
def getCa(self): def getCa(self, handler):
return crypto.dump_certificate(crypto.FILETYPE_PEM, self.ca) return crypto.dump_certificate(crypto.FILETYPE_PEM, self.ca)
def getPeerList(self, n): def declare(self, handler, address):
# guess prefix from handler.client_address
ip1, ip2 = struct.unpack('>QQ', socket.inet_pton(socket.AF_INET6, handler.client_address)))
prefix = bin(ip1)[2:] + bin(ip2)]2:]
ip, port, proto = address
self.db.execute("INSERT INTO peers VALUES (?,?,?,?)", (prefix, ip, port, proto))
def getPeerList(self, handler, n, address):
assert 0 < n < 1000 assert 0 < n < 1000
self.declare(handler, address)
return self.db.execute("SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?", (n,)).fetchall() return self.db.execute("SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?", (n,)).fetchall()
......
#!/usr/bin/env python #!/usr/bin/env python
from OpenSSL import crypto from OpenSSL import crypto
import argparse, os, xmlrpclib import argparse, os, subprocess, xmlrpclib
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
...@@ -46,6 +46,9 @@ def main(): ...@@ -46,6 +46,9 @@ def main():
ca = s.getCa() ca = s.getCa()
cert = s.requestCertificate(token,req) cert = s.requestCertificate(token,req)
# Generating dh file
subprocess.call(['openssl', 'dhparam', '-out', os.path.join(config.dir, 'dh2048.pem'), '2048'])
# Store cert and key # Store cert and key
with open(os.path.join(config.dir, 'cert.key'), 'w') as f: with open(os.path.join(config.dir, 'cert.key'), 'w') as f:
f.write(key) f.write(key)
......
...@@ -36,7 +36,9 @@ class PeersDB: ...@@ -36,7 +36,9 @@ class PeersDB:
self.populateDB(100) self.populateDB(100)
def populateDB(self, n): def populateDB(self, n):
self.db.executemany("INSERT INTO peers (ip, port, proto) VALUES ?", self.proxy.getPeerList(n)) (ip, port) = upnpigd.GetExternalInfo(1194)
proto = 'udp'
self.db.executemany("INSERT INTO peers (ip, port, proto) VALUES ?", self.proxy.getPeerList(n, port, proto))
def getUnusedPeers(self, nPeers): def getUnusedPeers(self, nPeers):
return self.db.execute("SELECT id, ip, port, proto FROM peers WHERE used = 0 " return self.db.execute("SELECT id, ip, port, proto FROM peers WHERE used = 0 "
...@@ -191,6 +193,7 @@ def main(): ...@@ -191,6 +193,7 @@ def main():
# Get arguments # Get arguments
getConfig() getConfig()
log.verbose = config.verbose log.verbose = config.verbose
# TODO: get proto to use ?
(externalIp, externalPort) = upnpigd.GetExternalInfo(1194) (externalIp, externalPort) = upnpigd.GetExternalInfo(1194)
# Setup database # Setup database
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment