From 0a994e5b9885265873e02b3b3ab9add7ec7e7e6b Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 13 Nov 2017 19:58:29 +0900 Subject: [PATCH] Add access log widget --- locales/en.yml | 3 + locales/ja.yml | 3 + src/api/stream/requests.ts | 19 ++++ src/api/streaming.ts | 6 ++ src/log-request.ts | 21 +++++ src/server.ts | 6 ++ src/web/app/common/mixins/index.ts | 2 + .../common/scripts/requests-stream-manager.ts | 12 +++ src/web/app/common/scripts/requests-stream.ts | 14 +++ .../desktop/tags/home-widgets/access-log.tag | 93 +++++++++++++++++++ src/web/app/desktop/tags/home.tag | 1 + src/web/app/desktop/tags/index.ts | 1 + 12 files changed, 181 insertions(+) create mode 100644 src/api/stream/requests.ts create mode 100644 src/log-request.ts create mode 100644 src/web/app/common/scripts/requests-stream-manager.ts create mode 100644 src/web/app/common/scripts/requests-stream.ts create mode 100644 src/web/app/desktop/tags/home-widgets/access-log.tag diff --git a/locales/en.yml b/locales/en.yml index 574af26a6..d464b2fe2 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -390,6 +390,9 @@ desktop: post: "Post" placeholder: "What's happening?" + mk-access-log-home-widget: + title: "Access log" + mk-repost-form: quote: "Quote..." cancel: "Cancel" diff --git a/locales/ja.yml b/locales/ja.yml index 9e6251d0d..5c6f8e38e 100644 --- a/locales/ja.yml +++ b/locales/ja.yml @@ -390,6 +390,9 @@ desktop: post: "投稿" placeholder: "いまどうしてる?" + mk-access-log-home-widget: + title: "アクセスログ" + mk-repost-form: quote: "引用する..." cancel: "キャンセル" diff --git a/src/api/stream/requests.ts b/src/api/stream/requests.ts new file mode 100644 index 000000000..2c36e58b6 --- /dev/null +++ b/src/api/stream/requests.ts @@ -0,0 +1,19 @@ +import * as websocket from 'websocket'; +import Xev from 'xev'; + +const ev = new Xev(); + +export default function homeStream(request: websocket.request, connection: websocket.connection): void { + const onRequest = request => { + connection.send(JSON.stringify({ + type: 'request', + body: request + })); + }; + + ev.addListener('request', onRequest); + + connection.on('close', () => { + ev.removeListener('request', onRequest); + }); +} diff --git a/src/api/streaming.ts b/src/api/streaming.ts index 0e512fb21..6caf7db3e 100644 --- a/src/api/streaming.ts +++ b/src/api/streaming.ts @@ -9,6 +9,7 @@ import isNativeToken from './common/is-native-token'; import homeStream from './stream/home'; import messagingStream from './stream/messaging'; import serverStream from './stream/server'; +import requestsStream from './stream/requests'; import channelStream from './stream/channel'; module.exports = (server: http.Server) => { @@ -27,6 +28,11 @@ module.exports = (server: http.Server) => { return; } + if (request.resourceURL.pathname === '/requests') { + requestsStream(request, connection); + return; + } + // Connect to Redis const subscriber = redis.createClient( config.redis.port, config.redis.host); diff --git a/src/log-request.ts b/src/log-request.ts new file mode 100644 index 000000000..e431aa271 --- /dev/null +++ b/src/log-request.ts @@ -0,0 +1,21 @@ +import * as crypto from 'crypto'; +import * as express from 'express'; +import * as proxyAddr from 'proxy-addr'; +import Xev from 'xev'; + +const ev = new Xev(); + +export default function(req: express.Request) { + const ip = proxyAddr(req, () => true); + + const md5 = crypto.createHash('md5'); + md5.update(ip); + const hashedIp = md5.digest('hex').substr(0, 3); + + ev.emit('request', { + ip: hashedIp, + method: req.method, + hostname: req.hostname, + path: req.originalUrl + }); +} diff --git a/src/server.ts b/src/server.ts index 3e9bd44ee..9cf44eb0d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -11,6 +11,7 @@ import * as morgan from 'morgan'; import Accesses from 'accesses'; import vhost = require('vhost'); +import log from './log-request'; import config from './conf'; /** @@ -35,6 +36,11 @@ app.use(morgan(process.env.NODE_ENV == 'production' ? 'combined' : 'dev', { stream: config.accesslog ? fs.createWriteStream(config.accesslog) : null })); +app.use((req, res, next) => { + log(req); + next(); +}); + // Drop request when without 'Host' header app.use((req, res, next) => { if (!req.headers['host']) { diff --git a/src/web/app/common/mixins/index.ts b/src/web/app/common/mixins/index.ts index 45427fb9d..a11bfa7b6 100644 --- a/src/web/app/common/mixins/index.ts +++ b/src/web/app/common/mixins/index.ts @@ -3,6 +3,7 @@ import * as riot from 'riot'; import activateMe from './i'; import activateApi from './api'; import ServerStreamManager from '../scripts/server-stream-manager'; +import RequestsStreamManager from '../scripts/requests-stream-manager'; export default (me, stream) => { activateMe(me); @@ -11,4 +12,5 @@ export default (me, stream) => { (riot as any).mixin('stream', { stream }); (riot as any).mixin('server-stream', { serverStream: new ServerStreamManager() }); + (riot as any).mixin('requests-stream', { requestsStream: new RequestsStreamManager() }); }; diff --git a/src/web/app/common/scripts/requests-stream-manager.ts b/src/web/app/common/scripts/requests-stream-manager.ts new file mode 100644 index 000000000..44db913e7 --- /dev/null +++ b/src/web/app/common/scripts/requests-stream-manager.ts @@ -0,0 +1,12 @@ +import StreamManager from './stream-manager'; +import Connection from './requests-stream'; + +export default class RequestsStreamManager extends StreamManager { + public getConnection() { + if (this.connection == null) { + this.connection = new Connection(); + } + + return this.connection; + } +} diff --git a/src/web/app/common/scripts/requests-stream.ts b/src/web/app/common/scripts/requests-stream.ts new file mode 100644 index 000000000..325224587 --- /dev/null +++ b/src/web/app/common/scripts/requests-stream.ts @@ -0,0 +1,14 @@ +'use strict'; + +import Stream from './stream'; + +/** + * Requests stream connection + */ +class Connection extends Stream { + constructor() { + super('requests'); + } +} + +export default Connection; diff --git a/src/web/app/desktop/tags/home-widgets/access-log.tag b/src/web/app/desktop/tags/home-widgets/access-log.tag new file mode 100644 index 000000000..a14857756 --- /dev/null +++ b/src/web/app/desktop/tags/home-widgets/access-log.tag @@ -0,0 +1,93 @@ + + +

%i18n:desktop.tags.mk-access-log-home-widget.title%

+
+
+

+ { ip } + { method } + { path } +

+
+ + +
diff --git a/src/web/app/desktop/tags/home.tag b/src/web/app/desktop/tags/home.tag index fd286851d..88d06d2ba 100644 --- a/src/web/app/desktop/tags/home.tag +++ b/src/web/app/desktop/tags/home.tag @@ -20,6 +20,7 @@ + diff --git a/src/web/app/desktop/tags/index.ts b/src/web/app/desktop/tags/index.ts index 15677471c..8b5a52d67 100644 --- a/src/web/app/desktop/tags/index.ts +++ b/src/web/app/desktop/tags/index.ts @@ -43,6 +43,7 @@ require('./home-widgets/slideshow.tag'); require('./home-widgets/channel.tag'); require('./home-widgets/timemachine.tag'); require('./home-widgets/post-form.tag'); +require('./home-widgets/access-log.tag'); require('./timeline.tag'); require('./messaging/window.tag'); require('./messaging/room-window.tag');