print error log if tunnel fails

This commit is contained in:
Concedo 2023-11-03 23:48:21 +08:00
parent c794fd5ceb
commit 373c20ad51

View file

@ -1811,6 +1811,7 @@ def setuptunnel():
def run_tunnel(): def run_tunnel():
tunnelproc = None tunnelproc = None
tunneloutput = "" tunneloutput = ""
tunnelrawlog = ""
time.sleep(0.2) time.sleep(0.2)
if os.name == 'nt': if os.name == 'nt':
print("Starting Cloudflare Tunnel for Windows...") print("Starting Cloudflare Tunnel for Windows...")
@ -1818,12 +1819,13 @@ def setuptunnel():
else: else:
print("Starting Cloudflare Tunnel for Linux...") print("Starting Cloudflare Tunnel for Linux...")
tunnelproc = subprocess.Popen(f"cloudflared-linux-amd64 tunnel --url http://localhost:{args.port}", text=True, encoding='utf-8', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) tunnelproc = subprocess.Popen(f"cloudflared-linux-amd64 tunnel --url http://localhost:{args.port}", text=True, encoding='utf-8', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
time.sleep(5.4) time.sleep(6)
def tunnel_reader(): def tunnel_reader():
nonlocal tunnelproc,tunneloutput nonlocal tunnelproc,tunneloutput,tunnelrawlog
pattern = r'https://[\w\.-]+\.trycloudflare\.com' pattern = r'https://[\w\.-]+\.trycloudflare\.com'
while True: while True:
line = tunnelproc.stderr.readline() #cloudflare writes to stderr for some reason line = tunnelproc.stderr.readline() #cloudflare writes to stderr for some reason
tunnelrawlog += line+"\n"
if not line: if not line:
return return
found = re.findall(pattern, line) found = re.findall(pattern, line)
@ -1833,11 +1835,11 @@ def setuptunnel():
tunnel_reader_thread = threading.Thread(target=tunnel_reader) tunnel_reader_thread = threading.Thread(target=tunnel_reader)
tunnel_reader_thread.start() tunnel_reader_thread.start()
time.sleep(0.4) time.sleep(0.8)
if tunneloutput!="": if tunneloutput!="":
print(f"Your remote tunnel is ready, please connect to {tunneloutput}") print(f"Your remote tunnel is ready, please connect to {tunneloutput}")
else: else:
print(f"Error: Could not create cloudflare tunnel.") print(f"Error: Could not create cloudflare tunnel!\nMore Info:\n{tunnelrawlog}")
tunnelproc.wait() tunnelproc.wait()
if os.name == 'nt': if os.name == 'nt':