Skip to content

Commit

Permalink
feat: support zero config
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Dec 2, 2020
1 parent 9f61029 commit a79eef1
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 11 deletions.
8 changes: 7 additions & 1 deletion app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ const db = createFiledb(configPath , {raw:true} , {

proxy_server:'',

ocr_server:'https://api.reruin.net/ocr'
ocr_server:'https://api.reruin.net/ocr',

smb_server_enable: false,

smb_server_port:8445,

smb_anonymous_enable:true
});

if(process.env.PORT){
Expand Down
20 changes: 18 additions & 2 deletions app/controllers/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const handlers = async (a, body , ctx) => {
cache.clear()
result.message = 'Success'
} else if (a == 'cfg') {
let { proxy_enable, preview_enable, readme_enable, max_age_dir, max_age_file,max_age_download, webdav_path, anonymous_uplod_enable, ignore_file_extensions , ignore_paths , custom_style , custom_script , proxy_paths , proxy_server , ocr_server , language, anonymous_download, index_enable } = body
let { proxy_enable, preview_enable, readme_enable, max_age_dir, max_age_file,max_age_download, webdav_path, anonymous_uplod_enable, ignore_file_extensions , ignore_paths , custom_style , custom_script , proxy_paths , proxy_server , ocr_server , language, anonymous_download, index_enable, smb_server_enable, smb_server_port, smb_anonymous_enable } = body
let opts = {}
if (max_age_dir !== undefined) {
max_age_dir = parseInt(max_age_dir)
Expand Down Expand Up @@ -132,7 +132,24 @@ const handlers = async (a, body , ctx) => {
//console.log(ctx,ctx.__setLocale)
ctx.__setLocale(language)
}

if (smb_server_enable) {
smb_server_enable = smb_server_enable == '1' ? 1 : 0
opts.smb_server_enable = smb_server_enable
}

if( smb_server_port ){
smb_server_port = parseInt(smb_server_port)
if (!isNaN(smb_server_port)) {
opts.smb_server_port = smb_server_port
}
}

if (smb_anonymous_enable) {
smb_anonymous_enable = smb_anonymous_enable == '1' ? 1 : 0
opts.smb_anonymous_enable = smb_anonymous_enable
}

opts.custom_script = custom_script
opts.custom_style = custom_style
opts.ignore_paths = config.getConfig('ignore_paths')
Expand All @@ -142,7 +159,6 @@ const handlers = async (a, body , ctx) => {
opts.proxy_server = proxy_server
opts.anonymous_download = anonymous_download
opts.ocr_server = ocr_server || ''

await config.save(opts)
result.message = 'Success'
}
Expand Down
10 changes: 8 additions & 2 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ const routers = require('./routers/index')
const cors = require('@koa/cors')
const config = require('./config')

const pluginLoad = require('./services/plugin').load
const { loader , bonjour } = require('./services/plugin')
const fs = require('fs')

// const proxy = require('./utils/proxy')

const app = new Koa()
app.proxy = true
pluginLoad({

loader('plugin', {
dirs: [__dirname + '/plugins',path.resolve(__dirname,'../plugins')],
})

loader('endpoints')

onerror(app)

locales(app, {
Expand Down Expand Up @@ -87,4 +91,6 @@ app.use(async (ctx) => {
}
})

bonjour.publish({'name':'Sharelist',type:"http",port:config.getConfig('port')})

module.exports = app
3 changes: 2 additions & 1 deletion app/middleware/koa-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const xml2js = ( xml , options = {}) => {
}

const guessWebDAV = (ua) => {
return /(Microsoft\-WebDAV|FileExplorer|WinSCP|WebDAVLib|WebDAVFS|rclone)/i.test(ua)
return /(Microsoft\-WebDAV|FileExplorer|WinSCP|WebDAVLib|WebDAVFS|rclone|Kodi)/i.test(ua)
}

const webdavMethods = ['options','head','trace','get','put','post','delete','mkcol','propfind','proppatch','copy','move','lock','unlock']
Expand Down Expand Up @@ -54,6 +54,7 @@ const parseConfig = (str) => {
return ret
}
module.exports = async(ctx, next) => {
console.log(ctx.request.headers['user-agent'])
if (!ctx.session.access) {
ctx.session.access = new Set()
}
Expand Down
30 changes: 29 additions & 1 deletion app/services/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { sendFile , sendHTTPFile ,sendStream, getFile, getHTTPFile } = require('.
const wrapReadableStream = require('../utils/wrapReadableStream')
const rectifier = require ('../utils/rectifier')
const chunkStream = require('../utils/chunkStream')
const bonjour = require('bonjour')()

const assign = (...rest) => Object.assign(...rest)

Expand Down Expand Up @@ -400,8 +401,35 @@ const load = (options) => {
ready = true
}

var endpoints = []
const loader = (type, ...rest) => {
if( type == 'plugin' ){
load(...rest)
}else{
const files = fs.readdirSync(path.resolve(__dirname,'../../'+type))
const services = {}
services.command = command
services.setRuntime = config.setRuntime
services.getConfig = config.getConfig
services.bonjour = bonjour

for (let i = 0; i<files.length;i++) {
let filepath = path.join(path.resolve(__dirname,'../../'+type),files[i])
let instance = new (require(filepath))(services)
let name = instance.name || (type+'_' + files[i].replace(/\./g,'_'))
endpoints.push( instance )
console.log(`Load ${type}: ${name}`)
}
}
}

const reload = () => {
load(loadOptions)
if( endpoints ){
for(let endpoint of endpoints){
endpoint.restart()
}
}
}
/**
* 根据协议获取可处理的驱动
Expand Down Expand Up @@ -599,4 +627,4 @@ const createWriteStream = async (options) => {
}
}

module.exports = { load , reload , getDrive , getStream , getSource , updateFolder , updateFile , updateLnk , getVendors , getAuth , getPreview , isPreviewable , command}
module.exports = { load , reload , getDrive , getStream , getSource , updateFolder , updateFile , updateLnk , getVendors , getAuth , getPreview , isPreviewable , command , loader , bonjour}
19 changes: 19 additions & 0 deletions app/views/default/manage.pug
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ block content
label= __('webdav_path')
.col-sm-8
input.form-control(type='text', name='webdav_path' , value=config.webdav_path,required)
.form-group
.col-sm-4
label= __('smb_server_enable')
.col-sm-8
select.form-control(name='smb_server_enable',value=config.smb_server_enable,required)
option(value='1', selected=config.smb_server_enable==1 ? 'selected' : null)= __('enable')
option(value='0', selected=config.smb_server_enable==0 ? 'selected' : null)= __('disable')
.form-group
.col-sm-4
label= __('smb_server_port')
.col-sm-8
input.form-control(type='text', name='smb_server_port' , value=config.smb_server_port,required)
.form-group
.col-sm-4
label= __('smb_anonymous_enable')
.col-sm-8
select.form-control(name='smb_anonymous_enable',value=config.smb_anonymous_enable,required)
option(value='1', selected=config.smb_anonymous_enable==1 ? 'selected' : null)= __('enable')
option(value='0', selected=config.smb_anonymous_enable==0 ? 'selected' : null)= __('disable')
.form-group
.col-sm-4
label= __('ocr_server')
Expand Down
21 changes: 21 additions & 0 deletions docs/zh-cn/advance.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ ShareList支持将请求发送到多个对等的网盘,实现负载均衡。
***
## Zeroconf
ShareList支持向其他系统申明服务。包括:
1. Sharelist Web站点
2. Sharelist WebDAV(Emby Zeroconf 浏览器可用)
***
## SMB(实验功能)
ShareList尝试支持将挂载内容以SMB服务形式分享,需在后台管理处启用。暂只支持SMB1.0 且为只读模式。
默认的SMB连接信息
```
端口: 8445
路径: SL
用户名: admin
密码:后台管理密码
```
***
## Nginx(Caddy)反向代理
使用反代时,请添加以下配置。
Expand Down
9 changes: 9 additions & 0 deletions docs/zh-cn/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ ShareList 也支持配置多个中转服务器,只需用```;```分割即可,
#### WebDAV路径
ShareList支持WebDAV导出,此处可设置访问路径。

#### SMB
ShareList支持SMB导出,此处可设置是否启用此功能。[详细使用](zh-cn/advance?id=SMB)

#### SMB 端口
设置SMB 端口,默认8445。

#### SMB 匿名访问
设置SMB是否支持匿名访问,默认支持。

#### 验证码识别接口
设置打码接口。

Expand Down
91 changes: 91 additions & 0 deletions endpoints/smb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const SMBServer = require('@sharelist/node-smb-server')
const lm = require('@sharelist/node-smb-server/lib/ntlm').lm
const ntlm = require('@sharelist/node-smb-server/lib/ntlm').ntlm

const getDefaultConfig = (app) => {
let port = app.getConfig('smb_server_port') || 8445
let allowAnonymous = !!app.getConfig('smb_anonymous_enable') || true
let token = app.getConfig('token')
let users = {
"admin" : {
"lmHash" : lm.createHash(token).toString('hex'),
"ntlmHash" : ntlm.createHash(token).toString('hex')
}
}
if( allowAnonymous ){
users.guest = {
"lmHash" : "aad3b435b51404eeaad3b435b51404ee",
"ntlmHash" : "31d6cfe0d16ae931b73c59d7e0c089c0"
}
}
return {
"listen": {
"port": port,
"host": "0.0.0.0"
},
"smb2Support": false,
"domainName": "WORKGROUP",
"extendedSecurity": true,
"allowAnonymous":allowAnonymous,
"users": users,
"shares": {
"SL": {
"backend": "sharelist",
"description": "sharelist share",
"instance":app
}
}
}
}
class SMB {
constructor(app) {
this.name = 'SMBServer'
this.app = app
this.start()
}

start(){
let enable = this.app.getConfig('smb_server_enable')
if( enable ){
let config = getDefaultConfig(this.app)
this.config = config
this.server = new SMBServer(config, null)

this.server.start(config.listen.port,config.listen.host, () => {
this.onStart()
});

this.server.on('error', (err) => {
this.onError(err)
})
}
}

onStart(){
console.log('smb is running at :'+this.config.listen.port)
}

onError(err){
if (err) {
console.error('error during startup %s', err.message);
}
}

restart(){
try{
if( this.server ){
this.server.stop(() => {
console.log('smb has closed')
this.server = null
this.start()
})
}else{
this.start()
}
}catch(e){
console.log(e)
}
}
}

module.exports = SMB
26 changes: 26 additions & 0 deletions endpoints/webdav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

class WebDAV {
constructor(app) {
this.name = 'WebDAVServer'
this.app = app
this.start()
}

start(){
let { app } = this
let port = app.getConfig('port')
let path = app.getConfig('webdav_path')

this.zeroconf = app.bonjour.publish({ name: 'ShareList WebDAV', type: 'webdav', port , txt: { path:'/webdav' } })
}

restart(){
if( this.zeroconf ){
this.app.bonjour.unpublish(this.zeroconf)
this.zeroconf = null
}
this.start()
}
}

module.exports = WebDAV
5 changes: 4 additions & 1 deletion locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ module.exports = {
"signout":"Sign Out",
"anonymous_download":"Anonymous download rules",
"anonymous_download_placeholder":"Match regular expression, leave blank to allow downloading any files",
"index_enable":"Index"
"index_enable":"Index",
"smb_server_enable":"SMB Server",
"smb_server_port":"SMB Server Port",
"smb_anonymous_enable":"SMB Allow Anonymous"
}
5 changes: 4 additions & 1 deletion locales/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ module.exports = {
"signout":"登出",
"anonymous_download":"下载限制",
"anonymous_download_placeholder":"匹配正则表达式,留空表示允许下载任意文件。",
"index_enable":"目录索引"
"index_enable":"目录索引",
"smb_server_enable":"SMB 服务",
"smb_server_port":"SMB 端口",
"smb_anonymous_enable":"SMB 允许匿名访问"
}
5 changes: 4 additions & 1 deletion locales/zh-TW.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ module.exports = {
"signout":"登出",
"anonymous_download":"匿名下载限制",
"anonymous_download_placeholder":"匹配正則表達式,留空表示允許下載任意文件。",
"index_enable":"目錄索引"
"index_enable":"目錄索引",
"smb_server_enable":"SMB 服務",
"smb_server_port":"SMB 端口",
"smb_anonymous_enable":"SMB 允许匿名瀏覽"
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
},
"dependencies": {
"@koa/cors": "^2.2.1",
"@sharelist/node-smb-server": "^0.0.1",
"bonjour": "^3.5.0",
"iconv-lite": "^0.5.0",
"koa": "^2.2.0",
"koa-body": "^4.1.1",
Expand All @@ -28,7 +30,7 @@
"koa-views": "^6.2.1",
"less-middleware": "^2.2.1",
"lowdb": "^1.0.0",
"markdown-it": "^10.0.0",
"markdown-it": "^12.0.2",
"mime": "^2.4.2",
"node-rsa": "^1.1.1",
"pug": "^2.0.4",
Expand Down
Loading

0 comments on commit a79eef1

Please sign in to comment.