谷歌Google Drive免费永久搭建个人直链网盘教程

  • A+
所属分类:Linux

谷歌Google Drive免费永久搭建个人直链网盘教程 – EDU教育网邮箱注册申请

文章目录[隐藏]

Google Drive介绍

Google Drive是谷歌公司推出的一项在线云存储服务。通过这项服务,用户可以获得15GB的免费存储空间。同时,如果用户有更大的需求,则可以通过付费的方式获得更大的存储空间。

Google Drive服务有本地客户端版本、也有网络界面版本,后者与Google Docs界面相似,支持直接从网页浏览器打开多达30多种文件格式,包括高清视频和Photoshop文件,即使在没有安装相关软件的情况下也可以直接打开。

另外,Google还向第三方提供API接口,允许用户从其它程序上传文件存储到Google Drive。
针对Google Drive的文件目录代码,利用GoIndex程序,以及CloudFlare服务进行部署Google Drive直链不限速目录网盘。

GoIndex介绍

功能介绍

不用梯子就可以访问Google Drive文件;
● 博客等网站可直接调用网盘图片、音频、视频文件,相当于网盘直链;
● 完全免费,所有人都可以使用。不用服务器(部署在CloudFlare Workers),也不用域名(由CloudFlare提供三级域名)即可快速部署使用;
● 可以将Google Drive文件以目录形式(类似OneIndex部署OneDrive网盘)列出,可直链调用下载;
● 流量走CloudFlare ,网速由CloudFlare决定;
PS:CloudFlare Workers允许您编写在所有CloudFlare的150多个全球数据中心上运行的JavaScript

GoIndex是一款部署在CloudFlare Workers的小程序,可以将Google Drive文件以目录形式列出,并且直链下载,流量走CloudFlare ,网速由CloudFlare决定,所以不用担心Google无法访问的困恼。

GoIndex程序地址:https://github.com/dzhl/goindex
您无需拥有域名和服务器,只要拥有一个Google Drive账号即可快速部署属于您自己的Google Drive直链不限速目录网盘。
GoIndex快速部署: 首先访问:https://install.gd.workers.dev
点击“获取认证码”,登陆Google(请自备梯子)。登陆后点击“允许”获取您的Auth认证码。
谷歌Google Drive免费永久搭建个人直链网盘教程

谷歌Google Drive免费永久搭建个人直链网盘教程
目录ID可以留空,点击“生成代码”后保存代码。
登陆:https://dash.cloudflare.com 如果您没有CloudFlare帐户,请先行注册激活帐户。
谷歌Google Drive免费永久搭建个人直链网盘教程
谷歌Google Drive免费永久搭建个人直链网盘教程
设置您的子域名前缀,域名后缀.workers.dev无法修改。
谷歌Google Drive免费永久搭建个人直链网盘教程
您的子域名xxxxx.workers.dev已全部设置完毕
谷歌Google Drive免费永久搭建个人直链网盘教程
先删除自带的代码,将生成的代码粘贴到Script,如果不能生成代码,可以复制下面代码,手工修改对应选项
~~~
var authConfig = {
"siteName": "goindex", // 网站名称
"root_pass": "password", // 根目录密码,优先于.password
"version" : "1.0.6", // 程序版本
"theme" : "material", // material classic
"client_id": "",//通过rclone配置文件查找
"client_secret": "",通过rclone配置文件查找
"refresh_token": "", // 授权 token
"root": "root" // 根目录ID
};

var gd;

var html = `





${authConfig.siteName}



`;

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});

/*
* Fetch and log a request
* @param {Request} request
/
async function handleRequest(request) {
if(gd == undefined){
gd = new googleDrive(authConfig);
}

if(request.method == 'POST'){
  return apiRequest(request);
}

let url = new URL(request.url);
let path = url.pathname;
let action = url.searchParams.get('a');

if(path.substr(-1) == '/' || action != null){
  return new Response(html,{status:200,headers:{'Content-Type':'text/html; charset=utf-8'}});
}else{
  if(path.split('/').pop().toLowerCase() == ".password"){
     return new Response("",{status:404});
  }
  let file = await gd.file(path);
  let range = request.headers.get('Range');
  return gd.down(file.id, range);
}

}

async function apiRequest(request) {
let url = new URL(request.url);
let path = url.pathname;

let option = {status:200,headers:{'Access-Control-Allow-Origin':'*'}}

if(path.substr(-1) == '/'){
  // check password
  let password = await gd.password(path);
  console.log("dir password", password);
  if(password != undefined && password != null && password != ""){
    try{
      var obj = await request.json();
    }catch(e){
      var obj = {};
    }
    console.log(password,obj);
    if(password != obj.password){
      let html = `{"error": {"code": 401,"message": "password error."}}`;
      return new Response(html,option);
    }
  }
  let list = await gd.list(path);
  return new Response(JSON.stringify(list),option);
}else{
  let file = await gd.file(path);
  let range = request.headers.get('Range');
  return new Response(JSON.stringify(file));
}

}

class googleDrive {
constructor(authConfig) {
this.authConfig = authConfig;
this.paths = [];
this.files = [];
this.passwords = [];
this.paths["/"] = authConfig.root;
if(authConfig.root_pass != ""){
this.passwords["/"] = authConfig.root_pass;
}
this.accessToken();
}

async down(id, range=''){
  let url = `https://www.googleapis.com/drive/v3/files/${id}?alt=media`;
  let requestOption = await this.requestOption();
  requestOption.headers['Range'] = range;
  return await fetch(url, requestOption);
}

async file(path){
  if(typeof this.files[path] == 'undefined'){
    this.files[path]  = await this._file(path);
  }
  return this.files[path] ;
}

async _file(path){
  let arr = path.split('/');
  let name = arr.pop();
  name = decodeURIComponent(name).replace(/\'/g, "\\'");
  let dir = arr.join('/')+'/';
  console.log(name, dir);
  let parent = await this.findPathId(dir);
  console.log(parent);
  let url = 'https://www.googleapis.com/drive/v3/files';
  let params = {'includeItemsFromAllDrives':true,'supportsAllDrives':true};
  params.q = `'${parent}' in parents and name = '${name}' andtrashed = false`;
  params.fields = "files(id, name, mimeType, size ,createdTime, modifiedTime, iconLink, thumbnailLink)";
  url += '?'+this.enQuery(params);
  let requestOption = await this.requestOption();
  let response = await fetch(url, requestOption);
  let obj = await response.json();
  console.log(obj);
  return obj.files[0];
}

// 通过reqeust cache 来缓存
async list(path){
  let id = await this.findPathId(path);
  return this._ls(id);
}

async password(path){
  if(this.passwords[path] !== undefined){
    return this.passwords[path];
  }

  console.log("load",path,".password",this.passwords[path]);

  let file = await gd.file(path+'.password');
  if(file == undefined){
    this.passwords[path] = null;
  }else{
    let url = `https://www.googleapis.com/drive/v3/files/${file.id}?alt=media`;
    let requestOption = await this.requestOption();
    let response = await this.fetch200(url, requestOption);
    this.passwords[path] = await response.text();
  }

  return this.passwords[path];
}

async _ls(parent){
  console.log("_ls",parent);

  if(parent==undefined){
    return null;
  }
  let url = 'https://www.googleapis.com/drive/v3/files';
  let params = {'includeItemsFromAllDrives':true,'supportsAllDrives':true};
  params.q = `'${parent}' in parents and trashed = false AND name !='.password'`;
  params.orderBy= 'folder,name,modifiedTime desc';
  params.fields = "nextPageToken, files(id, name, mimeType, size , modifiedTime)";
  params.pageSize = 1000;
  url += '?'+this.enQuery(params);
  let requestOption = await this.requestOption();
  let response = await fetch(url, requestOption);
  let obj = await response.json();
  return obj;
}

async findPathId(path){
  let c_path = '/';
  let c_id = this.paths[c_path];

  let arr = path.trim('/').split('/');
  for(let name of arr){
    c_path += name+'/';

    if(typeof this.paths[c_path] == 'undefined'){
      let id = await this._findDirId(c_id, name);
      this.paths[c_path] = id;
    }

    c_id = this.paths[c_path];
    if(c_id == undefined || c_id == null){
      break;
    }
  }
  console.log(this.paths);
  return this.paths[path];
}

async _findDirId(parent, name){
  name = decodeURIComponent(name).replace(/\'/g, "\\'");

  console.log("_findDirId",parent,name);

  if(parent==undefined){
    return null;
  }

  let url = 'https://www.googleapis.com/drive/v3/files';
  let params = {'includeItemsFromAllDrives':true,'supportsAllDrives':true};
  params.q = `'${parent}' in parents and mimeType = 'application/vnd.google-apps.folder' and name = '${name}'  and trashed = false`;
  params.fields = "nextPageToken, files(id, name, mimeType)";
  url += '?'+this.enQuery(params);
  let requestOption = await this.requestOption();
  let response = await fetch(url, requestOption);
  let obj = await response.json();
  if(obj.files[0] == undefined){
    return null;
  }
  return obj.files[0].id;
}

async accessToken(){
  console.log("accessToken");
  if(this.authConfig.expires == undefined  ||this.authConfig.expires< Date.now()){
    const obj = await this.fetchAccessToken();
    if(obj.access_token != undefined){
      this.authConfig.accessToken = obj.access_token;
      this.authConfig.expires = Date.now()+3500*1000;
    }
  }
  return this.authConfig.accessToken;
}

async fetchAccessToken() {
    console.log("fetchAccessToken");
    const url = "https://www.googleapis.com/oauth2/v4/token";
    const headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    };
    const post_data = {
        'client_id': this.authConfig.client_id,
        'client_secret': this.authConfig.client_secret,
        'refresh_token': this.authConfig.refresh_token,
        'grant_type': 'refresh_token'
    }

    let requestOption = {
        'method': 'POST',
        'headers': headers,
        'body': this.enQuery(post_data)
    };

    const response = await fetch(url, requestOption);
    return await response.json();
}

async fetch200(url, requestOption) {
    let response;
    for (let i = 0; i < 3; i++) {
        response = await fetch(url, requestOption);
        console.log(response.status);
        if (response.status != 403) {
            break;
        }
        await this.sleep(800 * (i + 1));
    }
    return response;
}

async requestOption(headers={},method='GET'){
  const accessToken = await this.accessToken();
  headers['authorization'] = 'Bearer '+ accessToken;
  return {'method': method, 'headers':headers};
}

enQuery(data) {
    const ret = [];
    for (let d in data) {
        ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));
    }
    return ret.join('&');
}

sleep(ms) {
    return new Promise(function (resolve, reject) {
        let i = 0;
        setTimeout(function () {
            console.log('sleep' + ms);
            i++;
            if (i >= 2) reject(new Error('i>=2'));
            else resolve(i);
        }, ms);
    })
}

}

String.prototype.trim = function (char) {
if (char) {
return this.replace(new RegExp('^\'+char+'+|\'+char+'+$', 'g'), '');
}
return this.replace(/^\s+|\s+$/g, '');
};
~~~

注意原作者已删除库,导致生成的代码已经翻车,我已经fork了源代码到自己git账户下,需要修改第21行代码,
原代码是
~~~
//cdn.jsdelivr.net/combine/gh/jquery/[email protected]/dist/jquery.min.js,gh/donwa/[email protected]${authConfig.version}/themes/${authConfig.theme}/app.js
~~~
改为
~~~
//cdn.jsdelivr.net/combine/gh/jquery/[email protected]/dist/jquery.min.js,gh/dzhl/[email protected]${authConfig.version}/themes/${authConfig.theme}/app.js
~~~

谷歌Google Drive免费永久搭建个人直链网盘教程
设置网站名称、根目录设置访问密码(如果不想设置密码,留空即可)。
点击“Run”右侧预览,如果能够正常访问,点击“Save and Deploy”保存代码,OK!
如果嫌网站三级域名太难记,可以修改三级域名前缀。 重新登陆:https://dash.cloudflare.com
谷歌Google Drive免费永久搭建个人直链网盘教程

注意事项

GoIndex网盘注意事项:
● CloudFlare Workers一天10万请求限制,这个可以在CloudFlare Workers界面查看。
● Google Drive下载10T/24h;
● Google API调用限制; GoIndex网盘如何绑定自定义域名?
请参见:https://github.com/donwa/goindex/issues/4

(L) (L) (L)

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin