translet / run_public_server.py
bllndebrahim's picture
Clean history (free space)
ae033d7
Raw History Blame Contribute Delete
6.25 kB
import os
import sys
import time
import subprocess
import re
import socket
def get_desktop_path():
desktop = os.path.join(os.path.expanduser("~"), "Desktop")
if os.path.exists(desktop):
return desktop
return os.path.dirname(os.path.abspath(__file__))
def get_local_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "127.0.0.1"
def is_port_open(port=3000):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
res = s.connect_ex(('127.0.0.1', port))
s.close()
return res == 0
except Exception:
return False
def show_windows_notification(title, msg):
try:
ps_cmd = f'''
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$template = @"
<toast>
<visual>
<binding template="ToastGeneric">
<text>{title}</text>
<text>{msg}</text>
</binding>
</visual>
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("Translet AI").Show($toast)
'''
subprocess.run(["powershell", "-NoProfile", "-Command", ps_cmd], creationflags=0x08000000, timeout=5)
except Exception:
pass
def update_desktop_files(public_url, local_ip):
desktop = get_desktop_path()
txt_path = os.path.join(desktop, "لینکی_مۆبایل.txt")
url_path = os.path.join(desktop, "Translet_Mobile.url")
content = f"""====================================================================
🚀 سێرڤەری Translet بۆ مۆبایل چالاکە (Online)
====================================================================
📱 لینکی ڕاستەوخۆ بۆ مۆبایلەکەت لە هەر شوێنێک بیت (4G / 5G / دەرەوە):
👉 {public_url}
(ئەم بەستەرە لە سەفاری و گووگڵ کرۆمی مۆبایل کاردەکات و مایک و دەنگ تەواو کاردەکات)
📶 ئەگەر لەسەر هەمان وایفای کۆمپیوتەرەکەت بوویت دەتوانی ئەمەش بەکاربهێنیت:
👉 http://{local_ip}:3000
====================================================================
تێبینی: هەر کاتێک کۆمپیوتەرەکەت هەڵبکەیت، ئەم لینکە ئۆتۆماتیکی نوێ دەبێتەوە.
کاتێک کۆمپیوتەرەکە دەکوژێنیتەوە، سێرڤەرەکەش خۆی دەکوژێتەوە.
====================================================================
"""
try:
with open(txt_path, "w", encoding="utf-8") as f:
f.write(content)
except Exception as e:
print("Failed to write desktop txt:", e)
# Windows .url Internet Shortcut file
try:
with open(url_path, "w", encoding="utf-8") as f:
f.write(f"[InternetShortcut]\nURL={public_url}\n")
except Exception as e:
print("Failed to write desktop url:", e)
def main():
base_dir = os.path.dirname(os.path.abspath(__file__))
log_file = os.path.join(base_dir, "tunnel_boot.log")
cloudflared_path = os.path.join(base_dir, "cloudflared.exe")
# 1. Start Python Server if not running
server_proc = None
if not is_port_open(3000):
server_py = os.path.join(base_dir, "server.py")
server_proc = subprocess.Popen(
[sys.executable, "-u", server_py, "3000"],
cwd=base_dir,
creationflags=0x08000000 # CREATE_NO_WINDOW
)
time.sleep(2)
# 2. Check cloudflared.exe
if not os.path.isfile(cloudflared_path):
subprocess.run(
["curl.exe", "-L", "-o", cloudflared_path, "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe"],
creationflags=0x08000000
)
# 3. Launch Cloudflare Tunnel
if os.path.exists(log_file):
try: os.remove(log_file)
except: pass
tunnel_proc = subprocess.Popen(
[cloudflared_path, "tunnel", "--url", "http://localhost:3000", "--logfile", log_file],
cwd=base_dir,
creationflags=0x08000000 # CREATE_NO_WINDOW
)
# 4. Extract Public URL
public_url = None
for _ in range(40):
time.sleep(1)
if os.path.exists(log_file):
try:
with open(log_file, "r", encoding="utf-8", errors="ignore") as f:
data = f.read()
m = re.search(r'https://[a-zA-Z0-9\-]+\.trycloudflare\.com', data)
if m:
public_url = m.group(0)
break
except Exception:
pass
local_ip = get_local_ip()
if public_url:
update_desktop_files(public_url, local_ip)
show_windows_notification(
"Translet AI Server چالاک کرا",
f"سێرڤەر ئۆنلاینە! لینکی مۆبایلەکەت لەسەر دێسکتۆپ ئامادەیە."
)
# Keep alive and monitor
while True:
time.sleep(15)
# Check if server or tunnel crashed, restart if needed
if not is_port_open(3000):
server_py = os.path.join(base_dir, "server.py")
server_proc = subprocess.Popen(
[sys.executable, "-u", server_py, "3000"],
cwd=base_dir,
creationflags=0x08000000
)
if tunnel_proc.poll() is not None:
tunnel_proc = subprocess.Popen(
[cloudflared_path, "tunnel", "--url", "http://localhost:3000", "--logfile", log_file],
cwd=base_dir,
creationflags=0x08000000
)
if __name__ == "__main__":
main()