Skip to content

Commit 52805fe

Browse files
计算cost所需的对象,由 new Date() 调整为 Date.now()
1 parent bdcff4a commit 52805fe

4 files changed

Lines changed: 16 additions & 16 deletions

File tree

packages/mitmproxy/src/lib/dns/base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module.exports = class BaseDNS {
7070
this.cache.set(hostname, ipCache)
7171
}
7272

73-
const t = new Date()
73+
const t = Date.now()
7474
let ipList = await this._lookupWithPreSetIpList(hostname)
7575
if (ipList == null) {
7676
// 没有获取到ipv4地址
@@ -81,7 +81,7 @@ module.exports = class BaseDNS {
8181
ipCache.setBackupList(ipList)
8282

8383
const ip = ipCache.value
84-
log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] ${hostname}${ip} (${new Date() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))
84+
log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] ${hostname}${ip} (${Date.now() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))
8585

8686
if (ipChecker) {
8787
if (ip != null && ip !== hostname && ipChecker(ip)) {

packages/mitmproxy/src/lib/proxy/mitmproxy/createConnectHandler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = function createConnectHandler (sslConnectInterceptor, middlewar
5555
function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDirect = false, target = null) {
5656
// tunneling https
5757
// log.info('connect:', hostname, port)
58-
const start = new Date()
58+
const start = Date.now()
5959
const isDnsIntercept = {}
6060
const hostport = `${hostname}:${port}`
6161

@@ -134,7 +134,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire
134134
cltSocket.pipe(proxySocket)
135135
})
136136
proxySocket.on('timeout', () => {
137-
const cost = new Date() - start
137+
const cost = Date.now() - start
138138
const errorMsg = `${isDirect ? '直连' : '代理连接'}超时: ${hostport}, cost: ${cost} ms`
139139
log.error(errorMsg)
140140

@@ -148,7 +148,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire
148148
})
149149
proxySocket.on('error', (e) => {
150150
// 连接失败,可能被GFW拦截,或者服务端拥挤
151-
const cost = new Date() - start
151+
const cost = Date.now() - start
152152
const errorMsg = `${isDirect ? '直连' : '代理连接'}失败: ${hostport}, cost: ${cost} ms, errorMsg: ${e.message}`
153153
log.error(`${errorMsg}\r\n`, e)
154154

packages/mitmproxy/src/lib/proxy/mitmproxy/createRequestHandler.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
109109

110110
function onFree () {
111111
url = `${rOptions.method}${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}`
112-
const start = new Date()
112+
const start = Date.now()
113113
log.info('发起代理请求:', url, (rOptions.servername ? `, sni: ${rOptions.servername}` : ''), ', headers:', jsonApi.stringify2(rOptions.headers))
114114

115115
const isDnsIntercept = {}
@@ -156,7 +156,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
156156
}
157157

158158
proxyReq = (rOptions.protocol === 'https:' ? https : http).request(rOptions, (proxyRes) => {
159-
const cost = new Date() - start
159+
const cost = Date.now() - start
160160
if (rOptions.protocol === 'https:') {
161161
log.info(`代理请求返回: 【${proxyRes.statusCode}${url}, cost: ${cost} ms`)
162162
} else {
@@ -173,7 +173,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
173173

174174
// 代理请求的事件监听
175175
proxyReq.on('timeout', () => {
176-
const cost = new Date() - start
176+
const cost = Date.now() - start
177177
const errorMsg = `代理请求超时: ${url}, cost: ${cost} ms`
178178
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
179179
countSlow(isDnsIntercept, `代理请求超时, cost: ${cost} ms`)
@@ -184,7 +184,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
184184
reject(error)
185185
})
186186
proxyReq.on('error', (e) => {
187-
const cost = new Date() - start
187+
const cost = Date.now() - start
188188
log.error(`代理请求错误: ${url}, cost: ${cost} ms, error:`, e, ', rOptions:', jsonApi.stringify2(rOptions))
189189
countSlow(isDnsIntercept, `代理请求错误: ${e.message}`)
190190
reject(e)
@@ -195,7 +195,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
195195
}
196196
})
197197
proxyReq.on('aborted', () => {
198-
const cost = new Date() - start
198+
const cost = Date.now() - start
199199
const errorMsg = `代理请求被取消: ${url}, cost: ${cost} ms`
200200
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
201201

@@ -211,7 +211,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
211211

212212
// 原始请求的事件监听
213213
req.on('aborted', () => {
214-
const cost = new Date() - start
214+
const cost = Date.now() - start
215215
const errorMsg = `请求被取消: ${url}, cost: ${cost} ms`
216216
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
217217
proxyReq.abort()
@@ -221,12 +221,12 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
221221
reject(new Error(errorMsg))
222222
})
223223
req.on('error', (e, req, res) => {
224-
const cost = new Date() - start
224+
const cost = Date.now() - start
225225
log.error(`请求错误: ${url}, cost: ${cost} ms, error:`, e, ', rOptions:', jsonApi.stringify2(rOptions))
226226
reject(e)
227227
})
228228
req.on('timeout', () => {
229-
const cost = new Date() - start
229+
const cost = Date.now() - start
230230
const errorMsg = `请求超时: ${url}, cost: ${cost} ms`
231231
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
232232
reject(new Error(errorMsg))

packages/mitmproxy/src/lib/proxy/tls/tlsUtils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ utils.createCA = function (CN) {
2222
const keys = pki.rsa.generateKeyPair(2048)
2323
const cert = pki.createCertificate()
2424
cert.publicKey = keys.publicKey
25-
cert.serialNumber = `${(new Date()).getTime()}`
26-
cert.validity.notBefore = new Date(new Date() - (60 * 60 * 1000))
25+
cert.serialNumber = `${Date.now()}`
26+
cert.validity.notBefore = new Date(Date.now() - (60 * 60 * 1000))
2727
cert.validity.notAfter = new Date()
2828
cert.validity.notAfter.setFullYear(cert.validity.notAfter.getFullYear() + 20)
2929
const attrs = [{
@@ -87,7 +87,7 @@ utils.createFakeCertificateByDomain = function (caKey, caCert, domain, mappingHo
8787
const cert = pki.createCertificate()
8888
cert.publicKey = keys.publicKey
8989

90-
cert.serialNumber = `${(new Date()).getTime()}`
90+
cert.serialNumber = `${Date.now()}`
9191
cert.validity.notBefore = new Date()
9292
cert.validity.notBefore.setFullYear(cert.validity.notBefore.getFullYear() - 1)
9393
cert.validity.notAfter = new Date()

0 commit comments

Comments
 (0)