Skip to content

Commit e5b9657

Browse files
committed
possible proxy fix for dalicc request
1 parent 27c7398 commit e5b9657

3 files changed

Lines changed: 49 additions & 39 deletions

File tree

server/app/common/file-analyzer.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
11
const shasum = require('js-sha256');
22
const axios = require('axios');
33
const UriUtils = require('./utils/uri-utils');
4-
const { HttpsProxyAgent } = require('https-proxy-agent');
5-
const { HttpProxyAgent } = require('http-proxy-agent');
4+
const ServerUtils = require('./utils/server-utils');
65

76
var analyzer = {};
87

9-
function getProxyAgent(url) {
10-
const target = new URL(url);
11-
const host = target.hostname;
12-
13-
const noProxyVar =
14-
process.env.NO_PROXY || process.env.no_proxy || '';
15-
const noProxy = noProxyVar.split(',').map(s => s.trim()).filter(Boolean);
16-
17-
if (noProxy.some(np => host === np || host.endsWith('.' + np)))
18-
return null;
19-
20-
const httpsProxy =
21-
process.env.HTTPS_PROXY || process.env.https_proxy;
22-
const httpProxy =
23-
process.env.HTTP_PROXY || process.env.http_proxy;
24-
25-
const proxyUrl = target.protocol === 'https:' ? httpsProxy : httpProxy;
26-
27-
if (!proxyUrl) return null;
28-
29-
const agent =
30-
target.protocol === 'https:'
31-
? new HttpsProxyAgent(proxyUrl)
32-
: new HttpProxyAgent(proxyUrl);
33-
34-
// Allow MITM proxy certificates (testing only)
35-
if (process.env.ALLOW_INSECURE_PROXY === 'true') {
36-
agent.options.rejectUnauthorized = false;
37-
}
38-
39-
return agent;
40-
}
41-
428

439
analyzer.processFile = async function (fileUri, url, webdav) {
4410

@@ -143,7 +109,7 @@ analyzer.analyzeFile = async function (url) {
143109

144110
var hash = shasum.create();
145111

146-
const agent = getProxyAgent(url);
112+
const agent = ServerUtils.getProxyAgent(url);
147113

148114
const response = await axios.request({
149115
method: 'GET',

server/app/common/utils/server-utils.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,43 @@ var ASN1 = require('asn1js');
22
const fs = require('fs');
33
const DatabusUris = require('../../../../public/js/utils/databus-uris');
44
const DatabusConstants = require('../../../../public/js/utils/databus-constants');
5+
const { HttpsProxyAgent } = require('https-proxy-agent');
6+
const { HttpProxyAgent } = require('http-proxy-agent');
57

68
class ServerUtils {
79

10+
static getProxyAgent(url) {
11+
const target = new URL(url);
12+
const host = target.hostname;
13+
14+
const noProxyVar =
15+
process.env.NO_PROXY || process.env.no_proxy || '';
16+
const noProxy = noProxyVar.split(',').map(s => s.trim()).filter(Boolean);
17+
18+
if (noProxy.some(np => host === np || host.endsWith('.' + np)))
19+
return null;
20+
21+
const httpsProxy =
22+
process.env.HTTPS_PROXY || process.env.https_proxy;
23+
const httpProxy =
24+
process.env.HTTP_PROXY || process.env.http_proxy;
25+
26+
const proxyUrl = target.protocol === 'https:' ? httpsProxy : httpProxy;
27+
28+
if (!proxyUrl) return null;
29+
30+
const agent =
31+
target.protocol === 'https:'
32+
? new HttpsProxyAgent(proxyUrl)
33+
: new HttpProxyAgent(proxyUrl);
34+
35+
// Allow MITM proxy certificates (testing only)
36+
if (process.env.ALLOW_INSECURE_PROXY === 'true') {
37+
agent.options.rejectUnauthorized = false;
38+
}
39+
40+
return agent;
41+
}
842

943
static setupRequireExtensions() {
1044
// add a sparql file loading extension (simply read the file as a string)

server/app/webapp/modules/publish-wizard.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ const ServerUtils = require('../../common/utils/server-utils.js');
55
module.exports = function (router, protector) {
66
require('../../common/file-analyzer').route(router, protector);
77

8-
router.get('/app/publish-wizard/licenses', protector.protect(), async function(req, res, next) {
8+
router.get('/app/publish-wizard/licenses', protector.protect(), async function (req, res, next) {
99
const search = new URL(`${req.protocol}://${req.get('host')}${req.originalUrl}`).search;
1010

11+
const url = `https://api.dalicc.net/licenselibrary/list${search}`;
12+
const agent = ServerUtils.getProxyAgent(url);
13+
1114
try {
12-
const daliccRes = await axios.get(`https://api.dalicc.net/licenselibrary/list${search}`, {
13-
headers: { accept: 'application/json' }
15+
16+
const daliccRes = await axios.request({
17+
method: 'GET',
18+
url,
19+
headers: { accept: 'application/json' },
20+
httpAgent: agent,
21+
httpsAgent: agent,
22+
proxy: false
1423
});
24+
1525
res.status(200).send(daliccRes.data);
1626
} catch (err) {
1727
console.log(err);

0 commit comments

Comments
 (0)