Skip to content

Commit

Permalink
+ auth
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Jul 2, 2018
1 parent 0db0fe7 commit a66bc7d
Show file tree
Hide file tree
Showing 14 changed files with 481 additions and 231 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
## 特性
1. 文件夹ID挂载网盘
2. 支持虚拟目录
在网盘内以如下格式重命名文件夹
格式:```显示名称.文件夹id.类型```
类型支持 od(OneDrive) gd(GoogleDrive)
在网盘内以如下格式重命名文件夹
格式:```显示名称.文件夹id.类型```
类型支持 od(OneDrive) gd(GoogleDrive)
3. 加密目录
在文件夹内新建 ```.密码.passwd``` 命名的文件即可,例如
```.123456.passwd```
不要省略最前方的```.```

## 安装
### Shell
Expand Down
5 changes: 5 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const koaStatic = require('koa-static')
const session = require('koa-session-minimal')

const less = require('./middleware/koa-less')
const addr = require('./middleware/koa-addr')
Expand All @@ -20,6 +21,10 @@ const app = new Koa()

onerror(app)

app.use(session({
key: 'USER_SID'
}))

app.use(cors())

app.use(bodyparser({
Expand Down
75 changes: 61 additions & 14 deletions controllers/sharelist.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
const service = require('./../models/index')
const base = require('../utils/base')
const http = require('../utils/http')
const encode = require('../utils/format').encode
const {encode , decode} = require('../utils/format')
const request = require('request')
const config =require('../config')
const cache = {}
const parse_path = require('../utils/base').parse_path

module.exports = {
const auth = ()=>{

}

const checkAuth = (data)=>{
let passwd = false
if(data.children){
let hit = data.children.find(i=>(i.ext == 'passwd'))
if(hit){
passwd = hit.name.replace(/\.passwd$/,'').substring(1)
}
}
return passwd
}

module.exports = {
async index(ctx){
let data = await service.path(ctx.paths , ctx.query , ctx.paths_raw)
let base_url = ctx.path == '/' ? '' : ctx.path
Expand All @@ -23,20 +38,28 @@ module.exports = {
//需要验证
}
else if(data.type == 'folder'){
let resp = data.children.map((i)=>{
let href = i.href || base.path(base_url+'/'+ encode(i.pathname|| i.name ))
let passwd = checkAuth(data)
if( passwd !== false && !ctx.session.access.has( data.id )){
await ctx.render('auth',{parent , id:data.id , name:decodeURIComponent(decode(ctx.paths[ctx.paths.length-1]))})

}else{
let resp = []
data.children.forEach((i)=>{
if(i.ext != 'passwd'){
let href = i.href || base.path(base_url+'/'+ encode(i.pathname|| i.name ))

if(['audio','video','image'].indexOf(i.type) >= 0){
href += (href.indexOf('?')>=0 ? '&' : '?') + 'preview'
}
return{
href , type: i.type, name: i.name
}
})
if(['audio','video','image'].indexOf(i.type) >= 0){
href += (href.indexOf('?')>=0 ? '&' : '?') + 'preview'
}
resp.push( { href , type : i.type , name:i.name})
}
})

await ctx.render('index',{
data:resp , base_url , parent
})
await ctx.render('index',{
data:resp , base_url , parent
})
}

}else{
let preview = ctx.request.querystring.indexOf('preview') >= 0
let download_url = data.url
Expand Down Expand Up @@ -77,6 +100,7 @@ module.exports = {
}

},

async api(ctx){
let token = ctx.params.token
if(token == config.data.token){
Expand All @@ -88,5 +112,28 @@ module.exports = {
ctx.body = []
}

},

async auth(ctx){
let { path , passwd } = ctx.request.body
let [paths , paths_raw] = parse_path(path.substring(1))

let data = await service.path(paths , ctx.query , paths_raw)
let hit = checkAuth(data)
let result = { status : 0 , message:''}

//需要验证
if( hit !== false && hit){
if( hit == passwd ){
ctx.session.access.add( data.id )
}else{
result.status = 403
result.message = '验证失败'
}
}else{
result.message = '此目录不需要验证'
}

ctx.body = result
}
}
26 changes: 9 additions & 17 deletions middleware/koa-paths.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
const decode = require('../utils/format').decode
const parse_path = require('../utils/base').parse_path

module.exports = async (ctx , next)=>{
let url = ctx.request.path.substring(1)
if(url){
let raw = url.split('/')
let paths = []
for(let i = 0 ; i< raw.length ; i++){
if( i == 0 || /[^!]$/.test(raw[i-1]) ){
paths.push(decode(raw[i].replace(/!$/,'')))
}
}
// let paths = raw.filter((i)=>(!/^!/.test(i)))
ctx.paths = paths
ctx.paths_raw = raw
}else{
ctx.paths = []
ctx.paths_raw = []
module.exports = async(ctx, next) => {
if (!ctx.session.access) {
ctx.session.access = new Set()
}
let url = ctx.request.path.substring(1)
let [paths, paths_raw] = parse_path(url)
ctx.paths = paths
ctx.paths_raw = paths_raw

await next()
}
File renamed without changes.
2 changes: 1 addition & 1 deletion models/googledrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const folder = async(id) => {
updated_at:format.datetime(i[10]),
size:format.byte(i[13]),
ext:i[44],
type : i[13] ? base.mime_type(i[44]) : 'folder',
type : i[3].indexOf('.folder')>=0 ? 'folder' : base.mime_type(i[44]),
provider:'gd'
} , format.ln(i[2]))
}) : []
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "Apache-2.0",
"scripts": {
"start": "set NODE_ENV=dev && node bin/www",
"dev": "./node_modules/.bin/nodemon app",
"dev": "./node_modules/.bin/nodemon bin/www",
"prd": "pm2 start bin/www --env prod",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -18,6 +18,7 @@
"koa-logger": "^2.0.1",
"koa-onerror": "^1.2.1",
"koa-router": "^7.1.1",
"koa-session-minimal": "^3.0.4",
"koa-static": "^3.0.0",
"koa-views": "^5.2.1",
"less-middleware": "^2.2.1",
Expand Down
Loading

0 comments on commit a66bc7d

Please sign in to comment.