Agregar endpoints proxy para evitar CORS entre subdominios
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m5s
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m5s
- /api/streams/list: proxy a streams.nucleoriofrio.com/api/streams - /api/frigate/event: proxy a camaras.nucleoriofrio.com/api/events - Actualizar composables para usar los proxies del backend - Los iframes de streaming siguen usando URLs directas (sesion propia)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Composable para gestionar eventos de Frigate
|
* Composable para gestionar eventos de Frigate
|
||||||
* API: https://camaras.nucleoriofrio.com/api/events/{camera}/create
|
* Usa proxy backend para evitar problemas de CORS/cookies entre subdominios
|
||||||
|
* API Proxy: /api/frigate/event
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export interface FrigateEventParams {
|
export interface FrigateEventParams {
|
||||||
@@ -24,14 +25,12 @@ export interface FrigateEventResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const useFrigateEvents = () => {
|
export const useFrigateEvents = () => {
|
||||||
const BASE_URL = 'https://camaras.nucleoriofrio.com'
|
|
||||||
|
|
||||||
const isCreating = useState<boolean>('frigate_creating', () => false)
|
const isCreating = useState<boolean>('frigate_creating', () => false)
|
||||||
const error = useState<string | null>('frigate_error', () => null)
|
const error = useState<string | null>('frigate_error', () => null)
|
||||||
const lastEventId = useState<string | null>('frigate_last_event', () => null)
|
const lastEventId = useState<string | null>('frigate_last_event', () => null)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crea un evento en Frigate para una camara especifica
|
* Crea un evento en Frigate para una camara especifica via proxy
|
||||||
*/
|
*/
|
||||||
const createEvent = async (
|
const createEvent = async (
|
||||||
camera: string,
|
camera: string,
|
||||||
@@ -41,17 +40,14 @@ export const useFrigateEvents = () => {
|
|||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Extraer el nombre base de la camara (sin _main o _sub)
|
// Usar proxy backend para evitar CORS/cookies issues
|
||||||
const cameraName = camera.replace(/_main$|_sub$/, '')
|
const response = await $fetch<FrigateEventResponse>('/api/frigate/event', {
|
||||||
|
|
||||||
const response = await $fetch<FrigateEventResponse>(
|
|
||||||
`${BASE_URL}/api/events/${cameraName}/create`,
|
|
||||||
{
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'include',
|
body: {
|
||||||
body: params
|
camera,
|
||||||
|
...params
|
||||||
}
|
}
|
||||||
)
|
})
|
||||||
|
|
||||||
if (response.event_id) {
|
if (response.event_id) {
|
||||||
lastEventId.value = response.event_id
|
lastEventId.value = response.event_id
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* Composable para gestionar streams de video desde go2rtc
|
* Composable para gestionar streams de video desde go2rtc
|
||||||
* API: https://streams.nucleoriofrio.com/api/streams
|
* Usa proxy backend para evitar problemas de CORS/cookies entre subdominios
|
||||||
|
* API Proxy: /api/streams/list
|
||||||
|
* Streaming: https://streams.nucleoriofrio.com (iframe con sesion propia)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export type StreamType = 'webrtc' | 'mse' | 'mp4' | 'hls' | 'mjpeg'
|
export type StreamType = 'webrtc' | 'mse' | 'mp4' | 'hls' | 'mjpeg'
|
||||||
@@ -56,16 +58,15 @@ export const useStreams = () => {
|
|||||||
const error = useState<string | null>('streams_error', () => null)
|
const error = useState<string | null>('streams_error', () => null)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtiene la lista de streams disponibles desde la API
|
* Obtiene la lista de streams disponibles via proxy backend
|
||||||
*/
|
*/
|
||||||
const fetchStreams = async (): Promise<void> => {
|
const fetchStreams = async (): Promise<void> => {
|
||||||
isLoading.value = true
|
isLoading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await $fetch<Record<string, unknown>>(`${BASE_URL}/api/streams`, {
|
// Usar proxy backend para evitar CORS/cookies issues
|
||||||
credentials: 'include'
|
const response = await $fetch<Record<string, unknown>>('/api/streams/list')
|
||||||
})
|
|
||||||
|
|
||||||
// Extraer nombres de streams del objeto
|
// Extraer nombres de streams del objeto
|
||||||
streams.value = Object.keys(response).sort()
|
streams.value = Object.keys(response).sort()
|
||||||
|
|||||||
99
nuxt4/server/api/frigate/event.post.ts
Normal file
99
nuxt4/server/api/frigate/event.post.ts
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
/**
|
||||||
|
* Proxy endpoint para crear eventos en Frigate
|
||||||
|
* POST /api/frigate/event
|
||||||
|
* Body: { camera: string, label: string, sub_label?: string, duration?: number, include_recording?: boolean }
|
||||||
|
*/
|
||||||
|
|
||||||
|
interface EventRequestBody {
|
||||||
|
camera: string
|
||||||
|
label: string
|
||||||
|
sub_label?: string
|
||||||
|
duration?: number
|
||||||
|
include_recording?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
// Verificar autenticación via headers de Authentik
|
||||||
|
const headers = getRequestHeaders(event)
|
||||||
|
const username = headers['x-authentik-username']
|
||||||
|
|
||||||
|
if (!username) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
message: 'No autenticado'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leer body
|
||||||
|
const body = await readBody<EventRequestBody>(event)
|
||||||
|
|
||||||
|
if (!body.camera || !body.label) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 400,
|
||||||
|
message: 'Se requiere camera y label'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extraer nombre base de la cámara (sin _main o _sub)
|
||||||
|
const cameraName = body.camera.replace(/_main$|_sub$/, '')
|
||||||
|
|
||||||
|
// Preparar payload para Frigate
|
||||||
|
const frigatePayload: Record<string, unknown> = {
|
||||||
|
label: body.label
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body.sub_label) {
|
||||||
|
frigatePayload.sub_label = body.sub_label
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body.duration) {
|
||||||
|
frigatePayload.duration = body.duration
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body.include_recording !== undefined) {
|
||||||
|
frigatePayload.include_recording = body.include_recording
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
`https://camaras.nucleoriofrio.com/api/events/${cameraName}/create`,
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(frigatePayload)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text()
|
||||||
|
console.error('[Frigate Proxy] Error response:', errorText)
|
||||||
|
throw createError({
|
||||||
|
statusCode: response.status,
|
||||||
|
message: `Error al crear evento: ${response.statusText}`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json()
|
||||||
|
|
||||||
|
console.log(`[Frigate Proxy] Evento creado por ${username}: ${body.label} en ${cameraName}`)
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
...data
|
||||||
|
}
|
||||||
|
} catch (error: unknown) {
|
||||||
|
console.error('[Frigate Proxy] Error:', error)
|
||||||
|
|
||||||
|
if ((error as any).statusCode) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: (error as Error)?.message || 'Error al crear evento'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
48
nuxt4/server/api/streams/list.get.ts
Normal file
48
nuxt4/server/api/streams/list.get.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/**
|
||||||
|
* Proxy endpoint para obtener la lista de streams de go2rtc
|
||||||
|
* Evita problemas de CORS/cookies entre subdominios
|
||||||
|
*/
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
// Verificar autenticación via headers de Authentik
|
||||||
|
const headers = getRequestHeaders(event)
|
||||||
|
const username = headers['x-authentik-username']
|
||||||
|
|
||||||
|
if (!username) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
message: 'No autenticado'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('https://streams.nucleoriofrio.com/api/streams', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: response.status,
|
||||||
|
message: `Error al obtener streams: ${response.statusText}`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json()
|
||||||
|
|
||||||
|
return data
|
||||||
|
} catch (error: unknown) {
|
||||||
|
console.error('[Streams Proxy] Error:', error)
|
||||||
|
|
||||||
|
if ((error as any).statusCode) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: (error as Error)?.message || 'Error al obtener streams'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user