Skip to content

Commit

Permalink
+ enhanced encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed May 21, 2020
1 parent baaf4d7 commit b74ab06
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 48 deletions.
6 changes: 3 additions & 3 deletions app/controllers/sharelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const isProxyPath = (path , paths) => {

const output = async (ctx , data)=>{

const isPreview = ctx.request.querystring.indexOf('preview') >= 0
const isPreview = ctx.runtime.isPreview

const isforward = ctx.request.querystring.indexOf('forward') >= 0
const isforward = ctx.runtime.isForward

const downloadLinkAge = config.getConfig('max_age_download')

Expand Down Expand Up @@ -190,7 +190,7 @@ module.exports = {
})
}
else if(data.type == 'auth_response'){
let result = {status:0 , message:"success"}
let result = {status:0 , message:"success" , rurl:ctx.query.rurl}
if(!data.result){
result.status = 403
result.message = '验证失败'
Expand Down
3 changes: 3 additions & 0 deletions app/middleware/koa-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const parseConfig = (str) => {
if(params.has('preview')){
ret.isPreview = true
}
if(params.has('forward')){
ret.isForward = true
}
if(params.has('sort')){
let s = params.get('sort')
let r = {}
Expand Down
17 changes: 13 additions & 4 deletions app/plugins/cmd.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,26 @@ module.exports = ({ cache , getVendor , getConfig , getRuntime , updateFolder ,

let hit = root()
let paths = (p == '' || p == '/') ? [] : p.replace(/^\//,'').split('/').map(i => decodeURIComponent(i))
let idx = paths.length
let isRoot = idx == 0
let idx = 0
let isRoot = paths.length == 0

//逆向查询节点
//逆向查询节点 path -> filemeta { protocol , id , type } -> filedata
while( idx >= 0 ){
let cur = '/' + paths.slice(0,idx).join('/')
let content = nodeCache[cur]
if( content ){
hit = content
//过滤器拦截
if(filter && filter(hit , paths.slice(0,idx+1))){
return hit;
}
idx--
break
}else{
idx--
}
}


if(hit.protocol == 'root'){
if( hit.children && hit.children.length == 1 ){
Expand All @@ -152,7 +157,6 @@ module.exports = ({ cache , getVendor , getConfig , getRuntime , updateFolder ,
continue
}


let vendor = getVendor(hit.protocol)

if (hit.lnk) {
Expand Down Expand Up @@ -202,6 +206,11 @@ module.exports = ({ cache , getVendor , getConfig , getRuntime , updateFolder ,
}
}

//过滤器拦截
if(filter && filter(hit , paths.slice(0,idx+1))){
return hit;
}

}

return hit
Expand Down
14 changes: 10 additions & 4 deletions app/services/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ var resourcesCount = 0
const recognize = async (image , type, lang) => {
let server = config.getConfig('ocr_server')
if(server){
let resp = await http.post(server,{ image , type, lang },{json:true})
if(resp.body){
let resp
try{
resp = await http.post(server,{ image , type, lang },{json:true})
}catch(e){
//console.log(e)
}

if(resp && resp.body){
return { error:false , result:resp.body.result}
}
}
Expand All @@ -64,10 +70,10 @@ const recognize = async (image , type, lang) => {
/*
* 根据文件id获取详情
*/
const getSource = async (id , driverName) => {
const getSource = async (id , driverName , data) => {
if(driveMap.has(driverName)){
let vendor = getDrive(driverName)
let d = await vendor.file(id , { req: config.getRuntime('req') } )
let d = await vendor.file(id , { req: config.getRuntime('req') , data } )
if(d.outputType === 'file'){
return await getFile(d.url)
}
Expand Down
76 changes: 56 additions & 20 deletions app/services/sharelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,35 @@ const format = require('../utils/format')
const { getDrive, getAuth, getStream , getSource, updateLnk, checkAuthority, updateFile, updateFolder , getPreview , isPreviewable , command } = require('./plugin')
// const wrapReqStream = require('../utils/wrapReqStream')

const diff = (a, b) => {
let ret = []
b.forEach((v, i) => {
if (v != a[i]) {
ret.push(v)
}
})
return ret
}

const requireAuth = (data) => !!(data.children && data.children.find(i=>(i.name == '.passwd')))

class ShareList {
constructor(root) {
this.passwdPaths = new Set()
}

async diff(a,b){
let ret = []
b.forEach((v, i) => {
if (v != a[i]) {
ret.push(v)
}
})
return ret
}

access(data){
return !!(data.children && data.children.find(i=>(i.name == '.passwd')))
}

searchPasswdPath(req){
for(let i = 1 ; i <= req.paths.length ; i++){
let path = '/'+req.paths.slice(0,i).join('/')
if( this.passwdPaths.has(path) && req.access.has(path) == false && !req.isAdmin){
return path
}
}

return null
}

async path(req) {
Expand Down Expand Up @@ -59,17 +73,39 @@ class ShareList {
}
}
else{
let data = await command('ls' , req.paths.join('/') , function(data){
if( requireAuth(data) && req.access.has(req.path) == false && !req.isAdmin) {
return true
let passwdPath = this.searchPasswdPath(req)
let targetPath = '/'+req.paths.join('/') , currentPath = ''
if(!passwdPath || passwdPath == targetPath || targetPath == '/'){
let currentPath
let data = await command('ls' , targetPath, (data , paths) => {
currentPath = '/' + paths.join('/')
if( this.access(data) && req.access.has(currentPath) == false) {
this.passwdPaths.add(currentPath)
return true
}
})
if(data.type == 'folder'){
if(currentPath && targetPath != currentPath){
return { type:'redirect', 'redirect':currentPath+'?rurl='+targetPath }
}else{
if( this.access(data) && req.access.has(req.path) == false && !req.isAdmin) {
this.passwdPaths.add(targetPath)
data.type = 'auth'
}else{
if(this.passwdPaths.has(targetPath)){
this.passwdPaths.delete(targetPath)
}
}
return data
}
}else{
return data
}
})

//管理员模式无需密码
if( requireAuth(data) && req.access.has(req.path) == false && !req.isAdmin) {
data.type = 'auth'

}else{
return { type:'redirect', 'redirect':passwdPath+'?rurl='+targetPath }
}
return data

}

}
Expand Down
6 changes: 5 additions & 1 deletion app/views/default/auth.pug
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ block content
success:function(resp){
$('button').removeClass('loading')
if(resp.status == 0){
location.reload()
if(resp.rurl){
location.href = resp.rurl
}else{
location.reload()
}
}else{
alert(resp.message)
}
Expand Down
28 changes: 18 additions & 10 deletions plugins/drive.189cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Manager {

async ocr(image){
let resp = await this.recognize(image,'189cloud')
let ret = { error:resp.error }
let ret = { error:resp.error , msg:resp.msg }
if(!resp.error){
let code = resp.result.replace(/[^a-z0-9]/i,'')
// retry
Expand Down Expand Up @@ -69,9 +69,11 @@ class Manager {
let hit = this.clientMap[data.username]
if(hit){
if( !hit.cookies || (Date.now() - hit.updated_at) > COOKIE_MAX_AGE ){
let result = await this.create(hit.username , hit.password)
let { result , msg } = await this.create(hit.username , hit.password)
if( result ){
hit = this.clientMap[data.username]
}else{
return { error : msg }
}
}
}
Expand Down Expand Up @@ -188,6 +190,7 @@ class Manager {
//服务不可用
if(error){
formdata.validateCode = ''
msg = '验证码识别接口无法使用'
break;
}
else if(code){
Expand Down Expand Up @@ -226,7 +229,6 @@ class Manager {
await this.updateHandle(this.stringify({username , password}))

this.clientMap[username] = client
console.log('cookies>>',cookies)
result = true
break;
}else{
Expand All @@ -235,9 +237,6 @@ class Manager {

}

if( needcaptcha && !formdata.validateCode){
msg = '请设置验证码识别接口!'
}
return { result , msg }
}

Expand All @@ -247,7 +246,7 @@ class Manager {
if(data.username){
let hit = this.clientMap[data.username]
if(hit){
await this.create(hit.username , hit.password)
return await this.create(hit.username , hit.password)
}
}
}
Expand Down Expand Up @@ -292,6 +291,10 @@ module.exports = ({ request, cache, getConfig, querystring, base64, saveDrive, g

let { path, cookies, username, error } = await manager.get(id)

if( error ){
return { id, type: 'folder', protocol: defaultProtocol,body: await install(error) }
}

if( cookies ) {

return { cookies , path , username }
Expand All @@ -318,8 +321,13 @@ module.exports = ({ request, cache, getConfig, querystring, base64, saveDrive, g
resp = await request({async:true,...rest})
//cookie失效
if(resp.headers['Content-Type'] && resp.headers['Content-Type'].includes('text/html')){
await manager.update(id)
continue
let { result , msg } = await manager.update(id)
if( result ){
resp = { msg }
break;
}else{
continue
}
}else{
break;
}
Expand Down Expand Up @@ -361,7 +369,7 @@ module.exports = ({ request, cache, getConfig, querystring, base64, saveDrive, g
})

if (!resp || !resp.body) {
return { id, type: 'folder', protocol: defaultProtocol,body:'解析错误' }
return { id, type: 'folder', protocol: defaultProtocol,body:resp.msg || '解析错误' }

}
let children = resp.body.data.map( file => {
Expand Down
15 changes: 9 additions & 6 deletions plugins/drive.caiyun.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ class Manager {
let hit = this.clientMap[data.username]
if(hit){
if( !hit.cookie || (Date.now() - hit.updated_at) > COOKIE_MAX_AGE ){
let result = await this.create(hit.username , hit.password)
let { result , msg } = await this.create(hit.username , hit.password)
if( result ){
hit = this.clientMap[data.username]
}else{
return { error: msg }
}
}
}
Expand Down Expand Up @@ -175,6 +177,7 @@ class Manager {
if(error){
console.log('服务不可用')
formdata.validateCode = ''
msg = '验证码识别服务不可用!'
break;
}
else if(code){
Expand Down Expand Up @@ -241,9 +244,6 @@ class Manager {
}
}

if( needcaptcha && !formdata.validate){
msg = '请设置验证码识别接口!'
}
return { result , msg }
}

Expand All @@ -253,7 +253,7 @@ class Manager {
if(data.username){
let hit = this.clientMap[data.username]
if(hit){
await this.create(hit.username , hit.password)
return await this.create(hit.username , hit.password)
}
}
}
Expand Down Expand Up @@ -340,8 +340,11 @@ module.exports = ({ request, cache, getConfig, querystring, base64, saveDrive, g

if (!predata.cookie) return predata

let { path, cookie , username } = await prepare(id)
let { path, cookie , username , error } = await prepare(id)

if(error){
return { id, type: 'folder', protocol: defaultProtocol,body:'异常:'+error }
}
let r = cache.get(id)
if (r) {
if (
Expand Down

0 comments on commit b74ab06

Please sign in to comment.