fix(proxy): 青果检测改真探并按供应商分码,三面收口凭据外漏与「无法判定」
用户报「青果也要加检测」,往下挖出四层假象,一次收口: - 后端:probeProxy 的青果短路(过去无条件回「提取成功(青果隧道端点,连接鉴权在客户端)」, 依据的注释已过期——存下来的链接里就带 key/pwd)删掉,改成真打一次提取,按供应商返回码 分档出独立 check_code:channel_busy / invalid_key / forbidden / rate_limited / insufficient_balance / tunnel_unverified。文案出口一律抹 key/pwd。 - 后端:巡检遇 channel_busy 不落库也不推通知。通道被占不是「密钥坏了」而是「此刻问不出 结论」,而巡检自己那一发真提取就要占掉唯一一条通道——落库等于把用户早上看到的 「代理异常」写成我们自己造成的。 - 后端:余量口径按供应商分档。青果账号此前会落进「否则查平台套餐」那条,界面显示的是 别人家的 jikip 余量(串档);青果不提供可鉴权的余量/余额查询(余额接口是控制台 appid/appkey 签名档),这一档宁可返回「查不了」也不借极客的数。 - 后端:SecretMasking.maskProxy 的脏值回落不再套 mask()——它保后 4 字符,而青果的 pwd= 恰好挂在末尾,等于把口令尾 4 位交给界面与日志;改成只交「像 host 的那一段」。 - 用户端:去掉「青果账密直连,无需检测」的 disabled 例外,未保存账密现拼链接送检; 「无法判定」回落改成后端文案原样显示+可重试语义;代理卡不再套 LLM 渠道字典 (「龙抬头」错标的真根因就在这行)。 - 后管:货源标签拆成两张互不相通的字典,代理列只读 masked 且不回落到原文,删掉 user-select:all 的全选复制旁路,「修改代理」改成重填 Authkey/Authpwd 的弹窗。 判据:SecretMaskingTest(5)、UserProxyBalanceServiceTest(6)、UserApiSecretServiceTest (+1 巡检不落库)、UserApiSecretCheckServiceTest(+138 行分码)、ProxyLinkKeyTest、 用户端 node 5 支+mount 10 支、后管 node 9 支+mount 8 支。后端整量 5187/0 失败, full 闸门四面绿。
This commit is contained in:
@@ -33,7 +33,7 @@ public final class SecretMasking {
|
||||
try {
|
||||
URI uri = URI.create(value.trim());
|
||||
if (uri.getHost() == null || uri.getHost().isBlank()) {
|
||||
return mask(value);
|
||||
return hostOnlyOrHidden(value);
|
||||
}
|
||||
StringBuilder masked = new StringBuilder();
|
||||
masked.append(uri.getScheme() == null ? "http" : uri.getScheme()).append("://");
|
||||
@@ -46,7 +46,37 @@ public final class SecretMasking {
|
||||
}
|
||||
return masked.toString();
|
||||
} catch (Exception ex) {
|
||||
return mask(value);
|
||||
return hostOnlyOrHidden(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析不出 host 的代理串(无 scheme 的 {@code host/get?key=..&pwd=..}、脏值、纯凭据串)走这里。
|
||||
*
|
||||
* <p>不能套 {@link #mask}:它保前 4 <b>和后 4</b>,而青果链接的 {@code pwd=} 就挂在末尾——
|
||||
* 那等于把口令尾 4 位交给界面与日志(2026-11-02 后管「密钥管理」列的残余外漏形状)。
|
||||
* 这里只肯交出「像 host 的那一段」,其余一个字符都不给。</p>
|
||||
*/
|
||||
private static String hostOnlyOrHidden(String value) {
|
||||
String text = value.trim();
|
||||
int scheme = text.indexOf("://");
|
||||
String rest = scheme >= 0 ? text.substring(scheme + 3) : text;
|
||||
int at = rest.lastIndexOf('@');
|
||||
if (at >= 0) {
|
||||
rest = rest.substring(at + 1);
|
||||
}
|
||||
int cut = rest.length();
|
||||
for (char boundary : new char[]{'/', '?', '#'}) {
|
||||
int index = rest.indexOf(boundary);
|
||||
if (index >= 0) {
|
||||
cut = Math.min(cut, index);
|
||||
}
|
||||
}
|
||||
String host = rest.substring(0, cut);
|
||||
// 没有点号的不认 host(多半是裸凭据),带 = 的更可能是整串 query
|
||||
if (host.isBlank() || !host.contains(".") || host.contains("=")) {
|
||||
return "****";
|
||||
}
|
||||
return "****" + host;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -18,10 +18,10 @@ public class AdminUserSecretModuleVo {
|
||||
@Schema(description = "货源:LLM 行为渠道(longtaitou/t8star),代理行为供应商(qingguo/jikip)")
|
||||
private String channel;
|
||||
|
||||
@Schema(description = "脱敏值")
|
||||
@Schema(description = "脱敏值(代理列为 scheme://host[:port],不含链接里的 key/pwd)")
|
||||
private String masked;
|
||||
|
||||
@Schema(description = "完整明文值:仅代理列返回(含账号密码),密钥列为空")
|
||||
@Schema(description = "完整明文值:后台侧一律不再下发(2026-11-02 收口,代理链接里带账密);字段保留只为界面兼容,恒为空")
|
||||
private String full;
|
||||
|
||||
@Schema(description = "是否已配置")
|
||||
|
||||
+138
-26
@@ -8,6 +8,7 @@ import com.nanri.aiimage.config.HttpClientPool;
|
||||
import com.nanri.aiimage.config.SimilarAsinProperties;
|
||||
import com.nanri.aiimage.config.UserSecretProperties;
|
||||
import com.nanri.aiimage.modules.usersecret.client.JikipProxyClient;
|
||||
import com.nanri.aiimage.modules.usersecret.support.ProxyLinkKey;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
import com.nanri.aiimage.modules.usersecret.support.ProxyProvider;
|
||||
import com.nanri.aiimage.modules.usersecret.support.LlmChannel;
|
||||
@@ -56,10 +57,25 @@ public class UserApiSecretCheckService {
|
||||
public static final String CODE_TIMEOUT = "timeout";
|
||||
public static final String CODE_PROVIDER_ERROR = "provider_error";
|
||||
public static final String CODE_INSUFFICIENT_BALANCE = "insufficient_balance";
|
||||
/**
|
||||
* 青果通道被占满({@code NO_AVAILABLE_CHANNEL}):隧道转发按存活周期只给一条通道,
|
||||
* 检测里这一次提取就是在抢通道;抢不到是「稍后再点」,不是「密钥坏了」,必须与
|
||||
* {@link #CODE_PROVIDER_ERROR} 分开——混成一个码,界面只能显示「无法判定」(现网 uid=1301)。
|
||||
*/
|
||||
public static final String CODE_CHANNEL_BUSY = "channel_busy";
|
||||
/** 隧道端点拿到了,但服务端组不出可验连的地址(链接未带 key/pwd,或账密含会切歪 host 的分隔符)。 */
|
||||
public static final String CODE_TUNNEL_UNVERIFIED = "tunnel_unverified";
|
||||
|
||||
/** 供应商欠费提示文案(前后端都按 code 识别展示)。 */
|
||||
public static final String INSUFFICIENT_BALANCE_MESSAGE = "代理服务商余额不足,请充值后重试";
|
||||
|
||||
/** 通道被自己占用的提示文案:说人话,并给出可执行的下一步(等存活周期释放)。 */
|
||||
public static final String CHANNEL_BUSY_MESSAGE = "青果通道正被你自己的采集占着,等约 3 分钟释放后再点检测";
|
||||
|
||||
/** 提取成功但服务端无法验连的提示文案:如实说"没验成",不拿它冒充 passed。 */
|
||||
public static final String TUNNEL_UNVERIFIED_MESSAGE = "青果提取成功,但这条链接里没有可用于隧道验连的 key/pwd,"
|
||||
+ "服务端无法确认代理是否可用(青果只支持账密档,请在密钥设置里补全提取链接)";
|
||||
|
||||
/** 超时与网络不可达提示文案:面向用户展示,技术细节只进服务端日志。 */
|
||||
public static final String TIMEOUT_MESSAGE = "检测超时:上游服务响应超时,请稍后重试";
|
||||
public static final String NETWORK_ERROR_MESSAGE = "网络不可达:无法连接上游服务,请检查网络后重试";
|
||||
@@ -78,6 +94,12 @@ public class UserApiSecretCheckService {
|
||||
private static final Pattern IP_PORT_PATTERN =
|
||||
Pattern.compile("(\\d{1,3}(?:\\.\\d{1,3}){3}):(\\d{2,5})");
|
||||
|
||||
/** 供应商文案里可能回显的凭据型查询参数(值一律盖掉)。 */
|
||||
private static final Pattern CREDENTIAL_PARAM_PATTERN =
|
||||
Pattern.compile("(?i)\\b(key|pwd|password|passwd|token|secret)=([^&\\s\"]+)");
|
||||
/** 短于这个长度的值不按原文替换:像 "0"/"ab" 这种会误伤正常文案。 */
|
||||
private static final int MIN_REDACT_LENGTH = 4;
|
||||
|
||||
private final AppearancePatentProperties appearancePatentProperties;
|
||||
private final SimilarAsinProperties similarAsinProperties;
|
||||
private final UserSecretProperties userSecretProperties;
|
||||
@@ -168,13 +190,19 @@ public class UserApiSecretCheckService {
|
||||
|
||||
/**
|
||||
* 代理配置探测(按「提取链接」语义,与品牌检测的实际用法一致):
|
||||
* 先 GET 用户链接真实提取一次——余额不足报余额不足、解析不出 IP 报配置错误;
|
||||
* 提取到 IP(可含账密)后经该代理请求一次自家域名,拿到任意 HTTP 响应即转发可用。
|
||||
* 先 GET 用户链接真实提取一次——余额不足报余额不足、通道占用报通道占用、解析不出 IP 报配置错误;
|
||||
* 提取到端点后经它请求一次自家域名,拿到任意 HTTP 响应才算转发可用。
|
||||
* 兼容纯静态代理地址(无 path/query 的 host:port)直接探测。
|
||||
*
|
||||
* <p>2026-09-13 修复:旧实现把提取链接直接交给 HttpClientPool 当静态代理使用,链接无
|
||||
* 显式端口时解析失败会静默回退直连,探测到的是自家站点的直连响应——40 个用户全部
|
||||
* 「假通过」(3-39ms + HTTP 301),余额不足 / 链接失效都测不出来。
|
||||
* 「假通过」(3-39ms + HTTP 301),余额不足 / 链接失效都测不出来。</p>
|
||||
*
|
||||
* <p>2026-11-02 修复(青果):那次修复只覆盖了极客,青果分支仍在看不到账密的借口下
|
||||
* 「提取成功=passed」短路。现在青果也走真实的经隧道转发探测,供应商返回码逐档落独立
|
||||
* code(见 {@link #classifyProviderRejection})。副作用要如实登记:这一次提取会占掉
|
||||
* 青果那唯一一条通道(存活周期约 3 分钟),所以「点检测」本身是有一次通道成本的,
|
||||
* 服务层的新鲜期复用({@code reuseFreshOutcome})是它唯一的挡箭牌。</p>
|
||||
*/
|
||||
private CheckOutcome probeProxy(String proxyUrl) {
|
||||
return probeProxy(proxyUrl, null);
|
||||
@@ -200,8 +228,8 @@ public class UserApiSecretCheckService {
|
||||
}
|
||||
|
||||
// 提取链接:先真实提取一次。刻意读响应体而不依赖 2xx——
|
||||
// 青果的失败码(API_AUTH_DENY / BALANCE_INSUFFICIENT)走 HTTP 4xx,
|
||||
// 交给 RestClient 抛异常会把「密钥没授权」误报成「接口无法访问」。
|
||||
// 青果的失败码(API_AUTH_DENY / BALANCE_INSUFFICIENT / NO_AVAILABLE_CHANNEL)走 HTTP 4xx,
|
||||
// 交给 RestClient 抛异常会把「密钥没授权」「通道被占」统统误报成「接口无法访问」。
|
||||
StatusAndBody extract;
|
||||
try {
|
||||
extract = clientFor(null).get().uri(normalized)
|
||||
@@ -216,7 +244,7 @@ public class UserApiSecretCheckService {
|
||||
"代理提取接口无法访问,请检查提取链接或稍后重试", (int) latency, false);
|
||||
}
|
||||
long extractLatency = System.currentTimeMillis() - startMillis;
|
||||
CheckOutcome rejection = classifyProviderRejection(provider, extract.body(), (int) extractLatency);
|
||||
CheckOutcome rejection = classifyProviderRejection(provider, extract.body(), (int) extractLatency, normalized);
|
||||
if (rejection != null) {
|
||||
log.warn("[user-secret][check] 代理提取被供应商拒绝 provider={} httpStatus={} code={}",
|
||||
provider.key(), extract.statusCode(), rejection.code());
|
||||
@@ -225,33 +253,95 @@ public class UserApiSecretCheckService {
|
||||
String extractedProxyUrl = extractProxyUrlFromBody(extract.body());
|
||||
if (extractedProxyUrl == null) {
|
||||
log.warn("[user-secret][check] 代理提取响应未解析出 IP provider={} httpStatus={} 响应前 150 字={}",
|
||||
provider.key(), extract.statusCode(), abbreviate(extract.body(), 150));
|
||||
provider.key(), extract.statusCode(),
|
||||
redactCredentials(abbreviate(extract.body(), 150), normalized));
|
||||
return new CheckOutcome(STATUS_FAILED, CODE_INVALID_KEY,
|
||||
"链接未返回可用 IP,请检查代理提取配置", (int) extractLatency, false);
|
||||
}
|
||||
if (provider == ProxyProvider.QINGGUO) {
|
||||
// 青果通道提取返回的是**隧道端点**(如 tunpool-xxx.qg.net:16359),连它要用
|
||||
// Authkey/Authpwd 做 Basic 认证,而这对账密只存在用户本机、不上报服务端;
|
||||
// 所以这里只验"提取接口可用且拿到端点",连接态由客户端的实际任务证明。
|
||||
log.info("[user-secret][check] 青果提取成功,跳过经隧道探测(端点鉴权在客户端)endpoint={}",
|
||||
SecretMasking.maskProxy(extractedProxyUrl));
|
||||
return new CheckOutcome(STATUS_PASSED, CODE_OK,
|
||||
"提取成功(青果隧道端点,连接鉴权在客户端)", (int) extractLatency, false);
|
||||
return probeQingguoTunnel(normalized, extractedProxyUrl, startMillis);
|
||||
}
|
||||
return probeThroughProxy(extractedProxyUrl, startMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
* 青果隧道真探(2026-11-02 改):旧实现在这里短路返回 {@code passed/ok}「提取成功(青果隧道端点,
|
||||
* 连接鉴权在客户端)」,依据是「这对账密只存在用户本机、不上报服务端」——那句注释已经过期:
|
||||
* 青果只做账密档(AGENTS.md 铁律 25),用户填进来并存下来的提取链接里就带着 {@code key=} 与 {@code pwd=}。
|
||||
* 服务端既然拿得到凭据,就必须拿它经隧道打一次自家域名,否则「提取成功」永远冒充「代理可用」。
|
||||
* 现网 uid=1301(2026-11-02 13:08)是同一条路上另一半的坑:提取回 {@code NO_AVAILABLE_CHANNEL}
|
||||
* 被落成一坨 {@code provider_error},界面只剩「无法判定」——所以本方法连同
|
||||
* {@link #classifyProviderRejection} 一起改:探测要真打,失败要一档一码。
|
||||
*
|
||||
* <p>组出来的带鉴权地址只在内存里存在,日志与文案一律只出现 {@link SecretMasking#maskProxy} 之后的端点。</p>
|
||||
*/
|
||||
CheckOutcome probeQingguoTunnel(String extractLink, String endpointUrl, long startMillis) {
|
||||
String tunnelProxyUrl = qingguoTunnelProxyUrl(extractLink, endpointUrl);
|
||||
if (tunnelProxyUrl == null) {
|
||||
int latency = (int) (System.currentTimeMillis() - startMillis);
|
||||
log.warn("[user-secret][check] 青果隧道无法组链验连(链接未带可用 key/pwd)endpoint={}",
|
||||
SecretMasking.maskProxy(endpointUrl));
|
||||
return new CheckOutcome(STATUS_ERROR, CODE_TUNNEL_UNVERIFIED, TUNNEL_UNVERIFIED_MESSAGE, latency, false);
|
||||
}
|
||||
log.info("[user-secret][check] 青果隧道探测开始 endpoint={}(账密来自本条链接,不入日志)",
|
||||
SecretMasking.maskProxy(tunnelProxyUrl));
|
||||
return probeThroughProxy(tunnelProxyUrl, startMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给青果隧道端点补上链接里的 key/pwd:补不出安全可解析的地址就返回 null(宁可不验,也不猜一条
|
||||
* 会把 host 切歪的地址——{@code pwd} 里含 {@code @}/{@code :} 时 userinfo 会吞掉主机名)。
|
||||
*/
|
||||
static String qingguoTunnelProxyUrl(String extractLink, String endpointUrl) {
|
||||
if (endpointUrl == null || !looksLikeStaticProxy(endpointUrl)) {
|
||||
return null;
|
||||
}
|
||||
String key = ProxyLinkKey.fromUrl(extractLink);
|
||||
String pwd = ProxyLinkKey.passwordFromUrl(extractLink);
|
||||
if (!urlSafeCredential(key) || !urlSafeCredential(pwd)) {
|
||||
return null;
|
||||
}
|
||||
int schemeEnd = endpointUrl.indexOf("://");
|
||||
if (schemeEnd < 0) {
|
||||
return null;
|
||||
}
|
||||
return endpointUrl.substring(0, schemeEnd + 3) + key + ":" + pwd + "@" + endpointUrl.substring(schemeEnd + 3);
|
||||
}
|
||||
|
||||
/** 能原样嵌进 userinfo 的凭据判定:URL 分隔符与控制字符/空白一律不算(青果的 Authkey/Authpwd 本就不含它们)。 */
|
||||
private static boolean urlSafeCredential(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char c = value.charAt(i);
|
||||
if (c <= ' ' || c == ':' || c == '@' || c == '/' || c == '?' || c == '#' || c == '%' || c == '&') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 供应商拒付/鉴权失败的独立分类(两家错误码词表不同):
|
||||
* <ul>
|
||||
* <li>青果:code 为字符串。BALANCE_INSUFFICIENT=余额不足;INVALID_KEY/UNAVAILABLE_KEY/KEY_BLOCK/
|
||||
* ACCESS_DENY/API_AUTH_DENY=密钥不可用或未授权;REQUEST_LIMIT_EXCEEDED=限流;
|
||||
* INVALID_PARAMETER=参数错;其余业务失败按上游异常。</li>
|
||||
* <li>青果:code 为字符串,逐码分档,**一档一个 code**——
|
||||
* BALANCE_INSUFFICIENT=余额不足;INVALID_KEY/UNAVAILABLE_KEY/KEY_BLOCK=密钥本身不可用;
|
||||
* API_AUTH_DENY/ACCESS_DENY=这一条链接的鉴权没过(多数是 pwd 没填);
|
||||
* NO_AVAILABLE_CHANNEL=通道被自己的采集占着({@link #CODE_CHANNEL_BUSY},稍后再点);
|
||||
* REQUEST_LIMIT_EXCEEDED=限流;INVALID_PARAMETER=参数错;其余业务失败按上游异常。</li>
|
||||
* <li>极客:沿用历史口径(code=-1 或 message 含「余额不足」=余额不足)。</li>
|
||||
* </ul>
|
||||
* 返回 null 表示不是拒付/鉴权类失败,继续走 IP 解析。
|
||||
*
|
||||
* <p>供应商文案可能整条回显请求链接(含 key/pwd),出口前一律过 {@link #redactCredentials}。</p>
|
||||
*/
|
||||
CheckOutcome classifyProviderRejection(ProxyProvider provider, String body, int latencyMs) {
|
||||
return classifyProviderRejection(provider, body, latencyMs, null);
|
||||
}
|
||||
|
||||
/** {@code extractLink} 只用于抹掉供应商文案里回显的本条链接凭据,不参与判定。 */
|
||||
CheckOutcome classifyProviderRejection(ProxyProvider provider, String body, int latencyMs, String extractLink) {
|
||||
if (provider == ProxyProvider.QINGGUO) {
|
||||
JsonNode root = parseJson(body);
|
||||
if (root == null || !root.isObject()) {
|
||||
@@ -262,21 +352,25 @@ public class UserApiSecretCheckService {
|
||||
if (code.isEmpty() || "SUCCESS".equalsIgnoreCase(code) || "0".equals(code)) {
|
||||
return null;
|
||||
}
|
||||
String message = text(root.get("message"));
|
||||
String message = redactCredentials(text(root.get("message")), extractLink);
|
||||
String detail = message == null || message.isBlank() ? "" : ":" + abbreviate(message, 120);
|
||||
String upper = code.toUpperCase(java.util.Locale.ROOT);
|
||||
return switch (upper) {
|
||||
case "BALANCE_INSUFFICIENT" ->
|
||||
new CheckOutcome(STATUS_FAILED, CODE_INSUFFICIENT_BALANCE, INSUFFICIENT_BALANCE_MESSAGE, latencyMs, false);
|
||||
case "INVALID_KEY", "UNAVAILABLE_KEY", "KEY_BLOCK", "ACCESS_DENY", "API_AUTH_DENY" ->
|
||||
new CheckOutcome(STATUS_FAILED, CODE_FORBIDDEN,
|
||||
"青果密钥不可用或未授权(" + upper + ")" + detail, latencyMs, false);
|
||||
case "REQUEST_LIMIT_EXCEEDED" ->
|
||||
new CheckOutcome(STATUS_ERROR, CODE_RATE_LIMITED, "青果提取触发限流,本次无法判定" + detail, latencyMs, false);
|
||||
case "NO_AVAILABLE_CHANNEL" ->
|
||||
// 通道被占满(存活期内一个通道只给一个端点):不是密钥问题,稍后重试即可
|
||||
new CheckOutcome(STATUS_ERROR, CODE_PROVIDER_ERROR,
|
||||
"青果通道已全部占用,请等当前通道释放(约 3 分钟)后重试" + detail, latencyMs, false);
|
||||
// 通道被占满(存活期内一个通道只给一个端点):不是密钥问题,等释放后重点一次即可
|
||||
new CheckOutcome(STATUS_ERROR, CODE_CHANNEL_BUSY, CHANNEL_BUSY_MESSAGE, latencyMs, false);
|
||||
case "INVALID_KEY", "UNAVAILABLE_KEY", "KEY_BLOCK" ->
|
||||
new CheckOutcome(STATUS_FAILED, CODE_INVALID_KEY,
|
||||
"青果密钥不可用或已失效(" + upper + "),请核对提取链接里的 key" + detail, latencyMs, false);
|
||||
case "API_AUTH_DENY", "ACCESS_DENY" ->
|
||||
new CheckOutcome(STATUS_FAILED, CODE_FORBIDDEN,
|
||||
"青果拒绝了这条链接的鉴权(" + upper + "),请检查提取链接里的 pwd(青果只支持账密档,pwd 必填)"
|
||||
+ detail, latencyMs, false);
|
||||
case "REQUEST_LIMIT_EXCEEDED" ->
|
||||
new CheckOutcome(STATUS_ERROR, CODE_RATE_LIMITED,
|
||||
"青果提取接口请求过于频繁(限流),请等约 1 分钟后再点检测" + detail, latencyMs, false);
|
||||
case "INVALID_PARAMETER" ->
|
||||
new CheckOutcome(STATUS_FAILED, CODE_BAD_REQUEST, "青果提取参数不正确" + detail, latencyMs, false);
|
||||
default ->
|
||||
@@ -289,6 +383,24 @@ public class UserApiSecretCheckService {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 抹掉文案里回显的链接凭据:先按参数名把 {@code key=…}/{@code pwd=…} 的值盖掉,
|
||||
* 再按这把 key/pwd 的原文逐处替换(供应商可能只回显裸值、不带参数名)。
|
||||
* 返回值用于日志与界面,绝不带凭据原文。
|
||||
*/
|
||||
static String redactCredentials(String text, String extractLink) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
String redacted = CREDENTIAL_PARAM_PATTERN.matcher(text).replaceAll("$1=****");
|
||||
for (String secret : new String[]{ProxyLinkKey.fromUrl(extractLink), ProxyLinkKey.passwordFromUrl(extractLink)}) {
|
||||
if (secret != null && secret.length() >= MIN_REDACT_LENGTH && redacted.contains(secret)) {
|
||||
redacted = redacted.replace(secret, "****");
|
||||
}
|
||||
}
|
||||
return redacted;
|
||||
}
|
||||
|
||||
/** 经指定代理请求自家域名:任意 HTTP 响应即转发可用;407 为代理自身认证失败;网络异常不可达。 */
|
||||
private CheckOutcome probeThroughProxy(String proxyUrl, long startMillis) {
|
||||
try {
|
||||
|
||||
+22
-45
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.usersecret.service;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.text.SecretMasking;
|
||||
import com.nanri.aiimage.modules.admin.spi.UserSecretCleanupPort;
|
||||
import com.nanri.aiimage.common.security.ShopCredentialCryptoService;
|
||||
import com.nanri.aiimage.config.UserSecretProperties;
|
||||
@@ -78,7 +79,6 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
public static final String ROW_STATUS_UNKNOWN = "unknown";
|
||||
|
||||
private static final String STATUS_UNKNOWN = "unknown";
|
||||
private static final int MASK_MIN_LENGTH = 8;
|
||||
private static final int MESSAGE_MAX_LENGTH = 500;
|
||||
/** 「输入值检测」结果暂存键前缀:保存同一个值时沿用该结果,避免用户检完还要再检一次。 */
|
||||
private static final String VERIFIED_KEY_PREFIX = "aiimage:user-secret-verified";
|
||||
@@ -711,6 +711,14 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
probed++;
|
||||
sleepQuietly(200L);
|
||||
}
|
||||
if (UserApiSecretCheckService.CODE_CHANNEL_BUSY.equals(outcome.code())) {
|
||||
// 通道被占不是「密钥坏了」:这一轮问不出结论,就既不改状态也不推通知。
|
||||
// 青果通道数只有 1 条(2026-11-02 控制台核实),而巡检本身就要占一条——
|
||||
// 若把 channel_busy 落库,用户早上看到的「代理异常」其实是我们自己 04:30
|
||||
// 那一枪把他的通道占了(现网 uid=1301 的「无法判定」就是这个形状)。
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
applyCheckOutcome(row.getUserId(), module.get().key(), outcome);
|
||||
checked++;
|
||||
if (adminAudience != null) {
|
||||
@@ -1157,6 +1165,10 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
.last("limit 1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台侧的模块单元(列表行 + 代填回包):值一律脱敏,**不再下发代理明文**(见下方 2026-11-02 收口注释)。
|
||||
* 客户端侧另走 {@link #toItem}——那里锁定的代理要把完整地址发给配置的主人本人。
|
||||
*/
|
||||
private AdminUserSecretModuleVo toModuleVo(UserSecretModule module, UserApiSecretEntity row) {
|
||||
AdminUserSecretModuleVo vo = new AdminUserSecretModuleVo();
|
||||
vo.setModuleKey(module.key());
|
||||
@@ -1174,10 +1186,11 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
String plain = decryptQuietly(row.getSecretValue());
|
||||
vo.setChannel(module.channelOrDefault(row.getChannel(), plain));
|
||||
vo.setMasked(maskValue(module, plain));
|
||||
// 后台代理列要求完整可见(含账号密码),密钥列不下发明文
|
||||
if (module == UserSecretModule.PROXY) {
|
||||
vo.setFull(plain);
|
||||
}
|
||||
// 2026-11-02 收口:后台列表/代填回包一律**不再下发代理明文**(full 留空)。
|
||||
// 之前这一列把用户填的提取链接原文交给界面,而青果/极客的链接里就带着 key= 与 pwd=
|
||||
// ——等于把凭据摊在管理员屏幕、浏览器 DOM 与接口响应里(同页 LLM 列一直是脱敏的)。
|
||||
// 管理员要改代理只能重新粘贴完整链接(后管「改代理」的预填因此变空,属预期的行为收紧);
|
||||
// 客户端侧的锁定代理仍按 toItem 下发完整地址(那是用户自己的配置、发给本人)。
|
||||
vo.setExists(hasText(plain));
|
||||
vo.setLocked(isAdminLocked(row));
|
||||
vo.setCheckStatus(hasText(row.getCheckStatus()) ? row.getCheckStatus() : STATUS_UNKNOWN);
|
||||
@@ -1304,47 +1317,11 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
}
|
||||
}
|
||||
|
||||
/** 脱敏单出口:一律走 {@link SecretMasking},不在这里长第二份实现(代理列与日志必须同口径)。 */
|
||||
private String maskValue(UserSecretModule module, String plainValue) {
|
||||
if (module == UserSecretModule.PROXY) {
|
||||
return maskProxy(plainValue);
|
||||
}
|
||||
return mask(plainValue);
|
||||
}
|
||||
|
||||
private String mask(String value) {
|
||||
if (!hasText(value)) {
|
||||
return "";
|
||||
}
|
||||
String text = value.trim();
|
||||
if (text.length() <= MASK_MIN_LENGTH) {
|
||||
return "****";
|
||||
}
|
||||
return text.substring(0, 4) + "****" + text.substring(text.length() - 4);
|
||||
}
|
||||
|
||||
/** 代理掩码:隐去账号密码,保留 scheme://host:port 便于运维核对。 */
|
||||
private String maskProxy(String value) {
|
||||
if (!hasText(value)) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
URI uri = URI.create(value.trim());
|
||||
if (uri.getHost() == null || uri.getHost().isBlank()) {
|
||||
return mask(value);
|
||||
}
|
||||
StringBuilder masked = new StringBuilder();
|
||||
masked.append(uri.getScheme() == null ? "http" : uri.getScheme()).append("://");
|
||||
if (uri.getUserInfo() != null && !uri.getUserInfo().isBlank()) {
|
||||
masked.append("***@");
|
||||
}
|
||||
masked.append(uri.getHost());
|
||||
if (uri.getPort() > 0) {
|
||||
masked.append(':').append(uri.getPort());
|
||||
}
|
||||
return masked.toString();
|
||||
} catch (Exception ex) {
|
||||
return mask(value);
|
||||
}
|
||||
return module == UserSecretModule.PROXY
|
||||
? SecretMasking.maskProxy(plainValue)
|
||||
: SecretMasking.mask(plainValue);
|
||||
}
|
||||
|
||||
private String truncate(String value, int maxLength) {
|
||||
|
||||
+36
-1
@@ -7,6 +7,8 @@ import com.nanri.aiimage.modules.notification.service.NotificationService;
|
||||
import com.nanri.aiimage.modules.usersecret.client.JikipProxyClient;
|
||||
import com.nanri.aiimage.modules.usersecret.model.entity.UserProxyPlanEntity;
|
||||
import com.nanri.aiimage.modules.usersecret.model.vo.UserApiSecretBalanceVo;
|
||||
import com.nanri.aiimage.modules.usersecret.support.ProxyProvider;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -33,6 +35,7 @@ public class UserProxyBalanceService {
|
||||
private final Map<Long, CachedProxyBalance> cache = new ConcurrentHashMap<>();
|
||||
|
||||
private final UserProxyPlanService userProxyPlanService;
|
||||
private final UserApiSecretService userApiSecretService;
|
||||
private final JikipProxyClient jikipProxyClient;
|
||||
private final NotificationDispatchService notificationDispatchService;
|
||||
private final UserSecretProperties userSecretProperties;
|
||||
@@ -45,6 +48,27 @@ public class UserProxyBalanceService {
|
||||
if (cached != null && now - cached.cachedAt() < CACHE_MILLIS) {
|
||||
return cached.balance();
|
||||
}
|
||||
UserApiSecretBalanceVo vo = loadByProvider(userId);
|
||||
cache.put(userId, new CachedProxyBalance(vo, now));
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按供应商分档取余量:这套余量口径**只属于 jikip**(套餐 IP 余量+账户余额,走 find-balance)。
|
||||
*
|
||||
* <p>青果必须原样拒:它的余额接口是控制台档 {@code https://www.qg.net/api/merchant/getBalance},
|
||||
* 公共参数要 {@code domain}+{@code appid}+{@code time}+{@code sign=md5(appid+domain+time+appkey)},
|
||||
* 而用户在这里只填 Authkey/Authpwd(铁律 25 口径)——2026-11-02 实测提取域上的
|
||||
* {@code /balance} 对 key/pwd 直接 {@code API_AUTH_DENY},{@code /traffic}、{@code /package} 是
|
||||
* {@code NO_ROUTE}。此前青果账号走到这里会落进"否则查平台套餐"那一条,界面显示的是
|
||||
* **别人家的 jikip 余量**(串档),所以这一档宁可返回"查不了",也不许借用极客的数。</p>
|
||||
*/
|
||||
private UserApiSecretBalanceVo loadByProvider(Long userId) {
|
||||
ProxyProvider provider = ProxyProvider.detectFromUrl(
|
||||
userApiSecretService.findPlainValue(userId, UserSecretModule.PROXY.key()));
|
||||
if (provider == ProxyProvider.QINGGUO) {
|
||||
return qingguoUnsupported();
|
||||
}
|
||||
UserProxyPlanEntity plan = userProxyPlanService.getEntity(userId);
|
||||
UserApiSecretBalanceVo vo;
|
||||
if (plan != null) {
|
||||
@@ -59,7 +83,18 @@ public class UserProxyBalanceService {
|
||||
vo.setScope("platform");
|
||||
vo.setLow(false);
|
||||
}
|
||||
cache.put(userId, new CachedProxyBalance(vo, now));
|
||||
return vo;
|
||||
}
|
||||
|
||||
/** 青果档的余量答复:不打供应商、不冒充平台套餐,只把"去哪看"说清楚。 */
|
||||
private UserApiSecretBalanceVo qingguoUnsupported() {
|
||||
UserApiSecretBalanceVo vo = new UserApiSecretBalanceVo();
|
||||
vo.setAvailable(false);
|
||||
vo.setScope("none");
|
||||
vo.setLow(false);
|
||||
vo.setMessage("青果不提供余量/余额查询(它的余额接口要控制台 Appid 与 Appkey 签名,"
|
||||
+ "这里填的 Authkey/Authpwd 只能提取)。用量请看青果控制台,"
|
||||
+ "通道与密钥是否正常请点「代理设置」里的「检测」。");
|
||||
return vo;
|
||||
}
|
||||
|
||||
|
||||
+33
-5
@@ -4,8 +4,9 @@ import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 从代理提取链接里取供应商 Key(青果白名单 API 复用它,无需用户再填一次)。
|
||||
* 链接本身是凭据载体,调用方不得把返回值写进日志。
|
||||
* 从代理提取链接里取供应商凭据(Key 与密码)——这是全仓唯一读这对值的入口。
|
||||
* 青果白名单 API 复用 {@link #fromUrl(String)},隧道验连复用 {@link #passwordFromUrl(String)}。
|
||||
* 链接本身是凭据载体,调用方不得把返回值写进日志/界面文案。
|
||||
*/
|
||||
public final class ProxyLinkKey {
|
||||
|
||||
@@ -14,7 +15,25 @@ public final class ProxyLinkKey {
|
||||
|
||||
/** 取 {@code key} 查询参数(大小写不敏感,兼容 Key);没有返回 null。 */
|
||||
public static String fromUrl(String url) {
|
||||
if (url == null || url.isBlank()) {
|
||||
return param(url, "key");
|
||||
}
|
||||
|
||||
/**
|
||||
* 取账密档的密码参数:青果的提取链接用 {@code pwd},极客旧链接用 {@code password}。
|
||||
* 返回值已 URL 解码,调用方不得写进日志。
|
||||
*/
|
||||
public static String passwordFromUrl(String url) {
|
||||
return param(url, "pwd", "password");
|
||||
}
|
||||
|
||||
/**
|
||||
* 按参数名(大小写不敏感)取链接查询串里的值,取第一个命中的非空项;没有返回 null。
|
||||
*
|
||||
* <p>查询串里存的是 URL 编码后的值,这里按 URL 组件解码返回;需要重新拼进 URL 的调用方
|
||||
* 要自己按组件编码(AGENTS.md 铁律 25:{@code pwd} 里的 {@code @}/{@code :} 会把 host 切歪)。</p>
|
||||
*/
|
||||
public static String param(String url, String... names) {
|
||||
if (url == null || url.isBlank() || names == null || names.length == 0) {
|
||||
return null;
|
||||
}
|
||||
int queryStart = url.indexOf('?');
|
||||
@@ -32,12 +51,12 @@ public final class ProxyLinkKey {
|
||||
continue;
|
||||
}
|
||||
String name = pair.substring(0, separator);
|
||||
if (!"key".equalsIgnoreCase(name)) {
|
||||
if (!matchesAny(name, names)) {
|
||||
continue;
|
||||
}
|
||||
String value = pair.substring(separator + 1);
|
||||
if (value.isEmpty()) {
|
||||
return null;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
return URLDecoder.decode(value, StandardCharsets.UTF_8);
|
||||
@@ -47,4 +66,13 @@ public final class ProxyLinkKey {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean matchesAny(String name, String[] names) {
|
||||
for (String candidate : names) {
|
||||
if (candidate != null && candidate.equalsIgnoreCase(name.trim())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user