From a9dd72b901f3a2bac959892b4d0cf48fccd990ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 18 Sep 2020 18:25:56 +0800 Subject: [PATCH] init --- .gitignore | 9 ++++++ LICENSE | 21 ++++++++++++ Readme.md | 12 +++++++ lib/native-store.js | 79 +++++++++++++++++++++++++++++++++++++++++++++ lib/redis-store.js | 79 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 11 +++++++ session.js | 66 +++++++++++++++++++++++++++++++++++++ 7 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Readme.md create mode 100644 lib/native-store.js create mode 100644 lib/redis-store.js create mode 100644 package.json create mode 100644 session.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7c24ca8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride +._* +.idea +.vscode diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ab60297 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..77191b0 --- /dev/null +++ b/Readme.md @@ -0,0 +1,12 @@ +![module info](https://nodei.co/npm/@gm5/controller.png?downloads=true&downloadRank=true&stars=true) + +# @gm5/controller + +> 控制器基类。 + +## 安装 + +```bash +npm install @gm5/controller +``` + diff --git a/lib/native-store.js b/lib/native-store.js new file mode 100644 index 0000000..b52f9a5 --- /dev/null +++ b/lib/native-store.js @@ -0,0 +1,79 @@ +/** + * 内存版会话管理(仅用于测试,数据) + * @author yutent + * @date 2020/09/18 16:35:26 + */ + +function hideProperty(host, name, value) { + Object.defineProperty(host, name, { + value: value, + writable: true, + enumerable: false, + configurable: true + }) +} + +export default class Session { + constructor(store, opt, uuid) { + this.opt = opt + this.uuid = uuid + this.store = store + } + + createSsid(ssid) { + if (ssid) { + if (!this.opt.jwt && this.opt.level > 0 && ssid !== this.uuid) { + ssid = this.uuid + } + } else { + ssid = this.uuid + } + this.ssid = ssid + + if ( + !this.store.hasOwnProperty(ssid) || + this.store[ssid].__EXPIRES__ < Date.now() + ) { + this.store[ssid] = {} + } + //设置session有效期 + hideProperty( + this.store[ssid], + '__EXPIRES__', + Date.now() + this.opt.ttl * 1000 + ) + } + + start(ssid) { + this.createSsid(ssid) + return this.ssid + } + + // 获取session字段值 + get(key) { + return key ? this.store[this.ssid][key] || null : this.store[this.ssid] + } + + // 设置session字段值 + set(key, val) { + if (typeof key === 'object') { + for (let i in key) { + this.store[this.ssid][i] = key[i] + } + } else { + this.store[this.ssid][key] = val + } + } + + // 删除单个字段 + unset(key) { + if (this.store[this.ssid].hasOwnProperty(key)) { + delete this.store[this.ssid][key] + } + } + + // 清除个人session + clear() { + this.store[this.ssid] = {} + } +} diff --git a/lib/redis-store.js b/lib/redis-store.js new file mode 100644 index 0000000..8896c85 --- /dev/null +++ b/lib/redis-store.js @@ -0,0 +1,79 @@ +/** + * redis版会话管理 + * @author yutent + * @date 2020/09/18 16:35:26 + */ + +export default class Session { + constructor(store, opt, uuid) { + this.store = store + this.opt = opt + this.uuid = uuid + } + + createSsid(ssid) { + if (ssid) { + if (!this.opt.jwt && this.opt.level > 0 && ssid !== this.uuid) { + ssid = this.uuid + } + } else { + ssid = this.uuid + } + + this.ssid = ssid + // 设置session有效期 + this.store.expire(ssid, this.opt.ttl) + } + + start(ssid) { + this.createSsid(ssid) + return this.ssid + } + + // 获取session字段值, 需要await指令 + get(key) { + var defer = Promise.defer() + + this.store.hgetall(this.ssid, (err, obj) => { + if (err) { + return defer.reject(err) + } + + for (let i in obj) { + if (!obj[i]) { + continue + } + + obj[i] = Number.parse(obj[i]) + } + //不传key时,直接返回全部字段 + if (!key) { + defer.resolve(obj) + } else { + defer.resolve(obj.hasOwnProperty(key) ? obj[key] : null) + } + }) + return defer.promise + } + + //设置session字段值 + set(key, val) { + if (typeof key === 'object') { + for (let i in key) { + this.store.hset(this.ssid, i, key[i]) + } + } else { + this.store.hset(this.ssid, key, val) + } + } + + //删除单个字段 + unset(key) { + this.store.hdel(this.ssid, key) + } + + //清除个人session + clear() { + this.store.del(this.ssid) + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..dff7a05 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "@gm5/session", + "version": "1.0.0", + "type": "module", + "description": "会话中间件。", + "main": "index.js", + "author": "yutent", + "keywords": ["fivejs", "controller", "http"], + "repository": "https://github.com/bytedo/gmf.session.git", + "license": "MIT" +} diff --git a/session.js b/session.js new file mode 100644 index 0000000..8ae5b8d --- /dev/null +++ b/session.js @@ -0,0 +1,66 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2018-07-26 15:50:25 + * @version $Id$ + */ +import redisStore from './lib/redis-store.js' +import nativeStore from './lib/native-store.js' +import { uuid, sha1 } from 'crypto.js' + +export default function(req, res, next) { + var opt = this.get('session') + var jwt = this.get('jwt') + var cookie = this.ins('cookie') + var session = null + var deviceID = uuid() + var ssid = '' + + opt.jwt = jwt + + if (req.method === 'OPTIONS') { + return next() + } + + if (jwt) { + var auth = req.header('authorization') + if (auth) { + ssid = auth.split('.').pop() + deviceID = auth + } + } else { + ssid = cookie('NODESSID') + // 校验级别为1, 则混入ua + if (opt.level > 0) { + deviceID += req.header('user-agent') + } + // 校验级别为2, 则混入ip + if (opt.level > 1) { + deviceID += req.ip() + } + } + deviceID = sha1(deviceID) + + if (opt.type === 'redis') { + session = new redisStore(this.__SESSION_STORE__, opt, deviceID) + } else { + session = new nativeStore(this.__SESSION_STORE__, opt, deviceID) + } + + // 启用SESSION + // ssid非法或过期时,需要重写 + if (!ssid || ssid !== session.start(ssid)) { + ssid = session.start(ssid) + if (!jwt) { + cookie('NODESSID', ssid, { + httpOnly: true, + expires: opt.ttl, + domain: opt.domain + }) + } + } + + this.__INSTANCE__.session = session + + next() +}