(app: FastifyInstance)
| 129 | } |
| 130 | |
| 131 | export async function monitorRoutes(app: FastifyInstance) { |
| 132 | app.get('/api/monitor/config', { preHandler: [limitMonitorConfigRead] }, async () => { |
| 133 | const ldohCookie = await getSettingString(LDOH_COOKIE_SETTING_KEY); |
| 134 | return { |
| 135 | ldohCookieConfigured: !!ldohCookie, |
| 136 | ldohCookieMasked: ldohCookie ? maskCookieValue(ldohCookie) : '', |
| 137 | }; |
| 138 | }); |
| 139 | |
| 140 | app.put<{ Body: unknown }>( |
| 141 | '/api/monitor/config', |
| 142 | { preHandler: [limitMonitorConfigWrite] }, |
| 143 | async (request, reply) => { |
| 144 | const parsedBody = parseMonitorConfigPayload(request.body); |
| 145 | if (!parsedBody.success) { |
| 146 | return reply.code(400).send({ success: false, message: parsedBody.error }); |
| 147 | } |
| 148 | |
| 149 | const raw = String(parsedBody.data.ldohCookie || '').trim(); |
| 150 | if (!raw) { |
| 151 | await upsertSetting(LDOH_COOKIE_SETTING_KEY, ''); |
| 152 | return { success: true, message: 'LDOH Cookie 已清空', ldohCookieConfigured: false }; |
| 153 | } |
| 154 | |
| 155 | const normalized = normalizeLdohCookie(raw); |
| 156 | if (!normalized.startsWith('ld_auth_session=') || normalized.length < 24) { |
| 157 | return reply.code(400).send({ success: false, message: 'Cookie 格式无效,请填写 ld_auth_session 或其值' }); |
| 158 | } |
| 159 | |
| 160 | await upsertSetting(LDOH_COOKIE_SETTING_KEY, normalized); |
| 161 | return { |
| 162 | success: true, |
| 163 | message: 'LDOH Cookie 已保存', |
| 164 | ldohCookieConfigured: true, |
| 165 | ldohCookieMasked: maskCookieValue(normalized), |
| 166 | }; |
| 167 | }, |
| 168 | ); |
| 169 | |
| 170 | app.post('/api/monitor/session', { preHandler: [limitMonitorSession] }, async (_, reply) => { |
| 171 | // HttpOnly cookie for iframe proxy auth within current origin. |
| 172 | reply.header( |
| 173 | 'Set-Cookie', |
| 174 | `${MONITOR_AUTH_COOKIE}=${config.authToken}; Path=/; HttpOnly; SameSite=Lax; Max-Age=7200`, |
| 175 | ); |
| 176 | return { success: true }; |
| 177 | }); |
| 178 | |
| 179 | const handleLdohProxy = async (request: FastifyRequest, reply: FastifyReply) => { |
| 180 | if (!ensureMonitorAuth(request, reply)) return; |
| 181 | |
| 182 | const storedCookie = await getSettingString(LDOH_COOKIE_SETTING_KEY); |
| 183 | if (!storedCookie) { |
| 184 | return reply.code(400).send('LDOH cookie not configured'); |
| 185 | } |
| 186 | |
| 187 | const wildcardPath = resolveLdohProxyPath(request); |
| 188 | const targetUrl = new URL(`${LDOH_BASE_URL}/${wildcardPath}`); |
nothing calls this directly
no test coverage detected