2019-12-23 07:51:24 +00:00
|
|
|
"""
|
2020-07-07 13:37:23 +00:00
|
|
|
Onionr - Private P2P Communication.
|
2019-06-13 02:35:30 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
Use a communicator instance to announce
|
|
|
|
our transport address to connected nodes
|
|
|
|
"""
|
2020-07-26 20:49:34 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
import logger
|
2020-07-07 13:37:23 +00:00
|
|
|
from onionrutils import basicrequests
|
2019-12-23 07:51:24 +00:00
|
|
|
from utils import gettransports
|
|
|
|
from netcontroller import NetController
|
|
|
|
from communicator import onlinepeers
|
|
|
|
from coredb import keydb
|
|
|
|
import onionrexceptions
|
2020-07-26 20:49:34 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from deadsimplekv import DeadSimpleKV
|
2019-12-23 07:51:24 +00:00
|
|
|
"""
|
2019-06-13 02:35:30 +00:00
|
|
|
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/>.
|
2019-12-23 07:51:24 +00:00
|
|
|
"""
|
|
|
|
|
2019-12-20 07:24:38 +00:00
|
|
|
|
2019-06-13 02:35:30 +00:00
|
|
|
def announce_node(daemon):
|
2019-12-23 07:51:24 +00:00
|
|
|
"""Announce our node to our peers."""
|
2019-08-09 20:41:27 +00:00
|
|
|
ret_data = False
|
2020-07-26 03:28:32 +00:00
|
|
|
kv: "DeadSimpleKV" = daemon.shared_state.get_by_string("DeadSimpleKV")
|
2019-12-23 07:51:24 +00:00
|
|
|
|
2019-06-13 02:35:30 +00:00
|
|
|
# Do not let announceCache get too large
|
2020-07-26 20:26:15 +00:00
|
|
|
if len(kv.get('announceCache')) >= 10000:
|
|
|
|
kv.get('announceCache').popitem()
|
2019-06-13 02:35:30 +00:00
|
|
|
|
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
|
2020-07-26 03:28:32 +00:00
|
|
|
for i in kv.get('onlinePeers'):
|
2020-07-27 02:45:17 +00:00
|
|
|
if i not in kv.get('announceCache'):
|
2019-06-13 02:35:30 +00:00
|
|
|
peer = i
|
|
|
|
break
|
|
|
|
else:
|
2019-12-20 07:24:38 +00:00
|
|
|
try:
|
2020-08-08 17:38:14 +00:00
|
|
|
peer = onlinepeers.pick_online_peer(kv)
|
2019-12-20 07:24:38 +00:00
|
|
|
except onionrexceptions.OnlinePeerNeeded:
|
|
|
|
peer = ""
|
2019-06-13 02:35:30 +00:00
|
|
|
|
2020-01-20 03:02:04 +00:00
|
|
|
try:
|
|
|
|
ourID = gettransports.get()[0]
|
|
|
|
if not peer:
|
|
|
|
raise onionrexceptions.OnlinePeerNeeded
|
|
|
|
except (IndexError, onionrexceptions.OnlinePeerNeeded):
|
|
|
|
pass
|
|
|
|
else:
|
2019-06-13 02:35:30 +00:00
|
|
|
url = 'http://' + peer + '/announce'
|
|
|
|
data = {'node': ourID}
|
|
|
|
|
2020-01-20 03:02:04 +00:00
|
|
|
logger.info('Announcing node to ' + url)
|
|
|
|
if basicrequests.do_post_request(
|
|
|
|
url,
|
|
|
|
data,
|
|
|
|
port=daemon.shared_state.get(NetController).socksPort)\
|
|
|
|
== 'Success':
|
|
|
|
logger.info('Successfully introduced node to ' + peer,
|
|
|
|
terminal=True)
|
|
|
|
ret_data = True
|
|
|
|
keydb.transportinfo.set_address_info(peer, 'introduced', 1)
|
2019-06-13 02:35:30 +00:00
|
|
|
|
2019-06-13 06:58:17 +00:00
|
|
|
daemon.decrementThreadCount('announce_node')
|
2019-12-23 07:51:24 +00:00
|
|
|
return ret_data
|