2017-04-17 22:28:51 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2021-02-22 16:35:14 -05:00
|
|
|
Copyright (C) 2014-2021 Micah Lee, et al. <micah@micahflee.com>
|
2017-04-17 22:28:51 -04: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 <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os, shutil
|
2019-10-13 00:01:25 -04:00
|
|
|
from .common import AutoStopTimer
|
|
|
|
|
2017-04-17 22:28:51 -04:00
|
|
|
|
|
|
|
class OnionShare(object):
|
|
|
|
"""
|
|
|
|
OnionShare is the main application class. Pass in options and run
|
|
|
|
start_onion_service and it will do the magic.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2019-03-25 00:05:54 -04:00
|
|
|
def __init__(self, common, onion, local_only=False, autostop_timer=0):
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common = common
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log("OnionShare", "__init__")
|
2017-05-16 14:23:18 -04:00
|
|
|
|
2017-04-17 22:28:51 -04:00
|
|
|
# The Onion object
|
|
|
|
self.onion = onion
|
|
|
|
|
|
|
|
self.hidserv_dir = None
|
|
|
|
self.onion_host = None
|
2018-04-28 18:00:23 -04:00
|
|
|
self.port = None
|
2017-04-17 22:28:51 -04:00
|
|
|
|
|
|
|
# files and dirs to delete on shutdown
|
|
|
|
self.cleanup_filenames = []
|
|
|
|
|
|
|
|
# do not use tor -- for development
|
|
|
|
self.local_only = local_only
|
|
|
|
|
2017-11-08 04:25:59 -05:00
|
|
|
# optionally shut down after N hours
|
2019-03-25 00:05:54 -04:00
|
|
|
self.autostop_timer = autostop_timer
|
|
|
|
# init auto-stop timer thread
|
|
|
|
self.autostop_timer_thread = None
|
2017-11-08 04:25:59 -05:00
|
|
|
|
2018-04-28 18:00:23 -04:00
|
|
|
def choose_port(self):
|
|
|
|
"""
|
|
|
|
Choose a random port.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.port = self.common.get_available_port(17600, 17650)
|
|
|
|
except:
|
2020-10-13 01:40:55 -04:00
|
|
|
raise OSError("Cannot find an available OnionShare port")
|
2017-04-17 22:28:51 -04:00
|
|
|
|
2020-11-27 14:22:29 -05:00
|
|
|
def start_onion_service(self, mode, mode_settings, await_publication=True):
|
2017-04-17 22:28:51 -04:00
|
|
|
"""
|
|
|
|
Start the onionshare onion service.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log("OnionShare", "start_onion_service")
|
2017-05-16 14:23:18 -04:00
|
|
|
|
2018-04-28 18:00:23 -04:00
|
|
|
if not self.port:
|
|
|
|
self.choose_port()
|
2017-04-17 22:28:51 -04:00
|
|
|
|
2019-03-25 00:05:54 -04:00
|
|
|
if self.autostop_timer > 0:
|
|
|
|
self.autostop_timer_thread = AutoStopTimer(self.common, self.autostop_timer)
|
2018-05-04 19:43:30 -04:00
|
|
|
|
2017-04-17 22:28:51 -04:00
|
|
|
if self.local_only:
|
2019-10-20 13:15:16 -04:00
|
|
|
self.onion_host = f"127.0.0.1:{self.port}"
|
2017-04-17 22:28:51 -04:00
|
|
|
return
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
self.onion_host = self.onion.start_onion_service(
|
2020-11-27 14:22:29 -05:00
|
|
|
mode, mode_settings, self.port, await_publication
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2017-04-17 22:28:51 -04:00
|
|
|
|
2019-11-02 20:01:47 -04:00
|
|
|
if mode_settings.get("general", "client_auth"):
|
2017-04-17 22:28:51 -04:00
|
|
|
self.auth_string = self.onion.auth_string
|
|
|
|
|
2019-11-10 20:32:34 -05:00
|
|
|
def stop_onion_service(self, mode_settings):
|
|
|
|
"""
|
|
|
|
Stop the onion service
|
|
|
|
"""
|
|
|
|
self.onion.stop_onion_service(mode_settings)
|
|
|
|
|
2017-04-17 22:28:51 -04:00
|
|
|
def cleanup(self):
|
|
|
|
"""
|
|
|
|
Shut everything down and clean up temporary files, etc.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log("OnionShare", "cleanup")
|
2017-05-16 14:23:18 -04:00
|
|
|
|
2019-02-18 15:28:02 -05:00
|
|
|
# Cleanup files
|
|
|
|
try:
|
|
|
|
for filename in self.cleanup_filenames:
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
os.remove(filename)
|
|
|
|
elif os.path.isdir(filename):
|
|
|
|
shutil.rmtree(filename)
|
|
|
|
except:
|
|
|
|
# Don't crash if file is still in use
|
|
|
|
pass
|
2017-04-17 22:28:51 -04:00
|
|
|
self.cleanup_filenames = []
|