2022-02-10 01:29:16 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
|
|
|
This default plugin handles "flow" messages
|
|
|
|
(global chatroom style communication)
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import locale
|
2022-02-21 21:15:26 +00:00
|
|
|
from time import sleep
|
2022-02-17 05:58:04 +00:00
|
|
|
import traceback
|
2022-02-14 23:47:54 +00:00
|
|
|
from typing import Set, TYPE_CHECKING
|
|
|
|
import base64
|
2022-02-21 21:15:26 +00:00
|
|
|
from threading import Thread
|
2022-02-10 01:29:16 +00:00
|
|
|
|
2022-02-16 06:49:32 +00:00
|
|
|
import stem
|
2022-02-14 23:47:54 +00:00
|
|
|
from stem.control import Controller
|
|
|
|
|
|
|
|
import logger
|
|
|
|
from utils import readstatic
|
|
|
|
import config
|
|
|
|
from filepaths import gossip_server_socket_file
|
|
|
|
|
|
|
|
from gossip.peer import Peer
|
2022-02-10 01:29:16 +00:00
|
|
|
|
|
|
|
locale.setlocale(locale.LC_ALL, '')
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
# import after path insert
|
2022-02-14 23:47:54 +00:00
|
|
|
import starttor
|
2022-02-17 05:58:04 +00:00
|
|
|
from torpeer import TorPeer
|
2022-02-14 23:47:54 +00:00
|
|
|
from torfilepaths import control_socket
|
2022-02-26 07:07:18 +00:00
|
|
|
|
|
|
|
from bootstrap import on_bootstrap
|
|
|
|
from announce import on_announce_rec
|
2022-02-10 01:29:16 +00:00
|
|
|
|
|
|
|
"""
|
2022-02-14 23:47:54 +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/>.
|
2022-02-10 01:29:16 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
plugin_name = 'tor'
|
|
|
|
PLUGIN_VERSION = '0.0.0'
|
|
|
|
|
|
|
|
|
|
|
|
class OnionrTor:
|
|
|
|
def __init__(self):
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def on_init(api, data=None):
|
2022-02-14 23:47:54 +00:00
|
|
|
logger.info(
|
|
|
|
f"Tor Transport Plugin v{PLUGIN_VERSION} enabled", terminal=True)
|
|
|
|
|
|
|
|
|
2022-02-25 07:02:04 +00:00
|
|
|
def on_get_our_transport(api, data=None):
|
|
|
|
callback_func = data['callback']
|
|
|
|
for_peer = data['peer']
|
|
|
|
if data['peer'].__class__ == TorPeer:
|
|
|
|
callback_func(for_peer, config.get('tor.transport_address'))
|
|
|
|
|
|
|
|
|
2022-02-17 05:58:04 +00:00
|
|
|
def on_gossip_start(api, data: Set[Peer] = None):
|
|
|
|
# We don't do gossip logic
|
|
|
|
|
2022-02-14 23:47:54 +00:00
|
|
|
starttor.start_tor()
|
|
|
|
|
|
|
|
with Controller.from_socket_file(control_socket) as controller:
|
|
|
|
controller.authenticate()
|
2022-02-16 06:49:32 +00:00
|
|
|
logger.info(
|
|
|
|
"Tor socks is listening on " +
|
2022-03-02 13:29:59 +00:00
|
|
|
f"{controller.get_listeners('SOCKS')[0]}", terminal=True)
|
2022-02-14 23:47:54 +00:00
|
|
|
key = config.get('tor.key')
|
|
|
|
new_address = ''
|
|
|
|
if not key:
|
|
|
|
add_onion_resp = controller.create_ephemeral_hidden_service(
|
|
|
|
{'80': f'unix:{gossip_server_socket_file}'},
|
2022-02-21 21:15:26 +00:00
|
|
|
key_content='BEST', key_type='NEW', detached=True)
|
2022-02-14 23:47:54 +00:00
|
|
|
config.set('tor.key', add_onion_resp.private_key, savefile=True)
|
|
|
|
new_address = 'Generated '
|
2022-02-21 21:15:26 +00:00
|
|
|
config.set('tor.transport_address', add_onion_resp.service_id,
|
|
|
|
savefile=True)
|
2022-02-14 23:47:54 +00:00
|
|
|
else:
|
2022-02-16 06:49:32 +00:00
|
|
|
try:
|
|
|
|
add_onion_resp = controller.create_ephemeral_hidden_service(
|
|
|
|
{'80': f'unix:{gossip_server_socket_file}'},
|
2022-02-21 21:15:26 +00:00
|
|
|
key_content=key, key_type='ED25519-V3', detached=True)
|
2022-02-16 06:49:32 +00:00
|
|
|
except stem.ProtocolError:
|
|
|
|
logger.error(
|
|
|
|
"Could not start Tor transport. Try restarting Onionr",
|
|
|
|
terminal=True)
|
|
|
|
config.set('tor.key', '', savefile=True)
|
|
|
|
return
|
2022-02-14 23:47:54 +00:00
|
|
|
logger.info(
|
|
|
|
f'{new_address}Tor transport address {add_onion_resp.service_id}' +
|
|
|
|
'.onion',
|
|
|
|
terminal=True)
|