Spaces:
Runtime error
Runtime error
File size: 4,888 Bytes
4ab2d90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
"""
Code credits: ColabKobold
https://github.com/lostruins/koboldcpp
"""
import os, sys, threading, time, subprocess, re
def create_cloudflare_tunnel(port: int):
"""
Create a cloudflare tunnel for the server
"""
try:
def run_tunnel():
"""
Run the cloudflare tunnel
"""
# variables to be used in the thread
tunnel_process = None
tunnel_output = ""
tunnel_raw_dump = ""
time.sleep(0.2)
if os.name == 'nt':
print("Starting Cloudflare Tunnel for Windows, please wait...", flush=True)
tunnel_process = subprocess.Popen(f"cloudflared.exe tunnel --url localhost:{port}", text=True, encoding='utf-8', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
elif sys.platform=="darwin":
print("Starting Cloudflare Tunnel for MacOS, please wait...", flush=True)
tunnel_process = subprocess.Popen(f"./cloudflared tunnel --url http://localhost:{port}", text=True, encoding='utf-8', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
else:
print("Starting Cloudflare Tunnel for Linux, please wait...", flush=True)
tunnel_process = subprocess.Popen(f"./cloudflared-linux-amd64 tunnel --url http://localhost:{port}", text=True, encoding='utf-8', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
# wait for the tunnel to be ready
time.sleep(10)
def tunnel_reader():
"""
Read the stderr of the cloudflare tunnel process
"""
nonlocal tunnel_process, tunnel_output, tunnel_raw_dump
pattern: str = r'https://[\w\.-]+\.trycloudflare\.com'
while True:
line = tunnel_process.stderr.readline() #cloudflare writes to stderr for some reason
tunnel_raw_dump += line+"\n"
if not line:
# if the line is empty, the tunnel is closed
return
found = re.findall(pattern, line)
for x in found:
tunneloutput = x
return
tunnel_reader_thread = threading.Thread(target=tunnel_reader)
tunnel_reader_thread.start()
time.sleep(5) # wait for the tunnel to be ready
if tunnel_output=="":
print(f"Error: Could not create cloudflare tunnel!\nMore Info:\n{tunnel_raw_dump}", flush=True)
time.sleep(0.5)
tunnel_process.wait()
# Windows
if os.name == 'nt':
if os.path.exists("cloudflared.exe") and os.path.getsize("cloudflared.exe") > 1000000:
print("Cloudflared file exists, reusing it...")
else:
print("Downloading Cloudflare Tunnel for Windows...")
subprocess.run("curl -fL https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe -o cloudflared.exe", shell=True, capture_output=True, text=True, check=True, encoding='utf-8')
# MacOS
elif sys.platform=="darwin":
if os.path.exists("cloudflared") and os.path.getsize("cloudflared") > 1000000:
print("Cloudflared file exists, reusing it...")
else:
print("Downloading Cloudflare Tunnel for MacOS...")
subprocess.run("curl -fL https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-darwin-amd64.tgz -o cloudflared-darwin-amd64.tgz", shell=True, capture_output=True, text=True, check=True, encoding='utf-8')
subprocess.run("tar -xzf cloudflared-darwin-amd64.tgz", shell=True)
subprocess.run("chmod +x 'cloudflared'", shell=True)
else:
if os.path.exists("cloudflared-linux-amd64") and os.path.getsize("cloudflared-linux-amd64") > 1000000:
print("Cloudflared file exists, reusing it...")
else:
print("Downloading Cloudflare Tunnel for Linux...")
subprocess.run("curl -fL https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o cloudflared-linux-amd64", shell=True, capture_output=True, text=True, check=True, encoding='utf-8')
subprocess.run("chmod +x 'cloudflared-linux-amd64'", shell=True)
print("Attempting to start tunnel thread...", flush=True)
tunnel_thread = threading.Thread(target=run_tunnel)
tunnel_thread.start()
except Exception as ex:
print("Remote Tunnel Failed!")
print(str(ex))
return None
|