|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | +from ..utils import * |
| 7 | + |
| 8 | +PLATFORM = sys.platform |
| 9 | + |
| 10 | + |
| 11 | +# -------------------------------- |
| 12 | +# Helper functions |
| 13 | +# -------------------------------- |
| 14 | +def has_dependencies_installed() -> bool: |
| 15 | + """ |
| 16 | + Checks if the required dependencies are installed. |
| 17 | +
|
| 18 | + Returns: |
| 19 | + bool: True if all dependencies are installed AND EXECUTABLE, False otherwise. |
| 20 | + """ |
| 21 | + exec_path = shutil.which("tor") |
| 22 | + return (exec_path and os.access(exec_path, os.X_OK)) == True |
| 23 | + |
| 24 | + |
| 25 | +def send(config_file=None, auto_stop_seconds=None, no_autostop_sharing=None): |
| 26 | + files = input("Which file(s) would you like to share? ") |
| 27 | + args = [ |
| 28 | + "onionshare-cli", |
| 29 | + files, |
| 30 | + "--local-only", |
| 31 | + f"--config {config_file}" if config_file else None, |
| 32 | + f"--auto-stop-timer {auto_stop_seconds}" if auto_stop_seconds else None, |
| 33 | + f"--no-autostop-sharing {no_autostop_sharing}" if no_autostop_sharing else None |
| 34 | + ] |
| 35 | + filtered: list[str] = list(filter(lambda x: x is not None, args)) |
| 36 | + process = subprocess.Popen(filtered, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True) |
| 37 | + |
| 38 | + for line in iter(process.stdout.readline, ''): |
| 39 | + print(line, end='') |
| 40 | + |
| 41 | + process.stdout.close() |
| 42 | + process.wait() |
| 43 | + |
| 44 | + |
| 45 | +def receive(config_file=None, auto_stop_seconds=None): |
| 46 | + args = [ |
| 47 | + "onionshare-cli", |
| 48 | + "--local-only", |
| 49 | + "--receive", |
| 50 | + f"--config {config_file}" if config_file else None, |
| 51 | + f"--auto-stop-timer {auto_stop_seconds}" if auto_stop_seconds else None |
| 52 | + ] |
| 53 | + |
| 54 | + receiving = input("Would you like to receive files, text or both (f/t/b) ") |
| 55 | + |
| 56 | + while receiving not in ["f", "t", "b"]: |
| 57 | + receiving = input(f"Sorry, {receiving} was not one of the allowed values (f, t or b)") |
| 58 | + |
| 59 | + if receiving == "t": |
| 60 | + args.append("--disable-files") |
| 61 | + elif receiving == "f": |
| 62 | + args.append("--disable-text") |
| 63 | + |
| 64 | + if receiving != "t": |
| 65 | + data_dir = input("Where would you like the files to be saved to? (blank for default)") |
| 66 | + if data_dir != "" and data_dir is not None: |
| 67 | + args.append(f"--data-dir {data_dir}") |
| 68 | + |
| 69 | + filtered: list[str] = list(filter(lambda x: x is not None, args)) |
| 70 | + print("Filtered: ", filtered) |
| 71 | + process = subprocess.Popen(filtered, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True) |
| 72 | + |
| 73 | + for line in iter(process.stdout.readline, ''): |
| 74 | + print(line, end='') |
| 75 | + |
| 76 | + process.stdout.close() |
| 77 | + process.wait() |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +def chat(config_file=None, auto_stop_seconds=None): |
| 82 | + args = [ |
| 83 | + "onionshare-cli", |
| 84 | + "--local-only", |
| 85 | + "--chat", |
| 86 | + f"--config {config_file}" if config_file else None, |
| 87 | + f"--auto-stop-timer {auto_stop_seconds}" if auto_stop_seconds else None |
| 88 | + ] |
| 89 | + filtered: list[str] = list(filter(lambda x: x is not None, args)) |
| 90 | + process = subprocess.Popen(filtered, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True) |
| 91 | + |
| 92 | + for line in iter(process.stdout.readline, ''): |
| 93 | + print(line, end='') |
| 94 | + |
| 95 | + process.stdout.close() |
| 96 | + process.wait() |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +def onionshare(advanced=False): |
| 101 | + if PLATFORM == "win32": |
| 102 | + print_alert("Using Onionshare from the commandline is not supported on Windows. Please use the GUI which you can download from:") |
| 103 | + print("\t- https://onionshare.org/#download") |
| 104 | + print_alert("You'll also need Tor installed, which you can download from:") |
| 105 | + print("\t- winget install --id=TorProject.TorBrowser -e") |
| 106 | + print("\t- Or visit https://www.torproject.org/download/ to download directly.") |
| 107 | + return |
| 108 | + |
| 109 | + if not has_dependencies_installed(): |
| 110 | + print("You need to have Tor installed to utilise this feature.") |
| 111 | + |
| 112 | + match PLATFORM: |
| 113 | + case "linux": |
| 114 | + print( |
| 115 | + """ |
| 116 | + Depending on your distro, the command will something like: |
| 117 | +
|
| 118 | + sudo apt install tor |
| 119 | + sudo pacman -S tor |
| 120 | + sudo dnf install tor |
| 121 | + """ |
| 122 | + ) |
| 123 | + return |
| 124 | + case "darwin": |
| 125 | + print( |
| 126 | + """ |
| 127 | + brew install tor |
| 128 | + """ |
| 129 | + ) |
| 130 | + return |
| 131 | + case _: |
| 132 | + print("Unknown platform") |
| 133 | + return |
| 134 | + |
| 135 | + running = True |
| 136 | + config_file = None |
| 137 | + no_autostop_sharing = None |
| 138 | + auto_stop_seconds = None |
| 139 | + |
| 140 | + if advanced: |
| 141 | + config = input("Custom config file location (blank for defaults): ") |
| 142 | + if config and config != "": |
| 143 | + config_file = config |
| 144 | + |
| 145 | + while running: |
| 146 | + method = input("Are you looking to send or receive files? (send/receive/chat/q) ") |
| 147 | + |
| 148 | + if method == "q": |
| 149 | + return |
| 150 | + |
| 151 | + if not config_file: |
| 152 | + auto_stop = input("Would you like the server to stop automatically? (Y/n) ") |
| 153 | + if auto_stop.lower() == "y": |
| 154 | + auto_stop_seconds = input("How many seconds should the server run for? ") |
| 155 | + |
| 156 | + |
| 157 | + try: |
| 158 | + if method == "send": |
| 159 | + if not config_file: |
| 160 | + no_autostop_sharing = True if input("Should the server stop after you have shared the files? (Y/n) ").lower() == 'y' else False |
| 161 | + |
| 162 | + send(config_file=config_file, no_autostop_sharing=no_autostop_sharing, auto_stop_seconds=auto_stop_seconds) |
| 163 | + return |
| 164 | + elif method == "receive": |
| 165 | + receive(config_file=config_file, auto_stop_seconds=auto_stop_seconds) |
| 166 | + return |
| 167 | + elif method == "chat": |
| 168 | + chat(config_file=config_file, auto_stop_seconds=auto_stop_seconds) |
| 169 | + return |
| 170 | + else: |
| 171 | + print(f"Method {method} not available or possible. Try another or exit (q)") |
| 172 | + except Exception as e: |
| 173 | + print(f"{FAILED}: {str(e)}") |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + onionshare() |
0 commit comments