perf: 支持日志文件
parent
c8437439ca
commit
83276a328f
|
@ -31,6 +31,9 @@ const localApi = {
|
|||
return {
|
||||
version: pk.version
|
||||
}
|
||||
},
|
||||
getConfigDir () {
|
||||
return getDefaultConfigBasePath()
|
||||
}
|
||||
},
|
||||
/**
|
||||
|
@ -81,6 +84,7 @@ const localApi = {
|
|||
*/
|
||||
save (newConfig) {
|
||||
// 对比默认config的异同
|
||||
DevSidecar.api.config.set(newConfig)
|
||||
const defConfig = DevSidecar.api.config.getDefault()
|
||||
const saveConfig = doMerge(defConfig, newConfig)
|
||||
fs.writeFileSync(_getConfigPath(), JSON5.stringify(saveConfig, null, 2))
|
||||
|
|
|
@ -17,13 +17,18 @@ const bindApi = (api, param1) => {
|
|||
})
|
||||
}
|
||||
const apiObj = {
|
||||
on (channel, callback) {
|
||||
ipcRenderer.on(channel, callback)
|
||||
},
|
||||
invoke,
|
||||
send,
|
||||
openExternal (href) {
|
||||
shell.openExternal(href)
|
||||
ipc: {
|
||||
on (channel, callback) {
|
||||
ipcRenderer.on(channel, callback)
|
||||
},
|
||||
invoke,
|
||||
send,
|
||||
openExternal (href) {
|
||||
shell.openExternal(href)
|
||||
},
|
||||
openPath (file) {
|
||||
shell.openPath(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
let inited = false
|
||||
|
|
|
@ -39,13 +39,14 @@ export default {
|
|||
this.applyLoading = true
|
||||
await this.applyBefore()
|
||||
await this.saveConfig()
|
||||
if (this.applyAfter) {
|
||||
await this.applyAfter()
|
||||
}
|
||||
await this.applyAfter()
|
||||
this.applyLoading = false
|
||||
},
|
||||
async applyBefore () {
|
||||
|
||||
},
|
||||
async applyAfter () {
|
||||
|
||||
},
|
||||
resetDefault () {
|
||||
const key = this.getKey()
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
function install (app, api) {
|
||||
api.on('error.core', (event, message) => {
|
||||
api.ipc.on('error.core', (event, message) => {
|
||||
console.error('view on error', message)
|
||||
const key = message.key
|
||||
if (key === 'server') {
|
||||
handleServerStartError(message, message.error, app, api)
|
||||
}
|
||||
})
|
||||
api.on('error', (event, message) => {
|
||||
api.ipc.on('error', (event, message) => {
|
||||
console.error('error', event, message)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
let updateParams = { }
|
||||
function install (app, api) {
|
||||
api.on('update', (event, message) => {
|
||||
api.ipc.on('update', (event, message) => {
|
||||
console.log('on message', event, message)
|
||||
handleUpdateMessage(message, app)
|
||||
})
|
||||
|
@ -8,13 +8,13 @@ function install (app, api) {
|
|||
api.update = {
|
||||
checkForUpdate (params) {
|
||||
updateParams = params || { fromUser: false, autoDownload: true, progress: 0 }
|
||||
api.send('update', { key: 'checkForUpdate' })
|
||||
api.ipc.send('update', { key: 'checkForUpdate' })
|
||||
},
|
||||
downloadUpdate () {
|
||||
api.send('update', { key: 'downloadUpdate' })
|
||||
api.ipc.send('update', { key: 'downloadUpdate' })
|
||||
},
|
||||
doUpdateNow () {
|
||||
api.send('update', { key: 'doUpdateNow' })
|
||||
api.ipc.send('update', { key: 'doUpdateNow' })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
<div class="flex-l-r star" style="padding:10px;">
|
||||
<div>如果它解决了你的问题,请不要吝啬你的star哟! <a-icon type="smile" theme="outlined" /></div>
|
||||
<a @click="openExternal('https://gitee.com/docmirror/dev-sidecar')"><img src='https://gitee.com/docmirror/dev-sidecar/badge/star.svg?theme=dark' alt='star'/></a>
|
||||
<a @click="openExternal('https://github.com/docmirror/dev-sidecar')"><img alt="GitHub stars" src="https://img.shields.io/github/stars/docmirror/dev-sidecar?logo=github"></a>
|
||||
<a @click="openExternal('https://github.com/docmirror/dev-sidecar')"><img alt="GitHub stars" src="https://img.shields.io/github/stars/docmirror/dev-sidecar?logo=github"></a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
@ -233,7 +233,7 @@ export default {
|
|||
this.$api.update.checkForUpdate(this.update)
|
||||
},
|
||||
openExternal (url) {
|
||||
this.$api.openExternal(url)
|
||||
this.$api.ipc.openExternal(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
<a-tag v-else color="red">
|
||||
当前未启动
|
||||
</a-tag>
|
||||
|
||||
<a-button class="md-mr-10" icon="profile" @click="openLog()">日志</a-button>
|
||||
</a-form-item>
|
||||
<a-form-item label="代理端口" :label-col="labelCol" :wrapper-col="wrapperCol" >
|
||||
<a-input v-model="config.server.port"/>
|
||||
|
@ -132,6 +134,10 @@ export default {
|
|||
},
|
||||
addDnsMapping () {
|
||||
this.dnsMappings.unshift({ key: '', value: 'usa' })
|
||||
},
|
||||
async openLog () {
|
||||
const dir = await this.$api.info.getConfigDir()
|
||||
this.$api.ipc.openPath(dir + '/logs/server.log')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ const status = {
|
|||
}
|
||||
}
|
||||
async function install (api) {
|
||||
api.on('status', (event, message) => {
|
||||
api.ipc.on('status', (event, message) => {
|
||||
console.log('view on status', event, message)
|
||||
const value = message.value
|
||||
const key = message.key
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const mitmproxy = require('./lib/proxy')
|
||||
const ProxyOptions = require('./options')
|
||||
const config = require('./lib/proxy/common/config')
|
||||
|
||||
const log = require('./utils/util.log')
|
||||
function fireError (e) {
|
||||
process.send({ type: 'error', event: e })
|
||||
}
|
||||
|
@ -13,28 +13,28 @@ let server
|
|||
|
||||
function registerProcessListener () {
|
||||
process.on('message', function (msg) {
|
||||
console.log('child get msg: ' + JSON.stringify(msg))
|
||||
log.info('child get msg: ' + JSON.stringify(msg))
|
||||
if (msg.type === 'action') {
|
||||
api[msg.event.key](msg.event.params)
|
||||
}
|
||||
})
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
console.log('on sigint : closed ')
|
||||
log.info('on sigint : closed ')
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
// 避免异常崩溃
|
||||
process.on('uncaughtException', function (err) {
|
||||
if (err.code === 'ECONNABORTED') {
|
||||
// console.error(err.errno)
|
||||
// log.error(err.errno)
|
||||
return
|
||||
}
|
||||
console.error('uncaughtException:', err)
|
||||
log.error('uncaughtException:', err)
|
||||
})
|
||||
|
||||
process.on('unhandledRejection', (reason, p) => {
|
||||
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason)
|
||||
log.info('Unhandled Rejection at: Promise', p, 'reason:', reason)
|
||||
// application specific logging, throwing an error, or other logic here
|
||||
})
|
||||
}
|
||||
|
@ -49,17 +49,17 @@ const api = {
|
|||
}
|
||||
const newServer = mitmproxy.createProxy(proxyOptions, () => {
|
||||
fireStatus(true)
|
||||
console.log('代理服务已启动:127.0.0.1:' + proxyOptions.port)
|
||||
log.info('代理服务已启动:127.0.0.1:' + proxyOptions.port)
|
||||
})
|
||||
newServer.on('close', () => {
|
||||
console.log('server will closed ')
|
||||
log.info('server will closed ')
|
||||
if (server === newServer) {
|
||||
server = null
|
||||
fireStatus(false)
|
||||
}
|
||||
})
|
||||
newServer.on('error', (e) => {
|
||||
console.log('server error', e)
|
||||
log.info('server error', e)
|
||||
// newServer = null
|
||||
fireError(e)
|
||||
})
|
||||
|
@ -72,20 +72,20 @@ const api = {
|
|||
if (server) {
|
||||
server.close((err) => {
|
||||
if (err) {
|
||||
console.log('close error', err, ',', err.code, ',', err.message, ',', err.errno)
|
||||
log.info('close error', err, ',', err.code, ',', err.message, ',', err.errno)
|
||||
if (err.code === 'ERR_SERVER_NOT_RUNNING') {
|
||||
console.log('代理服务关闭成功')
|
||||
log.info('代理服务关闭成功')
|
||||
resolve()
|
||||
return
|
||||
}
|
||||
reject(err)
|
||||
} else {
|
||||
console.log('代理服务关闭成功')
|
||||
log.info('代理服务关闭成功')
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
console.log('server is null')
|
||||
log.info('server is null')
|
||||
fireStatus(false)
|
||||
resolve()
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
const LRU = require('lru-cache')
|
||||
// const { isIP } = require('validator')
|
||||
const getLogger = require('../utils/logger')
|
||||
|
||||
const logger = getLogger('dns')
|
||||
const log = require('../../utils/util.log')
|
||||
const cacheSize = 1024
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
// function _isIP (v) {
|
||||
|
@ -104,11 +102,11 @@ module.exports = class BaseDNS {
|
|||
|
||||
ipCache.setIpList(ipList)
|
||||
|
||||
logger.debug(`[DNS] ${hostname} -> ${ipCache.ip} (${new Date() - t} ms)`)
|
||||
log.info(`[DNS] ${hostname} -> ${ipCache.ip} (${new Date() - t} ms)`)
|
||||
|
||||
return ipCache.ip
|
||||
} catch (error) {
|
||||
logger.debug(`[DNS] cannot resolve hostname ${hostname} (${error})`)
|
||||
log.error(`[DNS] cannot resolve hostname ${hostname} (${error})`)
|
||||
return hostname
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const { promisify } = require('util')
|
||||
const doh = require('dns-over-http')
|
||||
const BaseDNS = require('./base')
|
||||
|
||||
const log = require('../../utils/util.log')
|
||||
const dohQueryAsync = promisify(doh.query)
|
||||
|
||||
module.exports = class DNSOverHTTPS extends BaseDNS {
|
||||
|
@ -15,18 +15,18 @@ module.exports = class DNSOverHTTPS extends BaseDNS {
|
|||
const result = await dohQueryAsync({ url: this.dnsServer }, [{ type: 'A', name: hostname }])
|
||||
if (result.answers.length === 0) {
|
||||
// 说明没有获取到ip
|
||||
console.log('该域名没有ip地址解析', hostname)
|
||||
log.info('该域名没有ip地址解析', hostname)
|
||||
return []
|
||||
}
|
||||
const ret = result.answers.filter(item => { return item.type === 'A' }).map(item => { return item.data })
|
||||
if (ret.length === 0) {
|
||||
console.log('该域名没有ipv4地址解析', hostname)
|
||||
log.info('该域名没有ipv4地址解析', hostname)
|
||||
} else {
|
||||
console.log('获取到域名地址:', hostname, JSON.stringify(ret))
|
||||
log.info('获取到域名地址:', hostname, JSON.stringify(ret))
|
||||
}
|
||||
return ret
|
||||
} catch (err) {
|
||||
console.log('dns query error', hostname, err)
|
||||
log.info('dns query error', hostname, err.message)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
module.exports = {
|
||||
requestInterceptor (interceptOpt, rOptions, req, res, ssl) {
|
||||
console.log('abort:', rOptions.hostname, req.url)
|
||||
res.writeHead(403)
|
||||
res.write('DevSidecar 403: \n\n request abort, this request is matched by abort intercept.\n\n 因配置abort拦截器,本请求将取消')
|
||||
res.end()
|
||||
},
|
||||
is (interceptOpt) {
|
||||
return !!interceptOpt.abort
|
||||
|
||||
module.exports = function createIntercept (context) {
|
||||
const { log } = context
|
||||
return {
|
||||
requestInterceptor (interceptOpt, rOptions, req, res, ssl) {
|
||||
log.info('abort:', rOptions.hostname, req.url)
|
||||
res.writeHead(403)
|
||||
res.write('DevSidecar 403: \n\n request abort, this request is matched by abort intercept.\n\n 因配置abort拦截器,本请求将取消')
|
||||
res.end()
|
||||
},
|
||||
is (interceptOpt) {
|
||||
return !!interceptOpt.abort
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,25 @@
|
|||
const url = require('url')
|
||||
module.exports = {
|
||||
requestInterceptor (interceptOpt, rOptions, req, res, ssl, next) {
|
||||
const proxyTarget = interceptOpt.proxy
|
||||
// const backup = interceptOpt.backup
|
||||
const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
|
||||
// eslint-disable-next-line node/no-deprecated-api
|
||||
const URL = url.parse(proxy)
|
||||
rOptions.protocol = URL.protocol
|
||||
rOptions.hostname = URL.host
|
||||
rOptions.host = URL.host
|
||||
rOptions.headers.host = URL.host
|
||||
if (URL.port == null) {
|
||||
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
|
||||
}
|
||||
module.exports = function createInterceptor (context) {
|
||||
const { log } = context
|
||||
return {
|
||||
requestInterceptor (interceptOpt, rOptions, req, res, ssl, next) {
|
||||
const proxyTarget = interceptOpt.proxy
|
||||
// const backup = interceptOpt.backup
|
||||
const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
|
||||
// eslint-disable-next-line node/no-deprecated-api
|
||||
const URL = url.parse(proxy)
|
||||
rOptions.protocol = URL.protocol
|
||||
rOptions.hostname = URL.host
|
||||
rOptions.host = URL.host
|
||||
rOptions.headers.host = URL.host
|
||||
if (URL.port == null) {
|
||||
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
|
||||
}
|
||||
|
||||
console.log('proxy:', rOptions.hostname, req.url, proxyTarget)
|
||||
},
|
||||
is (interceptOpt) {
|
||||
return !!interceptOpt.proxy
|
||||
log.info('proxy:', rOptions.hostname, req.url, proxyTarget)
|
||||
},
|
||||
is (interceptOpt) {
|
||||
return !!interceptOpt.proxy
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,21 @@
|
|||
module.exports = {
|
||||
requestInterceptor (interceptOpt, rOptions, req, res, ssl) {
|
||||
const url = req.url
|
||||
let redirect
|
||||
if (typeof interceptOpt.redirect === 'string') {
|
||||
redirect = rOptions.protocol + '//' + interceptOpt.redirect + url
|
||||
} else {
|
||||
redirect = interceptOpt.redirect(url)
|
||||
module.exports = function createInterceptor (context) {
|
||||
const { log } = context
|
||||
return {
|
||||
requestInterceptor (interceptOpt, rOptions, req, res, ssl) {
|
||||
const url = req.url
|
||||
let redirect
|
||||
if (typeof interceptOpt.redirect === 'string') {
|
||||
redirect = rOptions.protocol + '//' + interceptOpt.redirect + url
|
||||
} else {
|
||||
redirect = interceptOpt.redirect(url)
|
||||
}
|
||||
log.info('请求重定向:', rOptions.hostname, url, redirect)
|
||||
res.writeHead(302, { Location: redirect })
|
||||
res.end()
|
||||
return true
|
||||
},
|
||||
is (interceptOpt) {
|
||||
return interceptOpt.redirect // 如果配置中有redirect,那么这个配置是需要redirect拦截的
|
||||
}
|
||||
console.log('请求重定向:', rOptions.hostname, url, redirect)
|
||||
res.writeHead(302, { Location: redirect })
|
||||
res.end()
|
||||
return true
|
||||
},
|
||||
is (interceptOpt) {
|
||||
return interceptOpt.redirect // 如果配置中有redirect,那么这个配置是需要redirect拦截的
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
const proxy = require('./impl/proxy')
|
||||
const redirect = require('./impl/redirect')
|
||||
const abort = require('./impl/abort')
|
||||
|
||||
const modules = [proxy, redirect, abort]
|
||||
const log = require('../../utils/util.log')
|
||||
const context = { log }
|
||||
const modules = [proxy(context), redirect(context), abort(context)]
|
||||
|
||||
module.exports = modules
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
require('babel-polyfill')
|
||||
const mitmproxy = require('../mitmproxy')
|
||||
const program = require('commander')
|
||||
const packageJson = require('../../package.json')
|
||||
// const tlsUtils = require('../tls/tlsUtils')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const colors = require('colors')
|
||||
|
||||
fs.existsSync = fs.existsSync || path.existsSync
|
||||
|
||||
program
|
||||
.version(packageJson.version)
|
||||
.option('-c, --config [value]', 'config file path')
|
||||
.parse(process.argv)
|
||||
|
||||
console.log(program.config)
|
||||
|
||||
const configPath = path.resolve(program.config)
|
||||
|
||||
if (fs.existsSync(configPath)) {
|
||||
const configObject = require(configPath)
|
||||
|
||||
if (typeof configObject !== 'object') {
|
||||
console.error(colors.red(`Config Error in ${configPath}`))
|
||||
} else {
|
||||
mitmproxy.createProxy(configObject)
|
||||
}
|
||||
} else {
|
||||
console.error(colors.red(`Can not find \`config file\` file: ${configPath}`))
|
||||
}
|
|
@ -2,7 +2,7 @@ const url = require('url')
|
|||
const Agent = require('./ProxyHttpAgent')
|
||||
const HttpsAgent = require('./ProxyHttpsAgent')
|
||||
const tunnelAgent = require('tunnel-agent')
|
||||
|
||||
const log = require('../../../utils/util.log')
|
||||
const util = exports
|
||||
const httpsAgent = new HttpsAgent({
|
||||
keepAlive: true,
|
||||
|
@ -34,7 +34,7 @@ util.getOptionsFormRequest = (req, ssl, externalProxy = null) => {
|
|||
try {
|
||||
externalProxyUrl = externalProxy(req, ssl)
|
||||
} catch (e) {
|
||||
console.error('externalProxy', e)
|
||||
log.error('externalProxy', e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const net = require('net')
|
||||
const url = require('url')
|
||||
const log = require('../../../utils/util.log')
|
||||
// const colors = require('colors')
|
||||
const DnsUtil = require('../../dns/index')
|
||||
const localIP = '127.0.0.1'
|
||||
|
@ -15,7 +16,7 @@ module.exports = function createConnectHandler (sslConnectInterceptor, fakeServe
|
|||
fakeServerCenter.getServerPromise(hostname, srvUrl.port).then((serverObj) => {
|
||||
connect(req, cltSocket, head, localIP, serverObj.port)
|
||||
}, (e) => {
|
||||
console.error('getServerPromise', e)
|
||||
log.error('getServerPromise', e)
|
||||
})
|
||||
} else {
|
||||
connect(req, cltSocket, head, hostname, srvUrl.port, dnsConfig)
|
||||
|
@ -25,7 +26,7 @@ module.exports = function createConnectHandler (sslConnectInterceptor, fakeServe
|
|||
|
||||
function connect (req, cltSocket, head, hostname, port, dnsConfig) {
|
||||
// tunneling https
|
||||
// console.log('connect:', hostname, port)
|
||||
// log.info('connect:', hostname, port)
|
||||
const start = new Date().getTime()
|
||||
let isDnsIntercept = null
|
||||
try {
|
||||
|
@ -62,21 +63,21 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig) {
|
|||
|
||||
proxySocket.on('timeout', () => {
|
||||
const end = new Date().getTime()
|
||||
console.log('代理socket timeout:', hostname, port, (end - start) + 'ms')
|
||||
log.info('代理socket timeout:', hostname, port, (end - start) + 'ms')
|
||||
})
|
||||
proxySocket.on('error', (e) => {
|
||||
// 连接失败,可能被GFW拦截,或者服务端拥挤
|
||||
const end = new Date().getTime()
|
||||
console.error('代理连接失败:', e.message, hostname, port, (end - start) + 'ms')
|
||||
log.error('代理连接失败:', e.message, hostname, port, (end - start) + 'ms')
|
||||
cltSocket.destroy()
|
||||
if (isDnsIntercept) {
|
||||
const { dns, ip, hostname } = isDnsIntercept
|
||||
dns.count(hostname, ip, true)
|
||||
console.error('记录ip失败次数,用于优选ip:', hostname, ip)
|
||||
log.error('记录ip失败次数,用于优选ip:', hostname, ip)
|
||||
}
|
||||
})
|
||||
return proxySocket
|
||||
} catch (error) {
|
||||
console.log('connect err', error)
|
||||
log.error('connect err', error)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ const fs = require('fs')
|
|||
const forge = require('node-forge')
|
||||
const FakeServersCenter = require('../tls/FakeServersCenter')
|
||||
const colors = require('colors')
|
||||
|
||||
const log = require('../../../utils/util.log')
|
||||
module.exports = function createFakeServerCenter ({
|
||||
caCertPath,
|
||||
caKeyPath,
|
||||
|
@ -20,7 +20,7 @@ module.exports = function createFakeServerCenter ({
|
|||
caCert = forge.pki.certificateFromPem(caCertPem)
|
||||
caKey = forge.pki.privateKeyFromPem(caKeyPem)
|
||||
} catch (e) {
|
||||
console.log(colors.red('Can not find `CA certificate` or `CA key`.'), e)
|
||||
log.info(colors.red('Can not find `CA certificate` or `CA key`.'), e)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ const https = require('https')
|
|||
const commonUtil = require('../common/util')
|
||||
// const upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i
|
||||
const DnsUtil = require('../../dns/index')
|
||||
|
||||
const log = require('../../../utils/util.log')
|
||||
// create requestHandler function
|
||||
module.exports = function createRequestHandler (requestInterceptor, responseInterceptor, middlewares, externalProxy, dnsConfig) {
|
||||
// return
|
||||
|
@ -43,7 +43,7 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
|
|||
// const dns = DnsUtil.hasDnsLookup(dnsConfig, rOptions.host)
|
||||
// if (dns) {
|
||||
// const ip = await dns.lookup(rOptions.host)
|
||||
// console.log('使用自定义dns:', rOptions.host, ip, dns.dnsServer)
|
||||
// log.info('使用自定义dns:', rOptions.host, ip, dns.dnsServer)
|
||||
// rOptions.host = ip
|
||||
// }
|
||||
// }
|
||||
|
@ -63,7 +63,7 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
|
|||
function onFree () {
|
||||
const url = `${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}`
|
||||
const start = new Date().getTime()
|
||||
console.log('代理请求:', url, rOptions.method)
|
||||
log.info('代理请求:', url, rOptions.method)
|
||||
let isDnsIntercept
|
||||
if (dnsConfig) {
|
||||
const dns = DnsUtil.hasDnsLookup(dnsConfig, rOptions.hostname)
|
||||
|
@ -80,11 +80,10 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
proxyReq = (rOptions.protocol === 'https:' ? https : http).request(rOptions, (proxyRes) => {
|
||||
const end = new Date().getTime()
|
||||
if (rOptions.protocol === 'https:') {
|
||||
console.log('代理请求返回:', url, (end - start) + 'ms')
|
||||
log.info('代理请求返回:', url, (end - start) + 'ms')
|
||||
}
|
||||
resolve(proxyRes)
|
||||
})
|
||||
|
@ -94,10 +93,15 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
|
|||
if (isDnsIntercept) {
|
||||
const { dns, ip, hostname } = isDnsIntercept
|
||||
dns.count(hostname, ip, true)
|
||||
console.error('记录ip失败次数,用于优选ip:', hostname, ip)
|
||||
log.error('记录ip失败次数,用于优选ip:', hostname, ip)
|
||||
}
|
||||
console.error('代理请求超时', rOptions.protocol, rOptions.hostname, rOptions.path, (end - start) + 'ms')
|
||||
reject(new Error(`${rOptions.host}:${rOptions.port}, 代理请求超时`))
|
||||
log.error('代理请求超时', rOptions.protocol, rOptions.hostname, rOptions.path, (end - start) + 'ms')
|
||||
// reject(new Error(`${rOptions.host}:${rOptions.port}, 代理请求超时`))
|
||||
proxyReq.end()
|
||||
proxyReq.destroy()
|
||||
res.writeHead(408)
|
||||
res.write('DevSidecar Warning:\n\n 请求超时')
|
||||
res.end()
|
||||
})
|
||||
|
||||
proxyReq.on('error', (e) => {
|
||||
|
@ -105,28 +109,34 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
|
|||
if (isDnsIntercept) {
|
||||
const { dns, ip, hostname } = isDnsIntercept
|
||||
dns.count(hostname, ip, true)
|
||||
console.error('记录ip失败次数,用于优选ip:', hostname, ip)
|
||||
log.error('记录ip失败次数,用于优选ip:', hostname, ip)
|
||||
}
|
||||
console.error('代理请求错误', e.errno, rOptions.hostname, rOptions.path, (end - start) + 'ms', e)
|
||||
log.error('代理请求错误', e.code, e.message, rOptions.hostname, rOptions.path, (end - start) + 'ms')
|
||||
reject(e)
|
||||
})
|
||||
|
||||
proxyReq.on('aborted', () => {
|
||||
console.error('代理请求被取消', rOptions.hostname, rOptions.path)
|
||||
log.error('代理请求被取消', rOptions.hostname, rOptions.path)
|
||||
if (res.finished) {
|
||||
return
|
||||
}
|
||||
reject(new Error('代理请求被取消'))
|
||||
})
|
||||
|
||||
req.on('aborted', function () {
|
||||
console.error('请求被取消', rOptions.hostname, rOptions.path)
|
||||
log.error('请求被取消', rOptions.hostname, rOptions.path)
|
||||
proxyReq.abort()
|
||||
if (res.finished) {
|
||||
return
|
||||
}
|
||||
reject(new Error('请求被取消'))
|
||||
})
|
||||
req.on('error', function (e, req, res) {
|
||||
console.error('请求错误:', e.errno, rOptions.hostname, rOptions.path)
|
||||
log.error('请求错误:', e.errno, rOptions.hostname, rOptions.path)
|
||||
reject(e)
|
||||
})
|
||||
req.on('timeout', () => {
|
||||
console.error('请求超时', rOptions.hostname, rOptions.path)
|
||||
log.error('请求超时', rOptions.hostname, rOptions.path)
|
||||
reject(new Error(`${rOptions.hostname}:${rOptions.port}, 请求超时`))
|
||||
})
|
||||
req.pipe(proxyReq)
|
||||
|
@ -191,8 +201,8 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
|
|||
res.writeHead(500)
|
||||
res.write(`DevSidecar Warning:\n\n ${e.toString()}`)
|
||||
res.end()
|
||||
log.error('request error', e.message)
|
||||
}
|
||||
console.error('request error', e.message)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const http = require('http')
|
||||
const https = require('https')
|
||||
const util = require('../common/util')
|
||||
|
||||
const log = require('../../../utils/util.log')
|
||||
// copy from node-http-proxy. ^_^
|
||||
|
||||
// create connectHandler function
|
||||
|
@ -11,7 +11,7 @@ module.exports = function createUpgradeHandler () {
|
|||
const clientOptions = util.getOptionsFormRequest(req, ssl)
|
||||
const proxyReq = (ssl ? https : http).request(clientOptions)
|
||||
proxyReq.on('error', (e) => {
|
||||
console.error(e)
|
||||
log.error('upgradeHandler', e)
|
||||
})
|
||||
proxyReq.on('response', function (res) {
|
||||
// if upgrade event isn't going to happen, close the socket
|
||||
|
@ -20,12 +20,11 @@ module.exports = function createUpgradeHandler () {
|
|||
|
||||
proxyReq.on('upgrade', function (proxyRes, proxySocket, proxyHead) {
|
||||
proxySocket.on('error', (e) => {
|
||||
console.log('error-----1111')
|
||||
console.error(e)
|
||||
log.error('on upgrade:', e)
|
||||
})
|
||||
|
||||
cltSocket.on('error', function () {
|
||||
console.log('error-----2222')
|
||||
cltSocket.on('error', function (e) {
|
||||
log.error('upgrade socket ', e)
|
||||
proxySocket.end()
|
||||
})
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const tlsUtils = require('../tls/tlsUtils')
|
||||
const http = require('http')
|
||||
const config = require('../common/config')
|
||||
const colors = require('colors')
|
||||
const log = require('../../../utils/util.log')
|
||||
const createRequestHandler = require('./createRequestHandler')
|
||||
const createConnectHandler = require('./createConnectHandler')
|
||||
const createFakeServerCenter = require('./createFakeServerCenter')
|
||||
|
@ -27,8 +27,8 @@ module.exports = {
|
|||
caCertPath = rs.caCertPath
|
||||
caKeyPath = rs.caKeyPath
|
||||
if (rs.create) {
|
||||
console.log(colors.cyan(`CA Cert saved in: ${caCertPath}`))
|
||||
console.log(colors.cyan(`CA private key saved in: ${caKeyPath}`))
|
||||
log.info(`CA Cert saved in: ${caCertPath}`)
|
||||
log.info(`CA private key saved in: ${caKeyPath}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,18 +59,18 @@ module.exports = {
|
|||
|
||||
const server = new http.Server()
|
||||
server.listen(port, () => {
|
||||
console.log(colors.green(`dev-sidecar启动端口: ${port}`))
|
||||
log.info(`dev-sidecar启动端口: ${port}`)
|
||||
server.on('error', (e) => {
|
||||
console.error(colors.red(e))
|
||||
log.error('server error', e)
|
||||
})
|
||||
server.on('request', (req, res) => {
|
||||
const ssl = false
|
||||
// console.log('request,', req.url, req.port, req.host)
|
||||
// log.info('request,', req.url, req.port, req.host)
|
||||
requestHandler(req, res, ssl)
|
||||
})
|
||||
// tunneling for https
|
||||
server.on('connect', (req, cltSocket, head) => {
|
||||
// console.log('connect,', req.url)
|
||||
// log.info('connect,', req.url)
|
||||
connectHandler(req, cltSocket, head)
|
||||
})
|
||||
// TODO: handler WebSocket
|
||||
|
|
|
@ -5,7 +5,7 @@ const forge = require('node-forge')
|
|||
const pki = forge.pki
|
||||
// const colors = require('colors')
|
||||
const tls = require('tls')
|
||||
|
||||
const log = require('../../../utils/util.log')
|
||||
module.exports = class FakeServersCenter {
|
||||
constructor ({ maxLength = 256, requestHandler, upgradeHandler, caCert, caKey, getCertSocketTimeout }) {
|
||||
this.queue = []
|
||||
|
@ -23,13 +23,13 @@ module.exports = class FakeServersCenter {
|
|||
if (this.queue.length >= this.maxLength) {
|
||||
const delServerObj = this.queue.shift()
|
||||
try {
|
||||
console.log('超过最大服务数量,删除旧服务', delServerObj)
|
||||
log.info('超过最大服务数量,删除旧服务', delServerObj)
|
||||
delServerObj.serverObj.server.close()
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
log.info(e)
|
||||
}
|
||||
}
|
||||
console.log('add server promise:', serverPromiseObj)
|
||||
log.info('add server promise:', serverPromiseObj)
|
||||
this.queue.push(serverPromiseObj)
|
||||
return serverPromiseObj
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ module.exports = class FakeServersCenter {
|
|||
SNICallback: (hostname, done) => {
|
||||
(async () => {
|
||||
const certObj = await this.certAndKeyContainer.getCertPromise(hostname, port)
|
||||
console.log('sni callback:', hostname)
|
||||
log.info('sni callback:', hostname)
|
||||
done(null, tls.createSecureContext({
|
||||
key: pki.privateKeyToPem(certObj.key),
|
||||
cert: pki.certificateToPem(certObj.cert)
|
||||
|
@ -88,7 +88,7 @@ module.exports = class FakeServersCenter {
|
|||
this.requestHandler(req, res, ssl)
|
||||
})
|
||||
fakeServer.on('error', (e) => {
|
||||
console.error(e)
|
||||
log.error(e)
|
||||
})
|
||||
fakeServer.on('listening', () => {
|
||||
const mappingHostNames = tlsUtils.getMappingHostNamesFormCert(certObj.cert)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
const interceptors = require('./lib/interceptor')
|
||||
const dnsUtil = require('./lib/dns')
|
||||
const lodash = require('lodash')
|
||||
const log = require('./utils/util.log')
|
||||
function matchHostname (hostMap, hostname) {
|
||||
const value = hostMap[hostname]
|
||||
if (value) {
|
||||
|
@ -57,7 +58,7 @@ module.exports = (config) => {
|
|||
const hostname = req.url.split(':')[0]
|
||||
const inWhiteList = matchHostname(whiteList, hostname) != null
|
||||
if (inWhiteList) {
|
||||
console.log('白名单域名,不拦截', hostname)
|
||||
log.info('白名单域名,不拦截', hostname)
|
||||
return false
|
||||
}
|
||||
return !!matchHostname(intercepts, hostname) // 配置了拦截的域名,将会被代理
|
||||
|
@ -89,7 +90,7 @@ module.exports = (config) => {
|
|||
}
|
||||
} catch (err) {
|
||||
// 拦截失败
|
||||
console.error(err)
|
||||
log.error('拦截器执行错误', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
const os = require('os')
|
||||
const log = require('util.log')
|
||||
const util = {
|
||||
getNodeVersion () {
|
||||
const version = process.version
|
||||
console.log(version)
|
||||
log.info(version)
|
||||
}
|
||||
}
|
||||
util.getNodeVersion()
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
const log4js = require('log4js')
|
||||
const proxyConfig = require('../lib/proxy/common/config')
|
||||
log4js.configure({
|
||||
appenders: { std: { type: 'stdout' }, file: { type: 'file', pattern: '.yyyy-MM-dd', daysToKeep: 3, filename: proxyConfig.getDefaultCABasePath() + '/logs/server.log' } },
|
||||
categories: { default: { appenders: ['file', 'std'], level: 'info' } }
|
||||
})
|
||||
const logger = log4js.getLogger('server')
|
||||
module.exports = logger
|
Loading…
Reference in New Issue