try to make the tunnel more reliable

This commit is contained in:
Concedo 2023-11-04 09:18:19 +08:00
parent 38471fbe06
commit 135001abc4

View file

@ -1814,12 +1814,12 @@ def setuptunnel():
tunnelrawlog = "" 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, please wait...")
tunnelproc = subprocess.Popen(f"cloudflared.exe tunnel --url localhost:{args.port}", text=True, encoding='utf-8', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) tunnelproc = subprocess.Popen(f"cloudflared.exe tunnel --url localhost:{args.port}", text=True, encoding='utf-8', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
else: else:
print("Starting Cloudflare Tunnel for Linux...") print("Starting Cloudflare Tunnel for Linux, please wait...")
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(6) time.sleep(10)
def tunnel_reader(): def tunnel_reader():
nonlocal tunnelproc,tunneloutput,tunnelrawlog nonlocal tunnelproc,tunneloutput,tunnelrawlog
pattern = r'https://[\w\.-]+\.trycloudflare\.com' pattern = r'https://[\w\.-]+\.trycloudflare\.com'
@ -1831,15 +1831,15 @@ def setuptunnel():
found = re.findall(pattern, line) found = re.findall(pattern, line)
for x in found: for x in found:
tunneloutput = x tunneloutput = x
print(f"Your remote tunnel is ready, please connect to {tunneloutput}")
return return
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.8) time.sleep(5)
if tunneloutput!="": if tunneloutput=="":
print(f"Your remote tunnel is ready, please connect to {tunneloutput}")
else:
print(f"Error: Could not create cloudflare tunnel!\nMore Info:\n{tunnelrawlog}") print(f"Error: Could not create cloudflare tunnel!\nMore Info:\n{tunnelrawlog}")
time.sleep(0.5)
tunnelproc.wait() tunnelproc.wait()
if os.name == 'nt': if os.name == 'nt':