├── .github └── workflows │ └── auto_close_issues.yaml ├── README.md ├── LICENSE └── generate_homeproxy_rules.sh /.github/workflows/auto_close_issues.yaml: -------------------------------------------------------------------------------- 1 | name: Close inactive issues 2 | on: 3 | schedule: 4 | - cron: "30 1 * * *" 5 | 6 | jobs: 7 | close-issues: 8 | runs-on: ubuntu-latest 9 | permissions: 10 | issues: write 11 | pull-requests: write 12 | steps: 13 | - uses: actions/stale@v5 14 | with: 15 | days-before-issue-stale: 3 16 | days-before-issue-close: 2 17 | stale-issue-label: "stale" 18 | stale-issue-message: "This issue is stale because it has been open for 3 days with no activity." 19 | close-issue-message: "This issue was closed because it has been inactive for 2 days since being marked as stale." 20 | days-before-pr-stale: -1 21 | days-before-pr-close: -1 22 | repo-token: ${{ secrets.GITHUB_TOKEN }} 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # homeproxy-autogen-configuration 2 | 3 | ## English 4 | 5 | An alternative way to generate your dedicated [HomeProxy](https://github.com/immortalwrt/homeproxy) configuration on ImmortalWrt/OpenWrt(23.05.x+) and simplify the major setup process significantly. 6 | 7 | ### Introduction 8 | 9 | * Save substantial time for repetitive chores in configuration; 10 | * Provide an alternative solution to solve the potential issues that homeproxy cannot start or be used normally due to incorrect operations and other reasons. 11 | 12 | 13 | 14 | ### Steps 15 | 16 | #### 1. Execute the Script 17 | 18 | Prepare the runtime environment necessary for the script execution via (Skip this step if it has already been prepared): 19 | 20 | ```bash 21 | opkg update 22 | opkg install bash jq curl 23 | ``` 24 | 25 | , then: 26 | 27 | * (Required) Customize your dedicated [rules.sh](https://gist.github.com/thisIsIan-W/2c582a751e56c1a3f36d9ce48e312b31) configuration by using Secret Gist or other private links; 28 | * Execute the given script on your ImmortalWrt/OpenWrt(23.05.x+) system (You will be asked for the URL of your dedicated 'rules.sh' script during the execution): 29 | 30 | ```bash 31 | bash -c "$(curl -fsSl https://raw.githubusercontent.com/thisIsIan-W/homeproxy-autogen-configuration/refs/heads/main/generate_homeproxy_rules.sh)" 32 | ``` 33 | 34 | * Done! 35 | 36 |
37 | 38 | #### 2. Manual Configuration 39 | 40 | **In your browser,** open the ImmortalWrt/OpenWrt admin panel. (**For a clean session, it's recommended to use an incognito/private window and refresh the page.)** 41 | 42 | * Client Settings 43 | * Routing Settings 44 | * Select one of the Default Outbound nodes 45 | * Save 46 | * Routing Nodes 47 | * Select the Outbound Node(s) 48 | * Save 49 | * DNS Settings 50 | * Modify the Default DNS Server 51 | * Save 52 | * DNS Servers (Optional) 53 | * Manually select the `Address Resolver` for each entry (i.e., which DNS server to use for resolving the current DNS server's URL/hostname) 54 | * Save 55 | * DNS Rules (Optional) 56 | * Manually select the `Server` for each entry (i.e., which DNS server to use for resolving all domains under the current DNS rule) 57 | * Save 58 | * Save & Apply 59 | 60 | All done! 61 | 62 |
63 | 64 | --- 65 | 66 |
67 | 68 | ## 中文 69 | 70 | 一个更方便地生成 ImmortalWrt/OpenWrt(23.05.x+) [HomeProxy](https://github.com/immortalwrt/homeproxy) 插件大多数常用配置的脚本。 71 | 72 | ### 简介 73 | 74 | * 节省大量重复配置所需的时间; 75 | * 提供替代方案,解决由于潜在的配置不当等原因导致 HomeProxy 无法启动或正常使用的问题。 76 | 77 | 78 | 79 | ### 使用步骤 80 | 81 | #### 1. 执行脚本 82 | 83 | 准备脚本运行时所需环境(如已准备,则跳过此步): 84 | 85 | ```bash 86 | opkg update 87 | opkg install bash jq curl 88 | ``` 89 | 90 | ,然后: 91 | 92 | * (必备) 通过私密 Gist 或其它可被正常访问的私有链接定制你的专属 [rules.sh](https://gist.github.com/thisIsIan-W/2c582a751e56c1a3f36d9ce48e312b31) 配置内容; 93 | * 执行以下命令(脚本执行期间会向你索要你的定制配置URL): 94 | 95 | ```bash 96 | bash -c "$(curl -fsSl https://raw.githubusercontent.com/thisIsIan-W/homeproxy-autogen-configuration/refs/heads/main/generate_homeproxy_rules.sh)" 97 | ``` 98 | 99 | * 完成! 100 | 101 |
102 | 103 | #### 2. 界面操作 104 | 105 | 回到浏览器 (推荐使用无痕标签页重新登陆后台,并刷新 HomeProxy 界面): 106 | 107 | * 客户端设置 108 | * 路由设置 109 | * 默认出站 110 | * 若 `rules.sh` 的规则集仅匹配直连其余全部走代理,选择`你自定义的某个代理节点` 111 | * 若 `rules.sh` 的规则集仅匹配代理其余全部走直连,选择`直连` 112 | * 保存 113 | * 路由节点 114 | * 选择出站节点 115 | * 保存 116 | * DNS 设置 117 | * 修改默认 DNS 服务器 118 | * 若 `rules.sh` 的规则集匹配直连其余全部走代理,选择`境外 DNS 服务器` 119 | * 若 `rules.sh` 的规则集匹配代理其余全部走直连,选择`国内 DNS 服务器` 120 | * 保存 121 | * DNS 服务器 (可选) 122 | * 手动为每一个条目选择 `地址解析器`,也就是用哪个 DNS 服务器解析当前 DNS 服务器 URL 123 | * 保存 124 | * DNS 规则 (可选) 125 | * 手动为每一个条目选择 `服务器`,也就是用哪个 DNS 服务器解析当前 DNS 规则下的所有域名 126 | * 保存 127 | * 保存并应用 128 | 129 | 结束! 130 | 131 | 132 | 133 | 更多细节,请参阅 [说明书](https://thisisian-w.github.io/2024/10/30/homeproxy-one-click-configure-scripts) 。 134 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. -------------------------------------------------------------------------------- /generate_homeproxy_rules.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # SPDX-License-Identifier: GPL-2.0-only 3 | # 4 | # Copyright (C) 2025 thisIsIan-W 5 | 6 | TARGET_HOMEPROXY_CONFIG_PATH="/etc/config/homeproxy" 7 | 8 | to_upper() { 9 | echo -e "$1" | tr "[a-z]" "[A-Z]" 10 | } 11 | 12 | log_error() { 13 | echo -e "\e[31mERROR: ${1}\e[0m" 14 | } 15 | 16 | log_warn() { 17 | echo -e "\e[33mWARNING: ${1}\e[0m" 18 | } 19 | 20 | match_node() { 21 | local -n array_to_match=$1 22 | local key=$2 23 | local tmp_key_to_match 24 | properly_matched_node_name="" 25 | for tmp_key_to_match in "${array_to_match[@]}"; do 26 | if [[ "${key^^}" == "${tmp_key_to_match^^}"* ]]; then 27 | properly_matched_node_name="$tmp_key_to_match" 28 | break 29 | fi 30 | done 31 | } 32 | 33 | config_map() { 34 | local -n array_ref=$1 35 | local -n map_ref=$2 36 | local -n map_order_ref=$3 37 | local entry 38 | 39 | for entry in "${array_ref[@]}"; do 40 | if [[ "$entry" == *"|"* ]]; then 41 | local key="${entry%%|*}" 42 | local values="${entry#*|}" 43 | map_order_ref+=("$key") 44 | IFS=$'\n' read -r -d '' -a urls <<<"$values" 45 | [ "${#urls[@]}" -le 0 ] && echo "WARN: The tag [${key}] is invalid and will be skipped..." && continue 46 | map_ref["$key"]="${urls[*]}" 47 | fi 48 | done 49 | } 50 | 51 | fetch_homeproxy_config_file() { 52 | echo -n "------ Fetching the original homeproxy file from GitHub......" 53 | local download_count=0 54 | [ -n "$GLOBAL_GITHUB_PROXY_URL" ] && HOMEPROXY_CONFIG_URL="$GLOBAL_GITHUB_PROXY_URL/$HOMEPROXY_CONFIG_URL" 55 | while true; do 56 | ((download_count++)) 57 | 58 | if [ "$download_count" -gt 5 ]; then 59 | echo "" 60 | log_error "Please check the network connection. The file download link is: $HOMEPROXY_CONFIG_URL" 61 | log_error "Failed to fetch the homeproxy file from GitHub. Exiting..." 62 | exit 1 63 | fi 64 | 65 | wget -qO "/tmp/homeproxy" "$HOMEPROXY_CONFIG_URL" 66 | if [ $? -ne 0 ]; then 67 | log_warn "Operation failed, will try again after 2 seconds!(total times: 5)......" 68 | sleep 2 69 | else 70 | mv /tmp/homeproxy "$TARGET_HOMEPROXY_CONFIG_PATH" 71 | chmod +x "$TARGET_HOMEPROXY_CONFIG_PATH" 72 | break 73 | fi 74 | done 75 | echo "done!" 76 | } 77 | 78 | subscribe() { 79 | if [ -z "${SUBSCRIPTION_URLS+x}" ] || [ ${#SUBSCRIPTION_URLS[@]} -eq 0 ]; then 80 | return 0 81 | fi 82 | 83 | local hp_log_file="/var/run/$UCI_GLOBAL_CONFIG/$UCI_GLOBAL_CONFIG.log" 84 | [ -f "$hp_log_file" ] && > "$hp_log_file" 85 | 86 | echo -n "------ Updating subscriptions (May take some time, please wait) ......" 87 | for sub_url in ${SUBSCRIPTION_URLS[@]}; do 88 | uci add_list $UCI_GLOBAL_CONFIG.subscription.subscription_url=$sub_url 89 | done 90 | 91 | if [ -n "${SUBSCRIPTION_USER_AGENT:-}" ]; then 92 | uci set $UCI_GLOBAL_CONFIG.subscription.user_agent="$SUBSCRIPTION_USER_AGENT" 93 | fi 94 | uci commit $UCI_GLOBAL_CONFIG 95 | 96 | # Execute the subscription logics. 97 | local update_subscription_file_path="/etc/homeproxy/scripts/update_subscriptions.uc" 98 | if [ ! -f "${update_subscription_file_path}" ] || [ ! -x "${update_subscription_file_path}" ]; then 99 | log_warn "An issue has been detected within your homeproxy files. Reinstall the application and subsequently attempt this option again!" 100 | return 0 101 | fi 102 | 103 | "${update_subscription_file_path}" 2>/dev/null 104 | if grep -Eiq 'unsupported|Failed to fetch|Error|FATAL' "$hp_log_file"; then 105 | echo "" 106 | grep -Ei 'unsupported|Failed to fetch|Error|FATAL' "$hp_log_file" | while IFS= read -r line; do 107 | log_warn "$line" 108 | done 109 | echo "" 110 | else 111 | echo "done!" 112 | fi 113 | } 114 | 115 | get_last_dns_server() { 116 | local last_dns_server_name="${DNS_SERVERS_MAP_KEY_ORDER_ARRAY[-1]}" 117 | local last_dns_servers_array=(${DNS_SERVERS_MAP[$last_dns_server_name]}) 118 | local last_dns_server_element_count=${#last_dns_servers_array[@]} 119 | if [ "$last_dns_server_element_count" -eq 1 ]; then 120 | echo -e "$last_dns_server_name" 121 | else 122 | echo -e "${last_dns_server_name}_1" 123 | fi 124 | } 125 | 126 | gen_public_config() { 127 | echo -n "------ Generating public config......" 128 | local output_msg=$(uci get $UCI_GLOBAL_CONFIG.config 2>&1) 129 | if [[ "$output_msg" != *"Entry not found"* ]]; then 130 | uci delete $UCI_GLOBAL_CONFIG.config 131 | fi 132 | 133 | # Default configuration 134 | uci -q batch <<-EOF >"/dev/null" 135 | set $UCI_GLOBAL_CONFIG.routing.default_outbound=$DEFAULT_GLOBAL_OUTBOUND 136 | set $UCI_GLOBAL_CONFIG.routing.sniff_override='0' 137 | set $UCI_GLOBAL_CONFIG.routing.udp_timeout='300' 138 | set $UCI_GLOBAL_CONFIG.routing.bypass_cn_traffic='0' 139 | set $UCI_GLOBAL_CONFIG.routing.domain_strategy='ipv4_only' 140 | 141 | set $UCI_GLOBAL_CONFIG.config=$UCI_GLOBAL_CONFIG 142 | set $UCI_GLOBAL_CONFIG.config.routing_mode='custom' 143 | set $UCI_GLOBAL_CONFIG.config.routing_port='common' 144 | set $UCI_GLOBAL_CONFIG.config.proxy_mode='redirect_tproxy' 145 | set $UCI_GLOBAL_CONFIG.config.ipv6_support='1' 146 | 147 | del $UCI_GLOBAL_CONFIG.nodes_domain 148 | del $UCI_GLOBAL_CONFIG.dns 149 | del $UCI_GLOBAL_CONFIG.control.wan_proxy_ipv6_ips 150 | 151 | set $UCI_GLOBAL_CONFIG.dns=$UCI_GLOBAL_CONFIG 152 | set $UCI_GLOBAL_CONFIG.dns.dns_strategy='ipv4_only' 153 | set $UCI_GLOBAL_CONFIG.dns.default_strategy='ipv4_only' 154 | set $UCI_GLOBAL_CONFIG.dns.disable_cache='1' 155 | set $UCI_GLOBAL_CONFIG.dns.default_server=dns_server_$(get_last_dns_server) 156 | 157 | set $UCI_GLOBAL_CONFIG.dns_rule_any='dns_rule' 158 | set $UCI_GLOBAL_CONFIG.dns_rule_any.label='dns_rule_any' 159 | set $UCI_GLOBAL_CONFIG.dns_rule_any.enabled='1' 160 | set $UCI_GLOBAL_CONFIG.dns_rule_any.mode='default' 161 | set $UCI_GLOBAL_CONFIG.dns_rule_any.server='default-dns' 162 | add_list $UCI_GLOBAL_CONFIG.dns_rule_any.outbound='any-out' 163 | EOF 164 | 165 | uci commit $UCI_GLOBAL_CONFIG 166 | echo "done!" 167 | } 168 | 169 | gen_rule_sets_config() { 170 | for key in ${RULESET_MAP_KEY_ORDER_ARRAY[@]}; do 171 | RULESET_CONFIG_KEY_ORDER_ARRAY+=("$key") 172 | for url in ${RULESET_MAP[$key]}; do 173 | local file_type=0 174 | # A Linux-based absolute path. 175 | [[ -f "$url" && -s "$url" && ("$url" == *.srs || "$url" == *.json) ]] && file_type=1 176 | # A valid link 177 | [[ "$url" =~ ^(https?):// && ( "$url" =~ \.srs$ || "$url" =~ \.json$ ) ]] && file_type=2 178 | [ "$file_type" -eq 0 ] && echo -e "\n\e[33mWARN: [$url] is invalid, skipping this rule!\e[0m" && continue 179 | 180 | local tmp_rule_name=$(basename "$url") 181 | local rule_name="${tmp_rule_name%.*}" 182 | local rule_name_suffix="${tmp_rule_name##*.}" 183 | 184 | # Note that the character '-' should not be placed in the middle 185 | echo "$rule_name" | grep -q '[-.*#@!&]' && rule_name=$(echo "$rule_name" | sed 's/[-.*#@!&]/_/g') 186 | 187 | case "$url" in 188 | *"geoip"*) [[ ! "$rule_name" == *"geoip"* ]] && rule_name="geoip_${rule_name}" ;; 189 | *"ip"*) [[ ! "$rule_name" == *"ip"* ]] && rule_name="geoip_${rule_name}" ;; 190 | *"geosite"*) [[ ! "$rule_name" == *"geosite"* ]] && rule_name="geosite_${rule_name}" ;; 191 | esac 192 | 193 | [ -n "${RULESET_CONFIG_MAP["$key"]}" ] && { 194 | RULESET_CONFIG_MAP["$key"]="${RULESET_CONFIG_MAP["$key"]},$rule_name" 195 | } || { 196 | RULESET_CONFIG_MAP["$key"]="$rule_name" 197 | } 198 | 199 | { 200 | printf "config ruleset '%s'\n" "$rule_name" 201 | printf " option label '%s'\n" "$rule_name" 202 | printf " option enabled '1'\n" 203 | if [ "$file_type" -eq 1 ]; then 204 | printf " option type 'local'\n" 205 | printf " option path '%s'\n" "$url" 206 | else 207 | printf " option type 'remote'\n" 208 | printf " option update_interval '24h'\n" 209 | [ -n "$GLOBAL_GITHUB_PROXY_URL" ] \ 210 | && printf " option url '%s/%s'\n" "$GLOBAL_GITHUB_PROXY_URL" "$url" \ 211 | || printf " option url '%s'\n" "$url" 212 | fi 213 | 214 | local extension="${tmp_rule_name##*.}" 215 | [ "$extension" = "srs" ] && { 216 | printf " option format 'binary'\n\n" 217 | } || { 218 | printf " option format 'source'\n\n" 219 | } 220 | } >>"$TARGET_HOMEPROXY_CONFIG_PATH" 221 | done 222 | done 223 | } 224 | 225 | gen_dns_server_config() { 226 | for dns_key in "${DNS_SERVERS_MAP_KEY_ORDER_ARRAY[@]}"; do 227 | local server_url_count=1 228 | local dns_servers_array=(${DNS_SERVERS_MAP[$dns_key]}) 229 | local dns_server_element_count=${#dns_servers_array[@]} 230 | 231 | for server_url in ${DNS_SERVERS_MAP[$dns_key]}; do 232 | local dns_server_name 233 | [ "$dns_server_element_count" -eq 1 ] && dns_server_name="${dns_key}" || dns_server_name="${dns_key}_${server_url_count}" 234 | 235 | { 236 | printf "config dns_server 'dns_server_%s'\n" "$dns_server_name" 237 | printf " option label 'dns_server_%s'\n" "$dns_server_name" 238 | printf " option address '%s'\n" "$server_url" 239 | printf " option address_resolver 'default-dns'\n" 240 | printf " option address_strategy 'ipv4_only'\n" 241 | printf " option resolve_strategy 'ipv4_only'\n" 242 | printf " option enabled '1'\n" 243 | 244 | match_node RULESET_CONFIG_KEY_ORDER_ARRAY "$dns_key" 245 | if [ -n "$properly_matched_node_name" ]; then 246 | printf " option outbound '%s'\n\n" "routing_node_$properly_matched_node_name" 247 | else 248 | printf " option outbound '%s'\n\n" "$DEFAULT_GLOBAL_OUTBOUND" 249 | fi 250 | } >>"$TARGET_HOMEPROXY_CONFIG_PATH" 251 | ((server_url_count++)) 252 | done 253 | done 254 | } 255 | 256 | CONFIG_TYPES=("dns|dns_rule" "outbound|routing_rule") 257 | 258 | match_server_for_dns_rule() { 259 | # Inspect the provided dns rule and look for an appropriate dns server that corresponds to it. 260 | local template=$1 261 | local dns_key_array=("$2") 262 | 263 | if [ "$2" = "direct_out" ]; then 264 | template+=" 265 | option server 'default-dns'" 266 | echo -e "$template" 267 | return 0 268 | fi 269 | 270 | local matched_dns_server_name="" 271 | for tmp_dns_server_name in "${DNS_SERVERS_MAP_KEY_ORDER_ARRAY[@]}"; do 272 | properly_matched_node_name="" 273 | match_node dns_key_array "$tmp_dns_server_name" 274 | [ -n "$properly_matched_node_name" ] && matched_dns_server_name="$tmp_dns_server_name" && break 275 | done 276 | 277 | if [ -n "$matched_dns_server_name" ]; then 278 | template+=" 279 | option server 'dns_server_${matched_dns_server_name}'" 280 | echo -e "$template" 281 | return 0 282 | fi 283 | 284 | if [ "$2" != "reject_out" ]; then 285 | local last_dns_server=$(get_last_dns_server) 286 | template+=" 287 | option server 'dns_server_$last_dns_server'" 288 | fi 289 | echo -e "$template" 290 | } 291 | 292 | gen_rules_config() { 293 | local config_type="$1" 294 | local keyword 295 | for entry in "${CONFIG_TYPES[@]}"; do 296 | config_type_key="${entry%%|*}" 297 | [ "$config_type" = "$config_type_key" ] && keyword="${entry##*|}" && break 298 | done 299 | 300 | for key in ${RULESET_CONFIG_KEY_ORDER_ARRAY[@]}; do 301 | for value in ${RULESET_CONFIG_MAP[$key]}; do 302 | IFS=',' read -ra config_values <<<"${RULESET_CONFIG_MAP["$key"]}" 303 | local template="config ${keyword} '${keyword}_${key}' 304 | option label '${keyword}_${key}' 305 | option enabled '1' 306 | option mode 'default'" 307 | 308 | if [ "$config_type" = "outbound" ]; then 309 | template+=" 310 | option source_ip_is_private '0' 311 | option ip_is_private '0' 312 | option rule_set_ipcidr_match_source '0'" 313 | if [ "$key" = "direct_out" ]; then 314 | template+=" 315 | option outbound 'direct-out'" 316 | else 317 | template+=" 318 | option outbound 'routing_node_${key}'" 319 | fi 320 | fi 321 | 322 | if [ "$key" = "reject_out" ]; then 323 | template="config ${keyword} '${keyword}_${key}_blocked' 324 | option label '${keyword}_${key}_blocked' 325 | option enabled '1' 326 | option mode 'default'" 327 | if [ "$config_type" = "outbound" ]; then 328 | template+=" 329 | option outbound 'block-out' 330 | option rule_set_ipcidr_match_source '0' 331 | option source_ip_is_private '0' 332 | option ip_is_private '0'" 333 | else 334 | template+=" 335 | option server 'block-dns'" 336 | fi 337 | fi 338 | 339 | if [ "$config_type" = "dns" ]; then 340 | local rulesets_count=0 341 | for value in "${config_values[@]}"; do 342 | grep -q "geoip" <<<"$value" || grep -q "ip" <<<"$value" && ((rulesets_count++)) 343 | done 344 | # Bypass if the ruleset has only one URL, and that URL is an IP-based ruleset. 345 | [ "$rulesets_count" -eq ${#config_values[@]} ] && continue 346 | template=$(match_server_for_dns_rule "$template" "$key") 347 | fi 348 | 349 | printf "%s\n" "$template" >>"$TARGET_HOMEPROXY_CONFIG_PATH" 350 | for value in "${config_values[@]}"; do 351 | [ "$config_type" = "dns" ] && { grep -q "geoip" <<<"$value" || grep -q "ip" <<<"$value"; } && continue 352 | printf " list rule_set '%s'\n" "$value" >>"$TARGET_HOMEPROXY_CONFIG_PATH" 353 | done 354 | printf "\n" >>"$TARGET_HOMEPROXY_CONFIG_PATH" 355 | done 356 | done 357 | } 358 | 359 | gen_routing_nodes_config() { 360 | for key in ${RULESET_CONFIG_KEY_ORDER_ARRAY[@]}; do 361 | if [ "$key" = "reject_out" ] || [ "$key" = "direct_out" ]; then 362 | continue 363 | fi 364 | { 365 | printf "config routing_node 'routing_node_%s'\n" "$key" 366 | printf " option label routing_node_%s\n" "$key" 367 | printf " option enabled '1'\n" 368 | printf " option domain_strategy 'ipv4_only'\n" 369 | printf " option node 'node_%s_outbound_nodes'\n\n" "$key" 370 | } >>"$TARGET_HOMEPROXY_CONFIG_PATH" 371 | done 372 | } 373 | 374 | upgrade_sing_box_core() { 375 | [ "$UPGRADE_SING_BOX_VERSION" != "y" ] && return 0 376 | 377 | echo -n "------ Upgrading sing-box to the latest version (This may take some time, please wait)......" 378 | local arch=$(uname -m) 379 | case "$arch" in 380 | x86_64) arch="amd64" ;; 381 | aarch64 | arm64) arch="arm64" ;; 382 | armv7l | armv7) arch="armv7" ;; 383 | armv6l | armv6) arch="armv6" ;; 384 | i386 | i686) arch="386" ;; 385 | *) echo "" && log_warn "The ${arch} architecture is unsupported, bypassing..." ; return 1 ;; 386 | esac 387 | 388 | local response=$(curl -fs --max-time 5 "$SING_BOX_API_URL") 389 | local failed_msg="Failed to establish connection with the remote repository, bypassing..." 390 | if [ -z "$response" ]; then 391 | response=$(curl -fs --max-time 5 "$SING_BOX_MIRROR_API_URL") 392 | [ -z "$response" ] && echo "" && log_warn "$failed_msg" && return 1 393 | fi 394 | 395 | local latest_tag=$(echo "$response" | jq -r '.[].name' | head -n 1) 396 | [ -z "$latest_tag" ] && echo -e "$failed_msg" && return 1 397 | local current_version=$(sing-box version | awk 'NR==1 {print $3}') 398 | [[ "$current_version" == "${latest_tag#v}" ]] && echo -en "\n\e[32mYour sing-box version is up-to-date --> $latest_tag!\e[0m" && return 0 399 | 400 | local full_link="https://github.com/$SING_BOX_REPO/releases/download/$latest_tag/sing-box-${latest_tag#v}-linux-$arch.tar.gz" 401 | [ -n "$GLOBAL_GITHUB_PROXY_URL" ] && full_link="$GLOBAL_GITHUB_PROXY_URL/https://github.com/$SING_BOX_REPO/releases/download/$latest_tag/sing-box-${latest_tag#v}-linux-$arch.tar.gz" 402 | local file_name=$(basename "$full_link") 403 | curl -fsSl -o "$file_name" "$full_link" 404 | [ $? -ne 0 ] && return 1 405 | 406 | tar -zxf "$file_name" 407 | cp "${file_name%.tar.gz}"/sing-box /usr/bin/sing-box 408 | rm -rf "${file_name%.tar.gz}" 409 | rm "$file_name" 410 | chmod +x /usr/bin/sing-box 411 | 412 | echo -e "done! \e[32mSing-box is upgraded to $latest_tag.\e[0m" 413 | } 414 | 415 | eval_dedicated_configuration() { 416 | DECICATED_RULES=$(curl -fsSl -H "Cache-Control: no-cache" --max-time 8 "$DEDICATED_RULES_LINK") 417 | if [ $? -ne 0 ]; then 418 | log_error "Failed to fetch the configuration from $DEDICATED_RULES_LINK." 419 | log_error "This might be due to network issues. Please check your network connection, firewall settings, and ensure that the URL is accessible." 420 | exit 1 421 | fi 422 | eval "$DECICATED_RULES" 423 | 424 | if [[ -z "${RULESET_URLS+x}" ]] || [[ "${#RULESET_URLS[@]}" -le 0 ]]; then 425 | log_error "The RULESET_URLS array wasn't found in the specified configuration, or it contains no valid elements at all. Please review your configuration!" 426 | log_error "Exiting..." 427 | exit 1 428 | fi 429 | if [[ -z "${DNS_SERVERS+x}" ]] || [[ "${#DNS_SERVERS[@]}" -le 0 ]]; then 430 | log_error "The DNS_SERVERS array wasn't found in the specified configuration, or it contains no valid elements at all. Please review your configuration!" 431 | log_error "Exiting..." 432 | exit 1 433 | fi 434 | } 435 | 436 | gen_homeproxy_config() { 437 | eval_dedicated_configuration 438 | 439 | config_map RULESET_URLS RULESET_MAP RULESET_MAP_KEY_ORDER_ARRAY 440 | config_map DNS_SERVERS DNS_SERVERS_MAP DNS_SERVERS_MAP_KEY_ORDER_ARRAY 441 | 442 | upgrade_sing_box_core 443 | echo "" 444 | 445 | if [ -f "$TARGET_HOMEPROXY_CONFIG_PATH" ]; then 446 | mv "$TARGET_HOMEPROXY_CONFIG_PATH" "$TARGET_HOMEPROXY_CONFIG_PATH.bak" 447 | echo -e "------ The file '$TARGET_HOMEPROXY_CONFIG_PATH' is backed up to ---> \e[32m$TARGET_HOMEPROXY_CONFIG_PATH.bak\e[0m" 448 | fi 449 | 450 | # Use the standard homeproxy configuration template as a guarantee. 451 | fetch_homeproxy_config_file 452 | # Pull all nodes from the subscription servers if specified. 453 | subscribe 454 | gen_public_config 455 | 456 | echo -n "------ Configuring rule sets..." 457 | gen_rule_sets_config 458 | echo "done!" 459 | 460 | echo -n "------ Configuring DNS servers..." 461 | gen_dns_server_config 462 | echo "done!" 463 | 464 | echo -n "------ Configuring DNS rules and routing rules..." 465 | gen_rules_config "dns" 466 | gen_rules_config "outbound" 467 | echo "done!" 468 | 469 | echo -n "------ Configuring routing nodes..." 470 | gen_routing_nodes_config 471 | echo "done!" 472 | 473 | local lan_ipv4_addr 474 | lan_ipv4_addr=$(ubus call network.interface.lan status | grep '\"address\"\: \"' | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' || true) 475 | [ -n "$lan_ipv4_addr" ] && { 476 | echo -e "\nThe execution of the script has finished ---> https://$lan_ipv4_addr/cgi-bin/luci/admin/services/homeproxy\n" 477 | } || echo -e "\nThe execution of the script has finished!\n" 478 | } 479 | 480 | entrance() { 481 | echo "" 482 | echo -e "\e[32m" 483 | echo "" 484 | echo "_______ _ _ _ ______ " 485 | echo "|__ __| | | (_) | | ____| " 486 | echo " | | __ _| | _____ _| |_ | |__ __ _ ___ _ _ " 487 | echo " | |/ _ | |/ / _ \ | | __| | __| / _ / __| | | | " 488 | echo " | | (_| | < __/ | | |_ | |___| (_| \__ \ |_| | _ " 489 | echo " |_|\__,_|_|\_\___| |_|\__| |______\__,_|___/\__, | (_)" 490 | echo " __/ | " 491 | echo " |___/ " 492 | echo "" 493 | echo -e "\e[0m" 494 | echo "" 495 | log_warn "Please make sure that you have backed up the /etc/config/homeproxy file in advance!" 496 | log_warn "Running the script will overwrite the backup file from the previous execution." 497 | echo "" 498 | echo "" 499 | read -p "Please provide the URL to your dedicated configuration file: " DEDICATED_RULES_LINK 500 | if [ -z "$DEDICATED_RULES_LINK" ]; then 501 | log_error "The required configuration URL is missing. The script cannot proceed without it, and will now exit." 502 | exit 1 503 | fi 504 | read -p "Would you like to upgrade the sing-box to the latest version? (y/n): " UPGRADE_SING_BOX_VERSION 505 | echo "" 506 | 507 | UPGRADE_SING_BOX_VERSION=$( [ "$UPGRADE_SING_BOX_VERSION" == "y" ] && echo "$UPGRADE_SING_BOX_VERSION" || echo "n" ) 508 | } 509 | 510 | declare -A RULESET_MAP 511 | declare -a RULESET_MAP_KEY_ORDER_ARRAY 512 | declare -A DNS_SERVERS_MAP 513 | declare -a DNS_SERVERS_MAP_KEY_ORDER_ARRAY 514 | declare -A DNS_SERVER_NAMES_MAP 515 | declare -A RULESET_CONFIG_MAP 516 | declare -a RULESET_CONFIG_KEY_ORDER_ARRAY 517 | 518 | DEFAULT_GLOBAL_OUTBOUND="direct-out" 519 | UPGRADE_SING_BOX_VERSION="y" 520 | DEDICATED_RULES_LINK="" 521 | DECICATED_RULES="" 522 | 523 | UCI_GLOBAL_CONFIG="homeproxy" 524 | HOMEPROXY_CONFIG_URL="https://raw.githubusercontent.com/immortalwrt/homeproxy/master/root/etc/config/homeproxy" 525 | SING_BOX_REPO="SagerNet/sing-box" 526 | SING_BOX_API_URL="https://api.github.com/repos/$SING_BOX_REPO/tags" 527 | SING_BOX_MIRROR_API_URL="https://gh-api.p3terx.com/repos/$SING_BOX_REPO/tags" 528 | 529 | entrance 530 | gen_homeproxy_config --------------------------------------------------------------------------------