轻松访问GitHub私有仓库

"cloudflare worker/page"

Posted by xuhao on June 7, 2024

“省略掉非必需的字,这样读者就会注意到你想保留的。 ”

轻松访问GitHub私有仓库

一、cloudflare worker/page代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let token = "";
export default {
	async fetch(request ,env) {
		const url = new URL(request.url);
		if(url.pathname !== '/'){
			let githubRawUrl = 'https://raw.githubusercontent.com';
			if (new RegExp(githubRawUrl, 'i').test(url.pathname)){
				githubRawUrl += url.pathname.split(githubRawUrl)[1];
			} else {
				if (env.GH_NAME) {
					githubRawUrl += '/' + env.GH_NAME;
					if (env.GH_REPO) {
						githubRawUrl += '/' + env.GH_REPO;
						if (env.GH_BRANCH) githubRawUrl += '/' + env.GH_BRANCH;
					}
				}
				githubRawUrl += url.pathname;
			}
			//console.log(githubRawUrl);
			if (env.GH_TOKEN && env.TOKEN){
				if (env.TOKEN == url.searchParams.get('token')) token = env.GH_TOKEN || token;
				else token = url.searchParams.get('token') || token;
			} else token = url.searchParams.get('token') || env.GH_TOKEN || env.TOKEN || token;
			
			const githubToken = token;
			//console.log(githubToken);
			if (!githubToken || githubToken == '') return new Response('TOKEN不能为空', { status: 400 });
			
			// 构建请求头
			const headers = new Headers();
			headers.append('Authorization', `token ${githubToken}`);

			// 发起请求
			const response = await fetch(githubRawUrl, { headers });

			// 检查请求是否成功 (状态码 200 到 299)
			if (response.ok) {
				// 读取文件内容
				const content = await response.text();

				// 在这里您可以处理文件内容,例如返回给客户端或进行其他操作
				return new Response(content, {
					status: 200,
					headers: {
						'Content-Type': 'text/plain; charset=UTF-8',
					},
				});
			} else {
				const errorText = env.ERROR || '无法获取文件,检查路径或TOKEN是否正确。';
				// 如果请求不成功,返回适当的错误响应
				return new Response(errorText, { status: response.status });
			}

		} else {
			const envKey = env.URL302 ? 'URL302' : (env.URL ? 'URL' : null);
			if (envKey) {
				const URLs = await ADD(env[envKey]);
				const URL = URLs[Math.floor(Math.random() * URLs.length)];
				return envKey === 'URL302' ? Response.redirect(URL, 302) : fetch(new Request(URL, request));
			}
			//首页改成一个nginx伪装页
			return new Response(await nginx(), {
				headers: {
					'Content-Type': 'text/html; charset=UTF-8',
				},
			});
		}
	}
};

async function nginx() {
	const text = `
	<!DOCTYPE html>
	<html>
	<head>
	<title>Welcome to nginx!</title>
	<style>
		body {
			width: 35em;
			margin: 0 auto;
			font-family: Tahoma, Verdana, Arial, sans-serif;
		}
	</style>
	</head>
	<body>
	<h1>Welcome to nginx!</h1>
	<p>If you see this page, the nginx web server is successfully installed and
	working. Further configuration is required.</p>
	
	<p>For online documentation and support please refer to
	<a href="http://nginx.org/">nginx.org</a>.<br/>
	Commercial support is available at
	<a href="http://nginx.com/">nginx.com</a>.</p>
	
	<p><em>Thank you for using nginx.</em></p>
	</body>
	</html>
	`
	return text ;
}

async function ADD(envadd) {
	var addtext = envadd.replace(/[	|"'\r\n]+/g, ',').replace(/,+/g, ',');	// 将空格、双引号、单引号和换行符替换为逗号
	//console.log(addtext);
	if (addtext.charAt(0) == ',') addtext = addtext.slice(1);
	if (addtext.charAt(addtext.length -1) == ',') addtext = addtext.slice(0, addtext.length - 1);
	const add = addtext.split(',');
	//console.log(add);
	return add ;
}

二、使用方法

1.创建你的GitHub个人访问令牌(PAT)

请生成拥有 repo 功能的token

2.使用变量存储

变量名 示例 必填 备注
GH_TOKEN ghp_CgmlL2b5J8Z1soNUquc0bZblkbO3gKxhn13t 您的GitHub令牌 token
TOKEN nicaibudaowo GH_TOKENTOKEN同时存在的时候会作为访问鉴权,单独赋值时的效果与GH_TOKEN相同
GH_NAME marker 你的GitHub用户名
GH_REPO CF-Workers-URL-Visit 你的GitHub仓库(必须设置GH_NAME变量为前提)
GH_BRANCH main 你的GitHub仓库(必须设置GH_NAMEGH_REPO变量为前提)
URL302 https://www.baidu.com/ 主页302跳转
URL https://www.baidu.com/ 主页伪装
ERROR 无法获取文件,检查路径或TOKEN是否正确。 自定义错误提示

3.访问方法

假设你的Cloudflare Workers项目部署在raw.090227.xyz

而你要访问的私有文件是https://raw.githubusercontent.com/marker/CF-Workers-URL-Visit/main/_worker.js

方法1:通过URL参数传递令牌

最直接的方法是在URL中添加你的GitHub令牌作为参数:

1
https://raw.090227.xyz/marker/CF-Workers-URL-Visit/main/_worker.js?token=你的GitHub令牌

或者,如果你喜欢完整的原始URL:

1
https://raw.090227.xyz/https://raw.githubusercontent.com/marker/CF-Workers-URL-Visit/main/_worker.js?token=你的GitHub令牌

方法2:在Workers中设置全局令牌

如果你经常访问同一个私有仓库,可以在Workers设置中添加一个名为GH_TOKEN的变量,值为你的GitHub令牌。这样,你就可以直接访问,无需在URL中每次都包含令牌:

1
https://raw.090227.xyz/marker/CF-Workers-URL-Visit/main/_worker.js

或者,如果你喜欢完整的原始URL:

1
https://raw.090227.xyz/https://raw.githubusercontent.com/marker/CF-Workers-URL-Visit/main/_worker.js

方法3:添加额外的访问控制(推荐)

为了更高的安全性,你可以设置两个变量:

  • GH_TOKEN:你的GitHub令牌
  • TOKEN:一个自定义的访问密钥(比如mysecretkey)

然后,你的URL会是这样的:

1
https://raw.090227.xyz/marker/CF-Workers-URL-Visit/main/_worker.js?token=mysecretkey

或者,如果你喜欢完整的原始URL:

1
https://raw.090227.xyz/https://raw.githubusercontent.com/marker/CF-Workers-URL-Visit/main/_worker.js?token=mysecretkey

这种方法提供了双重安全:即使有人猜到了你的自定义密钥,他们仍然无法访问你的GitHub文件,因为GitHub令牌是安全地存储在Workers设置中的。

方法4:添加GH_NAMEGH_REPOGH_BRANCH变量隐藏GitHub路径信息

为了更高的隐私性,你可以设置多个变量:

  • GH_NAME:你的GitHub用户名(例如: marker) 然后,你的URL会是这样的:
1
https://raw.090227.xyz/CF-Workers-URL-Visit/main/_worker.js?token=sd123123
  • GH_REPO:你的GitHub仓库名(例如: CF-Workers-URL-Visit,必须设置GH_NAME变量为前提) 然后,你的URL会是这样的:
1
https://raw.090227.xyz/main/_worker.js?token=sd123123
  • GH_BRANCH:你的GitHub仓库名(例如: main,必须设置GH_NAMEGH_REPO变量为前提) 然后,你的URL会是这样的:
1
https://raw.090227.xyz/_worker.js?token=sd123123

如您使用完整的原始URL,则以上变量将不会生效!

1
https://raw.090227.xyz/https://raw.githubusercontent.com/marker/CF-Workers-URL-Visit/main/_worker.js?token=sd123123

tip: GitHub项目代码由cmliu创建,可访问 https://github.com/cmliu/CF-Workers-Raw/ 查看。