refactor: 打包支持

pull/180/head
xiaojunnuo 2020-11-18 17:28:18 +08:00
parent 041a4b16a1
commit 987f8eb112
14 changed files with 52 additions and 37 deletions

View File

@ -3,7 +3,8 @@ module.exports = {
enabled: true,
port: 1181,
setting: {
NODE_TLS_REJECT_UNAUTHORIZED: true
NODE_TLS_REJECT_UNAUTHORIZED: true,
scriptDir: '../../../scripts/'
},
intercepts: {
'github.com': {

View File

@ -1,4 +1,7 @@
// eslint-disable-next-line no-unused-vars
const server = require('@docmirror/mitmproxy')
const config = JSON.parse(process.argv[2])
const path = require('path')
const scriptDir = '../../gui/extra/scripts/'
config.setting.scriptDir = path.join(__dirname, scriptDir)
server.start(config)

View File

@ -1,4 +1,12 @@
// eslint-disable-next-line no-unused-vars
const server = require('@docmirror/mitmproxy')
const config = JSON.parse(process.argv[2])
const path = require('path')
const log = require('../utils/util.log')
let scriptDir = '../extra/scripts/'
if (process.env.NODE_ENV === 'development') {
scriptDir = '../extra/scripts/'
}
config.setting.scriptDir = path.join(__dirname, scriptDir)
log.debug('scriptDir', config.setting.scriptDir)
server.start(config)

View File

@ -49,6 +49,7 @@ function install (app, api) {
updateParams.newVersion = true
if (updateParams.autoDownload !== false) {
app.$message.info('发现新版本,正在下载中...')
api.update.downloadUpdate()
return
}

View File

@ -14,23 +14,27 @@ function getScript (key, script) {
module.exports = {
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
const { rOptions, log } = context
const { rOptions, log, setting } = context
let keys = interceptOpt.script
if (typeof keys === 'string') {
keys = [keys]
}
let tags = getScript('global', monkey.get().global.script)
for (const key of keys) {
const script = monkey.get()[key]
if (script == null) {
continue
try {
let tags = getScript('global', monkey.get(setting.scriptDir).global.script)
for (const key of keys) {
const script = monkey.get(setting.scriptDir)[key]
if (script == null) {
continue
}
const scriptTag = getScript(key, script.script)
tags += '\r\n' + scriptTag
}
const scriptTag = getScript(key, script.script)
tags += '\r\n' + scriptTag
}
log.info('responseIntercept: insert script', rOptions.hostname, rOptions.path)
return {
head: tags
log.info('responseIntercept: insert script', rOptions.hostname, rOptions.path)
return {
head: tags
}
} catch (err) {
log.error('load monkey script error', err)
}
},
is (interceptOpt) {

View File

@ -1,8 +0,0 @@
const Monkey_Grants = {
GM_registerMenuCommand: () => {},
GM_unregisterMenuCommand: () => {},
GM_openInTab: () => {},
GM_getValue: () => {},
GM_setValue: () => {},
GM_notification: () => {}
}

View File

@ -1,5 +1,6 @@
const fs = require('fs')
const path = require('path')
const log = require('../../utils/util.log')
let scripts
function buildScript (sc, content) {
@ -54,22 +55,24 @@ function loadScript (content) {
return sc
}
function readFile (script) {
return fs.readFileSync(path.join(__dirname, './scripts/' + script)).toString()
function readFile (rootDir, script) {
const location = path.join(rootDir, './' + script)
log.debug('script location:', location)
return fs.readFileSync(location).toString()
}
const api = {
get () {
get (rootDir) {
if (scripts == null) {
api.load()
api.load(rootDir)
}
return scripts
},
load () {
load (rootDir) {
scripts = {}
scripts.github = loadScript(readFile('github.script'))
scripts.jquery = { script: readFile('jquery.min.js') }
scripts.global = { script: readFile('global.script') }
scripts.github = loadScript(readFile(rootDir, 'github.script'))
scripts.jquery = { script: readFile(rootDir, 'jquery.min.js') }
scripts.global = { script: readFile(rootDir, 'global.script') }
return scripts
}
}

View File

@ -63,14 +63,14 @@ const monkey = require('../../monkey')
module.exports = {
requestIntercept (context, req, res, ssl, next) {
const { rOptions, log } = context
const { rOptions, log, setting } = context
if (rOptions.path.indexOf(contextPath) !== 0) {
return
}
const urlPath = rOptions.path
const filename = urlPath.replace(contextPath, '')
const script = monkey.get()[filename]
const script = monkey.get(setting.scriptDir)[filename]
log.info('ds_script', filename, script != null)
res.writeHead(200)

View File

@ -9,7 +9,7 @@ const InsertScriptMiddleware = require('../middleware/InsertScriptMiddleware')
const defaultDns = require('dns')
const MAX_SLOW_TIME = 8000 // 超过此时间 则认为太慢了
// create requestHandler function
module.exports = function createRequestHandler (createIntercepts, externalProxy, dnsConfig) {
module.exports = function createRequestHandler (createIntercepts, externalProxy, dnsConfig, setting) {
// return
return function requestHandler (req, res, ssl) {
let proxyReq
@ -25,7 +25,8 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy,
const context = {
rOptions,
log,
RequestCounter
RequestCounter,
setting
}
let interceptors = createIntercepts(context)
if (interceptors == null) {

View File

@ -16,7 +16,8 @@ module.exports = {
getCertSocketTimeout = 1 * 1000,
middlewares = [],
externalProxy,
dnsConfig
dnsConfig,
setting
}, callback) {
// Don't reject unauthorized
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
@ -34,9 +35,9 @@ module.exports = {
port = ~~port
const requestHandler = createRequestHandler(
createIntercepts,
middlewares,
externalProxy,
dnsConfig
dnsConfig,
setting
)
const upgradeHandler = createUpgradeHandler()

View File

@ -2,6 +2,7 @@ const interceptors = require('./lib/interceptor')
const dnsUtil = require('./lib/dns')
const lodash = require('lodash')
const log = require('./utils/util.log')
const path = require('path')
function matchHostname (hostMap, hostname) {
const value = hostMap[hostname]
if (value) {
@ -47,13 +48,13 @@ module.exports = (config) => {
const dnsMapping = config.dns.mapping
const serverConfig = config
return {
port: serverConfig.port,
dnsConfig: {
providers: dnsUtil.initDNS(serverConfig.dns.providers),
mapping: dnsMapping
},
setting: serverConfig.setting,
sslConnectInterceptor: (req, cltSocket, head) => {
const hostname = req.url.split(':')[0]
const inWhiteList = matchHostname(whiteList, hostname) != null