2020-08-20 04:05:06 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-05-12 14:21:17 +00:00
|
|
|
|
2020-08-20 04:05:06 +00:00
|
|
|
Onionr services provide the server component to direct connections
|
|
|
|
"""
|
|
|
|
import time
|
|
|
|
from . import connectionserver, bootstrapservice, serverexists
|
|
|
|
from onionrutils import stringvalidators, basicrequests
|
|
|
|
import config
|
|
|
|
"""
|
2019-05-12 14:21:17 +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/>.
|
2020-08-20 04:05:06 +00:00
|
|
|
"""
|
|
|
|
|
2019-08-21 08:03:24 +00:00
|
|
|
server_exists = serverexists.server_exists
|
2020-08-20 04:05:06 +00:00
|
|
|
|
|
|
|
|
2019-03-18 05:22:31 +00:00
|
|
|
class OnionrServices:
|
2020-08-20 04:05:06 +00:00
|
|
|
"""Create a client or server for connecting to peer interfaces."""
|
2019-07-19 19:49:56 +00:00
|
|
|
def __init__(self):
|
2019-03-18 05:22:31 +00:00
|
|
|
self.servers = {}
|
|
|
|
self.clients = {}
|
2019-03-19 05:09:53 +00:00
|
|
|
self.shutdown = False
|
2020-08-20 04:05:06 +00:00
|
|
|
|
2019-07-28 05:33:26 +00:00
|
|
|
def create_server(self, peer, address, comm_inst):
|
2020-08-20 04:05:06 +00:00
|
|
|
"""
|
|
|
|
Create a server for direct connections
|
|
|
|
|
|
|
|
When a client wants to connect, contact their bootstrap address and tell them our ephemeral address for our service by creating a new ConnectionServer instance
|
|
|
|
"""
|
|
|
|
if not stringvalidators.validate_transport(address):
|
|
|
|
raise ValueError('address must be valid')
|
|
|
|
# How many times to attempt contacting the bootstrap server
|
|
|
|
BOOTSTRAP_TRIES = 10
|
|
|
|
# Seconds to wait before trying bootstrap again
|
|
|
|
TRY_WAIT = 3
|
2019-05-15 23:25:36 +00:00
|
|
|
# HTTP is fine because .onion/i2p is encrypted/authenticated
|
2019-03-25 03:32:17 +00:00
|
|
|
base_url = 'http://%s/' % (address,)
|
2019-07-19 19:49:56 +00:00
|
|
|
socks = config.get('tor.socksport')
|
2019-03-19 05:09:53 +00:00
|
|
|
for x in range(BOOTSTRAP_TRIES):
|
2020-08-20 04:05:06 +00:00
|
|
|
if basicrequests.do_get_request(
|
|
|
|
base_url + 'ping', port=socks, ignoreAPI=True) == 'pong!':
|
2019-05-15 23:25:36 +00:00
|
|
|
# if bootstrap sever is online, tell them our service address
|
2020-08-20 04:05:06 +00:00
|
|
|
connectionserver.ConnectionServer(
|
|
|
|
peer, address, comm_inst=comm_inst)
|
2019-03-19 05:09:53 +00:00
|
|
|
else:
|
|
|
|
time.sleep(TRY_WAIT)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-08-16 21:28:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def create_client(peer, comm_inst=None):
|
2019-08-25 08:08:09 +00:00
|
|
|
# Create ephemeral onion service to bootstrap connection to server
|
2020-08-20 04:05:06 +00:00
|
|
|
if comm_inst is not None:
|
2019-08-21 08:03:24 +00:00
|
|
|
try:
|
|
|
|
return comm_inst.direct_connection_clients[peer]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2019-08-16 22:40:17 +00:00
|
|
|
address = bootstrapservice.bootstrap_client_service(peer, comm_inst)
|
|
|
|
return address
|