Delete src
Browse filesFiles changed (4)
- src/__init__.pyDeleted-75
- src/_config.pyDeleted-47
- src/_tunnels.pyDeleted-168
- src/_utils.pyDeleted-256
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475"""
colab-proxy — утилиты для туннелирования веб-интерфейсов в Google Colab.
Быстрый старт:
from colab_proxy import get_share_link
link = get_share_link('cloudflared', 7860)
print(link)
"""
from ._config import WORK_FOLDER
from ._utils import run, download
from ._tunnels import (
get_tmole_url,
get_tunwg_url,
get_cloudflared_url,
get_localt_url,
get_gradio_url,
get_native_url,
)
__version__ = '1.0.0'
__all__ = [
'get_share_link',
'try_all',
'proxies_functions',
'get_tmole_url',
'get_tunwg_url',
'get_cloudflared_url',
'get_localt_url',
'get_gradio_url',
'get_native_url',
'download',
'run',
'WORK_FOLDER',
]
proxies_functions: dict = {
'tmole': get_tmole_url,
'tunwg': get_tunwg_url,
'cloudflared': get_cloudflared_url,
'localt': get_localt_url,
'gradio': get_gradio_url,
'native': get_native_url,
}
def try_all(port: int) -> str:
"""Запускает все доступные туннели и возвращает все полученные ссылки."""
results: list[str] = []
for name, func in proxies_functions.items():
try:
results.append(func(port))
except Exception as e:
print(f'{name}: {e}')
return '\n'.join(results)
def get_share_link(host: str, port: int) -> str:
"""
Получить публичную ссылку на локальный порт через указанный туннель.
Args:
host: Имя провайдера: 'cloudflared', 'tmole', 'tunwg',
'localt', 'gradio', 'native' — или 'all' для всех сразу.
port: Локальный порт веб-интерфейса.
Returns:
Публичная URL-ссылка (или несколько, если host='all').
"""
if host in proxies_functions:
return proxies_functions[host](port)
if host == 'all':
return try_all(port)
available = ', '.join(proxies_functions.keys())
return f'провайдер "{host}" не найден. Доступные: {available}'1234567891011121314151617181920212223242526272829303132333435363738394041424344454647from os import environ
from pathlib import Path
# Переопределяется переменной окружения COLAB_PROXY_WORK_DIR
WORK_FOLDER = Path(environ.get('COLAB_PROXY_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'
HEADERS = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'cache-control': 'no-cache',
'device-memory': '8',
'downlink': '10.0',
'dpr': '1.5',
'ect': '4g',
'origin': 'https://colab.research.google.com',
'pragma': 'no-cache',
'priority': 'u=0, i',
'referer': 'https://colab.research.google.com',
'rtt': '200',
'sec-ch-ua': '"Google Chrome";v="148", "Not?A)Brand";v="24", "Chromium";v="148"',
'sec-ch-ua-arch': '"x86"',
'sec-ch-ua-bitness': '"64"',
'sec-ch-ua-full-version': '"148.0.7778.168"',
'sec-ch-ua-full-version-list': '"Google Chrome";v="148.0.7778.168", "Not?A)Brand";v="24.0.0.0", "Chromium";v="148.0.7778.168"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-model': '""',
'sec-ch-ua-platform': '"Windows"',
'sec-ch-ua-platform-version': '"19.0.0"',
'sec-ch-ua-wow64': '?0',
'sec-ch-viewport-height': '592',
'sec-ch-viewport-width': '1216',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'same-origin',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.7778.168 Safari/537.36',
'viewport-width': '1216'
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168from atexit import register as exit_register
from pathlib import Path
from re import search
from subprocess import PIPE, Popen
from time import sleep
from requests import get as get_url
from ._config import (
WORK_FOLDER,
claudflare_bin, tmole_bin, tunwg_bin,
go_localt_bin, gradio_bin, links_file,
)
from ._utils import (
download, unpack_archive, run,
is_process_running, terminate_process,
move_path, is_ipv4,
)
def get_revproxy_url(
bin_url: str,
need_unpack: bool,
bin_path: Path,
start_commands: list,
read_from_stderr: bool,
lines_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_before
if 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)
if 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.stdout
lines: list[str] = []
for _ in range(lines_to_read):
line = stream.readline()
if not line:
break
lines.append(line.strip())
ipv4 = next((ln for ln in lines if is_ipv4(ln)), None)
urls = [search(url_pattern, ln).group() for ln in lines if search(url_pattern, ln)]
if urls:
exit_register(terminate_process, bin_path.name, process)
if write_link:
links_file.write_text(urls[0])
return f'{urls[0]}\n пароль(IP): {ipv4}' if ipv4 else urls[0]
run(f'pkill -f {bin_path.name}')
raise RuntimeError(
f'ссылку получить не удалось, вывод работы бинарника:\n' + '\n'.join(lines)
)
# ---------------------------------------------------------------------------
# Конкретные провайдеры
# ---------------------------------------------------------------------------
def get_tmole_url(port: int) -> str:
return get_revproxy_url(
bin_url='https://tunnelmole.com/downloads/tmole-linux.gz',
need_unpack=True,
bin_path=tmole_bin,
start_commands=[str(tmole_bin), str(port)],
read_from_stderr=False,
lines_to_read=5,
)
def get_tunwg_url(port: int) -> str:
return get_revproxy_url(
bin_url='https://github.com/ntnj/tunwg/releases/latest/download/tunwg',
need_unpack=False,
bin_path=tunwg_bin,
start_commands=[str(tunwg_bin), f'--forward=http://127.0.0.1:{port}'],
read_from_stderr=True,
lines_to_read=2,
)
def get_cloudflared_url(port: int) -> str:
return 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)',
)
def get_localt_url(port: int) -> str:
return get_revproxy_url(
bin_url='https://huggingface.co/prolapse/go_localt/resolve/main/go_localt',
need_unpack=False,
bin_path=go_localt_bin,
start_commands=[str(go_localt_bin), str(port)],
read_from_stderr=False,
lines_to_read=2,
)
def get_gradio_url(port: int) -> str:
max_attempts = 3
last_error: Exception | None = None
for attempt in range(max_attempts):
try:
resp = get_url('https://api.gradio.app/v2/tunnel-request', timeout=10)
resp.raise_for_status()
info = resp.json()[0]
remote_host, remote_port = info['host'], int(info['port'])
cmd = [
str(gradio_bin), 'http', '-n', 'random',
'-l', str(port), '-i', '127.0.0.1',
'--uc', '--sd', 'random', '--ue',
'--server_addr', f'{remote_host}:{remote_port}',
'--disable_log_color',
]
return get_revproxy_url(
bin_url='https://cdn-media.huggingface.co/frpc-gradio-0.1/frpc_linux_amd64',
need_unpack=False,
bin_path=gradio_bin,
start_commands=cmd,
read_from_stderr=False,
lines_to_read=3,
)
except Exception as e:
last_error = e
if attempt < max_attempts - 1:
print(f'попытка {attempt + 1} получить ссылку градио провалилась: {e}. пробуем ещё...')
sleep(5)
raise RuntimeError(f'после {max_attempts} попыток поднять туннель градио не удалось: {last_error}')
def get_native_url(port: int) -> str:
try:
from google.colab.output import eval_js # type: ignore[import]
except ImportError:
raise RuntimeError('get_native_url доступен только в среде Google Colab.')
return eval_js(f'google.colab.kernel.proxyPort({port})')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256from os import name as os_name
from pathlib import Path
from re import compile, search
from shutil import move
from subprocess import PIPE, Popen, STDOUT, check_output, CalledProcessError
from sys import stdout
from time import time
from urllib.parse import unquote, urlparse
from requests import get as get_url, head as get_head
from requests.structures import CaseInsensitiveDict
from ._config import WORK_FOLDER, HEADERS
# ---------------------------------------------------------------------------
# Архивы
# ---------------------------------------------------------------------------
def determine_archive_format(filepath: str | Path) -> str | None:
filepath = Path(filepath)
zip_sig = bytes([0x50, 0x4B, 0x03, 0x04])
seven_z = bytes([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])
lzma_xz = bytes([0xFD, 0x37, 0x7A, 0x58, 0x5A])
tgz = bytes([0x1F, 0x8B])
tbz = bytes([0x42, 0x5A, 0x68])
ustar = bytes([0x75, 0x73, 0x74, 0x61, 0x72])
with filepath.open('rb') as fh:
header = fh.read(262)
if header.startswith(zip_sig):
return 'zip'
if header.startswith(seven_z) or header.startswith(lzma_xz):
return '7z'
if header.startswith(tgz):
return 'tar.gz'
if header.startswith(tbz):
return 'tar.bz2'
if header[0x101:0x101 + len(ustar)] == ustar:
return 'tar'
return None
def unpack_archive(archive_path: str | Path, dest_path: str | Path, rm_archive: bool = True) -> None:
if str(archive_path).startswith(('https://', 'http://')):
archive_path = download(archive_path, save_path=WORK_FOLDER, progress=False)
archive_path = Path(archive_path)
dest_path = Path(dest_path)
if not archive_path.exists():
raise RuntimeError(f'архив {archive_path} не найден.')
fmt = determine_archive_format(archive_path)
def _rm() -> None:
if rm_archive:
archive_path.unlink(missing_ok=True)
try:
if fmt == '7z' or archive_path.suffix == '.7z':
run(f'7z -bso0 -bd -slp -y x "{archive_path}" -o"{dest_path}"')
elif fmt == 'tar' or archive_path.suffix == '.tar':
run(f'tar -xvpf "{archive_path}" -C "{dest_path}"')
elif fmt in ('tar.gz', 'tar.bz2') or archive_path.suffix in ('.tar.gz', '.tar.bz2', '.tar.xz'):
result = run(f'tar -xvzpf "{archive_path}" -C "{dest_path}"')
if result['status_code'] != 0:
run(f'gzip -d "{archive_path}"', dest_path)
elif fmt == 'zip' or archive_path.suffix == '.zip':
run(f'unzip "{archive_path}" -d "{dest_path}"')
else:
run(f'7z -bso0 -bd -slp -y x "{archive_path}" -o"{dest_path}"')
_rm()
except Exception:
try:
run(f'7z -bso0 -bd -mmt4 -slp -y x "{archive_path}" -o"{dest_path}"')
_rm()
except Exception:
raise RuntimeError(
f'формат архива {archive_path.suffix} не определён. '
f'Задайте формат вручную через determine_archive_format.'
)
# ---------------------------------------------------------------------------
# Процессы
# ---------------------------------------------------------------------------
def run(command: str, cwd: str | Path | None = None, live_output: bool = False) -> dict:
process = Popen(command, shell=True, cwd=cwd, stdout=PIPE, stderr=STDOUT)
_encodings = ['utf-8', 'iso-8859-5', 'windows-1251', 'cp866', 'koi8-r', 'mac_cyrillic']
_progress_pattern = r'\d+%|\d+/\d+'
def _decode(data: bytes) -> str:
for enc in _encodings:
try:
return data.decode(enc)
except UnicodeDecodeError:
continue
return data.decode('utf-8', errors='replace')
final_output: list[str] = []
last_progress = ''
for raw_line in iter(process.stdout.readline, b''):
line = _decode(raw_line.strip())
if search(_progress_pattern, line):
last_progress = line
if live_output:
stdout.write('\r' + line)
else:
final_output.append(line)
if live_output:
stdout.write(line + '\n')
if live_output:
stdout.flush()
process.wait()
if last_progress:
final_output.append(last_progress)
return {
'status_code': process.returncode,
'output': '\n'.join(final_output),
}
def is_process_running(process_name: str | Path) -> bool:
try:
output = check_output(['pgrep', '-f', str(process_name)], text=True)
return bool(output.strip())
except CalledProcessError:
return False
def terminate_process(process_name: str, process_obj: Popen) -> None:
try:
process_obj.stdout.close()
except Exception:
pass
try:
process_obj.stderr.close()
except Exception:
pass
process_obj.terminate()
run(f'pkill -f {process_name}')
def 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 это str
if not old.exists():
raise RuntimeError(f'не найден исходный путь для перемещения: {old}')
try:
old.replace(new)
except Exception:
try:
move(old, new)
except Exception:
if os_name == 'posix':
run(f'mv "{old}" "{new}"')
else:
run(f'move "{old}" "{new}"')
# ---------------------------------------------------------------------------
# Сеть
# ---------------------------------------------------------------------------
def is_ipv4(address: str) -> bool:
pattern = compile(
r'^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$'
)
return 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 True
except Exception:
try:
resp = get_head(url, headers=HEADERS, allow_redirects=True, timeout=10)
if 200 <= resp.status_code < 300:
return True
except Exception:
pass
else:
break
return False
def get_filename_from_headers(headers: CaseInsensitiveDict) -> str | None:
cd = headers.get('content-disposition')
if not cd:
return headers.get('filename')
for part in cd.split(';'):
part = part.strip()
if part.startswith('filename*='):
encoding, _, encoded = part[len('filename*='):].partition("''")
return unquote(encoded, encoding=encoding)
if part.startswith('filename='):
return part[len('filename='):].strip('"')
return None
def download(
url: str,
filename: str | Path | None = None,
save_path: str | Path | None = None,
progress: bool = True,
) -> Path:
# Поддержка --header= суффикса
extra_headers: dict | None = None
url_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).name
file_path = save_path / file_name
chunk_size = max(4096, file_size // 2000) if file_size else 4096
downloaded = 0
start = time()
try:
with open(file_path, 'wb') as fp:
for chunk in req.iter_content(chunk_size=chunk_size):
if chunk:
fp.write(chunk)
if progress and file_size: # BUGFIX: guard деления на 0
downloaded += len(chunk)
elapsed = time() - start
print(
f'\rзагрузка {file_name}: {downloaded / file_size * 100:.2f}% | {elapsed:.2f} сек.',
end='',
)
except Exception as e:
raise RuntimeError(f'не удалось загрузить файл по ссылке {url}:\n{e}')
if progress and file_size:
print() # перевод строки после прогресс-бара
return file_pathsrc/__init__.pyDeleted-75
src/_config.pyDeleted-47
src/_tunnels.pyDeleted-168
src/_utils.pyDeleted-256