From 075b68012e975ba98a48a4880509ebf406e1a090 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 25 Jun 2018 18:07:10 +0800 Subject: [PATCH] first --- .gitignore | 3 +++ Dockerfile | 10 ++++++++++ app.js | 15 +++++++++++++++ config/index.js | 7 +++++++ index.js | 7 +++++++ libs/middleware/index.js | 15 +++++++++++++++ package.json | 23 +++++++++++++++++++++++ route/index.js | 6 ++++++ route/line/index.js | 34 ++++++++++++++++++++++++++++++++++ 9 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 app.js create mode 100644 config/index.js create mode 100644 index.js create mode 100644 libs/middleware/index.js create mode 100644 package.json create mode 100644 route/index.js create mode 100644 route/line/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5693cd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +.env +package-lock.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b07ac33 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM node:8 +LABEL maintainer="Jay " +ENV NODE_PORT 5111 +RUN mkdir -p /data +WORKDIR /data +COPY . . +RUN rm .env +RUN npm install +EXPOSE ${NODE_PORT} +CMD ["npm", "start"] \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..ab9bc1e --- /dev/null +++ b/app.js @@ -0,0 +1,15 @@ +const config = require('./config') +const Koa = require('koa') +const koaLogger = require('koa-logger') +const router = require('./route') +const app = new Koa() + +app.use(koaLogger()) +app.use(router.allowedMethods()) +app.use(router.routes()) + +const server = app.listen(config.port, () => { + console.log(`server start on port ${server.address().port}`) +}) + +module.exports = server diff --git a/config/index.js b/config/index.js new file mode 100644 index 0000000..03d5e16 --- /dev/null +++ b/config/index.js @@ -0,0 +1,7 @@ +module.exports = { + port: process.env.NODE_PORT || 10230, + line: { + secret: process.env.LINE_SECRET || '', + access: process.env.LINE_ACCESS || '' + } +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..40b60f9 --- /dev/null +++ b/index.js @@ -0,0 +1,7 @@ +const fs = require('fs') +const path = require('path') +try { + fs.accessSync(path.resolve(__dirname, '.env')) + require('dotenv').config() +} catch (err) {} +require('./app') diff --git a/libs/middleware/index.js b/libs/middleware/index.js new file mode 100644 index 0000000..0674f73 --- /dev/null +++ b/libs/middleware/index.js @@ -0,0 +1,15 @@ +const config = require('../../config') +const crypto = require('crypto') + +const verifyLine = async (c, n) => { + if (!('request' in c) || !('raw' in c.request)) return c.throw(400, 'body not found') + let xSignature = c.get('x-line-signature') || '' + if (typeof xSignature !== 'string' || xSignature.length === 0) return c.throw(400, 'signature not found') + let signature = crypto.createHmac('SHA256', config.line.secret).update(c.request.raw).digest('base64') + if (signature !== xSignature) return c.throw(403, 'signature not match') + return n() +} + +module.exports = { + verifyLine +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..4f79385 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "mtfosbot", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node index.js 2>&1 | tee runtime.txt", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "JayChen", + "license": "ISC", + "dependencies": { + "dotenv": "^6.0.0", + "koa": "^2.5.1", + "koa-body": "^4.0.3", + "koa-logger": "^3.2.0", + "koa-router": "^7.4.0" + }, + "devDependencies": { + "standard": "^11.0.1" + } +} diff --git a/route/index.js b/route/index.js new file mode 100644 index 0000000..75c6dc4 --- /dev/null +++ b/route/index.js @@ -0,0 +1,6 @@ +const Router = require('koa-router') +const r = new Router() + +r.use('/line', require('./line').routes()) + +module.exports = r diff --git a/route/line/index.js b/route/line/index.js new file mode 100644 index 0000000..7929354 --- /dev/null +++ b/route/line/index.js @@ -0,0 +1,34 @@ +const Router = require('koa-router') +const r = new Router() +// const koaBody = require('koa-body') +const rawBody = require('raw-body') +const { + verifyLine +} = require('../../libs/middleware') + +const getRaw = async (c, n) => { + let raw = await rawBody(c.req, { + length: c.request.length, + limit: '5mb', + encoding: c.request.charset + }) + c.request.raw = raw + let txt = raw instanceof Buffer ? raw.toString() : raw + if (c.request.type === 'application/json') { + try { + c.request.body = JSON.parse(txt) + } catch (err) { + c.request.body = txt + } + } + return n() +} + +r.post('/', getRaw, verifyLine, async (c, n) => { + console.log(c.request.body) + if (!('event' in c.request.body)) return c.throw(400, 'data struct error') + c.body = 'success' + c.status = 200 +}) + +module.exports = r