Rev399, Urandom and Msgpack benchmark, Better random string generation, Never render page on OPTIONS request, Fix for Chrome browser socket hang on zeronet version update

This commit is contained in:
HelloZeroNet 2015-09-13 23:17:13 +02:00
parent 5c72030373
commit 8f63e4c421
9 changed files with 79 additions and 25 deletions

File diff suppressed because one or more lines are too long

View file

@ -8,7 +8,7 @@ class Config(object):
def __init__(self, argv): def __init__(self, argv):
self.version = "0.3.2" self.version = "0.3.2"
self.rev = 396 self.rev = 399
self.argv = argv self.argv = argv
self.action = None self.action = None
self.createParser() self.createParser()

View file

@ -13,6 +13,7 @@ from Debug import Debug
from Connection import Connection from Connection import Connection
from Config import config from Config import config
from Crypt import CryptConnection from Crypt import CryptConnection
from Crypt import CryptHash
class ConnectionServer: class ConnectionServer:
@ -36,10 +37,7 @@ class ConnectionServer:
self.bytes_sent = 0 self.bytes_sent = 0
# Bittorrent style peerid # Bittorrent style peerid
self.peer_id = "-ZN0%s-%s" % ( self.peer_id = "-ZN0%s-%s" % (config.version.replace(".", ""), CryptHash.random(12, "base64"))
config.version.replace(".", ""),
''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(12))
)
# Check msgpack version # Check msgpack version
if msgpack.version[0] == 0 and msgpack.version[1] < 4: if msgpack.version[0] == 0 and msgpack.version[1] < 4:

View file

@ -1,4 +1,6 @@
import hashlib import hashlib
import os
import base64
def sha1sum(file, blocksize=65536): def sha1sum(file, blocksize=65536):
@ -19,6 +21,15 @@ def sha512sum(file, blocksize=65536):
return hash.hexdigest()[0:64] # Truncate to 256bits is good enough return hash.hexdigest()[0:64] # Truncate to 256bits is good enough
def random(length=64, encoding="hex"):
if encoding == "base64": # Characters: A-Za-z0-9
hash = hashlib.sha512(os.urandom(256)).digest()
return base64.standard_b64encode(hash).replace("+", "").replace("/", "").replace("=", "")[0:length]
else: # Characters: a-f0-9 (faster)
return hashlib.sha512(os.urandom(256)).hexdigest()[0:length]
if __name__ == "__main__": if __name__ == "__main__":
import cStringIO as StringIO import cStringIO as StringIO
a = StringIO.StringIO() a = StringIO.StringIO()

View file

@ -24,6 +24,7 @@ from Worker import WorkerManager
from Debug import Debug from Debug import Debug
from Content import ContentManager from Content import ContentManager
from SiteStorage import SiteStorage from SiteStorage import SiteStorage
from Crypt import CryptHash
import SiteManager import SiteManager
@ -50,16 +51,12 @@ class Site:
self.content_manager = ContentManager(self) # Load contents self.content_manager = ContentManager(self) # Load contents
if not self.settings.get("auth_key"): # To auth user in site (Obsolete, will be removed) if not self.settings.get("auth_key"): # To auth user in site (Obsolete, will be removed)
self.settings["auth_key"] = ''.join( self.settings["auth_key"] = CryptHash.random()
random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(24)
)
self.log.debug("New auth key: %s" % self.settings["auth_key"]) self.log.debug("New auth key: %s" % self.settings["auth_key"])
self.saveSettings() self.saveSettings()
if not self.settings.get("wrapper_key"): # To auth websocket permissions if not self.settings.get("wrapper_key"): # To auth websocket permissions
self.settings["wrapper_key"] = ''.join( self.settings["wrapper_key"] = CryptHash.random()
random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(12)
)
self.log.debug("New wrapper key: %s" % self.settings["wrapper_key"]) self.log.debug("New wrapper key: %s" % self.settings["wrapper_key"])
self.saveSettings() self.saveSettings()

View file

@ -12,6 +12,7 @@ from Site import SiteManager
from User import UserManager from User import UserManager
from Plugin import PluginManager from Plugin import PluginManager
from Ui.UiWebsocket import UiWebsocket from Ui.UiWebsocket import UiWebsocket
from Crypt import CryptHash
status_texts = { status_texts = {
200: "200 OK", 200: "200 OK",
@ -48,6 +49,10 @@ class UiRequest(object):
path = re.sub("^http://zero[/]+", "/", path) # Remove begining http://zero/ for chrome extension path = re.sub("^http://zero[/]+", "/", path) # Remove begining http://zero/ for chrome extension
path = re.sub("^http://", "/", path) # Remove begining http for chrome extension .bit access path = re.sub("^http://", "/", path) # Remove begining http for chrome extension .bit access
if self.env["REQUEST_METHOD"] == "OPTIONS":
self.sendHeader()
return ""
if path == "/": if path == "/":
return self.actionIndex() return self.actionIndex()
elif path.endswith("favicon.ico"): elif path.endswith("favicon.ico"):
@ -265,9 +270,7 @@ class UiRequest(object):
# Create a new wrapper nonce that allows to get one html file without the wrapper # Create a new wrapper nonce that allows to get one html file without the wrapper
def getWrapperNonce(self): def getWrapperNonce(self):
wrapper_nonce = ''.join( wrapper_nonce = CryptHash.random()
random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(24)
)
self.server.wrapper_nonces.append(wrapper_nonce) self.server.wrapper_nonces.append(wrapper_nonce)
return wrapper_nonce return wrapper_nonce

View file

@ -1,6 +1,7 @@
import logging import logging
import time import time
import cgi import cgi
import socket
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
from gevent.pywsgi import WSGIHandler from gevent.pywsgi import WSGIHandler
@ -22,7 +23,6 @@ class UiWSGIHandler(WSGIHandler):
self.kwargs = kwargs self.kwargs = kwargs
def run_application(self): def run_application(self):
self.server.sockets[self.client_address] = self.socket
if "HTTP_UPGRADE" in self.environ: # Websocket request if "HTTP_UPGRADE" in self.environ: # Websocket request
try: try:
ws_handler = WebSocketHandler(*self.args, **self.kwargs) ws_handler = WebSocketHandler(*self.args, **self.kwargs)
@ -32,20 +32,21 @@ class UiWSGIHandler(WSGIHandler):
logging.error("UiWSGIHandler websocket error: %s" % Debug.formatException(err)) logging.error("UiWSGIHandler websocket error: %s" % Debug.formatException(err))
if config.debug: # Allow websocket errors to appear on /Debug if config.debug: # Allow websocket errors to appear on /Debug
import sys import sys
del self.server.sockets[self.client_address]
sys.modules["main"].DebugHook.handleError() sys.modules["main"].DebugHook.handleError()
else: # Standard HTTP request else: # Standard HTTP request
# print self.application.__class__.__name__
try: try:
super(UiWSGIHandler, self).run_application() super(UiWSGIHandler, self).run_application()
except Exception, err: except Exception, err:
logging.error("UiWSGIHandler error: %s" % Debug.formatException(err)) logging.error("UiWSGIHandler error: %s" % Debug.formatException(err))
if config.debug: # Allow websocket errors to appear on /Debug if config.debug: # Allow websocket errors to appear on /Debug
import sys import sys
del self.server.sockets[self.client_address]
sys.modules["main"].DebugHook.handleError() sys.modules["main"].DebugHook.handleError()
if self.client_address in self.server.sockets:
del self.server.sockets[self.client_address] def handle(self):
# Save socket to be able to close them properly on exit
self.server.sockets[self.client_address] = self.socket
super(UiWSGIHandler, self).handle()
del self.server.sockets[self.client_address]
class UiServer: class UiServer:
@ -131,12 +132,15 @@ class UiServer:
sock_closed = 0 sock_closed = 0
for sock in self.server.sockets.values(): for sock in self.server.sockets.values():
try: try:
sock._sock.close() sock.send("bye")
sock.close() sock.shutdown(socket.SHUT_RDWR)
#sock._sock.close()
#sock.close()
sock_closed += 1 sock_closed += 1
except Exception: except Exception, err:
pass self.log.debug("Http connection close error: %s" % err)
self.log.debug("Socket closed: %s" % sock_closed) self.log.debug("Socket closed: %s" % sock_closed)
time.sleep(0.1)
self.server.socket.close() self.server.socket.close()
self.server.stop() self.server.stop()

View file

@ -10,6 +10,7 @@ a { color: black }
.button { padding: 5px 10px; margin-left: 10px; background-color: #FFF85F; border-bottom: 2px solid #CDBD1E; border-radius: 2px; text-decoration: none; transition: all 0.5s; background-position: left center; } .button { padding: 5px 10px; margin-left: 10px; background-color: #FFF85F; border-bottom: 2px solid #CDBD1E; border-radius: 2px; text-decoration: none; transition: all 0.5s; background-position: left center; }
.button:hover { background-color: #FFF400; border-bottom: 2px solid #4D4D4C; transition: none } .button:hover { background-color: #FFF400; border-bottom: 2px solid #4D4D4C; transition: none }
.button:active { position: relative; top: 1px } .button:active { position: relative; top: 1px }
.button:focus { outline: none }
.button-Delete { background-color: #e74c3c; border-bottom-color: #c0392b; color: white } .button-Delete { background-color: #e74c3c; border-bottom-color: #c0392b; color: white }
.button-Delete:hover { background-color: #FF5442; border-bottom-color: #8E2B21 } .button-Delete:hover { background-color: #FF5442; border-bottom-color: #8E2B21 }

View file

@ -15,6 +15,7 @@ a { color: black }
.button { padding: 5px 10px; margin-left: 10px; background-color: #FFF85F; border-bottom: 2px solid #CDBD1E; -webkit-border-radius: 2px; -moz-border-radius: 2px; -o-border-radius: 2px; -ms-border-radius: 2px; border-radius: 2px ; text-decoration: none; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; -o-transition: all 0.5s; -ms-transition: all 0.5s; transition: all 0.5s ; background-position: left center; } .button { padding: 5px 10px; margin-left: 10px; background-color: #FFF85F; border-bottom: 2px solid #CDBD1E; -webkit-border-radius: 2px; -moz-border-radius: 2px; -o-border-radius: 2px; -ms-border-radius: 2px; border-radius: 2px ; text-decoration: none; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; -o-transition: all 0.5s; -ms-transition: all 0.5s; transition: all 0.5s ; background-position: left center; }
.button:hover { background-color: #FFF400; border-bottom: 2px solid #4D4D4C; -webkit-transition: none ; -moz-transition: none ; -o-transition: none ; -ms-transition: none ; transition: none } .button:hover { background-color: #FFF400; border-bottom: 2px solid #4D4D4C; -webkit-transition: none ; -moz-transition: none ; -o-transition: none ; -ms-transition: none ; transition: none }
.button:active { position: relative; top: 1px } .button:active { position: relative; top: 1px }
.button:focus { outline: none }
.button-Delete { background-color: #e74c3c; border-bottom-color: #c0392b; color: white } .button-Delete { background-color: #e74c3c; border-bottom-color: #c0392b; color: white }
.button-Delete:hover { background-color: #FF5442; border-bottom-color: #8E2B21 } .button-Delete:hover { background-color: #FF5442; border-bottom-color: #8E2B21 }