CloudFlare Workers 代理 Google Drive

CloudFlare Workers 代理 Google Drive

介绍

众所周知Google Drive是被墙的,而通过CloudFlare代理链接可实现国内访问Google Drive服务,本文介绍如何搭建Google Drive文件索引

goindex-theme-acrou
结合 Cloudflare WorkersGoogle Drive 的力量,可以在Cloudflare Workers的浏览器上建立你的文件索引

项目地址:goindex-theme-acrou

部署

创建 Google 项目

跳转创建项目页面,输入项目名之后确认创建

/posts/cloudflareworkers_googledrive/pic01.png

接着会跳转到OAuth 同意屏幕User Type选择外部

/posts/cloudflareworkers_googledrive/pic02.png

修改应用注册,必填项为应用名称用户支持电子邮件以及底下的开发者联系信息,其他可不填,保存即可

/posts/cloudflareworkers_googledrive/pic03.png

/posts/cloudflareworkers_googledrive/pic04.png

接下来的测试用户添加自己的邮箱即可

/posts/cloudflareworkers_googledrive/pic05.png

开启 Google Drive API

跳转开启 Google Drive API页面,开启该功能

/posts/cloudflareworkers_googledrive/pic06.png

开启成功后如下

/posts/cloudflareworkers_googledrive/pic07.png

创建 OAuth client ID

跳转创建 OAuth client ID页面,应用类型选择桌面应用名称随意,完成后点击创建

/posts/cloudflareworkers_googledrive/pic08.png

创建成功会返回客户端 ID客户端密钥自行保存,后面需要用到

/posts/cloudflareworkers_googledrive/pic09.png

安装 Rclone

下载地址,根据自己的系统版本选择下载之后解压

CMD 运行 Rclone

有两种方法,就Win10系统举例

  1. Rclone解压的目录的地址栏输入cmd,打开当前路径的命令行终端
  2. 在系统环境变量Path添加Rclone的路径
注意

在之后获取Google Driverefresh token时需要访问到Google的服务,所以必不可免的需要用到代理

CMD默认是不走代理的

可自行设置临时代理

1
2
set http_proxy=http://ip:port
set https_proxy=http://ip:port

使用 Rclone 获取 refresh_token

在终端上执行rclone confign新建,名字随意

1
2
3
4
$ rclone config                                                                           
No remotes found, make a new one?                                                               n) New remote                                                                                   s) Set configuration password                                                                   q) Quit config                                                                                   n/s/q> n

Enter name for new remote.                                                                       name> GoIndex

选择18Google Drive

1
2
3
4
5
Option Storage.                                                                                 Type of storage to configure.                                                                   Choose a number from below, or type in your own value.
...
17 / Google Cloud Storage (this is not Google Drive)                                             \ (google cloud storage)                                                                         18 / Google Drive                                                                               \ (drive)                                                                                       19 / Google Photos                                                                               \ (google photos)
...
Storage> 18

之后填写先前获取的client idclient secretscope权限选择1,即所有权限

service_account_file回车不填即可

Edit advanced config也是回车默认No

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Option client_id.                                                                               Google Application Client Id                                                                     Setting your own is recommended.                                                                 See https://rclone.org/drive/#making-your-own-client-id for how to create your own.             If you leave this blank, it will use an internal key which is low performance.                   Enter a value. Press Enter to leave empty.                                                       client_id> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com

Option client_secret.                                                                           OAuth Client Secret.                                                                             Leave blank normally.                                                                           Enter a value. Press Enter to leave empty.                                                       client_secret> xxxxxxxxxxxxxxxxxxx

Option scope.        
Scope that rclone should use when requesting access from drive.     
Choose a number from below, or type in your own value.                      
Press Enter to leave empty.                            
1 / Full access all files, excluding Application Data Folder.         
\ (drive)                                                    
2 / Read-only access to file metadata and file contents.               
\ (drive.readonly)                                            
/ Access to files created by rclone only.                     
3 | These are visible in the drive website.                           
| File authorization is revoked when the user deauthorizes the app.                   
\ (drive.file)                                                            
/ Allows read and write access to the Application Data folder.                  
4 | This is not visible in the drive website.                              
\ (drive.appfolder)                                                     
/ Allows read-only access to file metadata but                           
5 | does not allow any access to read or download file content.              
\ (drive.metadata.readonly)        
scope> 1

Use auto config选择n

1
2
3
4
5
6
Use auto config?                       
* Say Y if not sure                    
* Say N if you are working on a remote or headless machine       
y) Yes (default)                                             
n) No                                                             
y/n> n

之后就会提示输入验证码,复制rclone authorize "drive" "xxx"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Option config_token.                          
For this to work, you will need rclone available on a machine that has      
a web browser available.                                            
For more help and alternate methods see: https://rclone.org/remote_setup/      
Execute the following on the machine with the web browser (same rclone            
version recommended):                                                           
rclone authorize "drive" "xxx"                                                            
Then paste the result.    
Enter a value.                                                             
config_token> 

开启另外一个终端执行复制的命令,记得先开代理哦

之后会跳转到浏览器,登录并授权

/posts/cloudflareworkers_googledrive/pic10.png

显示如下即代表成功获取

/posts/cloudflareworkers_googledrive/pic11.png

回到终端复制返回的验证码,并在第一个终端输入

1
2
3
Paste the following into your remote machine --->   
此段为验证码
<---End paste 

根据自己的盘是不是Team Drive选择,得到的token里就有我们需要的refresh_token

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Configure this as a Shared Drive (Team Drive)?    
y) Yes                                       
n) No (default)                        
y/n> n

Configuration complete.    
Options:              
- type: drive     
- client_id: 642717824648-vv6fp0441ldalr9um9jqdftu49lisacu.apps.googleusercontent.com        
- client_secret: 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                     
- scope: drive                                                                 
- token: {"access_token":"xxx","token_type":"Bearer","refresh_token":"xxx","expiry":"20xx-xx-xxTxx:xx:xx.xxxxxxx+xx:xx"}

获取GoIndex

替换index.js里的client_id,client_secret,refresh_token为自己的

部署到 CloudFlare Workers

前往CloudFlare

Workers页面下,创建服务

/posts/cloudflareworkers_googledrive/pic12.png

/posts/cloudflareworkers_googledrive/pic13.png

/posts/cloudflareworkers_googledrive/pic14.png

点击快速编辑,将内容替换为刚才修改后的index.js,保存并部署

Note

默认访问的路径有TeamDrive,PrivateDrivefolder1

可按需删除或移动顺序,也可设置访问的账号密码,第一个为默认访问

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
roots: [
      {
        id: "",
        name: "TeamDrive",
        pass: "",
      },
      {
        id: "root",
        name: "PrivateDrive",
        user: "",
        pass: "",
        protect_file_link: true,
      },
      {
        id: "",
        name: "folder1",
        pass: "",
      },
],

其他个性化设置可参考README

鸣谢