wimotek-chromium-sync 0.0.0-stage → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -2
- package/bin/cli.js +15 -0
- package/chromium_sync/__init__.py +7 -0
- package/chromium_sync/cli.py +34 -0
- package/chromium_sync/core.py +290 -0
- package/package.json +22 -4
package/README.md
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
|
-
#
|
|
1
|
+
# wimotek-chromium-sync
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://pypi.org/project/wimotek-chromium-sync/)
|
|
4
|
+
[](https://www.npmjs.com/package/wimotek-chromium-sync)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
专为 Chromium / Chrome 设计的通用会话与 Cookie 穿透热同步工具。
|
|
8
|
+
|
|
9
|
+
支持在本地容器与远程服务器容器之间双向热同步登录凭证(Cookie + LocalStorage),全端口可参数化自定义,内置智能流向识别与带心跳保活的 SSH 隧道自愈管理。
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 🌟 核心特性
|
|
13
|
+
|
|
14
|
+
* 全端口自由定义:本地容器端口、远程实际端口、本地隧道映射端口及 SSH 端口均可自由配置或通过环境变量覆盖。
|
|
15
|
+
* 智能双向流向探测 (auto):连通后自动探测两端 Cookie 数量与页面状态,支持智能推荐、人工交互确认或全自动执行。
|
|
16
|
+
* 内置心跳保活隧道:内置 ServerAliveInterval=15 与端口占用检测,已存在则自动复用,不存在则自动拉起,绝不产生假死僵尸连接。
|
|
17
|
+
* 无 TTY 环境自适应:在 Linux Crontab 定时任务、无图形后台或 CI 流程中自动回退,不因无键盘输入而崩溃。
|
|
18
|
+
* 双生态支持:同时发布于 PyPI 与 npm,支持 Python 代码导入与命令行跨语言一键执行。
|
|
19
|
+
|
|
20
|
+
## 📦 安装
|
|
21
|
+
|
|
22
|
+
Python 环境 (PyPI)
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install wimotek-chromium-sync
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Node.js 环境 (npm)
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install wimotek-chromium-sync
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 🚀 极简使用示例
|
|
35
|
+
|
|
36
|
+
### 1. Python 代码调用(仅需 3 行)
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from chromium_sync import ChromiumSync
|
|
40
|
+
|
|
41
|
+
自由定制两端的所有端口
|
|
42
|
+
|
|
43
|
+
syncer = ChromiumSync( remote_ssh="root@xx.xx.xx", # 远程 SSH 服务器
|
|
44
|
+
remote_ssh_port=22, # SSH 连接端口
|
|
45
|
+
local_cdp_port=16002, # 本地容器的 CDP 端口
|
|
46
|
+
remote_cdp_port=9222, # 远程容器的实际
|
|
47
|
+
CDP 端口 remote_tunnel_port=9222, # 本地用于接收隧道的端口
|
|
48
|
+
direction="auto", # 智能判断: 自动往缺少凭证的一端同步
|
|
49
|
+
timeout=15 # 倒计时等待秒数 (0 为秒执行) )
|
|
50
|
+
|
|
51
|
+
syncer.run()
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 2. 命令行使用 (CLI 模式)
|
|
55
|
+
|
|
56
|
+
无论是通过 pip 安装还是 npm 安装,均可直接在终端中敲命令运行:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
|
|
60
|
+
查看所有端口与流向选项
|
|
61
|
+
|
|
62
|
+
chromium-sync --help
|
|
63
|
+
|
|
64
|
+
自由指定各端口全自动推送 (0 秒等待)
|
|
65
|
+
|
|
66
|
+
chromium-sync
|
|
67
|
+
--remote-ssh root@xx.xx.xx
|
|
68
|
+
--ssh-port 22
|
|
69
|
+
--local-port 9222
|
|
70
|
+
--tunnel-port 9222
|
|
71
|
+
--direction push
|
|
72
|
+
--timeout 0
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## ⚙️ 端口与参数对应说明
|
|
76
|
+
|
|
77
|
+
| 参数名称 | 命令行选项 | 对应环境变量 | 默认值 | 详细说明 |
|
|
78
|
+
| :------------------- | :------------- | :-------------------------- | :--------------------- | :------------------- |
|
|
79
|
+
| remote\_ssh | \--remote-ssh | CHROMIUM\_REMOTE\_SSH | <root@xx.xx.xx> | 远程 SSH 连接主机 |
|
|
80
|
+
| remote\_ssh\_port | \--ssh-port | CHROMIUM\_SSH\_PORT | 22 | 远程服务器的 SSH 端口 |
|
|
81
|
+
| local\_cdp\_port | \--local-port | CHROMIUM\_LOCAL\_PORT | 16002 | 运行在本机的容器 CDP 端口 |
|
|
82
|
+
| remote\_cdp\_port | \--remote-port | CHROMIUM\_REMOTE\_CDP\_PORT | 9222 | 运行在远端服务器上的容器实际端口 |
|
|
83
|
+
| remote\_tunnel\_port | \--tunnel-port | CHROMIUM\_TUNNEL\_PORT | 9222 | SSH 隧道映射到本机的监听端口 |
|
|
84
|
+
| direction | \--direction | CHROMIUM\_DIRECTION | push | 默认同步流向 (push 或 pull 或 auto) |
|
|
85
|
+
| timeout | \--timeout | CHROMIUM\_TIMEOUT | 15 | 倒计时交互等待秒数,0 为免打扰秒执行 |
|
|
86
|
+
| auto\_close\_tunnel | \--auto-close | \- | False | 任务完成后是否关闭创建的 SSH 隧道 |
|
|
87
|
+
|
|
88
|
+
## 🤖 自动化与定时任务配置示例
|
|
89
|
+
配合 Linux Crontab,每天凌晨自动将本地维护好的登录态推送到云端远程容器:
|
|
90
|
+
```bash
|
|
91
|
+
编辑定时任务
|
|
92
|
+
crontab -e
|
|
93
|
+
添加此行:每天凌晨 3:00 自动执行同步且不弹任何提示
|
|
94
|
+
0 3 * * * /usr/local/bin/chromium-sync --direction push --timeout 0 --auto-close >> /tmp/chromium_sync.log 2>&1
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## 📄 License
|
|
98
|
+
|
|
99
|
+
MIT License
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { spawn } = require('child_process');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const cliScript = path.resolve(__dirname, '../chromium_sync/cli.py');
|
|
7
|
+
const args = [cliScript, ...process.argv.slice(2)];
|
|
8
|
+
|
|
9
|
+
const child = spawn('python3', args, {
|
|
10
|
+
stdio: 'inherit'
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
child.on('exit', (code) => {
|
|
14
|
+
process.exit(code || 0);
|
|
15
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
from .core import ChromiumSync
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def main():
|
|
9
|
+
parser = argparse.ArgumentParser(description="Chromium 一体化会话热同步工具")
|
|
10
|
+
parser.add_argument("--remote-ssh", default="root@kr2.wimotek.com", help="远程 SSH 地址")
|
|
11
|
+
parser.add_argument("--ssh-port", type=int, default=22, help="远程 SSH 端口")
|
|
12
|
+
parser.add_argument("--local-port", type=int, default=9223, help="本地容器 CDP 端口")
|
|
13
|
+
parser.add_argument("--remote-port", type=int, default=9222, help="远程容器暴露的端口")
|
|
14
|
+
parser.add_argument("--tunnel-port", type=int, default=9222, help="本地隧道端口")
|
|
15
|
+
parser.add_argument("--direction", choices=["push", "pull", "auto"], default=None, help="同步方向")
|
|
16
|
+
parser.add_argument("--timeout", type=int, default=5, help="倒计时秒数 (设为 0 即秒执行)")
|
|
17
|
+
parser.add_argument("--auto-close", action="store_true", help="任务完成后关闭 SSH 隧道")
|
|
18
|
+
args = parser.parse_args()
|
|
19
|
+
|
|
20
|
+
syncer = ChromiumSync(
|
|
21
|
+
remote_ssh=args.remote_ssh,
|
|
22
|
+
remote_ssh_port=args.ssh_port,
|
|
23
|
+
local_cdp_port=args.local_port,
|
|
24
|
+
remote_cdp_port=args.remote_port,
|
|
25
|
+
remote_tunnel_port=args.tunnel_port,
|
|
26
|
+
direction=args.direction or "auto",
|
|
27
|
+
timeout=args.timeout,
|
|
28
|
+
auto_close_tunnel=args.auto_close
|
|
29
|
+
)
|
|
30
|
+
syncer.run()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
main()
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
import asyncio
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
import select
|
|
8
|
+
import socket
|
|
9
|
+
import subprocess
|
|
10
|
+
import sys
|
|
11
|
+
import time
|
|
12
|
+
import urllib.request
|
|
13
|
+
from typing import Optional
|
|
14
|
+
from playwright.async_api import async_playwright, BrowserContext
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ChromiumSync:
|
|
18
|
+
"""
|
|
19
|
+
通用 Chromium 会话热同步与 SSH 隧道调度器
|
|
20
|
+
"""
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
remote_ssh: str = "root@kr2.wimotek.com",
|
|
24
|
+
remote_ssh_port: int = 22,
|
|
25
|
+
local_cdp_port: int = 9223,
|
|
26
|
+
remote_cdp_port: int = 9222,
|
|
27
|
+
remote_tunnel_port: int = 9222,
|
|
28
|
+
direction: str = "push", # "push", "pull", 或 "auto" (智能判断)
|
|
29
|
+
timeout: int = 5,
|
|
30
|
+
auto_close_tunnel: bool = False,
|
|
31
|
+
verbose: bool = True
|
|
32
|
+
):
|
|
33
|
+
self.remote_ssh = os.getenv("CHROMIUM_REMOTE_SSH", remote_ssh)
|
|
34
|
+
self.remote_ssh_port = int(os.getenv("CHROMIUM_SSH_PORT", str(remote_ssh_port)))
|
|
35
|
+
self.local_cdp_port = int(os.getenv("CHROMIUM_LOCAL_PORT", str(local_cdp_port)))
|
|
36
|
+
self.remote_cdp_port = int(os.getenv("CHROMIUM_REMOTE_CDP_PORT", str(remote_cdp_port)))
|
|
37
|
+
self.remote_tunnel_port = int(os.getenv("CHROMIUM_TUNNEL_PORT", str(remote_tunnel_port)))
|
|
38
|
+
|
|
39
|
+
self.default_direction = os.getenv("CHROMIUM_DIRECTION", direction).lower()
|
|
40
|
+
self.timeout = int(os.getenv("CHROMIUM_TIMEOUT", str(timeout)))
|
|
41
|
+
self.auto_close_tunnel = auto_close_tunnel
|
|
42
|
+
|
|
43
|
+
self.remote_cdp_url = f"http://127.0.0.1:{self.remote_tunnel_port}"
|
|
44
|
+
self.local_cdp_url = f"http://127.0.0.1:{self.local_cdp_port}"
|
|
45
|
+
|
|
46
|
+
self.tunnel_proc: Optional[subprocess.Popen] = None
|
|
47
|
+
self.is_reused_tunnel: bool = False
|
|
48
|
+
|
|
49
|
+
log_level = logging.INFO if verbose else logging.WARNING
|
|
50
|
+
logging.basicConfig(level=log_level, format="%(asctime)s [%(levelname)s] %(message)s")
|
|
51
|
+
|
|
52
|
+
if self.local_cdp_port == self.remote_tunnel_port:
|
|
53
|
+
logging.warning(
|
|
54
|
+
f"[警告] 本地容器端口与隧道映射端口重复 ({self.local_cdp_port})!建议区分 (如 9223 与 9222)。"
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def _is_port_in_use(self, port: int) -> bool:
|
|
58
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
59
|
+
s.settimeout(0.5)
|
|
60
|
+
return s.connect_ex(("127.0.0.1", port)) == 0
|
|
61
|
+
|
|
62
|
+
def _is_cdp_ready(self, url: str) -> bool:
|
|
63
|
+
try:
|
|
64
|
+
proxy_handler = urllib.request.ProxyHandler({})
|
|
65
|
+
opener = urllib.request.build_opener(proxy_handler)
|
|
66
|
+
resp = opener.open(f"{url}/json/version", timeout=2)
|
|
67
|
+
return resp.getcode() == 200
|
|
68
|
+
except Exception:
|
|
69
|
+
return False
|
|
70
|
+
|
|
71
|
+
def _start_tunnel(self):
|
|
72
|
+
if self._is_port_in_use(self.remote_tunnel_port):
|
|
73
|
+
logging.info(f"[*] 本地 {self.remote_tunnel_port} 端口已被监听,跳过执行 SSH 命令直接复用。")
|
|
74
|
+
self.is_reused_tunnel = True
|
|
75
|
+
return
|
|
76
|
+
|
|
77
|
+
logging.info(
|
|
78
|
+
f"[*] 正在拉起 SSH 隧道: {self.remote_ssh}:{self.remote_ssh_port} "
|
|
79
|
+
f"(映射本地 {self.remote_tunnel_port} -> 远程 {self.remote_cdp_port}) ..."
|
|
80
|
+
)
|
|
81
|
+
cmd = [
|
|
82
|
+
"ssh", "-N",
|
|
83
|
+
"-p", str(self.remote_ssh_port),
|
|
84
|
+
"-o", "ServerAliveInterval=15",
|
|
85
|
+
"-o", "ServerAliveCountMax=3",
|
|
86
|
+
"-o", "TCPKeepAlive=yes",
|
|
87
|
+
"-o", "ExitOnForwardFailure=yes",
|
|
88
|
+
"-L", f"{self.remote_tunnel_port}:127.0.0.1:{self.remote_cdp_port}",
|
|
89
|
+
self.remote_ssh
|
|
90
|
+
]
|
|
91
|
+
self.tunnel_proc = subprocess.Popen(
|
|
92
|
+
cmd,
|
|
93
|
+
stdout=subprocess.DEVNULL,
|
|
94
|
+
stderr=subprocess.DEVNULL,
|
|
95
|
+
start_new_session=(not self.auto_close_tunnel)
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
for _ in range(20):
|
|
99
|
+
time.sleep(0.5)
|
|
100
|
+
if self._is_port_in_use(self.remote_tunnel_port) and self._is_cdp_ready(self.remote_cdp_url):
|
|
101
|
+
logging.info(f"[+] SSH 隧道建立成功!端口 {self.remote_tunnel_port} 已就绪。")
|
|
102
|
+
return
|
|
103
|
+
if self.tunnel_proc.poll() is not None:
|
|
104
|
+
break
|
|
105
|
+
|
|
106
|
+
self._stop_tunnel(force=True)
|
|
107
|
+
raise RuntimeError("[-] 建立 SSH 隧道失败,请检查网络或 SSH 免密连接配置!")
|
|
108
|
+
|
|
109
|
+
def _stop_tunnel(self, force: bool = False):
|
|
110
|
+
if self.is_reused_tunnel and not force:
|
|
111
|
+
return
|
|
112
|
+
if self.tunnel_proc and self.tunnel_proc.poll() is None:
|
|
113
|
+
if self.auto_close_tunnel or force:
|
|
114
|
+
logging.info(f"[*] 正在关闭 SSH 隧道 (PID: {self.tunnel_proc.pid})...")
|
|
115
|
+
self.tunnel_proc.terminate()
|
|
116
|
+
self.tunnel_proc.wait()
|
|
117
|
+
logging.info("[*] 隧道已释放。")
|
|
118
|
+
else:
|
|
119
|
+
logging.info("[*] 提示:SSH 隧道已保留在后台持续运行。")
|
|
120
|
+
|
|
121
|
+
def _timed_input(self, prompt: str, timeout: int, default_val: str) -> str:
|
|
122
|
+
# 非交互式环境 (例如 cron / 后台运行),直接返回默认值
|
|
123
|
+
if not sys.stdin or not sys.stdin.isatty() or timeout <= 0:
|
|
124
|
+
sys.stdout.write(f"\n[*] 非交互环境或超时设为 0,自动采用默认值: {default_val}\n")
|
|
125
|
+
return default_val
|
|
126
|
+
|
|
127
|
+
sys.stdout.write(prompt)
|
|
128
|
+
sys.stdout.flush()
|
|
129
|
+
|
|
130
|
+
if sys.platform != "win32":
|
|
131
|
+
ready, _, _ = select.select([sys.stdin], [], [], timeout)
|
|
132
|
+
if ready:
|
|
133
|
+
line = sys.stdin.readline().strip()
|
|
134
|
+
return line if line else default_val
|
|
135
|
+
sys.stdout.write(f"\n[*] 超过 {timeout} 秒无操作,自动执行默认流向: {default_val}\n")
|
|
136
|
+
return default_val
|
|
137
|
+
else:
|
|
138
|
+
start_time = time.time()
|
|
139
|
+
import msvcrt
|
|
140
|
+
input_chars = []
|
|
141
|
+
while True:
|
|
142
|
+
if msvcrt.kbhit():
|
|
143
|
+
char = msvcrt.getwche()
|
|
144
|
+
if char in ("\r", "\n"):
|
|
145
|
+
print()
|
|
146
|
+
line = "".join(input_chars).strip()
|
|
147
|
+
return line if line else default_val
|
|
148
|
+
input_chars.append(char)
|
|
149
|
+
if time.time() - start_time > timeout:
|
|
150
|
+
print(f"\n[*] 超过 {timeout} 秒无操作,自动执行默认流向: {default_val}")
|
|
151
|
+
return default_val
|
|
152
|
+
time.sleep(0.05)
|
|
153
|
+
|
|
154
|
+
async def _get_info(self, context: BrowserContext, name: str) -> dict:
|
|
155
|
+
cookies = await context.cookies()
|
|
156
|
+
page_info = "无打开页面"
|
|
157
|
+
page_url = ""
|
|
158
|
+
if context.pages:
|
|
159
|
+
p = context.pages[0]
|
|
160
|
+
page_url = p.url
|
|
161
|
+
try:
|
|
162
|
+
title = await p.title()
|
|
163
|
+
page_info = f"[{title}] {p.url}"
|
|
164
|
+
except Exception:
|
|
165
|
+
page_info = p.url
|
|
166
|
+
return {"name": name, "cookie_count": len(cookies), "page_info": page_info, "page_url": page_url}
|
|
167
|
+
|
|
168
|
+
async def _transfer(self, source: BrowserContext, target: BrowserContext, from_name: str, to_name: str):
|
|
169
|
+
logging.info(f"[*] 开始同步: 【{from_name}】 ---> 【{to_name}】")
|
|
170
|
+
cookies = await source.cookies()
|
|
171
|
+
storage_state = await source.storage_state()
|
|
172
|
+
logging.info(f" -> 提取到 {len(cookies)} 个 Cookie")
|
|
173
|
+
|
|
174
|
+
if cookies:
|
|
175
|
+
await target.add_cookies(cookies)
|
|
176
|
+
|
|
177
|
+
if target.pages:
|
|
178
|
+
p = target.pages[0]
|
|
179
|
+
for origin in storage_state.get("origins", []):
|
|
180
|
+
origin_url = origin.get("origin", "")
|
|
181
|
+
items = origin.get("localStorage", [])
|
|
182
|
+
if items:
|
|
183
|
+
# 如果当前是空白页,尝试导航至目标源以允许写入 LocalStorage
|
|
184
|
+
if p.url.startswith("about:") and origin_url:
|
|
185
|
+
try:
|
|
186
|
+
await p.goto(origin_url)
|
|
187
|
+
except Exception:
|
|
188
|
+
pass
|
|
189
|
+
|
|
190
|
+
try:
|
|
191
|
+
await p.evaluate(
|
|
192
|
+
"""(items) => {
|
|
193
|
+
for (const item of items) {
|
|
194
|
+
try { window.localStorage.setItem(item.name, item.value); } catch(e){}
|
|
195
|
+
}
|
|
196
|
+
}""",
|
|
197
|
+
items
|
|
198
|
+
)
|
|
199
|
+
except Exception as e:
|
|
200
|
+
logging.debug(f"LocalStorage 写入跳过: {e}")
|
|
201
|
+
try:
|
|
202
|
+
await p.reload()
|
|
203
|
+
logging.info(f" -> 【{to_name}】页面已自动刷新生效!")
|
|
204
|
+
except Exception:
|
|
205
|
+
pass
|
|
206
|
+
|
|
207
|
+
logging.info(f"[+] 同步完成!【{to_name}】已成功获得登录状态。\n")
|
|
208
|
+
|
|
209
|
+
async def _run_async(self, direction_override: Optional[str] = None, timeout_override: Optional[int] = None):
|
|
210
|
+
target_dir = direction_override.lower() if direction_override else None
|
|
211
|
+
target_timeout = timeout_override if timeout_override is not None else self.timeout
|
|
212
|
+
|
|
213
|
+
self._start_tunnel()
|
|
214
|
+
try:
|
|
215
|
+
async with async_playwright() as p:
|
|
216
|
+
logging.info("正在连通【本地】与【远程】容器 CDP 接口...")
|
|
217
|
+
try:
|
|
218
|
+
browser_remote = await p.chromium.connect_over_cdp(self.remote_cdp_url)
|
|
219
|
+
ctx_remote = browser_remote.contexts[0] if browser_remote.contexts else await browser_remote.new_context()
|
|
220
|
+
except Exception as e:
|
|
221
|
+
logging.error(f"连接【远程容器】失败 ({self.remote_cdp_url}): {e}")
|
|
222
|
+
return
|
|
223
|
+
|
|
224
|
+
try:
|
|
225
|
+
browser_local = await p.chromium.connect_over_cdp(self.local_cdp_url)
|
|
226
|
+
ctx_local = browser_local.contexts[0] if browser_local.contexts else await browser_local.new_context()
|
|
227
|
+
except Exception as e:
|
|
228
|
+
logging.error(f"连接【本地容器】失败 ({self.local_cdp_url}): {e}")
|
|
229
|
+
await browser_remote.close()
|
|
230
|
+
return
|
|
231
|
+
|
|
232
|
+
info_r = await self._get_info(ctx_remote, "远程容器")
|
|
233
|
+
info_l = await self._get_info(ctx_local, "本地容器")
|
|
234
|
+
|
|
235
|
+
print("\n" + "=" * 65)
|
|
236
|
+
print(" 两端容器状态探测就绪")
|
|
237
|
+
print("=" * 65)
|
|
238
|
+
print(f" [本地] URL/标题 : {info_l['page_info']}")
|
|
239
|
+
print(f" Cookie数 : {info_l['cookie_count']} 条 (端口: {self.local_cdp_port})")
|
|
240
|
+
print("-" * 65)
|
|
241
|
+
print(f" [远程] URL/标题 : {info_r['page_info']}")
|
|
242
|
+
print(f" Cookie数 : {info_r['cookie_count']} 条 (端口: {self.remote_tunnel_port})")
|
|
243
|
+
print("=" * 65 + "\n")
|
|
244
|
+
|
|
245
|
+
# 智能识别推荐
|
|
246
|
+
smart_recommend = self.default_direction
|
|
247
|
+
if self.default_direction == "auto":
|
|
248
|
+
if info_r['cookie_count'] > info_l['cookie_count']:
|
|
249
|
+
smart_recommend = "pull"
|
|
250
|
+
else:
|
|
251
|
+
smart_recommend = "push"
|
|
252
|
+
|
|
253
|
+
if not target_dir:
|
|
254
|
+
def_key = "1" if smart_recommend == "push" else "2"
|
|
255
|
+
def_text = "Push (本地->远程)" if smart_recommend == "push" else "Pull (远程->本地)"
|
|
256
|
+
|
|
257
|
+
print(f"请选择同步流向(等待 {target_timeout} 秒自动执行默认操作):")
|
|
258
|
+
print(" 1. 本地 -> 远程 (Push: 推送本地凭证至远程) " + ("[默认]" if smart_recommend == "push" else ""))
|
|
259
|
+
print(" 2. 远程 -> 本地 (Pull: 拉取远程凭证到本地) " + ("[默认]" if smart_recommend == "pull" else ""))
|
|
260
|
+
print(" 0. 退出 (仅查看状态,不执行同步)")
|
|
261
|
+
|
|
262
|
+
prompt = f"\n请输入选项 [1/2/0] (默认 {def_key} [{def_text}]): "
|
|
263
|
+
choice = await asyncio.to_thread(self._timed_input, prompt, target_timeout, def_key)
|
|
264
|
+
choice = choice.strip()
|
|
265
|
+
|
|
266
|
+
if choice == "1":
|
|
267
|
+
target_dir = "push"
|
|
268
|
+
elif choice == "2":
|
|
269
|
+
target_dir = "pull"
|
|
270
|
+
elif choice == "0":
|
|
271
|
+
logging.info("[*] 取消同步,安全退出。")
|
|
272
|
+
await browser_remote.close()
|
|
273
|
+
await browser_local.close()
|
|
274
|
+
return
|
|
275
|
+
else:
|
|
276
|
+
target_dir = smart_recommend
|
|
277
|
+
|
|
278
|
+
if target_dir == "push":
|
|
279
|
+
await self._transfer(ctx_local, ctx_remote, "本地容器", "远程容器")
|
|
280
|
+
elif target_dir == "pull":
|
|
281
|
+
await self._transfer(ctx_remote, ctx_local, "远程容器", "本地容器")
|
|
282
|
+
|
|
283
|
+
await browser_remote.close()
|
|
284
|
+
await browser_local.close()
|
|
285
|
+
finally:
|
|
286
|
+
self._stop_tunnel()
|
|
287
|
+
|
|
288
|
+
def run(self, direction: Optional[str] = None, timeout: Optional[int] = None):
|
|
289
|
+
"""同步运行入口 (开箱即用)"""
|
|
290
|
+
asyncio.run(self._run_async(direction_override=direction, timeout_override=timeout))
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wimotek-chromium-sync",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Universal Chromium Session & Cookie Synchronizer with SSH Tunnel",
|
|
5
|
+
"main": "chromium_sync/__init__.py",
|
|
6
|
+
"bin": {
|
|
7
|
+
"chromium-sync": "./bin/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"chromium_sync",
|
|
11
|
+
"bin",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"keywords": [
|
|
16
|
+
"chromium",
|
|
17
|
+
"cdp",
|
|
18
|
+
"cookie-sync",
|
|
19
|
+
"playwright",
|
|
20
|
+
"ssh-tunnel"
|
|
21
|
+
],
|
|
22
|
+
"author": "wimotek",
|
|
23
|
+
"license": "MIT"
|
|
24
|
+
}
|