Upload 5 files
Browse filesFiles changed (5)
- colab_tunnel/__init__.py+12-1
- colab_tunnel/_config.py+5-1
- colab_tunnel/_ssh.pyAdded+175
- colab_tunnel/_tunnels.py+63-10
- colab_tunnel/_utils.py+18-11
12312 unmodified lines1617181920217 unmodified lines2930313233346 unmodified lines41424344454614 unmodified lines61626364656667"""colab-proxy — утилиты для туннелирования веб-интерфейсов в Google Colab.12 unmodified linesget_localt_url,get_gradio_url,get_native_url,)__version__ = '1.0.0'7 unmodified lines'get_localt_url','get_gradio_url','get_native_url','download','run','WORK_FOLDER',6 unmodified lines'localt': get_localt_url,'gradio': get_gradio_url,'native': get_native_url,}14 unmodified linesArgs:host: Имя провайдера: 'cloudflared', 'tmole', 'tunwg','localt', 'gradio', 'native' — или 'all' для всех сразу.port: Локальный порт веб-интерфейса.Returns:123412 unmodified lines1718192021222324257 unmodified lines3334353637383940416 unmodified lines48495051525354555614 unmodified lines7172737475767778# ./colab_tunnel/__init__.py"""colab-proxy — утилиты для туннелирования веб-интерфейсов в Google Colab.12 unmodified linesget_localt_url,get_gradio_url,get_native_url,get_mmar_url,get_tunnelite_url,get_beeceptor_url,)__version__ = '1.0.0'7 unmodified lines'get_localt_url','get_gradio_url','get_native_url','get_mmar_url','get_tunnelite_url','get_beeceptor_url','download','run','WORK_FOLDER',6 unmodified lines'localt': get_localt_url,'gradio': get_gradio_url,'native': get_native_url,'mmar': get_mmar_url,'tunnelite': get_tunnelite_url,'beeceptor': get_beeceptor_url,}14 unmodified linesArgs:host: Имя провайдера: 'cloudflared', 'tmole', 'tunwg','localt', 'gradio', 'native', 'mmar', 'tunnelite','beeceptor' — или 'all' для всех сразу.port: Локальный порт веб-интерфейса.Returns:
1234567891011121314from os import environfrom pathlib import Path# Переопределяется переменной окружения colab_tunnel_WORK_DIRWORK_FOLDER = Path(environ.get('TUNNEL_WORK_DIR', '/content/.config'))claudflare_bin = WORK_FOLDER / 'claudflared'tmole_bin = WORK_FOLDER / 'tmole'tunwg_bin = WORK_FOLDER / 'tunwg'go_localt_bin = WORK_FOLDER / 'go_localt'gradio_bin = WORK_FOLDER / 'frpc_linux_amd64'colab_native_url = WORK_FOLDER / 'colab_url.txt'links_file = WORK_FOLDER / 'links.txt'123456789101112131415161718# ./colab_tunnel/_config.pyfrom os import environfrom pathlib import Path# Переопределяется переменной окружения colab_tunnel_WORK_DIRWORK_FOLDER = Path(environ.get('TUNNEL_WORK_DIR', '/content/.config'))cloudflare_bin = WORK_FOLDER / 'cloudflared'tmole_bin = WORK_FOLDER / 'tmole'tunwg_bin = WORK_FOLDER / 'tunwg'go_localt_bin = WORK_FOLDER / 'go_localt'gradio_bin = WORK_FOLDER / 'frpc_linux_amd64'mmar_bin = WORK_FOLDER / 'mmar'tunnelite_bin = WORK_FOLDER / 'tunnelite'beeceptor_bin = WORK_FOLDER / 'beeceptor-cli'colab_native_url = WORK_FOLDER / 'colab_url.txt'links_file = WORK_FOLDER / 'links.txt'
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175from atexit import register as exit_register
import os
from pathlib import Path
from re import search
from select import select
from subprocess import DEVNULL, PIPE, Popen
import time
from colab_tunnel._utils import terminate_process, run
# Глобальный реестр для предотвращения Garbage Collection (сборки мусора) процессов и их труб
_active_tunnels: list[Popen] = []
def ensure_ssh_key() -> None:
"""
Гарантирует наличие приватного SSH-ключа в системе.
"""
ssh_dir = Path.home() / '.ssh'
ssh_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
key_file = ssh_dir / 'id_ed25519'
if not key_file.exists():
import subprocess
subprocess.run(
['ssh-keygen', '-t', 'ed25519', '-N', '', '-f', str(key_file)],
stdout=DEVNULL,
stderr=DEVNULL
)
def get_ssh_tunnel_url(
start_commands: list[str],
process_name: str,
url_pattern: str,
timeout: float = 15.0,
) -> str:
"""
Запускает SSH-процесс с открытым stdin=PIPE для поддержания соединения.
"""
ensure_ssh_key()
# Завершаем старые процессы
run(f'pkill -f "{process_name}"')
# Важно: stdin=PIPE оставляем ОТКРЫТЫМ, чтобы SSH не получал EOF и не выходил
process = Popen(start_commands, stdout=PIPE, stderr=PIPE, stdin=PIPE)
stdout_raw = process.stdout.raw
stderr_raw = process.stderr.raw
# Делаем сырые дескрипторы неблокирующими
for raw_stream in (stdout_raw, stderr_raw):
try:
os.set_blocking(raw_stream.fileno(), False)
except Exception:
pass
start_time = time.time()
buffer = b""
while time.time() - start_time < timeout:
ready_readers, _, _ = select([stdout_raw, stderr_raw], [], [], 0.1)
read_any = False
for reader in ready_readers:
try:
chunk = reader.read(1024)
if chunk:
buffer += chunk
read_any = True
except (BlockingIOError, ValueError):
pass
except Exception:
pass
decoded_text = buffer.decode('utf-8', errors='ignore')
match = search(url_pattern, decoded_text)
if match:
# Регистрируем завершение при выходе из Python
exit_register(terminate_process, process_name, process)
# Добавляем в глобальный список, чтобы сохранить процесс и stdin-трубу открытыми
_active_tunnels.append(process)
return match.group()
if not read_any and process.poll() is not None:
break
time.sleep(0.05)
process.terminate()
run(f'pkill -f "{process_name}"')
final_output = buffer.decode('utf-8', errors='ignore')
raise RuntimeError(
f'Не удалось получить SSH-ссылку за {timeout} сек. Вывод процесса:\n{final_output}'
)
# ---------------------------------------------------------------------------
# Провайдеры
# ---------------------------------------------------------------------------
def get_optimistix_url(port: int) -> str:
cmd = [
'ssh',
'-o', 'StrictHostKeyChecking=no',
'-o', 'UserKnownHostsFile=/dev/null',
'-o', 'ExitOnForwardFailure=yes',
'-o', 'ServerAliveInterval=10',
'-o', 'ServerAliveCountMax=3',
'-p', '1122',
'-R', f'80:127.0.0.1:{port}',
'ssh.optimistixtunnel.com'
]
return get_ssh_tunnel_url(
start_commands=cmd,
process_name='ssh.optimistixtunnel.com',
url_pattern=r'https://\S+\.otnl\.link',
)
def get_srvus_url(port: int) -> str:
cmd = [
'ssh',
'-o', 'StrictHostKeyChecking=no',
'-o', 'UserKnownHostsFile=/dev/null',
'-o', 'ExitOnForwardFailure=yes',
'-o', 'ServerAliveInterval=10',
'-o', 'ServerAliveCountMax=3',
'-R', f'1:127.0.0.1:{port}',
'srv.us'
]
return get_ssh_tunnel_url(
start_commands=cmd,
process_name='srv.us',
url_pattern=r'https://[a-z0-9]{10,}\.srv\.us/?',
)
def get_serveo_url(port: int) -> str:
cmd = [
'ssh',
'-o', 'StrictHostKeyChecking=no',
'-o', 'UserKnownHostsFile=/dev/null',
'-o', 'ExitOnForwardFailure=yes',
'-o', 'ServerAliveInterval=10',
'-o', 'ServerAliveCountMax=3',
'-R', f'80:127.0.0.1:{port}',
'serveo.net'
]
return get_ssh_tunnel_url(
start_commands=cmd,
process_name='serveo.net',
url_pattern=r'https://\S+\.serveousercontent\.com',
)
def get_localhostrun_url(port: int) -> str:
cmd = [
'ssh',
'-o', 'StrictHostKeyChecking=no',
'-o', 'UserKnownHostsFile=/dev/null',
'-o', 'ExitOnForwardFailure=yes',
'-o', 'ServerAliveInterval=10',
'-o', 'ServerAliveCountMax=3',
'-R', f'80:127.0.0.1:{port}',
'nokey@localhost.run'
]
return get_ssh_tunnel_url(
start_commands=cmd,
process_name='localhost.run',
url_pattern=r'https://(?!admin\b)[a-zA-Z0-9-]+\.(?:localhost\.run|lhr\.life)',
)
1234 unmodified lines8910111213141511 unmodified lines2728293031323334353637383940414243444546474849501 unmodified line5253545556575848 unmodified lines10710810911011111211311445 unmodified lines160161162163164165from atexit import register as exit_registerfrom pathlib import Pathfrom re import search4 unmodified linesfrom ._config import (WORK_FOLDER,claudflare_bin, tmole_bin, tunwg_bin,go_localt_bin, gradio_bin, links_file,)from ._utils import (download, unpack_archive, run,11 unmodified lineslines_to_read: int,url_pattern: str = r'https://\S+',write_link: bool = False,) -> str:if not bin_path.exists():files_before = {f.name for f in WORK_FOLDER.iterdir() if f.is_file()}if need_unpack:unpack_archive(download(bin_url, progress=False), WORK_FOLDER, rm_archive=True)else:download(bin_url, save_path=bin_path.parent, progress=False)files_after = {f.name for f in WORK_FOLDER.iterdir() if f.is_file()}new_files = files_after - files_beforeif len(new_files) == 1:move_path(WORK_FOLDER / new_files.pop(), bin_path)else:raise RuntimeError(f'ошибка при определении нового файла после распаковки!\n'f'ожидался один новый файл, а появились: {", ".join(new_files)}')bin_path.chmod(0o777)1 unmodified lineif is_process_running(bin_path.name):run(f'pkill -f {bin_path.name}')process = Popen(start_commands, stdout=PIPE, stderr=PIPE, text=True)stream = process.stderr if read_from_stderr else process.stdoutlines: list[str] = []48 unmodified linesreturn get_revproxy_url(bin_url='https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64',need_unpack=False,bin_path=claudflare_bin,start_commands=[str(claudflare_bin), 'tunnel', '--url', f'http://127.0.0.1:{port}'],read_from_stderr=True,lines_to_read=6,url_pattern=r'(?P<url>https?://\S+\.trycloudflare\.com)',45 unmodified linesraise RuntimeError(f'после {max_attempts} попыток поднять туннель градио не удалось: {last_error}')def get_native_url(port: int) -> str:try:from google.colab.output import eval_js # type: ignore[import]12344 unmodified lines9101112131415161711 unmodified lines293031323334353637383940414243444546474849505152535455565758591 unmodified line616263646566676869707172737448 unmodified lines12312412512612712812913045 unmodified lines176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218# ./colab_tunnel/_tunnels.pyfrom atexit import register as exit_registerfrom pathlib import Pathfrom re import search4 unmodified linesfrom ._config import (WORK_FOLDER,cloudflare_bin, tmole_bin, tunwg_bin,go_localt_bin, gradio_bin, mmar_bin,tunnelite_bin, beeceptor_bin, links_file)from ._utils import (download, unpack_archive, run,11 unmodified lineslines_to_read: int,url_pattern: str = r'https://\S+',write_link: bool = False,stdin_input: str | None = None,) -> str:if not bin_path.exists():files_before = {f for f in WORK_FOLDER.iterdir() if f.is_file()}if need_unpack:unpack_archive(download(bin_url, progress=False), WORK_FOLDER, rm_archive=True)else:download(bin_url, save_path=bin_path.parent, progress=False)files_after = {f for f in WORK_FOLDER.iterdir() if f.is_file()}new_files = list(files_after - files_before)if len(new_files) == 1:move_path(new_files[0], bin_path)elif len(new_files) > 1:largest_file = max(new_files, key=lambda f: f.stat().st_size)move_path(largest_file, bin_path)for f in new_files:if f != largest_file:f.unlink(missing_ok=True)else:raise RuntimeError(f'ошибка при определении нового файла после распаковки!\n'f'ожидалось появление новых файлов в директории {WORK_FOLDER}')bin_path.chmod(0o777)1 unmodified lineif is_process_running(bin_path.name):run(f'pkill -f {bin_path.name}')# Если требуется отправка ввода, открываем канал stdinstdin_channel = PIPE if stdin_input is not None else Noneprocess = Popen(start_commands, stdout=PIPE, stderr=PIPE, stdin=stdin_channel, text=True)if stdin_input is not None and process.stdin:process.stdin.write(stdin_input)process.stdin.flush()stream = process.stderr if read_from_stderr else process.stdoutlines: list[str] = []48 unmodified linesreturn get_revproxy_url(bin_url='https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64',need_unpack=False,bin_path=cloudflare_bin,start_commands=[str(cloudflare_bin), 'tunnel', '--url', f'http://127.0.0.1:{port}'],read_from_stderr=True,lines_to_read=6,url_pattern=r'(?P<url>https?://\S+\.trycloudflare\.com)',45 unmodified linesraise RuntimeError(f'после {max_attempts} попыток поднять туннель градио не удалось: {last_error}')def get_mmar_url(port: int) -> str:return get_revproxy_url(bin_url='https://github.com/yusuf-musleh/mmar/releases/latest/download/mmar_Linux_x86_64.tar.gz',need_unpack=True,bin_path=mmar_bin,start_commands=[str(mmar_bin), 'client', '--local-port', str(port)],read_from_stderr=True,lines_to_read=10,url_pattern=r'(?P<url>https?://\S+\.mmar\.dev)',)def get_tunnelite_url(port: int) -> str:return get_revproxy_url(bin_url='https://github.com/hehepiska/tunnellite-linux/releases/download/latest/tunnelite',need_unpack=False,bin_path=tunnelite_bin,start_commands=[str(tunnelite_bin), f'http://127.0.0.1:{port}'],read_from_stderr=False,lines_to_read=60,url_pattern=r'(?P<url>https?://\S+\.tunnelite\.com)',)def get_beeceptor_url(port: int) -> str:return get_revproxy_url(bin_url='https://cdn.beeceptor.com/downloads/cli/latest/beeceptor-cli-linux-x64.tar.gz',need_unpack=True,bin_path=beeceptor_bin,start_commands=[str(beeceptor_bin), '-p', str(port), '-y'],read_from_stderr=False,lines_to_read=15,url_pattern=r'(?P<url>https?://\S+\.beeceptor\.com)',stdin_input='\n', # Симулируем нажатие клавиши Enter для выбора первого пункта)def get_native_url(port: int) -> str:try:from google.colab.output import eval_js # type: ignore[import]
148 unmodified lines14915015115215315415519 unmodified lines17517617717817918018118218318418518618718818924 unmodified lines2142152162172182192202212222232242252262272282292302312322332346 unmodified lines2412422432442452462474 unmodified lines252253254255256148 unmodified linesdef move_path(old_path: Path | str, new_path: Path | str) -> None:old, new = Path(old_path), Path(new_path)# BUGFIX: оригинал проверял old_path.exists() — падало если old_path это strif not old.exists():raise RuntimeError(f'не найден исходный путь для перемещения: {old}')try:19 unmodified linesreturn bool(pattern.match(address))def is_valid_url(url: str) -> bool:for attempt in range(2):try:with get_url(url, headers=HEADERS, allow_redirects=True, stream=True, timeout=10) as resp:if 200 <= resp.status_code < 300:return Trueexcept Exception:try:resp = get_head(url, headers=HEADERS, allow_redirects=True, timeout=10)if 200 <= resp.status_code < 300:return Trueexcept Exception:24 unmodified linessave_path: str | Path | None = None,progress: bool = True,) -> Path:# Поддержка --header= суффиксаextra_headers: dict | None = Noneurl_parts = url.replace('"', '').replace("'", '').split('--header=')if len(url_parts) > 1:url = url_parts[0].strip()key, _, value = url_parts[1].partition(':') # BUGFIX: partition вместо split(':')[1]extra_headers = {key.strip(): value.strip()}if not is_valid_url(url):raise RuntimeError(f'недействительная ссылка на файл: {url}')save_path = Path(save_path) if save_path else Path.cwd()save_path.mkdir(parents=True, exist_ok=True)with get_url(url, stream=True, allow_redirects=True, headers=extra_headers) as req:file_size = int(req.headers.get('content-length', 0))file_name = filename or get_filename_from_headers(req.headers) or Path(urlparse(req.url).path).namefile_path = save_path / file_name6 unmodified linesfor chunk in req.iter_content(chunk_size=chunk_size):if chunk:fp.write(chunk)if progress and file_size: # BUGFIX: guard деления на 0downloaded += len(chunk)elapsed = time() - startprint(4 unmodified linesraise RuntimeError(f'не удалось загрузить файл по ссылке {url}:\n{e}')if progress and file_size:print() # перевод строки после прогресс-бараreturn file_path148 unmodified lines14915015115215315419 unmodified lines17417517617717817918018118218318418518618718818919019119224 unmodified lines2172182192202212222232242252262272282292302312322332342352362372382392402416 unmodified lines2482492502512522532544 unmodified lines259260261262263148 unmodified linesdef move_path(old_path: Path | str, new_path: Path | str) -> None:old, new = Path(old_path), Path(new_path)if not old.exists():raise RuntimeError(f'не найден исходный путь для перемещения: {old}')try:19 unmodified linesreturn bool(pattern.match(address))def is_valid_url(url: str, custom_headers: dict | None = None) -> bool:req_headers = HEADERS.copy()if custom_headers:req_headers.update(custom_headers)for attempt in range(2):try:with get_url(url, headers=req_headers, allow_redirects=True, stream=True, timeout=10) as resp:if 200 <= resp.status_code < 300:return Trueexcept Exception:try:resp = get_head(url, headers=req_headers, allow_redirects=True, timeout=10)if 200 <= resp.status_code < 300:return Trueexcept Exception:24 unmodified linessave_path: str | Path | None = None,progress: bool = True,) -> Path:extra_headers: dict | None = Noneurl_parts = url.replace('"', '').replace("'", '').split('--header=')if len(url_parts) > 1:url = url_parts[0].strip()key, _, value = url_parts[1].partition(':')extra_headers = {key.strip(): value.strip()}# Проверка валидности с учетом переданных заголовковif not is_valid_url(url, custom_headers=extra_headers):raise RuntimeError(f'недействительная ссылка на файл или доступ ограничен: {url}')save_path = Path(save_path) if save_path else Path.cwd()save_path.mkdir(parents=True, exist_ok=True)req_headers = HEADERS.copy()if extra_headers:req_headers.update(extra_headers)with get_url(url, stream=True, allow_redirects=True, headers=req_headers) as req:file_size = int(req.headers.get('content-length', 0))file_name = filename or get_filename_from_headers(req.headers) or Path(urlparse(req.url).path).namefile_path = save_path / file_name6 unmodified linesfor chunk in req.iter_content(chunk_size=chunk_size):if chunk:fp.write(chunk)if progress and file_size:downloaded += len(chunk)elapsed = time() - startprint(4 unmodified linesraise RuntimeError(f'не удалось загрузить файл по ссылке {url}:\n{e}')if progress and file_size:print()return file_path
colab_tunnel/__init__.py+12-1
colab_tunnel/_config.py+5-1
colab_tunnel/_ssh.pyAdded+175
colab_tunnel/_tunnels.py+63-10
colab_tunnel/_utils.py+18-11