diff --git a/index.js b/index.js index f74241309399ea7c5c32484ae81f11638b7b6672..22adefe154afff49f8acba0f4ae428a7c6adb769 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,13 @@ const technicalAccountUsername = process.env.TECHNICAL_ACCOUNT_USERNAME; const technicalAccountPassword = process.env.TECHNICAL_ACCOUNT_PASSWORD; const proxyHostTarget = process.env.PROXY_HOST_TARGET; +// Configuring the different proxy server +// Proxy IGN Ortho +var ingProxy = httpProxy.createProxyServer({ + changeOrigin: true, + target: 'https://wxs.ign.fr/q7kc4me8vbbf4iw7epsfoiy7/geoportail/r/wms/', +}); + // Configuring the different proxy server // Proxy WMS var wmsProxy = httpProxy.createProxyServer({ @@ -35,6 +42,10 @@ var mvtUnauthProxy = httpProxy.createProxyServer({ target: proxyHostTarget, }); +ingProxy.on('proxyReq', function (proxyReq, req) { + req._proxyReq = proxyReq; +}); + // keep a referece of the proxyRequest in the req object wmsProxy.on('proxyReq', function (proxyReq, req) { req._proxyReq = proxyReq; @@ -48,6 +59,14 @@ mvtUnauthProxy.on('proxyReq', function (proxyReq, req) { req._proxyReq = proxyReq; }); +ingProxy.on('error', function (err, req, res) { + // If the client cancelled the request, abort the request to the upstream server + if (req.socket.destroyed && err.code === 'ECONNRESET') { + req._proxyReq.abort(); + } + return console.log(`ING Error, req.socket.destroyed: ${req.socket.destroyed}, ${err}`); +}); + wmsProxy.on('error', function (err, req, res) { // If the client cancelled the request, abort the request to the upstream server if (req.socket.destroyed && err.code === 'ECONNRESET') { @@ -75,21 +94,23 @@ mvtUnauthProxy.on('error', function (err, req, res) { // Create an HTTP server http.createServer(async function (req, res) { + /******* IGN */ + if (req.url.includes('/ign')) { + req.headers['referer'] = 'grandlyon.com'; + ingProxy.web(req, res, {}); + return; + } + /******************* WMS *****************************/ if (req.url.includes('/wms')) { - printLog('Request received', 'WMS'); wmsProxy.web(req, res, {}); - printLog('WMS request', 'proxified'); return; } /******************* MVT *****************************/ if (req.url.includes('/mvt')) { - printLog('Request received', 'MVT'); - // If no cookies then then we can't identify a user and directly proxy the request without credentials if (req.headers["x-anonymous-consumer"]) { - printLog('Request received', `Unauthenticated - proxying the request`); mvtUnauthProxy.web(req, res, {}); return; } @@ -98,24 +119,19 @@ http.createServer(async function (req, res) { const layer = getParameterValueFromUrl(req.url, 'LAYERS'); if (!layer) { - printError('Request received', 'No layer provided'); res.statusCode = 400; res.end(); return; } - printLog('Request received', `Layer found: ${layer}`); - const userRightsOnTheLayer = await getRedisValue(`${layer}-${req.headers['x-consumer-username']}`); if (userRightsOnTheLayer === 'true') { - printLog('Request received', 'Authorized (value read from Redis)'); mvtProxy.web(req, res, {}); return; } if (userRightsOnTheLayer === 'false') { - printLog('Request received', 'Unauthorized (value read from Redis)'); res.statusCode = 401; res.end(); return; @@ -142,8 +158,6 @@ http.createServer(async function (req, res) { } }; - printLog('Request received', options); - let response; try { @@ -164,14 +178,9 @@ http.createServer(async function (req, res) { return; } - printLog('Request to ES', 'MVT found'); - const editorialMetadata = response.data.hits.hits[0]._source['editorial-metadata']; - printLog('Request to ES', editorialMetadata); - if (!editorialMetadata.isOpenAccess && editorialMetadata.isSample) { - printError('Proxy', 'Unauthorized'); setRedisValue(`${layer}-${req.headers['x-consumer-username']}`, false, redisUnauthorizedTTL); res.statusCode = 401; res.end(); @@ -180,8 +189,6 @@ http.createServer(async function (req, res) { await setRedisValue(`${layer}-${req.headers['x-consumer-username']}`, true, redisAuthorizedTTL); - printLog('Proxy', 'Authorized'); - mvtProxy.web(req, res, {}); } @@ -202,7 +209,7 @@ async function setRedisValue(key, value, ttl) { sentinels: [{ host: redisSentinelHost, port: redisSentinelPort - }, ], + },], name: redisGroupName, }); @@ -232,7 +239,7 @@ async function getRedisValue(key) { sentinels: [{ host: redisSentinelHost, port: redisSentinelPort - }, ], + },], name: redisGroupName, });