mirror of
https://github.com/ngthanhvu/kr-phim.git
synced 2026-08-10 13:27:46 +00:00
feat: add HLS proxy, view tracking, and player improvements
- Add HLS proxy middleware for CORS bypass - Add view tracking API endpoint - Improve HLS player with proxy fallback mechanism - Add custom episode support in admin - Update admin movie list with view count - Add description expand/collapse on movie detail - Fix hydration warnings in watch page
This commit is contained in:
@@ -76,6 +76,7 @@ function mapMovieToResponse(movie: any) {
|
||||
lang: movie.lang || undefined,
|
||||
type: movie.type || undefined,
|
||||
rating: movie.rating || undefined,
|
||||
views: movie.views || 0,
|
||||
categories: movie.categories || [],
|
||||
countries: movie.countries || [],
|
||||
sources: movie.sources || [],
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { movies } from '../../../database/schema'
|
||||
import { eq, and, sql } from 'drizzle-orm'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const slug = getRouterParam(event, 'slug')
|
||||
if (!slug) {
|
||||
throw createError({ statusCode: 400, message: 'Thiếu slug phim' })
|
||||
}
|
||||
|
||||
const query = getQuery(event)
|
||||
const source = typeof query.source === 'string' ? query.source : ''
|
||||
|
||||
const db = useDb()
|
||||
|
||||
const whereClause = source
|
||||
? and(eq(movies.slug, slug), eq(movies.source, source), eq(movies.active, true))
|
||||
: and(eq(movies.slug, slug), eq(movies.active, true))
|
||||
|
||||
await db
|
||||
.update(movies)
|
||||
.set({ views: sql`${movies.views} + 1` })
|
||||
.where(whereClause)
|
||||
|
||||
return { success: true }
|
||||
})
|
||||
@@ -0,0 +1,83 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
const url = body.url as string
|
||||
|
||||
if (!url) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Missing url parameter',
|
||||
})
|
||||
}
|
||||
|
||||
// Validate URL to prevent SSRF
|
||||
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Invalid URL protocol',
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
'Accept': '*/*',
|
||||
'Referer': new URL(url).origin,
|
||||
'Origin': new URL(url).origin,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw createError({
|
||||
statusCode: response.status,
|
||||
message: `Upstream error: ${response.statusText}`,
|
||||
})
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || 'application/octet-stream'
|
||||
const isM3u8 = url.includes('.m3u8') || contentType.includes('mpegurl')
|
||||
|
||||
setHeader(event, 'access-control-allow-origin', '*')
|
||||
setHeader(event, 'access-control-allow-methods', 'POST, OPTIONS')
|
||||
setHeader(event, 'cache-control', 'public, max-age=3600')
|
||||
|
||||
// For m3u8 files, rewrite relative URLs to use proxy
|
||||
if (isM3u8) {
|
||||
let body = await response.text()
|
||||
const baseUrl = url.substring(0, url.lastIndexOf('/') + 1)
|
||||
|
||||
// Rewrite absolute URLs
|
||||
body = body.replace(/^(https?:\/\/[^\s]+)$/gm, (match) => {
|
||||
const encoded = Buffer.from(match).toString('base64')
|
||||
return `/api/proxy-m3u8/${encoded}`
|
||||
})
|
||||
|
||||
// Rewrite relative URLs (not starting with http/https or /api/proxy-m3u8/)
|
||||
body = body.replace(/^([^#\s][^\n]*)$/gm, (match) => {
|
||||
if (match.startsWith('http://') || match.startsWith('https://') || match.startsWith('/api/proxy-m3u8/')) {
|
||||
return match
|
||||
}
|
||||
const absoluteUrl = new URL(match, baseUrl).href
|
||||
const encoded = Buffer.from(absoluteUrl).toString('base64')
|
||||
return `/api/proxy-m3u8/${encoded}`
|
||||
})
|
||||
|
||||
setHeader(event, 'content-type', 'application/vnd.apple.mpegurl')
|
||||
return body
|
||||
} else {
|
||||
// For .ts files and other binary data, stream directly
|
||||
setHeader(event, 'content-type', contentType)
|
||||
const contentLength = response.headers.get('content-length')
|
||||
if (contentLength) {
|
||||
setHeader(event, 'content-length', contentLength)
|
||||
}
|
||||
return response.body
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.statusCode) throw error
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: `Proxy error: ${error.message}`,
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,77 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
// Get base64 encoded URL from path after /api/proxy-m3u8/
|
||||
const path = event.path || event.node?.req?.url || ''
|
||||
const base64Url = path.replace('/api/proxy-m3u8/', '')
|
||||
|
||||
if (!base64Url) {
|
||||
throw createError({ statusCode: 400, message: 'Missing url parameter' })
|
||||
}
|
||||
|
||||
// Decode URL from base64
|
||||
let url: string
|
||||
try {
|
||||
url = Buffer.from(base64Url, 'base64').toString('utf-8')
|
||||
} catch {
|
||||
throw createError({ statusCode: 400, message: 'Invalid URL encoding' })
|
||||
}
|
||||
|
||||
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
||||
throw createError({ statusCode: 400, message: 'Invalid URL protocol' })
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
'Accept': '*/*',
|
||||
'Referer': new URL(url).origin,
|
||||
'Origin': new URL(url).origin,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw createError({ statusCode: response.status, message: `Upstream error: ${response.statusText}` })
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || 'application/octet-stream'
|
||||
const isM3u8 = url.includes('.m3u8') || contentType.includes('mpegurl')
|
||||
|
||||
setHeader(event, 'access-control-allow-origin', '*')
|
||||
setHeader(event, 'access-control-allow-methods', 'GET, OPTIONS')
|
||||
setHeader(event, 'cache-control', 'public, max-age=3600')
|
||||
|
||||
if (isM3u8) {
|
||||
let body = await response.text()
|
||||
const baseUrl = url.substring(0, url.lastIndexOf('/') + 1)
|
||||
|
||||
// Rewrite absolute URLs
|
||||
body = body.replace(/^(https?:\/\/[^\s]+)$/gm, (match) => {
|
||||
const encoded = Buffer.from(match).toString('base64')
|
||||
return `/api/proxy-m3u8/${encoded}`
|
||||
})
|
||||
|
||||
// Rewrite relative URLs
|
||||
body = body.replace(/^([^#\s][^\n]*)$/gm, (match) => {
|
||||
if (match.startsWith('http://') || match.startsWith('https://') || match.startsWith('/api/proxy-m3u8/')) {
|
||||
return match
|
||||
}
|
||||
const absoluteUrl = new URL(match, baseUrl).href
|
||||
const encoded = Buffer.from(absoluteUrl).toString('base64')
|
||||
return `/api/proxy-m3u8/${encoded}`
|
||||
})
|
||||
|
||||
setHeader(event, 'content-type', 'application/vnd.apple.mpegurl')
|
||||
return body
|
||||
} else {
|
||||
setHeader(event, 'content-type', contentType)
|
||||
const contentLength = response.headers.get('content-length')
|
||||
if (contentLength) {
|
||||
setHeader(event, 'content-length', contentLength)
|
||||
}
|
||||
return response.body
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.statusCode) throw error
|
||||
throw createError({ statusCode: 500, message: `Proxy error: ${error.message}` })
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE `movies` ADD `views` int DEFAULT 0 NOT NULL;
|
||||
@@ -0,0 +1,329 @@
|
||||
{
|
||||
"version": "5",
|
||||
"dialect": "mysql",
|
||||
"id": "12998654-722b-4627-9970-1f407a7d7edd",
|
||||
"prevId": "39aa52ed-af36-43ed-ab3e-2222785d1c00",
|
||||
"tables": {
|
||||
"movies": {
|
||||
"name": "movies",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "int",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": true
|
||||
},
|
||||
"source": {
|
||||
"name": "source",
|
||||
"type": "varchar(50)",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"slug": {
|
||||
"name": "slug",
|
||||
"type": "varchar(500)",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "varchar(500)",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"origin_name": {
|
||||
"name": "origin_name",
|
||||
"type": "varchar(500)",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"thumb": {
|
||||
"name": "thumb",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"poster": {
|
||||
"name": "poster",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"year": {
|
||||
"name": "year",
|
||||
"type": "int",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"time": {
|
||||
"name": "time",
|
||||
"type": "varchar(100)",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"episode": {
|
||||
"name": "episode",
|
||||
"type": "varchar(100)",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"quality": {
|
||||
"name": "quality",
|
||||
"type": "varchar(50)",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"lang": {
|
||||
"name": "lang",
|
||||
"type": "varchar(50)",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"type": {
|
||||
"name": "type",
|
||||
"type": "varchar(50)",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"rating": {
|
||||
"name": "rating",
|
||||
"type": "int",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"views": {
|
||||
"name": "views",
|
||||
"type": "int",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": 0
|
||||
},
|
||||
"content": {
|
||||
"name": "content",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"categories": {
|
||||
"name": "categories",
|
||||
"type": "json",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"countries": {
|
||||
"name": "countries",
|
||||
"type": "json",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"sources": {
|
||||
"name": "sources",
|
||||
"type": "json",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"custom_poster": {
|
||||
"name": "custom_poster",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"custom_thumb": {
|
||||
"name": "custom_thumb",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"custom_content": {
|
||||
"name": "custom_content",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"custom_episodes": {
|
||||
"name": "custom_episodes",
|
||||
"type": "json",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"active": {
|
||||
"name": "active",
|
||||
"type": "boolean",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": false
|
||||
},
|
||||
"api_updated_at": {
|
||||
"name": "api_updated_at",
|
||||
"type": "timestamp",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"synced_at": {
|
||||
"name": "synced_at",
|
||||
"type": "timestamp",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "(now())"
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "timestamp",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "(now())"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "timestamp",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"onUpdate": true,
|
||||
"default": "(now())"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {
|
||||
"movies_id": {
|
||||
"name": "movies_id",
|
||||
"columns": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
},
|
||||
"uniqueConstraints": {},
|
||||
"checkConstraint": {}
|
||||
},
|
||||
"users": {
|
||||
"name": "users",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "int",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": true
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "varchar(200)",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"email": {
|
||||
"name": "email",
|
||||
"type": "varchar(255)",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"password": {
|
||||
"name": "password",
|
||||
"type": "varchar(255)",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"role": {
|
||||
"name": "role",
|
||||
"type": "varchar(50)",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "'user'"
|
||||
},
|
||||
"avatar": {
|
||||
"name": "avatar",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"active": {
|
||||
"name": "active",
|
||||
"type": "boolean",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": true
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "timestamp",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "(now())"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "timestamp",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"onUpdate": true,
|
||||
"default": "(now())"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {
|
||||
"users_id": {
|
||||
"name": "users_id",
|
||||
"columns": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
},
|
||||
"uniqueConstraints": {
|
||||
"users_email_unique": {
|
||||
"name": "users_email_unique",
|
||||
"columns": [
|
||||
"email"
|
||||
]
|
||||
}
|
||||
},
|
||||
"checkConstraint": {}
|
||||
}
|
||||
},
|
||||
"views": {},
|
||||
"_meta": {
|
||||
"schemas": {},
|
||||
"tables": {},
|
||||
"columns": {}
|
||||
},
|
||||
"internal": {
|
||||
"tables": {},
|
||||
"indexes": {}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,13 @@
|
||||
"when": 1784707891329,
|
||||
"tag": "0004_opposite_sasquatch",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 5,
|
||||
"version": "5",
|
||||
"when": 1784709136940,
|
||||
"tag": "0005_illegal_purple_man",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -27,6 +27,7 @@ export const movies = mysqlTable('movies', {
|
||||
lang: varchar('lang', { length: 50 }),
|
||||
type: varchar('type', { length: 50 }),
|
||||
rating: int('rating'),
|
||||
views: int('views').notNull().default(0),
|
||||
content: text('content'),
|
||||
categories: json('categories').$type<string[]>(),
|
||||
countries: json('countries').$type<string[]>(),
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const path = event.path || event.node?.req?.url || ''
|
||||
|
||||
// Only handle /api/proxy-m3u8/ paths
|
||||
if (!path.startsWith('/api/proxy-m3u8/')) {
|
||||
return
|
||||
}
|
||||
|
||||
const base64Url = path.replace('/api/proxy-m3u8/', '')
|
||||
|
||||
if (!base64Url) {
|
||||
throw createError({ statusCode: 400, message: 'Missing url parameter' })
|
||||
}
|
||||
|
||||
// Decode URL from base64
|
||||
let url: string
|
||||
try {
|
||||
url = Buffer.from(base64Url, 'base64').toString('utf-8')
|
||||
} catch {
|
||||
throw createError({ statusCode: 400, message: 'Invalid URL encoding' })
|
||||
}
|
||||
|
||||
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
||||
throw createError({ statusCode: 400, message: 'Invalid URL protocol' })
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
'Accept': '*/*',
|
||||
'Referer': new URL(url).origin,
|
||||
'Origin': new URL(url).origin,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw createError({ statusCode: response.status, message: `Upstream error: ${response.statusText}` })
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || 'application/octet-stream'
|
||||
const isM3u8 = url.includes('.m3u8') || contentType.includes('mpegurl')
|
||||
|
||||
setHeader(event, 'access-control-allow-origin', '*')
|
||||
setHeader(event, 'access-control-allow-methods', 'GET, OPTIONS')
|
||||
setHeader(event, 'cache-control', 'public, max-age=3600')
|
||||
|
||||
if (isM3u8) {
|
||||
let body = await response.text()
|
||||
const baseUrl = url.substring(0, url.lastIndexOf('/') + 1)
|
||||
|
||||
body = body.replace(/^(https?:\/\/[^\s]+)$/gm, (match) => {
|
||||
const encoded = Buffer.from(match).toString('base64')
|
||||
return `/api/proxy-m3u8/${encoded}`
|
||||
})
|
||||
|
||||
body = body.replace(/^([^#\s][^\n]*)$/gm, (match) => {
|
||||
if (match.startsWith('http://') || match.startsWith('https://') || match.startsWith('/api/proxy-m3u8/')) {
|
||||
return match
|
||||
}
|
||||
const absoluteUrl = new URL(match, baseUrl).href
|
||||
const encoded = Buffer.from(absoluteUrl).toString('base64')
|
||||
return `/api/proxy-m3u8/${encoded}`
|
||||
})
|
||||
|
||||
setHeader(event, 'content-type', 'application/vnd.apple.mpegurl')
|
||||
return body
|
||||
} else {
|
||||
setHeader(event, 'content-type', contentType)
|
||||
const contentLength = response.headers.get('content-length')
|
||||
if (contentLength) {
|
||||
setHeader(event, 'content-length', contentLength)
|
||||
}
|
||||
return response.body
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.statusCode) throw error
|
||||
throw createError({ statusCode: 500, message: `Proxy error: ${error.message}` })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user