diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ac4c68c..faabe43 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,13 +13,13 @@ repos: args: [--py310-plus] - repo: https://github.com/psf/black - rev: 23.1.0 + rev: 23.3.0 hooks: - id: black - repo: https://github.com/charliermarsh/ruff-pre-commit # Ruff version. - rev: "v0.0.244" + rev: "v0.0.260" hooks: - id: ruff args: [--fix] diff --git a/README.md b/README.md index 07ed594..7ad3d8b 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,10 @@ add `--remotemoe` to commandline options. The feature of `remote.moe` is that as long as the same ssh key is used, the same url is generated. The ssh keys for `localhost.run` and `remote.moe` are created with the name `id_rsa` in the script's root folder. However, if there is a problem with the write permission, it is created in a temporary folder instead, so a different url is created each time. + +----- +### Discord webhook + +add `--tunnel-webhook ` to commandline options. + +This feature was taken from [nur-zaman/sd-webui-tunnels](https://github.com/nur-zaman/sd-webui-tunnels) fork. thanks. diff --git a/discord_webhook.py b/discord_webhook.py new file mode 100644 index 0000000..3f2478e --- /dev/null +++ b/discord_webhook.py @@ -0,0 +1,27 @@ +import requests + + +def send_to_discord(message: str, webhook_url: str) -> bool: + """ + Sends a message to a Discord channel using a webhook URL. + Args: + message (str): The message to send to the Discord channel. + webhook_url (str): The Discord webhook URL for the channel to send the message to. + Returns: + bool: True if the message was successfully sent, False otherwise. + """ + try: + # Define the JSON payload to send to the webhook URL. + payload = {"content": message} + + # Make a POST request to the webhook URL with the JSON payload. + response = requests.post(webhook_url, json=payload) + + # Check the response status code and return True if it was successful. + response.raise_for_status() + return True + + except Exception as e: + # If there was an error sending the message, print the error message and return False. + print(f"Error sending message to Discord channel: {e}") + return False diff --git a/preload.py b/preload.py index 10d066e..d742fbc 100644 --- a/preload.py +++ b/preload.py @@ -19,3 +19,7 @@ def preload(parser: argparse.ArgumentParser): action="store_true", help="use remote.moe, alternative to gradio --share", ) + + parser.add_argument( + "--tunnel-webhook", type=str, help="discord webhook to send tunnel url to" + ) diff --git a/pyproject.toml b/pyproject.toml index 82a7ea2..b2ce1c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "sd-webui-tunnels" -version = "23.2.1" +version = "23.4.0" description = "Tunneling extension for automatic1111 sd-webui" authors = [ {name = "dowon", email = "ks2515@naver.com"}, @@ -14,7 +14,7 @@ repository = "https://github.com/Bing-su/sd-webui-tunnels" [tool.isort] profile = "black" -known_first_party = ["modules", "launch"] +known_first_party = ["modules", "launch", "discord_webhook"] [tool.ruff] select = ["A", "B", "C4", "E", "F", "I001", "N", "PT", "UP", "W"] @@ -22,4 +22,4 @@ ignore = ["B008", "B905", "E501"] unfixable = ["F401"] [tool.ruff.isort] -known-first-party = ["modules", "launch"] +known-first-party = ["modules", "launch", "discord_webhook"] diff --git a/scripts/ssh_tunnel.py b/scripts/ssh_tunnel.py index 326241f..d12b8e9 100644 --- a/scripts/ssh_tunnel.py +++ b/scripts/ssh_tunnel.py @@ -7,6 +7,7 @@ import subprocess from pathlib import Path from tempfile import TemporaryDirectory +from discord_webhook import send_to_discord from modules.shared import cmd_opts LOCALHOST_RUN = "localhost.run" @@ -23,7 +24,7 @@ def gen_key(path: str | Path) -> None: path.chmod(0o600) -def ssh_tunnel(host: str = LOCALHOST_RUN) -> None: +def ssh_tunnel(host: str = LOCALHOST_RUN) -> str: ssh_name = "id_rsa" ssh_path = Path(__file__).parent.parent / ssh_name @@ -67,12 +68,19 @@ def ssh_tunnel(host: str = LOCALHOST_RUN) -> None: raise RuntimeError(f"Failed to run {host}") print(f" * Running on {tunnel_url}") + return tunnel_url if cmd_opts.localhostrun: print("localhost.run detected, trying to connect...") - ssh_tunnel(LOCALHOST_RUN) + lhr_url = ssh_tunnel(LOCALHOST_RUN) + + if cmd_opts.tunnel_webhook: + send_to_discord(lhr_url, cmd_opts.tunnel_webhook) if cmd_opts.remotemoe: print("remote.moe detected, trying to connect...") - ssh_tunnel(REMOTE_MOE) + moe_url = ssh_tunnel(REMOTE_MOE) + + if cmd_opts.tunnel_webhook: + send_to_discord(moe_url, cmd_opts.tunnel_webhook) diff --git a/scripts/try_cloudflare.py b/scripts/try_cloudflare.py index ce5be03..834e108 100644 --- a/scripts/try_cloudflare.py +++ b/scripts/try_cloudflare.py @@ -1,8 +1,11 @@ from pycloudflared import try_cloudflare +from discord_webhook import send_to_discord from modules.shared import cmd_opts if cmd_opts.cloudflared: print("cloudflared detected, trying to connect...") port = cmd_opts.port if cmd_opts.port else 7860 - try_cloudflare(port) + urls = try_cloudflare(port) + if cmd_opts.tunnel_webhook: + send_to_discord(urls.tunnel, cmd_opts.tunnel_webhook)