File size: 12,328 Bytes
ea775bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | import { useQuery } from "@tanstack/react-query";
import { Check, Copy, Info } from "lucide-react";
import { useMemo, useState } from "react";
import { Button } from "../../components/ui/Button";
import { Input } from "../../components/ui/Input";
import { useI18n } from "../../i18n";
import { getEnvConfig } from "../systemConfig/api";
const PROXY_TOKEN_STORAGE_KEY = "resin_proxy_token";
const TOKEN_PLACEHOLDER = "<token>";
type ProxyEndpoint = { scheme: string; host: string };
function loadStoredProxyToken(): string {
if (typeof window === "undefined") {
return "";
}
return window.localStorage.getItem(PROXY_TOKEN_STORAGE_KEY) ?? "";
}
function persistProxyToken(value: string): void {
if (typeof window === "undefined") {
return;
}
if (value) {
window.localStorage.setItem(PROXY_TOKEN_STORAGE_KEY, value);
} else {
window.localStorage.removeItem(PROXY_TOKEN_STORAGE_KEY);
}
}
function formatHostWithPort(hostname: string, port: number): string {
const host = hostname.includes(":") && !hostname.startsWith("[") ? `[${hostname}]` : hostname;
return port ? `${host}:${port}` : host;
}
function configuredApiEndpoint(): ProxyEndpoint | null {
const apiBase = import.meta.env.VITE_API_BASE_URL?.trim();
if (!apiBase || !/^https?:\/\//i.test(apiBase)) {
return null;
}
try {
const url = new URL(apiBase);
return { scheme: url.protocol.replace(/:$/, ""), host: url.host };
} catch {
return null;
}
}
function currentProxyEndpoint(fallbackPort: number): ProxyEndpoint {
const configured = configuredApiEndpoint();
if (configured) {
return configured;
}
if (typeof window === "undefined") {
return { scheme: "http", host: fallbackPort ? `127.0.0.1:${fallbackPort}` : "127.0.0.1:2260" };
}
const scheme = currentScheme();
const expectedPort = fallbackPort || 2260;
const currentPort = Number(window.location.port);
if (import.meta.env.DEV && window.location.hostname && currentPort && currentPort !== expectedPort) {
return { scheme, host: formatHostWithPort(window.location.hostname, expectedPort) };
}
if (window.location.host) {
return { scheme, host: window.location.host };
}
return {
scheme,
host: window.location.hostname
? formatHostWithPort(window.location.hostname, expectedPort)
: `127.0.0.1:${expectedPort}`,
};
}
function currentScheme(): string {
if (typeof window === "undefined" || !window.location.protocol) {
return "http";
}
return window.location.protocol.replace(/:$/, "");
}
// Encode a URL segment/userinfo component, but keep the literal <token>
// placeholder readable so users can see where to paste the real token.
function encodeSegment(value: string): string {
return value === TOKEN_PLACEHOLDER ? value : encodeURIComponent(value);
}
function shellQuote(value: string): string {
return `'${value.replace(/'/g, `'\\''`)}'`;
}
type ParsedTarget = { protocol: string; rest: string };
function parseTarget(raw: string): ParsedTarget | null {
const trimmed = raw.trim();
if (!trimmed) {
return null;
}
const withScheme = /^[a-zA-Z][\w+.-]*:\/\//.test(trimmed) ? trimmed : `https://${trimmed}`;
let url: URL;
try {
url = new URL(withScheme);
} catch {
return null;
}
const protocol = url.protocol.replace(/:$/, "").toLowerCase();
if (protocol !== "http" && protocol !== "https") {
return null;
}
const path = url.pathname === "/" ? "" : url.pathname;
return { protocol, rest: `${url.host}${path}${url.search}` };
}
type CopyFieldProps = {
label: string;
value: string;
hint?: string;
copyLabel: string;
copiedLabel: string;
};
function CopyField({ label, value, hint, copyLabel, copiedLabel }: CopyFieldProps) {
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(value);
} else {
const area = document.createElement("textarea");
area.value = value;
area.style.position = "fixed";
area.style.opacity = "0";
document.body.appendChild(area);
area.select();
document.execCommand("copy");
document.body.removeChild(area);
}
setCopied(true);
window.setTimeout(() => setCopied(false), 1500);
} catch {
setCopied(false);
}
};
return (
<div className="platform-access-field">
<div className="platform-access-field-head">
<span className="platform-access-field-label">{label}</span>
{hint ? <span className="platform-access-field-hint">{hint}</span> : null}
</div>
<div className="platform-access-field-body">
<code className="platform-access-value" title={value}>
{value}
</code>
<Button
variant="secondary"
size="sm"
onClick={() => void handleCopy()}
className="platform-access-copy-btn"
>
{copied ? <Check size={14} /> : <Copy size={14} />}
{copied ? copiedLabel : copyLabel}
</Button>
</div>
</div>
);
}
type PlatformAccessPanelProps = {
platformName: string;
};
export function PlatformAccessPanel({ platformName }: PlatformAccessPanelProps) {
const { t } = useI18n();
const [account, setAccount] = useState("");
const [token, setToken] = useState(loadStoredProxyToken);
const [target, setTarget] = useState("https://api.ipify.org");
const envQuery = useQuery({
queryKey: ["system-env-config"],
queryFn: getEnvConfig,
staleTime: 60_000,
});
const env = envQuery.data;
const proxyTokenSet = env?.proxy_token_set ?? true;
const isLegacy = env?.auth_version === "LEGACY_V0";
const endpoint = currentProxyEndpoint(env?.resin_port ?? 2260);
const host = endpoint.host;
const scheme = endpoint.scheme;
const handleTokenChange = (value: string) => {
setToken(value);
persistProxyToken(value.trim());
};
const urls = useMemo(() => {
const platform = platformName.trim() || "Default";
const acc = account.trim();
const tokenRaw = proxyTokenSet ? token.trim() : "";
const sep = isLegacy ? ":" : ".";
// Raw identity/credential are used verbatim for the curl -U value.
const identityRaw = acc ? `${platform}${sep}${acc}` : platform;
// Encoded identity is reused for URL userinfo and reverse path segment.
const identityEnc = acc
? `${encodeSegment(platform)}${sep}${encodeSegment(acc)}`
: encodeSegment(platform);
const reverseIdentityEnc = isLegacy
? `${encodeSegment(platform)}:${acc ? encodeSegment(acc) : ""}`
: identityEnc;
// forward token: literal placeholder only when auth is enabled but unset.
const forwardToken = tokenRaw || (proxyTokenSet ? TOKEN_PLACEHOLDER : "");
let forwardCredential: string;
let userInfo: string;
if (isLegacy) {
forwardCredential = forwardToken ? `${forwardToken}:${identityRaw}` : identityRaw;
userInfo = forwardToken ? `${encodeSegment(forwardToken)}:${identityEnc}` : identityEnc;
} else {
forwardCredential = forwardToken ? `${identityRaw}:${forwardToken}` : identityRaw;
userInfo = forwardToken ? `${identityEnc}:${encodeSegment(forwardToken)}` : identityEnc;
}
const httpForward = `http://${userInfo}@${host}`;
const socksForward = `socks5h://${userInfo}@${host}`;
const reverseTokenSeg = proxyTokenSet ? encodeSegment(tokenRaw || TOKEN_PLACEHOLDER) : "";
const parsed = parseTarget(target);
const reverseUrl = parsed
? `${scheme}://${host}/${reverseTokenSeg}/${reverseIdentityEnc}/${parsed.protocol}/${parsed.rest}`
: "";
const curlForward = [
"curl",
"-x",
shellQuote(`http://${host}`),
"-U",
shellQuote(forwardCredential),
shellQuote("https://api.ipify.org"),
].join(" ");
const curlReverse = reverseUrl ? `curl ${shellQuote(reverseUrl)}` : "";
return { httpForward, socksForward, reverseUrl, curlForward, curlReverse };
}, [platformName, account, token, isLegacy, proxyTokenSet, host, scheme, target]);
const copyLabel = t("复制");
const copiedLabel = t("已复制");
const tokenMissing = proxyTokenSet && !token.trim();
const tokenInputValue = proxyTokenSet ? token : "";
return (
<section className="platform-detail-tabpanel platform-access-section">
<div className="platform-drawer-section-head">
<h4>{t("接入方式")}</h4>
<p>{t("填写账号与代理 token,一键复制正向/反向代理地址。")}</p>
</div>
<div className="platform-access-inputs">
<div className="field-group">
<label className="field-label" htmlFor="access-account">
{t("业务账号(可选)")}
</label>
<Input
id="access-account"
placeholder={t("例如 user_tom,留空则只按平台路由")}
value={account}
onChange={(event) => setAccount(event.target.value)}
/>
</div>
<div className="field-group">
<label className="field-label field-label-with-info" htmlFor="access-token">
<span>{t("代理 token")}</span>
<span
className="subscription-info-icon"
title={t("即后端 RESIN_PROXY_TOKEN。仅保存在浏览器本地,不会上传服务器。")}
aria-label={t("即后端 RESIN_PROXY_TOKEN。仅保存在浏览器本地,不会上传服务器。")}
tabIndex={0}
>
<Info size={13} />
</span>
</label>
<Input
id="access-token"
type="password"
placeholder={proxyTokenSet ? t("填写 RESIN_PROXY_TOKEN") : t("当前代理免认证,无需填写")}
value={tokenInputValue}
onChange={(event) => {
if (proxyTokenSet) {
handleTokenChange(event.target.value);
}
}}
disabled={!proxyTokenSet}
autoComplete="off"
/>
{tokenMissing ? (
<p className="muted" style={{ marginTop: 4, fontSize: 12 }}>
{t("尚未填写 token,地址中将以 <token> 占位,请替换为实际值。")}
</p>
) : null}
</div>
</div>
<div className="platform-access-group">
<h5>{t("正向代理")}</h5>
<CopyField
label={t("HTTP 正向代理")}
value={urls.httpForward}
copyLabel={copyLabel}
copiedLabel={copiedLabel}
/>
{isLegacy ? (
<p className="muted" style={{ fontSize: 12 }}>
{t("当前为 LEGACY_V0 鉴权,SOCKS5 正向代理未启用。")}
</p>
) : (
<CopyField
label={t("SOCKS5 正向代理")}
value={urls.socksForward}
copyLabel={copyLabel}
copiedLabel={copiedLabel}
/>
)}
<CopyField
label={t("curl 示例")}
value={urls.curlForward}
copyLabel={copyLabel}
copiedLabel={copiedLabel}
/>
</div>
<div className="platform-access-group">
<h5>{t("反向代理")}</h5>
<div className="field-group">
<label className="field-label" htmlFor="access-target">
{t("目标网址")}
</label>
<Input
id="access-target"
placeholder={t("例如 https://api.ipify.org")}
value={target}
onChange={(event) => setTarget(event.target.value)}
/>
</div>
{urls.reverseUrl ? (
<>
<CopyField
label={t("反向代理地址")}
value={urls.reverseUrl}
copyLabel={copyLabel}
copiedLabel={copiedLabel}
/>
<CopyField
label={t("curl 示例")}
value={urls.curlReverse}
copyLabel={copyLabel}
copiedLabel={copiedLabel}
/>
</>
) : (
<p className="muted" style={{ fontSize: 12 }}>
{t("请输入合法的 http/https 目标网址以生成反向代理地址。")}
</p>
)}
</div>
</section>
);
}
|