forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.lanzou.js
197 lines (160 loc) · 5.93 KB
/
drive.lanzou.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* www.lanzous.com
* lanzou:password@foldId / foldId
*/
const name = 'Lanzous'
const version = '1.0'
const protocols = ['lanzou']
const defaultProtocol = 'lanzou'
const host = 'https://www.lanzous.com'
module.exports = ({ request , getConfig , datetime , cache , retrieveSize }) => {
const downloadCache = {}
const allowExt = 'd,apk,zip,rar,txt,7z,z,e,ct,doc,docx,exe,ke,db,tar,pdf,epub,mobi,azw,azw3,w3x,osk,osz,jar,xpk,cpk,lua,dmg,ppt,pptx,xls,xlsx,mp3,gz,psd,ipa,iso,ttf,txf,ttc,img,bin,gho,patch'.split(',')
const convExt = 'txt,ct'
const parse = (id) => {
let tmp = id.split('@')
let passwd , fid
if( tmp.length == 2 ){
passwd = tmp[0]
fid = tmp[1]
}else if( tmp.length == 1){
fid = id
}
return { passwd , fid }
}
const allowExtReg = new RegExp('('+allowExt.join('|')+').('+convExt.replace(/\,/g,'|')+')$')
const filterExt = (name) => {
if(!allowExtReg.test(name)){
return name.replace(new RegExp('.('+convExt.replace(/\,/g,'|')+')$'),'')
}else{
return name
}
}
const getFormDataForFolder = (body) => {
let data = (body.match(/(?<=data\s*:\s*)\{[\w\W]*?\}/) || [''])[0]
if( data ){
data = data.replace(/\s/g,'').replace(/\'/g,'"').replace(',}','}')
let formdata = {}
try{
let field_k = data.match(/(?<=\"k\":)[^\s,\}]+/)[0]
let field_t = data.match(/(?<=\"t\":)[^\s,\}]+/)[0]
let value_k = body.match(new RegExp('(?<='+field_k+"\\s*=\\s*')[^']+"))[0]
let value_t = body.match(new RegExp('(?<='+field_t+"\\s*=\\s*')[^']+"))[0] // timestamp
data = data.replace(new RegExp('(pgs|'+field_k+'|'+field_t+')','g'),'""').replace(/\'/g,'"').replace(/(?<=:)pwd/,'""')
formdata = JSON.parse( data )
formdata.k = value_k
formdata.t = value_t
delete formdata.pwd
return formdata
}catch(e){
return false
}
}else{
return false
}
}
const updateFile = (f) => {
if(f.name.endsWith('.d.txt')){
f.id = f.name.split('.').slice(-3,-2)[0]
f.name = f.name.split('.').slice(0,-3).join('.')
f.type = 'folder'
f.size = null
f.ext = null
}
return f
}
// gd folder => files
const folder = async(id) => {
let resid = `${defaultProtocol}:${id}`
let resp = false
let r = cache.get(resid)
if(r) {
resp = r
if(
resp.$cached_at &&
resp.children &&
( Date.now() - resp.$cached_at < getConfig('max_age_dir'))
){
console.log('get folder from cache')
return resp
}
}
let params = parse(id)
if(params.fid){
let { body , headers } = await request.get(host+'/'+params.fid , { headers:{'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'}})
let cookie = headers['set-cookie'] ? headers['set-cookie'].join('; ') : ''
let formdata = getFormDataForFolder(body)
if( formdata ){
formdata.pg = 1
if(params.passwd) {
formdata.pwd = params.passwd
}
let res = await request.post(`${host}/filemoreajax.php` , formdata ,{headers:{'Cookie':cookie} , json:true})
// let cookie = headers['set-cookie'] ? headers['set-cookie'].join('; ') : ''
if(res.body.zt != 1){
res.body.text = []
}
if( res.body && res.body.text){
resp = {id , type:'folder' , protocol:defaultProtocol}
let children = []
res.body.text.forEach( i => {
let name = filterExt(i.name_all)//.replace(/\.ct$/,'')
console.log(name+'<<<')
children.push(updateFile({
id:i.id,
name:name,
ext:name.split('.').pop(),
protocol:defaultProtocol,
// updated_at:datetime(i.upload_at*1000),
size:retrieveSize(i.size),
displaySize:i.size,
type : undefined,
}))
})
//folder 额外保存
resp.children = children
resp.$cached_at = Date.now()
cache.set(resid,resp)
}
}
}
return resp
}
/**
* 获取文件实际路径
*/
const file = async(id , { data = {} } = {}) =>{
let url
let { body } = await request.get(`${host}/tp/${id}` , {headers:{'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; 1503-M02 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.2 TBS/036558 Safari/537.36 MicroMessenger/6.3.25.861 NetType/WIFI Language/zh_CN'}})
if(downloadCache[id] && downloadCache[id].expired_at > Date.now() ){
url = downloadCache[id].url
}else{
try{
let code = body.split('<script type="text/javascript">')[1].split('</script>')[0]
code = 'var data = {}; function $c(){ return data };' + code.replace('document.getElementById','$c').replace(/document/g,'') + ';data.onfocus();return data.href;'
url = (new Function(code))()
if( url ){
let { headers } = await request.get(url,{followRedirect:false})
console.log(headers)
if(headers && headers.location){
url = headers.location
let expired_at = parseInt((url.match(/(?<=e=)(\d+)/g) || [0])[0]) * 1000
if(expired_at){
downloadCache[id] = { expired_at , url }
}
}
}
}catch(e){
console.log(e)
}
}
//url = 'https://dev25.baidupan.com/060314bb/2020/06/03/314be4db21834f4ca38975eff8e31764.ct?st=wfpNvMCms57yAOrJwj436w&e=1591169395&b=BSMNaAV2VSQCeVZvCzwGZQV_bCzQMeQ_c_c&fi=24056602&pid=183-159-180-160&up='
if(!url) return false
let name = (body.match(/(?<="md">)[^<]+/) || [''])[0].replace(/\s*$/,'')
data.url = url
data.name = filterExt(name)
data.$cached_at = Date.now()
return data
}
return { name , label:'蓝奏云',version, drive:{ protocols, folder , file } }
}