|
12 | 12 | import pyzstd as zstd |
13 | 13 | import aiohttp |
14 | 14 | from tqdm import tqdm |
15 | | - |
| 15 | +import subprocess |
| 16 | +import re |
16 | 17 | from . import web |
17 | 18 | from . import utils, logger, config, scheduler, units, storages, i18n |
18 | 19 | from .storages import File as SFile |
@@ -752,6 +753,34 @@ async def callback(data: tuple[Any, Any]): |
752 | 753 | scheduler.cancel(timeout_task) |
753 | 754 | return fut.result() |
754 | 755 |
|
| 756 | +class Tunnel(): |
| 757 | + def __init__(self,tunnel_program: str,output_regex: str,timeout: int=30): |
| 758 | + self.tunnel_program = tunnel_program |
| 759 | + self.output_regex = output_regex |
| 760 | + self.timeout = timeout |
| 761 | + self.output_logs = [] |
| 762 | + async def create_tunnel(self): |
| 763 | + process = subprocess.Popen( |
| 764 | + [self.tunnel_program], |
| 765 | + stdout=subprocess.PIPE, |
| 766 | + stderr=subprocess.PIPE, |
| 767 | + text=True |
| 768 | + ) |
| 769 | + for line in process.stdout: |
| 770 | + self.output_logs.append(line.strip()) |
| 771 | + async def get_tunnel_info(self): |
| 772 | + regex = re.compile(self.output_regex) |
| 773 | + for log in self.output_logs: |
| 774 | + match = regex.search(log) |
| 775 | + if match: |
| 776 | + host = match.group('host') |
| 777 | + port = match.group('port') |
| 778 | + return {'host': host, 'port': port} |
| 779 | + raise ValueError("无法获取隧道信息") |
| 780 | + async def get_port(self): |
| 781 | + return (await self.get_tunnel_info()).get('port',1145) |
| 782 | + |
| 783 | + |
755 | 784 | @dataclass |
756 | 785 | class ClusterCertificate: |
757 | 786 | host: str |
|
0 commit comments