├── .gitignore ├── LICENSE ├── README.md ├── dev.sh ├── install.sh ├── loopbackd ├── __init__.py ├── auth.py └── daemon.py ├── main.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | .vscode 132 | 133 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 loopbackai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [Discord](https://discord.gg/ZHPcHH8vZF) | [Discord on Scrolls](https://www.scrollsbot.com/discord/1026774968576507925/1026774969100812290/latest) 2 | 3 | # Loopbackd 4 | Launch the LoopbackAI daemon and control your computer remotely via Telegram/Whatsapp/SMS 5 | 6 | [Loopback.ai/download](https://www.loopback.ai/download) 7 | 8 | ## Installation 9 | 10 | ```bash 11 | curl -sL https://raw.githubusercontent.com/loopbackai/loopbackd/master/install.sh | sudo bash 12 | loopbackd up 13 | # Follow the instructions to authenticate 14 | ``` 15 | 16 | ## Development & Build it from source 17 | 18 | Requires python 3.6+ installation in PATH 19 | 20 | ```bash 21 | # create virtual env and install the dependencies 22 | ./dev.sh install 23 | 24 | # run in dev mode 25 | ./dev.sh run 26 | 27 | # build a single binary 28 | ./dev.sh build 29 | ``` 30 | 31 | ## Building for new architectures / platforms 32 | 33 | `"Unsupported system" error on installation` 34 | 35 | Install python 3.6+ and clone the project 36 | 37 | ```bash 38 | ./dev.sh build 39 | cp dist/loopbackd* /usr/local/bin/loopbackd 40 | ``` 41 | Create a systemd service or similar based on the example in ```install.sh``` 42 | 43 | ## Uninstall 44 | 45 | ```bash 46 | sudo systemctl stop loopbackd.service 47 | sudo systemctl disable loopbackd.service 48 | sudo rm /usr/local/bin/loopbackd 49 | sudo rm /etc/systemd/system/loopbackd.service 50 | ``` 51 | -------------------------------------------------------------------------------- /dev.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | while [[ $# -gt 0 ]]; do 4 | case $1 in 5 | install) 6 | python -m venv venv 7 | ./venv/bin/pip install -r requirements.txt 8 | exit 0 9 | ;; 10 | build) 11 | rm -rf venv_build 12 | python -m venv venv_build 13 | ./venv_build/bin/pip install -r requirements.txt 14 | ./venv_build/bin/pyinstaller main.py --onefile 15 | mv dist/main dist/loopbackd_$(uname)_$(arch) 16 | rm -rf venv_build 17 | exit 0 18 | ;; 19 | run) 20 | shift 21 | ./venv/bin/python main.py $@ 22 | exit 0 23 | ;; 24 | -* | --*) 25 | echo "Unknown option $1" 26 | exit 1 27 | ;; 28 | *) 29 | POSITIONAL_ARGS+=("$1") # save positional arg 30 | shift # past argument 31 | ;; 32 | esac 33 | done 34 | 35 | echo "Doing nothing" -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Download binary 4 | 5 | OS=$(uname -s) 6 | ARCH=$(arch) 7 | 8 | # stop & disable service if exists 9 | { 10 | systemctl stop loopbackd 11 | systemctl disable loopbackd 12 | } &> /dev/null 13 | 14 | curl -sfLo /usr/local/bin/loopbackd https://github.com/loopbackai/loopbackd/releases/latest/download/loopbackd_"$OS"_"$ARCH" 15 | CURL_RESPONSE=$? 16 | 17 | if [[ $CURL_RESPONSE == 0 ]] 18 | then 19 | chmod +x /usr/local/bin/loopbackd 20 | else 21 | echo "Unsupported system." 22 | exit 1 23 | fi 24 | 25 | # Create Systemd service 26 | 27 | cat <"/etc/systemd/system/loopbackd.service" 28 | [Unit] 29 | Description=Loopbackd service to connect remotely to your machine, loopbackd --help for more info 30 | After=network.target 31 | 32 | [Service] 33 | Type=simple 34 | Restart=always 35 | RestartSec=5 36 | TimeoutStartSec=0 37 | User=$SUDO_USER 38 | ExecStart=/usr/local/bin/loopbackd daemon 39 | 40 | [Install] 41 | WantedBy=multi-user.target 42 | EOM 43 | 44 | systemctl start loopbackd 45 | systemctl enable loopbackd 46 | 47 | echo "Installed successfully - run 'loopbackd up' for the next step" -------------------------------------------------------------------------------- /loopbackd/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loopbackai/loopbackd/b6cfca1eb9d966f1b6c97543134eec4080878f65/loopbackd/__init__.py -------------------------------------------------------------------------------- /loopbackd/auth.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | import json 3 | from uuid import uuid4 4 | 5 | 6 | def get_token(): 7 | p = pathlib.Path.home() / ".loopbackd" 8 | if p.exists(): 9 | with p.open("r") as loopbackd_conf: 10 | return json.load(loopbackd_conf)["token"] 11 | else: 12 | with p.open("w") as loopbackd_conf: 13 | token = f"loopback:{uuid4().hex}" 14 | json.dump({"token": token}, loopbackd_conf) 15 | return token 16 | -------------------------------------------------------------------------------- /loopbackd/daemon.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import pty 3 | import websockets 4 | import os 5 | import subprocess 6 | import json 7 | import base64 8 | import sys 9 | 10 | from loopbackd.auth import get_token 11 | 12 | 13 | LOOPBACK_TOKEN = os.environ.get("LOOPBACK_TOKEN", get_token()) 14 | VERSION = os.environ.get("LOOPBACK_DAEMON_VERSION", "1") 15 | 16 | master_fd, slave_fd = pty.openpty() 17 | producer_q = asyncio.Queue() 18 | consumer_q = asyncio.Queue() 19 | 20 | consumer_q.put_nowait("stty -echo\n") 21 | consumer_q.put_nowait("stty rows 30\n") 22 | consumer_q.put_nowait("stty cols 40\n") 23 | consumer_q.put_nowait("cd ~\n") 24 | 25 | p = subprocess.Popen( 26 | ["bash"], 27 | stdin=slave_fd, 28 | stdout=slave_fd, 29 | stderr=slave_fd, 30 | # Run in a new process group to enable bash's job control. 31 | preexec_fn=os.setsid, 32 | # Run bash in "dumb" terminal. 33 | env=dict(os.environ, TERM="dumb"), 34 | ) 35 | 36 | 37 | def read_pty(): 38 | output = os.read(master_fd, 10240) 39 | # os.write(sys.stdout.fileno(), output) 40 | producer_q.put_nowait(output) 41 | 42 | 43 | def write_pty(): 44 | while not consumer_q.empty(): 45 | user_input: str = consumer_q.get_nowait() 46 | os.write(master_fd, user_input.encode("utf-8")) 47 | 48 | 49 | async def consumer_handler(websocket): 50 | async for message in websocket: 51 | print("RECEIVED:", message) 52 | message = json.loads(message) 53 | if message["event"] == "new_command": 54 | await consumer_q.put(message["payload"]) 55 | # await consumer_q.put(message) 56 | 57 | 58 | async def producer_handler(websocket): 59 | while True: 60 | message = await producer_q.get() 61 | print("SENDING: ", message) 62 | await websocket.send( 63 | json.dumps( 64 | { 65 | "topic": LOOPBACK_TOKEN, 66 | "event": "data", 67 | "payload": base64.b64encode(message).decode("utf-8"), 68 | "ref": "", 69 | "join_ref": "", 70 | } 71 | ) 72 | ) 73 | # await websocket.send(await producer_q.get()) 74 | 75 | 76 | async def handler(): 77 | loop = asyncio.get_event_loop() 78 | loop.add_reader(master_fd, read_pty) 79 | loop.add_writer(master_fd, write_pty) 80 | 81 | # poll process and terminate when process exit 82 | def poll_process(): 83 | if p.poll() is not None: 84 | loop.stop() 85 | sys.exit(0) 86 | loop.call_soon(poll_process) 87 | 88 | loop.call_soon(poll_process) 89 | 90 | async with websockets.connect("wss://gw.loopback.ai/websocket") as websocket: 91 | await websocket.send( 92 | json.dumps( 93 | { 94 | "topic": LOOPBACK_TOKEN, 95 | "event": "phx_join", 96 | "payload": {"daemon_version": VERSION}, 97 | "ref": "", 98 | "join_ref": "", 99 | } 100 | ) 101 | ) 102 | await asyncio.gather( 103 | consumer_handler(websocket), 104 | producer_handler(websocket), 105 | ) 106 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import typer 2 | from rich import print 3 | 4 | app = typer.Typer( 5 | help="LoopbackAI CLI manage and run your loopback daemon", 6 | epilog="Find more help at https://www.loopback.ai", 7 | ) 8 | 9 | 10 | @app.command() 11 | def up(): 12 | """ 13 | Initalize the CLI and start the daemon if not already running 14 | """ 15 | from loopbackd.auth import get_token 16 | 17 | print( 18 | f""" 19 | You're almost done :boom: Now talk with our [link=https://t.me/LoopbackAIBot]telegram bot t.me/LoopbackAIBot[/link] and paste your token there. 20 | Your token [bold blue]{get_token()}[/bold blue] 21 | """ 22 | ) 23 | 24 | 25 | @app.command() 26 | def status(): 27 | """ 28 | Show the daemon status 29 | """ 30 | import subprocess 31 | 32 | subprocess.run("systemctl status loopbackd".split()) 33 | 34 | 35 | @app.command(rich_help_panel="Debug") 36 | def daemon(): 37 | """ 38 | Run the daemon in foreground for debug purposes 39 | """ 40 | import loopbackd.daemon as daemon 41 | import asyncio 42 | 43 | asyncio.run(daemon.handler()) 44 | 45 | 46 | if __name__ == "__main__": 47 | app() 48 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | websockets 2 | typer[all] 3 | pyinstaller --------------------------------------------------------------------------------