From 54f182c08d67e9ecf63dd5d9878f4d9f0aa4d255 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 14 Jan 2017 10:51:48 +0900 Subject: [PATCH] Clean up proxy codes Closes #35 --- src/config.ts | 2 -- src/server.ts | 1 - src/web/service/proxy/proxy.ts | 31 ------------------------------- src/web/service/proxy/server.ts | 17 ----------------- src/web/service/url-preview.ts | 4 +++- 5 files changed, 3 insertions(+), 52 deletions(-) delete mode 100644 src/web/service/proxy/proxy.ts delete mode 100644 src/web/service/proxy/server.ts diff --git a/src/config.ts b/src/config.ts index d6b1af0e3..7c6ecaab2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -58,7 +58,6 @@ interface Mixin { auth_url: string; dev_url: string; drive_url: string; - proxy_url: string; } export type IConfig = ISource & Mixin; @@ -90,7 +89,6 @@ export default (path: string) => { mixin.auth_url = `${mixin.scheme}://auth.${mixin.host}`; mixin.dev_url = `${mixin.scheme}://dev.${mixin.host}`; mixin.drive_url = `${mixin.secondary_scheme}://file.${mixin.secondary_host}`; - mixin.proxy_url = `${mixin.secondary_scheme}://proxy.${mixin.secondary_host}`; return Object.assign(config || {}, mixin) as IConfig; }; diff --git a/src/server.ts b/src/server.ts index 421ad60c1..5ab6f3be5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -29,7 +29,6 @@ app.use((req, res, next) => { app.use(vhost(`api.${config.host}`, require('./api/server'))); app.use(vhost(config.secondary_host, require('./himasaku/server'))); app.use(vhost(`file.${config.secondary_host}`, require('./file/server'))); -app.use(vhost(`proxy.${config.secondary_host}`, require('./web/service/proxy/server'))); app.use(require('./web/server')); /** diff --git a/src/web/service/proxy/proxy.ts b/src/web/service/proxy/proxy.ts deleted file mode 100644 index 48c9fa4a5..000000000 --- a/src/web/service/proxy/proxy.ts +++ /dev/null @@ -1,31 +0,0 @@ -import * as URL from 'url'; -import * as express from 'express'; -import * as request from 'request'; -import * as isUrl from 'is-url'; - -module.exports = (req: express.Request, res: express.Response) => { - const url = req.params.url; - - if (!url || !isUrl(url)) { - return; - } - - request({ - url: url + URL.parse(req.url, true).search, - encoding: null - }, (err, response, content) => { - if (err) { - console.error(err); - return; - } - - const contentType = response.headers['content-type']; - - if (/^text\//.test(contentType) || contentType === 'application/javascript') { - content = content.toString().replace(/http:\/\//g, `${config.secondary_scheme}://proxy.${config.secondary_host}/http://`); - } - - res.header('Content-Type', contentType); - res.send(content); - }); -}; diff --git a/src/web/service/proxy/server.ts b/src/web/service/proxy/server.ts deleted file mode 100644 index 5b1b8d106..000000000 --- a/src/web/service/proxy/server.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Forward Proxy Service - */ - -import * as express from 'express'; -import * as cors from 'cors'; - -/** - * Init app - */ -const app = express(); -app.disable('x-powered-by'); -app.use(cors()); - -app.get('/:url(*)', require('./proxy')); - -module.exports = app; diff --git a/src/web/service/url-preview.ts b/src/web/service/url-preview.ts index d1a345ef1..0c5fd8a78 100644 --- a/src/web/service/url-preview.ts +++ b/src/web/service/url-preview.ts @@ -9,5 +9,7 @@ module.exports = async (req: express.Request, res: express.Response) => { }; function wrap(url: string): string { - return `${config.proxy_url}/${url}`; + return url != null + ? `https://images.weserv.nl/?url=${url.replace(/^https?:\/\//, '')}` + : null; }