Do not print sensitive data
[novacoin.git] / contrib / seeds / getseeds / getseeds.py
1 #!/usr/bin/python3
2
3 ###
4 ###  Automatic seeding over HTTP
5 ###  (C) 2016 Alex D.
6 ###
7 ###  http://www.gnu.org/licenses/agpl-3.0.en.html
8 ###
9
10 import os
11 import sys
12 import json
13 import random
14 import platform
15 import urllib.request
16
17 sources = ['http://dalexhz1.cloudapp.net', 'http://dalexhz2.cloudapp.net', 'http://dalexhz4.cloudapp.net', 'http://dalexhz5.cloudapp.net']
18 data = urllib.request.urlopen(random.choice(sources)).readall();
19
20 if 'norpc' in sys.argv:
21     for node in json.loads(data.decode()):
22         print (node)
23     sys.exit()
24
25 ###  This module is required to function properly: 
26 ###      https://github.com/jgarzik/python-bitcoinrpc
27
28 import errno
29 from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
30 from socket import error as socket_error
31
32 def confFile():
33     folder = ''
34     if platform.system() == 'Windows':
35         folder = os.path.join(os.path.join(os.environ['APPDATA'], 'NovaCoin'))
36     else:
37         if platform.system() == 'Darwin':
38             folder = os.path.expanduser('~/Library/Application Support/NovaCoin/')
39         else:
40             folder = os.path.expanduser('~/.novacoin')
41
42     return os.path.join(folder, 'novacoin.conf')
43
44 conf_path = confFile()
45 if not os.path.exists(conf_path):
46     parser.error('''Novacoin configuration file not found. Manually enter your RPC password.\r\n'''
47         '''If you actually haven't created a configuration file, you should create one at %s with the text like this:\r\n'''
48         '''\r\n'''
49         '''server=1\r\n'''
50         '''rpcuser=yourname\r\n'''
51         '''rpcpassword=%x\r\n'''
52         '''\r\n'''
53         '''Keep that password secret! After creating the file, restart Novacoin.''' % (conf_path, random.randrange(2**128)))
54
55 conf = open(conf_path, 'rb').read()
56 contents = {}
57
58 for line in conf.splitlines(True):
59     if line.startswith(b'#'):
60         line = line[:line.index(b'#')]
61     if not line.__contains__(b'='):
62         continue
63     k, v = line.split(b'=', 1)
64     contents[k.strip()] = v.strip()
65
66 if b'rpcpassword' not in contents.keys():
67     print('''RPC password is not found in the %s file.''' % (conf_path))
68     sys.exit()
69
70 if b'rpcuser' not in contents.keys():
71     print('''RPC user is not found in the %s file.''' % (conf_path))
72     sys.exit()
73
74 rpcuser = contents[b'rpcuser'].decode()
75 rpcpassword = contents[b'rpcpassword'].decode()
76 rpcport = 8344
77 rpclisten = '127.0.0.1'
78
79 if b'rpcport' in contents.keys():
80     rpcport = contents[b'rpcport'].decode()
81 if b'rpclisten' in contents.keys():
82     rpclisten = contents[b'rpclisten'].decode()
83
84 access = AuthServiceProxy("http://"+rpcuser+":"+rpcpassword+"@"+rpclisten+":"+rpcport+"/")
85
86 for node in json.loads(data.decode()):
87     print ('Adding', node)
88     try:
89         access.addnode(node, 'add')
90     except socket_error as e:
91         if e.errno == errno.ECONNREFUSED:
92             print ('Unable to communicate with Novacoin RPC')
93         break
94     except JSONRPCException as e:
95         if e.code == -23:
96             print ('Already added')
97             continue
98         break