Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
This commit is contained in:
parent
c9578e9037
commit
e9d2cdfd37
99 changed files with 9476 additions and 267 deletions
|
@ -4,8 +4,12 @@ from lib.PySocks import socks
|
|||
|
||||
|
||||
def create_connection(address, timeout=None, source_address=None):
|
||||
sock = socks.socksocket()
|
||||
sock.connect(address)
|
||||
if address == "127.0.0.1":
|
||||
sock = socket.socket_noproxy(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect(address)
|
||||
else:
|
||||
sock = socks.socksocket()
|
||||
sock.connect(address)
|
||||
return sock
|
||||
|
||||
|
||||
|
@ -14,9 +18,9 @@ def getaddrinfo(*args):
|
|||
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
|
||||
|
||||
|
||||
def monkeyPath(proxy_ip, proxy_port):
|
||||
print proxy_ip, proxy_port
|
||||
def monkeyPatch(proxy_ip, proxy_port):
|
||||
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_ip, int(proxy_port))
|
||||
socket.socket_noproxy = socket.socket
|
||||
socket.socket = socks.socksocket
|
||||
socket.create_connection = create_connection
|
||||
socket.getaddrinfo = getaddrinfo
|
||||
|
|
|
@ -36,7 +36,10 @@ def _m_search_ssdp(local_ip):
|
|||
sock.bind((local_ip, 10000))
|
||||
|
||||
sock.sendto(ssdp_request, ('239.255.255.250', 1900))
|
||||
sock.settimeout(5)
|
||||
if local_ip == "127.0.0.1":
|
||||
sock.settimeout(1)
|
||||
else:
|
||||
sock.settimeout(5)
|
||||
|
||||
try:
|
||||
return sock.recv(2048)
|
||||
|
@ -233,6 +236,9 @@ def open_port(port=15441, desc="UpnpPunch"):
|
|||
if __name__ == "__main__":
|
||||
from gevent import monkey
|
||||
monkey.patch_socket()
|
||||
import time
|
||||
|
||||
s = time.time()
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
print open_port(15441, "ZeroNet")
|
||||
print "Done in", time.time()-s
|
||||
|
|
|
@ -4,18 +4,41 @@ import struct
|
|||
import re
|
||||
import collections
|
||||
import time
|
||||
import logging
|
||||
import base64
|
||||
|
||||
|
||||
def atomicWrite(dest, content, mode="w"):
|
||||
with open(dest + "-new", mode) as f:
|
||||
f.write(content)
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
if os.path.isfile(dest + "-old"): # Previous incomplete write
|
||||
os.rename(dest + "-old", dest + "-old-%s" % time.time())
|
||||
os.rename(dest, dest + "-old")
|
||||
os.rename(dest + "-new", dest)
|
||||
os.unlink(dest + "-old")
|
||||
try:
|
||||
with open(dest + "-new", mode) as f:
|
||||
f.write(content)
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
if os.path.isfile(dest + "-old"): # Previous incomplete write
|
||||
os.rename(dest + "-old", dest + "-old-%s" % time.time())
|
||||
os.rename(dest, dest + "-old")
|
||||
os.rename(dest + "-new", dest)
|
||||
os.unlink(dest + "-old")
|
||||
return True
|
||||
except Exception, err:
|
||||
from Debug import Debug
|
||||
logging.error(
|
||||
"File %s write failed: %s, reverting..." %
|
||||
(dest, Debug.formatException(err))
|
||||
)
|
||||
if os.path.isfile(dest + "-old") and not os.path.isfile(dest):
|
||||
os.rename(dest + "-old", dest)
|
||||
return False
|
||||
|
||||
|
||||
def openLocked(path, mode="w"):
|
||||
if os.name == "posix":
|
||||
import fcntl
|
||||
f = open(path, mode)
|
||||
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
else:
|
||||
f = open(path, mode)
|
||||
return f
|
||||
|
||||
|
||||
def shellquote(*args):
|
||||
|
@ -25,6 +48,16 @@ def shellquote(*args):
|
|||
return tuple(['"%s"' % arg.replace('"', "") for arg in args])
|
||||
|
||||
|
||||
def packPeers(peers):
|
||||
packed_peers = {"ip4": [], "onion": []}
|
||||
for peer in peers:
|
||||
if peer.ip.endswith(".onion"):
|
||||
packed_peers["onion"].append(peer.packMyAddress())
|
||||
else:
|
||||
packed_peers["ip4"].append(peer.packMyAddress())
|
||||
return packed_peers
|
||||
|
||||
|
||||
# ip, port to packed 6byte format
|
||||
def packAddress(ip, port):
|
||||
return socket.inet_aton(ip) + struct.pack("H", port)
|
||||
|
@ -32,9 +65,21 @@ def packAddress(ip, port):
|
|||
|
||||
# From 6byte format to ip, port
|
||||
def unpackAddress(packed):
|
||||
assert len(packed) == 6, "Invalid length ip4 packed address: %s" % len(packed)
|
||||
return socket.inet_ntoa(packed[0:4]), struct.unpack_from("H", packed, 4)[0]
|
||||
|
||||
|
||||
# onion, port to packed 12byte format
|
||||
def packOnionAddress(onion, port):
|
||||
onion = onion.replace(".onion", "")
|
||||
return base64.b32decode(onion.upper()) + struct.pack("H", port)
|
||||
|
||||
|
||||
# From 12byte format to ip, port
|
||||
def unpackOnionAddress(packed):
|
||||
return base64.b32encode(packed[0:-2]).lower() + ".onion", struct.unpack("H", packed[-2:])[0]
|
||||
|
||||
|
||||
# Get dir from file
|
||||
# Return: data/site/content.json -> data/site
|
||||
def getDirname(path):
|
||||
|
@ -62,3 +107,34 @@ def mergeDicts(dicts):
|
|||
for key, val in d.iteritems():
|
||||
back[key].update(val)
|
||||
return dict(back)
|
||||
|
||||
|
||||
# Request https url using gevent SSL error workaround
|
||||
def httpRequest(url, as_file=False):
|
||||
if url.startswith("http://"):
|
||||
import urllib
|
||||
response = urllib.urlopen(url)
|
||||
else: # Hack to avoid Python gevent ssl errors
|
||||
import socket
|
||||
import httplib
|
||||
import ssl
|
||||
|
||||
host, request = re.match("https://(.*?)(/.*?)$", url).groups()
|
||||
|
||||
conn = httplib.HTTPSConnection(host)
|
||||
sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address)
|
||||
conn.sock = ssl.wrap_socket(sock, conn.key_file, conn.cert_file)
|
||||
conn.request("GET", request)
|
||||
response = conn.getresponse()
|
||||
|
||||
if as_file:
|
||||
import cStringIO as StringIO
|
||||
data = StringIO.StringIO()
|
||||
while True:
|
||||
buff = response.read(1024 * 16)
|
||||
if not buff:
|
||||
break
|
||||
data.write(buff)
|
||||
return data
|
||||
else:
|
||||
return response
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue