├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── README_zh.md ├── examples ├── github ├── google ├── google-scholar ├── nytimes ├── pornhub │ ├── ci.phncdn.com │ └── www.pornhub.com ├── wikipedia └── wikipedia-zh ├── package-lock.json ├── package.json └── src └── index.js /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: Siujoeng-Lau 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the bug. 15 | 16 | **Screenshots** 17 | If applicable, add screenshots to help explain your problem. 18 | 19 | **Additional context** 20 | Add any other context about the problem here. 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: enhancement 6 | assignees: Siujoeng-Lau 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | bin/ 5 | pkg/ 6 | wasm-pack.log 7 | worker/ 8 | node_modules/ 9 | .cargo-ok 10 | wrangler.toml 11 | dist/ 12 | .DS_store -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at admin@siujoeng-lau.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Siujoeng-Lau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://repository-images.githubusercontent.com/102965805/c78b5880-7f54-11ea-9e8c-5ec65c48633c) 2 | 3 |

4 | 5 | # Workers-Proxy 6 | 7 | [![LICENSE](https://img.shields.io/github/license/Berkeley-Reject/Workers-Proxy.svg?style=for-the-badge)](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/LICENSE) 8 | [![GitHub closed issues](https://img.shields.io/github/issues-closed-raw/Berkeley-Reject/Workers-Proxy?style=for-the-badge)](https://github.com/Berkeley-Reject/Workers-Proxy/issues) 9 | [![GitHub stars](https://img.shields.io/github/stars/Berkeley-Reject/Workers-Proxy?style=for-the-badge)](https://github.com/Berkeley-Reject/Workers-Proxy/stargazers) 10 | 11 | [Issues](https://github.com/Berkeley-Reject/Workers-Proxy/issues) | 12 | [Pull requests](https://github.com/Berkeley-Reject/Workers-Proxy/pulls) | 13 | [Contributors](https://github.com/Berkeley-Reject/Workers-Proxy/graphs/contributors) 14 | 15 |

16 | 17 | ## Introduction 18 | 19 | Workers-Proxy is a lightweight Javascript [Reverse Proxy](https://www.cloudflare.com/learning/cdn/glossary/reverse-proxy/) based on [Cloudflare Workers](https://workers.cloudflare.com/). 20 | 21 | Users could deploy the reverse proxy on Cloudflare's global network without setting up virtual private servers and configuring Nginx or Apache. 22 | 23 | ### Features 24 | 25 | * Build mirror websites 26 | * Improve loading speed with Cloudflare's global network 27 | * Increase security (Hide IP addresses of websites) 28 | * Block specific areas or IP addresses 29 | * Redirect mobile users to different web pages 30 | 31 | ## Demo 32 | 33 | [GitHub](https://cdn.reverse-proxy.live) (This demo may not be available in specific regions.) 34 | 35 | [University of California, Los Angeles (UCLA)](https://ucla.reverse-proxy.live) 36 | 37 | [Python Documentation](https://python.reverse-proxy.live) 38 | 39 | 40 | ## Getting Started 41 | 42 | ### Build and Deploy 43 | 44 | #### Deploy with Wrangler 45 | 46 | 1. [Install Wrangler.](https://github.com/cloudflare/wrangler#installation) 47 | 48 | 2. Generate a new project. 49 | 50 | ``` 51 | wrangler generate my-workers-proxy https://github.com/Berkeley-Reject/Workers-Proxy 52 | ``` 53 | 54 | 3. [Configure](https://developers.cloudflare.com/workers/quickstart/#configure) your project's `wrangler.toml` file to prepare your project for deployment. 55 | 56 | ``` 57 | wrangler config 58 | ``` 59 | 60 | 4. Build and deploy on Cloudflare Workers. 61 | 62 | ``` 63 | wrangler build 64 | wrangler publish 65 | ``` 66 | 67 | #### Deploy manually 68 | 69 | 1. Navigate to [Cloudflare Workers](https://workers.cloudflare.com), register or sign in your Cloudflare account, and set custom subdomain for workers, and create a new Worker. 70 | 71 | 2. Customize '[src/index.js](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/src/index.js)', paste the code into Cloudflare online editor to replace the default one. 72 | 73 | 3. Change name of your Worker, save and deploy it, and check whether its performance fulfills your demand. 74 | 75 | ### Bind to Custom Domain 76 | 77 | 1. Check whether your domain is currently under Cloudflare's protection. 78 | 79 | 2. Navigate to the dashboard of your domain, select 'Workers' page, and click on 'Add Route'. 80 | 81 | 3. Type `https:///*` in `Route` and select the Worker you created previously. 82 | 83 | 4. Add a CNAME DNS record for your custom domain. Concretely, enter the subdomain (or '@' for root) in the 'Name' field, enter the **second level domain** of your workers in the 'Target' field, and set 'Proxy status' to 'Proxied'. 84 | 85 | ### Customize index.js 86 | 87 | Basically, there are a few constants on the top of the 'index.js' file. 88 | 89 | To customize your own Workers-Proxy Service, you should edit these constants according to your demands. 90 | 91 | ``` 92 | // Website you intended to retrieve for users. 93 | const upstream = 'www.google.com' 94 | 95 | // Custom pathname for the upstream website. 96 | const upstream_path = '/' 97 | 98 | // Website you intended to retrieve for users using mobile devices. 99 | const upstream_mobile = 'www.google.com' 100 | 101 | // Countries and regions where you wish to suspend your service. 102 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 103 | 104 | // IP addresses which you wish to block from using your service. 105 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 106 | 107 | // Whether to use HTTPS protocol for upstream address. 108 | const https = true 109 | 110 | // Whether to disable cache. 111 | const disable_cache = false 112 | 113 | // Replace texts. 114 | const replace_dict = { 115 | '$upstream': '$custom_domain', 116 | '//google.com': '' 117 | } 118 | ``` 119 | 120 | ### Example Configurations 121 | 122 | * [Google](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/google) 123 | * [Google Scholars](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/google-scholar) 124 | * [Github](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/github) 125 | * [Wikipedia](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/wikipedia) 126 | * [Wikipedia (Chinese)](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/wikipedia-zh) 127 | * [The New York Times](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/nytimes) 128 | * [Pornhub](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/pornhub) 129 | 130 | ### Websites with Multiple Domains 131 | 132 | If the website uses another domain name to serve static resources, users could deploy multiple Workers-Proxy and configure text replacement. 133 | 134 | 1. **www.google.com** serve static resources on **www.gstatic.com** 135 | 2. Deploy **Workers-Proxy A** to proxy **www.gstatic.com** 136 | 3. Deploy **Workers-Proxy B** to proxy **www.google.com** 137 | 4. Configure text replacement for **Workers-Proxy B**: 138 | ``` 139 | const replace_dict = { 140 | '$upstream': '$custom_domain', 141 | 'www.gstatic.com': '' 142 | } 143 | ``` 144 | -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 | ![](https://repository-images.githubusercontent.com/102965805/c78b5880-7f54-11ea-9e8c-5ec65c48633c) 2 | 3 |

4 | 5 | # Workers-Proxy 6 | 7 | [![LICENSE](https://img.shields.io/github/license/Berkeley-Reject/Workers-Proxy.svg?style=for-the-badge)](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/LICENSE) 8 | [![GitHub closed issues](https://img.shields.io/github/issues-closed-raw/Berkeley-Reject/Workers-Proxy?style=for-the-badge)](https://github.com/Berkeley-Reject/Workers-Proxy/issues) 9 | [![GitHub stars](https://img.shields.io/github/stars/Berkeley-Reject/Workers-Proxy?style=for-the-badge)](https://github.com/Berkeley-Reject/Workers-Proxy/stargazers) 10 | 11 | [Issues](https://github.com/Berkeley-Reject/Workers-Proxy/issues) | 12 | [Pull requests](https://github.com/Berkeley-Reject/Workers-Proxy/pulls) | 13 | [贡献者](https://github.com/Berkeley-Reject/Workers-Proxy/graphs/contributors) 14 | 15 |

16 | 17 | ## 介绍 18 | 19 | Workers-Proxy 是基于 [Cloudflare Workers](https://workers.cloudflare.com/) 的轻量级 Javascript [反向代理](https://www.cloudflare.com/learning/cdn/glossary/reverse-proxy/). 20 | 21 | 用户无需购买主机并配置 Web 服务器 (例如 Nginx 或 Apache) 即可在 Cloudflare 的全球网络上部署反向代理. 22 | 23 | ### 功能 24 | 25 | * 搭建网站镜像 26 | * 通过 Cloudflare 的全球网络加速前端资源访问 27 | * 增加安全性, 隐藏网站的真实 IP 地址 28 | * 屏蔽特定地区或 IP 地址 29 | * 将移动设备用户转发到不同网站 30 | 31 | ## 示例 32 | 33 | [GitHub](https://cdn.reverse-proxy.live) (该示例在部分地区无法使用) 34 | 35 | [University of California, Los Angeles (UCLA)](https://ucla.reverse-proxy.live) 36 | 37 | [Python 文档](https://python.reverse-proxy.live) 38 | 39 | ## 配置教程 40 | 41 | ### 安装和部署 42 | 43 | #### 使用 Wrangler 部署 44 | 45 | 1. [安装 Wrangler.](https://github.com/cloudflare/wrangler#installation) 46 | 47 | 2. 创建一个新的项目. 48 | 49 | ``` 50 | wrangler generate my-workers-proxy https://github.com/Berkeley-Reject/Workers-Proxy 51 | ``` 52 | 53 | 3. [配置](https://developers.cloudflare.com/workers/quickstart/#configure)该项目的 `wrangler.toml` 文件来准备部署你的项目. 54 | 55 | ``` 56 | wrangler config 57 | ``` 58 | 59 | 4. 构建并部署于 Cloudflare Workers. 60 | 61 | ``` 62 | wrangler build 63 | wrangler publish 64 | ``` 65 | 66 | #### 手动部署 67 | 68 | 1. 转到 [Cloudflare Workers](https://workers.cloudflare.com), 注册或登录 Cloudflare 账号, 为 Workers 设置子域名, 创建新的 Worker. 69 | 70 | 2. 自定义 '[src/index.js](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/src/index.js)', 将代码复制到在线编辑器中, 替换默认代码. 71 | 72 | 3. 更改 Worker 名称, 保存代码并部署, 测试反向代理是否符合需求. 73 | 74 | ### 绑定自定义域名 75 | 76 | 1. 检查域名是否接入 Cloudflare. 77 | 78 | 2. 跳转到域名的控制面板, 选择 'Workers' 页面, 点击 'Add Route'. 79 | 80 | 3. 在 `Route` 中输入 `https://<自定义域名>/*` 并且选择刚创建的 Worker. 81 | 82 | 4. 为自定义域名添加 CNAME DNS 记录. 在 DNS 页面中, 在 'Name' 区域输入自定义域名的子域名 (或者 @), 在 'Target' 区域输入 Worker 的**二级域名** (例如 test.workers.dev), 将代理状态选择为 '代理'. 83 | 84 | ### 自定义 index.js 85 | 86 | 在 'index.js' 文件顶部有一些常量. 87 | 88 | 为了自定义 Workers-Proxy 反向代理, 请根据需求编辑这些常量. 89 | 90 | ``` 91 | // 代理网站 92 | const upstream = 'www.google.com' 93 | 94 | // 代理网站的目录 95 | const upstream_path = '/' 96 | 97 | // 手机用户代理网站 98 | const upstream_mobile = 'www.google.com' 99 | 100 | // 屏蔽国家和地区 101 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 102 | 103 | // 屏蔽 IP 地址 104 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 105 | 106 | // 源站是否开启 HTTPS 107 | const https = true 108 | 109 | // 是否允许浏览器缓存 110 | const disable_cache = false 111 | 112 | // 文本替换 113 | const replace_dict = { 114 | '$upstream': '$custom_domain', 115 | '//google.com': '' 116 | } 117 | ``` 118 | 119 | ### 配置模板 120 | 121 | * [Google](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/google) 122 | * [Google Scholars](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/google-scholar) 123 | * [GitHub](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/github) 124 | * [Wikipedia](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/wikipedia) 125 | * [Wikipedia (Chinese)](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/wikipedia-zh) 126 | * [The New York Times](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/nytimes) 127 | * [Pornhub](https://github.com/Berkeley-Reject/Workers-Proxy/blob/master/examples/pornhub) 128 | 129 | ### 部署多个域名 130 | 131 | 如果被反代的网站使用其他域名的静态资源时, 可以部署多个 Workers-Proxy 并配置文本替换. 132 | 133 | 1. www.google.com 使用位于 www.gstatic.com 的静态资源 134 | 2. 部署 Workers-Proxy A, 用于代理 www.gstatic.com 135 | 3. 部署 Workers-Proxy B, 用于代理 www.google.com 136 | 4. 配置 Workers-Proxy B 的文本替换: 137 | ``` 138 | const replace_dict = { 139 | '$upstream': '$custom_domain', 140 | 'www.gstatic.com': '' 141 | } 142 | ``` -------------------------------------------------------------------------------- /examples/github: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'github.com' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'github.com' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = true 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain' 25 | } -------------------------------------------------------------------------------- /examples/google: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'www.google.com' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'www.google.com' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = false 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain', 25 | '//google.com': '' 26 | } -------------------------------------------------------------------------------- /examples/google-scholar: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'scholar.google.com' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'scholar.google.com' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = false 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain' 25 | } -------------------------------------------------------------------------------- /examples/nytimes: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'www.nytimes.com' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'www.nytimes.com' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = false 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain', 25 | "static01.nyt.com": "$custom_domain" 26 | } -------------------------------------------------------------------------------- /examples/pornhub/ci.phncdn.com: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'ci.phncdn.com' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'ci.phncdn.com' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = false 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain' 25 | } -------------------------------------------------------------------------------- /examples/pornhub/www.pornhub.com: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'www.pornhub.com' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'www.pornhub.com' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = false 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain', 25 | '//ci.phncdn.com': '//ciphncdncom.pornproxy.workers.dev', // replace this with your other deployment. 26 | 'var scriptText': '', // Issue #30 27 | 'trafficjunky.com': '', 28 | 'trafficjunky.net': '', 29 | 'contentabc.com': '', 30 | 'Ads By Traffic Junky': '', 31 | 'cdn1d-static-shared.phncdn.com/iframe-1.1.5.html': '', 32 | '/ads/iframe-mobile-3.0.0.html': '', 33 | '': '-->', 35 | 'Remove Ads': '' 36 | } 37 | -------------------------------------------------------------------------------- /examples/wikipedia: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'en.wikipedia.org' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'en.m.wikipedia.org' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = false 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain' 25 | } -------------------------------------------------------------------------------- /examples/wikipedia-zh: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'zh.wikipedia.org' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'zh.m.wikipedia.org' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = false 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain' 25 | } -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "workers-proxy", 3 | "version": "1.1.7", 4 | "lockfileVersion": 1 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "workers-proxy", 3 | "version": "1.1.7", 4 | "description": "Lightweight reverse proxy implemented with Cloudflare Workers.", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "test": "wrangler preview --watch", 8 | "build": "wrangler build", 9 | "deploy": "wrangler publish" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/Siujoeng-Lau/Workers-Proxy.git" 14 | }, 15 | "keywords": [ 16 | "reverse-proxy", 17 | "cloudflare-workers", 18 | "serverless" 19 | ], 20 | "author": "Siujoeng-Lau (https://siujoeng-lau.com)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/Siujoeng-Lau/Workers-Proxy/issues" 24 | }, 25 | "homepage": "https://github.com/Siujoeng-Lau/Workers-Proxy#readme" 26 | } 27 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | // Website you intended to retrieve for users. 2 | const upstream = 'www.google.com' 3 | 4 | // Custom pathname for the upstream website. 5 | const upstream_path = '/' 6 | 7 | // Website you intended to retrieve for users using mobile devices. 8 | const upstream_mobile = 'www.google.com' 9 | 10 | // Countries and regions where you wish to suspend your service. 11 | const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU'] 12 | 13 | // IP addresses which you wish to block from using your service. 14 | const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] 15 | 16 | // Whether to use HTTPS protocol for upstream address. 17 | const https = true 18 | 19 | // Whether to disable cache. 20 | const disable_cache = false 21 | 22 | // Replace texts. 23 | const replace_dict = { 24 | '$upstream': '$custom_domain', 25 | '//google.com': '' 26 | } 27 | 28 | addEventListener('fetch', event => { 29 | event.respondWith(fetchAndApply(event.request)); 30 | }) 31 | 32 | async function fetchAndApply(request) { 33 | const region = request.headers.get('cf-ipcountry').toUpperCase(); 34 | const ip_address = request.headers.get('cf-connecting-ip'); 35 | const user_agent = request.headers.get('user-agent'); 36 | 37 | let response = null; 38 | let url = new URL(request.url); 39 | let url_hostname = url.hostname; 40 | 41 | if (https == true) { 42 | url.protocol = 'https:'; 43 | } else { 44 | url.protocol = 'http:'; 45 | } 46 | 47 | if (await device_status(user_agent)) { 48 | var upstream_domain = upstream; 49 | } else { 50 | var upstream_domain = upstream_mobile; 51 | } 52 | 53 | url.host = upstream_domain; 54 | if (url.pathname == '/') { 55 | url.pathname = upstream_path; 56 | } else { 57 | url.pathname = upstream_path + url.pathname; 58 | } 59 | 60 | if (blocked_region.includes(region)) { 61 | response = new Response('Access denied: WorkersProxy is not available in your region yet.', { 62 | status: 403 63 | }); 64 | } else if (blocked_ip_address.includes(ip_address)) { 65 | response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', { 66 | status: 403 67 | }); 68 | } else { 69 | let method = request.method; 70 | let request_headers = request.headers; 71 | let new_request_headers = new Headers(request_headers); 72 | 73 | new_request_headers.set('Host', upstream_domain); 74 | new_request_headers.set('Referer', url.protocol + '//' + url_hostname); 75 | 76 | let original_response = await fetch(url.href, { 77 | method: method, 78 | headers: new_request_headers 79 | }) 80 | 81 | connection_upgrade = new_request_headers.get("Upgrade"); 82 | if (connection_upgrade && connection_upgrade.toLowerCase() == "websocket") { 83 | return original_response; 84 | } 85 | 86 | let original_response_clone = original_response.clone(); 87 | let original_text = null; 88 | let response_headers = original_response.headers; 89 | let new_response_headers = new Headers(response_headers); 90 | let status = original_response.status; 91 | 92 | if (disable_cache) { 93 | new_response_headers.set('Cache-Control', 'no-store'); 94 | } 95 | 96 | new_response_headers.set('access-control-allow-origin', '*'); 97 | new_response_headers.set('access-control-allow-credentials', true); 98 | new_response_headers.delete('content-security-policy'); 99 | new_response_headers.delete('content-security-policy-report-only'); 100 | new_response_headers.delete('clear-site-data'); 101 | 102 | if (new_response_headers.get("x-pjax-url")) { 103 | new_response_headers.set("x-pjax-url", response_headers.get("x-pjax-url").replace("//" + upstream_domain, "//" + url_hostname)); 104 | } 105 | 106 | const content_type = new_response_headers.get('content-type'); 107 | if (content_type != null && content_type.includes('text/html') && content_type.includes('UTF-8')) { 108 | original_text = await replace_response_text(original_response_clone, upstream_domain, url_hostname); 109 | } else { 110 | original_text = original_response_clone.body 111 | } 112 | 113 | response = new Response(original_text, { 114 | status, 115 | headers: new_response_headers 116 | }) 117 | } 118 | return response; 119 | } 120 | 121 | async function replace_response_text(response, upstream_domain, host_name) { 122 | let text = await response.text() 123 | 124 | var i, j; 125 | for (i in replace_dict) { 126 | j = replace_dict[i] 127 | if (i == '$upstream') { 128 | i = upstream_domain 129 | } else if (i == '$custom_domain') { 130 | i = host_name 131 | } 132 | 133 | if (j == '$upstream') { 134 | j = upstream_domain 135 | } else if (j == '$custom_domain') { 136 | j = host_name 137 | } 138 | 139 | let re = new RegExp(i, 'g') 140 | text = text.replace(re, j); 141 | } 142 | return text; 143 | } 144 | 145 | 146 | async function device_status(user_agent_info) { 147 | var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; 148 | var flag = true; 149 | for (var v = 0; v < agents.length; v++) { 150 | if (user_agent_info.indexOf(agents[v]) > 0) { 151 | flag = false; 152 | break; 153 | } 154 | } 155 | return flag; 156 | } --------------------------------------------------------------------------------