@@ -1935,192 +1935,6 @@ EOF
19351935 replaced=true
19361936 fi
19371937
1938- # ========== 方法B: b6 定点重写(机器码源函数,仅 main.js) ==========
1939- local b6_patched=false
1940- if [ " $( basename " $file " ) " = " main.js" ]; then
1941- if command -v python3 > /dev/null 2>&1 ; then
1942- local b6_result
1943- b6_result=$( python3 - " $file " " $machine_guid " " $machine_id " << 'PY '
1944- if True:
1945- import re, sys
1946-
1947- def diag(msg):
1948- print(f"[方案B][诊断] {msg}", file=sys.stderr)
1949-
1950- path, machine_guid, machine_id = sys.argv[1], sys.argv[2], sys.argv[3]
1951-
1952- with open(path, "r", encoding="utf-8") as f:
1953- data = f.read()
1954-
1955- # ✅ 1+3 融合:限定 out-build/vs/base/node/id.js 模块内做特征匹配 + 花括号配对定位函数边界
1956- marker = "out-build/vs/base/node/id.js"
1957- marker_index = data.find(marker)
1958- if marker_index < 0:
1959- print("NOT_FOUND")
1960- diag(f"未找到模块标记: {marker}")
1961- raise SystemExit(0)
1962-
1963- window_end = min(len(data), marker_index + 200000)
1964- window = data[marker_index:window_end]
1965-
1966- def find_matching_brace(text, open_index, max_scan=20000):
1967- limit = min(len(text), open_index + max_scan)
1968- depth = 1
1969- in_single = in_double = in_template = False
1970- in_line_comment = in_block_comment = False
1971- escape = False
1972- i = open_index + 1
1973- while i < limit:
1974- ch = text[i]
1975- nxt = text[i + 1] if i + 1 < limit else ""
1976-
1977- if in_line_comment:
1978- if ch == "\n":
1979- in_line_comment = False
1980- i += 1
1981- continue
1982- if in_block_comment:
1983- if ch == "*" and nxt == "/":
1984- in_block_comment = False
1985- i += 2
1986- continue
1987- i += 1
1988- continue
1989-
1990- if in_single:
1991- if escape:
1992- escape = False
1993- elif ch == "\\\\":
1994- escape = True
1995- elif ch == "'":
1996- in_single = False
1997- i += 1
1998- continue
1999- if in_double:
2000- if escape:
2001- escape = False
2002- elif ch == "\\\\":
2003- escape = True
2004- elif ch == '"':
2005- in_double = False
2006- i += 1
2007- continue
2008- if in_template:
2009- if escape:
2010- escape = False
2011- elif ch == "\\\\":
2012- escape = True
2013- elif ch == "`":
2014- in_template = False
2015- i += 1
2016- continue
2017-
2018- if ch == "/" and nxt == "/":
2019- in_line_comment = True
2020- i += 2
2021- continue
2022- if ch == "/" and nxt == "*":
2023- in_block_comment = True
2024- i += 2
2025- continue
2026-
2027- if ch == "'":
2028- in_single = True
2029- i += 1
2030- continue
2031- if ch == '"':
2032- in_double = True
2033- i += 1
2034- continue
2035- if ch == "`":
2036- in_template = True
2037- i += 1
2038- continue
2039-
2040- if ch == "{":
2041- depth += 1
2042- elif ch == "}":
2043- depth -= 1
2044- if depth == 0:
2045- return i
2046-
2047- i += 1
2048- return None
2049-
2050- # 🔧 修复:避免 raw string + 单引号 + ['"] 字符组导致的语法错误;同时修正正则转义,提升 b6 特征匹配命中率
2051- hash_re = re.compile(r"""createHash\(["']sha256["']\)""")
2052- sig_re = re.compile(r'^async function (\w+)\((\w+)\)')
2053-
2054- hash_matches = list(hash_re.finditer(window))
2055- diag(f"marker_index={marker_index} window_len={len(window)} sha256_createHash={len(hash_matches)}")
2056-
2057- for idx, hm in enumerate(hash_matches, start=1):
2058- hash_pos = hm.start()
2059- func_start = window.rfind("async function", 0, hash_pos)
2060- if func_start < 0:
2061- if idx <= 3:
2062- diag(f"候选#{idx}: 未找到 async function 起点")
2063- continue
2064-
2065- open_brace = window.find("{", func_start)
2066- if open_brace < 0:
2067- if idx <= 3:
2068- diag(f"候选#{idx}: 未找到函数起始花括号")
2069- continue
2070-
2071- end_brace = find_matching_brace(window, open_brace, max_scan=20000)
2072- if end_brace is None:
2073- if idx <= 3:
2074- diag(f"候选#{idx}: 花括号配对失败(扫描上限内未闭合)")
2075- continue
2076-
2077- func_text = window[func_start:end_brace + 1]
2078- if len(func_text) > 8000:
2079- if idx <= 3:
2080- diag(f"候选#{idx}: 函数体过长 len={len(func_text)},已跳过")
2081- continue
2082-
2083- sm = sig_re.match(func_text)
2084- if not sm:
2085- if idx <= 3:
2086- diag(f"候选#{idx}: 未解析到函数签名(async function name(param))")
2087- continue
2088- name, param = sm.group(1), sm.group(2)
2089-
2090- # 特征校验:sha256 + hex digest + return param ? raw : hash
2091- has_digest = re.search(r"""\.digest\(["']hex["']\)""", func_text) is not None
2092- has_return = re.search(r'return\s+' + re.escape(param) + r'\?\w+:\w+\}', func_text) is not None
2093- if idx <= 3:
2094- diag(f"候选#{idx}: {name}({param}) len={len(func_text)} digest={has_digest} return={has_return}")
2095- if not has_digest:
2096- continue
2097- if not has_return:
2098- continue
2099-
2100- replacement = f'async function {name}({param}){{return {param}?"{machine_guid}":"{machine_id}";}}'
2101- abs_start = marker_index + func_start
2102- abs_end = marker_index + end_brace
2103- new_data = data[:abs_start] + replacement + data[abs_end + 1:]
2104- with open(path, "w", encoding="utf-8") as f:
2105- f.write(new_data)
2106- diag(f"命中并重写: {name}({param}) len={len(func_text)}")
2107- print("PATCHED")
2108- break
2109- else:
2110- diag("未找到满足特征的候选函数")
2111- print("NOT_FOUND")
2112- PY
2113- )
2114- if [ " $b6_result " = " PATCHED" ]; then
2115- log_info " ✓ [方案B] 已重写 b6 特征函数"
2116- b6_patched=true
2117- else
2118- log_warn " ⚠️ [方案B] 未定位到 b6 特征函数"
2119- fi
2120- else
2121- log_warn " ⚠️ [方案B] 未检测到 python3,跳过 b6 定点重写"
2122- fi
2123- fi
21241938
21251939 # ========== 方法C: Loader Stub 注入 ==========
21261940 local inject_code=' // ========== Cursor Hook Loader 开始 ==========
0 commit comments