feat(csms): 增强 OCPP 操作的日志记录,添加请求和响应的详细信息
This commit is contained in:
@@ -91,9 +91,18 @@ app.get(
|
||||
'/ocpp/:chargePointId',
|
||||
async (c, next) => {
|
||||
const chargePointId = c.req.param('chargePointId')
|
||||
const connInfo = getConnInfo(c)
|
||||
const remoteAddr = connInfo.remote.address
|
||||
const authHeader = c.req.header('Authorization')
|
||||
|
||||
console.info(
|
||||
`[OCPP][AUTH][BEGIN] cp=${chargePointId} remote=${remoteAddr} hasAuthHeader=${Boolean(authHeader)}`,
|
||||
)
|
||||
|
||||
if (!authHeader?.startsWith('Basic ')) {
|
||||
console.warn(
|
||||
`[OCPP][AUTH][REJECT] cp=${chargePointId} remote=${remoteAddr} reason=missing_or_invalid_scheme`,
|
||||
)
|
||||
c.header('WWW-Authenticate', 'Basic realm="OCPP"')
|
||||
return c.json({ error: 'Unauthorized' }, 401)
|
||||
}
|
||||
@@ -106,10 +115,16 @@ app.get(
|
||||
id = decoded.slice(0, colonIdx)
|
||||
password = decoded.slice(colonIdx + 1)
|
||||
} catch {
|
||||
console.warn(
|
||||
`[OCPP][AUTH][REJECT] cp=${chargePointId} remote=${remoteAddr} reason=invalid_authorization_header`,
|
||||
)
|
||||
return c.json({ error: 'Invalid Authorization header' }, 400)
|
||||
}
|
||||
|
||||
if (id !== chargePointId) {
|
||||
console.warn(
|
||||
`[OCPP][AUTH][REJECT] cp=${chargePointId} remote=${remoteAddr} reason=identity_mismatch authId=${id}`,
|
||||
)
|
||||
return c.json({ error: 'Unauthorized' }, 401)
|
||||
}
|
||||
|
||||
@@ -121,9 +136,14 @@ app.get(
|
||||
.limit(1)
|
||||
|
||||
if (!cp?.passwordHash || !(await verifyOcppPassword(password, cp.passwordHash))) {
|
||||
console.warn(
|
||||
`[OCPP][AUTH][REJECT] cp=${chargePointId} remote=${remoteAddr} reason=bad_credentials_or_missing_cp`,
|
||||
)
|
||||
return c.json({ error: 'Unauthorized' }, 401)
|
||||
}
|
||||
|
||||
console.info(`[OCPP][AUTH][PASS] cp=${chargePointId} remote=${remoteAddr}`)
|
||||
|
||||
await next()
|
||||
},
|
||||
upgradeWebSocket((c) => {
|
||||
|
||||
Reference in New Issue
Block a user