Skip to content
Snippets Groups Projects
Unverified Commit 02bb866b authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

Convert packages/rocketchat-cors to JS

parent d0519dc6
No related branches found
No related tags found
No related merge requests found
Meteor.startup ->
RocketChat.settings.onload 'Force_SSL', (key, value) ->
Meteor.absoluteUrl.defaultOptions.secure = value
Meteor.startup(function() {
return RocketChat.settings.onload('Force_SSL', function(key, value) {
return Meteor.absoluteUrl.defaultOptions.secure = value;
});
});
# Adding CORS headers so we can use CDNs for static content
# Try to parse all request bodies as JSON
WebApp.rawConnectHandlers.use (req, res, next) ->
if req._body
return next()
if req.headers['transfer-encoding'] is undefined and isNaN(req.headers['content-length'])
return next()
if req.headers['content-type'] not in ['', undefined]
return next()
if req.url.indexOf('/ufs/') is 0
return next()
buf = ''
req.setEncoding('utf8')
req.on 'data', (chunk) -> buf += chunk
req.on 'end', ->
if RocketChat?.debugLevel? and RocketChat.debugLevel is 'debug'
console.log '[request]'.green, req.method, req.url, '\nheaders ->', req.headers, '\nbody ->', buf
try
req.body = JSON.parse(buf)
catch err
req.body = buf
req._body = true
next()
WebApp.rawConnectHandlers.use (req, res, next) ->
if /^\/(api|_timesync|sockjs|tap-i18n|__cordova)(\/|$)/.test req.url
res.setHeader("Access-Control-Allow-Origin", "*")
# Block next handlers to override CORS with value http://meteor.local
setHeader = res.setHeader
res.setHeader = (key, val) ->
if key.toLowerCase() is 'access-control-allow-origin' and val is 'http://meteor.local'
return
return setHeader.apply @, arguments
return next()
_staticFilesMiddleware = WebAppInternals.staticFilesMiddleware
WebAppInternals._staticFilesMiddleware = (staticFiles, req, res, next) ->
res.setHeader("Access-Control-Allow-Origin", "*")
_staticFilesMiddleware(staticFiles, req, res, next)
url = Npm.require("url")
httpServer = WebApp.httpServer
oldHttpServerListeners = httpServer.listeners('request').slice(0)
httpServer.removeAllListeners('request')
httpServer.addListener 'request', (req, res) ->
args = arguments
next = ->
for oldListener in oldHttpServerListeners
oldListener.apply(httpServer, args)
if RocketChat.settings.get('Force_SSL') isnt true
next()
return
remoteAddress = req.connection.remoteAddress or req.socket.remoteAddress
localhostRegexp = /^\s*(127\.0\.0\.1|::1)\s*$/
localhostTest = (x) ->
return localhostRegexp.test(x)
isLocal = localhostRegexp.test(remoteAddress) and (not req.headers['x-forwarded-for'] or _.all(req.headers['x-forwarded-for'].split(','), localhostTest))
isSsl = req.connection.pair or (req.headers['x-forwarded-proto'] and req.headers['x-forwarded-proto'].indexOf('https') isnt -1)
if RocketChat?.debugLevel? and RocketChat.debugLevel is 'debug'
console.log 'req.url', req.url
console.log 'remoteAddress', remoteAddress
console.log 'isLocal', isLocal
console.log 'isSsl', isSsl
console.log 'req.headers', req.headers
if not isLocal and not isSsl
host = req.headers['host'] or url.parse(Meteor.absoluteUrl()).hostname
host = host.replace(/:\d+$/, '')
res.writeHead 302,
'Location': 'https://' + host + req.url
res.end()
return
next()
/* globals WebAppInternals */
import url from 'url';
WebApp.rawConnectHandlers.use(function(req, res, next) {
if (req._body) {
return next();
}
if (req.headers['transfer-encoding'] === void 0 && isNaN(req.headers['content-length'])) {
return next();
}
if (req.headers['content-type'] !== '' && req.headers['content-type'] !== undefined) {
return next();
}
if (req.url.indexOf('/ufs/') === 0) {
return next();
}
let buf = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
return buf += chunk;
});
req.on('end', function() {
if (RocketChat && RocketChat.debugLevel === 'debug') {
console.log('[request]'.green, req.method, req.url, '\nheaders ->', req.headers, '\nbody ->', buf);
}
try {
req.body = JSON.parse(buf);
} catch (error) {
req.body = buf;
}
req._body = true;
return next();
});
});
WebApp.rawConnectHandlers.use(function(req, res, next) {
if (/^\/(api|_timesync|sockjs|tap-i18n|__cordova)(\/|$)/.test(req.url)) {
res.setHeader('Access-Control-Allow-Origin', '*');
}
const setHeader = res.setHeader;
res.setHeader = function(key, val) {
if (key.toLowerCase() === 'access-control-allow-origin' && val === 'http://meteor.local') {
return;
}
return setHeader.apply(this, arguments);
};
return next();
});
const _staticFilesMiddleware = WebAppInternals.staticFilesMiddleware;
WebAppInternals._staticFilesMiddleware = function(staticFiles, req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
return _staticFilesMiddleware(staticFiles, req, res, next);
};
const oldHttpServerListeners = WebApp.httpServer.listeners('request').slice(0);
WebApp.httpServer.removeAllListeners('request');
WebApp.httpServer.addListener('request', function(req, res) {
const next = () => {
for (const oldListener of oldHttpServerListeners) {
oldListener.apply(WebApp.httpServer, arguments);
}
};
if (RocketChat.settings.get('Force_SSL') !== true) {
next();
return;
}
const remoteAddress = req.connection.remoteAddress || req.socket.remoteAddress;
const localhostRegexp = /^\s*(127\.0\.0\.1|::1)\s*$/;
const localhostTest = function(x) {
return localhostRegexp.test(x);
};
const isLocal = localhostRegexp.test(remoteAddress) && (!req.headers['x-forwarded-for'] || _.all(req.headers['x-forwarded-for'].split(','), localhostTest));
const isSsl = req.connection.pair || (req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'].indexOf('https') !== -1);
if (RocketChat && RocketChat.debugLevel === 'debug') {
console.log('req.url', req.url);
console.log('remoteAddress', remoteAddress);
console.log('isLocal', isLocal);
console.log('isSsl', isSsl);
console.log('req.headers', req.headers);
}
if (!isLocal && !isSsl) {
let host = req.headers['host'] || url.parse(Meteor.absoluteUrl()).hostname;
host = host.replace(/:\d+$/, '');
res.writeHead(302, {
'Location': 'https://' + host + req.url
});
res.end();
return;
}
return next();
});
......@@ -7,11 +7,10 @@ Package.describe({
Package.onUse(function(api) {
api.use([
'coffeescript',
'ecmascript',
'webapp'
]);
api.addFiles('cors.coffee', 'server');
api.addFiles('common.coffee');
api.addFiles('cors.js', 'server');
api.addFiles('common.js');
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment