.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CF-Workers-Raw:轻松访问GitHub私有仓库
2 | 这个项目允许你通过Cloudflare Workers安全地访问GitHub私有仓库中的原始文件,无需直接暴露你的GitHub令牌。
3 | ## 为什么需要这个工具?
4 |
5 | - 你有一些存储在GitHub私有仓库中的重要文件。
6 | - 你想直接通过URL访问这些文件的原始内容(比如配置文件、数据文件等)。
7 | - 但是,你不想在URL中直接暴露你的GitHub令牌,因为这可能会被他人滥用。
8 |
9 | 我们的解决方案是使用Cloudflare Workers作为中间层,它替你安全地处理身份验证,让你可以安全地访问私有文件。
10 | ## 如何使用? [视频教程](https://www.youtube.com/watch?v=T-bK5o96lqI)
11 | 假设你的Cloudflare Workers项目部署在`raw.090227.xyz`,
12 |
13 | 而你要访问的私有文件是`https://raw.githubusercontent.com/cmliu/CF-Workers-Raw/main/_worker.js`。
14 |
15 | ## 方法1:通过URL参数传递令牌
16 | 最直接的方法是在URL中添加你的GitHub令牌作为参数:
17 | ```url
18 | https://raw.090227.xyz/cmliu/CF-Workers-Raw/main/_worker.js?token=你的GitHub令牌
19 | ```
20 | 或者,如果你喜欢完整的原始URL:
21 | ```url
22 | https://raw.090227.xyz/https://raw.githubusercontent.com/cmliu/CF-Workers-Raw/main/_worker.js?token=你的GitHub令牌
23 | ```
24 |
25 | ## 方法2:在Workers中设置全局令牌
26 | 如果你经常访问同一个私有仓库,可以在Workers设置中添加一个名为`GH_TOKEN`的变量,值为你的GitHub令牌。这样,你就可以直接访问,无需在URL中每次都包含令牌:
27 | ```url
28 | https://raw.090227.xyz/cmliu/CF-Workers-Raw/main/_worker.js
29 | ```
30 | 或者,如果你喜欢完整的原始URL:
31 | ```url
32 | https://raw.090227.xyz/https://raw.githubusercontent.com/cmliu/CF-Workers-Raw/main/_worker.js
33 | ```
34 |
35 | ## 方法3:添加额外的访问控制(推荐)
36 | 为了更高的安全性,你可以设置两个变量:
37 |
38 | - `GH_TOKEN`:你的GitHub令牌
39 | - `TOKEN`:一个自定义的访问密钥(比如mysecretkey)
40 |
41 | 然后,你的URL会是这样的:
42 | ```url
43 | https://raw.090227.xyz/cmliu/CF-Workers-Raw/main/_worker.js?token=mysecretkey
44 | ```
45 | 或者,如果你喜欢完整的原始URL:
46 | ```url
47 | https://raw.090227.xyz/https://raw.githubusercontent.com/cmliu/CF-Workers-Raw/main/_worker.js?token=mysecretkey
48 | ```
49 | 这种方法提供了双重安全:即使有人猜到了你的自定义密钥,他们仍然无法访问你的GitHub文件,因为GitHub令牌是安全地存储在Workers设置中的。
50 |
51 | ## 方法4:添加`GH_NAME`、`GH_REPO`、`GH_BRANCH`变量**隐藏GitHub路径信息**
52 |
53 | 为了更高的隐私性,你可以设置多个变量:
54 | - `GH_NAME`:你的GitHub用户名(例如: **cmliu**)
55 | 然后,你的URL会是这样的:
56 | ```url
57 | https://raw.090227.xyz/CF-Workers-Raw/main/_worker.js?token=sd123123
58 | ```
59 |
60 | - `GH_REPO`:你的GitHub仓库名(例如: **CF-Workers-Raw**,必须设置`GH_NAME`变量为前提)
61 | 然后,你的URL会是这样的:
62 | ```url
63 | https://raw.090227.xyz/main/_worker.js?token=sd123123
64 | ```
65 |
66 | - `GH_BRANCH`:你的GitHub仓库名(例如: **main**,必须设置`GH_NAME`和`GH_REPO`变量为前提)
67 | 然后,你的URL会是这样的:
68 | ```url
69 | https://raw.090227.xyz/_worker.js?token=sd123123
70 | ```
71 |
72 | **如您使用完整的原始URL,则以上变量将不会生效!**
73 | ```url
74 | https://raw.090227.xyz/https://raw.githubusercontent.com/cmliu/CF-Workers-Raw/main/_worker.js?token=sd123123
75 | ```
76 |
77 | ## 如何设置这些变量?
78 |
79 | 在你的Cloudflare Workers管理面板中:
80 |
81 | 1. 进入你的Workers项目。
82 | 2. 点击**设置**标签。
83 | 3. 滚动到**环境变量**部分。
84 | 4. 添加以下变量:
85 | - 变量:GH_TOKEN,值:你的GitHub个人访问令牌
86 | - 变量:TOKEN(可选),值:你的自定义访问密钥
87 |
88 | GitHub个人访问令牌可以在GitHub设置中的"Developer settings" > "Personal access tokens (classic)"页面生成。
89 |
90 | ## 错误处理
91 |
92 | 如果出现问题,你会看到以下错误消息之一:
93 |
94 | - **TOKEN有误**:你提供的自定义访问密钥不正确。
95 | - **TOKEN不能为空**:需要提供GitHub令牌。
96 | - **无法获取文件 检测路径或TOKEN**:文件路径错误或令牌无权访问该文件。
97 | - **路径不能为空**:你没有指定要访问的文件路径。
98 |
99 | # 变量说明
100 | | 变量名 | 示例 | 必填 | 备注 |
101 | |--|--|--|--|
102 | | GH_TOKEN| `ghp_CgmlL2b5J8Z1soNUquc0bZblkbO3gKxhn13t`| ❌| 您的GitHub令牌 **token**|
103 | | TOKEN| `nicaibudaowo` | ❌| `GH_TOKEN`和`TOKEN`同时存在的时候会作为访问鉴权,单独赋值时的效果与`GH_TOKEN`相同|
104 | | GH_NAME| `cmliu` | ❌| 你的GitHub用户名 |
105 | | GH_REPO| `CF-Workers-Raw` | ❌| 你的GitHub仓库(必须设置`GH_NAME`变量为前提) |
106 | | GH_BRANCH| `main` | ❌| 你的GitHub仓库(必须设置`GH_NAME`和`GH_REPO`变量为前提) |
107 | | URL302 | `https://t.me/CMLiussss` |❌| 主页302跳转 |
108 | | URL | `https://github.com/cmliu/CF-Workers-Raw/blob/main/README.md` |❌| 主页伪装 |
109 | | ERROR | `无法获取文件,检查路径或TOKEN是否正确。` |❌| 自定义错误提示 |
110 |
111 | # 感谢
112 | 我自己的脑洞、ChatGPT
113 |
--------------------------------------------------------------------------------
/_worker.js:
--------------------------------------------------------------------------------
1 | let token = "";
2 | export default {
3 | async fetch(request ,env) {
4 | const url = new URL(request.url);
5 | if(url.pathname !== '/'){
6 | let githubRawUrl = 'https://raw.githubusercontent.com';
7 | if (new RegExp(githubRawUrl, 'i').test(url.pathname)){
8 | githubRawUrl += url.pathname.split(githubRawUrl)[1];
9 | } else {
10 | if (env.GH_NAME) {
11 | githubRawUrl += '/' + env.GH_NAME;
12 | if (env.GH_REPO) {
13 | githubRawUrl += '/' + env.GH_REPO;
14 | if (env.GH_BRANCH) githubRawUrl += '/' + env.GH_BRANCH;
15 | }
16 | }
17 | githubRawUrl += url.pathname;
18 | }
19 | //console.log(githubRawUrl);
20 | if (env.GH_TOKEN && env.TOKEN){
21 | if (env.TOKEN == url.searchParams.get('token')) token = env.GH_TOKEN || token;
22 | else token = url.searchParams.get('token') || token;
23 | } else token = url.searchParams.get('token') || env.GH_TOKEN || env.TOKEN || token;
24 |
25 | const githubToken = token;
26 | //console.log(githubToken);
27 | if (!githubToken || githubToken == '') return new Response('TOKEN不能为空', { status: 400 });
28 |
29 | // 构建请求头
30 | const headers = new Headers();
31 | headers.append('Authorization', `token ${githubToken}`);
32 |
33 | // 发起请求
34 | const response = await fetch(githubRawUrl, { headers });
35 |
36 | // 检查请求是否成功 (状态码 200 到 299)
37 | if (response.ok) {
38 | return new Response(response.body, {
39 | status: response.status,
40 | headers: response.headers
41 | });
42 | } else {
43 | const errorText = env.ERROR || '无法获取文件,检查路径或TOKEN是否正确。';
44 | // 如果请求不成功,返回适当的错误响应
45 | return new Response(errorText, { status: response.status });
46 | }
47 |
48 | } else {
49 | const envKey = env.URL302 ? 'URL302' : (env.URL ? 'URL' : null);
50 | if (envKey) {
51 | const URLs = await ADD(env[envKey]);
52 | const URL = URLs[Math.floor(Math.random() * URLs.length)];
53 | return envKey === 'URL302' ? Response.redirect(URL, 302) : fetch(new Request(URL, request));
54 | }
55 | //首页改成一个nginx伪装页
56 | return new Response(await nginx(), {
57 | headers: {
58 | 'Content-Type': 'text/html; charset=UTF-8',
59 | },
60 | });
61 | }
62 | }
63 | };
64 |
65 | async function nginx() {
66 | const text = `
67 |
68 |
69 |
70 | Welcome to nginx!
71 |
78 |
79 |
80 | Welcome to nginx!
81 | If you see this page, the nginx web server is successfully installed and
82 | working. Further configuration is required.
83 |
84 | For online documentation and support please refer to
85 | nginx.org.
86 | Commercial support is available at
87 | nginx.com.
88 |
89 | Thank you for using nginx.
90 |
91 |
92 | `
93 | return text ;
94 | }
95 |
96 | async function ADD(envadd) {
97 | var addtext = envadd.replace(/[ |"'\r\n]+/g, ',').replace(/,+/g, ','); // 将空格、双引号、单引号和换行符替换为逗号
98 | //console.log(addtext);
99 | if (addtext.charAt(0) == ',') addtext = addtext.slice(1);
100 | if (addtext.charAt(addtext.length -1) == ',') addtext = addtext.slice(0, addtext.length - 1);
101 | const add = addtext.split(',');
102 | //console.log(add);
103 | return add ;
104 | }
105 |
--------------------------------------------------------------------------------