├── README.md ├── LICENSE └── script.sh /README.md: -------------------------------------------------------------------------------- 1 | # HubProxy 2 | 本项目是一个轻量、高性能的代理部署脚本,支持一键搭建 Docker 镜像和 GitHub 文件加速服务。集成了 Cloudflare API、Caddy 和 HubProxy,实现自动配置与快速部署。 3 | 4 | # HubProxy 一键安装脚本 5 | 6 | 🚀 **Docker 和 GitHub 加速代理服务器一键部署脚本** 7 | 8 | 一个轻量级、高性能的多功能代理服务部署脚本,提供 Docker 镜像加速、GitHub 文件加速、下载离线镜像、在线搜索 Docker 镜像等功能的一键部署。 9 | 10 | 本项目基于 [sky22333/hubproxy](https://github.com/sky22333/hubproxy) 开发,感谢原项目作者的杰出贡献。 11 | 12 | 官方示例: https://demo.52013120.xyz/ 13 | 14 | ## 📖 目录 15 | 16 | - [📝 更新日志](#-更新日志) 17 | - [✨ 脚本特点](#-脚本特点) 18 | - [🎯 解决的问题](#-解决的问题) 19 | - [📥 安装](#-安装) 20 | - [🔐 创建 Cloudflare API Token](#-创建-cloudflare-api-token) 21 | - [⚙️ 使用方法](#️-使用方法) 22 | - [安装过程](#安装过程) 23 | - [卸载过程](#卸载过程) 24 | - [🐳 方案说明](#-方案说明) 25 | - [VPS方案](#vps方案) 26 | - [Docker方案](#docker方案) 27 | - [部署及 itdog 多地 ping 截图](#部署及-itdog-多地-ping-截图) 28 | - [🚀 HubProxy 功能说明](#-hubproxy-功能说明) 29 | - [🔧 配置说明](#-配置说明) 30 | - [⚠️ 免责声明](#️-免责声明) 31 | 32 | ## 📝 更新日志 33 | 34 | - **2025-08-08 v1.0.1** 1.优化 Zone ID 获取方式:通过 API 自动获取,无需用户手动输入; 2.VPS 卸载增强:卸载时可选择是否一并卸载 Caddy,默认保留 Caddy 35 | 36 | - **2025-07-30 v1.0.0** 1. 全自动化HTTPS解决方案:深度整合Caddy 2反向代理与Let's Encrypt证书自动签发,实现零配置的HTTPS服务部署; 2. 双模式智能部署:支持Docker容器化与宿主机原生部署,通过环境自适应的安装脚本自动选择最优方案 37 | 38 | ## ✨ 脚本特点 39 | 40 | - 🔄 **双方案部署** - 支持传统VPS部署和Docker容器化部署两种方案 41 | - 🌐 **自动DNS配置** - 集成Cloudflare API,自动创建和配置DNS记录 42 | - 🔐 **SSL证书自动申请与维护** - 集成Caddy自动申请和维护Let's Encrypt SSL证书 43 | - 🚀 **一键部署** - 自动化安装所有依赖,配置反向代理,启用Cloudflare代理 44 | - 🧹 **完整卸载** - 支持完全卸载已部署的方案,清理所有相关资源 45 | - 🏗️ **系统适配** - 支持Ubuntu、Debian、CentOS、RHEL、Fedora、Arch Linux等主流系统 46 | - 📦 **依赖管理** - 自动检测和安装所需依赖,包括curl/wget、jq、docker等 47 | - 🌍 **IPv4/IPv6互通** - 使用Cloudflare CDN,突破 IPv4/IPv6 互通限制,即使在仅支持 IPv6 的机器上,也能成功下载来自 GitHub(IPv4-only)的文件 48 | 49 | ## 🎯 解决的问题 50 | 51 | 本脚本主要解决以下问题: 52 | 53 | 1. **复杂的手动配置** - 传统部署需要手动安装Caddy、配置反向代理、申请SSL证书、配置DNS等复杂步骤 54 | 2. **环境依赖问题** - 自动检测和安装系统依赖,避免因缺少依赖导致的部署失败 55 | 3. **SSL证书配置** - 集成Caddy自动申请和管理SSL证书,无需手动操作 56 | 4. **DNS配置繁琐** - 集成Cloudflare API,自动创建和更新DNS记录 57 | 5. **部署方案选择困难** - 提供VPS和Docker两种部署方案,满足不同用户需求 58 | 6. **卸载不彻底** - 提供完整的卸载功能,确保所有相关资源被正确清理 59 | 60 | ## 📥 安装 61 | 62 | ```bash 63 | bash <(curl -sSL https://raw.githubusercontent.com/fscarmen2/hubproxy/main/script.sh) 64 | ``` 65 | 或者 66 | ```bash 67 | bash <(wget -qO- https://raw.githubusercontent.com/fscarmen2/hubproxy/main/script.sh) 68 | ``` 69 | 70 | ## 🔐 创建 Cloudflare API Token 71 | 72 | 1. 在 Cloudflare 控制台右上角点击用户头像,或访问 https://dash.cloudflare.com/profile/api-tokens 73 | 2. 选择"我的个人资料" 74 | 3. 点击"API Tokens"选项卡 75 | 4. 点击"创建令牌" 76 | 5. 选择"自定义令牌" 77 | 6. 配置以下权限: 78 | - 权限: 79 | - Zone - DNS - Edit 80 | - 区域资源: 81 | - 包含 - 指定区域(选择您的域名) 82 | 7. 点击"继续到摘要" 83 | 8. 点击"创建令牌" 84 | 9. 复制生成的 API Token 并保存 85 | 86 | 87 | image 88 | 89 | image 90 | 91 | image 92 | 93 | ## ⚙️ 使用方法 94 | 95 | ### 安装过程 96 | 97 | 脚本运行后会引导用户完成以下步骤: 98 | 99 | 1. **选择部署方案** - 选择 VPS 方案或 Docker 方案 100 | 2. **输入 Cloudflare 信息** - 输入 API Token 和域名(无需 Zone ID) 101 | 3. **选择服务器 IP** - 从检测到的 IP 地址中选择或输入自定义 IP 102 | 4. **自动部署** - 脚本会自动完成以下操作: 103 | - 安装系统依赖 104 | - 自动获取 Zone ID 105 | - 创建 DNS 记录 106 | - 安装和配置 Caddy 107 | - 部署 HubProxy 服务 108 | - 配置反向代理 109 | - 启用 Cloudflare 代理 110 | - 等待 SSL 证书生成 111 | 112 | ### 卸载过程 113 | 114 | 脚本支持完全卸载已部署的方案: 115 | 116 | 1. **选择卸载操作** - 在脚本菜单中选择卸载选项 117 | 2. **自动检测已部署方案** - 脚本会自动检测已部署的方案 118 | 3. **执行卸载** - 根据检测到的方案执行相应的卸载操作: 119 | - **VPS方案卸载**:停止并禁用服务,删除安装目录和配置文件 120 | - **Docker方案卸载**:停止并删除容器,删除相关镜像和映射目录 121 | 122 | ## 🐳 方案说明 123 | 124 | ### VPS方案 125 | 126 | VPS方案将HubProxy作为系统服务直接部署在服务器上: 127 | 128 | - HubProxy以systemd服务方式运行 129 | - Caddy作为反向代理和SSL证书管理器 130 | - 配置文件位于`/opt/hubproxy/config.toml` 131 | - 日志文件位于`/var/log/hubproxy/` 132 | - 服务名称:`hubproxy` 133 | 134 | ### Docker方案 135 | 136 | Docker方案使用Docker Compose部署HubProxy和Caddy: 137 | 138 | - 使用官方HubProxy镜像:`ghcr.io/sky22333/hubproxy` 139 | - 使用官方Caddy镜像:`caddy:latest` 140 | - 所有配置和数据映射到`/root/hubproxy/`目录 141 | - 自动创建Docker网络实现容器间通信 142 | - SSL证书存储在`/root/hubproxy/data/caddy/certificates/`目录 143 | 144 | ### 部署及 itdog 多地 ping 截图 145 | 146 | image 147 | 148 | image 149 | 150 | image 151 | 152 | image 153 | 154 | ## 🚀 HubProxy 功能说明 155 | 156 | - 🐳 **Docker 镜像加速** - 单域名实现 Docker Hub、GHCR、Quay 等多个镜像仓库加速,流式传输优化拉取速度。 157 | - 🐳 **离线镜像包** - 支持下载离线镜像包,流式传输加防抖设计。 158 | - 📁 **GitHub 文件加速** - 加速 GitHub Release、Raw 文件下载,支持`api.github.com`,脚本嵌套加速等等 159 | - 🤖 **AI 模型库支持** - 支持 Hugging Face 模型下载加速 160 | - 🛡️ **智能限流** - IP 限流保护,防止滥用 161 | - 🚫 **仓库审计** - 强大的自定义黑名单,白名单,同时审计镜像仓库,和GitHub仓库 162 | - 🔍 **镜像搜索** - 在线搜索 Docker 镜像 163 | - ⚡ **轻量高效** - 基于 Go 语言,单二进制文件运行,资源占用低,优雅的内存清理机制。 164 | - 🔧 **统一配置** - 统一配置管理 165 | 166 | ## 🔧 配置说明 167 | 168 | 脚本会自动配置大部分参数,用户只需提供以下信息: 169 | 170 | 1. **Cloudflare API Token** - 具有DNS记录编辑权限的API令牌 171 | 2. **域名** - 用于访问HubProxy服务的域名 172 | 173 | ## ⚠️ 免责声明 174 | 175 | - 本程序仅供学习交流使用,请勿用于非法用途 176 | - 使用本程序需遵守当地法律法规 177 | - 作者不对使用者的任何行为承担责任 178 | - 脚本按"现状"提供,不提供任何担保 179 | 180 | --- 181 | 182 |
183 | 184 | **⭐ 如果这个项目对你有帮助,请给个 Star!⭐** 185 | 186 |
-------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 版本信息 4 | VERSION="1.0.1" 5 | UPDATE_DATE="2025-08-08" 6 | 7 | # 更新日志 8 | # v1.0.1 (2025-08-08) 9 | # - 优化 Zone ID 获取方式:通过 API 自动获取,无需用户手动输入 10 | # - VPS 卸载增强:卸载时可选择是否一并卸载 Caddy,默认保留 Caddy 11 | # v1.0.0 (2025-07-30) 12 | # - 开箱即用的深度整合方案:从证书申请到HTTPS反代全程自动化,为HubProxy提供完整的Docker/宿主机双栈加速服务 13 | # - 支持VPS和Docker双方案部署 14 | # - 集成Cloudflare API,自动创建和配置DNS记录 15 | # - 自动申请和维护SSL证书 16 | # - 支持IPv4/IPv6双栈环境 17 | # - 一键部署和卸载功能 18 | 19 | # 颜色定义 20 | RED='\033[0;31m' 21 | GREEN='\033[0;32m' 22 | YELLOW='\033[1;33m' 23 | BLUE='\033[0;34m' 24 | NC='\033[0m' # No Color 25 | 26 | # 配置 27 | REPO="sky22333/hubproxy" 28 | GITHUB_API="https://api.github.com/repos/${REPO}" 29 | GITHUB_RELEASES="${GITHUB_API}/releases" 30 | SERVICE_NAME="hubproxy" 31 | INSTALL_DIR="/opt/hubproxy" 32 | CONFIG_FILE="config.toml" 33 | BINARY_NAME="hubproxy" 34 | LOG_DIR="/var/log/hubproxy" 35 | TEMP_DIR="/tmp/hubproxy-install" 36 | CADDYFILE="/etc/caddy/Caddyfile" 37 | 38 | echo -e "${RED}HubProxy with Caddy and Cloudflare 集成一键安装脚本${NC}" 39 | echo -e "${GREEN}版本: ${BLUE}${VERSION} ${GREEN}更新日期: ${BLUE}${UPDATE_DATE}${NC}" 40 | echo -e "${GREEN}更新日志: ${BLUE}开箱即用的深度整合方案 - 从证书申请到HTTPS反代全程自动化,为HubProxy提供完整的Docker/宿主机双栈加速服务。${NC}" 41 | echo "===============================================================" 42 | 43 | # 检查是否以root权限运行 44 | if [[ $EUID -ne 0 ]]; then 45 | echo -e "${RED}此脚本需要root权限运行${NC}" 46 | echo "请使用: su root -c \"$0\" 或在root用户下运行此脚本" 47 | exit 1 48 | fi 49 | 50 | # 检查已安装的方案 51 | check_installed_schemes() { 52 | VPS_INSTALLED=false 53 | DOCKER_INSTALLED=false 54 | 55 | # 检查VPS方案是否已安装 56 | if [ -d "${INSTALL_DIR}" ] || systemctl is-active --quiet ${SERVICE_NAME} 2>/dev/null; then 57 | VPS_INSTALLED=true 58 | fi 59 | 60 | # 检查Docker方案是否已安装 61 | if [ -d "/root/hubproxy" ] && [ -f "/root/hubproxy/docker-compose.yml" ]; then 62 | DOCKER_INSTALLED=true 63 | fi 64 | } 65 | 66 | # 选择安装方案 67 | select_installation_method() { 68 | # 检查已安装的方案 69 | check_installed_schemes 70 | 71 | echo -e "${BLUE}请选择操作:${NC}" 72 | echo "1) 安装 - VPS方案 (默认)" 73 | echo "2) 安装 - Docker方案" 74 | 75 | # 只有当有已安装的方案时才显示卸载选项 76 | if [ "$VPS_INSTALLED" = true ] || [ "$DOCKER_INSTALLED" = true ]; then 77 | echo "3) 卸载" 78 | fi 79 | 80 | # 根据是否有已安装的方案调整默认选项和读取范围 81 | if [ "$VPS_INSTALLED" = true ] || [ "$DOCKER_INSTALLED" = true ]; then 82 | read -p "请输入选项 (1-3, 默认为1): " INSTALL_METHOD 83 | INSTALL_METHOD=${INSTALL_METHOD:-1} 84 | else 85 | read -p "请输入选项 (1-2, 默认为1): " INSTALL_METHOD 86 | INSTALL_METHOD=${INSTALL_METHOD:-1} 87 | fi 88 | 89 | case $INSTALL_METHOD in 90 | 1) 91 | echo -e "${GREEN}已选择 VPS 安装方案${NC}" 92 | INSTALL_TYPE="vps" 93 | OPERATION="install" 94 | ;; 95 | 2) 96 | echo -e "${GREEN}已选择 Docker 安装方案${NC}" 97 | INSTALL_TYPE="docker" 98 | OPERATION="install" 99 | ;; 100 | 3) 101 | # 只有当有已安装的方案时才允许选择卸载 102 | if [ "$VPS_INSTALLED" = true ] || [ "$DOCKER_INSTALLED" = true ]; then 103 | echo -e "${GREEN}已选择卸载${NC}" 104 | OPERATION="uninstall" 105 | select_uninstall_method 106 | else 107 | echo -e "${YELLOW}无效选项,使用默认 VPS 安装方案${NC}" 108 | INSTALL_TYPE="vps" 109 | OPERATION="install" 110 | fi 111 | ;; 112 | *) 113 | echo -e "${YELLOW}无效选项,使用默认 VPS 安装方案${NC}" 114 | INSTALL_TYPE="vps" 115 | OPERATION="install" 116 | ;; 117 | esac 118 | } 119 | 120 | # 选择卸载方案 121 | select_uninstall_method() { 122 | # 检查已安装的方案 123 | check_installed_schemes 124 | 125 | # 如果只安装了一种方案,直接卸载该方案 126 | if [ "$VPS_INSTALLED" = true ] && [ "$DOCKER_INSTALLED" = false ]; then 127 | echo -e "${GREEN}检测到已安装 VPS 方案,将卸载该方案${NC}" 128 | INSTALL_TYPE="vps" 129 | return 130 | elif [ "$VPS_INSTALLED" = false ] && [ "$DOCKER_INSTALLED" = true ]; then 131 | echo -e "${GREEN}检测到已安装 Docker 方案,将卸载该方案${NC}" 132 | INSTALL_TYPE="docker" 133 | return 134 | fi 135 | 136 | # 如果两种方案都安装了,让用户选择 137 | echo -e "${BLUE}检测到已安装多个方案,请选择要卸载的方案:${NC}" 138 | echo "1) VPS方案 (默认)" 139 | echo "2) Docker方案" 140 | 141 | read -p "请输入选项 (1-2, 默认为1): " UNINSTALL_METHOD 142 | UNINSTALL_METHOD=${UNINSTALL_METHOD:-1} 143 | 144 | case $UNINSTALL_METHOD in 145 | 1) 146 | echo -e "${GREEN}已选择 VPS 卸载方案${NC}" 147 | INSTALL_TYPE="vps" 148 | ;; 149 | 2) 150 | echo -e "${GREEN}已选择 Docker 卸载方案${NC}" 151 | INSTALL_TYPE="docker" 152 | ;; 153 | *) 154 | echo -e "${YELLOW}无效选项,使用默认 VPS 卸载方案${NC}" 155 | INSTALL_TYPE="vps" 156 | ;; 157 | esac 158 | } 159 | 160 | # 检查命令是否存在 161 | command_exists() { 162 | command -v "$1" >/dev/null 2>&1 163 | } 164 | 165 | # 检查是否有可用的JSON处理工具 166 | has_json_tool() { 167 | # 检查是否有 jq 168 | if command_exists jq; then 169 | JSON_TOOL="jq" 170 | return 0 171 | fi 172 | 173 | return 1 174 | } 175 | 176 | # 检测操作系统 177 | detect_os() { 178 | if [ -f /etc/os-release ]; then 179 | . /etc/os-release 180 | OS=$ID 181 | OS_VERSION=$VERSION_ID 182 | else 183 | echo -e "${RED}错误: 无法从 /etc/os-release 检测操作系统${NC}" 184 | exit 1 185 | fi 186 | echo -e "${GREEN}检测到操作系统: $OS $OS_VERSION${NC}" 187 | } 188 | 189 | # 安装系统依赖 190 | install_system_dependencies() { 191 | echo -e "${BLUE}检查并安装系统依赖...${NC}" 192 | 193 | detect_os 194 | 195 | # 确定要使用的获取工具(curl 或 wget) 196 | if command_exists curl; then 197 | FETCH_TOOL="curl" 198 | echo -e "${GREEN}使用 curl 作为下载工具${NC}" 199 | elif command_exists wget; then 200 | FETCH_TOOL="wget" 201 | echo -e "${GREEN}使用 wget 作为下载工具${NC}" 202 | else 203 | echo -e "${YELLOW}未安装 curl 或 wget,正在安装 curl...${NC}" 204 | if [ "$OS" == "debian" ] || [ "$OS" == "ubuntu" ] || [ "$OS" == "raspbian" ]; then 205 | apt update &>/dev/null && apt install -y curl &>/dev/null 206 | elif [ "$OS" == "fedora" ]; then 207 | dnf install -y curl &>/dev/null 208 | elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then 209 | if [ "$OS_VERSION" == "7" ]; then 210 | yum install -y curl &>/dev/null 211 | else 212 | dnf install -y curl &>/dev/null 213 | fi 214 | elif [ "$OS" == "arch" ] || [ "$OS" == "manjaro" ] || [ "$OS" == "parabola" ]; then 215 | pacman -Syu --noconfirm curl &>/dev/null 216 | fi 217 | FETCH_TOOL="curl" 218 | echo -e "${GREEN}curl 安装完成${NC}" 219 | fi 220 | 221 | # 检查需要的其他依赖 222 | missing_deps=() 223 | for cmd in tar jq; do 224 | if ! command_exists $cmd; then 225 | missing_deps+=($cmd) 226 | fi 227 | done 228 | 229 | if [ ${#missing_deps[@]} -gt 0 ]; then 230 | echo -e "${YELLOW}检测到缺少依赖: ${missing_deps[*]}${NC}" 231 | echo -e "${BLUE}正在自动安装依赖...${NC}" 232 | 233 | if [ "$OS" == "debian" ] || [ "$OS" == "ubuntu" ] || [ "$OS" == "raspbian" ]; then 234 | apt install -y "${missing_deps[@]}" &>/dev/null 235 | elif [ "$OS" == "fedora" ]; then 236 | dnf install -y "${missing_deps[@]}" &>/dev/null 237 | elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then 238 | if [ "$OS_VERSION" == "7" ]; then 239 | yum install -y "${missing_deps[@]}" &>/dev/null 240 | else 241 | dnf install -y "${missing_deps[@]}" &>/dev/null 242 | fi 243 | elif [ "$OS" == "arch" ] || [ "$OS" == "manjaro" ] || [ "$OS" == "parabola" ]; then 244 | pacman -Syu --noconfirm "${missing_deps[@]}" &>/dev/null 245 | fi 246 | 247 | if [ $? -ne 0 ]; then 248 | echo -e "${RED}依赖安装失败${NC}" 249 | exit 1 250 | fi 251 | 252 | echo -e "${GREEN}依赖安装成功${NC}" 253 | 254 | # 重新检查JSON工具 255 | if ! has_json_tool; then 256 | echo -e "${RED}无法找到可用的JSON处理工具${NC}" 257 | exit 1 258 | fi 259 | else 260 | echo -e "${GREEN}所有依赖已安装${NC}" 261 | fi 262 | } 263 | 264 | # 安装 Caddy 265 | install_caddy() { 266 | # 检查 Caddy 是否已安装 267 | if command_exists caddy; then 268 | CADDY_VERSION=$(caddy version | awk 'NR==1 {print $1}') 269 | echo -e "${GREEN}Caddy 已安装,版本: $CADDY_VERSION${NC}" 270 | return 0 271 | fi 272 | 273 | echo -e "${YELLOW}正在安装 Caddy...${NC}" 274 | 275 | if [ "$OS" == "debian" ] || [ "$OS" == "ubuntu" ] || [ "$OS" == "raspbian" ]; then 276 | # 安装依赖 277 | apt install -y debian-keyring debian-archive-keyring apt-transport-https &>/dev/null 278 | 279 | # 获取 gpg 密钥(仅当文件不存在时) 280 | if [ ! -f /usr/share/keyrings/caddy-stable-archive-keyring.gpg ]; then 281 | if [ "$FETCH_TOOL" == "curl" ]; then 282 | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg &>/dev/null 283 | else 284 | wget -qO- 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg &>/dev/null 285 | fi 286 | fi 287 | 288 | # 获取 debian.list(仅当文件不存在时) 289 | if [ ! -f /etc/apt/sources.list.d/caddy-stable.list ]; then 290 | if [ "$FETCH_TOOL" == "curl" ]; then 291 | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' > /etc/apt/sources.list.d/caddy-stable.list 2>/dev/null 292 | else 293 | wget -qO- 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' > /etc/apt/sources.list.d/caddy-stable.list 2>/dev/null 294 | fi 295 | fi 296 | 297 | # 设置权限 298 | chmod o+r /usr/share/keyrings/caddy-stable-archive-keyring.gpg &>/dev/null 299 | chmod o+r /etc/apt/sources.list.d/caddy-stable.list &>/dev/null 300 | 301 | # 更新 apt 302 | apt update &>/dev/null 303 | 304 | # 安装 caddy 305 | apt install -y caddy &>/dev/null 306 | elif [ "$OS" == "fedora" ]; then 307 | # Fedora 308 | dnf install -y 'dnf-command(copr)' &>/dev/null 309 | dnf copr enable -y @caddy/caddy &>/dev/null 310 | dnf install -y caddy &>/dev/null 311 | elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then 312 | # RHEL/CentOS 313 | if [ "$OS_VERSION" == "7" ]; then 314 | # RHEL/CentOS 7 315 | yum install -y yum-plugin-copr &>/dev/null 316 | yum copr enable -y @caddy/caddy &>/dev/null 317 | yum install -y caddy &>/dev/null 318 | else 319 | # RHEL/CentOS 8+ 320 | dnf install -y 'dnf-command(copr)' &>/dev/null 321 | dnf copr enable -y @caddy/caddy &>/dev/null 322 | dnf install -y caddy &>/dev/null 323 | fi 324 | elif [ "$OS" == "arch" ] || [ "$OS" == "manjaro" ] || [ "$OS" == "parabola" ]; then 325 | # Arch Linux/Manjaro/Parabola 326 | pacman -Syu --noconfirm caddy &>/dev/null 327 | else 328 | echo -e "${RED}错误: 不支持在 $OS 上安装${NC}" 329 | exit 1 330 | fi 331 | 332 | echo -e "${GREEN}Caddy 安装完成${NC}" 333 | } 334 | 335 | # 安装 Docker 336 | install_docker() { 337 | echo -e "${BLUE}检查并安装 Docker...${NC}" 338 | 339 | # 检查 Docker 是否已安装 340 | if command_exists docker; then 341 | DOCKER_VERSION=$(docker --version | awk '{print $3}' | sed 's/,//') 342 | echo -e "${GREEN}Docker 已安装,版本: $DOCKER_VERSION${NC}" 343 | return 0 344 | fi 345 | 346 | echo -e "${YELLOW}正在安装 Docker...${NC}" 347 | 348 | if [ "$OS" == "debian" ] || [ "$OS" == "ubuntu" ] || [ "$OS" == "raspbian" ]; then 349 | # 添加 Docker 官方 GPG 密钥 350 | apt-get update &>/dev/null 351 | apt-get install -y ca-certificates curl gnupg &>/dev/null 352 | 353 | # 创建目录 354 | install -m 0755 -d /etc/apt/keyrings &>/dev/null 355 | 356 | # 下载并添加 GPG 密钥 357 | if [ "$OS" == "ubuntu" ]; then 358 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc &>/dev/null 359 | elif [ "$OS" == "debian" ]; then 360 | curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc &>/dev/null 361 | else # raspbian 362 | curl -fsSL https://download.docker.com/linux/raspbian/gpg -o /etc/apt/keyrings/docker.asc &>/dev/null 363 | fi 364 | 365 | chmod a+r /etc/apt/keyrings/docker.asc &>/dev/null 366 | 367 | # 添加仓库到 Apt 源 368 | if [ "$OS" == "ubuntu" ]; then 369 | echo \ 370 | "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ 371 | $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \ 372 | tee /etc/apt/sources.list.d/docker.list > /dev/null 373 | elif [ "$OS" == "debian" ]; then 374 | echo \ 375 | "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \ 376 | $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ 377 | tee /etc/apt/sources.list.d/docker.list > /dev/null 378 | else # raspbian 379 | echo \ 380 | "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/raspbian \ 381 | $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ 382 | tee /etc/apt/sources.list.d/docker.list > /dev/null 383 | fi 384 | 385 | # 更新并安装 Docker 386 | apt-get update &>/dev/null 387 | apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin &>/dev/null 388 | elif [ "$OS" == "fedora" ]; then 389 | dnf -y install dnf-plugins-core &>/dev/null 390 | dnf-3 config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo &>/dev/null 391 | dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin &>/dev/null 392 | systemctl enable --now docker &>/dev/null 393 | elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then 394 | dnf -y install dnf-plugins-core &>/dev/null 395 | dnf config-manager --add-repo https://download.docker.com/linux/"$OS"/docker-ce.repo &>/dev/null 396 | dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin &>/dev/null 397 | systemctl enable --now docker &>/dev/null 398 | elif [ "$OS" == "arch" ] || [ "$OS" == "manjaro" ] || [ "$OS" == "parabola" ]; then 399 | pacman -Syu --noconfirm docker docker-compose &>/dev/null 400 | systemctl enable --now docker &>/dev/null 401 | else 402 | echo -e "${RED}错误: 不支持在 $OS 上安装 Docker${NC}" 403 | exit 1 404 | fi 405 | 406 | # 启动 Docker 服务(除 Fedora/CentOS/RHEL/Arch 外) 407 | if [[ ! "$OS" =~ ^(fedora|centos|rhel|arch|manjaro|parabola)$ ]]; then 408 | systemctl enable --now docker &>/dev/null 409 | fi 410 | 411 | echo -e "${GREEN}Docker 安装完成${NC}" 412 | } 413 | 414 | # 检测系统架构 415 | detect_arch() { 416 | local arch=$(uname -m) 417 | case $arch in 418 | x86_64) 419 | echo "amd64" 420 | ;; 421 | aarch64|arm64) 422 | echo "arm64" 423 | ;; 424 | *) 425 | echo -e "${RED}不支持的架构: $arch${NC}" 426 | exit 1 427 | ;; 428 | esac 429 | } 430 | 431 | # 安装 HubProxy 432 | install_hubproxy() { 433 | ARCH=$(detect_arch) 434 | echo -e "${BLUE}检测到架构: linux-${ARCH}${NC}" 435 | 436 | # 检查是否为本地安装模式 437 | if [ -f "${BINARY_NAME}" ]; then 438 | echo -e "${BLUE}发现本地文件,使用本地安装模式${NC}" 439 | LOCAL_INSTALL=true 440 | else 441 | echo -e "${BLUE}本地无文件,使用自动下载模式${NC}" 442 | LOCAL_INSTALL=false 443 | 444 | # 自动下载功能 445 | if [ "$LOCAL_INSTALL" = false ]; then 446 | echo -e "${BLUE}获取最新版本信息...${NC}" 447 | if [ "$FETCH_TOOL" == "curl" ]; then 448 | LATEST_RELEASE=$(curl -s "${GITHUB_RELEASES}/latest") 449 | else 450 | LATEST_RELEASE=$(wget -qO- "${GITHUB_RELEASES}/latest") 451 | fi 452 | 453 | if [ $? -ne 0 ]; then 454 | echo -e "${RED}无法获取版本信息${NC}" 455 | exit 1 456 | fi 457 | 458 | VERSION=$(echo "$LATEST_RELEASE" | jq -r '.tag_name') 459 | if [ "$VERSION" = "null" ]; then 460 | echo -e "${RED}无法解析版本信息${NC}" 461 | exit 1 462 | fi 463 | 464 | echo -e "${GREEN}最新版本: ${VERSION}${NC}" 465 | 466 | # 构造下载URL 467 | ASSET_NAME="hubproxy-${VERSION}-linux-${ARCH}.tar.gz" 468 | DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/${ASSET_NAME}" 469 | 470 | echo -e "${BLUE}下载: ${ASSET_NAME}${NC}" 471 | 472 | # 创建临时目录并下载 473 | rm -rf "${TEMP_DIR}" 474 | mkdir -p "${TEMP_DIR}" 475 | cd "${TEMP_DIR}" 476 | 477 | if [ "$FETCH_TOOL" == "curl" ]; then 478 | curl -sSL -o "${ASSET_NAME}" "${DOWNLOAD_URL}" 479 | else 480 | wget -q -O "${ASSET_NAME}" "${DOWNLOAD_URL}" 481 | fi 482 | 483 | if [ $? -ne 0 ]; then 484 | echo -e "${RED}下载失败${NC}" 485 | exit 1 486 | fi 487 | 488 | # 解压 489 | tar -xzf "${ASSET_NAME}" 490 | if [ $? -ne 0 ] || [ ! -d "hubproxy" ]; then 491 | echo -e "${RED}解压失败${NC}" 492 | exit 1 493 | fi 494 | 495 | cd hubproxy 496 | echo -e "${GREEN}下载完成${NC}" 497 | fi 498 | fi 499 | 500 | echo -e "${YELLOW}开始安装 HubProxy...${NC}" 501 | 502 | # 停止现有服务(如果存在) 503 | if systemctl is-active --quiet ${SERVICE_NAME} 2>/dev/null; then 504 | echo -e "${YELLOW}停止现有服务...${NC}" 505 | systemctl stop ${SERVICE_NAME} 506 | fi 507 | 508 | # 备份现有配置(如果存在) 509 | CONFIG_BACKUP_EXISTS=false 510 | if [ -f "${INSTALL_DIR}/${CONFIG_FILE}" ]; then 511 | echo -e "${BLUE}备份现有配置...${NC}" 512 | cp "${INSTALL_DIR}/${CONFIG_FILE}" "${TEMP_DIR}/config.toml.backup" 513 | CONFIG_BACKUP_EXISTS=true 514 | fi 515 | 516 | # 1. 创建目录结构 517 | echo -e "${BLUE}创建目录结构${NC}" 518 | mkdir -p ${INSTALL_DIR} 519 | mkdir -p ${LOG_DIR} 520 | chmod 755 ${INSTALL_DIR} 521 | chmod 755 ${LOG_DIR} 522 | 523 | # 2. 复制二进制文件 524 | echo -e "${BLUE}复制二进制文件${NC}" 525 | cp "${BINARY_NAME}" "${INSTALL_DIR}/" 526 | chmod +x "${INSTALL_DIR}/${BINARY_NAME}" 527 | 528 | # 3. 复制配置文件 529 | echo -e "${BLUE}复制配置文件${NC}" 530 | if [ -f "${CONFIG_FILE}" ]; then 531 | if [ "$CONFIG_BACKUP_EXISTS" = false ]; then 532 | cp "${CONFIG_FILE}" "${INSTALL_DIR}/" 533 | echo -e "${GREEN}配置文件复制成功${NC}" 534 | else 535 | echo -e "${YELLOW}保留现有配置文件${NC}" 536 | fi 537 | else 538 | echo -e "${YELLOW}配置文件不存在,将使用默认配置${NC}" 539 | fi 540 | 541 | # 4. 安装systemd服务文件 542 | echo -e "${BLUE}安装systemd服务文件${NC}" 543 | cp "${SERVICE_NAME}.service" "/etc/systemd/system/" 544 | systemctl daemon-reload 545 | 546 | # 5. 恢复配置文件(如果有备份) 547 | if [ "$CONFIG_BACKUP_EXISTS" = true ]; then 548 | echo -e "${BLUE}恢复配置文件...${NC}" 549 | cp "${TEMP_DIR}/config.toml.backup" "${INSTALL_DIR}/${CONFIG_FILE}" 550 | fi 551 | 552 | # 6. 启用并启动服务 553 | echo -e "${BLUE}启用并启动服务${NC}" 554 | systemctl enable ${SERVICE_NAME} 555 | systemctl start ${SERVICE_NAME} 556 | 557 | # 7. 清理临时文件 558 | if [ "$LOCAL_INSTALL" = false ]; then 559 | echo -e "${BLUE}清理临时文件...${NC}" 560 | cd / 561 | rm -rf "${TEMP_DIR}" 562 | fi 563 | 564 | # 8. 检查服务状态 565 | sleep 2 566 | if systemctl is-active --quiet ${SERVICE_NAME}; then 567 | echo -e "${GREEN}HubProxy 安装成功!${NC}" 568 | echo -e "${GREEN}默认运行端口: 5000${NC}" 569 | echo -e "${GREEN}配置文件路径: ${INSTALL_DIR}/${CONFIG_FILE}${NC}" 570 | else 571 | echo -e "${RED}服务启动失败${NC}" 572 | echo "查看错误日志: sudo journalctl -u ${SERVICE_NAME} -f" 573 | exit 1 574 | fi 575 | } 576 | 577 | # 获取 IP 地址 578 | get_ip_addresses() { 579 | echo -e "${BLUE}获取 IP 地址信息...${NC}" 580 | 581 | # 尝试获取 IPv4 582 | if [ "$FETCH_TOOL" == "curl" ]; then 583 | IPV4=$(curl -4s https://icanhazip.com 2>/dev/null || curl -4s http://ipinfo.io/ip 2>/dev/null || true) 584 | else 585 | IPV4=$(wget -4qO- https://icanhazip.com 2>/dev/null || $FETCH_TOOL -4qO- http://ipinfo.io/ip 2>/dev/null || true) 586 | fi 587 | 588 | if [ -n "$IPV4" ]; then 589 | echo -e "${GREEN}检测到 IPv4: $IPV4${NC}" 590 | else 591 | echo -e "${YELLOW}未检测到 IPv4${NC}" 592 | fi 593 | 594 | # 尝试获取 IPv6 595 | if [ "$FETCH_TOOL" == "curl" ]; then 596 | IPV6=$(curl -6s https://icanhazip.com 2>/dev/null || curl -6s http://ipinfo.io/ip 2>/dev/null || true) 597 | else 598 | IPV6=$(wget -6qO- https://icanhazip.com 2>/dev/null || wget -6qO- http://ipinfo.io/ip 2>/dev/null || true) 599 | fi 600 | 601 | if [ -n "$IPV6" ]; then 602 | echo -e "${GREEN}检测到 IPv6: $IPV6${NC}" 603 | else 604 | echo -e "${YELLOW}未检测到 IPv6${NC}" 605 | fi 606 | 607 | # 如果都没有获取到,报错 608 | if [ -z "$IPV4" ] && [ -z "$IPV6" ]; then 609 | echo -e "${RED}无法获取公网 IP 地址${NC}" 610 | exit 1 611 | fi 612 | } 613 | 614 | # 验证 IPv4 地址格式 615 | validate_ipv4() { 616 | local ip=$1 617 | if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then 618 | IFS='.' read -ra ADDR <<< "$ip" 619 | for i in "${ADDR[@]}"; do 620 | if [[ $i -gt 255 ]]; then 621 | return 1 622 | fi 623 | done 624 | return 0 625 | else 626 | return 1 627 | fi 628 | } 629 | 630 | # 验证 IPv6 地址格式 631 | validate_ipv6() { 632 | local ip=$1 633 | if [[ $ip =~ ^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$ ]]; then 634 | return 0 635 | else 636 | return 1 637 | fi 638 | } 639 | 640 | # 选择 IP 地址 641 | select_ip_address() { 642 | echo -e "${BLUE}选择用于 DNS 记录的 IP 地址:${NC}" 643 | 644 | OPTIONS=() 645 | if [ -n "$IPV4" ]; then 646 | OPTIONS+=("IPv4: $IPV4") 647 | fi 648 | if [ -n "$IPV6" ]; then 649 | OPTIONS+=("IPv6: $IPV6") 650 | fi 651 | OPTIONS+=("自定义 IP 地址") 652 | 653 | select opt in "${OPTIONS[@]}"; do 654 | case $REPLY in 655 | 1) 656 | if [ -n "$IPV4" ]; then 657 | SELECTED_IP=$IPV4 658 | echo -e "${GREEN}已选择 IPv4: $SELECTED_IP${NC}" 659 | break 660 | else 661 | echo -e "${RED}无效选项${NC}" 662 | fi 663 | ;; 664 | 2) 665 | if [ -n "$IPV6" ]; then 666 | SELECTED_IP=$IPV6 667 | echo -e "${GREEN}已选择 IPv6: $SELECTED_IP${NC}" 668 | break 669 | elif [ ${#OPTIONS[@]} -eq 2 ]; then 670 | # 只有 IPv4 和自定义选项 671 | while true; do 672 | read -p "请输入 IPv4 或 IPv6 地址: " CUSTOM_IP 673 | if validate_ipv4 "$CUSTOM_IP"; then 674 | SELECTED_IP=$CUSTOM_IP 675 | echo -e "${GREEN}已选择自定义 IPv4: $SELECTED_IP${NC}" 676 | break 2 677 | elif validate_ipv6 "$CUSTOM_IP"; then 678 | SELECTED_IP=$CUSTOM_IP 679 | echo -e "${GREEN}已选择自定义 IPv6: $SELECTED_IP${NC}" 680 | break 2 681 | else 682 | echo -e "${RED}IP 地址格式无效,请重新输入${NC}" 683 | fi 684 | done 685 | else 686 | echo -e "${RED}无效选项${NC}" 687 | fi 688 | ;; 689 | 3) 690 | if [ ${#OPTIONS[@]} -eq 3 ]; then 691 | while true; do 692 | read -p "请输入 IPv4 或 IPv6 地址: " CUSTOM_IP 693 | if validate_ipv4 "$CUSTOM_IP"; then 694 | SELECTED_IP=$CUSTOM_IP 695 | echo -e "${GREEN}已选择自定义 IPv4: $SELECTED_IP${NC}" 696 | break 2 697 | elif validate_ipv6 "$CUSTOM_IP"; then 698 | SELECTED_IP=$CUSTOM_IP 699 | echo -e "${GREEN}已选择自定义 IPv6: $SELECTED_IP${NC}" 700 | break 2 701 | else 702 | echo -e "${RED}IP 地址格式无效,请重新输入${NC}" 703 | fi 704 | done 705 | else 706 | echo -e "${RED}无效选项${NC}" 707 | fi 708 | ;; 709 | *) 710 | echo -e "${RED}无效选项${NC}" 711 | ;; 712 | esac 713 | done 714 | } 715 | 716 | # 获取 Cloudflare API 和域名信息 717 | get_cloudflare_info() { 718 | echo -e "${BLUE}请输入 Cloudflare API 信息:${NC}" 719 | 720 | while [ -z "$CF_API_TOKEN" ]; do 721 | read -p "API Token: " CF_API_TOKEN_INPUT 722 | if [ -z "$CF_API_TOKEN_INPUT" ]; then 723 | echo -e "${RED}API Token 不能为空${NC}" 724 | else 725 | CF_API_TOKEN="$CF_API_TOKEN_INPUT" 726 | fi 727 | done 728 | 729 | while [ -z "$DOMAIN_NAME" ]; do 730 | read -p "域名 (例如: hubproxy.example.com): " DOMAIN_NAME_INPUT 731 | if [ -z "$DOMAIN_NAME_INPUT" ]; then 732 | echo -e "${RED}域名不能为空${NC}" 733 | else 734 | DOMAIN_NAME="$DOMAIN_NAME_INPUT" 735 | fi 736 | done 737 | } 738 | 739 | # 获取 Cloudflare Zone ID 信息 740 | get_cloudflare_zone_id() { 741 | if [ "$FETCH_TOOL" == "curl" ]; then 742 | CF_ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=${DOMAIN_NAME#*.}" \ 743 | -H "Authorization: Bearer ${CF_API_TOKEN}" \ 744 | -H "Content-Type: application/json" | jq -r '.result[0].id') 745 | else 746 | CF_ZONE_ID=$(wget -qO - \ 747 | --header="Authorization: Bearer ${CF_API_TOKEN}" \ 748 | --header="Content-Type: application/json" \ 749 | "https://api.cloudflare.com/client/v4/zones?name=${DOMAIN_NAME#*.}" | jq -r '.result[0].id') 750 | fi 751 | } 752 | 753 | # 创建 DNS 记录(不启用代理) 754 | create_dns_record_without_proxy() { 755 | echo -e "${BLUE}正在创建 DNS 记录(不启用代理)...${NC}" 756 | 757 | # 判断是 A 记录还是 AAAA 记录 758 | RECORD_TYPE="A" 759 | if validate_ipv6 "$SELECTED_IP"; then 760 | RECORD_TYPE="AAAA" 761 | fi 762 | 763 | # 获取 Cloudflare 域名 ID 764 | grep -q '^$' <<< "${CF_ZONE_ID}" && get_cloudflare_zone_id 765 | 766 | # 首先检查是否已存在同名记录 767 | echo -e "${BLUE}检查是否已存在同名 DNS 记录...${NC}" 768 | if [ "$FETCH_TOOL" == "curl" ]; then 769 | LIST_RESPONSE=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records?name=${DOMAIN_NAME}" \ 770 | -H "Authorization: Bearer ${CF_API_TOKEN}" \ 771 | -H "Content-Type: application/json") 772 | else 773 | # 使用 wget 发送 GET 请求 774 | LIST_RESPONSE=$(wget -q \ 775 | --header="Authorization: Bearer ${CF_API_TOKEN}" \ 776 | --header="Content-Type: application/json" \ 777 | -O - \ 778 | "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records?name=${DOMAIN_NAME}") 779 | fi 780 | 781 | # 检查API响应是否有效 782 | if [ -z "$LIST_RESPONSE" ]; then 783 | echo -e "${RED}无法从Cloudflare API获取响应,请检查网络连接和API凭证${NC}" 784 | exit 1 785 | fi 786 | 787 | # 验证响应是否为有效的JSON 788 | if ! echo "$LIST_RESPONSE" | jq empty 2>/dev/null; then 789 | echo -e "${RED}收到无效的API响应${NC}" 790 | echo -e "${YELLOW}原始响应内容: $LIST_RESPONSE${NC}" 791 | exit 1 792 | fi 793 | 794 | # 检查是否有现有记录 795 | RECORD_COUNT=$(echo "$LIST_RESPONSE" | jq -r '.result|length') 796 | 797 | # 如果有现有记录,则更新第一条记录 798 | if [ "$RECORD_COUNT" -gt 0 ]; then 799 | echo -e "${YELLOW}检测到已存在 DNS 记录,将更新现有记录...${NC}" 800 | RECORD_ID=$(echo "$LIST_RESPONSE" | jq -r '.result[0].id') 801 | 802 | # 更新现有记录 803 | if [ "$FETCH_TOOL" == "curl" ]; then 804 | RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${RECORD_ID}" \ 805 | -H "Authorization: Bearer ${CF_API_TOKEN}" \ 806 | -H "Content-Type: application/json" \ 807 | --data '{ 808 | "type": "'${RECORD_TYPE}'", 809 | "name": "'${DOMAIN_NAME}'", 810 | "content": "'${SELECTED_IP}'", 811 | "ttl": 3600, 812 | "proxied": false, 813 | "comment": "hubproxy project" 814 | }') 815 | else 816 | # 使用 wget 发送 PUT 请求更新记录 817 | RESPONSE=$(wget --method=PUT \ 818 | --header="Authorization: Bearer ${CF_API_TOKEN}" \ 819 | --header="Content-Type: application/json" \ 820 | --body-data='{ 821 | "type": "'"${RECORD_TYPE}"'", 822 | "name": "'"${DOMAIN_NAME}"'", 823 | "content": "'"${SELECTED_IP}"'", 824 | "ttl": 3600, 825 | "proxied": false, 826 | "comment": "hubproxy project" 827 | }' \ 828 | -qO - \ 829 | "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${RECORD_ID}") 830 | fi 831 | else 832 | # 没有现有记录,创建新记录 833 | echo -e "${BLUE}未检测到现有 DNS 记录,将创建新记录...${NC}" 834 | if [ "$FETCH_TOOL" == "curl" ]; then 835 | RESPONSE=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records" \ 836 | -H "Authorization: Bearer ${CF_API_TOKEN}" \ 837 | -H "Content-Type: application/json" \ 838 | --data '{ 839 | "type": "'${RECORD_TYPE}'", 840 | "name": "'${DOMAIN_NAME}'", 841 | "content": "'${SELECTED_IP}'", 842 | "ttl": 3600, 843 | "proxied": false, 844 | "comment": "hubproxy project" 845 | }') 846 | else 847 | # 使用 wget 发送 POST 请求 848 | RESPONSE=$(wget -q \ 849 | --header="Authorization: Bearer ${CF_API_TOKEN}" \ 850 | --header="Content-Type: application/json" \ 851 | --post-data='{ 852 | "type": "'${RECORD_TYPE}'", 853 | "name": "'${DOMAIN_NAME}'", 854 | "content": "'${SELECTED_IP}'", 855 | "ttl": 3600, 856 | "proxied": false, 857 | "comment": "hubproxy project" 858 | }' \ 859 | -O - \ 860 | "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records") 861 | fi 862 | fi 863 | 864 | # 验证响应是否有效 865 | if [ -z "$RESPONSE" ]; then 866 | echo -e "${RED}API请求无响应,请检查网络连接和API凭证${NC}" 867 | exit 1 868 | fi 869 | 870 | # 验证响应是否为有效的JSON 871 | if ! echo "$RESPONSE" | jq empty 2>/dev/null; then 872 | echo -e "${RED}收到无效的API响应${NC}" 873 | echo -e "${YELLOW}原始响应内容: $RESPONSE${NC}" 874 | exit 1 875 | fi 876 | 877 | SUCCESS=$(echo "$RESPONSE" | jq -r '.success') 878 | if [ "$SUCCESS" = "true" ]; then 879 | RECORD_ID=$(echo "$RESPONSE" | jq -r '.result.id') 880 | echo -e "${GREEN}DNS 记录创建/更新成功,记录ID: $RECORD_ID${NC}" 881 | else 882 | ERRORS=$(echo "$RESPONSE" | jq -r '.errors[].message') 883 | echo -e "${RED}DNS 记录创建/更新失败: $ERRORS${NC}" 884 | exit 1 885 | fi 886 | } 887 | 888 | # 查找可用端口 889 | find_available_port() { 890 | echo -e "${BLUE}查找可用端口...${NC}" 891 | 892 | # 检查端口是否可用,返回0表示可用,返回1表示被占用,返回2表示端口不在有效范围内 893 | check_port() { 894 | local PORT=$1 895 | local NO_CHECK_USED=$2 896 | # 检查端口是否为数字且在有效范围内 897 | if ! [[ "$PORT" =~ ^[0-9]+$ ]] || [ "$PORT" -lt 1024 ] || [ "$PORT" -gt 65535 ]; then 898 | return 2 # 返回2表示端口不在有效范围内 899 | fi 900 | 901 | if ! grep -q 'no_check_used' <<< "$NO_CHECK_USED"; then 902 | # 检查端口是否被占用 903 | # 方法1: 使用 nc 命令 904 | if command -v nc >/dev/null 2>&1; then 905 | nc -z 127.0.0.1 "$PORT" >/dev/null 2>&1 906 | if [ $? -eq 0 ]; then 907 | return 1 # 返回1表示端口被占用 908 | fi 909 | # 方法2: 使用 lsof 命令 910 | elif command -v lsof >/dev/null 2>&1; then 911 | lsof -i:"$PORT" >/dev/null 2>&1 912 | if [ $? -eq 0 ]; then 913 | return 1 # 返回1表示端口被占用 914 | fi 915 | # 方法3: 使用 netstat 命令 916 | elif command -v netstat >/dev/null 2>&1; then 917 | netstat -nltup 2>/dev/null | grep -q ":$PORT " 918 | if [ $? -eq 0 ]; then 919 | return 1 # 返回1表示端口被占用 920 | fi 921 | # 方法4: 使用 ss 命令 922 | elif command -v ss >/dev/null 2>&1; then 923 | ss -nltup 2>/dev/null | grep -q ":$PORT " 924 | if [ $? -eq 0 ]; then 925 | return 1 # 返回1表示端口被占用 926 | fi 927 | # 方法5: 尝试使用/dev/tcp检查 928 | else 929 | (echo >/dev/tcp/127.0.0.1/"$PORT") >/dev/null 2>&1 930 | if [ $? -eq 0 ]; then 931 | return 1 # 返回1表示端口被占用 932 | fi 933 | fi 934 | 935 | return 0 # 返回0表示端口可用 936 | fi 937 | } 938 | 939 | # 首先检查默认端口 5000 是否可用 940 | check_port 5000 941 | local port_status=$? 942 | 943 | if [ $port_status -eq 1 ]; then 944 | echo -e "${YELLOW}默认端口 5000 已被占用,正在查找其他端口...${NC}" 945 | 946 | # 随机生成端口并检查是否可用 947 | for i in {1..50}; do 948 | PORT=$((RANDOM % (65535-1024) + 1024)) 949 | check_port $PORT "no_check_used" 950 | if [ $? -eq 0 ]; then 951 | HUBPROXY_PORT=$PORT 952 | echo -e "${GREEN}找到可用端口: $HUBPROXY_PORT${NC}" 953 | return 954 | fi 955 | done 956 | 957 | echo -e "${RED}无法找到可用端口${NC}" 958 | exit 1 959 | elif [ $port_status -eq 2 ]; then 960 | echo -e "${RED}端口 5000 不在有效范围内${NC}" 961 | exit 1 962 | else 963 | HUBPROXY_PORT=5000 964 | echo -e "${GREEN}使用默认端口: $HUBPROXY_PORT${NC}" 965 | fi 966 | } 967 | 968 | # 创建 Docker Compose 文件 969 | create_docker_compose_files() { 970 | echo -e "${BLUE}创建 Docker Compose 文件...${NC}" 971 | 972 | # 创建工作目录 973 | HUBPROXY_DIR="/root/hubproxy" 974 | mkdir -p "${HUBPROXY_DIR}"/conf 975 | mkdir -p "${HUBPROXY_DIR}"/site 976 | mkdir -p "${HUBPROXY_DIR}"/data/caddy/certificates/acme-v02.api.letsencrypt.org-directory 977 | mkdir -p "${HUBPROXY_DIR}"/config 978 | 979 | # 创建 Caddyfile 980 | cat > "${HUBPROXY_DIR}"/conf/Caddyfile << EOF 981 | http://${DOMAIN_NAME}, https://${DOMAIN_NAME} { 982 | reverse_proxy hubproxy:5000 { 983 | header_up X-Forwarded-For {http.request.header.CF-Connecting-IP} 984 | header_up X-Real-IP {http.request.header.CF-Connecting-IP} 985 | header_up X-Forwarded-Proto {scheme} 986 | header_up X-Forwarded-Host {host} 987 | } 988 | } 989 | EOF 990 | 991 | # 创建 docker-compose.yml 992 | cat > "${HUBPROXY_DIR}"/docker-compose.yml << EOF 993 | services: 994 | caddy: 995 | image: caddy:latest 996 | container_name: caddy 997 | restart: unless-stopped 998 | ports: 999 | - "80:80" 1000 | - "443:443" 1001 | - "443:443/udp" 1002 | volumes: 1003 | - ./conf/Caddyfile:/etc/caddy/Caddyfile 1004 | - ./site:/srv 1005 | - ./data:/data 1006 | - ./config:/config 1007 | networks: 1008 | - hubproxy_network 1009 | 1010 | hubproxy: 1011 | image: ghcr.io/sky22333/hubproxy 1012 | container_name: hubproxy 1013 | restart: always 1014 | expose: 1015 | - "5000" # 仅容器内可访问 1016 | environment: 1017 | - PORT=5000 1018 | networks: 1019 | - hubproxy_network 1020 | 1021 | volumes: 1022 | caddy_data: 1023 | caddy_config: 1024 | 1025 | networks: 1026 | hubproxy_network: 1027 | name: hubproxy_network 1028 | driver: bridge 1029 | EOF 1030 | 1031 | echo -e "${GREEN}Docker Compose 文件创建完成${NC}" 1032 | } 1033 | 1034 | # 启动 Docker Compose 1035 | start_docker_compose() { 1036 | echo -e "${BLUE}启动 Docker Compose...${NC}" 1037 | 1038 | HUBPROXY_DIR="/root/hubproxy" 1039 | cd "${HUBPROXY_DIR}" 1040 | 1041 | # 启动服务 1042 | docker compose up -d 1043 | 1044 | if [ $? -eq 0 ]; then 1045 | echo -e "${GREEN}Docker Compose 启动成功${NC}" 1046 | else 1047 | echo -e "${RED}Docker Compose 启动失败${NC}" 1048 | exit 1 1049 | fi 1050 | } 1051 | 1052 | # 等待 Caddy 生成证书 1053 | wait_for_caddy_certificates() { 1054 | echo -e "${BLUE}等待 Caddy 生成证书...${NC}" 1055 | echo -e "${YELLOW}这可能需要几分钟时间${NC}" 1056 | 1057 | # 等待最多100秒 1058 | for i in {1..5}; do 1059 | sleep 5 1060 | 1061 | # 检查证书是否存在 1062 | if [ -d "/root/hubproxy/data/caddy/certificates/acme-v02.api.letsencrypt.org-directory" ] && [ -n "$(find /root/hubproxy/data/caddy/certificates/acme-v02.api.letsencrypt.org-directory -name "*.crt" 2>/dev/null)" ]; then 1063 | echo -e "${GREEN}证书已生成${NC}" 1064 | return 0 1065 | fi 1066 | 1067 | echo -e "${BLUE}等待中... (${i}/5)${NC}" 1068 | done 1069 | 1070 | echo -e "${YELLOW}证书可能仍在生成中,继续执行下一步${NC}" 1071 | } 1072 | 1073 | # 卸载 VPS 方案 1074 | uninstall_vps() { 1075 | echo -e "${BLUE}开始卸载 HubProxy (VPS方案)...${NC}" 1076 | 1077 | # 停止并禁用 HubProxy 服务 1078 | if systemctl is-active --quiet ${SERVICE_NAME} 2>/dev/null; then 1079 | echo -e "${BLUE}停止 HubProxy 服务...${NC}" 1080 | systemctl stop ${SERVICE_NAME} 1081 | fi 1082 | 1083 | if systemctl is-enabled --quiet ${SERVICE_NAME} 2>/dev/null; then 1084 | echo -e "${BLUE}禁用 HubProxy 服务...${NC}" 1085 | systemctl disable ${SERVICE_NAME} 1086 | fi 1087 | 1088 | # 删除 systemd 服务文件 1089 | if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]; then 1090 | echo -e "${BLUE}删除 systemd 服务文件...${NC}" 1091 | rm -f "/etc/systemd/system/${SERVICE_NAME}.service" 1092 | systemctl daemon-reload 1093 | fi 1094 | 1095 | # 删除安装目录 1096 | if [ -d "${INSTALL_DIR}" ]; then 1097 | echo -e "${BLUE}删除安装目录...${NC}" 1098 | rm -rf "${INSTALL_DIR}" 1099 | fi 1100 | 1101 | # 删除日志目录 1102 | if [ -d "${LOG_DIR}" ]; then 1103 | echo -e "${BLUE}删除日志目录...${NC}" 1104 | rm -rf "${LOG_DIR}" 1105 | fi 1106 | 1107 | # 删除临时目录 1108 | if [ -d "${TEMP_DIR}" ]; then 1109 | echo -e "${BLUE}删除临时目录...${NC}" 1110 | rm -rf "${TEMP_DIR}" 1111 | fi 1112 | 1113 | # 询问是否卸载 Caddy 1114 | echo -e "${BLUE}是否卸载 Caddy? (y/N, 默认为N): ${NC}" 1115 | read -r UNINSTALL_CADDY 1116 | if [[ "${UNINSTALL_CADDY}" =~ ^[Yy]$ ]]; then 1117 | echo -e "${BLUE}卸载 Caddy...${NC}" 1118 | detect_os 1119 | 1120 | if [ "$OS" == "debian" ] || [ "$OS" == "ubuntu" ] || [ "$OS" == "raspbian" ]; then 1121 | apt remove -y caddy &>/dev/null 1122 | rm -f /usr/share/keyrings/caddy-stable-archive-keyring.gpg 1123 | rm -f /etc/apt/sources.list.d/caddy-stable.list 1124 | elif [ "$OS" == "fedora" ]; then 1125 | dnf remove -y caddy &>/dev/null 1126 | dnf copr disable -y @caddy/caddy &>/dev/null 1127 | elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then 1128 | if [ "$OS_VERSION" == "7" ]; then 1129 | yum remove -y caddy &>/dev/null 1130 | yum copr disable -y @caddy/caddy &>/dev/null 1131 | else 1132 | dnf remove -y caddy &>/dev/null 1133 | dnf copr disable -y @caddy/caddy &>/dev/null 1134 | fi 1135 | elif [ "$OS" == "arch" ] || [ "$OS" == "manjaro" ] || [ "$OS" == "parabola" ]; then 1136 | pacman -R --noconfirm caddy &>/dev/null 1137 | fi 1138 | 1139 | # 删除 Caddy 配置文件 1140 | rm -f "$CADDYFILE" 1141 | rm -rf /etc/caddy 1142 | rm -rf /var/lib/caddy 1143 | 1144 | echo -e "${GREEN}Caddy 卸载完成${NC}" 1145 | else 1146 | echo -e "${GREEN}跳过 Caddy 卸载${NC}" 1147 | fi 1148 | 1149 | echo -e "${GREEN}VPS 方案卸载完成${NC}" 1150 | } 1151 | 1152 | # 卸载 Docker 方案 1153 | uninstall_docker() { 1154 | echo -e "${BLUE}开始卸载 HubProxy (Docker方案)...${NC}" 1155 | 1156 | HUBPROXY_DIR="/root/hubproxy" 1157 | 1158 | # 检查是否存在 docker-compose.yml 文件 1159 | if [ -f "${HUBPROXY_DIR}/docker-compose.yml" ]; then 1160 | echo -e "${BLUE}停止并删除 Docker 容器...${NC}" 1161 | cd "${HUBPROXY_DIR}" 1162 | docker compose down 1163 | 1164 | # 删除所有相关镜像 1165 | echo -e "${BLUE}删除相关 Docker 镜像...${NC}" 1166 | docker rmi caddy:latest ghcr.io/sky22333/hubproxy 2>/dev/null || true 1167 | fi 1168 | 1169 | # 删除 hubproxy 网络(如果存在) 1170 | if docker network ls | grep -q hubproxy_network; then 1171 | echo -e "${BLUE}删除 hubproxy 网络...${NC}" 1172 | docker network rm hubproxy_network 2>/dev/null || true 1173 | fi 1174 | 1175 | # 删除映射目录 1176 | if [ -d "${HUBPROXY_DIR}" ]; then 1177 | echo -e "${BLUE}删除映射目录...${NC}" 1178 | rm -rf "${HUBPROXY_DIR}" 1179 | fi 1180 | 1181 | echo -e "${GREEN}Docker 方案卸载完成${NC}" 1182 | } 1183 | 1184 | # 配置 HubProxy 端口 1185 | configure_hubproxy_port() { 1186 | CONFIG_PATH="${INSTALL_DIR}/${CONFIG_FILE}" 1187 | if [ -f "$CONFIG_PATH" ]; then 1188 | echo -e "${BLUE}配置 HubProxy 端口为 $HUBPROXY_PORT${NC}" 1189 | sed -i "s/port = .*/port = $HUBPROXY_PORT/" "$CONFIG_PATH" 1190 | systemctl restart ${SERVICE_NAME} 1191 | echo -e "${GREEN}HubProxy 配置更新完成${NC}" 1192 | else 1193 | echo -e "${RED}未找到配置文件: $CONFIG_PATH${NC}" 1194 | exit 1 1195 | fi 1196 | } 1197 | 1198 | # 配置 Caddy 反向代理 1199 | configure_caddy_reverse_proxy() { 1200 | echo -e "${BLUE}配置 Caddy 反向代理...${NC}" 1201 | 1202 | # 检查 Caddyfile 是否存在 1203 | if [ ! -f "$CADDYFILE" ]; then 1204 | echo -e "${YELLOW}Caddyfile 不存在,创建新文件${NC}" 1205 | touch "$CADDYFILE" 1206 | fi 1207 | 1208 | # 添加反向代理配置 1209 | cat >> "$CADDYFILE" << EOF 1210 | 1211 | http://${DOMAIN_NAME}, https://${DOMAIN_NAME} { 1212 | reverse_proxy localhost:${HUBPROXY_PORT} { 1213 | header_up X-Forwarded-For {http.request.header.CF-Connecting-IP} 1214 | header_up X-Real-IP {http.request.header.CF-Connecting-IP} 1215 | header_up X-Forwarded-Proto {scheme} 1216 | header_up X-Forwarded-Host {host} 1217 | } 1218 | } 1219 | EOF 1220 | 1221 | # 格式化并重新加载 Caddy 配置 1222 | caddy fmt --overwrite "$CADDYFILE" &>/dev/null 1223 | caddy reload --config "$CADDYFILE" &>/dev/null 1224 | 1225 | echo -e "${GREEN}Caddy 反向代理配置完成${NC}" 1226 | } 1227 | 1228 | # 启用 Cloudflare 代理模式 1229 | enable_cloudflare_proxy() { 1230 | echo -e "${BLUE}启用 Cloudflare 代理模式...${NC}" 1231 | 1232 | # 获取 Cloudflare 域名 ID 1233 | grep -q '^$' <<< "${CF_ZONE_ID}" && get_cloudflare_zone_id 1234 | 1235 | # 获取 DNS 记录 ID 1236 | if [ "$FETCH_TOOL" == "curl" ]; then 1237 | RESPONSE=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records?name=${DOMAIN_NAME}" \ 1238 | -H "Authorization: Bearer ${CF_API_TOKEN}" \ 1239 | -H "Content-Type: application/json") 1240 | else 1241 | # 使用 wget 发送 GET 请求 1242 | RESPONSE=$(wget -q \ 1243 | --header="Authorization: Bearer ${CF_API_TOKEN}" \ 1244 | --header="Content-Type: application/json" \ 1245 | -O - \ 1246 | "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records?name=${DOMAIN_NAME}") 1247 | fi 1248 | 1249 | # 验证响应是否有效 1250 | if [ -z "$RESPONSE" ]; then 1251 | echo -e "${RED}API请求无响应,请检查网络连接和API凭证${NC}" 1252 | exit 1 1253 | fi 1254 | 1255 | # 验证响应是否为有效的JSON 1256 | if ! echo "$RESPONSE" | jq empty 2>/dev/null; then 1257 | echo -e "${RED}收到无效的API响应${NC}" 1258 | echo -e "${YELLOW}原始响应内容: $RESPONSE${NC}" 1259 | exit 1 1260 | fi 1261 | 1262 | # 解析JSON数据 1263 | RECORD_ID=$(echo "$RESPONSE" | jq -r '.result[0].id') 1264 | RECORD_TYPE=$(echo "$RESPONSE" | jq -r '.result[0].type') 1265 | RECORD_NAME=$(echo "$RESPONSE" | jq -r '.result[0].name') 1266 | RECORD_CONTENT=$(echo "$RESPONSE" | jq -r '.result[0].content') 1267 | RECORD_TTL=$(echo "$RESPONSE" | jq -r '.result[0].ttl') 1268 | 1269 | # 验证是否成功获取记录ID 1270 | if [ "$RECORD_ID" = "null" ] || [ -z "$RECORD_ID" ]; then 1271 | echo -e "${RED}无法获取 DNS 记录 ID${NC}" 1272 | echo -e "${YELLOW}原始响应内容: $RESPONSE${NC}" 1273 | exit 1 1274 | fi 1275 | 1276 | # 验证记录类型 1277 | if [ "$RECORD_TYPE" != "A" ] && [ "$RECORD_TYPE" != "AAAA" ]; then 1278 | echo -e "${RED}不支持的 DNS 记录类型: $RECORD_TYPE${NC}" 1279 | exit 1 1280 | fi 1281 | 1282 | # 启用代理模式 1283 | if [ "$FETCH_TOOL" == "curl" ]; then 1284 | RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${RECORD_ID}" \ 1285 | -H "Authorization: Bearer ${CF_API_TOKEN}" \ 1286 | -H "Content-Type: application/json" \ 1287 | --data '{ 1288 | "type": "'${RECORD_TYPE}'", 1289 | "name": "'${RECORD_NAME}'", 1290 | "content": "'${RECORD_CONTENT}'", 1291 | "ttl": '${RECORD_TTL}', 1292 | "proxied": true, 1293 | "comment": "hubproxy project" 1294 | }') 1295 | else 1296 | # 使用 wget 发送 PUT 请求 1297 | RESPONSE=$(wget --method=PUT \ 1298 | --header="Authorization: Bearer ${CF_API_TOKEN}" \ 1299 | --header="Content-Type: application/json" \ 1300 | --body-data='{ 1301 | "type": "'"${RECORD_TYPE}"'", 1302 | "name": "'"${DOMAIN_NAME}"'", 1303 | "content": "'"${SELECTED_IP}"'", 1304 | "ttl": 3600, 1305 | "proxied": true, 1306 | "comment": "hubproxy project" 1307 | }' \ 1308 | -qO - \ 1309 | "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${RECORD_ID}") 1310 | fi 1311 | 1312 | # 验证更新后的响应 1313 | if [ -z "$RESPONSE" ]; then 1314 | echo -e "${RED}API请求无响应,请检查网络连接和API凭证${NC}" 1315 | exit 1 1316 | fi 1317 | 1318 | if ! echo "$RESPONSE" | jq empty 2>/dev/null; then 1319 | echo -e "${RED}更新 DNS 记录失败,收到无效的API响应${NC}" 1320 | echo -e "${YELLOW}原始响应内容: $RESPONSE${NC}" 1321 | exit 1 1322 | fi 1323 | 1324 | SUCCESS=$(echo "$RESPONSE" | jq -r '.success') 1325 | if [ "$SUCCESS" = "true" ]; then 1326 | echo -e "${GREEN}Cloudflare 代理模式已启用${NC}" 1327 | else 1328 | ERRORS=$(echo "$RESPONSE" | jq -r '.errors[].message') 1329 | echo -e "${RED}启用 Cloudflare 代理模式失败: $ERRORS${NC}" 1330 | fi 1331 | } 1332 | 1333 | # 主程序执行流程 1334 | main() { 1335 | # 选择安装方案 1336 | select_installation_method 1337 | 1338 | if [ "$OPERATION" = "uninstall" ]; then 1339 | # 执行卸载操作 1340 | if [ "$INSTALL_TYPE" = "vps" ]; then 1341 | uninstall_vps 1342 | else 1343 | uninstall_docker 1344 | fi 1345 | exit 0 1346 | fi 1347 | 1348 | # 1. 安装系统依赖(包括 jq 等) 1349 | install_system_dependencies 1350 | 1351 | # 2. 获取 Cloudflare API 信息 1352 | get_cloudflare_info 1353 | 1354 | # 3. 获取 IP 地址 1355 | get_ip_addresses 1356 | 1357 | # 4. 选择 IP 地址 1358 | select_ip_address 1359 | 1360 | if [ "$INSTALL_TYPE" = "vps" ]; then 1361 | # VPS方案 1362 | # 5. 创建 DNS 记录(不启用代理) 1363 | create_dns_record_without_proxy 1364 | 1365 | # 6. 安装 Caddy(如果未安装) 1366 | install_caddy 1367 | 1368 | # 7. 安装 HubProxy 1369 | install_hubproxy 1370 | 1371 | # 8. 查找可用端口 1372 | find_available_port 1373 | 1374 | # 9. 配置 HubProxy 端口 1375 | configure_hubproxy_port 1376 | 1377 | # 10. 配置 Caddy 反向代理 1378 | configure_caddy_reverse_proxy 1379 | 1380 | # 11. 启用 Cloudflare 代理模式(在 Caddy 重新加载之后) 1381 | enable_cloudflare_proxy 1382 | else 1383 | # Docker方案 1384 | # 5. 安装 Docker(如果未安装) 1385 | install_docker 1386 | 1387 | # 6. 创建 DNS 记录(不启用代理) 1388 | create_dns_record_without_proxy 1389 | 1390 | # 7. 创建工作目录 1391 | create_docker_compose_files 1392 | 1393 | # 8. 启动 Docker Compose 1394 | start_docker_compose 1395 | 1396 | # 9. 等待 Caddy 生成证书 1397 | wait_for_caddy_certificates 1398 | 1399 | # 10. 启用 Cloudflare 代理模式 1400 | enable_cloudflare_proxy 1401 | fi 1402 | 1403 | echo -e "${GREEN}========================================${NC}" 1404 | echo -e "${GREEN}安装和配置已完成!${NC}" 1405 | echo -e "${GREEN}您的 HubProxy 已通过以下地址访问:${NC}" 1406 | echo -e "${GREEN}https://${DOMAIN_NAME}${NC}" 1407 | echo -e "${GREEN}========================================${NC}" 1408 | } 1409 | 1410 | # 执行主程序 1411 | main --------------------------------------------------------------------------------