|
1867 | 1867 | if (!user) user = at; |
1868 | 1868 |
|
1869 | 1869 | const url = new URL(request.url); |
1870 | | - |
1871 | | - if (url.protocol === 'https:') { |
1872 | | - const httpUrl = 'http://' + url.host + url.pathname + url.search; |
1873 | | - return Response.redirect(httpUrl, 302); |
1874 | | - } |
1875 | | - |
1876 | 1870 | // 优先检查Cookie中的语言设置 |
1877 | 1871 | const cookieHeader = request.headers.get('Cookie') || ''; |
1878 | 1872 | let langFromCookie = null; |
|
4023 | 4017 | }); |
4024 | 4018 | } |
4025 | 4019 |
|
| 4020 | + function ipToHex(ip) { |
| 4021 | + const parts = ip.split('.'); |
| 4022 | + if (parts.length !== 4) return null; |
| 4023 | + let hex = ''; |
| 4024 | + for (let i = 0; i < 4; i++) { |
| 4025 | + const num = parseInt(parts[i]); |
| 4026 | + if (isNaN(num) || num < 0 || num > 255) return null; |
| 4027 | + hex += num.toString(16).padStart(2, '0'); |
| 4028 | + } |
| 4029 | + return hex; |
| 4030 | + } |
| 4031 | + |
4026 | 4032 | const coloMap = { |
4027 | 4033 | 'SJC': '圣何塞', 'LAX': '洛杉矶', 'SEA': '西雅图', 'SFO': '旧金山', 'DFW': '达拉斯', |
4028 | 4034 | 'ORD': '芝加哥', 'IAD': '华盛顿', 'ATL': '亚特兰大', 'MIA': '迈阿密', 'DEN': '丹佛', |
|
4098 | 4104 | } |
4099 | 4105 |
|
4100 | 4106 | const cleanHost = host.replace(/^\\[|\\]$/g, ''); |
4101 | | - const isIPv6 = cleanHost.includes(':'); |
4102 | | - const hostPart = isIPv6 ? ('[' + cleanHost + ']') : cleanHost; |
4103 | | - testUrl = 'http://' + hostPart + ':80/cdn-cgi/trace'; |
| 4107 | + const hexIP = ipToHex(cleanHost); |
| 4108 | + const testDomain = hexIP ? (hexIP + '.nip.lfree.org') : (cleanHost + '.nip.lfree.org'); |
| 4109 | + testUrl = 'https://' + testDomain + ':' + port + '/'; |
4104 | 4110 |
|
4105 | | - console.log('[LatencyTest] Testing:', testUrl, 'Original:', host + ':' + port); |
| 4111 | + console.log('[LatencyTest] Testing:', testUrl, 'Original:', host + ':' + port, 'HexIP:', hexIP); |
4106 | 4112 |
|
4107 | 4113 | const firstStart = Date.now(); |
4108 | 4114 | const response1 = await fetch(testUrl, { |
|
4118 | 4124 | try { |
4119 | 4125 | const text = await response1.text(); |
4120 | 4126 | console.log('[LatencyTest] Response body:', text.substring(0, 200)); |
4121 | | - const coloMatch = text.match(/colo=([A-Z]+)/); |
4122 | | - if (coloMatch) { |
4123 | | - colo = coloMatch[1]; |
| 4127 | + const data = JSON.parse(text); |
| 4128 | + if (data.colo) { |
| 4129 | + colo = data.colo; |
4124 | 4130 | } |
4125 | 4131 | } catch (e) { |
4126 | 4132 | console.log('[LatencyTest] Parse error:', e.message); |
|
0 commit comments