Merge branch 'download_cer' of git://github.com/guox191/anyproxy into guox191-download_cer

This commit is contained in:
xiaofeng.mxf
2019-03-26 20:40:33 +08:00
8 changed files with 170 additions and 46 deletions

View File

@@ -1,7 +1,5 @@
'use strict';
const DEFAULT_WEB_PORT = 8002; // port for web interface
const express = require('express'),
url = require('url'),
bodyParser = require('body-parser'),
@@ -14,11 +12,16 @@ const express = require('express'),
wsServer = require('./wsServer'),
juicer = require('juicer'),
ip = require('ip'),
compress = require('compression');
compress = require('compression'),
pug = require('pug');
const DEFAULT_WEB_PORT = 8002; // port for web interface
const packageJson = require('../package.json');
const MAX_CONTENT_SIZE = 1024 * 2000; // 2000kb
const certFileTypes = ['crt', 'cer', 'pem', 'der'];
/**
*
*
@@ -198,12 +201,18 @@ class webInterface extends events.EventEmitter {
}
});
app.get('/downloadCrt', (req, res) => {
const pageFn = pug.compileFile(path.join(__dirname, '../resource/cert_download.pug'));
res.end(pageFn({ ua: req.get('user-agent') }));
});
app.get('/fetchCrtFile', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
const _crtFilePath = certMgr.getRootCAFilePath();
if (_crtFilePath) {
const fileType = certFileTypes.indexOf(req.query.type) !== -1 ? req.query.type : 'crt';
res.setHeader('Content-Type', 'application/x-x509-ca-cert');
res.setHeader('Content-Disposition', 'attachment; filename="rootCA.crt"');
res.setHeader('Content-Disposition', `attachment; filename="rootCA.${fileType}"`);
res.end(fs.readFileSync(_crtFilePath, { encoding: null }));
} else {
res.setHeader('Content-Type', 'text/html');
@@ -211,25 +220,11 @@ class webInterface extends events.EventEmitter {
}
});
//make qr code
app.get('/qr', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'text/html');
const qr = qrCode.qrcode(4, 'M');
const targetUrl = req.protocol + '://' + req.get('host');
qr.addData(targetUrl);
qr.make();
const qrImageTag = qr.createImgTag(4);
const resDom = '<a href="__url"> __img <br> click or scan qr code to start client </a>'.replace(/__url/, targetUrl).replace(/__img/, qrImageTag);
res.end(resDom);
});
app.get('/api/getQrCode', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
const qr = qrCode.qrcode(4, 'M');
const targetUrl = req.protocol + '://' + req.get('host') + '/fetchCrtFile';
const targetUrl = req.protocol + '://' + req.get('host') + '/downloadCrt';
const isRootCAFileExists = certMgr.isRootCAFileExists();
qr.addData(targetUrl);