From a1ff8750d8e4a420a9d9b3def79ec112bcf35fcf Mon Sep 17 00:00:00 2001 From: Xi Xu Date: Tue, 22 Jul 2025 19:33:33 +0800 Subject: [PATCH] Add modern homepage and serve it at root path Introduces a new `generateHomepage` function in `src/homepage.js` that returns a modern HTML homepage for Xget, featuring a URL converter and platform information. Updates the root path handler in `src/index.js` to serve this homepage directly instead of redirecting to an external site. Adjusts tests in `test/index.spec.js` to validate the new homepage content and response headers. --- src/homepage.js | 844 +++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 17 +- test/index.spec.js | 8 +- 3 files changed, 858 insertions(+), 11 deletions(-) create mode 100644 src/homepage.js diff --git a/src/homepage.js b/src/homepage.js new file mode 100644 index 0000000..222202a --- /dev/null +++ b/src/homepage.js @@ -0,0 +1,844 @@ +/** + * Generates the modern homepage HTML for Xget + * @returns {string} The complete HTML page + */ +export function generateHomepage() { + return ` + + + + + Xget - 高性能代理服务 + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+
+
+

Xget

+

一个高性能、安全的代理服务,专为加速 GitHub、GitLab 和 Hugging Face 的文件下载和 Git 操作而设计

+ 立即开始 +
+
+
+ +
+
+
+

URL 转换器

+
+
+ +
+ +
+
+ +
+
+ 加速链接: +
+ +
+
+
+
+ +
+
+

🌟 核心特性

+
+
+
⚡
+

全球边缘分发

+

基于 Cloudflare 全球 CDN,200+ 城市边缘节点,就近响应用户请求,显著提升下载速度

+
+
+
🌐
+

多平台集成

+

完美支持 GitHub、GitLab、Hugging Face,智能路径转换,无缝访问各平台资源

+
+
+
🔒
+

企业级安全

+

多层安全标头保护,请求验证机制,超时保护,确保安全可靠的代理服务

+
+
+
🎯
+

Git 完全兼容

+

支持 git clone、push、pull 等所有 Git 操作,智能协议检测,无需修改工作流

+
+
+
📱
+

生态系统集成

+

专用浏览器扩展,完美兼容 wget、curl、aria2 等下载工具,CI/CD 环境友好

+
+
+
🚀
+

现代架构

+

智能重试机制,高效缓存策略,性能监控系统,提供稳定可靠的服务体验

+
+
+
+
+ +
+
+

🌐 支持平台

+
+
+ +

GitHub

+

加速 Releases、Archives、Raw 文件下载,完整支持 Git 协议操作

+
+ 前缀: /gh/
+ 示例: xget.xi-xu.me/gh/microsoft/vscode/archive/main.zip +
+
+
+ +

GitLab

+

全面适配 GitLab.com 的文件下载和版本控制功能

+
+ 前缀: /gl/
+ 示例: xget.xi-xu.me/gl/gitlab-org/gitlab/-/archive/master/gitlab-master.zip +
+
+
+ +

Hugging Face

+

针对大型模型文件和数据集进行专门优化,支持高速下载

+
+ 前缀: /hf/
+ 示例: xget.xi-xu.me/hf/microsoft/DialoGPT-medium/resolve/main/pytorch_model.bin +
+
+
+
+
+ +
+
+

📖 使用示例

+
+ + + + +
+ +
+

🎯 Git 版本控制操作

+

Xget 完全兼容 Git 协议,支持所有标准 Git 操作:

+
+# 克隆仓库 +git clone https://xget.xi-xu.me/gh/microsoft/vscode.git + +# 克隆指定分支 +git clone -b main https://xget.xi-xu.me/gh/facebook/react.git + +# 浅克隆(仅最新提交) +git clone --depth 1 https://xget.xi-xu.me/gh/torvalds/linux.git + +# 添加远程仓库 +git remote add upstream https://xget.xi-xu.me/gh/[用户名]/[仓库名].git +
+
+ +
+

⬇️ 主流下载工具集成

+

完美支持 wget、curl、aria2 等下载工具:

+
+# wget 下载 +wget https://xget.xi-xu.me/gh/microsoft/vscode/archive/refs/heads/main.zip + +# curl 下载 +curl -L -O https://xget.xi-xu.me/gh/golang/go/archive/refs/tags/go1.22.0.tar.gz + +# aria2 多线程下载 +aria2c -x 16 -s 16 https://xget.xi-xu.me/hf/microsoft/DialoGPT-large/resolve/main/pytorch_model.bin +
+
+ +
+

🐍 Python 环境中的应用

+

作为 Hugging Face 镜像使用:

+
+import os +from transformers import AutoTokenizer, AutoModelForCausalLM + +# 设置环境变量,让 transformers 库自动使用 Xget 镜像 +os.environ['HF_ENDPOINT'] = 'https://xget.xi-xu.me/hf' + +# 现在所有模型下载都会通过 Xget 加速 +model_name = 'microsoft/DialoGPT-medium' +tokenizer = AutoTokenizer.from_pretrained(model_name) +model = AutoModelForCausalLM.from_pretrained(model_name) +
+
+ +
+

🔄 CI/CD 环境集成

+

在 GitHub Actions 中使用 Xget:

+
+name: Download Dependencies +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Download model files + run: | + wget https://xget.xi-xu.me/hf/microsoft/DialoGPT-medium/resolve/main/pytorch_model.bin + + - name: Clone dependency repo + run: | + git clone https://xget.xi-xu.me/gh/[依赖项目]/[仓库名].git +
+
+
+
+
+ + + + + +`; +} diff --git a/src/index.js b/src/index.js index a8f206d..f59284f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ import { CONFIG } from "./config/index.js"; +import { generateHomepage } from "./homepage.js"; /** * Monitors performance metrics during request processing @@ -132,17 +133,13 @@ async function handleRequest(request, env, ctx) { // Serve homepage for root path if (url.pathname === "/" || url.pathname === "") { - const HOME_PAGE_URL = "https://xixu-me.github.io/Xget-page/"; - const requestHeaders = new Headers(request.headers); - - const homePageRequest = new Request(HOME_PAGE_URL, { - method: request.method, - headers: requestHeaders, - body: request.body, - redirect: "manual", + const homepageHTML = generateHomepage(); + return new Response(homepageHTML, { + headers: { + "Content-Type": "text/html; charset=utf-8", + ...addSecurityHeaders(new Headers()), + }, }); - - return fetch(homePageRequest, { redirect: "manual" }); } const monitor = new PerformanceMonitor(); diff --git a/test/index.spec.js b/test/index.spec.js index 90e7b5f..cddf663 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -13,8 +13,14 @@ describe("Xget Worker", () => { const response = await worker.fetch(request, env, ctx); await waitOnExecutionContext(ctx); - // The homepage should redirect to the external page + // The homepage should return HTML content expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toContain("text/html"); + + const html = await response.text(); + expect(html).toContain(""); + expect(html).toContain("Xget - 高性能代理服务"); + expect(html).toContain("URL 转换器"); }); it("handles invalid platform", async () => {