fix formatting in onionrservices

This commit is contained in:
Kevin Froman 2020-08-19 23:05:06 -05:00
parent 05127dd3b7
commit 6089cc3c98
1 changed files with 30 additions and 25 deletions

View File

@ -1,9 +1,12 @@
''' """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
Onionr services provide the server component to direct connections Onionr services provide the server component to direct connections
''' """
''' import time
from . import connectionserver, bootstrapservice, serverexists
from onionrutils import stringvalidators, basicrequests
import config
"""
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -16,37 +19,39 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' """
import time
import stem
from . import connectionserver, bootstrapservice, serverexists
from onionrutils import stringvalidators, basicrequests
import config
server_exists = serverexists.server_exists server_exists = serverexists.server_exists
class OnionrServices: class OnionrServices:
''' """Create a client or server for connecting to peer interfaces."""
Create a client or server for connecting to peer interfaces
'''
def __init__(self): def __init__(self):
self.servers = {} self.servers = {}
self.clients = {} self.clients = {}
self.shutdown = False self.shutdown = False
def create_server(self, peer, address, comm_inst): def create_server(self, peer, address, comm_inst):
''' """
When a client wants to connect, contact their bootstrap address and tell them our Create a server for direct connections
ephemeral address for our service by creating a new ConnectionServer instance
''' 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') """
BOOTSTRAP_TRIES = 10 # How many times to attempt contacting the bootstrap server if not stringvalidators.validate_transport(address):
TRY_WAIT = 3 # Seconds to wait before trying bootstrap again 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
# HTTP is fine because .onion/i2p is encrypted/authenticated # HTTP is fine because .onion/i2p is encrypted/authenticated
base_url = 'http://%s/' % (address,) base_url = 'http://%s/' % (address,)
socks = config.get('tor.socksport') socks = config.get('tor.socksport')
for x in range(BOOTSTRAP_TRIES): for x in range(BOOTSTRAP_TRIES):
if basicrequests.do_get_request(base_url + 'ping', port=socks, ignoreAPI=True) == 'pong!': if basicrequests.do_get_request(
base_url + 'ping', port=socks, ignoreAPI=True) == 'pong!':
# if bootstrap sever is online, tell them our service address # if bootstrap sever is online, tell them our service address
connectionserver.ConnectionServer(peer, address, comm_inst=comm_inst) connectionserver.ConnectionServer(
peer, address, comm_inst=comm_inst)
else: else:
time.sleep(TRY_WAIT) time.sleep(TRY_WAIT)
else: else:
@ -55,7 +60,7 @@ class OnionrServices:
@staticmethod @staticmethod
def create_client(peer, comm_inst=None): def create_client(peer, comm_inst=None):
# Create ephemeral onion service to bootstrap connection to server # Create ephemeral onion service to bootstrap connection to server
if not comm_inst == None: if comm_inst is not None:
try: try:
return comm_inst.direct_connection_clients[peer] return comm_inst.direct_connection_clients[peer]
except KeyError: except KeyError: