|
| 1 | +import { $app, Console, Lodash as _, Storage } from "@nsnanocat/util"; |
| 2 | +import database from "./function/database.mjs"; |
| 3 | +import setENV from "./function/setENV.mjs"; |
| 4 | +/***************** Processing *****************/ |
| 5 | +export async function Request($request) { |
| 6 | + // 构造回复数据 |
| 7 | + let $response = undefined; |
| 8 | + // 解构URL |
| 9 | + const url = new URL($request.url); |
| 10 | + Console.info(`url: ${url.toJSON()}`); |
| 11 | + // 解析格式 |
| 12 | + const FORMAT = ($request.headers?.["Content-Type"] ?? $request.headers?.["content-type"])?.split(";")?.[0]; |
| 13 | + Console.info(`FORMAT: ${FORMAT}`); |
| 14 | + /** |
| 15 | + * 设置 |
| 16 | + * @type {{Settings: import('./types').Settings}} |
| 17 | + */ |
| 18 | + const { Settings, Caches, Configs } = setENV("iRingo", "WeatherKit", database); |
| 19 | + // 创建空数据 |
| 20 | + let body = {}; |
| 21 | + // 方法判断 |
| 22 | + switch ($request.method) { |
| 23 | + case "POST": |
| 24 | + case "PUT": |
| 25 | + case "PATCH": |
| 26 | + // biome-ignore lint/suspicious/noFallthroughSwitchClause: <explanation> |
| 27 | + case "DELETE": |
| 28 | + // 格式判断 |
| 29 | + switch (FORMAT) { |
| 30 | + case undefined: // 视为无body |
| 31 | + break; |
| 32 | + case "application/x-www-form-urlencoded": |
| 33 | + case "text/plain": |
| 34 | + default: |
| 35 | + break; |
| 36 | + case "application/x-mpegURL": |
| 37 | + case "application/x-mpegurl": |
| 38 | + case "application/vnd.apple.mpegurl": |
| 39 | + case "audio/mpegurl": |
| 40 | + //body = M3U8.parse($request.body); |
| 41 | + //Console.debug(`body: ${JSON.stringify(body)}`); |
| 42 | + //$request.body = M3U8.stringify(body); |
| 43 | + break; |
| 44 | + case "text/xml": |
| 45 | + case "text/html": |
| 46 | + case "text/plist": |
| 47 | + case "application/xml": |
| 48 | + case "application/plist": |
| 49 | + case "application/x-plist": |
| 50 | + //body = XML.parse($request.body); |
| 51 | + //Console.debug(`body: ${JSON.stringify(body)}`); |
| 52 | + //$request.body = XML.stringify(body); |
| 53 | + break; |
| 54 | + case "text/vtt": |
| 55 | + case "application/vtt": |
| 56 | + //body = VTT.parse($request.body); |
| 57 | + //Console.debug(`body: ${JSON.stringify(body)}`); |
| 58 | + //$request.body = VTT.stringify(body); |
| 59 | + break; |
| 60 | + case "text/json": |
| 61 | + case "application/json": |
| 62 | + //body = JSON.parse($request.body ?? "{}"); |
| 63 | + //Console.debug(`body: ${JSON.stringify(body)}`); |
| 64 | + //$request.body = JSON.stringify(body); |
| 65 | + break; |
| 66 | + case "application/protobuf": |
| 67 | + case "application/x-protobuf": |
| 68 | + case "application/vnd.google.protobuf": |
| 69 | + case "application/grpc": |
| 70 | + case "application/grpc+proto": |
| 71 | + case "application/octet-stream": { |
| 72 | + //Console.debug(`$request: ${JSON.stringify($request, null, 2)}`); |
| 73 | + let rawBody = $app === "Quantumult X" ? new Uint8Array($request.bodyBytes ?? []) : ($request.body ?? new Uint8Array()); |
| 74 | + //Console.debug(`isBuffer? ${ArrayBuffer.isView(rawBody)}: ${JSON.stringify(rawBody, null, 2)}`); |
| 75 | + // 写入二进制数据 |
| 76 | + $request.body = rawBody; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + //break; // 不中断,继续处理URL |
| 81 | + case "GET": |
| 82 | + case "HEAD": |
| 83 | + case "OPTIONS": |
| 84 | + default: |
| 85 | + delete $request?.headers?.["If-None-Match"]; |
| 86 | + delete $request?.headers?.["if-none-match"]; |
| 87 | + // 主机判断 |
| 88 | + switch (url.hostname) { |
| 89 | + case "weatherkit.apple.com": |
| 90 | + // 路径判断 |
| 91 | + switch (true) { |
| 92 | + case url.pathname.startsWith("/api/v2/weather/"): { |
| 93 | + // 解决 macOS 天气 app 如果使用国际版 Maps 时,country 丢失不显示未来一小时降水的问题 |
| 94 | + switch (true) { |
| 95 | + case $request.headers["User-Agent"]?.startsWith("WeatherKit_Weather_macOS_Version"): |
| 96 | + case $request.headers["user-agent"]?.startsWith("WeatherKit_Weather_macOS_Version"): |
| 97 | + if (url.searchParams.has("country")) { |
| 98 | + //if (url.searchParams.get("country") === "CN") url.searchParams.set("country", "TW"); |
| 99 | + } else { |
| 100 | + const gcc = Storage.getItem("@iRingo.Location.Caches")?.pep?.gcc; |
| 101 | + if (gcc) url.searchParams.set("country", gcc); |
| 102 | + } |
| 103 | + break; |
| 104 | + } |
| 105 | + let dataSets = url.searchParams.get("dataSets")?.split(","); |
| 106 | + if (dataSets) { |
| 107 | + dataSets = dataSets?.filter(dataSet => Settings.DataSets?.includes(dataSet)); |
| 108 | + url.searchParams.set("dataSets", dataSets?.join(",")); |
| 109 | + } |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + break; |
| 114 | + case "weather-map2.apple.com": { |
| 115 | + // 路径判断 |
| 116 | + switch (url.pathname) { |
| 117 | + case "/v1/mapOverlay/precipitationRadarMap": |
| 118 | + case "/v1/mapOverlay/precipitationForecastByFrameTime": { |
| 119 | + /* |
| 120 | + switch (true) { |
| 121 | + case $request.headers["User-Agent"]?.startsWith("Weather_macOS_Version"): |
| 122 | + case $request.headers["user-agent"]?.startsWith("Weather_macOS_Version"): |
| 123 | + switch (true) { |
| 124 | + case $request?.headers?.geocountrycode === "CN": |
| 125 | + $request.headers.geocountrycode = "US"; |
| 126 | + break; |
| 127 | + case $request?.headers?.GeoCountryCode === "CN": |
| 128 | + $request.headers.GeoCountryCode = "US"; |
| 129 | + break; |
| 130 | + } |
| 131 | + break; |
| 132 | + } |
| 133 | + */ |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + break; |
| 141 | + case "CONNECT": |
| 142 | + case "TRACE": |
| 143 | + break; |
| 144 | + } |
| 145 | + $request.url = url.toString(); |
| 146 | + Console.debug(`$request.url: ${$request.url}`); |
| 147 | + return { $request, $response }; |
| 148 | +} |
0 commit comments