diff --git a/docker-compose.yml b/docker-compose.yml index d897ecb8b176550697a29ddfcf2f85da7908e93f..d307d201a2249b4ed56752a2ba36b1525a5d0fba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,6 +17,7 @@ services: - TECHNICAL_ACCOUNT_USERNAME=${TECHNICAL_ACCOUNT_USERNAME} - TECHNICAL_ACCOUNT_PASSWORD=${TECHNICAL_ACCOUNT_PASSWORD} - PROXY_HOST_TARGET=${PROXY_HOST_TARGET} + - IGN_KEY=${IGN_KEY} depends_on: - redis-sentinel-1 restart: unless-stopped diff --git a/index.js b/index.js index f74241309399ea7c5c32484ae81f11638b7b6672..8d0a144595cd27a1a5a8924bae120561faf47db0 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,14 @@ const elasticsearchUrl = process.env.ELASTICSEARCH_URL; const technicalAccountUsername = process.env.TECHNICAL_ACCOUNT_USERNAME; const technicalAccountPassword = process.env.TECHNICAL_ACCOUNT_PASSWORD; const proxyHostTarget = process.env.PROXY_HOST_TARGET; +const ignKey = process.env.IGN_KEY; + +// Configuring the different proxy server +// Proxy IGN Ortho +var ingProxy = httpProxy.createProxyServer({ + changeOrigin: true, + target: 'https://wxs.ign.fr/' + ignKey + '/geoportail/r/wms/', +}); // Configuring the different proxy server // Proxy WMS @@ -35,6 +43,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 +60,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 +95,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 +120,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 +159,6 @@ http.createServer(async function (req, res) { } }; - printLog('Request received', options); - let response; try { @@ -164,14 +179,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 +190,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 +210,7 @@ async function setRedisValue(key, value, ttl) { sentinels: [{ host: redisSentinelHost, port: redisSentinelPort - }, ], + },], name: redisGroupName, }); @@ -232,7 +240,7 @@ async function getRedisValue(key) { sentinels: [{ host: redisSentinelHost, port: redisSentinelPort - }, ], + },], name: redisGroupName, }); diff --git a/package-lock.json b/package-lock.json index 46e2d63ae969b89c8b6c1ab337e2d0fe9b0810b0..1064f9de32e312e11250cca3b0e08d36f91a25a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "proxy-mvt", - "version": "1.0.0", + "name": "proxy-map-services", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -10,7 +10,7 @@ "integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==", "requires": { "follow-redirects": "1.5.10", - "is-buffer": "2.0.4" + "is-buffer": "^2.0.2" }, "dependencies": { "debug": { @@ -26,7 +26,7 @@ "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", "requires": { - "debug": "3.1.0" + "debug": "=3.1.0" } }, "ms": { @@ -51,7 +51,7 @@ "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", "requires": { - "ms": "2.1.2" + "ms": "^2.1.1" } }, "denque": { @@ -69,7 +69,7 @@ "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.9.0.tgz", "integrity": "sha512-CRcPzsSIbXyVDl0QI01muNDu69S8trU4jArW9LpOt2WtC6LyUJetcIrmfHsRBx7/Jb6GHJUiuqyYxPooFfNt6A==", "requires": { - "debug": "3.2.6" + "debug": "^3.0.0" } }, "http": { @@ -82,9 +82,9 @@ "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.0.tgz", "integrity": "sha512-84I2iJM/n1d4Hdgc6y2+qY5mDaz2PUVjlg9znE9byl+q0uC3DeByqBGReQu5tpLK0TAqTIXScRUV+dg7+bUPpQ==", "requires": { - "eventemitter3": "4.0.0", - "follow-redirects": "1.9.0", - "requires-port": "1.0.0" + "eventemitter3": "^4.0.0", + "follow-redirects": "^1.0.0", + "requires-port": "^1.0.0" } }, "ioredis": { @@ -92,15 +92,15 @@ "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.14.1.tgz", "integrity": "sha512-94W+X//GHM+1GJvDk6JPc+8qlM7Dul+9K+lg3/aHixPN7ZGkW6qlvX0DG6At9hWtH2v3B32myfZqWoANUJYGJA==", "requires": { - "cluster-key-slot": "1.1.0", - "debug": "4.1.1", - "denque": "1.4.1", - "lodash.defaults": "4.2.0", - "lodash.flatten": "4.4.0", + "cluster-key-slot": "^1.1.0", + "debug": "^4.1.1", + "denque": "^1.1.0", + "lodash.defaults": "^4.2.0", + "lodash.flatten": "^4.4.0", "redis-commands": "1.5.0", - "redis-errors": "1.2.0", - "redis-parser": "3.0.0", - "standard-as-callback": "2.0.1" + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0", + "standard-as-callback": "^2.0.1" }, "dependencies": { "debug": { @@ -108,7 +108,7 @@ "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", "requires": { - "ms": "2.1.2" + "ms": "^2.1.1" } } } @@ -158,7 +158,7 @@ "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", "requires": { - "redis-errors": "1.2.0" + "redis-errors": "^1.0.0" } }, "requires-port": { diff --git a/package.json b/package.json index 3887be0d3be15bddadb51ef0cc4f79f2860a88ab..4bbb0a5dd5bd5de6fad1f65057ba13924e5bcfb3 100644 --- a/package.json +++ b/package.json @@ -17,4 +17,4 @@ "ioredis": "^4.14.1", "url": "^0.11.0" } -} \ No newline at end of file +}