Raise an error if we were unable to obtain a random port

This commit is contained in:
Miguel Jacq 2017-12-07 08:49:10 +11:00
parent b94c671911
commit 0aacfae925
No known key found for this signature in database
GPG Key ID: EEA4341C6D97A0B6
5 changed files with 27 additions and 9 deletions

View File

@ -196,8 +196,8 @@ def get_available_port(min_port, max_port):
try:
tmpsock.bind(("127.0.0.1", random.randint(min_port, max_port)))
break
except OSError:
pass
except OSError as e:
raise OSError(e)
_, port = tmpsock.getsockname()
return port

View File

@ -171,10 +171,16 @@ class Onion(object):
if self.system == 'Windows':
# Windows needs to use network ports, doesn't support unix sockets
torrc_template = open(common.get_resource_path('torrc_template-windows')).read()
self.tor_control_port = common.get_available_port(1000, 65535)
try:
self.tor_control_port = common.get_available_port(1000, 65535)
except:
raise OSError(strings._('no_available_port'))
self.tor_control_socket = None
self.tor_cookie_auth_file = os.path.join(self.tor_data_directory.name, 'cookie')
self.tor_socks_port = common.get_available_port(1000, 65535)
try:
self.tor_socks_port = common.get_available_port(1000, 65535)
except:
raise OSError(strings._('no_available_port'))
self.tor_torrc = os.path.join(self.tor_data_directory.name, 'torrc')
else:
# Linux and Mac can use unix sockets
@ -183,7 +189,10 @@ class Onion(object):
self.tor_control_port = None
self.tor_control_socket = os.path.join(self.tor_data_directory.name, 'control_socket')
self.tor_cookie_auth_file = os.path.join(self.tor_data_directory.name, 'cookie')
self.tor_socks_port = common.get_available_port(1000, 65535)
try:
self.tor_socks_port = common.get_available_port(1000, 65535)
except:
raise OSError(strings._('no_available_port'))
self.tor_torrc = os.path.join(self.tor_data_directory.name, 'torrc')
torrc_template = torrc_template.replace('{{data_directory}}', self.tor_data_directory.name)

View File

@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, shutil
from . import common
from . import common, strings
class OnionShare(object):
"""
@ -64,7 +64,10 @@ class OnionShare(object):
common.log('OnionShare', 'start_onion_service')
# Choose a random port
self.port = common.get_available_port(17600, 17650)
try:
self.port = common.get_available_port(17600, 17650)
except:
raise OSError(strings._('no_available_port'))
if self.local_only:
self.onion_host = '127.0.0.1:{0:d}'.format(self.port)
@ -72,6 +75,7 @@ class OnionShare(object):
if self.shutdown_timeout > 0:
self.shutdown_timer = common.close_after_seconds(self.shutdown_timeout)
self.onion_host = self.onion.start_onion_service(self.port)
if self.stealth:

View File

@ -250,7 +250,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.app.start_onion_service()
self.starting_server_step2.emit()
except (TorTooOld, TorErrorInvalidSetting, TorErrorAutomatic, TorErrorSocketPort, TorErrorSocketFile, TorErrorMissingPassword, TorErrorUnreadableCookieFile, TorErrorAuthError, TorErrorProtocolError, BundledTorTimeout) as e:
except (TorTooOld, TorErrorInvalidSetting, TorErrorAutomatic, TorErrorSocketPort, TorErrorSocketFile, TorErrorMissingPassword, TorErrorUnreadableCookieFile, TorErrorAuthError, TorErrorProtocolError, BundledTorTimeout, OSError) as e:
self.starting_server_error.emit(e.args[0])
return
@ -348,7 +348,11 @@ class OnionShareGui(QtWidgets.QMainWindow):
common.log('OnionShareGui', 'stop_server')
if self.server_status.status != self.server_status.STATUS_STOPPED:
web.stop(self.app.port)
try:
web.stop(self.app.port)
except:
# Probably we had no port to begin with (Onion service didn't start)
pass
self.app.cleanup()
self.filesize_warning.hide()
self.stop_server_finished.emit()

View File

@ -10,6 +10,7 @@
"ctrlc_to_stop": "Press Ctrl-C to stop server",
"not_a_file": "{0:s} is not a file.",
"not_a_readable_file": "{0:s} is not a readable file.",
"no_available_port": "Could not start the Onion service as we found no available port",
"download_page_loaded": "Download page loaded",
"other_page_loaded": "URL loaded",
"close_on_timeout": "Closing automatically because timeout was reached",