| 1 | import { InlineSelect } from "./inline-select"; |
| 2 | import React from "react"; |
| 3 | import { Globe2, LoaderCircle } from "lucide-react"; |
| 4 | import { canonicalExitPolicy } from "./safety-state.mjs"; |
| 5 | |
| 6 | const countries = |
| 7 | "US GB CA DE FR NL SE FI IE CH AU NZ JP SG KR HK MO TW IN BR".split(" "); |
| 8 | const names = new Intl.DisplayNames(["zh-CN"], { type: "region" }); |
| 9 | |
| 10 | export function ExitPolicyFields({ |
| 11 | value, |
| 12 | onChange, |
| 13 | observation, |
| 14 | onProbe, |
| 15 | busy, |
| 16 | probing, |
| 17 | }) { |
| 18 | const policy = { ...canonicalExitPolicy(), ...value }; |
| 19 | const update = (key, next) => onChange({ ...policy, [key]: next }); |
| 20 | const bind = () => |
| 21 | onChange( |
| 22 | policy.mode === "static" |
| 23 | ? { ...policy, ip: observation.ip } |
| 24 | : { |
| 25 | ...policy, |
| 26 | country_code: observation.country_code || "", |
| 27 | ...(policy.scope === "city" || policy.scope === "region" |
| 28 | ? { |
| 29 | region: observation.region || "", |
| 30 | ...(policy.scope === "city" ? { city: observation.city || "" } : {}), |
| 31 | } |
| 32 | : {}), |
| 33 | }, |
| 34 | ); |
| 35 | return ( |
| 36 | <> |
| 37 | <div className="section-title"> |
| 38 | <h3>IP 变化规则</h3> |
| 39 | <p>中国大陆出口禁止放行;静态 IP 和不限地区也必须确认出口国家。</p> |
| 40 | </div> |
| 41 | <label className="toggle-row"> |
| 42 | <span> |
| 43 | <strong>静态 IP</strong> |
| 44 | <small>开启后,检测到其他 IPv4 地址会暂停外网。</small> |
| 45 | </span> |
| 46 | <input |
| 47 | type="checkbox" |
| 48 | role="switch" |
| 49 | checked={policy.mode === "static"} |
| 50 | onChange={(e) => |
| 51 | update("mode", e.target.checked ? "static" : "dynamic") |
| 52 | } |
| 53 | /> |
| 54 | </label> |
| 55 | {policy.mode === "static" ? ( |
| 56 | <label> |
| 57 | 绑定的 IPv4 地址 |
| 58 | <input |
| 59 | value={policy.ip} |
| 60 | placeholder="首次保存时绑定检测到的地址" |
| 61 | spellCheck="false" |
| 62 | onChange={(e) => update("ip", e.target.value)} |
| 63 | /> |
| 64 | </label> |
| 65 | ) : ( |
| 66 | <> |
| 67 | <label> |
| 68 | 动态 IP 的允许范围 |
| 69 | <InlineSelect |
| 70 | aria-label="动态 IP 的允许范围" |
| 71 | value={policy.scope} |
| 72 | onValueChange={(next) => update("scope", next)} |
| 73 | > |
| 74 | <option value="city">同一城市</option> |
| 75 | <option value="region">同一州/省</option> |
| 76 | <option value="country">同一国家/地区</option> |
| 77 | <option value="any">不限地区(中国大陆除外)</option> |
| 78 | </InlineSelect> |
| 79 | </label> |
| 80 | {policy.scope !== "any" && ( |
| 81 | <> |
| 82 | <label> |
| 83 | 允许的国家/地区代码 |
| 84 | <InlineSelect editable |
| 85 | aria-label="允许的国家/地区代码" |
| 86 | value={policy.country_code} |
| 87 | onValueChange={(next) => |
| 88 | update("country_code", next.toUpperCase()) |
| 89 | } |
| 90 | placeholder="例如 US;留空则在首次保存时绑定" |
| 91 | maxLength={2} |
| 92 | spellCheck="false" |
| 93 | > |
| 94 | {countries.map((code) => ( |
| 95 | <option key={code} value={code}> |
| 96 | {names.of(code)} |
| 97 | </option> |
| 98 | ))} |
| 99 | </InlineSelect> |
| 100 | </label> |
| 101 | {(policy.scope === "city" || policy.scope === "region") && ( |
| 102 | <div className="field-row"> |
| 103 | <label> |
| 104 | 所属州/省 |
| 105 | <input |
| 106 | value={policy.region} |
| 107 | onChange={(e) => update("region", e.target.value)} |
| 108 | placeholder="例如 California" |
| 109 | /> |
| 110 | </label> |
| 111 | {policy.scope === "city" && ( |
| 112 | <label> |
| 113 | 允许的城市 |
| 114 | <input |
| 115 | value={policy.city} |
| 116 | onChange={(e) => update("city", e.target.value)} |
| 117 | placeholder="例如 Los Angeles" |
| 118 | /> |
| 119 | </label> |
| 120 | )} |
| 121 | </div> |
| 122 | )} |
| 123 | <p className="field-help"> |
| 124 | {policy.scope === "city" |
| 125 | ? "城市同时匹配所属国家和州/省。名称以定位结果为准,建议使用检测结果填写。" |
| 126 | : policy.scope === "region" |
| 127 | ? "州/省同时匹配所属国家,包括日本的都道府县。名称以检测结果为准。" |
| 128 | : "建议先检测出口,再使用检测到的国家/地区填写。"} |
| 129 | </p> |
| 130 | </> |
| 131 | )} |
| 132 | </> |
| 133 | )} |
| 134 | <div className="probe-line"> |
| 135 | <button disabled={busy} onClick={onProbe}> |
| 136 | {probing ? ( |
| 137 | <LoaderCircle size={15} className="spin" /> |
| 138 | ) : ( |
| 139 | <Globe2 size={15} /> |
| 140 | )} |
| 141 | 检测当前出口 |
| 142 | </button> |
| 143 | <button |
| 144 | disabled={ |
| 145 | busy || |
| 146 | !observation || |
| 147 | (policy.mode === "dynamic" && policy.scope === "any") |
| 148 | } |
| 149 | onClick={bind} |
| 150 | > |
| 151 | 用检测结果填写 |
| 152 | </button> |
| 153 | </div> |
| 154 | {observation && ( |
| 155 | <div className="exit-observation"> |
| 156 | <strong> |
| 157 | {observation.country} · {observation.region} · {observation.city} |
| 158 | </strong> |
| 159 | <code>{observation.ip}</code> |
| 160 | <small> |
| 161 | {observation.country_code} ·{" "} |
| 162 | {new Date(observation.checked_at).toLocaleTimeString("zh-CN")} |
| 163 | </small> |
| 164 | </div> |
| 165 | )} |
| 166 | <p className="footnote"> |
| 167 | 已有绑定会保留,重检和重启不会自动更换基准。命中大陆网段、定位为中国大陆、无法确认出口地区或超出保存范围时暂停外网,离线终端和文件仍可使用。 |
| 168 | </p> |
| 169 | <p className="field-help"> |
| 170 | 范围依据 IP 数据库的近似定位。后台约每 30 |
| 171 | 秒采样,不能代替代理自身的分流约束。 |
| 172 | </p> |
| 173 | </> |
| 174 | ); |
| 175 | } |