forked from NESS-Network/PrivatenessTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.py
More file actions
112 lines (87 loc) · 3.56 KB
/
backup.py
File metadata and controls
112 lines (87 loc) · 3.56 KB
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
import os
import sys
from base64 import b64encode
from base64 import b64decode
import json
import urllib.parse
from Crypto.Hash import SHA256
from Crypto.Random import get_random_bytes
import datetime
import requests
import uuid
import NessKeys.Prng as prng
from framework.Container import Container
from framework.ARGS import ARGS
from NessKeys.keys.Encrypted import Encrypted
from NessKeys.keys.Backup import Backup
class Backup:
def __manual(self):
print("*** PrivateNess keys BACKUP")
print("### Show backup SEED")
print(" ./backup seed")
print("### Show backup ADDRESS")
print(" ./backup address")
print("### Do backup to selected NODE or to FILE")
print(" ./backup backup [filename]")
print("### Restore backup from current NODE or from FILE")
print(" ./backup restore [filename]")
def process(self):
if ARGS.args(['seed']):
manager = Container.KeyManager()
bkey = manager.getBackupKey()
if bkey == False:
print ("Backup file not found")
else:
print(bkey.getSeed())
elif ARGS.args(['address']):
manager = Container.KeyManager()
bkey = manager.getBackupKey()
if bkey == False:
print ("Backup file not found")
else:
print(bkey.getAddress())
elif ARGS.args(['backup']):
fm = Container.FileManager()
manager = Container.KeyManager()
bkey = manager.getBackupKey()
if bkey.getType() == 'node':
ekey = manager.packKeysKey(manager.getLocalKeyFiles(), bkey.getCipher(), bytes(bkey.getKey(), 'utf8'))
shadowname = fm.uploadEncryptEncryptedKey(ekey)
fileinfo = fm.FilesService.fileinfo(shadowname)
bkey.setAddress(fileinfo['pub'])
print (" *** Backup link: " + fileinfo['pub'])
manager.saveKey(bkey)
else:
dt = datetime.datetime.now()
time = dt.strftime("%Y-%m-%d %H:%M:%S")
filename = "Backup " + time +".json"
manager.packKeys(manager.getLocalKeyFiles(), bkey.getCipher(), bytes(bkey.getKey(), 'utf8'), filename)
elif ARGS.args(['restore', str]):
manager = Container.KeyManager()
filename = sys.argv[2]
if not os.path.exists(filename):
print("File {} does not exist".format(filename))
return False
seed = input("Input seed:")
key = manager.KeyFromSeed(seed.strip())
if manager.unpackKeys(key, filename):
print (" *** Keys unpacked to {} directory".format(manager.directory))
elif ARGS.args(['restore']):
manager = Container.KeyManager()
address = input("Input address:")
seed = input("Input seed:")
key = manager.KeyFromSeed(seed.strip())
encr = requests.get(address, verify=False).content
try:
ekey_text = manager.unpack(encr, key)
except Exception as e:
print(" ~~~ Decryption error ~~~ ")
print("Check URL address and seed")
exit(1)
ekey = manager.EncrypedKeyFromString(ekey_text)
if manager.unpackKeysFromKey(bytes(key, 'utf-8'), ekey):
print (" *** Keys unpacked to {} directory".format(manager.directory))
else:
self.__manual()
backup = Backup()
backup.process()