forked from DigitalPrice/masternode-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
denarius.py
317 lines (265 loc) · 11.7 KB
/
denarius.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import Popen,PIPE,STDOUT
import collections
import os
import sys
import time
import math
import os
from urllib2 import urlopen
SERVER_IP = urlopen('http://ip.42.pl/raw').read()
BOOTSTRAP_URL = "https://gitlab.com/denarius/chaindata/raw/master/chaindata.zip"
DEFAULT_COLOR = "\x1b[0m"
PRIVATE_KEYS = []
def print_info(message):
BLUE = '\033[94m'
print(BLUE + "[*] " + str(message) + DEFAULT_COLOR)
time.sleep(1)
def print_warning(message):
YELLOW = '\033[93m'
print(YELLOW + "[*] " + str(message) + DEFAULT_COLOR)
time.sleep(1)
def print_error(message):
RED = '\033[91m'
print(RED + "[*] " + str(message) + DEFAULT_COLOR)
time.sleep(1)
def get_terminal_size():
import fcntl, termios, struct
h, w, hp, wp = struct.unpack('HHHH',
fcntl.ioctl(0, termios.TIOCGWINSZ,
struct.pack('HHHH', 0, 0, 0, 0)))
return w, h
def remove_lines(lines):
CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'
for l in lines:
sys.stdout.write(CURSOR_UP_ONE + '\r' + ERASE_LINE)
sys.stdout.flush()
def run_command(command):
out = Popen(command, stderr=STDOUT, stdout=PIPE, shell=True)
lines = []
while True:
line = out.stdout.readline()
if (line == ""):
break
# remove previous lines
remove_lines(lines)
w, h = get_terminal_size()
lines.append(line.strip().encode('string_escape')[:w-3] + "\n")
if(len(lines) >= 5):
del lines[0]
# print lines again
for l in lines:
sys.stdout.write('\r')
sys.stdout.write(l)
sys.stdout.flush()
remove_lines(lines)
out.wait()
def print_welcome():
os.system('clear')
print(" _____ _____ _____ ")
print(" /\ \ /\ \ /\ \ ")
print(" /::\ \ /::\____\ /::\ \ ")
print(" /::::\ \ /::::| | /::::\ \ ")
print(" /::::::\ \ /:::::| | /::::::\ \ ")
print(" /:::/\:::\ \ /::::::| | /:::/\:::\ \ ")
print(" /:::/ \:::\ \ /:::/|::| | /:::/__\:::\ \ ")
print(" /:::/ \:::\ \ /:::/ |::| | /::::\ \:::\ \ ")
print(" /:::/ / \:::\ \ /:::/ |::| | _____ /::::::\ \:::\ \ ")
print(" /:::/ / \:::\ ___\ /:::/ |::| |/\ \ /:::/\:::\ \:::\____\ ")
print("/:::/____/ \:::| |/:: / |::| /::\____\/:::/ \:::\ \:::| |")
print("\:::\ \ /:::|____|\::/ /|::| /:::/ /\::/ |::::\ /:::|____|")
print(" \:::\ \ /:::/ / \/____/ |::| /:::/ / \/____|:::::\/:::/ / ")
print(" \:::\ \ /:::/ / |::|/:::/ / |:::::::::/ / ")
print(" \:::\ /:::/ / |::::::/ / |::|\::::/ / ")
print(" \:::\ /:::/ / |:::::/ / |::| \::/____/ ")
print(" \:::\/:::/ / |::::/ / |::| ~| ")
print(" \::::::/ / /:::/ / |::| | ")
print(" \::::/ / /:::/ / \::| | ")
print(" \::/____/ \::/ / \:| | ")
print(" ~~ \/____/ \|___| ")
print("")
print_info("Denarius masternode(s) installer v1.337")
print("")
print("")
print("")
def update_system():
print_info("Updating the system...")
run_command("apt-get update")
# special install for grub
run_command('sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o DPkg::options::="--force-confdef" -o DPkg::options::="--force-confold" install grub-pc')
run_command("apt-get upgrade -y")
def chech_root():
print_info("Check root privileges")
user = os.getuid()
if user != 0:
print_error("This program requires root privileges. Run as root user.")
sys.exit(-1)
def secure_server():
print_info("Securing server...")
run_command("apt-get --assume-yes install ufw")
run_command("ufw allow ssh/tcp")
run_command("ufw limit ssh/tcp")
run_command("ufw allow 33339/tcp")
run_command("ufw allow 9999/tcp")
run_command("ufw --force enable")
def compile_wallet():
print_info("Allocating swap...")
run_command("fallocate -l 3G /swapfile")
run_command("chmod 600 /swapfile")
run_command("mkswap /swapfile")
run_command("swapon /swapfile")
f = open('/etc/fstab','r+b')
line = '/swapfile none swap sw 0 0 \n'
lines = f.readlines()
if (lines[-1] != line):
f.write(line)
f.close()
print_info("Installing wallet build dependencies...")
run_command("apt-get --assume-yes install git unzip build-essential libssl-dev libdb++-dev libboost-all-dev libcrypto++-dev libqrencode-dev libminiupnpc-dev libgmp-dev libgmp3-dev autoconf autogen automake libtool")
is_compile = True
if os.path.isfile('/usr/local/bin/denariusd'):
print_warning('Wallet already installed on the system')
is_compile = False
if is_compile:
print_info("Downloading wallet...")
run_command("rm -rf /opt/Denarius")
run_command("git clone https://github.com/carsenk/denarius /opt/Denarius")
print_info("Compiling wallet...")
run_command("chmod +x /opt/Denarius/src/leveldb/build_detect_platform")
run_command("chmod +x /opt/Denarius/src/secp256k1/autogen.sh")
run_command("cd /opt/Denarius/src/ && make -f makefile.unix USE_UPNP=-")
run_command("strip /opt/Denarius/src/denariusd")
run_command("cp /opt/Denarius/src/denariusd /usr/local/bin")
run_command("cd /opt/Denarius/src/ && make -f makefile.unix clean")
run_command("denariusd")
def get_total_memory():
return (os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES'))/(1024*1024)
def autostart_masternode(user):
job = "@reboot /usr/local/bin/denariusd\n"
p = Popen("crontab -l -u {} 2> /dev/null".format(user), stderr=STDOUT, stdout=PIPE, shell=True)
p.wait()
lines = p.stdout.readlines()
if job not in lines:
print_info("Cron job doesn't exist yet, adding it to crontab")
lines.append(job)
p = Popen('echo "{}" | crontab -u {} -'.format(''.join(lines), user), stderr=STDOUT, stdout=PIPE, shell=True)
p.wait()
def setup_first_masternode():
print_info("Setting up first masternode")
run_command("useradd --create-home -G sudo mn1")
os.system('su - mn1 -c "{}" '.format("denariusd -daemon &> /dev/null"))
print_info("Open your desktop wallet config file (%appdata%/Denarius/denarius.conf) and copy your rpc username and password! If it is not there create one! E.g.:\n\trpcuser=[SomeUserName]\n\trpcpassword=[DifficultAndLongPassword]")
global rpc_username
global rpc_password
rpc_username = raw_input("rpcuser: ")
rpc_password = raw_input("rpcpassword: ")
print_info("Open your wallet console (Help => Debug window => Console) and create a new masternode private key: masternode genkey")
masternode_priv_key = raw_input("masternodeprivkey: ")
PRIVATE_KEYS.append(masternode_priv_key)
config = """rpcuser={}
rpcpassword={}
rpcallowip=127.0.0.1
rpcport=32339
port=9999
server=1
listen=1
daemon=1
logtimestamps=1
mnconflock=1
masternode=1
masternodeaddr={}:9999
masternodeprivkey={}
""".format(rpc_username, rpc_password, SERVER_IP, masternode_priv_key)
print_info("Saving config file...")
f = open('/home/mn1/.denarius/denarius.conf', 'w')
f.write(config)
f.close()
print_info("Downloading chaindata.zip file...")
run_command('su - mn1 -c "{}" '.format("cd && wget --continue " + BOOTSTRAP_URL))
print_info("Unzipping chaindata.zip...")
filename = BOOTSTRAP_URL[BOOTSTRAP_URL.rfind('/')+1:]
run_command('su - mn1 -c "{}" '.format("cd && unzip -d .denarius -o " + filename))
run_command('rm /home/mn1/.denarius/peers.dat')
autostart_masternode('mn1')
os.system('su - mn1 -c "{}" '.format('denariusd -daemon &> /dev/null'))
print_warning("Masternode started syncing in the background...")
def setup_xth_masternode(xth):
print_info("Setting up {}th masternode".format(xth))
run_command("useradd --create-home -G sudo mn{}".format(xth))
run_command("rm -rf /home/mn{}/.denarius/".format(xth))
print_info('Copying wallet data from the first masternode...')
run_command("cp -rf /home/mn1/.denarius /home/mn{}/".format(xth))
run_command("sudo chown -R mn{}:mn{} /home/mn{}/.denarius".format(xth, xth, xth))
run_command("rm /home/mn{}/.denarius/peers.dat &> /dev/null".format(xth))
run_command("rm /home/mn{}/.denarius/wallet.dat &> /dev/null".format(xth))
print_info("Open your wallet console (Help => Debug window => Console) and create a new masternode private key: masternode genkey")
masternode_priv_key = raw_input("masternodeprivkey: ")
PRIVATE_KEYS.append(masternode_priv_key)
BASE_RPC_PORT = 32339
BASE_PORT = 9999
config = """rpcuser={}
rpcpassword={}
rpcallowip=127.0.0.1
rpcport={}
port={}
server=1
listen=1
daemon=1
logtimestamps=1
mnconflock=1
masternode=1
masternodeaddr={}:{}
masternodeprivkey={}
""".format(rpc_username, rpc_password, BASE_RPC_PORT + xth - 1, BASE_PORT + xth - 1, SERVER_IP, BASE_PORT + xth - 1, masternode_priv_key)
print_info("Saving config file...")
f = open('/home/mn{}/.denarius/denarius.conf'.format(xth), 'w')
f.write(config)
f.close()
autostart_masternode('mn'+str(xth))
os.system('su - mn{} -c "{}" '.format(xth, 'denariusd -daemon &> /dev/null'))
print_warning("Masternode started syncing in the background...")
def setup_masternodes():
memory = get_total_memory()
masternodes = int(math.floor(memory / 300))
print_info("This system is capable to run around {} masternodes. To support Denarius network only use one masternode per ip.".format(masternodes))
print_info("How much masternodes do you want to setup?")
masternodes = int(raw_input("Number of masternodes: "))
if masternodes >= 1:
setup_first_masternode()
for i in range(masternodes-1):
setup_xth_masternode(i+2)
def porologe():
mn_base_data = """
Alias: Masternode{}
IP: {}
Private key: {}
Transaction ID: [5k desposit transaction id. 'masternode outputs']
Transaction index: [5k desposit transaction index. 'masternode outputs']
--------------------------------------------------
"""
mn_data = ""
for idx, val in enumerate(PRIVATE_KEYS):
mn_data += mn_base_data.format(idx+1, SERVER_IP + ":" + str(9999 + idx), val)
imp = """Vs lbh sbhaq gur thvqr naq guvf fpevcg hfrshy pbafvqre gb fhccbeg zr.\n\tFLAK: FAbTfY8Rw7QhLpk5i2Ll1UsKxZbMMzmRlz\n\tOGP: 33PeQClZcpjWSlZGprIZGYWLYE8mOFfaJz\n\tRGU: 0k9n794240o456O8qQ5593n7r8q7NR92s4pn4Q9Q2s\n"""
print('')
print_info(
"""Masternodes setup finished!
\tWait until all masternodes are fully synced. To check the progress login the
\tmasternode account (su mnX, where X is the number of the masternode) and run
\tthe 'denariusd getinfo' to get actual block number. Go to
\thttps://denariusexplorer.org/ website to check the latest block number. After the
\tsyncronization is done add your masternodes to your desktop wallet.
Datas:""" + mn_data)
print_warning(imp.decode('rot13').decode('unicode-escape'))
def main():
print_welcome()
chech_root()
update_system()
secure_server()
compile_wallet()
setup_masternodes()
porologe()
if __name__ == "__main__":
main()