2019-06-13 02:35:30 +00:00
|
|
|
'''
|
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
|
|
|
Use a communicator instance to announce our transport address to connected nodes
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
|
|
|
import base64
|
|
|
|
import onionrproofs, logger
|
|
|
|
from etc import onionrvalues
|
2019-06-26 00:15:04 +00:00
|
|
|
from onionrutils import basicrequests, bytesconverter
|
2019-07-24 17:22:19 +00:00
|
|
|
from utils import gettransports
|
2019-08-04 04:52:57 +00:00
|
|
|
from netcontroller import NetController
|
2019-07-10 22:38:20 +00:00
|
|
|
from communicator import onlinepeers
|
2019-07-18 17:40:48 +00:00
|
|
|
from coredb import keydb
|
2019-12-20 07:24:38 +00:00
|
|
|
import onionrexceptions
|
|
|
|
|
2019-06-13 02:35:30 +00:00
|
|
|
def announce_node(daemon):
|
|
|
|
'''Announce our node to our peers'''
|
2019-08-09 20:41:27 +00:00
|
|
|
ret_data = False
|
|
|
|
announce_fail = False
|
2019-06-13 02:35:30 +00:00
|
|
|
|
|
|
|
# Do not let announceCache get too large
|
|
|
|
if len(daemon.announceCache) >= 10000:
|
|
|
|
daemon.announceCache.popitem()
|
|
|
|
|
2019-07-18 17:40:48 +00:00
|
|
|
if daemon.config.get('general.security_level', 0) == 0:
|
2019-06-13 02:35:30 +00:00
|
|
|
# Announce to random online peers
|
|
|
|
for i in daemon.onlinePeers:
|
|
|
|
if not i in daemon.announceCache and not i in daemon.announceProgress:
|
|
|
|
peer = i
|
|
|
|
break
|
|
|
|
else:
|
2019-12-20 07:24:38 +00:00
|
|
|
try:
|
|
|
|
peer = onlinepeers.pick_online_peer(daemon)
|
|
|
|
except onionrexceptions.OnlinePeerNeeded:
|
|
|
|
peer = ""
|
2019-06-13 02:35:30 +00:00
|
|
|
|
2019-12-20 07:24:38 +00:00
|
|
|
for _ in range(1):
|
2019-07-24 17:22:19 +00:00
|
|
|
try:
|
|
|
|
ourID = gettransports.get()[0]
|
2019-12-20 07:24:38 +00:00
|
|
|
if not peer:
|
|
|
|
raise onionrexceptions.OnlinePeerNeeded
|
|
|
|
except (IndexError, onionrexceptions.OnlinePeerNeeded):
|
2019-07-24 17:22:19 +00:00
|
|
|
break
|
2019-06-13 02:35:30 +00:00
|
|
|
|
|
|
|
url = 'http://' + peer + '/announce'
|
|
|
|
data = {'node': ourID}
|
|
|
|
|
|
|
|
combinedNodes = ourID + peer
|
|
|
|
if ourID != 1:
|
2019-07-22 05:24:42 +00:00
|
|
|
existingRand = bytesconverter.bytes_to_str(keydb.transportinfo.get_address_info(peer, 'powValue'))
|
2019-06-13 02:35:30 +00:00
|
|
|
# Reset existingRand if it no longer meets the minimum POW
|
2019-08-09 20:41:27 +00:00
|
|
|
if type(existingRand) is type(None) or not existingRand.endswith('0' * onionrvalues.ANNOUNCE_POW):
|
2019-06-13 02:35:30 +00:00
|
|
|
existingRand = ''
|
|
|
|
|
|
|
|
if peer in daemon.announceCache:
|
2019-06-13 23:40:45 +00:00
|
|
|
data['random'] = daemon.announceCache[peer]
|
2019-06-13 02:35:30 +00:00
|
|
|
elif len(existingRand) > 0:
|
|
|
|
data['random'] = existingRand
|
|
|
|
else:
|
|
|
|
daemon.announceProgress[peer] = True
|
2019-08-14 00:12:45 +00:00
|
|
|
proof = onionrproofs.DataPOW(combinedNodes, minDifficulty=onionrvalues.ANNOUNCE_POW)
|
2019-06-13 02:35:30 +00:00
|
|
|
del daemon.announceProgress[peer]
|
|
|
|
try:
|
|
|
|
data['random'] = base64.b64encode(proof.waitForResult()[1])
|
|
|
|
except TypeError:
|
|
|
|
# Happens when we failed to produce a proof
|
|
|
|
logger.error("Failed to produce a pow for announcing to " + peer)
|
2019-08-09 20:41:27 +00:00
|
|
|
announce_fail = True
|
2019-06-13 02:35:30 +00:00
|
|
|
else:
|
|
|
|
daemon.announceCache[peer] = data['random']
|
2019-08-09 20:41:27 +00:00
|
|
|
if not announce_fail:
|
2019-06-13 02:35:30 +00:00
|
|
|
logger.info('Announcing node to ' + url)
|
2019-08-05 17:57:25 +00:00
|
|
|
if basicrequests.do_post_request(url, data, port=daemon.shared_state.get(NetController).socksPort) == 'Success':
|
2019-06-25 23:07:35 +00:00
|
|
|
logger.info('Successfully introduced node to ' + peer, terminal=True)
|
2019-08-09 20:41:27 +00:00
|
|
|
ret_data = True
|
2019-07-22 05:24:42 +00:00
|
|
|
keydb.transportinfo.set_address_info(peer, 'introduced', 1)
|
|
|
|
keydb.transportinfo.set_address_info(peer, 'powValue', data['random'])
|
2019-06-13 06:58:17 +00:00
|
|
|
daemon.decrementThreadCount('announce_node')
|
2019-08-09 20:41:27 +00:00
|
|
|
return ret_data
|