refactor: 打包支持
parent
041a4b16a1
commit
987f8eb112
|
@ -3,7 +3,8 @@ module.exports = {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
port: 1181,
|
port: 1181,
|
||||||
setting: {
|
setting: {
|
||||||
NODE_TLS_REJECT_UNAUTHORIZED: true
|
NODE_TLS_REJECT_UNAUTHORIZED: true,
|
||||||
|
scriptDir: '../../../scripts/'
|
||||||
},
|
},
|
||||||
intercepts: {
|
intercepts: {
|
||||||
'github.com': {
|
'github.com': {
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const server = require('@docmirror/mitmproxy')
|
const server = require('@docmirror/mitmproxy')
|
||||||
const config = JSON.parse(process.argv[2])
|
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)
|
server.start(config)
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const server = require('@docmirror/mitmproxy')
|
const server = require('@docmirror/mitmproxy')
|
||||||
const config = JSON.parse(process.argv[2])
|
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)
|
server.start(config)
|
||||||
|
|
|
@ -49,6 +49,7 @@ function install (app, api) {
|
||||||
updateParams.newVersion = true
|
updateParams.newVersion = true
|
||||||
|
|
||||||
if (updateParams.autoDownload !== false) {
|
if (updateParams.autoDownload !== false) {
|
||||||
|
app.$message.info('发现新版本,正在下载中...')
|
||||||
api.update.downloadUpdate()
|
api.update.downloadUpdate()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,23 +14,27 @@ function getScript (key, script) {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
|
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
|
||||||
const { rOptions, log } = context
|
const { rOptions, log, setting } = context
|
||||||
let keys = interceptOpt.script
|
let keys = interceptOpt.script
|
||||||
if (typeof keys === 'string') {
|
if (typeof keys === 'string') {
|
||||||
keys = [keys]
|
keys = [keys]
|
||||||
}
|
}
|
||||||
let tags = getScript('global', monkey.get().global.script)
|
try {
|
||||||
for (const key of keys) {
|
let tags = getScript('global', monkey.get(setting.scriptDir).global.script)
|
||||||
const script = monkey.get()[key]
|
for (const key of keys) {
|
||||||
if (script == null) {
|
const script = monkey.get(setting.scriptDir)[key]
|
||||||
continue
|
if (script == null) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const scriptTag = getScript(key, script.script)
|
||||||
|
tags += '\r\n' + scriptTag
|
||||||
}
|
}
|
||||||
const scriptTag = getScript(key, script.script)
|
log.info('responseIntercept: insert script', rOptions.hostname, rOptions.path)
|
||||||
tags += '\r\n' + scriptTag
|
return {
|
||||||
}
|
head: tags
|
||||||
log.info('responseIntercept: insert script', rOptions.hostname, rOptions.path)
|
}
|
||||||
return {
|
} catch (err) {
|
||||||
head: tags
|
log.error('load monkey script error', err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
is (interceptOpt) {
|
is (interceptOpt) {
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
const Monkey_Grants = {
|
|
||||||
GM_registerMenuCommand: () => {},
|
|
||||||
GM_unregisterMenuCommand: () => {},
|
|
||||||
GM_openInTab: () => {},
|
|
||||||
GM_getValue: () => {},
|
|
||||||
GM_setValue: () => {},
|
|
||||||
GM_notification: () => {}
|
|
||||||
}
|
|
|
@ -1,5 +1,6 @@
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const log = require('../../utils/util.log')
|
||||||
let scripts
|
let scripts
|
||||||
|
|
||||||
function buildScript (sc, content) {
|
function buildScript (sc, content) {
|
||||||
|
@ -54,22 +55,24 @@ function loadScript (content) {
|
||||||
return sc
|
return sc
|
||||||
}
|
}
|
||||||
|
|
||||||
function readFile (script) {
|
function readFile (rootDir, script) {
|
||||||
return fs.readFileSync(path.join(__dirname, './scripts/' + script)).toString()
|
const location = path.join(rootDir, './' + script)
|
||||||
|
log.debug('script location:', location)
|
||||||
|
return fs.readFileSync(location).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
const api = {
|
const api = {
|
||||||
get () {
|
get (rootDir) {
|
||||||
if (scripts == null) {
|
if (scripts == null) {
|
||||||
api.load()
|
api.load(rootDir)
|
||||||
}
|
}
|
||||||
return scripts
|
return scripts
|
||||||
},
|
},
|
||||||
load () {
|
load (rootDir) {
|
||||||
scripts = {}
|
scripts = {}
|
||||||
scripts.github = loadScript(readFile('github.script'))
|
scripts.github = loadScript(readFile(rootDir, 'github.script'))
|
||||||
scripts.jquery = { script: readFile('jquery.min.js') }
|
scripts.jquery = { script: readFile(rootDir, 'jquery.min.js') }
|
||||||
scripts.global = { script: readFile('global.script') }
|
scripts.global = { script: readFile(rootDir, 'global.script') }
|
||||||
return scripts
|
return scripts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,14 +63,14 @@ const monkey = require('../../monkey')
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
requestIntercept (context, req, res, ssl, next) {
|
requestIntercept (context, req, res, ssl, next) {
|
||||||
const { rOptions, log } = context
|
const { rOptions, log, setting } = context
|
||||||
if (rOptions.path.indexOf(contextPath) !== 0) {
|
if (rOptions.path.indexOf(contextPath) !== 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const urlPath = rOptions.path
|
const urlPath = rOptions.path
|
||||||
const filename = urlPath.replace(contextPath, '')
|
const filename = urlPath.replace(contextPath, '')
|
||||||
|
|
||||||
const script = monkey.get()[filename]
|
const script = monkey.get(setting.scriptDir)[filename]
|
||||||
|
|
||||||
log.info('ds_script', filename, script != null)
|
log.info('ds_script', filename, script != null)
|
||||||
res.writeHead(200)
|
res.writeHead(200)
|
||||||
|
|
|
@ -9,7 +9,7 @@ const InsertScriptMiddleware = require('../middleware/InsertScriptMiddleware')
|
||||||
const defaultDns = require('dns')
|
const defaultDns = require('dns')
|
||||||
const MAX_SLOW_TIME = 8000 // 超过此时间 则认为太慢了
|
const MAX_SLOW_TIME = 8000 // 超过此时间 则认为太慢了
|
||||||
// create requestHandler function
|
// create requestHandler function
|
||||||
module.exports = function createRequestHandler (createIntercepts, externalProxy, dnsConfig) {
|
module.exports = function createRequestHandler (createIntercepts, externalProxy, dnsConfig, setting) {
|
||||||
// return
|
// return
|
||||||
return function requestHandler (req, res, ssl) {
|
return function requestHandler (req, res, ssl) {
|
||||||
let proxyReq
|
let proxyReq
|
||||||
|
@ -25,7 +25,8 @@ module.exports = function createRequestHandler (createIntercepts, externalProxy,
|
||||||
const context = {
|
const context = {
|
||||||
rOptions,
|
rOptions,
|
||||||
log,
|
log,
|
||||||
RequestCounter
|
RequestCounter,
|
||||||
|
setting
|
||||||
}
|
}
|
||||||
let interceptors = createIntercepts(context)
|
let interceptors = createIntercepts(context)
|
||||||
if (interceptors == null) {
|
if (interceptors == null) {
|
||||||
|
|
|
@ -16,7 +16,8 @@ module.exports = {
|
||||||
getCertSocketTimeout = 1 * 1000,
|
getCertSocketTimeout = 1 * 1000,
|
||||||
middlewares = [],
|
middlewares = [],
|
||||||
externalProxy,
|
externalProxy,
|
||||||
dnsConfig
|
dnsConfig,
|
||||||
|
setting
|
||||||
}, callback) {
|
}, callback) {
|
||||||
// Don't reject unauthorized
|
// Don't reject unauthorized
|
||||||
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
|
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
|
||||||
|
@ -34,9 +35,9 @@ module.exports = {
|
||||||
port = ~~port
|
port = ~~port
|
||||||
const requestHandler = createRequestHandler(
|
const requestHandler = createRequestHandler(
|
||||||
createIntercepts,
|
createIntercepts,
|
||||||
middlewares,
|
|
||||||
externalProxy,
|
externalProxy,
|
||||||
dnsConfig
|
dnsConfig,
|
||||||
|
setting
|
||||||
)
|
)
|
||||||
|
|
||||||
const upgradeHandler = createUpgradeHandler()
|
const upgradeHandler = createUpgradeHandler()
|
||||||
|
|
|
@ -2,6 +2,7 @@ const interceptors = require('./lib/interceptor')
|
||||||
const dnsUtil = require('./lib/dns')
|
const dnsUtil = require('./lib/dns')
|
||||||
const lodash = require('lodash')
|
const lodash = require('lodash')
|
||||||
const log = require('./utils/util.log')
|
const log = require('./utils/util.log')
|
||||||
|
const path = require('path')
|
||||||
function matchHostname (hostMap, hostname) {
|
function matchHostname (hostMap, hostname) {
|
||||||
const value = hostMap[hostname]
|
const value = hostMap[hostname]
|
||||||
if (value) {
|
if (value) {
|
||||||
|
@ -47,13 +48,13 @@ module.exports = (config) => {
|
||||||
|
|
||||||
const dnsMapping = config.dns.mapping
|
const dnsMapping = config.dns.mapping
|
||||||
const serverConfig = config
|
const serverConfig = config
|
||||||
|
|
||||||
return {
|
return {
|
||||||
port: serverConfig.port,
|
port: serverConfig.port,
|
||||||
dnsConfig: {
|
dnsConfig: {
|
||||||
providers: dnsUtil.initDNS(serverConfig.dns.providers),
|
providers: dnsUtil.initDNS(serverConfig.dns.providers),
|
||||||
mapping: dnsMapping
|
mapping: dnsMapping
|
||||||
},
|
},
|
||||||
|
setting: serverConfig.setting,
|
||||||
sslConnectInterceptor: (req, cltSocket, head) => {
|
sslConnectInterceptor: (req, cltSocket, head) => {
|
||||||
const hostname = req.url.split(':')[0]
|
const hostname = req.url.split(':')[0]
|
||||||
const inWhiteList = matchHostname(whiteList, hostname) != null
|
const inWhiteList = matchHostname(whiteList, hostname) != null
|
||||||
|
|
Loading…
Reference in New Issue