{"id":556,"date":"2025-07-31T15:53:59","date_gmt":"2025-07-31T07:53:59","guid":{"rendered":"https:\/\/blog.lcsoul.cn\/?p=556"},"modified":"2025-07-31T15:53:59","modified_gmt":"2025-07-31T07:53:59","slug":"%e4%bd%bf%e7%94%a8edgeone%e7%9a%84%e8%be%b9%e7%bc%98%e5%87%bd%e6%95%b0%e8%8e%b7%e5%8f%96%e7%94%a8%e6%88%b7ip","status":"publish","type":"post","link":"https:\/\/blog.lcsoul.top\/index.php\/2025\/07\/31\/%e4%bd%bf%e7%94%a8edgeone%e7%9a%84%e8%be%b9%e7%bc%98%e5%87%bd%e6%95%b0%e8%8e%b7%e5%8f%96%e7%94%a8%e6%88%b7ip\/","title":{"rendered":"\u4f7f\u7528EdgeOne\u7684\u8fb9\u7f18\u51fd\u6570\u83b7\u53d6\u7528\u6237IP"},"content":{"rendered":"<p>\u539f\u672c\u662f\u60f3\u7528EdgeOne Pages\u5b9e\u73b0 \u50cfCloudFlare Pages\u90a3\u6837\u90e8\u7f72\u4e00\u4e2a<\/p>\n<p>\u540e\u6765\u53d1\u73b0EdgeOne\u7684\u8fb9\u7f18\u51fd\u6570\u53ef\u4ee5\u641e\u5b9a \u7136\u540e\u5c31\u6298\u817e\u4e86\u4e00\u4e0b<\/p>\n<p>\u9996\u5148\u6dfb\u52a0\u57df\u540d<\/p>\n<p>\u8fdb\u5165edgeone\u63a7\u5236\u53f0 \u9009\u62e9\u5df2\u7ed1\u5b9a\u57df\u540d<\/p>\n<p>\u70b9\u51fb\u6dfb\u52a0\u57df\u540d \u8f93\u5165\u4f60\u9700\u8981\u8bbe\u7f6e\u7684\u4e8c\u7ea7\u57df\u540d \u56de\u6e90\u914d\u7f6e\u968f\u4fbf\u586b \u6a21\u677f\u9009\u62e9 \u4e0d\u4f7f\u7528\u6a21\u677f<\/p>\n<p>\u6dfb\u52a0\u5b8c\u6210\u540e\u7b49\u5f85\u90e8\u7f72\u5b8c\u6210 HTTPS\u914d\u7f6e\u9009\u62e9\u514d\u8d39\u8bc1\u4e66<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728\u7b49\u5f85\u90e8\u7f72\u671f\u95f4 \u5de6\u4fa7\u83dc\u5355\u6ed1\u52a8\u5230\u5e95\u90e8 \u8fb9\u7f18\u51fd\u6570 \u9009\u62e9\u51fd\u6570\u7ba1\u7406<\/p>\n<p>\u65b0\u5efa\u51fd\u6570 \u9009\u7b2c\u4e00\u4e2a \u521b\u5efaHello World \u6a21\u677f\u5373\u53ef \u70b9\u51fb\u4e0b\u4e00\u6b65<\/p>\n<p>\u8f93\u5165\u51fd\u6570\u540d\u79f0\u00a0 \u00a0\u4e0b\u9762\u51fd\u6570\u4ee3\u7801\u533a\u57df \u7c98\u8d34\u51fd\u6570\u4ee3\u7801 \u66ff\u6362\u539f\u6709\u7684<\/p>\n<pre class=\"corepress-code-pre\"><code>addEventListener('fetch', event =&gt; {\n  event.respondWith(handleRequest(event.request));\n});\n\nfunction handleRequest(request) {\n  const url = new URL(request.url);\n  const path = url.pathname;\n  \/\/ \u901a\u8fc7 request.eo.clientIp \u83b7\u53d6\u5ba2\u6237\u7aef IP\n  const ip = request.eo.clientIp || '';\n\n  if(path==='\/'){\n    return new Response(ip, {status: 200,headers: {\n            \"content-type\": \"text\/html;charset=UTF-8\",\n            }})\n  } else if(path==='\/json'){\n    return new Response(JSON.stringify({ ip }), {\n      headers: {\n        'content-type': 'application\/json',\n      },\n    });\n  } else if(path==='\/info') {\n    const info = {\n      ip,\n      geo: request.eo.geo\n    }\n     return new Response(JSON.stringify({ info }), {\n      headers: {\n        'content-type': 'application\/json',\n      },\n    });\n  } else {\n    return new Response(\"404\", {status: 404})\n  }\n  \n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>20250905\u66f4\u65b0<br \/>\n\u652f\u6301cors\u767d\u540d\u5355 \u8bbe\u7f6eallowedDomains \u5c31\u884c<\/p>\n<pre class=\"corepress-code-pre\"><code>addEventListener('fetch', event =&gt; {\n  event.respondWith(handleRequest(event.request));\n});\n\nfunction handleRequest(request) {\n  const url = new URL(request.url);\n  const path = url.pathname;\n  const ip = request.eo.clientIp || '';\n  const origin = request.headers.get('Origin');\n\n  \/\/ \u2705 \u914d\u7f6e\u4f60\u60f3\u8981\u652f\u6301\u7684\u4e3b\u57df\u540d\u5217\u8868\uff08\u4e0d\u5305\u542b\u534f\u8bae\u548c\u8def\u5f84\uff09\n  const allowedDomains = [\n    'lcsoul.cn',\n    'localhost',\n    '127.0.0.1'\n  ];\n\n  \/\/ \u68c0\u67e5 origin \u662f\u5426\u5339\u914d\u4efb\u4f55\u4e00\u4e2a\u5141\u8bb8\u7684\u57df\u540d\uff08\u5305\u62ec\u5b50\u57df\u540d\uff09\n  const corsOrigin = isOriginAllowed(origin, allowedDomains) ? origin : null;\n\n  \/\/ \u5904\u7406\u9884\u68c0\u8bf7\u6c42 (OPTIONS)\n  if (request.method === 'OPTIONS') {\n    if (corsOrigin) {\n      const headers = {\n        'Access-Control-Allow-Origin': corsOrigin,\n        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',\n        'Access-Control-Allow-Headers': 'Content-Type, Authorization',\n        'Access-Control-Max-Age': '86400', \/\/ 24\u5c0f\u65f6\u7f13\u5b58\u9884\u68c0\n      };\n      return new Response(null, { status: 204, headers });\n    }\n    return new Response('Forbidden', { status: 403 });\n  }\n\n  \/\/ \u6784\u5efa\u5b9e\u9645\u54cd\u5e94\n  let response;\n\n  if (path === '\/') {\n    response = new Response(`${ip}\\n`, {\n      status: 200,\n      headers: { 'content-type': 'text\/html;charset=UTF-8' },\n    });\n  } else if (path === '\/json') {\n    response = new Response(JSON.stringify({ ip }), {\n      headers: { 'content-type': 'application\/json' },\n    });\n  } else if (path === '\/info') {\n    const info = {\n      ip,\n      geo: request.eo.geo,\n    };\n    response = new Response(JSON.stringify({ info }), {\n      headers: { 'content-type': 'application\/json' },\n    });\n  } else {\n    response = new Response('404 Not Found', { status: 404 });\n  }\n\n  \/\/ \u6dfb\u52a0 CORS \u54cd\u5e94\u5934\uff08\u4ec5\u5f53\u6765\u6e90\u5408\u6cd5\uff09\n  if (corsOrigin) {\n    response.headers.set('Access-Control-Allow-Origin', corsOrigin);\n    response.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');\n    response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization');\n  }\n\n  return response;\n}\n\n\/**\n * \u5224\u65ad origin \u662f\u5426\u5c5e\u4e8e\u5141\u8bb8\u7684\u57df\u540d\u6216\u5176\u5b50\u57df\u540d\n * \u652f\u6301 http:\/\/ \u548c https:\/\/\uff0c\u5ffd\u7565\u534f\u8bae\n *\/\nfunction isOriginAllowed(origin, allowedDomains) {\n  if (!origin || typeof origin !== 'string') return false;\n\n  try {\n    const url = new URL(origin); \/\/ \u81ea\u52a8\u8865\u5168\u534f\u8bae\u5904\u7406\n    const hostname = url.hostname; \/\/ \u5982\uff1aapi.example.com\n\n    for (const domain of allowedDomains) {\n      \/\/ \u5339\u914d\u4e3b\u57df\u540d\u672c\u8eab\uff1aexample.com\n      if (hostname === domain) return true;\n      \/\/ \u5339\u914d\u5b50\u57df\u540d\uff1a*.example.com\n      if (hostname.endsWith('.' + domain)) return true;\n    }\n  } catch (e) {\n    return false;\n  }\n\n  return false;\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u70b9\u51fb \u521b\u5efa\u5e76\u4fdd\u5b58\u90e8\u7f72<\/p>\n<p>\u5728\u90e8\u7f72\u6210\u529f\u5f39\u7a97\u4e2d \u70b9\u51fb\u65b0\u589e\u89e6\u53d1\u89c4\u5219<\/p>\n<p>\u89e6\u53d1\u6761\u4ef6\u4e2d\u586b\u5165\u4e0a\u9762\u6dfb\u52a0\u7684\u5b50\u57df\u540d\u7136\u540e\u786e\u5b9a\u4fdd\u5b58<\/p>\n<p>\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5b50\u57df\u540d\u83b7\u53d6\u8bbf\u95ee\u7528\u6237\u7684IP\u4e86<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>PS\uff1a\u6dfb\u52a0\u57df\u540d\u65f6\u9009\u62e9\u5f00\u542fIPv6\u8bbf\u95ee\u00a0 \u56e0\u7f51\u7edc\u8bbf\u95ee\u9ed8\u8ba4IPv6\u4f18\u5148 \u5927\u6982\u7387\u53ea\u80fd\u83b7\u53d6\u5230v6\u5730\u5740\uff08\u5982\u679c\u6709\uff09<\/p>\n<p>\u5982\u679c\u9700\u8981\u83b7\u53d6IPv4 \u53ef\u4ee5\u518d\u6dfb\u52a0\u4e00\u4e2a\u57df\u540d \u5173\u95edIPv6\u8bbf\u95ee \u7136\u540e\u518d\u53bb\u8fb9\u7f18\u51fd\u6570 \u89e6\u53d1\u914d\u7f6e\u4e2d \u5728\u521a\u6dfb\u52a0\u7684\u89c4\u5219\u4e2d\u6dfb\u52a0\u65b0\u589e\u7684\u5b50\u57df\u540d\u5373\u53ef<\/p>\n<p>\u5982\u679c\u5565\u4e5f\u4e0d\u77e5\u9053\u00a0 \u6dfb\u52a0\u5b8c\u8fb9\u7f18\u51fd\u6570\u00a0 \u5728\u5217\u8868\u4e2d\u70b9\u51fb\u51fd\u6570\u540d\u79f0 \u8fdb\u5165\u8be6\u60c5\u540e\u70b9\u51fb\u7f16\u8f91\u4ee3\u7801<\/p>\n<p>\u7f16\u8f91\u9875\u9762\u53f3\u4e0a\u89d2\u6709\u4e2aAI\u52a9\u624b \u53ef\u4ee5\u8ba9AI\u5e2e\u4f60\u5199\uff08\u6709\u5751\u81ea\u884c\u6ce8\u610f\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ffffff;\"><a style=\"color: #ffffff;\" href=\"https:\/\/ip.eo.lcsoul.top\/\">\u83b7\u53d6\u8bbf\u95eeIP\uff08\u652f\u6301IPv6\uff09<\/a><\/span><\/p>\n<p><span style=\"color: #ffffff;\"><a style=\"color: #ffffff;\" href=\"https:\/\/ipv4.eo.lcsoul.top\/\">\u83b7\u53d6\u8bbf\u95eeIP\uff08\u4ec5IPv4\uff09<\/a><\/span><\/p>\n<p>\u53c2\u8003\u6587\u6863\uff1a<\/p>\n<p><a href=\"https:\/\/cloud.tencent.com\/document\/product\/1552\/101774\">\u83b7\u53d6\u5ba2\u6237\u7aefIP<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u672c\u662f\u60f3\u7528EdgeOne Pages\u5b9e\u73b0 \u50cfCloudFlare Pages\u90a3\u6837\u90e8\u7f72\u4e00\u4e2a \u540e\u6765\u53d1\u73b0EdgeOn [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,15,17],"tags":[],"class_list":["post-556","post","type-post","status-publish","format-standard","hentry","category-edgeone","category-15","category-17"],"_links":{"self":[{"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/posts\/556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/comments?post=556"}],"version-history":[{"count":0,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/posts\/556\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/media?parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/categories?post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/tags?post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}