Skip to content

Commit d8ad48f

Browse files
committed
feat: 初步支持自动打洞
1 parent b40694b commit d8ad48f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

core/cluster.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import pyzstd as zstd
1313
import aiohttp
1414
from tqdm import tqdm
15-
15+
import subprocess
16+
import re
1617
from . import web
1718
from . import utils, logger, config, scheduler, units, storages, i18n
1819
from .storages import File as SFile
@@ -752,6 +753,34 @@ async def callback(data: tuple[Any, Any]):
752753
scheduler.cancel(timeout_task)
753754
return fut.result()
754755

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+
755784
@dataclass
756785
class ClusterCertificate:
757786
host: str

0 commit comments

Comments
 (0)