-
Notifications
You must be signed in to change notification settings - Fork 0
/
zeronet.py
118 lines (95 loc) · 3.26 KB
/
zeronet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import json
from config import data_directory
def guess_private_key():
try:
with open(data_directory + "/users.json", "r") as f:
users = json.loads(f.read())
user = users[users.keys()[0]]
zeroid = user["certs"]["zeroid.bit"]["auth_address"]
try:
zeromail = user["sites"]["1MaiL5gfBM1cyb4a8e3iiL8L5gXmoAJu27"]
keyname = [key for key in zeromail.keys() if "encrypt_privatekey" in key]
if len(keyname) != 1:
return (zeroid, None, None)
privatekey = zeromail[keyname[0]]
publickey = None
keyname = [key for key in zeromail.keys() if "encrypt_publickey" in key]
if len(keyname) == 0:
import cryptlib
publickey = cryptlib.private_to_public(privatekey)
elif len(keyname) != 1:
return (zeroid, None, None)
else:
publickey = zeromail[keyname[0]]
return (zeroid, publickey, privatekey)
except (KeyError, TypeError):
return (zeroid, None, None)
except (IOError, KeyError, TypeError):
return (None, None, None)
def guess_public_key(zeroid):
try:
with open(data_directory + "/1MaiL5gfBM1cyb4a8e3iiL8L5gXmoAJu27/data/users/" + zeroid + "/data.json") as f:
data = json.loads(f.read())
return data["publickey"]
except (IOError, KeyError, TypeError):
return None
def sign(address, content, privatekey=None):
if privatekey is None:
with open(data_directory + "/users.json") as f:
users = json.loads(f.read())
try:
user = users[users.keys()[0]]
privatekey = user["certs"]["zeroid.bit"]["auth_privatekey"]
except KeyError:
raise TypeError("Private key for zeroid.bit not found in users.json")
# Check for lock
from util import helper
try:
with helper.openLocked("%s/lock.pid" % data_directory, "w") as f:
pass
# Could get lock; let's run normal sitePublish then
from Site import Site
site = Site(address, allow_create=False)
site.content_manager.sign(
inner_path=content,
privatekey=privatekey,
update_changed_files=True,
remove_missing_optional=False
)
except IOError:
# Could not get lock
sign_socket(address, content, privatekey)
def sign_socket(address, content, privatekey):
# Sign file via ZeroWebSocket
# Find wrapper_key in sites.json
wrapper_key = None
with open(data_directory + "/sites.json", "r") as f:
sites = json.loads(f.read())
wrapper_key = sites[address]["wrapper_key"]
# Access WebSocket
from zerowebsocket import ZeroWebSocket
with ZeroWebSocket(wrapper_key) as ws:
ws.siteSign(inner_path=content, privatekey=privatekey, update_changed_files=True)
def publish(address, content):
# Check for lock
from util import helper
try:
with helper.openLocked("%s/lock.pid" % data_directory, "w") as f:
pass
# Could get lock; let's run normal sitePublish then
from src import main as zeronet_lib
zeronet_lib.actions.sitePublish(address, inner_path=content)
except IOError:
# Could not get lock
publish_socket(address, content)
def publish_socket(address, content):
# Publish file via ZeroWebSocket
# Find wrapper_key in sites.json
wrapper_key = None
with open(data_directory + "/sites.json", "r") as f:
sites = json.loads(f.read())
wrapper_key = sites[address]["wrapper_key"]
# Access WebSocket
from zerowebsocket import ZeroWebSocket
with ZeroWebSocket(wrapper_key) as ws:
ws.sitePublish(inner_path=content, sign=False)