├── .github ├── dependabot.yml └── workflows │ ├── image.yaml │ └── realease.yml ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── cert └── cert.go ├── config.json ├── config ├── handler.go ├── load.go └── prepare.go ├── docker-compose.yml ├── go.mod ├── go.sum ├── handler.go ├── main.go └── plugins ├── proxy.go └── reverse.go /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: docker 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "21:00" 8 | open-pull-requests-limit: 10 9 | -------------------------------------------------------------------------------- /.github/workflows/image.yaml: -------------------------------------------------------------------------------- 1 | name: Create and publish a Docker image 2 | 3 | on: 4 | push: 5 | tags: 6 | - v* 7 | 8 | # Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. 9 | env: 10 | REGISTRY: ghcr.io 11 | 12 | # There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. 13 | jobs: 14 | build-and-push-image: 15 | runs-on: ubuntu-latest 16 | # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. 17 | permissions: 18 | contents: read 19 | packages: write 20 | attestations: write 21 | id-token: write 22 | # 23 | steps: 24 | - name: Get commit to build 25 | id: ref 26 | run: |- 27 | if [[ -z "${{ github.event.inputs.tag }}" ]]; then 28 | ref="${{ github.ref_name }}" 29 | else 30 | ref="${{ github.event.inputs.tag }}" 31 | fi 32 | echo "ref=$ref" 33 | echo "ref=$ref" >> $GITHUB_OUTPUT 34 | if [[ $ref == *"-"* ]]; then 35 | latest=latest-beta 36 | else 37 | latest=latest 38 | fi 39 | echo "latest=$latest" 40 | echo "latest=$latest" >> $GITHUB_OUTPUT 41 | - name: Checkout repository 42 | uses: actions/checkout@v4 43 | with: 44 | ref: ${{ steps.ref.outputs.ref }} 45 | - name: Set up QEMU 46 | uses: docker/setup-qemu-action@v3 47 | - name: Set up Docker Buildx 48 | uses: docker/setup-buildx-action@v3 49 | - name: Log in to the Container registry 50 | uses: docker/login-action@v3 51 | with: 52 | registry: ${{ env.REGISTRY }} 53 | username: ${{ github.actor }} 54 | password: ${{ secrets.GITHUB_TOKEN }} 55 | - name: Extract metadata (tags, labels) for Docker 56 | id: meta 57 | uses: docker/metadata-action@v5 58 | with: 59 | images: ghcr.io/huangzheng2016/the-only-mirror 60 | - name: Build and push Docker image 61 | id: push 62 | uses: docker/build-push-action@v6 63 | with: 64 | platforms: linux/amd64,linux/arm64 65 | target: prod 66 | context: . 67 | push: true 68 | tags: | 69 | ghcr.io/huangzheng2016/the-only-mirror:${{ steps.ref.outputs.latest }} 70 | ghcr.io/huangzheng2016/the-only-mirror:${{ steps.ref.outputs.ref }} 71 | labels: ${{ steps.meta.outputs.labels }} 72 | -------------------------------------------------------------------------------- /.github/workflows/realease.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | tags: 4 | - v* 5 | 6 | jobs: 7 | releases-matrix: 8 | name: Release Go Binary 9 | runs-on: ubuntu-latest 10 | permissions: 11 | contents: write 12 | strategy: 13 | max-parallel: 1 14 | matrix: 15 | include: 16 | - goarch: amd64 17 | goos: darwin 18 | - goarch: arm64 19 | goos: darwin 20 | - goarch: amd64 21 | goos: linux 22 | - goarch: arm64 23 | goos: linux 24 | steps: 25 | - name: Show environment 26 | run: export 27 | - uses: actions/checkout@v3 28 | - uses: ncipollo/release-action@v1 29 | with: 30 | allowUpdates: true 31 | token: ${{ secrets.GITHUB_TOKEN }} 32 | - uses: wangyoucao577/go-release-action@master 33 | with: 34 | github_token: ${{ secrets.GITHUB_TOKEN }} 35 | goos: ${{ matrix.goos }} 36 | goarch: ${{ matrix.goarch }} 37 | build_command: "make" 38 | binary_name: "the-only-mirror" 39 | extra_files: the-only-mirror 40 | 41 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:alpine as build 2 | 3 | WORKDIR /app 4 | 5 | COPY . . 6 | 7 | RUN go build -v -trimpath -o the-only-mirror ./ 8 | 9 | FROM alpine:latest as prod 10 | 11 | WORKDIR /app 12 | 13 | COPY --from=build /app/the-only-mirror /app/the-only-mirror 14 | COPY --from=build /app/config.json /app/config.json 15 | EXPOSE 8080 16 | 17 | ENTRYPOINT [ "/app/the-only-mirror" ] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | go build -v -trimpath -o the-only-mirror ./ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TheOnlyMirror 2 | 3 | 既然镜像源太占空间,那为什么不能用反代源呢! 4 | 5 | 但加速代理域名那么多,为什么不尝试用同一个域名,代理所有的镜像源呢? 6 | 7 | 原理为不同应用请求镜像源时的UA/PATH/HOST/HEADER会有相应的特征,从而确定请求的镜像源。 8 | 9 | 如果采用Docker+Nginx反代方式部署,请务必打开HTTP/2支持 10 | 11 | ## 快速部署 12 | 13 | 感谢@jimyag的PR 14 | ```shell 15 | docker run -d --restart=unless-stopped -p 8080:8080 ghcr.io/huangzheng2016/the-only-mirror 16 | ``` 17 | 18 | ## 计划支持 19 | 20 | - [x] Ubuntu/Debian/CentOS/Kali/Alpine 21 | - [x] Python - Pypi 22 | - [x] Docker - Dockerhub 23 | - [x] Github 24 | - [x] Node - Npm 25 | - [x] Golang - Goproxy 26 | - [x] Golang/Docker - download 27 | - [x] 指定域名代理 githubusercontent、gist 等 28 | - [ ] ...... 29 | 30 | 其他源理论上也支持,目前仅作为demo使用,不做过多添加,需要添加的可以自行fork或者pr 31 | 32 | ## 使用 33 | 34 | ```shell 35 | # Docker 换源 36 | tee /etc/docker/daemon.json <<-'EOF' 37 | { 38 | "registry-mirrors": ["https://example.com"] 39 | } 40 | EOF 41 | 42 | # Linux 换源,仅作参考 43 | sed -i "s/http.kali.org/example.com/g" /etc/apt/sources.list 44 | 45 | # Python 换源 46 | pip config set global.index-url https://example.com 47 | pip install -i https://example.com -r requirements.txt 48 | 49 | # Golang 换源 50 | $ export GO111MODULE=on 51 | $ export GOPROXY=https://example.com 52 | 53 | # Node 换源 54 | npm config set registry https://example.com 55 | 56 | # 代理下载 github 文件 (TODO:302跳转已处理,非HTTPS情况未处理) 57 | curl -O https://example.com/github.com/huangzheng2016/TheOnlyMirror/archive/master.zip 58 | wget https://example.com/raw.githubusercontent.com/huangzheng2016/TheOnlyMirror/main/README.md 59 | ``` 60 | 61 | ## 配置文件 62 | 63 | ```json 64 | { 65 | "port":8080,//代理端口 66 | "tls":false,//是否使用TLS 67 | "tls_redirect":false,//在不使用TLS的情况下,打开此功能可以解决一些不支持TLS的问题 68 | "crt":"",//TLS证书 69 | "key":"", 70 | "sources":{ 71 | "ubuntu":{ 72 | "path":"/ubuntu",//镜像源路径 73 | "mirror":"https://archive.ubuntu.com"//镜像源地址 74 | }, 75 | ...... 76 | "docker_auth":{ 77 | "priority":2,//优先级,优先级越大越先匹配,默认为0 78 | "ua":"docker",//匹配的User-Agent 79 | "path":"/token",//匹配的路径 80 | //当ua/path两者都存在时,需要同时满足 81 | "mirror":"https://auth.docker.io" 82 | }, 83 | "docker_registry":{ 84 | "priority":1, 85 | "ua":"docker", 86 | "replaces":[ 87 | { 88 | "type":"header",//替换规则类型,header为替换请求头 89 | "header": "www-authenticate",//匹配头 90 | "src":"https://",//匹配目标 91 | "dst":""//如果tls_redirect为true,则会根据tls开关,替换为https或者http 92 | }, 93 | { 94 | "type":"header", 95 | "header": "www-authenticate", 96 | "src":"auth.docker.io", 97 | "dst":""//替换为请求的Host 98 | } 99 | ], 100 | "mirror":"https://registry-1.docker.io" 101 | }, 102 | ...... 103 | "pypi_web":{ 104 | ...... 105 | "prefix":"/simple",//代理时需要加入的前缀 106 | ...... 107 | }, 108 | ...... 109 | }, 110 | "proxy":[ 111 | "https://github.com",//允许代理下载的域名 112 | ...... 113 | ] 114 | } 115 | ``` 116 | 117 | 118 | ## 其他项目 119 | 120 | 本项目的另一种实现:https://github.com/Jlan45/TheOnlyMirror 121 | -------------------------------------------------------------------------------- /cert/cert.go: -------------------------------------------------------------------------------- 1 | package cert 2 | 3 | import ( 4 | "crypto/rand" 5 | "crypto/rsa" 6 | "crypto/x509" 7 | "encoding/pem" 8 | "io/ioutil" 9 | "log" 10 | "math/big" 11 | "os" 12 | "time" 13 | ) 14 | 15 | func Generator_key() { 16 | if _, err := os.Stat("data"); os.IsNotExist(err) { 17 | err := os.Mkdir("data", 0755) 18 | if err != nil { 19 | log.Println("Error to create cert folder:", err) 20 | } 21 | } 22 | _, err1 := os.Stat("data/private.key") 23 | _, err2 := os.Stat("data/certificate.crt") 24 | if os.IsNotExist(err1) || os.IsNotExist(err2) { 25 | privateKey, err := rsa.GenerateKey(rand.Reader, 2048) 26 | if err != nil { 27 | log.Fatal(err) 28 | } 29 | template := x509.Certificate{ 30 | SerialNumber: big.NewInt(1), 31 | NotBefore: time.Now(), 32 | NotAfter: time.Now().AddDate(10, 0, 0), // 有效期为十年 33 | BasicConstraintsValid: true, 34 | } 35 | derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) 36 | if err != nil { 37 | log.Fatal(err) 38 | } 39 | err = ioutil.WriteFile("data/private.key", encodePrivateKeyToPEM(privateKey), 0600) 40 | if err != nil { 41 | log.Fatal(err) 42 | } 43 | err = ioutil.WriteFile("data/certificate.crt", pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}), 0644) 44 | if err != nil { 45 | log.Fatal(err) 46 | } 47 | } 48 | } 49 | 50 | func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte { 51 | privateKeyPEM := &pem.Block{ 52 | Type: "RSA PRIVATE KEY", 53 | Bytes: x509.MarshalPKCS1PrivateKey(privateKey), 54 | } 55 | return pem.EncodeToMemory(privateKeyPEM) 56 | } 57 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "port":8080, 3 | "tls":false, 4 | "tls_redirect":false, 5 | "crt":"", 6 | "key":"", 7 | "sources":{ 8 | "copr":{ 9 | "path":"/results", 10 | "mirror":"https://download.copr.fedorainfracloud.org" 11 | }, 12 | "chrome-linux":{ 13 | "path":"/linue/chrome", 14 | "mirror":"https://dl.google.com" 15 | }, 16 | "ubuntu":{ 17 | "path":"/ubuntu", 18 | "mirror":"http://archive.ubuntu.com" 19 | }, 20 | "debian":{ 21 | "path":"/debian", 22 | "mirror":"https://deb.debian.org" 23 | }, 24 | "centos":{ 25 | "path":"/centos", 26 | "mirror":"http://mirror.centos.org" 27 | }, 28 | "epel-archieve":{ 29 | "path":"/pub/archive", 30 | "mirror":"https://archives.fedoraproject.org" 31 | }, 32 | "epel":{ 33 | "path":"/pub/epel", 34 | "mirror":"https://dl.fedoraproject.org" 35 | }, 36 | "jdoss":{ 37 | "path":"/results/jdoss", 38 | "mirror":"https://download.copr.fedorainfracloud.org" 39 | }, 40 | "kali":{ 41 | "path":"/kali", 42 | "mirror":"https://http.kali.org" 43 | }, 44 | "alpine":{ 45 | "path":"/alpine", 46 | "mirror":"https://dl-cdn.alpinelinux.org" 47 | }, 48 | "github":{ 49 | "ua":"git", 50 | "mirror":"https://github.com" 51 | }, 52 | "golang":{ 53 | "path":"/go", 54 | "mirror":"https://dl.google.com" 55 | }, 56 | "docker-dl-linux":{ 57 | "path":"/linux", 58 | "mirror":"https://download.docker.com" 59 | }, 60 | "docker-dl-mac":{ 61 | "path":"/mac", 62 | "mirror":"https://download.docker.com" 63 | }, 64 | "docker-dl-win":{ 65 | "path":"/win", 66 | "mirror":"https://download.docker.com" 67 | }, 68 | "docker_production": { 69 | "priority": 3, 70 | "ua": "docker", 71 | "path": "/registry-v2", 72 | "mirror":"https://production.cloudflare.docker.com" 73 | }, 74 | "docker_auth":{ 75 | "priority":2, 76 | "ua":"docker", 77 | "path":"/token", 78 | "mirror":"https://auth.docker.io" 79 | }, 80 | "docker_registry":{ 81 | "priority":1, 82 | "ua":"docker", 83 | "replaces":[ 84 | { 85 | "type":"header", 86 | "header": "www-authenticate", 87 | "src":"https://", 88 | "dst":"" 89 | }, 90 | { 91 | "type":"header", 92 | "header": "www-authenticate", 93 | "src":"auth.docker.io", 94 | "dst":"" 95 | }, 96 | { 97 | "type": "header", 98 | "header": "location", 99 | "src": "https://", 100 | "dst": "" 101 | }, 102 | { 103 | "type": "header", 104 | "header": "location", 105 | "src": "production.cloudflare.docker.com", 106 | "dst": "" 107 | } 108 | ], 109 | "mirror":"https://registry-1.docker.io" 110 | }, 111 | "buildkit_production": { 112 | "priority": 3, 113 | "ua": "buildkit", 114 | "path": "/registry-v2", 115 | "mirror":"https://production.cloudflare.docker.com" 116 | }, 117 | "buildkit_auth":{ 118 | "priority":2, 119 | "ua":"containerd", 120 | "path":"/token", 121 | "mirror":"https://auth.docker.io" 122 | }, 123 | "buildkit_registry": { 124 | "priority": 1, 125 | "ua": "buildkit", 126 | "replaces": [ 127 | { 128 | "type": "header", 129 | "header": "www-authenticate", 130 | "src": "https://", 131 | "dst": "" 132 | }, 133 | { 134 | "type": "header", 135 | "header": "www-authenticate", 136 | "src": "auth.docker.io", 137 | "dst": "" 138 | }, 139 | { 140 | "type": "header", 141 | "header": "location", 142 | "src": "https://", 143 | "dst": "" 144 | }, 145 | { 146 | "type": "header", 147 | "header": "location", 148 | "src": "production.cloudflare.docker.com", 149 | "dst": "" 150 | } 151 | ], 152 | "mirror":"https://registry-1.docker.io" 153 | }, 154 | "pypi_package":{ 155 | "priority":2, 156 | "ua":"pip", 157 | "path":"/packages", 158 | "mirror":"https://files.pythonhosted.org" 159 | }, 160 | "pypi_web":{ 161 | "priority":1, 162 | "ua":"pip", 163 | "replaces":[ 164 | { 165 | "src":"https://files.pythonhosted.org", 166 | "dst":"" 167 | } 168 | ], 169 | "prefix":"/simple", 170 | "mirror": "https://pypi.org" 171 | }, 172 | "npm":{ 173 | "ua":"npm", 174 | "replaces":[ 175 | { 176 | "src":"https://", 177 | "dst":"" 178 | }, 179 | { 180 | "src":"registry.npmjs.org", 181 | "dst":"" 182 | } 183 | ], 184 | "mirror":"https://registry.npmjs.org" 185 | }, 186 | "go":{ 187 | "ua":"Go-http-client", 188 | "mirror":"https://proxy.golang.org" 189 | } 190 | }, 191 | "proxy":[ 192 | "https://raw.githubusercontent.com", 193 | "https://gist.githubusercontent.com", 194 | "https://gist.github.com", 195 | "https://objects.githubusercontent.com", 196 | "https://codeload.github.com", 197 | "https://github.com", 198 | "https://copr.fedorainfracloud.org", 199 | "https://cdn.jsdelivr.net", 200 | "https://archive.debian.org", 201 | "https://downloads.openwrt.org" 202 | ] 203 | } 204 | -------------------------------------------------------------------------------- /config/handler.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "TheOnlyMirror/cert" 5 | "net/url" 6 | "strconv" 7 | ) 8 | 9 | func GetPort() string { 10 | return strconv.Itoa(config.Port) 11 | } 12 | 13 | func GetSources() []SourceSlice { 14 | return SourceSlices 15 | } 16 | 17 | func GetProxyHost() []*url.URL { 18 | return proxyHost 19 | } 20 | 21 | func GetTls() bool { 22 | return config.Tls 23 | } 24 | func GetTlsRedirect() bool { 25 | return config.TlsRedirect 26 | } 27 | 28 | func GetCert() (string, string) { 29 | if config.Crt != "" && config.Key != "" { 30 | return config.Crt, config.Key 31 | } 32 | cert.Generator_key() 33 | return "data/certificate.crt", "data/private.key" 34 | } 35 | -------------------------------------------------------------------------------- /config/load.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io" 7 | "net/url" 8 | "os" 9 | ) 10 | 11 | type Config struct { 12 | Port int `json:"port"` 13 | Tls bool `json:"tls"` 14 | TlsRedirect bool `json:"tls_redirect"` 15 | Crt string `json:"crt"` 16 | Key string `json:"key"` 17 | Sources map[string]Source `json:"sources"` 18 | Proxy []string `json:"proxy"` 19 | } 20 | 21 | type Source struct { 22 | Priority int `json:"priority"` 23 | Type string `json:"type"` 24 | UA string `json:"ua"` 25 | Path string `json:"path"` 26 | Prefix string `json:"prefix"` 27 | Replaces []Replace `json:"replaces"` 28 | Mirror string `json:"mirror"` 29 | } 30 | 31 | type Replace struct { 32 | Type string `json:"type"` 33 | Header string `json:"header"` 34 | Src string `json:"src"` 35 | Dst string `json:"dst"` 36 | } 37 | 38 | var config Config 39 | 40 | var proxyHost []*url.URL 41 | 42 | func Load() error { 43 | file, err := os.Open("config.json") 44 | if err != nil { 45 | return fmt.Errorf("failed to open config file: %v", err) 46 | } 47 | defer file.Close() 48 | bytes, err := io.ReadAll(file) 49 | if err != nil { 50 | return fmt.Errorf("failed to read config file: %v", err) 51 | } 52 | err = json.Unmarshal(bytes, &config) 53 | if err != nil { 54 | return fmt.Errorf("failed to parse config file: %v", err) 55 | } 56 | prepareConfig() 57 | return nil 58 | } 59 | -------------------------------------------------------------------------------- /config/prepare.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "net/url" 5 | "sort" 6 | ) 7 | 8 | type SourceSlice struct { 9 | Key string 10 | Sources Source 11 | } 12 | 13 | var SourceSlices []SourceSlice 14 | 15 | func prepareConfig() { 16 | for key, source := range config.Sources { 17 | SourceSlices = append(SourceSlices, SourceSlice{Key: key, Sources: source}) 18 | } 19 | sort.Slice(SourceSlices, func(i, j int) bool { 20 | return SourceSlices[i].Sources.Priority > SourceSlices[j].Sources.Priority 21 | }) 22 | for _, proxy := range config.Proxy { 23 | targetUrl, _ := url.Parse(proxy) 24 | proxyHost = append(proxyHost, targetUrl) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | the_only_mirror: 4 | build: 5 | context: . 6 | dockerfile: Dockerfile 7 | restart: unless-stopped 8 | ports: 9 | - 50242:8080 10 | volumes: 11 | - ./config.json:/app/config.json -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module TheOnlyMirror 2 | 3 | go 1.20 4 | 5 | require golang.org/x/net v0.27.0 6 | 7 | require golang.org/x/text v0.16.0 // indirect 8 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= 2 | golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= 3 | golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= 4 | golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= 5 | -------------------------------------------------------------------------------- /handler.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "TheOnlyMirror/config" 5 | "TheOnlyMirror/plugins" 6 | "log" 7 | "net/http" 8 | "net/url" 9 | "strings" 10 | ) 11 | 12 | func endsWithSlash(path string) string { 13 | if !strings.HasSuffix(path, "/") { 14 | return path + "/" 15 | } 16 | return path 17 | } 18 | 19 | func inPath(path string, target string) bool { 20 | if path == "" || path == target || strings.HasPrefix(target, endsWithSlash(path)) { 21 | return true 22 | } 23 | return false 24 | } 25 | 26 | func inUA(ua string, target string) bool { 27 | if ua == "" { 28 | return true 29 | } 30 | return strings.HasPrefix(strings.ToLower(target), strings.ToLower(ua)) 31 | } 32 | 33 | func handler(w http.ResponseWriter, r *http.Request) { 34 | host := r.Host 35 | ua := r.UserAgent() 36 | path := r.URL.Path 37 | sources := config.GetSources() 38 | log.Println(host, ua, path) 39 | for _, s := range sources { 40 | if inPath(s.Sources.Path, path) && inUA(s.Sources.UA, ua) { 41 | log.Println("Match source " + s.Key) 42 | switch s.Sources.Type { 43 | default: 44 | plugins.HandlerReverse(w, r, s.Sources) 45 | } 46 | return 47 | } 48 | } 49 | path = strings.TrimPrefix(path, "/") 50 | path = strings.TrimPrefix(path, "http:/") 51 | path = strings.TrimPrefix(path, "https:/") 52 | path = strings.TrimPrefix(path, "/") 53 | 54 | proxyHost := config.GetProxyHost() 55 | targetUrl, err := url.Parse("https://" + path) 56 | if err == nil { 57 | for _, proxy := range proxyHost { 58 | if proxy.Host == targetUrl.Host { 59 | log.Println("Match proxy " + proxy.Host) 60 | targetUrl.Scheme = proxy.Scheme 61 | plugins.HandlerProxy(w, r, targetUrl) 62 | return 63 | } 64 | } 65 | } 66 | http.NotFound(w, r) 67 | } 68 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "TheOnlyMirror/config" 5 | "log" 6 | "net/http" 7 | ) 8 | 9 | func main() { 10 | if config.Load() != nil { 11 | log.Fatal("load config error") 12 | return 13 | } 14 | http.HandleFunc("/", handler) 15 | http.HandleFunc("/{path}", handler) 16 | 17 | if config.GetTls() == true { 18 | log.Println("Starting proxy server with tls on :" + config.GetPort()) 19 | crt, key := config.GetCert() 20 | if err := http.ListenAndServeTLS(":"+config.GetPort(), crt, key, nil); err != nil { 21 | log.Fatalf("Failed to start server: %v", err) 22 | } 23 | } else { 24 | log.Println("Starting proxy server on :" + config.GetPort()) 25 | if err := http.ListenAndServe(":"+config.GetPort(), nil); err != nil { 26 | log.Fatalf("Failed to start server: %v", err) 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /plugins/proxy.go: -------------------------------------------------------------------------------- 1 | package plugins 2 | 3 | import ( 4 | "TheOnlyMirror/config" 5 | "golang.org/x/net/http2" 6 | "net/http" 7 | "net/http/httputil" 8 | "net/url" 9 | "strings" 10 | ) 11 | 12 | func HandlerProxy(w http.ResponseWriter, r *http.Request, targetUrl *url.URL) { 13 | proxy := httputil.NewSingleHostReverseProxy(targetUrl) 14 | if r.ProtoMajor == 2 { 15 | proxy.Transport = &http2.Transport{} 16 | } 17 | proxy.Director = func(req *http.Request) { 18 | req.URL.Scheme = targetUrl.Scheme 19 | req.URL.Host = targetUrl.Host 20 | req.URL.Path = targetUrl.Path 21 | req.Host = targetUrl.Host 22 | } 23 | proxy.ModifyResponse = func(resp *http.Response) error { 24 | proxyHost := config.GetProxyHost() 25 | location := resp.Header.Get("location") 26 | for _, proxy := range proxyHost { 27 | if strings.Contains(location, proxy.Host) { 28 | location = strings.Replace(location, proxy.Host, r.Host+"/"+proxy.Host, -1) 29 | break 30 | } 31 | } 32 | resp.Header.Set("location", location) 33 | return nil 34 | } 35 | proxy.ServeHTTP(w, r) 36 | } 37 | -------------------------------------------------------------------------------- /plugins/reverse.go: -------------------------------------------------------------------------------- 1 | package plugins 2 | 3 | import ( 4 | "TheOnlyMirror/config" 5 | "bytes" 6 | "compress/gzip" 7 | "golang.org/x/net/http2" 8 | "io" 9 | "io/ioutil" 10 | "net/http" 11 | "net/http/httputil" 12 | "net/url" 13 | "strings" 14 | ) 15 | 16 | func HandlerReverse(w http.ResponseWriter, r *http.Request, source config.Source) { 17 | targetUrl, _ := url.Parse(source.Mirror) 18 | proxy := httputil.NewSingleHostReverseProxy(targetUrl) 19 | if r.ProtoMajor == 2 { 20 | proxy.Transport = &http2.Transport{} 21 | } 22 | proxy.Director = func(req *http.Request) { 23 | req.URL.Scheme = targetUrl.Scheme 24 | req.URL.Host = targetUrl.Host 25 | req.URL.Path = source.Prefix + strings.TrimPrefix(req.URL.Path, source.Prefix) 26 | req.Host = targetUrl.Host 27 | } 28 | proxy.ModifyResponse = func(resp *http.Response) error { 29 | if len(source.Replaces) == 0 { 30 | return nil 31 | } 32 | bodyBytes, err := io.ReadAll(resp.Body) 33 | if err != nil { 34 | return err 35 | } 36 | resp.Body.Close() 37 | encoding := resp.Header.Get("Content-Encoding") 38 | var modifiedBody string 39 | switch encoding { 40 | case "gzip": 41 | reader, _ := gzip.NewReader(bytes.NewBuffer(bodyBytes)) 42 | dataDecompressing, err := ioutil.ReadAll(reader) 43 | if err != nil { 44 | modifiedBody = string(bodyBytes) 45 | } else { 46 | modifiedBody = string(dataDecompressing) 47 | } 48 | default: 49 | modifiedBody = string(bodyBytes) 50 | } 51 | for _, replace := range source.Replaces { 52 | src := replace.Src 53 | dst := replace.Dst 54 | switch dst { 55 | case "": 56 | dst = r.Host 57 | case "": 58 | if config.GetTlsRedirect() == true { 59 | if config.GetTls() == true { 60 | dst = "https://" 61 | } else { 62 | dst = "http://" 63 | } 64 | } else { 65 | continue 66 | } 67 | } 68 | switch replace.Type { 69 | case "header": 70 | header := resp.Header.Get(replace.Header) 71 | header = strings.Replace(header, src, dst, -1) 72 | resp.Header.Set(replace.Header, header) 73 | default: 74 | modifiedBody = strings.Replace(modifiedBody, src, dst, -1) 75 | } 76 | 77 | } 78 | switch encoding { 79 | case "gzip": 80 | var b bytes.Buffer 81 | gw := gzip.NewWriter(&b) 82 | _, _ = gw.Write([]byte(modifiedBody)) 83 | _ = gw.Close() 84 | modifiedBody = string(b.Bytes()) 85 | } 86 | resp.Body = io.NopCloser(strings.NewReader(modifiedBody)) 87 | resp.ContentLength = int64(len(modifiedBody)) 88 | return nil 89 | } 90 | proxy.ServeHTTP(w, r) 91 | } 92 | --------------------------------------------------------------------------------