计算cost所需的对象,由 `new Date()` 调整为 `Date.now()`

release-2.0.0.2
王良 2025-04-18 11:57:34 +08:00
parent bdcff4a1cc
commit 52805fe313
4 changed files with 16 additions and 16 deletions

View File

@ -70,7 +70,7 @@ module.exports = class BaseDNS {
this.cache.set(hostname, ipCache)
}
const t = new Date()
const t = Date.now()
let ipList = await this._lookupWithPreSetIpList(hostname)
if (ipList == null) {
// 没有获取到ipv4地址
@ -81,7 +81,7 @@ module.exports = class BaseDNS {
ipCache.setBackupList(ipList)
const ip = ipCache.value
log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] ${hostname}${ip} (${new Date() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))
log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] ${hostname}${ip} (${Date.now() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))
if (ipChecker) {
if (ip != null && ip !== hostname && ipChecker(ip)) {

View File

@ -55,7 +55,7 @@ module.exports = function createConnectHandler (sslConnectInterceptor, middlewar
function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDirect = false, target = null) {
// tunneling https
// log.info('connect:', hostname, port)
const start = new Date()
const start = Date.now()
const isDnsIntercept = {}
const hostport = `${hostname}:${port}`
@ -134,7 +134,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire
cltSocket.pipe(proxySocket)
})
proxySocket.on('timeout', () => {
const cost = new Date() - start
const cost = Date.now() - start
const errorMsg = `${isDirect ? '直连' : '代理连接'}超时: ${hostport}, cost: ${cost} ms`
log.error(errorMsg)
@ -148,7 +148,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire
})
proxySocket.on('error', (e) => {
// 连接失败可能被GFW拦截或者服务端拥挤
const cost = new Date() - start
const cost = Date.now() - start
const errorMsg = `${isDirect ? '直连' : '代理连接'}失败: ${hostport}, cost: ${cost} ms, errorMsg: ${e.message}`
log.error(`${errorMsg}\r\n`, e)

View File

@ -109,7 +109,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
function onFree () {
url = `${rOptions.method}${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}`
const start = new Date()
const start = Date.now()
log.info('发起代理请求:', url, (rOptions.servername ? `, sni: ${rOptions.servername}` : ''), ', headers:', jsonApi.stringify2(rOptions.headers))
const isDnsIntercept = {}
@ -156,7 +156,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
}
proxyReq = (rOptions.protocol === 'https:' ? https : http).request(rOptions, (proxyRes) => {
const cost = new Date() - start
const cost = Date.now() - start
if (rOptions.protocol === 'https:') {
log.info(`代理请求返回: 【${proxyRes.statusCode}${url}, cost: ${cost} ms`)
} else {
@ -173,7 +173,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
// 代理请求的事件监听
proxyReq.on('timeout', () => {
const cost = new Date() - start
const cost = Date.now() - start
const errorMsg = `代理请求超时: ${url}, cost: ${cost} ms`
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
countSlow(isDnsIntercept, `代理请求超时, cost: ${cost} ms`)
@ -184,7 +184,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
reject(error)
})
proxyReq.on('error', (e) => {
const cost = new Date() - start
const cost = Date.now() - start
log.error(`代理请求错误: ${url}, cost: ${cost} ms, error:`, e, ', rOptions:', jsonApi.stringify2(rOptions))
countSlow(isDnsIntercept, `代理请求错误: ${e.message}`)
reject(e)
@ -195,7 +195,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
}
})
proxyReq.on('aborted', () => {
const cost = new Date() - start
const cost = Date.now() - start
const errorMsg = `代理请求被取消: ${url}, cost: ${cost} ms`
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
@ -211,7 +211,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
// 原始请求的事件监听
req.on('aborted', () => {
const cost = new Date() - start
const cost = Date.now() - start
const errorMsg = `请求被取消: ${url}, cost: ${cost} ms`
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
proxyReq.abort()
@ -221,12 +221,12 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
reject(new Error(errorMsg))
})
req.on('error', (e, req, res) => {
const cost = new Date() - start
const cost = Date.now() - start
log.error(`请求错误: ${url}, cost: ${cost} ms, error:`, e, ', rOptions:', jsonApi.stringify2(rOptions))
reject(e)
})
req.on('timeout', () => {
const cost = new Date() - start
const cost = Date.now() - start
const errorMsg = `请求超时: ${url}, cost: ${cost} ms`
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
reject(new Error(errorMsg))

View File

@ -22,8 +22,8 @@ utils.createCA = function (CN) {
const keys = pki.rsa.generateKeyPair(2048)
const cert = pki.createCertificate()
cert.publicKey = keys.publicKey
cert.serialNumber = `${(new Date()).getTime()}`
cert.validity.notBefore = new Date(new Date() - (60 * 60 * 1000))
cert.serialNumber = `${Date.now()}`
cert.validity.notBefore = new Date(Date.now() - (60 * 60 * 1000))
cert.validity.notAfter = new Date()
cert.validity.notAfter.setFullYear(cert.validity.notAfter.getFullYear() + 20)
const attrs = [{
@ -87,7 +87,7 @@ utils.createFakeCertificateByDomain = function (caKey, caCert, domain, mappingHo
const cert = pki.createCertificate()
cert.publicKey = keys.publicKey
cert.serialNumber = `${(new Date()).getTime()}`
cert.serialNumber = `${Date.now()}`
cert.validity.notBefore = new Date()
cert.validity.notBefore.setFullYear(cert.validity.notBefore.getFullYear() - 1)
cert.validity.notAfter = new Date()